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 <telepathy-yell/telepathy-yell.h>
30 #include <libempathy/empathy-contact-manager.h>
31 #include <libempathy/empathy-tp-streamed-media.h>
33 #include <libempathy-gtk/empathy-notify-manager.h>
35 #include "empathy-event-manager.h"
37 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
38 #include <libempathy/empathy-debug.h>
40 #include "empathy-notifications-approver.h"
42 struct _EmpathyNotificationsApproverPrivate
44 EmpathyEventManager *event_mgr;
45 EmpathyNotifyManager *notify_mgr;
47 NotifyNotification *notification;
51 G_DEFINE_TYPE (EmpathyNotificationsApprover, empathy_notifications_approver,
54 static EmpathyNotificationsApprover *notifications_approver = NULL;
57 notifications_approver_constructor (GType type,
58 guint n_construct_params,
59 GObjectConstructParam *construct_params)
63 if (notifications_approver != NULL)
64 return g_object_ref (notifications_approver);
66 retval = G_OBJECT_CLASS (empathy_notifications_approver_parent_class)->
67 constructor (type, n_construct_params, construct_params);
69 notifications_approver = EMPATHY_NOTIFICATIONS_APPROVER (retval);
70 g_object_add_weak_pointer (retval, (gpointer) ¬ifications_approver);
76 notifications_approver_dispose (GObject *object)
78 EmpathyNotificationsApprover *self = (EmpathyNotificationsApprover *) object;
80 tp_clear_object (&self->priv->event_mgr);
81 tp_clear_object (&self->priv->notify_mgr);
83 if (self->priv->notification != NULL)
85 notify_notification_close (self->priv->notification, NULL);
86 tp_clear_object (&self->priv->notification);
89 G_OBJECT_CLASS (empathy_notifications_approver_parent_class)->dispose (
94 empathy_notifications_approver_class_init (
95 EmpathyNotificationsApproverClass *klass)
97 GObjectClass *object_class = G_OBJECT_CLASS (klass);
99 object_class->dispose = notifications_approver_dispose;
100 object_class->constructor = notifications_approver_constructor;
102 g_type_class_add_private (object_class,
103 sizeof (EmpathyNotificationsApproverPrivate));
107 notification_closed_cb (NotifyNotification *notification,
108 EmpathyNotificationsApprover *self)
110 if (self->priv->notification == notification)
111 tp_clear_object (&self->priv->notification);
115 notification_close_helper (EmpathyNotificationsApprover *self)
117 if (self->priv->notification != NULL)
119 notify_notification_close (self->priv->notification, NULL);
120 tp_clear_object (&self->priv->notification);
125 notification_approve_no_video_cb (NotifyNotification *notification,
127 EmpathyNotificationsApprover *self)
129 if (self->priv->event)
131 tpy_call_channel_send_video (
132 TPY_CALL_CHANNEL (self->priv->event->handler_instance),
134 empathy_event_approve (self->priv->event);
139 notification_approve_cb (NotifyNotification *notification,
141 EmpathyNotificationsApprover *self)
143 if (self->priv->event != NULL)
144 empathy_event_approve (self->priv->event);
148 notification_decline_cb (NotifyNotification *notification,
150 EmpathyNotificationsApprover *self)
152 if (self->priv->event != NULL)
153 empathy_event_decline (self->priv->event);
157 notification_decline_subscription_cb (NotifyNotification *notification,
159 EmpathyNotificationsApprover *self)
161 EmpathyContactManager *manager;
163 if (self->priv->event == NULL)
166 manager = empathy_contact_manager_dup_singleton ();
167 empathy_contact_list_remove (EMPATHY_CONTACT_LIST (manager),
168 self->priv->event->contact, "");
170 empathy_event_remove (self->priv->event);
172 g_object_unref (manager);
176 notification_accept_subscription_cb (NotifyNotification *notification,
178 EmpathyNotificationsApprover *self)
180 EmpathyContactManager *manager;
182 if (self->priv->event == NULL)
185 manager = empathy_contact_manager_dup_singleton ();
186 empathy_contact_list_add (EMPATHY_CONTACT_LIST (manager),
187 self->priv->event->contact, "");
189 empathy_event_remove (self->priv->event);
191 g_object_unref (manager);
195 add_notification_actions (EmpathyNotificationsApprover *self,
196 NotifyNotification *notification)
200 switch (self->priv->event->type) {
201 case EMPATHY_EVENT_TYPE_CHAT:
202 notify_notification_add_action (notification,
203 "respond", _("Respond"), (NotifyActionCallback) notification_approve_cb,
207 case EMPATHY_EVENT_TYPE_VOIP:
208 case EMPATHY_EVENT_TYPE_CALL:
209 if (self->priv->event->type == EMPATHY_EVENT_TYPE_VOIP)
210 video = empathy_tp_streamed_media_has_initial_video (
211 EMPATHY_TP_STREAMED_MEDIA (self->priv->event->handler_instance));
213 video = tpy_call_channel_has_initial_video (
214 TPY_CALL_CHANNEL (self->priv->event->handler_instance));
216 notify_notification_add_action (notification,
217 "reject", _("Reject"), (NotifyActionCallback) notification_decline_cb,
220 if (video && self->priv->event->type == EMPATHY_EVENT_TYPE_CALL)
221 notify_notification_add_action (notification,
222 "answer-no-video", _("Answer"),
223 (NotifyActionCallback) notification_approve_no_video_cb,
226 notify_notification_add_action (notification,
227 "answer", video ? _("Answer with video") : _("Answer"),
228 (NotifyActionCallback) notification_approve_cb,
232 case EMPATHY_EVENT_TYPE_TRANSFER:
233 case EMPATHY_EVENT_TYPE_INVITATION:
234 notify_notification_add_action (notification,
235 "decline", _("Decline"), (NotifyActionCallback) notification_decline_cb,
238 notify_notification_add_action (notification,
239 "accept", _("Accept"), (NotifyActionCallback) notification_approve_cb,
243 case EMPATHY_EVENT_TYPE_SUBSCRIPTION:
244 notify_notification_add_action (notification,
245 "decline", _("Decline"),
246 (NotifyActionCallback) notification_decline_subscription_cb,
249 notify_notification_add_action (notification,
250 "accept", _("Accept"),
251 (NotifyActionCallback) notification_accept_subscription_cb,
255 case EMPATHY_EVENT_TYPE_AUTH:
256 notify_notification_add_action (notification,
257 /* translators: the 'Provide' button is displayed in a notification
258 * bubble when Empathy is asking for an account password; clicking on it
259 * brings the password popup. */
260 "provide", _("Provide"), (NotifyActionCallback) notification_approve_cb,
270 notification_is_urgent (EmpathyNotificationsApprover *self,
271 NotifyNotification *notification)
273 /* Mark as urgent all the notifications with which user should
275 switch (self->priv->event->type) {
276 case EMPATHY_EVENT_TYPE_CHAT:
277 case EMPATHY_EVENT_TYPE_VOIP:
278 case EMPATHY_EVENT_TYPE_CALL:
279 case EMPATHY_EVENT_TYPE_TRANSFER:
280 case EMPATHY_EVENT_TYPE_INVITATION:
281 case EMPATHY_EVENT_TYPE_AUTH:
284 case EMPATHY_EVENT_TYPE_SUBSCRIPTION:
285 case EMPATHY_EVENT_TYPE_PRESENCE_ONLINE:
286 case EMPATHY_EVENT_TYPE_PRESENCE_OFFLINE:
293 /* Use x-empathy as prefix for unofficial categories
294 * http://www.galago-project.org/specs/notification/0.9/x211.html */
296 get_category_for_event_type (EmpathyEventType type)
299 case EMPATHY_EVENT_TYPE_CHAT:
300 return "im.received";
301 case EMPATHY_EVENT_TYPE_PRESENCE_ONLINE:
302 return "presence.online";
303 case EMPATHY_EVENT_TYPE_PRESENCE_OFFLINE:
304 return "presence.offline";
305 case EMPATHY_EVENT_TYPE_VOIP:
306 case EMPATHY_EVENT_TYPE_CALL:
307 return "x-empathy.call.incoming";
308 case EMPATHY_EVENT_TYPE_TRANSFER:
309 return "x-empathy.transfer.incoming";
310 case EMPATHY_EVENT_TYPE_INVITATION:
311 return "x-empathy.im.room-invitation";
312 case EMPATHY_EVENT_TYPE_AUTH:
313 return "x-empathy.network.auth-request";
314 case EMPATHY_EVENT_TYPE_SUBSCRIPTION:
315 return "x-empathy.im.subscription-request";
322 update_notification (EmpathyNotificationsApprover *self)
324 GdkPixbuf *pixbuf = NULL;
325 gchar *message_esc = NULL;
326 gboolean has_x_canonical_append;
327 NotifyNotification *notification;
329 if (!empathy_notify_manager_notification_is_enabled (self->priv->notify_mgr))
331 /* always close the notification if this happens */
332 notification_close_helper (self);
336 if (self->priv->event == NULL)
338 notification_close_helper (self);
342 if (self->priv->event->message != NULL)
343 message_esc = g_markup_escape_text (self->priv->event->message, -1);
345 has_x_canonical_append = empathy_notify_manager_has_capability (
346 self->priv->notify_mgr, EMPATHY_NOTIFY_MANAGER_CAP_X_CANONICAL_APPEND);
348 if (self->priv->notification != NULL && ! has_x_canonical_append)
350 /* if the notification server does NOT supports x-canonical-append, it is
351 * better to not use notify_notification_update to avoid
352 * overwriting the current notification message */
353 notification = g_object_ref (self->priv->notification);
355 notify_notification_update (notification,
356 self->priv->event->header, message_esc, NULL);
360 const gchar *category;
362 /* if the notification server supports x-canonical-append,
363 * the hint will be added, so that the message from the
364 * just created notification will be automatically appended
365 * to an existing notification with the same title.
366 * In this way the previous message will not be lost: the new
367 * message will appear below it, in the same notification */
368 notification = notify_notification_new (self->priv->event->header,
371 if (self->priv->notification == NULL)
373 self->priv->notification = g_object_ref (notification);
375 g_signal_connect (notification, "closed",
376 G_CALLBACK (notification_closed_cb), self);
379 notify_notification_set_timeout (notification,
380 NOTIFY_EXPIRES_DEFAULT);
382 if (has_x_canonical_append)
384 notify_notification_set_hint (notification,
385 EMPATHY_NOTIFY_MANAGER_CAP_X_CANONICAL_APPEND,
386 g_variant_new_boolean (TRUE));
389 if (empathy_notify_manager_has_capability (self->priv->notify_mgr,
390 EMPATHY_NOTIFY_MANAGER_CAP_ACTIONS))
391 add_notification_actions (self, notification);
393 if (notification_is_urgent (self, notification))
394 notify_notification_set_urgency (notification, NOTIFY_URGENCY_CRITICAL);
396 category = get_category_for_event_type (self->priv->event->type);
397 if (category != NULL)
399 notify_notification_set_hint (notification,
400 EMPATHY_NOTIFY_MANAGER_CAP_CATEGORY,
401 g_variant_new_string (category));
405 pixbuf = empathy_notify_manager_get_pixbuf_for_notification (
406 self->priv->notify_mgr, self->priv->event->contact,
407 self->priv->event->icon_name);
411 notify_notification_set_image_from_pixbuf (notification, pixbuf);
412 g_object_unref (pixbuf);
415 notify_notification_show (notification, NULL);
417 g_free (message_esc);
418 g_object_unref (notification);
422 event_added_cb (EmpathyEventManager *manager,
424 EmpathyNotificationsApprover *self)
426 if (self->priv->event != NULL)
429 self->priv->event = event;
431 update_notification (self);
435 event_removed_cb (EmpathyEventManager *manager,
437 EmpathyNotificationsApprover *self)
439 if (event != self->priv->event)
442 self->priv->event = empathy_event_manager_get_top_event (
443 self->priv->event_mgr);
445 update_notification (self);
449 event_updated_cb (EmpathyEventManager *manager,
451 EmpathyNotificationsApprover *self)
453 if (event != self->priv->event)
456 if (empathy_notify_manager_notification_is_enabled (self->priv->notify_mgr))
457 update_notification (self);
461 empathy_notifications_approver_init (EmpathyNotificationsApprover *self)
463 EmpathyNotificationsApproverPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
464 EMPATHY_TYPE_NOTIFICATIONS_APPROVER, EmpathyNotificationsApproverPrivate);
468 self->priv->event_mgr = empathy_event_manager_dup_singleton ();
469 self->priv->notify_mgr = empathy_notify_manager_dup_singleton ();
471 tp_g_signal_connect_object (self->priv->event_mgr, "event-added",
472 G_CALLBACK (event_added_cb), self, 0);
473 tp_g_signal_connect_object (priv->event_mgr, "event-removed",
474 G_CALLBACK (event_removed_cb), self, 0);
475 tp_g_signal_connect_object (priv->event_mgr, "event-updated",
476 G_CALLBACK (event_updated_cb), self, 0);
479 EmpathyNotificationsApprover *
480 empathy_notifications_approver_dup_singleton (void)
482 return g_object_new (EMPATHY_TYPE_NOTIFICATIONS_APPROVER, NULL);