]> git.0d.be Git - empathy.git/blob - src/empathy-notifications-approver.c
set a not empty string when setting EMPATHY_NOTIFY_MANAGER_CAP_X_CANONICAL_APPEND
[empathy.git] / src / empathy-notifications-approver.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 <glib/gi18n.h>
24 #include <libnotify/notification.h>
25 #include <libnotify/notify.h>
26 #include <telepathy-glib/telepathy-glib.h>
27
28 #include <libempathy/empathy-contact-manager.h>
29
30 #include <libempathy-gtk/empathy-notify-manager.h>
31
32 #include "empathy-event-manager.h"
33
34 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
35 #include <libempathy/empathy-debug.h>
36
37 #include "empathy-notifications-approver.h"
38
39 struct _EmpathyNotificationsApproverPrivate
40 {
41   EmpathyEventManager *event_mgr;
42   EmpathyNotifyManager *notify_mgr;
43
44   NotifyNotification *notification;
45   EmpathyEvent *event;
46 };
47
48 G_DEFINE_TYPE (EmpathyNotificationsApprover, empathy_notifications_approver,
49     G_TYPE_OBJECT);
50
51 static EmpathyNotificationsApprover *notifications_approver = NULL;
52
53 static GObject *
54 notifications_approver_constructor (GType type,
55     guint n_construct_params,
56     GObjectConstructParam *construct_params)
57 {
58   GObject *retval;
59
60   if (notifications_approver != NULL)
61     return g_object_ref (notifications_approver);
62
63   retval = G_OBJECT_CLASS (empathy_notifications_approver_parent_class)->
64       constructor (type, n_construct_params, construct_params);
65
66   notifications_approver = EMPATHY_NOTIFICATIONS_APPROVER (retval);
67   g_object_add_weak_pointer (retval, (gpointer) &notifications_approver);
68
69   return retval;
70 }
71
72 static void
73 notifications_approver_dispose (GObject *object)
74 {
75   EmpathyNotificationsApprover *self = (EmpathyNotificationsApprover *) object;
76
77   tp_clear_object (&self->priv->event_mgr);
78   tp_clear_object (&self->priv->notify_mgr);
79
80   if (self->priv->notification != NULL)
81     {
82       notify_notification_close (self->priv->notification, NULL);
83       tp_clear_object (&self->priv->notification);
84     }
85
86   G_OBJECT_CLASS (empathy_notifications_approver_parent_class)->dispose (
87       object);
88 }
89
90 static void
91 empathy_notifications_approver_class_init (
92     EmpathyNotificationsApproverClass *klass)
93 {
94   GObjectClass *object_class = G_OBJECT_CLASS (klass);
95
96   object_class->dispose = notifications_approver_dispose;
97   object_class->constructor = notifications_approver_constructor;
98
99   g_type_class_add_private (object_class,
100       sizeof (EmpathyNotificationsApproverPrivate));
101 }
102
103 static void
104 notification_closed_cb (NotifyNotification *notification,
105     EmpathyNotificationsApprover *self)
106 {
107   if (self->priv->notification == notification)
108     tp_clear_object (&self->priv->notification);
109 }
110
111 static void
112 notification_close_helper (EmpathyNotificationsApprover *self)
113 {
114   if (self->priv->notification != NULL)
115     {
116       notify_notification_close (self->priv->notification, NULL);
117       tp_clear_object (&self->priv->notification);
118     }
119 }
120
121 static void
122 notification_approve_cb (NotifyNotification *notification,
123     gchar *action,
124     EmpathyNotificationsApprover *self)
125 {
126   if (self->priv->event != NULL)
127     empathy_event_approve (self->priv->event);
128 }
129
130 static void
131 notification_decline_cb (NotifyNotification *notification,
132     gchar *action,
133     EmpathyNotificationsApprover  *self)
134 {
135   if (self->priv->event != NULL)
136     empathy_event_decline (self->priv->event);
137 }
138
139 static void
140 notification_decline_subscription_cb (NotifyNotification *notification,
141     gchar *action,
142     EmpathyNotificationsApprover *self)
143 {
144   EmpathyContactManager *manager;
145
146   if (self->priv->event == NULL)
147     return;
148
149   manager = empathy_contact_manager_dup_singleton ();
150   empathy_contact_list_remove (EMPATHY_CONTACT_LIST (manager),
151       self->priv->event->contact, "");
152
153   empathy_event_remove (self->priv->event);
154
155   g_object_unref (manager);
156 }
157
158 static void
159 notification_accept_subscription_cb (NotifyNotification *notification,
160     gchar *action,
161     EmpathyNotificationsApprover *self)
162 {
163   EmpathyContactManager *manager;
164
165   if (self->priv->event == NULL)
166     return;
167
168   manager = empathy_contact_manager_dup_singleton ();
169   empathy_contact_list_add (EMPATHY_CONTACT_LIST (manager),
170       self->priv->event->contact, "");
171
172   empathy_event_remove (self->priv->event);
173
174   g_object_unref (manager);
175 }
176
177 static void
178 add_notification_actions (EmpathyNotificationsApprover *self,
179     NotifyNotification *notification)
180 {
181   switch (self->priv->event->type) {
182     case EMPATHY_EVENT_TYPE_CHAT:
183       notify_notification_add_action (notification,
184         "respond", _("Respond"), (NotifyActionCallback) notification_approve_cb,
185           self, NULL);
186       break;
187
188     case EMPATHY_EVENT_TYPE_VOIP:
189       notify_notification_add_action (notification,
190         "reject", _("Reject"), (NotifyActionCallback) notification_decline_cb,
191           self, NULL);
192
193       notify_notification_add_action (notification,
194         "answer", _("Answer"), (NotifyActionCallback) notification_approve_cb,
195           self, NULL);
196       break;
197
198     case EMPATHY_EVENT_TYPE_TRANSFER:
199     case EMPATHY_EVENT_TYPE_INVITATION:
200       notify_notification_add_action (notification,
201         "decline", _("Decline"), (NotifyActionCallback) notification_decline_cb,
202           self, NULL);
203
204       notify_notification_add_action (notification,
205         "accept", _("Accept"), (NotifyActionCallback) notification_approve_cb,
206           self, NULL);
207       break;
208
209     case EMPATHY_EVENT_TYPE_SUBSCRIPTION:
210       notify_notification_add_action (notification,
211         "decline", _("Decline"),
212           (NotifyActionCallback) notification_decline_subscription_cb,
213           self, NULL);
214
215       notify_notification_add_action (notification,
216         "accept", _("Accept"),
217           (NotifyActionCallback) notification_accept_subscription_cb,
218           self, NULL);
219       break;
220
221     case EMPATHY_EVENT_TYPE_AUTH:
222       notify_notification_add_action (notification,
223         "provide", _("Provide"), (NotifyActionCallback) notification_approve_cb,
224           self, NULL);
225       break;
226
227     default:
228       break;
229   }
230 }
231
232 static gboolean
233 notification_is_urgent (EmpathyNotificationsApprover *self,
234     NotifyNotification *notification)
235 {
236   /* Mark as urgent all the notifications with which user should
237    * interact ASAP */
238   switch (self->priv->event->type) {
239     case EMPATHY_EVENT_TYPE_CHAT:
240     case EMPATHY_EVENT_TYPE_VOIP:
241     case EMPATHY_EVENT_TYPE_TRANSFER:
242     case EMPATHY_EVENT_TYPE_INVITATION:
243     case EMPATHY_EVENT_TYPE_AUTH:
244       return TRUE;
245
246     case EMPATHY_EVENT_TYPE_SUBSCRIPTION:
247     case EMPATHY_EVENT_TYPE_PRESENCE_ONLINE:
248     case EMPATHY_EVENT_TYPE_PRESENCE_OFFLINE:
249       return FALSE;
250   }
251
252   return FALSE;
253 }
254
255 static const gchar *
256 get_category_for_event_type (EmpathyEventType type)
257 {
258   switch (type) {
259     case EMPATHY_EVENT_TYPE_CHAT:
260       return "im.received";
261     case EMPATHY_EVENT_TYPE_PRESENCE_ONLINE:
262       return "presence.online";
263     case EMPATHY_EVENT_TYPE_PRESENCE_OFFLINE:
264       return "presence.offline";
265     case EMPATHY_EVENT_TYPE_VOIP:
266     case EMPATHY_EVENT_TYPE_TRANSFER:
267     case EMPATHY_EVENT_TYPE_INVITATION:
268     case EMPATHY_EVENT_TYPE_AUTH:
269     case EMPATHY_EVENT_TYPE_SUBSCRIPTION:
270       return NULL;
271   }
272
273   return NULL;
274 }
275
276 static void
277 update_notification (EmpathyNotificationsApprover *self)
278 {
279   GdkPixbuf *pixbuf = NULL;
280   gchar *message_esc = NULL;
281   gboolean has_x_canonical_append;
282   NotifyNotification *notification;
283
284   if (!empathy_notify_manager_notification_is_enabled (self->priv->notify_mgr))
285     {
286       /* always close the notification if this happens */
287       notification_close_helper (self);
288       return;
289     }
290
291   if (self->priv->event == NULL)
292     {
293       notification_close_helper (self);
294       return;
295      }
296
297   if (self->priv->event->message != NULL)
298     message_esc = g_markup_escape_text (self->priv->event->message, -1);
299
300   has_x_canonical_append = empathy_notify_manager_has_capability (
301       self->priv->notify_mgr, EMPATHY_NOTIFY_MANAGER_CAP_X_CANONICAL_APPEND);
302
303   if (self->priv->notification != NULL && ! has_x_canonical_append)
304     {
305       /* if the notification server does NOT supports x-canonical-append, it is
306        * better to not use notify_notification_update to avoid
307        * overwriting the current notification message */
308       notification = g_object_ref (self->priv->notification);
309
310       notify_notification_update (notification,
311           self->priv->event->header, message_esc, NULL);
312     }
313   else
314     {
315       const gchar *category;
316
317       /* if the notification server supports x-canonical-append,
318        * the hint will be added, so that the message from the
319        * just created notification will be automatically appended
320        * to an existing notification with the same title.
321        * In this way the previous message will not be lost: the new
322        * message will appear below it, in the same notification */
323       notification = notify_notification_new (self->priv->event->header,
324            message_esc, NULL);
325
326       if (self->priv->notification == NULL)
327         {
328           self->priv->notification = g_object_ref (notification);
329
330           g_signal_connect (notification, "closed",
331               G_CALLBACK (notification_closed_cb), self);
332         }
333
334       notify_notification_set_timeout (notification,
335           NOTIFY_EXPIRES_DEFAULT);
336
337       if (has_x_canonical_append)
338         /* We have to set a not empty string to keep libnotify happy */
339         notify_notification_set_hint_string (notification,
340             EMPATHY_NOTIFY_MANAGER_CAP_X_CANONICAL_APPEND, "1");
341
342       if (empathy_notify_manager_has_capability (self->priv->notify_mgr,
343             EMPATHY_NOTIFY_MANAGER_CAP_ACTIONS))
344         add_notification_actions (self, notification);
345
346       if (notification_is_urgent (self, notification))
347         notify_notification_set_urgency (notification, NOTIFY_URGENCY_CRITICAL);
348
349       category = get_category_for_event_type (self->priv->event->type);
350       if (category != NULL)
351         notify_notification_set_hint_string (notification,
352             EMPATHY_NOTIFY_MANAGER_CAP_CATEGORY, category);
353     }
354
355   pixbuf = empathy_notify_manager_get_pixbuf_for_notification (
356       self->priv->notify_mgr, self->priv->event->contact,
357       self->priv->event->icon_name);
358
359   if (pixbuf != NULL)
360     {
361       notify_notification_set_icon_from_pixbuf (notification, pixbuf);
362       g_object_unref (pixbuf);
363     }
364
365   notify_notification_show (notification, NULL);
366
367   g_free (message_esc);
368   g_object_unref (notification);
369 }
370
371 static void
372 event_added_cb (EmpathyEventManager *manager,
373     EmpathyEvent *event,
374     EmpathyNotificationsApprover *self)
375 {
376   if (self->priv->event != NULL)
377     return;
378
379   self->priv->event = event;
380
381   update_notification (self);
382 }
383
384 static void
385 event_removed_cb (EmpathyEventManager *manager,
386     EmpathyEvent *event,
387     EmpathyNotificationsApprover *self)
388 {
389   if (event != self->priv->event)
390     return;
391
392   self->priv->event = empathy_event_manager_get_top_event (
393       self->priv->event_mgr);
394
395   update_notification (self);
396 }
397
398 static void
399 event_updated_cb (EmpathyEventManager *manager,
400     EmpathyEvent *event,
401     EmpathyNotificationsApprover *self)
402 {
403   if (event != self->priv->event)
404     return;
405
406   if (empathy_notify_manager_notification_is_enabled (self->priv->notify_mgr))
407     update_notification (self);
408 }
409
410 static void
411 empathy_notifications_approver_init (EmpathyNotificationsApprover *self)
412 {
413   EmpathyNotificationsApproverPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
414     EMPATHY_TYPE_NOTIFICATIONS_APPROVER, EmpathyNotificationsApproverPrivate);
415
416   self->priv = priv;
417
418   self->priv->event_mgr = empathy_event_manager_dup_singleton ();
419   self->priv->notify_mgr = empathy_notify_manager_dup_singleton ();
420
421   tp_g_signal_connect_object (self->priv->event_mgr, "event-added",
422       G_CALLBACK (event_added_cb), self, 0);
423   tp_g_signal_connect_object (priv->event_mgr, "event-removed",
424       G_CALLBACK (event_removed_cb), self, 0);
425   tp_g_signal_connect_object (priv->event_mgr, "event-updated",
426       G_CALLBACK (event_updated_cb), self, 0);
427 }
428
429 EmpathyNotificationsApprover *
430 empathy_notifications_approver_dup_singleton (void)
431 {
432   return g_object_new (EMPATHY_TYPE_NOTIFICATIONS_APPROVER, NULL);
433 }