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