info_msgcount/info_msgcount.c
changeset 17 be7e71438f76
equal deleted inserted replaced
16:c447081cb5b7 17:be7e71438f76
       
     1 /*
       
     2  *  Module "info_msgcount"  -- Show number of unread buffers in status bar
       
     3  *
       
     4  *  This module relies on the "info" option to display the number of
       
     5  *  unread messages in the status bar...
       
     6  *
       
     7  * Copyright (C) 2010 Mikael Berthe <mikael@lilotux.net>
       
     8  *
       
     9  * This module is free software; you can redistribute it and/or modify
       
    10  * it under the terms of the GNU General Public License as published by
       
    11  * the Free Software Foundation; either version 2 of the License, or (at
       
    12  * your option) any later version.
       
    13  *
       
    14  * This program is distributed in the hope that it will be useful, but
       
    15  * WITHOUT ANY WARRANTY; without even the implied warranty of
       
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    17  * General Public License for more details.
       
    18  *
       
    19  * You should have received a copy of the GNU General Public License
       
    20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
       
    21  */
       
    22 
       
    23 #include <stdio.h>
       
    24 #include <stdlib.h>
       
    25 #include <mcabber/modules.h>
       
    26 #include <mcabber/settings.h>
       
    27 #include <mcabber/screen.h>
       
    28 #include <mcabber/hooks.h>
       
    29 
       
    30 static void info_msgcount_init(void);
       
    31 static void info_msgcount_uninit(void);
       
    32 
       
    33 /* Module description */
       
    34 module_info_t info_info_msgcount = {
       
    35         .branch         = MCABBER_BRANCH,
       
    36         .api            = MCABBER_API_VERSION,
       
    37         .version        = "0.01",
       
    38         .description    = "Show unread message count in the status bar",
       
    39         .requires       = NULL,
       
    40         .init           = info_msgcount_init,
       
    41         .uninit         = info_msgcount_uninit,
       
    42         .next           = NULL,
       
    43 };
       
    44 
       
    45 // Hook handler id
       
    46 static guint unread_list_hid;
       
    47 
       
    48 static gchar *backup_info;
       
    49 
       
    50 // Event handler for HOOK_UNREAD_LIST_CHANGE events
       
    51 static guint unread_list_hh(const gchar *hookname, hk_arg_t *args,
       
    52                             gpointer userdata)
       
    53 {
       
    54   static gchar buf[128];
       
    55   guint all_unread = 0;
       
    56   guint muc_unread = 0;
       
    57   guint muc_attention = 0;
       
    58   guint unread; // private message count
       
    59 
       
    60   // Note: We can add "attention" string later, but it isn't used
       
    61   // yet in mcabber...
       
    62   for ( ; args->name; args++) {
       
    63     if (!g_strcmp0(args->name, "unread")) {
       
    64       all_unread = atoi(args->value);
       
    65     } else if (!g_strcmp0(args->name, "muc_unread")) {
       
    66       muc_unread = atoi(args->value);
       
    67     } else if (!g_strcmp0(args->name, "muc_attention")) {
       
    68       muc_attention = atoi(args->value);
       
    69     }
       
    70   }
       
    71 
       
    72   // Let's not count the MUC unread buffers that don't have the attention
       
    73   // flag (that is, MUC buffer that have no highlighted messages).
       
    74   unread = all_unread - (muc_unread - muc_attention);
       
    75 
       
    76   // Update the status bar
       
    77   snprintf(buf, sizeof(buf), "(%d/%d) ", unread, all_unread);
       
    78   settings_set(SETTINGS_TYPE_OPTION, "info", buf);
       
    79   scr_update_chat_status(TRUE);
       
    80 
       
    81   return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
       
    82 }
       
    83 
       
    84 // Initialization
       
    85 static void info_msgcount_init(void)
       
    86 {
       
    87   // Backup info option, set default initial string
       
    88   backup_info = g_strdup(settings_opt_get("info"));
       
    89   settings_set(SETTINGS_TYPE_OPTION, "info", "(...)");
       
    90   scr_update_chat_status(TRUE);
       
    91 
       
    92   // Add hook handler for unread message data
       
    93   unread_list_hid = hk_add_handler(unread_list_hh, HOOK_UNREAD_LIST_CHANGE,
       
    94                                    G_PRIORITY_DEFAULT_IDLE, NULL);
       
    95 }
       
    96 
       
    97 // Uninitialization
       
    98 static void info_msgcount_uninit(void)
       
    99 {
       
   100   // Unregister handler
       
   101   hk_del_handler(HOOK_UNREAD_LIST_CHANGE, unread_list_hid);
       
   102 
       
   103   // Restore initial info option value
       
   104   settings_set(SETTINGS_TYPE_OPTION, "info", backup_info);
       
   105   g_free(backup_info);
       
   106   backup_info = NULL;
       
   107   scr_update_chat_status(TRUE);
       
   108 }
       
   109 
       
   110 /* vim: set et cindent cinoptions=>2\:2(0 ts=2 sw=2:  For Vim users... */