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