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 case EMPATHY_EVENT_TYPE_MENTIONED:
187 notify_notification_add_action (notification,
188 "respond", _("Respond"), (NotifyActionCallback) notification_approve_cb,
192 case EMPATHY_EVENT_TYPE_CALL:
193 video = tp_call_channel_has_initial_video (
194 TP_CALL_CHANNEL (self->priv->event->handler_instance), NULL);
196 notify_notification_add_action (notification,
197 "reject", _("Reject"), (NotifyActionCallback) notification_decline_cb,
200 if (video && self->priv->event->type == EMPATHY_EVENT_TYPE_CALL)
201 notify_notification_add_action (notification,
202 "answer-no-video", _("Answer"),
203 (NotifyActionCallback) notification_approve_no_video_cb,
206 notify_notification_add_action (notification,
207 "answer", video ? _("Answer with video") : _("Answer"),
208 (NotifyActionCallback) notification_approve_cb,
212 case EMPATHY_EVENT_TYPE_TRANSFER:
213 case EMPATHY_EVENT_TYPE_INVITATION:
214 notify_notification_add_action (notification,
215 "decline", _("Decline"), (NotifyActionCallback) notification_decline_cb,
218 notify_notification_add_action (notification,
219 "accept", _("Accept"), (NotifyActionCallback) notification_approve_cb,
223 case EMPATHY_EVENT_TYPE_SUBSCRIPTION:
224 notify_notification_add_action (notification,
225 "decline", _("Decline"),
226 (NotifyActionCallback) notification_decline_subscription_cb,
229 notify_notification_add_action (notification,
230 "accept", _("Accept"),
231 (NotifyActionCallback) notification_accept_subscription_cb,
235 case EMPATHY_EVENT_TYPE_AUTH:
236 notify_notification_add_action (notification,
237 /* translators: the 'Provide' button is displayed in a notification
238 * bubble when Empathy is asking for an account password; clicking on it
239 * brings the password popup. */
240 "provide", _("Provide"), (NotifyActionCallback) notification_approve_cb,
250 notification_is_urgent (EmpathyNotificationsApprover *self,
251 NotifyNotification *notification)
253 /* Mark as urgent all the notifications with which user should
255 switch (self->priv->event->type) {
256 case EMPATHY_EVENT_TYPE_CHAT:
257 case EMPATHY_EVENT_TYPE_CALL:
258 case EMPATHY_EVENT_TYPE_TRANSFER:
259 case EMPATHY_EVENT_TYPE_INVITATION:
260 case EMPATHY_EVENT_TYPE_AUTH:
261 case EMPATHY_EVENT_TYPE_MENTIONED:
264 case EMPATHY_EVENT_TYPE_SUBSCRIPTION:
265 case EMPATHY_EVENT_TYPE_PRESENCE_ONLINE:
266 case EMPATHY_EVENT_TYPE_PRESENCE_OFFLINE:
274 get_category_for_event_type (EmpathyEventType type)
277 case EMPATHY_EVENT_TYPE_##x: \
278 return EMPATHY_NOTIFICATION_CATEGORY_##x;
281 CASE(PRESENCE_ONLINE)
282 CASE(PRESENCE_OFFLINE)
296 update_notification (EmpathyNotificationsApprover *self)
298 GdkPixbuf *pixbuf = NULL;
299 gchar *message_esc = NULL;
300 gboolean has_x_canonical_append;
301 NotifyNotification *notification;
303 if (!empathy_notify_manager_notification_is_enabled (self->priv->notify_mgr))
305 /* always close the notification if this happens */
306 notification_close_helper (self);
310 if (self->priv->event == NULL)
312 notification_close_helper (self);
316 if (self->priv->event->message != NULL)
317 message_esc = g_markup_escape_text (self->priv->event->message, -1);
319 has_x_canonical_append = empathy_notify_manager_has_capability (
320 self->priv->notify_mgr, EMPATHY_NOTIFY_MANAGER_CAP_X_CANONICAL_APPEND);
322 if (self->priv->notification != NULL && ! has_x_canonical_append)
324 /* if the notification server does NOT supports x-canonical-append, it is
325 * better to not use notify_notification_update to avoid
326 * overwriting the current notification message */
327 notification = g_object_ref (self->priv->notification);
329 notify_notification_update (notification,
330 self->priv->event->header, message_esc, NULL);
334 const gchar *category;
336 /* if the notification server supports x-canonical-append,
337 * the hint will be added, so that the message from the
338 * just created notification will be automatically appended
339 * to an existing notification with the same title.
340 * In this way the previous message will not be lost: the new
341 * message will appear below it, in the same notification */
342 notification = empathy_notify_manager_create_notification (
343 self->priv->event->header, message_esc, NULL);
345 if (self->priv->notification == NULL)
347 self->priv->notification = g_object_ref (notification);
349 g_signal_connect (notification, "closed",
350 G_CALLBACK (notification_closed_cb), self);
353 if (has_x_canonical_append)
355 notify_notification_set_hint (notification,
356 EMPATHY_NOTIFY_MANAGER_CAP_X_CANONICAL_APPEND,
357 g_variant_new_boolean (TRUE));
360 if (empathy_notify_manager_has_capability (self->priv->notify_mgr,
361 EMPATHY_NOTIFY_MANAGER_CAP_ACTIONS))
362 add_notification_actions (self, notification);
364 if (notification_is_urgent (self, notification))
365 notify_notification_set_urgency (notification, NOTIFY_URGENCY_CRITICAL);
367 category = get_category_for_event_type (self->priv->event->type);
368 if (category != NULL)
370 notify_notification_set_hint (notification,
371 EMPATHY_NOTIFY_MANAGER_CAP_CATEGORY,
372 g_variant_new_string (category));
376 pixbuf = empathy_notify_manager_get_pixbuf_for_notification (
377 self->priv->notify_mgr, self->priv->event->contact,
378 self->priv->event->icon_name);
382 notify_notification_set_image_from_pixbuf (notification, pixbuf);
383 g_object_unref (pixbuf);
386 notify_notification_show (notification, NULL);
388 g_free (message_esc);
389 g_object_unref (notification);
393 event_added_cb (EmpathyEventManager *manager,
395 EmpathyNotificationsApprover *self)
397 if (self->priv->event != NULL)
400 self->priv->event = event;
402 update_notification (self);
406 event_removed_cb (EmpathyEventManager *manager,
408 EmpathyNotificationsApprover *self)
410 if (event != self->priv->event)
413 self->priv->event = empathy_event_manager_get_top_event (
414 self->priv->event_mgr);
416 update_notification (self);
420 event_updated_cb (EmpathyEventManager *manager,
422 EmpathyNotificationsApprover *self)
424 if (event != self->priv->event)
427 if (empathy_notify_manager_notification_is_enabled (self->priv->notify_mgr))
428 update_notification (self);
432 empathy_notifications_approver_init (EmpathyNotificationsApprover *self)
434 EmpathyNotificationsApproverPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
435 EMPATHY_TYPE_NOTIFICATIONS_APPROVER, EmpathyNotificationsApproverPrivate);
439 self->priv->event_mgr = empathy_event_manager_dup_singleton ();
440 self->priv->notify_mgr = empathy_notify_manager_dup_singleton ();
442 tp_g_signal_connect_object (self->priv->event_mgr, "event-added",
443 G_CALLBACK (event_added_cb), self, 0);
444 tp_g_signal_connect_object (priv->event_mgr, "event-removed",
445 G_CALLBACK (event_removed_cb), self, 0);
446 tp_g_signal_connect_object (priv->event_mgr, "event-updated",
447 G_CALLBACK (event_updated_cb), self, 0);
450 EmpathyNotificationsApprover *
451 empathy_notifications_approver_dup_singleton (void)
453 return g_object_new (EMPATHY_TYPE_NOTIFICATIONS_APPROVER, NULL);