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-tp-streamed-media.h>
30 #include <libempathy-gtk/empathy-notify-manager.h>
31 #include <libempathy-gtk/empathy-call-utils.h>
33 #include "empathy-event-manager.h"
35 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
36 #include <libempathy/empathy-debug.h>
38 #include "empathy-notifications-approver.h"
40 struct _EmpathyNotificationsApproverPrivate
42 EmpathyEventManager *event_mgr;
43 EmpathyNotifyManager *notify_mgr;
45 NotifyNotification *notification;
49 G_DEFINE_TYPE (EmpathyNotificationsApprover, empathy_notifications_approver,
52 static EmpathyNotificationsApprover *notifications_approver = NULL;
55 notifications_approver_constructor (GType type,
56 guint n_construct_params,
57 GObjectConstructParam *construct_params)
61 if (notifications_approver != NULL)
62 return g_object_ref (notifications_approver);
64 retval = G_OBJECT_CLASS (empathy_notifications_approver_parent_class)->
65 constructor (type, n_construct_params, construct_params);
67 notifications_approver = EMPATHY_NOTIFICATIONS_APPROVER (retval);
68 g_object_add_weak_pointer (retval, (gpointer) ¬ifications_approver);
74 notifications_approver_dispose (GObject *object)
76 EmpathyNotificationsApprover *self = (EmpathyNotificationsApprover *) object;
78 tp_clear_object (&self->priv->event_mgr);
79 tp_clear_object (&self->priv->notify_mgr);
81 if (self->priv->notification != NULL)
83 notify_notification_close (self->priv->notification, NULL);
84 tp_clear_object (&self->priv->notification);
87 G_OBJECT_CLASS (empathy_notifications_approver_parent_class)->dispose (
92 empathy_notifications_approver_class_init (
93 EmpathyNotificationsApproverClass *klass)
95 GObjectClass *object_class = G_OBJECT_CLASS (klass);
97 object_class->dispose = notifications_approver_dispose;
98 object_class->constructor = notifications_approver_constructor;
100 g_type_class_add_private (object_class,
101 sizeof (EmpathyNotificationsApproverPrivate));
105 notification_closed_cb (NotifyNotification *notification,
106 EmpathyNotificationsApprover *self)
108 if (self->priv->notification == notification)
109 tp_clear_object (&self->priv->notification);
113 notification_close_helper (EmpathyNotificationsApprover *self)
115 if (self->priv->notification != NULL)
117 notify_notification_close (self->priv->notification, NULL);
118 tp_clear_object (&self->priv->notification);
123 notification_approve_no_video_cb (NotifyNotification *notification,
125 EmpathyNotificationsApprover *self)
127 if (self->priv->event)
129 empathy_call_channel_send_video (
130 TP_CALL_CHANNEL (self->priv->event->handler_instance),
132 empathy_event_approve (self->priv->event);
137 notification_approve_cb (NotifyNotification *notification,
139 EmpathyNotificationsApprover *self)
141 if (self->priv->event != NULL)
142 empathy_event_approve (self->priv->event);
146 notification_decline_cb (NotifyNotification *notification,
148 EmpathyNotificationsApprover *self)
150 if (self->priv->event != NULL)
151 empathy_event_decline (self->priv->event);
155 notification_decline_subscription_cb (NotifyNotification *notification,
157 EmpathyNotificationsApprover *self)
159 if (self->priv->event == NULL)
162 empathy_contact_remove_from_contact_list (self->priv->event->contact);
164 empathy_event_remove (self->priv->event);
168 notification_accept_subscription_cb (NotifyNotification *notification,
170 EmpathyNotificationsApprover *self)
172 if (self->priv->event == NULL)
175 empathy_contact_add_to_contact_list (self->priv->event->contact, "");
177 empathy_event_remove (self->priv->event);
181 add_notification_actions (EmpathyNotificationsApprover *self,
182 NotifyNotification *notification)
186 switch (self->priv->event->type) {
187 case EMPATHY_EVENT_TYPE_CHAT:
188 notify_notification_add_action (notification,
189 "respond", _("Respond"), (NotifyActionCallback) notification_approve_cb,
193 case EMPATHY_EVENT_TYPE_VOIP:
194 case EMPATHY_EVENT_TYPE_CALL:
195 if (self->priv->event->type == EMPATHY_EVENT_TYPE_VOIP)
196 video = empathy_tp_streamed_media_has_initial_video (
197 EMPATHY_TP_STREAMED_MEDIA (self->priv->event->handler_instance));
199 video = tp_call_channel_has_initial_video (
200 TP_CALL_CHANNEL (self->priv->event->handler_instance), NULL);
202 notify_notification_add_action (notification,
203 "reject", _("Reject"), (NotifyActionCallback) notification_decline_cb,
206 if (video && self->priv->event->type == EMPATHY_EVENT_TYPE_CALL)
207 notify_notification_add_action (notification,
208 "answer-no-video", _("Answer"),
209 (NotifyActionCallback) notification_approve_no_video_cb,
212 notify_notification_add_action (notification,
213 "answer", video ? _("Answer with video") : _("Answer"),
214 (NotifyActionCallback) notification_approve_cb,
218 case EMPATHY_EVENT_TYPE_TRANSFER:
219 case EMPATHY_EVENT_TYPE_INVITATION:
220 notify_notification_add_action (notification,
221 "decline", _("Decline"), (NotifyActionCallback) notification_decline_cb,
224 notify_notification_add_action (notification,
225 "accept", _("Accept"), (NotifyActionCallback) notification_approve_cb,
229 case EMPATHY_EVENT_TYPE_SUBSCRIPTION:
230 notify_notification_add_action (notification,
231 "decline", _("Decline"),
232 (NotifyActionCallback) notification_decline_subscription_cb,
235 notify_notification_add_action (notification,
236 "accept", _("Accept"),
237 (NotifyActionCallback) notification_accept_subscription_cb,
241 case EMPATHY_EVENT_TYPE_AUTH:
242 notify_notification_add_action (notification,
243 /* translators: the 'Provide' button is displayed in a notification
244 * bubble when Empathy is asking for an account password; clicking on it
245 * brings the password popup. */
246 "provide", _("Provide"), (NotifyActionCallback) notification_approve_cb,
256 notification_is_urgent (EmpathyNotificationsApprover *self,
257 NotifyNotification *notification)
259 /* Mark as urgent all the notifications with which user should
261 switch (self->priv->event->type) {
262 case EMPATHY_EVENT_TYPE_CHAT:
263 case EMPATHY_EVENT_TYPE_VOIP:
264 case EMPATHY_EVENT_TYPE_CALL:
265 case EMPATHY_EVENT_TYPE_TRANSFER:
266 case EMPATHY_EVENT_TYPE_INVITATION:
267 case EMPATHY_EVENT_TYPE_AUTH:
270 case EMPATHY_EVENT_TYPE_SUBSCRIPTION:
271 case EMPATHY_EVENT_TYPE_PRESENCE_ONLINE:
272 case EMPATHY_EVENT_TYPE_PRESENCE_OFFLINE:
279 /* Use x-empathy as prefix for unofficial categories
280 * http://www.galago-project.org/specs/notification/0.9/x211.html */
282 get_category_for_event_type (EmpathyEventType type)
285 case EMPATHY_EVENT_TYPE_CHAT:
286 return "im.received";
287 case EMPATHY_EVENT_TYPE_PRESENCE_ONLINE:
288 return "presence.online";
289 case EMPATHY_EVENT_TYPE_PRESENCE_OFFLINE:
290 return "presence.offline";
291 case EMPATHY_EVENT_TYPE_VOIP:
292 case EMPATHY_EVENT_TYPE_CALL:
293 return "x-empathy.call.incoming";
294 case EMPATHY_EVENT_TYPE_TRANSFER:
295 return "x-empathy.transfer.incoming";
296 case EMPATHY_EVENT_TYPE_INVITATION:
297 return "x-empathy.im.room-invitation";
298 case EMPATHY_EVENT_TYPE_AUTH:
299 return "x-empathy.network.auth-request";
300 case EMPATHY_EVENT_TYPE_SUBSCRIPTION:
301 return "x-empathy.im.subscription-request";
308 update_notification (EmpathyNotificationsApprover *self)
310 GdkPixbuf *pixbuf = NULL;
311 gchar *message_esc = NULL;
312 gboolean has_x_canonical_append;
313 NotifyNotification *notification;
315 if (!empathy_notify_manager_notification_is_enabled (self->priv->notify_mgr))
317 /* always close the notification if this happens */
318 notification_close_helper (self);
322 if (self->priv->event == NULL)
324 notification_close_helper (self);
328 if (self->priv->event->message != NULL)
329 message_esc = g_markup_escape_text (self->priv->event->message, -1);
331 has_x_canonical_append = empathy_notify_manager_has_capability (
332 self->priv->notify_mgr, EMPATHY_NOTIFY_MANAGER_CAP_X_CANONICAL_APPEND);
334 if (self->priv->notification != NULL && ! has_x_canonical_append)
336 /* if the notification server does NOT supports x-canonical-append, it is
337 * better to not use notify_notification_update to avoid
338 * overwriting the current notification message */
339 notification = g_object_ref (self->priv->notification);
341 notify_notification_update (notification,
342 self->priv->event->header, message_esc, NULL);
346 const gchar *category;
348 /* if the notification server supports x-canonical-append,
349 * the hint will be added, so that the message from the
350 * just created notification will be automatically appended
351 * to an existing notification with the same title.
352 * In this way the previous message will not be lost: the new
353 * message will appear below it, in the same notification */
354 notification = notify_notification_new (self->priv->event->header,
357 if (self->priv->notification == NULL)
359 self->priv->notification = g_object_ref (notification);
361 g_signal_connect (notification, "closed",
362 G_CALLBACK (notification_closed_cb), self);
365 notify_notification_set_timeout (notification,
366 NOTIFY_EXPIRES_DEFAULT);
368 if (has_x_canonical_append)
370 notify_notification_set_hint (notification,
371 EMPATHY_NOTIFY_MANAGER_CAP_X_CANONICAL_APPEND,
372 g_variant_new_boolean (TRUE));
375 if (empathy_notify_manager_has_capability (self->priv->notify_mgr,
376 EMPATHY_NOTIFY_MANAGER_CAP_ACTIONS))
377 add_notification_actions (self, notification);
379 if (notification_is_urgent (self, notification))
380 notify_notification_set_urgency (notification, NOTIFY_URGENCY_CRITICAL);
382 category = get_category_for_event_type (self->priv->event->type);
383 if (category != NULL)
385 notify_notification_set_hint (notification,
386 EMPATHY_NOTIFY_MANAGER_CAP_CATEGORY,
387 g_variant_new_string (category));
391 pixbuf = empathy_notify_manager_get_pixbuf_for_notification (
392 self->priv->notify_mgr, self->priv->event->contact,
393 self->priv->event->icon_name);
397 notify_notification_set_image_from_pixbuf (notification, pixbuf);
398 g_object_unref (pixbuf);
401 notify_notification_show (notification, NULL);
403 g_free (message_esc);
404 g_object_unref (notification);
408 event_added_cb (EmpathyEventManager *manager,
410 EmpathyNotificationsApprover *self)
412 if (self->priv->event != NULL)
415 self->priv->event = event;
417 update_notification (self);
421 event_removed_cb (EmpathyEventManager *manager,
423 EmpathyNotificationsApprover *self)
425 if (event != self->priv->event)
428 self->priv->event = empathy_event_manager_get_top_event (
429 self->priv->event_mgr);
431 update_notification (self);
435 event_updated_cb (EmpathyEventManager *manager,
437 EmpathyNotificationsApprover *self)
439 if (event != self->priv->event)
442 if (empathy_notify_manager_notification_is_enabled (self->priv->notify_mgr))
443 update_notification (self);
447 empathy_notifications_approver_init (EmpathyNotificationsApprover *self)
449 EmpathyNotificationsApproverPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
450 EMPATHY_TYPE_NOTIFICATIONS_APPROVER, EmpathyNotificationsApproverPrivate);
454 self->priv->event_mgr = empathy_event_manager_dup_singleton ();
455 self->priv->notify_mgr = empathy_notify_manager_dup_singleton ();
457 tp_g_signal_connect_object (self->priv->event_mgr, "event-added",
458 G_CALLBACK (event_added_cb), self, 0);
459 tp_g_signal_connect_object (priv->event_mgr, "event-removed",
460 G_CALLBACK (event_removed_cb), self, 0);
461 tp_g_signal_connect_object (priv->event_mgr, "event-updated",
462 G_CALLBACK (event_updated_cb), self, 0);
465 EmpathyNotificationsApprover *
466 empathy_notifications_approver_dup_singleton (void)
468 return g_object_new (EMPATHY_TYPE_NOTIFICATIONS_APPROVER, NULL);