]> git.0d.be Git - empathy.git/blob - src/empathy-notifications-approver.c
call: don't leak the contact
[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       notify_notification_add_action (notification,
187         "respond", _("Respond"), (NotifyActionCallback) notification_approve_cb,
188           self, NULL);
189       break;
190
191     case EMPATHY_EVENT_TYPE_CALL:
192         video = tp_call_channel_has_initial_video (
193             TP_CALL_CHANNEL (self->priv->event->handler_instance), NULL);
194
195       notify_notification_add_action (notification,
196         "reject", _("Reject"), (NotifyActionCallback) notification_decline_cb,
197           self, NULL);
198
199       if (video && self->priv->event->type == EMPATHY_EVENT_TYPE_CALL)
200           notify_notification_add_action (notification,
201           "answer-no-video", _("Answer"),
202           (NotifyActionCallback) notification_approve_no_video_cb,
203           self, NULL);
204
205       notify_notification_add_action (notification,
206           "answer", video ? _("Answer with video") : _("Answer"),
207           (NotifyActionCallback) notification_approve_cb,
208           self, NULL);
209       break;
210
211     case EMPATHY_EVENT_TYPE_TRANSFER:
212     case EMPATHY_EVENT_TYPE_INVITATION:
213       notify_notification_add_action (notification,
214         "decline", _("Decline"), (NotifyActionCallback) notification_decline_cb,
215           self, NULL);
216
217       notify_notification_add_action (notification,
218         "accept", _("Accept"), (NotifyActionCallback) notification_approve_cb,
219           self, NULL);
220       break;
221
222     case EMPATHY_EVENT_TYPE_SUBSCRIPTION:
223       notify_notification_add_action (notification,
224         "decline", _("Decline"),
225           (NotifyActionCallback) notification_decline_subscription_cb,
226           self, NULL);
227
228       notify_notification_add_action (notification,
229         "accept", _("Accept"),
230           (NotifyActionCallback) notification_accept_subscription_cb,
231           self, NULL);
232       break;
233
234     case EMPATHY_EVENT_TYPE_AUTH:
235       notify_notification_add_action (notification,
236         /* translators: the 'Provide' button is displayed in a notification
237          * bubble when Empathy is asking for an account password; clicking on it
238          * brings the password popup. */
239         "provide", _("Provide"), (NotifyActionCallback) notification_approve_cb,
240           self, NULL);
241       break;
242
243     default:
244       break;
245   }
246 }
247
248 static gboolean
249 notification_is_urgent (EmpathyNotificationsApprover *self,
250     NotifyNotification *notification)
251 {
252   /* Mark as urgent all the notifications with which user should
253    * interact ASAP */
254   switch (self->priv->event->type) {
255     case EMPATHY_EVENT_TYPE_CHAT:
256     case EMPATHY_EVENT_TYPE_CALL:
257     case EMPATHY_EVENT_TYPE_TRANSFER:
258     case EMPATHY_EVENT_TYPE_INVITATION:
259     case EMPATHY_EVENT_TYPE_AUTH:
260       return TRUE;
261
262     case EMPATHY_EVENT_TYPE_SUBSCRIPTION:
263     case EMPATHY_EVENT_TYPE_PRESENCE_ONLINE:
264     case EMPATHY_EVENT_TYPE_PRESENCE_OFFLINE:
265       return FALSE;
266   }
267
268   return FALSE;
269 }
270
271 /* Use x-empathy as prefix for unofficial categories
272  * http://www.galago-project.org/specs/notification/0.9/x211.html */
273 static const gchar *
274 get_category_for_event_type (EmpathyEventType type)
275 {
276   switch (type) {
277     case EMPATHY_EVENT_TYPE_CHAT:
278       return "im.received";
279     case EMPATHY_EVENT_TYPE_PRESENCE_ONLINE:
280       return "presence.online";
281     case EMPATHY_EVENT_TYPE_PRESENCE_OFFLINE:
282       return "presence.offline";
283     case EMPATHY_EVENT_TYPE_CALL:
284       return "x-empathy.call.incoming";
285     case EMPATHY_EVENT_TYPE_TRANSFER:
286       return "x-empathy.transfer.incoming";
287     case EMPATHY_EVENT_TYPE_INVITATION:
288       return "x-empathy.im.room-invitation";
289     case EMPATHY_EVENT_TYPE_AUTH:
290       return "x-empathy.network.auth-request";
291     case EMPATHY_EVENT_TYPE_SUBSCRIPTION:
292       return "x-empathy.im.subscription-request";
293   }
294
295   return NULL;
296 }
297
298 static void
299 update_notification (EmpathyNotificationsApprover *self)
300 {
301   GdkPixbuf *pixbuf = NULL;
302   gchar *message_esc = NULL;
303   gboolean has_x_canonical_append;
304   NotifyNotification *notification;
305
306   if (!empathy_notify_manager_notification_is_enabled (self->priv->notify_mgr))
307     {
308       /* always close the notification if this happens */
309       notification_close_helper (self);
310       return;
311     }
312
313   if (self->priv->event == NULL)
314     {
315       notification_close_helper (self);
316       return;
317      }
318
319   if (self->priv->event->message != NULL)
320     message_esc = g_markup_escape_text (self->priv->event->message, -1);
321
322   has_x_canonical_append = empathy_notify_manager_has_capability (
323       self->priv->notify_mgr, EMPATHY_NOTIFY_MANAGER_CAP_X_CANONICAL_APPEND);
324
325   if (self->priv->notification != NULL && ! has_x_canonical_append)
326     {
327       /* if the notification server does NOT supports x-canonical-append, it is
328        * better to not use notify_notification_update to avoid
329        * overwriting the current notification message */
330       notification = g_object_ref (self->priv->notification);
331
332       notify_notification_update (notification,
333           self->priv->event->header, message_esc, NULL);
334     }
335   else
336     {
337       const gchar *category;
338
339       /* if the notification server supports x-canonical-append,
340        * the hint will be added, so that the message from the
341        * just created notification will be automatically appended
342        * to an existing notification with the same title.
343        * In this way the previous message will not be lost: the new
344        * message will appear below it, in the same notification */
345       notification = notify_notification_new (self->priv->event->header,
346            message_esc, NULL);
347
348       if (self->priv->notification == NULL)
349         {
350           self->priv->notification = g_object_ref (notification);
351
352           g_signal_connect (notification, "closed",
353               G_CALLBACK (notification_closed_cb), self);
354         }
355
356       notify_notification_set_timeout (notification,
357           NOTIFY_EXPIRES_DEFAULT);
358
359       if (has_x_canonical_append)
360         {
361           notify_notification_set_hint (notification,
362               EMPATHY_NOTIFY_MANAGER_CAP_X_CANONICAL_APPEND,
363               g_variant_new_boolean (TRUE));
364         }
365
366       if (empathy_notify_manager_has_capability (self->priv->notify_mgr,
367             EMPATHY_NOTIFY_MANAGER_CAP_ACTIONS))
368         add_notification_actions (self, notification);
369
370       if (notification_is_urgent (self, notification))
371         notify_notification_set_urgency (notification, NOTIFY_URGENCY_CRITICAL);
372
373       category = get_category_for_event_type (self->priv->event->type);
374       if (category != NULL)
375         {
376           notify_notification_set_hint (notification,
377               EMPATHY_NOTIFY_MANAGER_CAP_CATEGORY,
378               g_variant_new_string (category));
379         }
380     }
381
382   pixbuf = empathy_notify_manager_get_pixbuf_for_notification (
383       self->priv->notify_mgr, self->priv->event->contact,
384       self->priv->event->icon_name);
385
386   if (pixbuf != NULL)
387     {
388       notify_notification_set_image_from_pixbuf (notification, pixbuf);
389       g_object_unref (pixbuf);
390     }
391
392   notify_notification_show (notification, NULL);
393
394   g_free (message_esc);
395   g_object_unref (notification);
396 }
397
398 static void
399 event_added_cb (EmpathyEventManager *manager,
400     EmpathyEvent *event,
401     EmpathyNotificationsApprover *self)
402 {
403   if (self->priv->event != NULL)
404     return;
405
406   self->priv->event = event;
407
408   update_notification (self);
409 }
410
411 static void
412 event_removed_cb (EmpathyEventManager *manager,
413     EmpathyEvent *event,
414     EmpathyNotificationsApprover *self)
415 {
416   if (event != self->priv->event)
417     return;
418
419   self->priv->event = empathy_event_manager_get_top_event (
420       self->priv->event_mgr);
421
422   update_notification (self);
423 }
424
425 static void
426 event_updated_cb (EmpathyEventManager *manager,
427     EmpathyEvent *event,
428     EmpathyNotificationsApprover *self)
429 {
430   if (event != self->priv->event)
431     return;
432
433   if (empathy_notify_manager_notification_is_enabled (self->priv->notify_mgr))
434     update_notification (self);
435 }
436
437 static void
438 empathy_notifications_approver_init (EmpathyNotificationsApprover *self)
439 {
440   EmpathyNotificationsApproverPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
441     EMPATHY_TYPE_NOTIFICATIONS_APPROVER, EmpathyNotificationsApproverPrivate);
442
443   self->priv = priv;
444
445   self->priv->event_mgr = empathy_event_manager_dup_singleton ();
446   self->priv->notify_mgr = empathy_notify_manager_dup_singleton ();
447
448   tp_g_signal_connect_object (self->priv->event_mgr, "event-added",
449       G_CALLBACK (event_added_cb), self, 0);
450   tp_g_signal_connect_object (priv->event_mgr, "event-removed",
451       G_CALLBACK (event_removed_cb), self, 0);
452   tp_g_signal_connect_object (priv->event_mgr, "event-updated",
453       G_CALLBACK (event_updated_cb), self, 0);
454 }
455
456 EmpathyNotificationsApprover *
457 empathy_notifications_approver_dup_singleton (void)
458 {
459   return g_object_new (EMPATHY_TYPE_NOTIFICATIONS_APPROVER, NULL);
460 }