Add module xttitle
authorMikael Berthe <mikael@lilotux.net>
Mon, 22 Mar 2010 22:24:23 +0100
changeset 14 e28cfd1d3084
parent 13 efe544108ca1
child 15 d7cced6660a7
Add module xttitle
Makefile.am
configure.ac
xttitle/Makefile.am
xttitle/xttitle.c
--- a/Makefile.am	Mon Mar 22 22:20:39 2010 +0100
+++ b/Makefile.am	Mon Mar 22 22:24:23 2010 +0100
@@ -1,1 +1,1 @@
-SUBDIRS = clock comment extsay lastmsg
+SUBDIRS = clock comment extsay lastmsg xttitle
--- a/configure.ac	Mon Mar 22 22:20:39 2010 +0100
+++ b/configure.ac	Mon Mar 22 22:24:23 2010 +0100
@@ -109,6 +109,9 @@
 AC_ARG_ENABLE(module-lastmsg,
               AC_HELP_STRING([--enable-module-lastmsg], [enable module lastmsg]),
               enable_module_lastmsg=$enableval)
+AC_ARG_ENABLE(module-xttitle,
+              AC_HELP_STRING([--enable-module-xttitle], [enable module xttitle]),
+              enable_module_xttitle=$enableval)
 
 AM_CONDITIONAL([INSTALL_MODULE_CLOCK],
                [test x"${enable_all_modules}" = x"yes" -o \
@@ -126,9 +129,14 @@
                [test x"${enable_all_modules}" = x"yes" -o \
                      x"${enable_module_lastmsg}" = x"yes"])
 
+AM_CONDITIONAL([INSTALL_MODULE_XTTITLE],
+               [test x"${enable_all_modules}" = x"yes" -o \
+                     x"${enable_module_xttitle}" = x"yes"])
+
 AC_CONFIG_FILES([clock/Makefile
                  comment/Makefile
                  extsay/Makefile
                  lastmsg/Makefile
+                 xttitle/Makefile
                  Makefile])
 AC_OUTPUT
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/xttitle/Makefile.am	Mon Mar 22 22:24:23 2010 +0100
@@ -0,0 +1,11 @@
+
+if INSTALL_MODULE_XTTITLE
+
+pkglib_LTLIBRARIES = libxttitle.la
+libxttitle_la_SOURCES = xttitle.c
+libxttitle_la_LDFLAGS = -module -avoid-version -shared
+
+LDADD = $(GLIB_LIBS) $(MCABBER_LIBS)
+AM_CPPFLAGS = -I$(top_srcdir) $(GLIB_CFLAGS) $(MCABBER_CFLAGS)
+
+endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/xttitle/xttitle.c	Mon Mar 22 22:24:23 2010 +0100
@@ -0,0 +1,88 @@
+/*
+   Copyright (C) 2010 Mikael Berthe <mikael@lilotux.net>
+
+  Module "xttitle"    -- Update xterm tittle
+
+
+This module is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <stdlib.h>
+#include <mcabber/modules.h>
+#include <mcabber/settings.h>
+#include <mcabber/hooks.h>
+#include <mcabber/logprint.h>
+
+static void xttitle_init(void);
+static void xttitle_uninit(void);
+
+/* Module description */
+module_info_t info_xttitle = {
+        .branch         = MCABBER_BRANCH,
+        .api            = MCABBER_API_VERSION,
+        .version        = "0.01",
+        .description    = "Show unread message count in X terminal title",
+        .requires       = NULL,
+        .init           = xttitle_init,
+        .uninit         = xttitle_uninit,
+        .next           = NULL,
+};
+
+static guint unread_list_hid;
+
+static guint unread_list_hh(const gchar *hookname, hk_arg_t *args,
+                            gpointer userdata)
+{
+  guint all_unread = 0;
+  guint muc_unread = 0;
+  guint muc_attention = 0;
+  guint unread;
+
+  for ( ; args->name; args++) {
+    if (!g_strcmp0(args->name, "unread")) {
+      all_unread = atoi(args->value);
+    } else if (!g_strcmp0(args->name, "muc_unread")) {
+      muc_unread = atoi(args->value);
+    } else if (!g_strcmp0(args->name, "muc_attention")) {
+      muc_attention = atoi(args->value);
+    }
+  }
+
+  unread = all_unread - (muc_unread - muc_attention);
+
+  if (muc_unread)
+    printf("\033]0;MCabber  %d message%c (total:%d / MUC:%d)\007",
+           unread, (unread > 1 ? 's' : ' '), all_unread, muc_unread);
+  else
+    printf("\033]0;MCabber  %d message%c\007", unread,
+           (unread > 1 ? 's' : ' '));
+  return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
+}
+
+/* Initialization */
+static void xttitle_init(void)
+{
+  /* Add hook handler for unread message data */
+  unread_list_hid = hk_add_handler(unread_list_hh, HOOK_UNREAD_LIST_CHANGE,
+                                   G_PRIORITY_DEFAULT_IDLE, NULL);
+}
+
+/* Uninitialization */
+static void xttitle_uninit(void)
+{
+  /* Unregister handler */
+  hk_del_handler(HOOK_UNREAD_LIST_CHANGE, unread_list_hid);
+}
+
+/* vim: set expandtab cindent cinoptions=>2\:2(0:  For Vim users... */