]> git.0d.be Git - empathy.git/blob - src/empathy-notifications-approver.c
use tp_g_signal_connect_object
[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-contact-manager.h>
29
30 #include <libempathy-gtk/empathy-notify-manager.h>
31
32 #include "empathy-event-manager.h"
33
34 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
35 #include <libempathy/empathy-debug.h>
36
37 #include "empathy-notifications-approver.h"
38
39 struct _EmpathyNotificationsApproverPrivate
40 {
41   EmpathyEventManager *event_mgr;
42   EmpathyNotifyManager *notify_mgr;
43
44   NotifyNotification *notification;
45   EmpathyEvent *event;
46 };
47
48 G_DEFINE_TYPE (EmpathyNotificationsApprover, empathy_notifications_approver,
49     G_TYPE_OBJECT);
50
51 static EmpathyNotificationsApprover *notifications_approver = NULL;
52
53 static GObject *
54 notifications_approver_constructor (GType type,
55     guint n_construct_params,
56     GObjectConstructParam *construct_params)
57 {
58   GObject *retval;
59
60   if (notifications_approver != NULL)
61     return g_object_ref (notifications_approver);
62
63   retval = G_OBJECT_CLASS (empathy_notifications_approver_parent_class)->
64       constructor (type, n_construct_params, construct_params);
65
66   notifications_approver = EMPATHY_NOTIFICATIONS_APPROVER (retval);
67   g_object_add_weak_pointer (retval, (gpointer) &notifications_approver);
68
69   return retval;
70 }
71
72 static void
73 notifications_approver_dispose (GObject *object)
74 {
75   EmpathyNotificationsApprover *self = (EmpathyNotificationsApprover *) object;
76
77   tp_clear_object (&self->priv->event_mgr);
78   tp_clear_object (&self->priv->notify_mgr);
79
80   if (self->priv->notification != NULL)
81     {
82       notify_notification_close (self->priv->notification, NULL);
83       tp_clear_object (&self->priv->notification);
84     }
85
86   G_OBJECT_CLASS (empathy_notifications_approver_parent_class)->dispose (
87       object);
88 }
89
90 static void
91 empathy_notifications_approver_class_init (
92     EmpathyNotificationsApproverClass *klass)
93 {
94   GObjectClass *object_class = G_OBJECT_CLASS (klass);
95
96   object_class->dispose = notifications_approver_dispose;
97   object_class->constructor = notifications_approver_constructor;
98
99   g_type_class_add_private (object_class,
100       sizeof (EmpathyNotificationsApproverPrivate));
101 }
102
103 static void
104 notification_closed_cb (NotifyNotification *notification,
105     EmpathyNotificationsApprover *self)
106 {
107   g_object_unref (notification);
108
109   if (self->priv->notification == notification)
110     self->priv->notification = NULL;
111 }
112
113 static void
114 notification_close_helper (EmpathyNotificationsApprover *self)
115 {
116   if (self->priv->notification != NULL)
117     {
118       notify_notification_close (self->priv->notification, NULL);
119       self->priv->notification = NULL;
120     }
121 }
122
123 static void
124 notification_approve_cb (NotifyNotification *notification,
125     gchar *action,
126     EmpathyNotificationsApprover *self)
127 {
128   if (self->priv->event != NULL)
129     empathy_event_approve (self->priv->event);
130 }
131
132 static void
133 notification_decline_cb (NotifyNotification *notification,
134     gchar *action,
135     EmpathyNotificationsApprover  *self)
136 {
137   if (self->priv->event != NULL)
138     empathy_event_decline (self->priv->event);
139 }
140
141 static void
142 notification_decline_subscription_cb (NotifyNotification *notification,
143     gchar *action,
144     EmpathyNotificationsApprover *self)
145 {
146   EmpathyContactManager *manager;
147
148   if (self->priv->event == NULL)
149     return;
150
151   manager = empathy_contact_manager_dup_singleton ();
152   empathy_contact_list_remove (EMPATHY_CONTACT_LIST (manager),
153       self->priv->event->contact, "");
154
155   empathy_event_remove (self->priv->event);
156
157   g_object_unref (manager);
158 }
159
160 static void
161 notification_accept_subscription_cb (NotifyNotification *notification,
162     gchar *action,
163     EmpathyNotificationsApprover *self)
164 {
165   EmpathyContactManager *manager;
166
167   if (self->priv->event == NULL)
168     return;
169
170   manager = empathy_contact_manager_dup_singleton ();
171   empathy_contact_list_add (EMPATHY_CONTACT_LIST (manager),
172       self->priv->event->contact, "");
173
174   empathy_event_remove (self->priv->event);
175
176   g_object_unref (manager);
177 }
178
179 static void
180 add_notification_actions (EmpathyNotificationsApprover *self,
181     NotifyNotification *notification)
182 {
183   switch (self->priv->event->type) {
184     case EMPATHY_EVENT_TYPE_CHAT:
185       notify_notification_add_action (notification,
186         "respond", _("Respond"), (NotifyActionCallback) notification_approve_cb,
187           self, NULL);
188       break;
189
190     case EMPATHY_EVENT_TYPE_VOIP:
191       notify_notification_add_action (notification,
192         "reject", _("Reject"), (NotifyActionCallback) notification_decline_cb,
193           self, NULL);
194
195       notify_notification_add_action (notification,
196         "answer", _("Answer"), (NotifyActionCallback) notification_approve_cb,
197           self, NULL);
198       break;
199
200     case EMPATHY_EVENT_TYPE_TRANSFER:
201     case EMPATHY_EVENT_TYPE_INVITATION:
202       notify_notification_add_action (notification,
203         "decline", _("Decline"), (NotifyActionCallback) notification_decline_cb,
204           self, NULL);
205
206       notify_notification_add_action (notification,
207         "accept", _("Accept"), (NotifyActionCallback) notification_approve_cb,
208           self, NULL);
209       break;
210
211     case EMPATHY_EVENT_TYPE_SUBSCRIPTION:
212       notify_notification_add_action (notification,
213         "decline", _("Decline"),
214           (NotifyActionCallback) notification_decline_subscription_cb,
215           self, NULL);
216
217       notify_notification_add_action (notification,
218         "accept", _("Accept"),
219           (NotifyActionCallback) notification_accept_subscription_cb,
220           self, NULL);
221
222     default:
223       break;
224   }
225 }
226
227 static void
228 update_notification (EmpathyNotificationsApprover *self)
229 {
230   GdkPixbuf *pixbuf = NULL;
231   gchar *message_esc = NULL;
232   gboolean has_x_canonical_append;
233   NotifyNotification *notification = self->priv->notification;
234
235   if (!empathy_notify_manager_notification_is_enabled (self->priv->notify_mgr))
236     {
237       /* always close the notification if this happens */
238       notification_close_helper (self);
239       return;
240     }
241
242   if (self->priv->event == NULL)
243     {
244       notification_close_helper (self);
245       return;
246      }
247
248   if (self->priv->event->message != NULL)
249     message_esc = g_markup_escape_text (self->priv->event->message, -1);
250
251   has_x_canonical_append = empathy_notify_manager_has_capability (
252       self->priv->notify_mgr, EMPATHY_NOTIFY_MANAGER_CAP_X_CANONICAL_APPEND);
253
254   if (notification != NULL && ! has_x_canonical_append)
255     {
256       /* if the notification server does NOT supports x-canonical-append, it is
257        * better to not use notify_notification_update to avoid
258        * overwriting the current notification message */
259       notify_notification_update (notification,
260           self->priv->event->header, message_esc, NULL);
261     }
262   else
263     {
264       /* if the notification server supports x-canonical-append,
265        * the hint will be added, so that the message from the
266        * just created notification will be automatically appended
267        * to an existing notification with the same title.
268        * In this way the previous message will not be lost: the new
269        * message will appear below it, in the same notification */
270       notification = notify_notification_new (self->priv->event->header,
271            message_esc, NULL);
272
273       if (self->priv->notification == NULL)
274         self->priv->notification = notification;
275
276       notify_notification_set_timeout (notification,
277           NOTIFY_EXPIRES_DEFAULT);
278
279       if (has_x_canonical_append)
280         notify_notification_set_hint_string (notification,
281             EMPATHY_NOTIFY_MANAGER_CAP_X_CANONICAL_APPEND, "");
282
283       if (empathy_notify_manager_has_capability (self->priv->notify_mgr,
284             EMPATHY_NOTIFY_MANAGER_CAP_ACTIONS))
285         add_notification_actions (self, notification);
286
287       g_signal_connect (notification, "closed",
288           G_CALLBACK (notification_closed_cb), self);
289     }
290
291   pixbuf = empathy_notify_manager_get_pixbuf_for_notification (
292       self->priv->notify_mgr, self->priv->event->contact,
293       self->priv->event->icon_name);
294
295   if (pixbuf != NULL)
296     {
297       notify_notification_set_icon_from_pixbuf (notification, pixbuf);
298       g_object_unref (pixbuf);
299     }
300
301   notify_notification_show (notification, NULL);
302
303   g_free (message_esc);
304 }
305
306 static void
307 event_added_cb (EmpathyEventManager *manager,
308     EmpathyEvent *event,
309     EmpathyNotificationsApprover *self)
310 {
311   if (self->priv->event != NULL)
312     return;
313
314   self->priv->event = event;
315   update_notification (self);
316 }
317
318 static void
319 event_removed_cb (EmpathyEventManager *manager,
320     EmpathyEvent *event,
321     EmpathyNotificationsApprover *self)
322 {
323   if (event != self->priv->event)
324     return;
325
326   self->priv->event = empathy_event_manager_get_top_event (
327       self->priv->event_mgr);
328
329   update_notification (self);
330 }
331
332 static void
333 event_updated_cb (EmpathyEventManager *manager,
334     EmpathyEvent *event,
335     EmpathyNotificationsApprover *self)
336 {
337   if (event != self->priv->event)
338     return;
339
340   if (empathy_notify_manager_notification_is_enabled (self->priv->notify_mgr))
341     update_notification (self);
342 }
343
344 static void
345 empathy_notifications_approver_init (EmpathyNotificationsApprover *self)
346 {
347   EmpathyNotificationsApproverPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
348     EMPATHY_TYPE_NOTIFICATIONS_APPROVER, EmpathyNotificationsApproverPrivate);
349
350   self->priv = priv;
351
352   self->priv->event_mgr = empathy_event_manager_dup_singleton ();
353   self->priv->notify_mgr = empathy_notify_manager_dup_singleton ();
354
355   tp_g_signal_connect_object (self->priv->event_mgr, "event-added",
356       G_CALLBACK (event_added_cb), self, 0);
357   tp_g_signal_connect_object (priv->event_mgr, "event-removed",
358       G_CALLBACK (event_removed_cb), self, 0);
359   tp_g_signal_connect_object (priv->event_mgr, "event-updated",
360       G_CALLBACK (event_updated_cb), self, 0);
361 }
362
363 EmpathyNotificationsApprover *
364 empathy_notifications_approver_dup_singleton (void)
365 {
366   return g_object_new (EMPATHY_TYPE_NOTIFICATIONS_APPROVER, NULL);
367 }