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