]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-notify-manager.c
Merge commit 'staz/dnd'
[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 #include <string.h>
22
23 #include <libnotify/notification.h>
24 #include <libnotify/notify.h>
25
26 #include <telepathy-glib/account-manager.h>
27
28 #include <libempathy/empathy-utils.h>
29
30 #include <libempathy-gtk/empathy-ui-utils.h>
31 #include <libempathy-gtk/empathy-conf.h>
32
33 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
34 #include <libempathy/empathy-debug.h>
35
36 #include "empathy-notify-manager.h"
37
38 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyNotifyManager)
39
40 typedef struct
41 {
42   /* owned (gchar *) => TRUE */
43   GHashTable *capabilities;
44   TpAccountManager *account_manager;
45 } EmpathyNotifyManagerPriv;
46
47 G_DEFINE_TYPE (EmpathyNotifyManager, empathy_notify_manager, G_TYPE_OBJECT);
48
49 static EmpathyNotifyManager *notify_manager = NULL;
50
51 static GObject *
52 notify_manager_constructor (GType type,
53     guint n_construct_params,
54     GObjectConstructParam *construct_params)
55 {
56   GObject *retval;
57
58   if (notify_manager != NULL)
59     return g_object_ref (notify_manager);
60
61   retval = G_OBJECT_CLASS (empathy_notify_manager_parent_class)->constructor
62     (type, n_construct_params, construct_params);
63
64   notify_manager = EMPATHY_NOTIFY_MANAGER (retval);
65   g_object_add_weak_pointer (retval, (gpointer) &notify_manager);
66
67   return retval;
68 }
69
70 static void
71 notify_manager_dispose (GObject *object)
72 {
73   EmpathyNotifyManagerPriv *priv = GET_PRIV (object);
74
75   if (priv->account_manager != NULL)
76     {
77       g_object_unref (priv->account_manager);
78       priv->account_manager = NULL;
79     }
80
81   G_OBJECT_CLASS (empathy_notify_manager_parent_class)->dispose (object);
82 }
83
84 static void
85 notify_manager_finalize (GObject *object)
86 {
87   EmpathyNotifyManagerPriv *priv = GET_PRIV (object);
88
89   g_hash_table_destroy (priv->capabilities);
90
91   G_OBJECT_CLASS (empathy_notify_manager_parent_class)->finalize (object);
92 }
93
94 static void
95 empathy_notify_manager_class_init (EmpathyNotifyManagerClass *klass)
96 {
97   GObjectClass *object_class = G_OBJECT_CLASS (klass);
98
99   object_class->dispose = notify_manager_dispose;
100   object_class->finalize = notify_manager_finalize;
101   object_class->constructor = notify_manager_constructor;
102
103   g_type_class_add_private (object_class, sizeof (EmpathyNotifyManagerPriv));
104 }
105
106 static void
107 account_manager_prepared_cb (GObject *source_object,
108     GAsyncResult *result,
109     gpointer user_data)
110 {
111   TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
112   GError *error = NULL;
113
114   if (!tp_account_manager_prepare_finish (account_manager, result, &error))
115     {
116       DEBUG ("Failed to prepare account manager: %s", error->message);
117       g_error_free (error);
118       return;
119     }
120 }
121
122 static void
123 empathy_notify_manager_init (EmpathyNotifyManager *self)
124 {
125   EmpathyNotifyManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
126     EMPATHY_TYPE_NOTIFY_MANAGER, EmpathyNotifyManagerPriv);
127   GList *list, *l;
128
129   self->priv = priv;
130
131   priv->capabilities = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
132       NULL);
133
134   /* fetch capabilities */
135   list = notify_get_server_caps ();
136   for (l = list; l != NULL; l = g_list_next (l))
137     {
138       gchar *cap = l->data;
139
140       DEBUG ("add capability: %s", cap);
141       /* owernship of the string is transfered to the hash table */
142       g_hash_table_insert (priv->capabilities, cap, GUINT_TO_POINTER (TRUE));
143     }
144   g_list_free (list);
145
146   priv->account_manager = tp_account_manager_dup ();
147
148   tp_account_manager_prepare_async (priv->account_manager, NULL,
149       account_manager_prepared_cb, self);
150 }
151
152 EmpathyNotifyManager *
153 empathy_notify_manager_dup_singleton (void)
154 {
155   return g_object_new (EMPATHY_TYPE_NOTIFY_MANAGER, NULL);
156 }
157
158 gboolean
159 empathy_notify_manager_has_capability (EmpathyNotifyManager *self,
160     const gchar *cap)
161 {
162   EmpathyNotifyManagerPriv *priv = GET_PRIV (self);
163
164   return g_hash_table_lookup (priv->capabilities, cap) != NULL;
165 }
166
167 GdkPixbuf *
168 empathy_notify_manager_get_pixbuf_for_notification (EmpathyNotifyManager *self,
169     EmpathyContact *contact,
170     const char *icon_name)
171 {
172   GdkPixbuf *pixbuf = NULL;
173
174   if (contact != NULL)
175     pixbuf = empathy_pixbuf_avatar_from_contact_scaled (contact, 48, 48);
176
177   if (pixbuf == NULL)
178     pixbuf = empathy_pixbuf_from_icon_name_sized (icon_name, 48);
179
180   return pixbuf;
181 }
182
183 gboolean
184 empathy_notify_manager_notification_is_enabled  (EmpathyNotifyManager *self)
185 {
186   EmpathyNotifyManagerPriv *priv = GET_PRIV (self);
187   EmpathyConf *conf;
188   gboolean res;
189   TpConnectionPresenceType presence;
190
191   conf = empathy_conf_get ();
192   res = FALSE;
193
194   empathy_conf_get_bool (conf, EMPATHY_PREFS_NOTIFICATIONS_ENABLED, &res);
195
196   if (!res)
197     return FALSE;
198
199   if (!tp_account_manager_is_prepared (priv->account_manager,
200         TP_ACCOUNT_MANAGER_FEATURE_CORE))
201     {
202       DEBUG ("account manager is not ready yet; display the notification");
203       return TRUE;
204     }
205
206   presence = tp_account_manager_get_most_available_presence (
207       priv->account_manager,
208       NULL, NULL);
209
210   if (presence != TP_CONNECTION_PRESENCE_TYPE_AVAILABLE &&
211       presence != TP_CONNECTION_PRESENCE_TYPE_UNSET)
212     {
213       empathy_conf_get_bool (conf, EMPATHY_PREFS_NOTIFICATIONS_DISABLED_AWAY,
214           &res);
215
216       if (res)
217         return FALSE;
218     }
219
220   return TRUE;
221 }