]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-notify-manager.c
Use a flat namespace for internal includes
[empathy.git] / libempathy-gtk / empathy-notify-manager.c
1 /* * Copyright (C) 2009 Collabora Ltd.
2  *
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 2.1 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
16  *
17  * Authors: Guillaume Desmottes <guillaume.desmottes@collabora.co.uk>
18  */
19
20 #include "config.h"
21
22 #include <libnotify/notify.h>
23
24 #include "empathy-gsettings.h"
25 #include "empathy-utils.h"
26
27 #include "empathy-ui-utils.h"
28
29 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
30 #include "empathy-debug.h"
31
32 #include "empathy-notify-manager.h"
33
34 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyNotifyManager)
35
36 typedef struct
37 {
38   /* owned (gchar *) => TRUE */
39   GHashTable *capabilities;
40   TpAccountManager *account_manager;
41   GSettings *gsettings_notif;
42 } EmpathyNotifyManagerPriv;
43
44 G_DEFINE_TYPE (EmpathyNotifyManager, empathy_notify_manager, G_TYPE_OBJECT);
45
46 static EmpathyNotifyManager *notify_manager = NULL;
47
48 static GObject *
49 notify_manager_constructor (GType type,
50     guint n_construct_params,
51     GObjectConstructParam *construct_params)
52 {
53   GObject *retval;
54
55   if (notify_manager != NULL)
56     return g_object_ref (notify_manager);
57
58   retval = G_OBJECT_CLASS (empathy_notify_manager_parent_class)->constructor
59     (type, n_construct_params, construct_params);
60
61   notify_manager = EMPATHY_NOTIFY_MANAGER (retval);
62   g_object_add_weak_pointer (retval, (gpointer) &notify_manager);
63
64   return retval;
65 }
66
67 static void
68 notify_manager_dispose (GObject *object)
69 {
70   EmpathyNotifyManagerPriv *priv = GET_PRIV (object);
71
72   if (priv->account_manager != NULL)
73     {
74       g_object_unref (priv->account_manager);
75       priv->account_manager = NULL;
76     }
77
78   tp_clear_object (&priv->gsettings_notif);
79
80   G_OBJECT_CLASS (empathy_notify_manager_parent_class)->dispose (object);
81 }
82
83 static void
84 notify_manager_finalize (GObject *object)
85 {
86   EmpathyNotifyManagerPriv *priv = GET_PRIV (object);
87
88   g_hash_table_unref (priv->capabilities);
89
90   G_OBJECT_CLASS (empathy_notify_manager_parent_class)->finalize (object);
91 }
92
93 static void
94 empathy_notify_manager_class_init (EmpathyNotifyManagerClass *klass)
95 {
96   GObjectClass *object_class = G_OBJECT_CLASS (klass);
97
98   object_class->dispose = notify_manager_dispose;
99   object_class->finalize = notify_manager_finalize;
100   object_class->constructor = notify_manager_constructor;
101
102   g_type_class_add_private (object_class, sizeof (EmpathyNotifyManagerPriv));
103 }
104
105 static void
106 account_manager_prepared_cb (GObject *source_object,
107     GAsyncResult *result,
108     gpointer user_data)
109 {
110   TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
111   GError *error = NULL;
112
113   if (!tp_proxy_prepare_finish (account_manager, result, &error))
114     {
115       DEBUG ("Failed to prepare account manager: %s", error->message);
116       g_error_free (error);
117       return;
118     }
119 }
120
121 static void
122 empathy_notify_manager_init (EmpathyNotifyManager *self)
123 {
124   EmpathyNotifyManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
125     EMPATHY_TYPE_NOTIFY_MANAGER, EmpathyNotifyManagerPriv);
126   GList *list, *l;
127
128   self->priv = priv;
129
130   priv->gsettings_notif = g_settings_new (EMPATHY_PREFS_NOTIFICATIONS_SCHEMA);
131
132   priv->capabilities = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
133       NULL);
134
135   /* fetch capabilities */
136   list = notify_get_server_caps ();
137   for (l = list; l != NULL; l = g_list_next (l))
138     {
139       gchar *cap = l->data;
140
141       DEBUG ("add capability: %s", cap);
142       /* owernship of the string is transfered to the hash table */
143       g_hash_table_insert (priv->capabilities, cap, GUINT_TO_POINTER (TRUE));
144     }
145   g_list_free (list);
146
147   priv->account_manager = tp_account_manager_dup ();
148
149   tp_proxy_prepare_async (priv->account_manager, NULL,
150       account_manager_prepared_cb, self);
151 }
152
153 EmpathyNotifyManager *
154 empathy_notify_manager_dup_singleton (void)
155 {
156   return g_object_new (EMPATHY_TYPE_NOTIFY_MANAGER, NULL);
157 }
158
159 gboolean
160 empathy_notify_manager_has_capability (EmpathyNotifyManager *self,
161     const gchar *cap)
162 {
163   EmpathyNotifyManagerPriv *priv = GET_PRIV (self);
164
165   return g_hash_table_lookup (priv->capabilities, cap) != NULL;
166 }
167
168 GdkPixbuf *
169 empathy_notify_manager_get_pixbuf_for_notification (EmpathyNotifyManager *self,
170     EmpathyContact *contact,
171     const char *icon_name)
172 {
173   GdkPixbuf *pixbuf = NULL;
174
175   if (contact != NULL)
176     pixbuf = empathy_pixbuf_avatar_from_contact_scaled (contact, 48, 48);
177
178   if (pixbuf == NULL)
179     pixbuf = empathy_pixbuf_from_icon_name_sized (icon_name, 48);
180
181   return pixbuf;
182 }
183
184 gboolean
185 empathy_notify_manager_notification_is_enabled  (EmpathyNotifyManager *self)
186 {
187   EmpathyNotifyManagerPriv *priv = GET_PRIV (self);
188   TpConnectionPresenceType presence;
189
190   if (!g_settings_get_boolean (priv->gsettings_notif,
191         EMPATHY_PREFS_NOTIFICATIONS_ENABLED))
192     return FALSE;
193
194   if (!tp_account_manager_is_prepared (priv->account_manager,
195         TP_ACCOUNT_MANAGER_FEATURE_CORE))
196     {
197       DEBUG ("account manager is not ready yet; display the notification");
198       return TRUE;
199     }
200
201   presence = tp_account_manager_get_most_available_presence (
202       priv->account_manager,
203       NULL, NULL);
204
205   if (presence != TP_CONNECTION_PRESENCE_TYPE_AVAILABLE &&
206       presence != TP_CONNECTION_PRESENCE_TYPE_UNSET)
207     {
208       if (g_settings_get_boolean (priv->gsettings_notif,
209             EMPATHY_PREFS_NOTIFICATIONS_DISABLED_AWAY))
210         return FALSE;
211     }
212
213   return TRUE;
214 }
215
216 NotifyNotification *
217 empathy_notify_manager_create_notification (const gchar *summary,
218     const char *body,
219     const gchar *icon)
220 {
221   NotifyNotification *notification;
222
223   notification = notify_notification_new (summary, body, icon);
224
225   notify_notification_set_hint (notification,
226       EMPATHY_NOTIFY_MANAGER_CAP_DESKTOP_ENTRY,
227       g_variant_new_string ("empathy"));
228
229   return notification;
230 }