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>
22 #include <glib/gi18n.h>
24 #include <libempathy-gtk/empathy-notify-manager.h>
25 #include <libempathy-gtk/empathy-call-utils.h>
27 #include "empathy-event-manager.h"
29 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
30 #include <libempathy/empathy-debug.h>
32 #include "empathy-notifications-approver.h"
34 struct _EmpathyNotificationsApproverPrivate
36 EmpathyEventManager *event_mgr;
37 EmpathyNotifyManager *notify_mgr;
39 NotifyNotification *notification;
43 G_DEFINE_TYPE (EmpathyNotificationsApprover, empathy_notifications_approver,
46 static EmpathyNotificationsApprover *notifications_approver = NULL;
49 notifications_approver_constructor (GType type,
50 guint n_construct_params,
51 GObjectConstructParam *construct_params)
55 if (notifications_approver != NULL)
56 return g_object_ref (notifications_approver);
58 retval = G_OBJECT_CLASS (empathy_notifications_approver_parent_class)->
59 constructor (type, n_construct_params, construct_params);
61 notifications_approver = EMPATHY_NOTIFICATIONS_APPROVER (retval);
62 g_object_add_weak_pointer (retval, (gpointer) ¬ifications_approver);
68 notifications_approver_dispose (GObject *object)
70 EmpathyNotificationsApprover *self = (EmpathyNotificationsApprover *) object;
72 tp_clear_object (&self->priv->event_mgr);
73 tp_clear_object (&self->priv->notify_mgr);
75 if (self->priv->notification != NULL)
77 notify_notification_close (self->priv->notification, NULL);
78 tp_clear_object (&self->priv->notification);
81 G_OBJECT_CLASS (empathy_notifications_approver_parent_class)->dispose (
86 empathy_notifications_approver_class_init (
87 EmpathyNotificationsApproverClass *klass)
89 GObjectClass *object_class = G_OBJECT_CLASS (klass);
91 object_class->dispose = notifications_approver_dispose;
92 object_class->constructor = notifications_approver_constructor;
94 g_type_class_add_private (object_class,
95 sizeof (EmpathyNotificationsApproverPrivate));
99 notification_closed_cb (NotifyNotification *notification,
100 EmpathyNotificationsApprover *self)
102 if (self->priv->notification == notification)
103 tp_clear_object (&self->priv->notification);
107 notification_close_helper (EmpathyNotificationsApprover *self)
109 if (self->priv->notification != NULL)
111 notify_notification_close (self->priv->notification, NULL);
112 tp_clear_object (&self->priv->notification);
117 notification_approve_no_video_cb (NotifyNotification *notification,
119 EmpathyNotificationsApprover *self)
121 if (self->priv->event)
123 empathy_call_channel_send_video (
124 TP_CALL_CHANNEL (self->priv->event->handler_instance),
126 empathy_event_approve (self->priv->event);
131 notification_approve_cb (NotifyNotification *notification,
133 EmpathyNotificationsApprover *self)
135 if (self->priv->event != NULL)
136 empathy_event_approve (self->priv->event);
140 notification_decline_cb (NotifyNotification *notification,
142 EmpathyNotificationsApprover *self)
144 if (self->priv->event != NULL)
145 empathy_event_decline (self->priv->event);
149 notification_decline_subscription_cb (NotifyNotification *notification,
151 EmpathyNotificationsApprover *self)
153 if (self->priv->event == NULL)
156 empathy_contact_remove_from_contact_list (self->priv->event->contact);
158 empathy_event_remove (self->priv->event);
162 notification_accept_subscription_cb (NotifyNotification *notification,
164 EmpathyNotificationsApprover *self)
166 if (self->priv->event == NULL)
169 empathy_contact_add_to_contact_list (self->priv->event->contact, "");
171 empathy_event_remove (self->priv->event);
175 add_notification_actions (EmpathyNotificationsApprover *self,
176 NotifyNotification *notification)
180 switch (self->priv->event->type) {
181 case EMPATHY_EVENT_TYPE_CHAT:
182 case EMPATHY_EVENT_TYPE_MENTIONED:
183 notify_notification_add_action (notification,
184 "respond", _("Respond"), (NotifyActionCallback) notification_approve_cb,
188 case EMPATHY_EVENT_TYPE_CALL:
189 video = tp_call_channel_has_initial_video (
190 TP_CALL_CHANNEL (self->priv->event->handler_instance), NULL);
192 notify_notification_add_action (notification,
193 "reject", _("Reject"), (NotifyActionCallback) notification_decline_cb,
196 if (video && self->priv->event->type == EMPATHY_EVENT_TYPE_CALL)
197 notify_notification_add_action (notification,
198 "answer-no-video", _("Answer"),
199 (NotifyActionCallback) notification_approve_no_video_cb,
202 notify_notification_add_action (notification,
203 "answer", video ? _("Answer with video") : _("Answer"),
204 (NotifyActionCallback) notification_approve_cb,
208 case EMPATHY_EVENT_TYPE_TRANSFER:
209 case EMPATHY_EVENT_TYPE_INVITATION:
210 notify_notification_add_action (notification,
211 "decline", _("Decline"), (NotifyActionCallback) notification_decline_cb,
214 notify_notification_add_action (notification,
215 "accept", _("Accept"), (NotifyActionCallback) notification_approve_cb,
219 case EMPATHY_EVENT_TYPE_SUBSCRIPTION:
220 notify_notification_add_action (notification,
221 "decline", _("Decline"),
222 (NotifyActionCallback) notification_decline_subscription_cb,
225 notify_notification_add_action (notification,
226 "accept", _("Accept"),
227 (NotifyActionCallback) notification_accept_subscription_cb,
231 case EMPATHY_EVENT_TYPE_AUTH:
232 notify_notification_add_action (notification,
233 /* translators: the 'Provide' button is displayed in a notification
234 * bubble when Empathy is asking for an account password; clicking on it
235 * brings the password popup. */
236 "provide", _("Provide"), (NotifyActionCallback) notification_approve_cb,
246 notification_is_urgent (EmpathyNotificationsApprover *self,
247 NotifyNotification *notification)
249 /* Mark as urgent all the notifications with which user should
251 switch (self->priv->event->type) {
252 case EMPATHY_EVENT_TYPE_CHAT:
253 case EMPATHY_EVENT_TYPE_CALL:
254 case EMPATHY_EVENT_TYPE_TRANSFER:
255 case EMPATHY_EVENT_TYPE_INVITATION:
256 case EMPATHY_EVENT_TYPE_AUTH:
257 case EMPATHY_EVENT_TYPE_MENTIONED:
260 case EMPATHY_EVENT_TYPE_SUBSCRIPTION:
261 case EMPATHY_EVENT_TYPE_PRESENCE_ONLINE:
262 case EMPATHY_EVENT_TYPE_PRESENCE_OFFLINE:
270 get_category_for_event_type (EmpathyEventType type)
273 case EMPATHY_EVENT_TYPE_##x: \
274 return EMPATHY_NOTIFICATION_CATEGORY_##x;
277 CASE(PRESENCE_ONLINE)
278 CASE(PRESENCE_OFFLINE)
292 update_notification (EmpathyNotificationsApprover *self)
294 GdkPixbuf *pixbuf = NULL;
295 gchar *message_esc = NULL;
296 gboolean has_x_canonical_append;
297 NotifyNotification *notification;
299 if (!empathy_notify_manager_notification_is_enabled (self->priv->notify_mgr))
301 /* always close the notification if this happens */
302 notification_close_helper (self);
306 if (self->priv->event == NULL)
308 notification_close_helper (self);
312 if (self->priv->event->message != NULL)
313 message_esc = g_markup_escape_text (self->priv->event->message, -1);
315 has_x_canonical_append = empathy_notify_manager_has_capability (
316 self->priv->notify_mgr, EMPATHY_NOTIFY_MANAGER_CAP_X_CANONICAL_APPEND);
318 if (self->priv->notification != NULL && ! has_x_canonical_append)
320 /* if the notification server does NOT supports x-canonical-append, it is
321 * better to not use notify_notification_update to avoid
322 * overwriting the current notification message */
323 notification = g_object_ref (self->priv->notification);
325 notify_notification_update (notification,
326 self->priv->event->header, message_esc, NULL);
330 const gchar *category;
332 /* if the notification server supports x-canonical-append,
333 * the hint will be added, so that the message from the
334 * just created notification will be automatically appended
335 * to an existing notification with the same title.
336 * In this way the previous message will not be lost: the new
337 * message will appear below it, in the same notification */
338 notification = empathy_notify_manager_create_notification (
339 self->priv->event->header, message_esc, NULL);
341 if (self->priv->notification == NULL)
343 self->priv->notification = g_object_ref (notification);
345 g_signal_connect (notification, "closed",
346 G_CALLBACK (notification_closed_cb), self);
349 if (has_x_canonical_append)
351 notify_notification_set_hint (notification,
352 EMPATHY_NOTIFY_MANAGER_CAP_X_CANONICAL_APPEND,
353 g_variant_new_boolean (TRUE));
356 if (empathy_notify_manager_has_capability (self->priv->notify_mgr,
357 EMPATHY_NOTIFY_MANAGER_CAP_ACTIONS))
358 add_notification_actions (self, notification);
360 if (notification_is_urgent (self, notification))
361 notify_notification_set_urgency (notification, NOTIFY_URGENCY_CRITICAL);
363 category = get_category_for_event_type (self->priv->event->type);
364 if (category != NULL)
366 notify_notification_set_hint (notification,
367 EMPATHY_NOTIFY_MANAGER_CAP_CATEGORY,
368 g_variant_new_string (category));
372 pixbuf = empathy_notify_manager_get_pixbuf_for_notification (
373 self->priv->notify_mgr, self->priv->event->contact,
374 self->priv->event->icon_name);
378 notify_notification_set_image_from_pixbuf (notification, pixbuf);
379 g_object_unref (pixbuf);
382 notify_notification_show (notification, NULL);
384 g_free (message_esc);
385 g_object_unref (notification);
389 event_added_cb (EmpathyEventManager *manager,
391 EmpathyNotificationsApprover *self)
393 if (self->priv->event != NULL)
396 self->priv->event = event;
398 update_notification (self);
402 event_removed_cb (EmpathyEventManager *manager,
404 EmpathyNotificationsApprover *self)
406 if (event != self->priv->event)
409 self->priv->event = empathy_event_manager_get_top_event (
410 self->priv->event_mgr);
412 update_notification (self);
416 event_updated_cb (EmpathyEventManager *manager,
418 EmpathyNotificationsApprover *self)
420 if (event != self->priv->event)
423 if (empathy_notify_manager_notification_is_enabled (self->priv->notify_mgr))
424 update_notification (self);
428 empathy_notifications_approver_init (EmpathyNotificationsApprover *self)
430 EmpathyNotificationsApproverPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
431 EMPATHY_TYPE_NOTIFICATIONS_APPROVER, EmpathyNotificationsApproverPrivate);
435 self->priv->event_mgr = empathy_event_manager_dup_singleton ();
436 self->priv->notify_mgr = empathy_notify_manager_dup_singleton ();
438 tp_g_signal_connect_object (self->priv->event_mgr, "event-added",
439 G_CALLBACK (event_added_cb), self, 0);
440 tp_g_signal_connect_object (priv->event_mgr, "event-removed",
441 G_CALLBACK (event_removed_cb), self, 0);
442 tp_g_signal_connect_object (priv->event_mgr, "event-updated",
443 G_CALLBACK (event_updated_cb), self, 0);
446 EmpathyNotificationsApprover *
447 empathy_notifications_approver_dup_singleton (void)
449 return g_object_new (EMPATHY_TYPE_NOTIFICATIONS_APPROVER, NULL);