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-gtk/empathy-notify-manager.h>
29 #include <libempathy-gtk/empathy-call-utils.h>
31 #include "empathy-event-manager.h"
33 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
34 #include <libempathy/empathy-debug.h>
36 #include "empathy-notifications-approver.h"
38 struct _EmpathyNotificationsApproverPrivate
40 EmpathyEventManager *event_mgr;
41 EmpathyNotifyManager *notify_mgr;
43 NotifyNotification *notification;
47 G_DEFINE_TYPE (EmpathyNotificationsApprover, empathy_notifications_approver,
50 static EmpathyNotificationsApprover *notifications_approver = NULL;
53 notifications_approver_constructor (GType type,
54 guint n_construct_params,
55 GObjectConstructParam *construct_params)
59 if (notifications_approver != NULL)
60 return g_object_ref (notifications_approver);
62 retval = G_OBJECT_CLASS (empathy_notifications_approver_parent_class)->
63 constructor (type, n_construct_params, construct_params);
65 notifications_approver = EMPATHY_NOTIFICATIONS_APPROVER (retval);
66 g_object_add_weak_pointer (retval, (gpointer) ¬ifications_approver);
72 notifications_approver_dispose (GObject *object)
74 EmpathyNotificationsApprover *self = (EmpathyNotificationsApprover *) object;
76 tp_clear_object (&self->priv->event_mgr);
77 tp_clear_object (&self->priv->notify_mgr);
79 if (self->priv->notification != NULL)
81 notify_notification_close (self->priv->notification, NULL);
82 tp_clear_object (&self->priv->notification);
85 G_OBJECT_CLASS (empathy_notifications_approver_parent_class)->dispose (
90 empathy_notifications_approver_class_init (
91 EmpathyNotificationsApproverClass *klass)
93 GObjectClass *object_class = G_OBJECT_CLASS (klass);
95 object_class->dispose = notifications_approver_dispose;
96 object_class->constructor = notifications_approver_constructor;
98 g_type_class_add_private (object_class,
99 sizeof (EmpathyNotificationsApproverPrivate));
103 notification_closed_cb (NotifyNotification *notification,
104 EmpathyNotificationsApprover *self)
106 if (self->priv->notification == notification)
107 tp_clear_object (&self->priv->notification);
111 notification_close_helper (EmpathyNotificationsApprover *self)
113 if (self->priv->notification != NULL)
115 notify_notification_close (self->priv->notification, NULL);
116 tp_clear_object (&self->priv->notification);
121 notification_approve_no_video_cb (NotifyNotification *notification,
123 EmpathyNotificationsApprover *self)
125 if (self->priv->event)
127 empathy_call_channel_send_video (
128 TP_CALL_CHANNEL (self->priv->event->handler_instance),
130 empathy_event_approve (self->priv->event);
135 notification_approve_cb (NotifyNotification *notification,
137 EmpathyNotificationsApprover *self)
139 if (self->priv->event != NULL)
140 empathy_event_approve (self->priv->event);
144 notification_decline_cb (NotifyNotification *notification,
146 EmpathyNotificationsApprover *self)
148 if (self->priv->event != NULL)
149 empathy_event_decline (self->priv->event);
153 notification_decline_subscription_cb (NotifyNotification *notification,
155 EmpathyNotificationsApprover *self)
157 if (self->priv->event == NULL)
160 empathy_contact_remove_from_contact_list (self->priv->event->contact);
162 empathy_event_remove (self->priv->event);
166 notification_accept_subscription_cb (NotifyNotification *notification,
168 EmpathyNotificationsApprover *self)
170 if (self->priv->event == NULL)
173 empathy_contact_add_to_contact_list (self->priv->event->contact, "");
175 empathy_event_remove (self->priv->event);
179 add_notification_actions (EmpathyNotificationsApprover *self,
180 NotifyNotification *notification)
184 switch (self->priv->event->type) {
185 case EMPATHY_EVENT_TYPE_CHAT:
186 notify_notification_add_action (notification,
187 "respond", _("Respond"), (NotifyActionCallback) notification_approve_cb,
191 case EMPATHY_EVENT_TYPE_CALL:
192 video = tp_call_channel_has_initial_video (
193 TP_CALL_CHANNEL (self->priv->event->handler_instance), NULL);
195 notify_notification_add_action (notification,
196 "reject", _("Reject"), (NotifyActionCallback) notification_decline_cb,
199 if (video && self->priv->event->type == EMPATHY_EVENT_TYPE_CALL)
200 notify_notification_add_action (notification,
201 "answer-no-video", _("Answer"),
202 (NotifyActionCallback) notification_approve_no_video_cb,
205 notify_notification_add_action (notification,
206 "answer", video ? _("Answer with video") : _("Answer"),
207 (NotifyActionCallback) notification_approve_cb,
211 case EMPATHY_EVENT_TYPE_TRANSFER:
212 case EMPATHY_EVENT_TYPE_INVITATION:
213 notify_notification_add_action (notification,
214 "decline", _("Decline"), (NotifyActionCallback) notification_decline_cb,
217 notify_notification_add_action (notification,
218 "accept", _("Accept"), (NotifyActionCallback) notification_approve_cb,
222 case EMPATHY_EVENT_TYPE_SUBSCRIPTION:
223 notify_notification_add_action (notification,
224 "decline", _("Decline"),
225 (NotifyActionCallback) notification_decline_subscription_cb,
228 notify_notification_add_action (notification,
229 "accept", _("Accept"),
230 (NotifyActionCallback) notification_accept_subscription_cb,
234 case EMPATHY_EVENT_TYPE_AUTH:
235 notify_notification_add_action (notification,
236 /* translators: the 'Provide' button is displayed in a notification
237 * bubble when Empathy is asking for an account password; clicking on it
238 * brings the password popup. */
239 "provide", _("Provide"), (NotifyActionCallback) notification_approve_cb,
249 notification_is_urgent (EmpathyNotificationsApprover *self,
250 NotifyNotification *notification)
252 /* Mark as urgent all the notifications with which user should
254 switch (self->priv->event->type) {
255 case EMPATHY_EVENT_TYPE_CHAT:
256 case EMPATHY_EVENT_TYPE_CALL:
257 case EMPATHY_EVENT_TYPE_TRANSFER:
258 case EMPATHY_EVENT_TYPE_INVITATION:
259 case EMPATHY_EVENT_TYPE_AUTH:
262 case EMPATHY_EVENT_TYPE_SUBSCRIPTION:
263 case EMPATHY_EVENT_TYPE_PRESENCE_ONLINE:
264 case EMPATHY_EVENT_TYPE_PRESENCE_OFFLINE:
271 /* Use x-empathy as prefix for unofficial categories
272 * http://www.galago-project.org/specs/notification/0.9/x211.html */
274 get_category_for_event_type (EmpathyEventType type)
277 case EMPATHY_EVENT_TYPE_CHAT:
278 return "im.received";
279 case EMPATHY_EVENT_TYPE_PRESENCE_ONLINE:
280 return "presence.online";
281 case EMPATHY_EVENT_TYPE_PRESENCE_OFFLINE:
282 return "presence.offline";
283 case EMPATHY_EVENT_TYPE_CALL:
284 return "x-empathy.call.incoming";
285 case EMPATHY_EVENT_TYPE_TRANSFER:
286 return "x-empathy.transfer.incoming";
287 case EMPATHY_EVENT_TYPE_INVITATION:
288 return "x-empathy.im.room-invitation";
289 case EMPATHY_EVENT_TYPE_AUTH:
290 return "x-empathy.network.auth-request";
291 case EMPATHY_EVENT_TYPE_SUBSCRIPTION:
292 return "x-empathy.im.subscription-request";
299 update_notification (EmpathyNotificationsApprover *self)
301 GdkPixbuf *pixbuf = NULL;
302 gchar *message_esc = NULL;
303 gboolean has_x_canonical_append;
304 NotifyNotification *notification;
306 if (!empathy_notify_manager_notification_is_enabled (self->priv->notify_mgr))
308 /* always close the notification if this happens */
309 notification_close_helper (self);
313 if (self->priv->event == NULL)
315 notification_close_helper (self);
319 if (self->priv->event->message != NULL)
320 message_esc = g_markup_escape_text (self->priv->event->message, -1);
322 has_x_canonical_append = empathy_notify_manager_has_capability (
323 self->priv->notify_mgr, EMPATHY_NOTIFY_MANAGER_CAP_X_CANONICAL_APPEND);
325 if (self->priv->notification != NULL && ! has_x_canonical_append)
327 /* if the notification server does NOT supports x-canonical-append, it is
328 * better to not use notify_notification_update to avoid
329 * overwriting the current notification message */
330 notification = g_object_ref (self->priv->notification);
332 notify_notification_update (notification,
333 self->priv->event->header, message_esc, NULL);
337 const gchar *category;
339 /* if the notification server supports x-canonical-append,
340 * the hint will be added, so that the message from the
341 * just created notification will be automatically appended
342 * to an existing notification with the same title.
343 * In this way the previous message will not be lost: the new
344 * message will appear below it, in the same notification */
345 notification = notify_notification_new (self->priv->event->header,
348 if (self->priv->notification == NULL)
350 self->priv->notification = g_object_ref (notification);
352 g_signal_connect (notification, "closed",
353 G_CALLBACK (notification_closed_cb), self);
356 notify_notification_set_timeout (notification,
357 NOTIFY_EXPIRES_DEFAULT);
359 if (has_x_canonical_append)
361 notify_notification_set_hint (notification,
362 EMPATHY_NOTIFY_MANAGER_CAP_X_CANONICAL_APPEND,
363 g_variant_new_boolean (TRUE));
366 if (empathy_notify_manager_has_capability (self->priv->notify_mgr,
367 EMPATHY_NOTIFY_MANAGER_CAP_ACTIONS))
368 add_notification_actions (self, notification);
370 if (notification_is_urgent (self, notification))
371 notify_notification_set_urgency (notification, NOTIFY_URGENCY_CRITICAL);
373 category = get_category_for_event_type (self->priv->event->type);
374 if (category != NULL)
376 notify_notification_set_hint (notification,
377 EMPATHY_NOTIFY_MANAGER_CAP_CATEGORY,
378 g_variant_new_string (category));
382 pixbuf = empathy_notify_manager_get_pixbuf_for_notification (
383 self->priv->notify_mgr, self->priv->event->contact,
384 self->priv->event->icon_name);
388 notify_notification_set_image_from_pixbuf (notification, pixbuf);
389 g_object_unref (pixbuf);
392 notify_notification_show (notification, NULL);
394 g_free (message_esc);
395 g_object_unref (notification);
399 event_added_cb (EmpathyEventManager *manager,
401 EmpathyNotificationsApprover *self)
403 if (self->priv->event != NULL)
406 self->priv->event = event;
408 update_notification (self);
412 event_removed_cb (EmpathyEventManager *manager,
414 EmpathyNotificationsApprover *self)
416 if (event != self->priv->event)
419 self->priv->event = empathy_event_manager_get_top_event (
420 self->priv->event_mgr);
422 update_notification (self);
426 event_updated_cb (EmpathyEventManager *manager,
428 EmpathyNotificationsApprover *self)
430 if (event != self->priv->event)
433 if (empathy_notify_manager_notification_is_enabled (self->priv->notify_mgr))
434 update_notification (self);
438 empathy_notifications_approver_init (EmpathyNotificationsApprover *self)
440 EmpathyNotificationsApproverPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
441 EMPATHY_TYPE_NOTIFICATIONS_APPROVER, EmpathyNotificationsApproverPrivate);
445 self->priv->event_mgr = empathy_event_manager_dup_singleton ();
446 self->priv->notify_mgr = empathy_notify_manager_dup_singleton ();
448 tp_g_signal_connect_object (self->priv->event_mgr, "event-added",
449 G_CALLBACK (event_added_cb), self, 0);
450 tp_g_signal_connect_object (priv->event_mgr, "event-removed",
451 G_CALLBACK (event_removed_cb), self, 0);
452 tp_g_signal_connect_object (priv->event_mgr, "event-updated",
453 G_CALLBACK (event_updated_cb), self, 0);
456 EmpathyNotificationsApprover *
457 empathy_notifications_approver_dup_singleton (void)
459 return g_object_new (EMPATHY_TYPE_NOTIFICATIONS_APPROVER, NULL);