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