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