[sylpheed:33635] [PATCH] Experimental support for the Notification protocol
Alexandre Erwin Ittner
listas at ittner.com.br
Wed Feb 10 03:20:00 JST 2010
Hi folks,
I just started exploring the Sylpheed source code and, as a learning
aid, I linked it to libnotify, so I can get incoming mail alerts
through the notification daemon. It's far from finished yet but,
once concluded, I plan to use it to replace the "notify-send" in
my "Execute this command when new messages arrive" setting.
Is this feature useful for anyone?
The following patch works against Subversion r2460. I am also tracking
revision in http://github.com/ittner/sylpheed-contrib , if someone else
gets interested.
diff --git a/configure.in b/configure.in
index 4c8c166..33918e8 100644
--- a/configure.in
+++ b/configure.in
@@ -328,6 +328,16 @@ else
AC_MSG_RESULT(no)
fi
+
+dnl Check for libnotify support
+AC_ARG_ENABLE(notify,
+ [ --disable-notify Do not use notifications (libnotify)],
+ [ac_cv_enable_notify=$enableval], [ac_cv_enable_notify=yes])
+if test "$ac_cv_enable_notify" = yes; then
+ AC_CHECK_LIB(notify, notify_init,,[ac_cv_enable_notify=no])
+fi
+
+
dnl check additional libraries
AC_CHECK_LIB(xpg4, setlocale)
AC_CHECK_LIB(resolv, res_init)
@@ -480,6 +490,7 @@ echo "IPv6 : $ac_cv_enable_ipv6"
echo "GtkSpell : $ac_cv_enable_gtkspell"
echo "Oniguruma : $ac_cv_enable_oniguruma"
echo "GThread : $use_threads"
+echo "Notifications : $ac_cv_enable_notify"
echo ""
echo "The binary will be installed in $prefix/bin"
echo ""
diff --git a/src/about.c b/src/about.c
index fc40081..19c3fa8 100644
--- a/src/about.c
+++ b/src/about.c
@@ -168,6 +168,9 @@ static void about_create(void)
#if USE_ONIGURUMA
" Oniguruma"
#endif
+#if HAVE_LIBNOTIFY
+ " notify"
+#endif
"");
label = gtk_label_new(buf);
diff --git a/src/inc.c b/src/inc.c
index 64e6f26..9ceb5ff 100644
--- a/src/inc.c
+++ b/src/inc.c
@@ -155,12 +155,7 @@ static void inc_finished(MainWindow *mainwin, gint new_messages)
new_messages += folderview_check_new(NULL);
if (new_messages > 0 && !block_notify) {
- gchar buf[1024];
-
- g_snprintf(buf, sizeof(buf), _("Sylpheed: %d new messages"),
- new_messages);
- trayicon_set_tooltip(buf);
- trayicon_set_notify(TRUE);
+ trayicon_set_new_messages(new_messages);
}
inc_block_notify(FALSE);
diff --git a/src/main.c b/src/main.c
index f2ed5ea..86e20ec 100644
--- a/src/main.c
+++ b/src/main.c
@@ -50,6 +50,11 @@
# include <gpgme.h>
#endif
+#if HAVE_LIBNOTIFY
+# include <libnotify/notify.h>
+#endif
+
+
#include "main.h"
#include "mainwindow.h"
#include "folderview.h"
@@ -225,7 +230,9 @@ int main(int argc, char *argv[])
lock_socket_remove();
return 0;
}
-
+#if HAVE_LIBNOTIFY
+ notify_init("sylpheed");
+#endif
#if USE_THREADS
gdk_threads_enter();
#endif
@@ -253,7 +260,6 @@ int main(int argc, char *argv[])
#if USE_SSL
ssl_set_verify_func(ssl_manager_verify_cert);
#endif
-
CHDIR_EXIT_IF_FAIL(get_home_dir(), 1);
prefs_common_read_config();
@@ -364,6 +370,9 @@ int main(int argc, char *argv[])
#if USE_THREADS
gdk_threads_leave();
#endif
+#if HAVE_LIBNOTIFY
+ notify_uninit();
+#endif
return 0;
}
diff --git a/src/trayicon.c b/src/trayicon.c
index 3d7bbb6..8c2ee15 100644
--- a/src/trayicon.c
+++ b/src/trayicon.c
@@ -43,6 +43,10 @@
#include "compose.h"
#include "prefs_common.h"
+#if HAVE_LIBNOTIFY
+# include <libnotify/notify.h>
+#endif
+
#if GTK_CHECK_VERSION(2, 10, 0) || defined(GDK_WINDOWING_X11)
#if GTK_CHECK_VERSION(2, 10, 0)
@@ -422,3 +426,70 @@ void trayicon_set_stock_icon(StockPixmap icon)
}
#endif /* GTK_CHECK_VERSION(2, 10, 0) || defined(GDK_WINDOWING_X11) */
+
+
+#if HAVE_LIBNOTIFY
+static NotifyNotification *new_mail_notification = NULL;
+
+static void new_mail_notification_cb(NotifyNotification *notification,
+ gchar *action, gpointer udata)
+{
+ main_window_popup(main_window_get());
+ notify_notification_close(notification, NULL);
+}
+
+#endif /* HAVE_LIBNOTIFY */
+
+void trayicon_set_new_messages(guint new_messages)
+{
+ if (new_messages == 0)
+ return;
+
+ gchar buf[1024];
+ g_snprintf(buf, sizeof(buf), _("Sylpheed: %d new messages"),
+ new_messages);
+ trayicon_set_tooltip(buf);
+ trayicon_set_notify(TRUE);
+
+#if HAVE_LIBNOTIFY
+ /* Suppress notifications if the main window have focus. */
+ MainWindow *mainw = main_window_get();
+ if (gtk_window_is_active(mainw->window))
+ return;
+
+ /* Send the 'New mail notification' to the user message bus */
+ gchar *ntitle;
+ gchar *nbody = NULL;
+
+ if (new_messages == 1) {
+ ntitle = g_strdup(_("You have a new message"));
+ /* TODO: Fetch new mail header (sender, subject, etc)
+ and show it to the user in the notification body. */
+ /* nbody = g_strdup(_("A new message was received")); */
+ } else
+ ntitle = g_strdup_printf(_("You have %d new messages"),
+ new_messages);
+
+ if (new_mail_notification == NULL) {
+ new_mail_notification = notify_notification_new(ntitle,
+ nbody, NULL, NULL);
+ notify_notification_set_category(new_mail_notification,
+ "email.arrived");
+ } else
+ notify_notification_update(new_mail_notification,
+ ntitle, nbody, NULL);
+
+#if GTK_CHECK_VERSION(2, 10, 0)
+ /* Try to put the notification near the tray icon. Its position may
+ * have changed after the last invocation, or the icon may have
+ * been enabled/disabled in the preferences. */
+ if (gtk_status_icon_get_visible(trayicon.status_icon))
+ notify_notification_attach_to_status_icon(
+ new_mail_notification, trayicon.status_icon);
+#endif
+ notify_notification_show(new_mail_notification, NULL);
+ g_free(ntitle);
+ if (nbody != NULL)
+ g_free(nbody);
+#endif /* HAVE_LIBNOTIFY */
+}
diff --git a/src/trayicon.h b/src/trayicon.h
index eea46af..02d0f70 100644
--- a/src/trayicon.h
+++ b/src/trayicon.h
@@ -48,6 +48,7 @@ void trayicon_hide (TrayIcon *tray_icon);
void trayicon_destroy (TrayIcon *tray_icon);
void trayicon_set_tooltip (const gchar *text);
void trayicon_set_notify (gboolean enabled);
+void trayicon_set_new_messages (guint new_messages);
void trayicon_set_stock_icon (StockPixmap icon);
#endif /* __TRAYICON_H__ */
--
Alexandre Erwin Ittner - alexandre at ittner.com.br
OpenPGP pubkey 0x0041A1FB @ http://pgp.mit.edu
More information about the Sylpheed
mailing list