aboutsummaryrefslogtreecommitdiffstats
path: root/src/empathy-notifications-approver.c
diff options
context:
space:
mode:
authorGuillaume Desmottes <guillaume.desmottes@collabora.co.uk>2010-11-22 19:52:31 +0800
committerGuillaume Desmottes <guillaume.desmottes@collabora.co.uk>2010-11-23 18:33:20 +0800
commitdedc37ecea14c0752c8a23b77e73248dd8a6fe2b (patch)
tree3bdf23ddb48a5fd8f78b0eff4b62db3ecab63943 /src/empathy-notifications-approver.c
parent426b89e06c978222479694873ec24d4dc8d2d2df (diff)
downloadgsoc2013-empathy-dedc37ecea14c0752c8a23b77e73248dd8a6fe2b.tar
gsoc2013-empathy-dedc37ecea14c0752c8a23b77e73248dd8a6fe2b.tar.gz
gsoc2013-empathy-dedc37ecea14c0752c8a23b77e73248dd8a6fe2b.tar.bz2
gsoc2013-empathy-dedc37ecea14c0752c8a23b77e73248dd8a6fe2b.tar.lz
gsoc2013-empathy-dedc37ecea14c0752c8a23b77e73248dd8a6fe2b.tar.xz
gsoc2013-empathy-dedc37ecea14c0752c8a23b77e73248dd8a6fe2b.tar.zst
gsoc2013-empathy-dedc37ecea14c0752c8a23b77e73248dd8a6fe2b.zip
Move notifications code from status-icon to notifications-approver (#635500)
Diffstat (limited to 'src/empathy-notifications-approver.c')
-rw-r--r--src/empathy-notifications-approver.c260
1 files changed, 260 insertions, 0 deletions
diff --git a/src/empathy-notifications-approver.c b/src/empathy-notifications-approver.c
index 2b73ce80f..980a7d514 100644
--- a/src/empathy-notifications-approver.c
+++ b/src/empathy-notifications-approver.c
@@ -20,10 +20,13 @@
#include <config.h>
#include <string.h>
+#include <glib/gi18n.h>
#include <libnotify/notification.h>
#include <libnotify/notify.h>
#include <telepathy-glib/telepathy-glib.h>
+#include <libempathy/empathy-contact-manager.h>
+
#include <libempathy-gtk/empathy-notify-manager.h>
#include "empathy-event-manager.h"
@@ -37,6 +40,9 @@ struct _EmpathyNotificationsApproverPrivate
{
EmpathyEventManager *event_mgr;
EmpathyNotifyManager *notify_mgr;
+
+ NotifyNotification *notification;
+ EmpathyEvent *event;
};
G_DEFINE_TYPE (EmpathyNotificationsApprover, empathy_notifications_approver,
@@ -71,6 +77,12 @@ notifications_approver_dispose (GObject *object)
tp_clear_object (&self->priv->event_mgr);
tp_clear_object (&self->priv->notify_mgr);
+ if (self->priv->notification != NULL)
+ {
+ notify_notification_close (self->priv->notification, NULL);
+ tp_clear_object (&self->priv->notification);
+ }
+
G_OBJECT_CLASS (empathy_notifications_approver_parent_class)->dispose (
object);
}
@@ -89,6 +101,247 @@ empathy_notifications_approver_class_init (
}
static void
+notification_closed_cb (NotifyNotification *notification,
+ EmpathyNotificationsApprover *self)
+{
+ g_object_unref (notification);
+
+ if (self->priv->notification == notification)
+ self->priv->notification = NULL;
+}
+
+static void
+notification_close_helper (EmpathyNotificationsApprover *self)
+{
+ if (self->priv->notification != NULL)
+ {
+ notify_notification_close (self->priv->notification, NULL);
+ self->priv->notification = NULL;
+ }
+}
+
+static void
+notification_approve_cb (NotifyNotification *notification,
+ gchar *action,
+ EmpathyNotificationsApprover *self)
+{
+ if (self->priv->event != NULL)
+ empathy_event_approve (self->priv->event);
+}
+
+static void
+notification_decline_cb (NotifyNotification *notification,
+ gchar *action,
+ EmpathyNotificationsApprover *self)
+{
+ if (self->priv->event != NULL)
+ empathy_event_decline (self->priv->event);
+}
+
+static void
+notification_decline_subscription_cb (NotifyNotification *notification,
+ gchar *action,
+ EmpathyNotificationsApprover *self)
+{
+ EmpathyContactManager *manager;
+
+ if (self->priv->event == NULL)
+ return;
+
+ manager = empathy_contact_manager_dup_singleton ();
+ empathy_contact_list_remove (EMPATHY_CONTACT_LIST (manager),
+ self->priv->event->contact, "");
+
+ empathy_event_remove (self->priv->event);
+
+ g_object_unref (manager);
+}
+
+static void
+notification_accept_subscription_cb (NotifyNotification *notification,
+ gchar *action,
+ EmpathyNotificationsApprover *self)
+{
+ EmpathyContactManager *manager;
+
+ if (self->priv->event == NULL)
+ return;
+
+ manager = empathy_contact_manager_dup_singleton ();
+ empathy_contact_list_add (EMPATHY_CONTACT_LIST (manager),
+ self->priv->event->contact, "");
+
+ empathy_event_remove (self->priv->event);
+
+ g_object_unref (manager);
+}
+
+static void
+add_notification_actions (EmpathyNotificationsApprover *self,
+ NotifyNotification *notification)
+{
+ switch (self->priv->event->type) {
+ case EMPATHY_EVENT_TYPE_CHAT:
+ notify_notification_add_action (notification,
+ "respond", _("Respond"), (NotifyActionCallback) notification_approve_cb,
+ self, NULL);
+ break;
+
+ case EMPATHY_EVENT_TYPE_VOIP:
+ notify_notification_add_action (notification,
+ "reject", _("Reject"), (NotifyActionCallback) notification_decline_cb,
+ self, NULL);
+
+ notify_notification_add_action (notification,
+ "answer", _("Answer"), (NotifyActionCallback) notification_approve_cb,
+ self, NULL);
+ break;
+
+ case EMPATHY_EVENT_TYPE_TRANSFER:
+ case EMPATHY_EVENT_TYPE_INVITATION:
+ notify_notification_add_action (notification,
+ "decline", _("Decline"), (NotifyActionCallback) notification_decline_cb,
+ self, NULL);
+
+ notify_notification_add_action (notification,
+ "accept", _("Accept"), (NotifyActionCallback) notification_approve_cb,
+ self, NULL);
+ break;
+
+ case EMPATHY_EVENT_TYPE_SUBSCRIPTION:
+ notify_notification_add_action (notification,
+ "decline", _("Decline"),
+ (NotifyActionCallback) notification_decline_subscription_cb,
+ self, NULL);
+
+ notify_notification_add_action (notification,
+ "accept", _("Accept"),
+ (NotifyActionCallback) notification_accept_subscription_cb,
+ self, NULL);
+
+ default:
+ break;
+ }
+}
+
+static void
+update_notification (EmpathyNotificationsApprover *self)
+{
+ GdkPixbuf *pixbuf = NULL;
+ gchar *message_esc = NULL;
+ gboolean has_x_canonical_append;
+ NotifyNotification *notification = self->priv->notification;
+
+ if (!empathy_notify_manager_notification_is_enabled (self->priv->notify_mgr))
+ {
+ /* always close the notification if this happens */
+ notification_close_helper (self);
+ return;
+ }
+
+ if (self->priv->event == NULL)
+ {
+ notification_close_helper (self);
+ return;
+ }
+
+ if (self->priv->event->message != NULL)
+ message_esc = g_markup_escape_text (self->priv->event->message, -1);
+
+ has_x_canonical_append = empathy_notify_manager_has_capability (
+ self->priv->notify_mgr, EMPATHY_NOTIFY_MANAGER_CAP_X_CANONICAL_APPEND);
+
+ if (notification != NULL && ! has_x_canonical_append)
+ {
+ /* if the notification server supports x-canonical-append, it is
+ * better to not use notify_notification_update to avoid
+ * overwriting the current notification message */
+ notify_notification_update (notification,
+ self->priv->event->header, message_esc, NULL);
+ }
+ else
+ {
+ /* if the notification server supports x-canonical-append,
+ * the hint will be added, so that the message from the
+ * just created notification will be automatically appended
+ * to an existing notification with the same title.
+ * In this way the previous message will not be lost: the new
+ * message will appear below it, in the same notification */
+ notification = notify_notification_new (self->priv->event->header,
+ message_esc, NULL);
+
+ if (self->priv->notification == NULL)
+ self->priv->notification = notification;
+
+ notify_notification_set_timeout (notification,
+ NOTIFY_EXPIRES_DEFAULT);
+
+ if (has_x_canonical_append)
+ notify_notification_set_hint_string (notification,
+ EMPATHY_NOTIFY_MANAGER_CAP_X_CANONICAL_APPEND, "");
+
+ if (empathy_notify_manager_has_capability (self->priv->notify_mgr,
+ EMPATHY_NOTIFY_MANAGER_CAP_ACTIONS))
+ add_notification_actions (self, notification);
+
+ g_signal_connect (notification, "closed",
+ G_CALLBACK (notification_closed_cb), self);
+ }
+
+ pixbuf = empathy_notify_manager_get_pixbuf_for_notification (
+ self->priv->notify_mgr, self->priv->event->contact,
+ self->priv->event->icon_name);
+
+ if (pixbuf != NULL)
+ {
+ notify_notification_set_icon_from_pixbuf (notification, pixbuf);
+ g_object_unref (pixbuf);
+ }
+
+ notify_notification_show (notification, NULL);
+
+ g_free (message_esc);
+}
+
+static void
+event_added_cb (EmpathyEventManager *manager,
+ EmpathyEvent *event,
+ EmpathyNotificationsApprover *self)
+{
+ if (self->priv->event != NULL)
+ return;
+
+ self->priv->event = event;
+ update_notification (self);
+}
+
+static void
+event_removed_cb (EmpathyEventManager *manager,
+ EmpathyEvent *event,
+ EmpathyNotificationsApprover *self)
+{
+ if (event != self->priv->event)
+ return;
+
+ self->priv->event = empathy_event_manager_get_top_event (
+ self->priv->event_mgr);
+
+ update_notification (self);
+}
+
+static void
+event_updated_cb (EmpathyEventManager *manager,
+ EmpathyEvent *event,
+ EmpathyNotificationsApprover *self)
+{
+ if (event != self->priv->event)
+ return;
+
+ if (empathy_notify_manager_notification_is_enabled (self->priv->notify_mgr))
+ update_notification (self);
+}
+
+static void
empathy_notifications_approver_init (EmpathyNotificationsApprover *self)
{
EmpathyNotificationsApproverPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
@@ -98,6 +351,13 @@ empathy_notifications_approver_init (EmpathyNotificationsApprover *self)
self->priv->event_mgr = empathy_event_manager_dup_singleton ();
self->priv->notify_mgr = empathy_notify_manager_dup_singleton ();
+
+ g_signal_connect (self->priv->event_mgr, "event-added",
+ G_CALLBACK (event_added_cb), self);
+ g_signal_connect (priv->event_mgr, "event-removed",
+ G_CALLBACK (event_removed_cb), self);
+ g_signal_connect (priv->event_mgr, "event-updated",
+ G_CALLBACK (event_updated_cb), self);
}
EmpathyNotificationsApprover *