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