1 /* * Copyright (C) 2009 Collabora Ltd.
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.
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.
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
17 * Authors: Guillaume Desmottes <guillaume.desmottes@collabora.co.uk>
23 #include <glib/gi18n.h>
24 #include <libnotify/notification.h>
25 #include <libnotify/notify.h>
26 #include <telepathy-glib/telepathy-glib.h>
28 #include <libempathy/empathy-contact-manager.h>
30 #include <libempathy-gtk/empathy-notify-manager.h>
32 #include "empathy-event-manager.h"
34 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
35 #include <libempathy/empathy-debug.h>
37 #include "empathy-notifications-approver.h"
39 struct _EmpathyNotificationsApproverPrivate
41 EmpathyEventManager *event_mgr;
42 EmpathyNotifyManager *notify_mgr;
44 NotifyNotification *notification;
48 G_DEFINE_TYPE (EmpathyNotificationsApprover, empathy_notifications_approver,
51 static EmpathyNotificationsApprover *notifications_approver = NULL;
54 notifications_approver_constructor (GType type,
55 guint n_construct_params,
56 GObjectConstructParam *construct_params)
60 if (notifications_approver != NULL)
61 return g_object_ref (notifications_approver);
63 retval = G_OBJECT_CLASS (empathy_notifications_approver_parent_class)->
64 constructor (type, n_construct_params, construct_params);
66 notifications_approver = EMPATHY_NOTIFICATIONS_APPROVER (retval);
67 g_object_add_weak_pointer (retval, (gpointer) ¬ifications_approver);
73 notifications_approver_dispose (GObject *object)
75 EmpathyNotificationsApprover *self = (EmpathyNotificationsApprover *) object;
77 tp_clear_object (&self->priv->event_mgr);
78 tp_clear_object (&self->priv->notify_mgr);
80 if (self->priv->notification != NULL)
82 notify_notification_close (self->priv->notification, NULL);
83 tp_clear_object (&self->priv->notification);
86 G_OBJECT_CLASS (empathy_notifications_approver_parent_class)->dispose (
91 empathy_notifications_approver_class_init (
92 EmpathyNotificationsApproverClass *klass)
94 GObjectClass *object_class = G_OBJECT_CLASS (klass);
96 object_class->dispose = notifications_approver_dispose;
97 object_class->constructor = notifications_approver_constructor;
99 g_type_class_add_private (object_class,
100 sizeof (EmpathyNotificationsApproverPrivate));
104 notification_closed_cb (NotifyNotification *notification,
105 EmpathyNotificationsApprover *self)
107 if (self->priv->notification == notification)
108 tp_clear_object (&self->priv->notification);
112 notification_close_helper (EmpathyNotificationsApprover *self)
114 if (self->priv->notification != NULL)
116 notify_notification_close (self->priv->notification, NULL);
117 tp_clear_object (&self->priv->notification);
122 notification_approve_cb (NotifyNotification *notification,
124 EmpathyNotificationsApprover *self)
126 if (self->priv->event != NULL)
127 empathy_event_approve (self->priv->event);
131 notification_decline_cb (NotifyNotification *notification,
133 EmpathyNotificationsApprover *self)
135 if (self->priv->event != NULL)
136 empathy_event_decline (self->priv->event);
140 notification_decline_subscription_cb (NotifyNotification *notification,
142 EmpathyNotificationsApprover *self)
144 EmpathyContactManager *manager;
146 if (self->priv->event == NULL)
149 manager = empathy_contact_manager_dup_singleton ();
150 empathy_contact_list_remove (EMPATHY_CONTACT_LIST (manager),
151 self->priv->event->contact, "");
153 empathy_event_remove (self->priv->event);
155 g_object_unref (manager);
159 notification_accept_subscription_cb (NotifyNotification *notification,
161 EmpathyNotificationsApprover *self)
163 EmpathyContactManager *manager;
165 if (self->priv->event == NULL)
168 manager = empathy_contact_manager_dup_singleton ();
169 empathy_contact_list_add (EMPATHY_CONTACT_LIST (manager),
170 self->priv->event->contact, "");
172 empathy_event_remove (self->priv->event);
174 g_object_unref (manager);
178 add_notification_actions (EmpathyNotificationsApprover *self,
179 NotifyNotification *notification)
181 switch (self->priv->event->type) {
182 case EMPATHY_EVENT_TYPE_CHAT:
183 notify_notification_add_action (notification,
184 "respond", _("Respond"), (NotifyActionCallback) notification_approve_cb,
188 case EMPATHY_EVENT_TYPE_VOIP:
189 notify_notification_add_action (notification,
190 "reject", _("Reject"), (NotifyActionCallback) notification_decline_cb,
193 notify_notification_add_action (notification,
194 "answer", _("Answer"), (NotifyActionCallback) notification_approve_cb,
198 case EMPATHY_EVENT_TYPE_TRANSFER:
199 case EMPATHY_EVENT_TYPE_INVITATION:
200 notify_notification_add_action (notification,
201 "decline", _("Decline"), (NotifyActionCallback) notification_decline_cb,
204 notify_notification_add_action (notification,
205 "accept", _("Accept"), (NotifyActionCallback) notification_approve_cb,
209 case EMPATHY_EVENT_TYPE_SUBSCRIPTION:
210 notify_notification_add_action (notification,
211 "decline", _("Decline"),
212 (NotifyActionCallback) notification_decline_subscription_cb,
215 notify_notification_add_action (notification,
216 "accept", _("Accept"),
217 (NotifyActionCallback) notification_accept_subscription_cb,
221 case EMPATHY_EVENT_TYPE_AUTH:
222 /* translators: the 'Provide' button is displayed in a notification
223 * bubble when Empathy is asking for an account password; clicking on it
224 * brings the password popup. */
225 notify_notification_add_action (notification,
226 "provide", _("Provide"), (NotifyActionCallback) notification_approve_cb,
236 notification_is_urgent (EmpathyNotificationsApprover *self,
237 NotifyNotification *notification)
239 /* Mark as urgent all the notifications with which user should
241 switch (self->priv->event->type) {
242 case EMPATHY_EVENT_TYPE_CHAT:
243 case EMPATHY_EVENT_TYPE_VOIP:
244 case EMPATHY_EVENT_TYPE_TRANSFER:
245 case EMPATHY_EVENT_TYPE_INVITATION:
246 case EMPATHY_EVENT_TYPE_AUTH:
249 case EMPATHY_EVENT_TYPE_SUBSCRIPTION:
250 case EMPATHY_EVENT_TYPE_PRESENCE_ONLINE:
251 case EMPATHY_EVENT_TYPE_PRESENCE_OFFLINE:
259 get_category_for_event_type (EmpathyEventType type)
262 case EMPATHY_EVENT_TYPE_CHAT:
263 return "im.received";
264 case EMPATHY_EVENT_TYPE_PRESENCE_ONLINE:
265 return "presence.online";
266 case EMPATHY_EVENT_TYPE_PRESENCE_OFFLINE:
267 return "presence.offline";
268 case EMPATHY_EVENT_TYPE_VOIP:
269 case EMPATHY_EVENT_TYPE_TRANSFER:
270 case EMPATHY_EVENT_TYPE_INVITATION:
271 case EMPATHY_EVENT_TYPE_AUTH:
272 case EMPATHY_EVENT_TYPE_SUBSCRIPTION:
280 update_notification (EmpathyNotificationsApprover *self)
282 GdkPixbuf *pixbuf = NULL;
283 gchar *message_esc = NULL;
284 gboolean has_x_canonical_append;
285 NotifyNotification *notification;
287 if (!empathy_notify_manager_notification_is_enabled (self->priv->notify_mgr))
289 /* always close the notification if this happens */
290 notification_close_helper (self);
294 if (self->priv->event == NULL)
296 notification_close_helper (self);
300 if (self->priv->event->message != NULL)
301 message_esc = g_markup_escape_text (self->priv->event->message, -1);
303 has_x_canonical_append = empathy_notify_manager_has_capability (
304 self->priv->notify_mgr, EMPATHY_NOTIFY_MANAGER_CAP_X_CANONICAL_APPEND);
306 if (self->priv->notification != NULL && ! has_x_canonical_append)
308 /* if the notification server does NOT supports x-canonical-append, it is
309 * better to not use notify_notification_update to avoid
310 * overwriting the current notification message */
311 notification = g_object_ref (self->priv->notification);
313 notify_notification_update (notification,
314 self->priv->event->header, message_esc, NULL);
318 const gchar *category;
320 /* if the notification server supports x-canonical-append,
321 * the hint will be added, so that the message from the
322 * just created notification will be automatically appended
323 * to an existing notification with the same title.
324 * In this way the previous message will not be lost: the new
325 * message will appear below it, in the same notification */
326 notification = notify_notification_new (self->priv->event->header,
329 if (self->priv->notification == NULL)
331 self->priv->notification = g_object_ref (notification);
333 g_signal_connect (notification, "closed",
334 G_CALLBACK (notification_closed_cb), self);
337 notify_notification_set_timeout (notification,
338 NOTIFY_EXPIRES_DEFAULT);
340 if (has_x_canonical_append)
341 /* We have to set a not empty string to keep libnotify happy */
342 notify_notification_set_hint_string (notification,
343 EMPATHY_NOTIFY_MANAGER_CAP_X_CANONICAL_APPEND, "1");
345 if (empathy_notify_manager_has_capability (self->priv->notify_mgr,
346 EMPATHY_NOTIFY_MANAGER_CAP_ACTIONS))
347 add_notification_actions (self, notification);
349 if (notification_is_urgent (self, notification))
350 notify_notification_set_urgency (notification, NOTIFY_URGENCY_CRITICAL);
352 category = get_category_for_event_type (self->priv->event->type);
353 if (category != NULL)
354 notify_notification_set_hint_string (notification,
355 EMPATHY_NOTIFY_MANAGER_CAP_CATEGORY, category);
358 pixbuf = empathy_notify_manager_get_pixbuf_for_notification (
359 self->priv->notify_mgr, self->priv->event->contact,
360 self->priv->event->icon_name);
364 notify_notification_set_icon_from_pixbuf (notification, pixbuf);
365 g_object_unref (pixbuf);
368 notify_notification_show (notification, NULL);
370 g_free (message_esc);
371 g_object_unref (notification);
375 event_added_cb (EmpathyEventManager *manager,
377 EmpathyNotificationsApprover *self)
379 if (self->priv->event != NULL)
382 self->priv->event = event;
384 update_notification (self);
388 event_removed_cb (EmpathyEventManager *manager,
390 EmpathyNotificationsApprover *self)
392 if (event != self->priv->event)
395 self->priv->event = empathy_event_manager_get_top_event (
396 self->priv->event_mgr);
398 update_notification (self);
402 event_updated_cb (EmpathyEventManager *manager,
404 EmpathyNotificationsApprover *self)
406 if (event != self->priv->event)
409 if (empathy_notify_manager_notification_is_enabled (self->priv->notify_mgr))
410 update_notification (self);
414 empathy_notifications_approver_init (EmpathyNotificationsApprover *self)
416 EmpathyNotificationsApproverPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
417 EMPATHY_TYPE_NOTIFICATIONS_APPROVER, EmpathyNotificationsApproverPrivate);
421 self->priv->event_mgr = empathy_event_manager_dup_singleton ();
422 self->priv->notify_mgr = empathy_notify_manager_dup_singleton ();
424 tp_g_signal_connect_object (self->priv->event_mgr, "event-added",
425 G_CALLBACK (event_added_cb), self, 0);
426 tp_g_signal_connect_object (priv->event_mgr, "event-removed",
427 G_CALLBACK (event_removed_cb), self, 0);
428 tp_g_signal_connect_object (priv->event_mgr, "event-updated",
429 G_CALLBACK (event_updated_cb), self, 0);
432 EmpathyNotificationsApprover *
433 empathy_notifications_approver_dup_singleton (void)
435 return g_object_new (EMPATHY_TYPE_NOTIFICATIONS_APPROVER, NULL);