]> git.0d.be Git - empathy.git/blob - src/empathy-notifications-approver.c
Use a new id for accepting without video
[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 static const gchar *
294 get_category_for_event_type (EmpathyEventType type)
295 {
296   switch (type) {
297     case EMPATHY_EVENT_TYPE_CHAT:
298       return "im.received";
299     case EMPATHY_EVENT_TYPE_PRESENCE_ONLINE:
300       return "presence.online";
301     case EMPATHY_EVENT_TYPE_PRESENCE_OFFLINE:
302       return "presence.offline";
303     case EMPATHY_EVENT_TYPE_VOIP:
304     case EMPATHY_EVENT_TYPE_CALL:
305     case EMPATHY_EVENT_TYPE_TRANSFER:
306     case EMPATHY_EVENT_TYPE_INVITATION:
307     case EMPATHY_EVENT_TYPE_AUTH:
308     case EMPATHY_EVENT_TYPE_SUBSCRIPTION:
309       return NULL;
310   }
311
312   return NULL;
313 }
314
315 static void
316 update_notification (EmpathyNotificationsApprover *self)
317 {
318   GdkPixbuf *pixbuf = NULL;
319   gchar *message_esc = NULL;
320   gboolean has_x_canonical_append;
321   NotifyNotification *notification;
322
323   if (!empathy_notify_manager_notification_is_enabled (self->priv->notify_mgr))
324     {
325       /* always close the notification if this happens */
326       notification_close_helper (self);
327       return;
328     }
329
330   if (self->priv->event == NULL)
331     {
332       notification_close_helper (self);
333       return;
334      }
335
336   if (self->priv->event->message != NULL)
337     message_esc = g_markup_escape_text (self->priv->event->message, -1);
338
339   has_x_canonical_append = empathy_notify_manager_has_capability (
340       self->priv->notify_mgr, EMPATHY_NOTIFY_MANAGER_CAP_X_CANONICAL_APPEND);
341
342   if (self->priv->notification != NULL && ! has_x_canonical_append)
343     {
344       /* if the notification server does NOT supports x-canonical-append, it is
345        * better to not use notify_notification_update to avoid
346        * overwriting the current notification message */
347       notification = g_object_ref (self->priv->notification);
348
349       notify_notification_update (notification,
350           self->priv->event->header, message_esc, NULL);
351     }
352   else
353     {
354       const gchar *category;
355
356       /* if the notification server supports x-canonical-append,
357        * the hint will be added, so that the message from the
358        * just created notification will be automatically appended
359        * to an existing notification with the same title.
360        * In this way the previous message will not be lost: the new
361        * message will appear below it, in the same notification */
362       notification = notify_notification_new (self->priv->event->header,
363            message_esc, NULL);
364
365       if (self->priv->notification == NULL)
366         {
367           self->priv->notification = g_object_ref (notification);
368
369           g_signal_connect (notification, "closed",
370               G_CALLBACK (notification_closed_cb), self);
371         }
372
373       notify_notification_set_timeout (notification,
374           NOTIFY_EXPIRES_DEFAULT);
375
376       if (has_x_canonical_append)
377         /* We have to set a not empty string to keep libnotify happy */
378         notify_notification_set_hint_string (notification,
379             EMPATHY_NOTIFY_MANAGER_CAP_X_CANONICAL_APPEND, "1");
380
381       if (empathy_notify_manager_has_capability (self->priv->notify_mgr,
382             EMPATHY_NOTIFY_MANAGER_CAP_ACTIONS))
383         add_notification_actions (self, notification);
384
385       if (notification_is_urgent (self, notification))
386         notify_notification_set_urgency (notification, NOTIFY_URGENCY_CRITICAL);
387
388       category = get_category_for_event_type (self->priv->event->type);
389       if (category != NULL)
390         notify_notification_set_hint_string (notification,
391             EMPATHY_NOTIFY_MANAGER_CAP_CATEGORY, category);
392     }
393
394   pixbuf = empathy_notify_manager_get_pixbuf_for_notification (
395       self->priv->notify_mgr, self->priv->event->contact,
396       self->priv->event->icon_name);
397
398   if (pixbuf != NULL)
399     {
400       notify_notification_set_icon_from_pixbuf (notification, pixbuf);
401       g_object_unref (pixbuf);
402     }
403
404   notify_notification_show (notification, NULL);
405
406   g_free (message_esc);
407   g_object_unref (notification);
408 }
409
410 static void
411 event_added_cb (EmpathyEventManager *manager,
412     EmpathyEvent *event,
413     EmpathyNotificationsApprover *self)
414 {
415   if (self->priv->event != NULL)
416     return;
417
418   self->priv->event = event;
419
420   update_notification (self);
421 }
422
423 static void
424 event_removed_cb (EmpathyEventManager *manager,
425     EmpathyEvent *event,
426     EmpathyNotificationsApprover *self)
427 {
428   if (event != self->priv->event)
429     return;
430
431   self->priv->event = empathy_event_manager_get_top_event (
432       self->priv->event_mgr);
433
434   update_notification (self);
435 }
436
437 static void
438 event_updated_cb (EmpathyEventManager *manager,
439     EmpathyEvent *event,
440     EmpathyNotificationsApprover *self)
441 {
442   if (event != self->priv->event)
443     return;
444
445   if (empathy_notify_manager_notification_is_enabled (self->priv->notify_mgr))
446     update_notification (self);
447 }
448
449 static void
450 empathy_notifications_approver_init (EmpathyNotificationsApprover *self)
451 {
452   EmpathyNotificationsApproverPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
453     EMPATHY_TYPE_NOTIFICATIONS_APPROVER, EmpathyNotificationsApproverPrivate);
454
455   self->priv = priv;
456
457   self->priv->event_mgr = empathy_event_manager_dup_singleton ();
458   self->priv->notify_mgr = empathy_notify_manager_dup_singleton ();
459
460   tp_g_signal_connect_object (self->priv->event_mgr, "event-added",
461       G_CALLBACK (event_added_cb), self, 0);
462   tp_g_signal_connect_object (priv->event_mgr, "event-removed",
463       G_CALLBACK (event_removed_cb), self, 0);
464   tp_g_signal_connect_object (priv->event_mgr, "event-updated",
465       G_CALLBACK (event_updated_cb), self, 0);
466 }
467
468 EmpathyNotificationsApprover *
469 empathy_notifications_approver_dup_singleton (void)
470 {
471   return g_object_new (EMPATHY_TYPE_NOTIFICATIONS_APPROVER, NULL);
472 }