]> git.0d.be Git - empathy.git/blobdiff - src/empathy-status-icon.c
Escape to cancel from status editing
[empathy.git] / src / empathy-status-icon.c
index 07fc3aa02a2e76b7ee4bbe8f2783912f40f33ad2..6c0ddb2502b39b0b43b6dac6e9ee9688740348d5 100644 (file)
@@ -27,6 +27,8 @@
 #include <glade/glade.h>
 #include <gdk/gdkkeysyms.h>
 
+#include <libnotify/notification.h>
+
 #include <libempathy/empathy-utils.h>
 #include <libempathy/empathy-idle.h>
 #include <libempathy/empathy-account-manager.h>
@@ -41,6 +43,7 @@
 #include "empathy-status-icon.h"
 #include "empathy-preferences.h"
 #include "empathy-event-manager.h"
+#include "empathy-misc.h"
 
 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
 #include <libempathy/empathy-debug.h>
@@ -57,6 +60,7 @@ typedef struct {
        guint                blink_timeout;
        EmpathyEventManager *event_manager;
        EmpathyEvent        *event;
+       NotifyNotification  *notification;
 
        GtkWindow           *window;
        GtkWidget           *popup_menu;
@@ -67,21 +71,119 @@ typedef struct {
 
 G_DEFINE_TYPE (EmpathyStatusIcon, empathy_status_icon, G_TYPE_OBJECT);
 
+static gboolean
+activate_event (EmpathyEvent *event)
+{
+       empathy_event_activate (event);
+
+       return FALSE;
+}
+
 static void
-status_icon_update_tooltip (EmpathyStatusIcon *icon)
+status_icon_notification_closed_cb (NotifyNotification *notification,
+                                   EmpathyStatusIcon  *icon)
+{
+       EmpathyStatusIconPriv *priv = GET_PRIV (icon);
+       EmpathyNotificationClosedReason reason = 0;
+
+#ifdef notify_notification_get_closed_reason
+       reason = notify_notification_get_closed_reason (notification);
+#endif
+       if (priv->notification) {
+               g_object_unref (priv->notification);
+               priv->notification = NULL;
+       }
+
+       if (!priv->event) {
+               return;
+       }
+
+       /* the notification has been closed by the user, see the
+        * DesktopNotification spec.
+        */
+       if (reason == EMPATHY_NOTIFICATION_CLOSED_DISMISSED) {
+               /* use an idle here, as this callback is called from a
+                * DBus signal handler inside libnotify, and we might call
+                * a *_run_* method when activating the event.
+                */
+               g_idle_add ((GSourceFunc) activate_event, priv->event);
+       } else {
+               /* inhibit other updates for this event */
+               empathy_event_inhibit_updates (priv->event);
+       }
+}
+
+static void
+notification_close_helper (EmpathyStatusIconPriv *priv)
+{
+       if (priv->notification) {
+               notify_notification_close (priv->notification, NULL);
+               g_object_unref (priv->notification);
+               priv->notification = NULL;
+       }
+}
+
+static void
+status_icon_update_notification (EmpathyStatusIcon *icon)
 {
        EmpathyStatusIconPriv *priv = GET_PRIV (icon);
-       const gchar           *tooltip = NULL;
+       GdkPixbuf *pixbuf = NULL;
+
+       if (!empathy_notification_is_enabled ()) {
+               /* always close the notification if this happens */
+               notification_close_helper (priv);
+               return;
+       }
 
        if (priv->event) {
-               tooltip = priv->event->message;
+               pixbuf = empathy_misc_get_pixbuf_for_notification (priv->event->contact,
+                                                                  priv->event->icon_name);
+
+               if (priv->notification) {
+                       notify_notification_update (priv->notification,
+                                                   priv->event->header, priv->event->message,
+                                                   NULL);
+               } else {
+                       priv->notification = notify_notification_new_with_status_icon
+                               (priv->event->header, priv->event->message, NULL, priv->icon);
+                       notify_notification_set_timeout (priv->notification,
+                                                        NOTIFY_EXPIRES_DEFAULT);
+
+                       g_signal_connect (priv->notification, "closed",
+                                         G_CALLBACK (status_icon_notification_closed_cb), icon);
+
+               }
+               notify_notification_set_icon_from_pixbuf (priv->notification,
+                                                         pixbuf);
+               notify_notification_show (priv->notification, NULL);
+
+               g_object_unref (pixbuf);
+       } else {
+               notification_close_helper (priv);
        }
+}
 
-       if (!tooltip) {
-               tooltip = empathy_idle_get_status (priv->idle);
+static void
+status_icon_update_tooltip (EmpathyStatusIcon *icon)
+{
+       EmpathyStatusIconPriv *priv = GET_PRIV (icon);
+       gchar                 *tooltip = NULL;
+
+       if (priv->event) {
+               if (priv->event->message != NULL)
+                               tooltip = g_strdup_printf ("<i>%s</i>\n%s",
+                                                          priv->event->header,
+                                                          priv->event->message);
+               else
+                               tooltip = g_strdup_printf ("<i>%s</i>",
+                                                          priv->event->header);
+               gtk_status_icon_set_tooltip_markup (priv->icon, tooltip);
+       } else {
+               tooltip = g_strdup (empathy_idle_get_status (priv->idle));
+               gtk_status_icon_set_tooltip_text (priv->icon, tooltip);
        }
 
-       gtk_status_icon_set_tooltip (priv->icon, tooltip);      
+       g_free (tooltip);
 }
 
 static void
@@ -112,7 +214,6 @@ status_icon_blink_timeout_cb (EmpathyStatusIcon *icon)
 
        return TRUE;
 }
-
 static void
 status_icon_event_added_cb (EmpathyEventManager *manager,
                            EmpathyEvent        *event,
@@ -131,6 +232,7 @@ status_icon_event_added_cb (EmpathyEventManager *manager,
 
        status_icon_update_icon (icon);
        status_icon_update_tooltip (icon);
+       status_icon_update_notification (icon);
 
        if (!priv->blink_timeout) {
                priv->blink_timeout = g_timeout_add (BLINK_TIMEOUT,
@@ -155,12 +257,35 @@ status_icon_event_removed_cb (EmpathyEventManager *manager,
        status_icon_update_tooltip (icon);
        status_icon_update_icon (icon);
 
+       /* update notification anyway, as it's safe and we might have been
+        * changed presence in the meanwhile
+        */     
+       status_icon_update_notification (icon);
+
        if (!priv->event && priv->blink_timeout) {
                g_source_remove (priv->blink_timeout);
                priv->blink_timeout = 0;
        }
 }
 
+static void
+status_icon_event_updated_cb (EmpathyEventManager *manager,
+                             EmpathyEvent        *event,
+                             EmpathyStatusIcon   *icon)
+{
+       EmpathyStatusIconPriv *priv = GET_PRIV (icon);
+
+       if (event != priv->event) {
+               return;
+       }
+
+       if (empathy_notification_is_enabled ()) {
+               status_icon_update_notification (icon);
+       }
+
+       status_icon_update_tooltip (icon);
+}
+
 static void
 status_icon_set_visibility (EmpathyStatusIcon *icon,
                            gboolean           visible,
@@ -217,8 +342,20 @@ status_icon_toggle_visibility (EmpathyStatusIcon *icon)
 static void
 status_icon_idle_notify_cb (EmpathyStatusIcon *icon)
 {
+       EmpathyStatusIconPriv *priv = GET_PRIV (icon);
+
        status_icon_update_icon (icon);
        status_icon_update_tooltip (icon);
+
+       if (!empathy_notification_is_enabled ()) {
+               /* dismiss the outstanding notification if present */
+
+               if (priv->notification) {
+                       notify_notification_close (priv->notification, NULL);
+                       g_object_unref (priv->notification);
+                       priv->notification = NULL;
+               }
+       }
 }
 
 static gboolean
@@ -371,6 +508,12 @@ status_icon_finalize (GObject *object)
                                              status_icon_connection_changed_cb,
                                              object);
 
+       if (priv->notification) {
+               notify_notification_close (priv->notification, NULL);
+               g_object_unref (priv->notification);
+               priv->notification = NULL;
+       }
+
        g_object_unref (priv->icon);
        g_object_unref (priv->idle);
        g_object_unref (priv->account_manager);
@@ -396,8 +539,8 @@ empathy_status_icon_init (EmpathyStatusIcon *icon)
        icon->priv = priv;
        priv->icon = gtk_status_icon_new ();
        priv->account_manager = empathy_account_manager_dup_singleton ();
-       priv->idle = empathy_idle_new ();
-       priv->event_manager = empathy_event_manager_new ();
+       priv->idle = empathy_idle_dup_singleton ();
+       priv->event_manager = empathy_event_manager_dup_singleton ();
 
        g_signal_connect (priv->account_manager,
                          "account-connection-changed",
@@ -421,6 +564,9 @@ empathy_status_icon_init (EmpathyStatusIcon *icon)
        g_signal_connect (priv->event_manager, "event-removed",
                          G_CALLBACK (status_icon_event_removed_cb),
                          icon);
+       g_signal_connect (priv->event_manager, "event-updated",
+                         G_CALLBACK (status_icon_event_updated_cb),
+                         icon);
        g_signal_connect (priv->icon, "activate",
                          G_CALLBACK (status_icon_activate_cb),
                          icon);
@@ -443,7 +589,7 @@ empathy_status_icon_new (GtkWindow *window, gboolean hide_contact_list)
 
        priv->window = g_object_ref (window);
 
-       g_signal_connect (priv->window, "key-press-event",
+       g_signal_connect_after (priv->window, "key-press-event",
                          G_CALLBACK (status_icon_key_press_event_cb),
                          icon);