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 /* translators: the 'Provide' button is displayed in a notification
257 * bubble when Empathy is asking for an account password; clicking on it
258 * brings the password popup. */
259 notify_notification_add_action (notification,
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:
294 get_category_for_event_type (EmpathyEventType type)
297 case EMPATHY_EVENT_TYPE_CHAT:
298 return "im.received";
299 case EMPATHY_EVENT_TYPE_PRESENCE_ONLINE:
300 return "presence.online";
301 case EMPATHY_EVENT_TYPE_PRESENCE_OFFLINE:
302 return "presence.offline";
303 case EMPATHY_EVENT_TYPE_VOIP:
304 case EMPATHY_EVENT_TYPE_CALL:
305 case EMPATHY_EVENT_TYPE_TRANSFER:
306 case EMPATHY_EVENT_TYPE_INVITATION:
307 case EMPATHY_EVENT_TYPE_AUTH:
308 case EMPATHY_EVENT_TYPE_SUBSCRIPTION:
316 update_notification (EmpathyNotificationsApprover *self)
318 GdkPixbuf *pixbuf = NULL;
319 gchar *message_esc = NULL;
320 gboolean has_x_canonical_append;
321 NotifyNotification *notification;
323 if (!empathy_notify_manager_notification_is_enabled (self->priv->notify_mgr))
325 /* always close the notification if this happens */
326 notification_close_helper (self);
330 if (self->priv->event == NULL)
332 notification_close_helper (self);
336 if (self->priv->event->message != NULL)
337 message_esc = g_markup_escape_text (self->priv->event->message, -1);
339 has_x_canonical_append = empathy_notify_manager_has_capability (
340 self->priv->notify_mgr, EMPATHY_NOTIFY_MANAGER_CAP_X_CANONICAL_APPEND);
342 if (self->priv->notification != NULL && ! has_x_canonical_append)
344 /* if the notification server does NOT supports x-canonical-append, it is
345 * better to not use notify_notification_update to avoid
346 * overwriting the current notification message */
347 notification = g_object_ref (self->priv->notification);
349 notify_notification_update (notification,
350 self->priv->event->header, message_esc, NULL);
354 const gchar *category;
356 /* if the notification server supports x-canonical-append,
357 * the hint will be added, so that the message from the
358 * just created notification will be automatically appended
359 * to an existing notification with the same title.
360 * In this way the previous message will not be lost: the new
361 * message will appear below it, in the same notification */
362 notification = notify_notification_new (self->priv->event->header,
365 if (self->priv->notification == NULL)
367 self->priv->notification = g_object_ref (notification);
369 g_signal_connect (notification, "closed",
370 G_CALLBACK (notification_closed_cb), self);
373 notify_notification_set_timeout (notification,
374 NOTIFY_EXPIRES_DEFAULT);
376 if (has_x_canonical_append)
377 /* We have to set a not empty string to keep libnotify happy */
378 notify_notification_set_hint_string (notification,
379 EMPATHY_NOTIFY_MANAGER_CAP_X_CANONICAL_APPEND, "1");
381 if (empathy_notify_manager_has_capability (self->priv->notify_mgr,
382 EMPATHY_NOTIFY_MANAGER_CAP_ACTIONS))
383 add_notification_actions (self, notification);
385 if (notification_is_urgent (self, notification))
386 notify_notification_set_urgency (notification, NOTIFY_URGENCY_CRITICAL);
388 category = get_category_for_event_type (self->priv->event->type);
389 if (category != NULL)
390 notify_notification_set_hint_string (notification,
391 EMPATHY_NOTIFY_MANAGER_CAP_CATEGORY, category);
394 pixbuf = empathy_notify_manager_get_pixbuf_for_notification (
395 self->priv->notify_mgr, self->priv->event->contact,
396 self->priv->event->icon_name);
400 notify_notification_set_icon_from_pixbuf (notification, pixbuf);
401 g_object_unref (pixbuf);
404 notify_notification_show (notification, NULL);
406 g_free (message_esc);
407 g_object_unref (notification);
411 event_added_cb (EmpathyEventManager *manager,
413 EmpathyNotificationsApprover *self)
415 if (self->priv->event != NULL)
418 self->priv->event = event;
420 update_notification (self);
424 event_removed_cb (EmpathyEventManager *manager,
426 EmpathyNotificationsApprover *self)
428 if (event != self->priv->event)
431 self->priv->event = empathy_event_manager_get_top_event (
432 self->priv->event_mgr);
434 update_notification (self);
438 event_updated_cb (EmpathyEventManager *manager,
440 EmpathyNotificationsApprover *self)
442 if (event != self->priv->event)
445 if (empathy_notify_manager_notification_is_enabled (self->priv->notify_mgr))
446 update_notification (self);
450 empathy_notifications_approver_init (EmpathyNotificationsApprover *self)
452 EmpathyNotificationsApproverPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
453 EMPATHY_TYPE_NOTIFICATIONS_APPROVER, EmpathyNotificationsApproverPrivate);
457 self->priv->event_mgr = empathy_event_manager_dup_singleton ();
458 self->priv->notify_mgr = empathy_notify_manager_dup_singleton ();
460 tp_g_signal_connect_object (self->priv->event_mgr, "event-added",
461 G_CALLBACK (event_added_cb), self, 0);
462 tp_g_signal_connect_object (priv->event_mgr, "event-removed",
463 G_CALLBACK (event_removed_cb), self, 0);
464 tp_g_signal_connect_object (priv->event_mgr, "event-updated",
465 G_CALLBACK (event_updated_cb), self, 0);
468 EmpathyNotificationsApprover *
469 empathy_notifications_approver_dup_singleton (void)
471 return g_object_new (EMPATHY_TYPE_NOTIFICATIONS_APPROVER, NULL);