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