xttitle/xttitle.c
changeset 16 c447081cb5b7
parent 15 d7cced6660a7
child 17 be7e71438f76
equal deleted inserted replaced
15:d7cced6660a7 16:c447081cb5b7
     1 /*
       
     2    Copyright (C) 2010 Mikael Berthe <mikael@lilotux.net>
       
     3 
       
     4   Module "xttitle"    -- Update xterm tittle
       
     5 
       
     6 
       
     7 This module is free software: you can redistribute it and/or modify
       
     8 it under the terms of the GNU General Public License as published by
       
     9 the Free Software Foundation, either version 2 of the License, or
       
    10 (at your option) any later version.
       
    11 
       
    12 This program is distributed in the hope that it will be useful,
       
    13 but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    15 GNU General Public License for more details.
       
    16 
       
    17 You should have received a copy of the GNU General Public License
       
    18 along with this program.  If not, see <http://www.gnu.org/licenses/>.
       
    19 */
       
    20 
       
    21 #include <stdlib.h>
       
    22 #include <mcabber/modules.h>
       
    23 #include <mcabber/settings.h>
       
    24 #include <mcabber/hooks.h>
       
    25 #include <mcabber/logprint.h>
       
    26 
       
    27 static void xttitle_init(void);
       
    28 static void xttitle_uninit(void);
       
    29 
       
    30 /* Module description */
       
    31 module_info_t info_xttitle = {
       
    32         .branch         = MCABBER_BRANCH,
       
    33         .api            = MCABBER_API_VERSION,
       
    34         .version        = "0.01",
       
    35         .description    = "Show unread message count in X terminal title",
       
    36         .requires       = NULL,
       
    37         .init           = xttitle_init,
       
    38         .uninit         = xttitle_uninit,
       
    39         .next           = NULL,
       
    40 };
       
    41 
       
    42 static guint unread_list_hid;
       
    43 
       
    44 static guint unread_list_hh(const gchar *hookname, hk_arg_t *args,
       
    45                             gpointer userdata)
       
    46 {
       
    47   guint all_unread = 0;
       
    48   guint muc_unread = 0;
       
    49   guint muc_attention = 0;
       
    50   guint unread;
       
    51 
       
    52   for ( ; args->name; args++) {
       
    53     if (!g_strcmp0(args->name, "unread")) {
       
    54       all_unread = atoi(args->value);
       
    55     } else if (!g_strcmp0(args->name, "muc_unread")) {
       
    56       muc_unread = atoi(args->value);
       
    57     } else if (!g_strcmp0(args->name, "muc_attention")) {
       
    58       muc_attention = atoi(args->value);
       
    59     }
       
    60   }
       
    61 
       
    62   unread = all_unread - (muc_unread - muc_attention);
       
    63 
       
    64   if (muc_unread)
       
    65     printf("\033]0;MCabber  %d message%c (total:%d / MUC:%d)\007",
       
    66            unread, (unread > 1 ? 's' : ' '), all_unread, muc_unread);
       
    67   else
       
    68     printf("\033]0;MCabber  %d message%c\007", unread,
       
    69            (unread > 1 ? 's' : ' '));
       
    70   return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
       
    71 }
       
    72 
       
    73 /* Initialization */
       
    74 static void xttitle_init(void)
       
    75 {
       
    76   /* Add hook handler for unread message data */
       
    77   unread_list_hid = hk_add_handler(unread_list_hh, HOOK_UNREAD_LIST_CHANGE,
       
    78                                    G_PRIORITY_DEFAULT_IDLE, NULL);
       
    79 }
       
    80 
       
    81 /* Uninitialization */
       
    82 static void xttitle_uninit(void)
       
    83 {
       
    84   /* Unregister handler */
       
    85   hk_del_handler(HOOK_UNREAD_LIST_CHANGE, unread_list_hid);
       
    86 }
       
    87 
       
    88 /* vim: set expandtab cindent cinoptions=>2\:2(0:  For Vim users... */