#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>
#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>
guint blink_timeout;
EmpathyEventManager *event_manager;
EmpathyEvent *event;
+ NotifyNotification *notification;
GtkWindow *window;
GtkWidget *popup_menu;
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
return TRUE;
}
-
static void
status_icon_event_added_cb (EmpathyEventManager *manager,
EmpathyEvent *event,
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,
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,
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
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);
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",
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);
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);