author | Mikael Berthe <mikael@lilotux.net> |
Sun, 28 Mar 2010 10:56:42 +0200 | |
changeset 1841 | 8ad982f83fd6 |
parent 1832 | 48b265f8c5cb |
child 2038 | 5d5af91a0a69 |
permissions | -rw-r--r-- |
1827 | 1 |
/* |
1832 | 2 |
* Module "xttitle" -- Update X terminal title |
1827 | 3 |
* |
4 |
* Copyright (C) 2010 Mikael Berthe <mikael@lilotux.net> |
|
5 |
* |
|
1841
8ad982f83fd6
[xttitle] Add option xttitle_short_format
Mikael Berthe <mikael@lilotux.net>
parents:
1832
diff
changeset
|
6 |
* The option 'xttitle_short_format' can be set to 1 to use a very |
8ad982f83fd6
[xttitle] Add option xttitle_short_format
Mikael Berthe <mikael@lilotux.net>
parents:
1832
diff
changeset
|
7 |
* short terminal title. |
8ad982f83fd6
[xttitle] Add option xttitle_short_format
Mikael Berthe <mikael@lilotux.net>
parents:
1832
diff
changeset
|
8 |
* |
1827 | 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 <stdlib.h> |
|
24 |
#include <mcabber/modules.h> |
|
25 |
#include <mcabber/settings.h> |
|
26 |
#include <mcabber/hooks.h> |
|
27 |
#include <mcabber/logprint.h> |
|
28 |
||
29 |
static void xttitle_init(void); |
|
30 |
static void xttitle_uninit(void); |
|
31 |
||
32 |
/* Module description */ |
|
33 |
module_info_t info_xttitle = { |
|
34 |
.branch = MCABBER_BRANCH, |
|
35 |
.api = MCABBER_API_VERSION, |
|
36 |
.version = MCABBER_VERSION, |
|
37 |
.description = "Show unread message count in X terminal title", |
|
38 |
.requires = NULL, |
|
39 |
.init = xttitle_init, |
|
40 |
.uninit = xttitle_uninit, |
|
41 |
.next = NULL, |
|
42 |
}; |
|
43 |
||
44 |
// Hook handler id |
|
45 |
static guint unread_list_hid; |
|
46 |
||
47 |
// Event handler for HOOK_UNREAD_LIST_CHANGE events |
|
48 |
static guint unread_list_hh(const gchar *hookname, hk_arg_t *args, |
|
49 |
gpointer userdata) |
|
50 |
{ |
|
51 |
guint all_unread = 0; |
|
52 |
guint muc_unread = 0; |
|
53 |
guint muc_attention = 0; |
|
54 |
guint unread; // private message count |
|
1841
8ad982f83fd6
[xttitle] Add option xttitle_short_format
Mikael Berthe <mikael@lilotux.net>
parents:
1832
diff
changeset
|
55 |
static gchar buf[128]; |
1827 | 56 |
|
57 |
// Note: We can add "attention" string later, but it isn't used |
|
58 |
// yet in mcabber... |
|
59 |
for ( ; args->name; args++) { |
|
60 |
if (!g_strcmp0(args->name, "unread")) { |
|
61 |
all_unread = atoi(args->value); |
|
62 |
} else if (!g_strcmp0(args->name, "muc_unread")) { |
|
63 |
muc_unread = atoi(args->value); |
|
64 |
} else if (!g_strcmp0(args->name, "muc_attention")) { |
|
65 |
muc_attention = atoi(args->value); |
|
66 |
} |
|
67 |
} |
|
68 |
||
69 |
// Let's not count the MUC unread buffers that don't have the attention |
|
70 |
// flag (that is, MUC buffer that have no highlighted messages). |
|
71 |
unread = all_unread - (muc_unread - muc_attention); |
|
72 |
||
1841
8ad982f83fd6
[xttitle] Add option xttitle_short_format
Mikael Berthe <mikael@lilotux.net>
parents:
1832
diff
changeset
|
73 |
// TODO: let the user use a format string, instead of hard-coded defaults... |
8ad982f83fd6
[xttitle] Add option xttitle_short_format
Mikael Berthe <mikael@lilotux.net>
parents:
1832
diff
changeset
|
74 |
if (settings_opt_get_int("xttitle_short_format") == 1) { |
8ad982f83fd6
[xttitle] Add option xttitle_short_format
Mikael Berthe <mikael@lilotux.net>
parents:
1832
diff
changeset
|
75 |
// Short title message |
8ad982f83fd6
[xttitle] Add option xttitle_short_format
Mikael Berthe <mikael@lilotux.net>
parents:
1832
diff
changeset
|
76 |
if (!all_unread) |
8ad982f83fd6
[xttitle] Add option xttitle_short_format
Mikael Berthe <mikael@lilotux.net>
parents:
1832
diff
changeset
|
77 |
snprintf(buf, sizeof(buf), "MCabber"); |
8ad982f83fd6
[xttitle] Add option xttitle_short_format
Mikael Berthe <mikael@lilotux.net>
parents:
1832
diff
changeset
|
78 |
else if (unread == all_unread) |
8ad982f83fd6
[xttitle] Add option xttitle_short_format
Mikael Berthe <mikael@lilotux.net>
parents:
1832
diff
changeset
|
79 |
snprintf(buf, sizeof(buf), "MCabber (%u)", unread); |
8ad982f83fd6
[xttitle] Add option xttitle_short_format
Mikael Berthe <mikael@lilotux.net>
parents:
1832
diff
changeset
|
80 |
else |
8ad982f83fd6
[xttitle] Add option xttitle_short_format
Mikael Berthe <mikael@lilotux.net>
parents:
1832
diff
changeset
|
81 |
snprintf(buf, sizeof(buf), "MCabber (%u/%u)", unread, all_unread); |
1827 | 82 |
} else { |
1841
8ad982f83fd6
[xttitle] Add option xttitle_short_format
Mikael Berthe <mikael@lilotux.net>
parents:
1832
diff
changeset
|
83 |
// Long title message |
8ad982f83fd6
[xttitle] Add option xttitle_short_format
Mikael Berthe <mikael@lilotux.net>
parents:
1832
diff
changeset
|
84 |
if (muc_unread) { |
8ad982f83fd6
[xttitle] Add option xttitle_short_format
Mikael Berthe <mikael@lilotux.net>
parents:
1832
diff
changeset
|
85 |
snprintf(buf, sizeof(buf), "MCabber -- %u message%c (total:%u / MUC:%u)", |
8ad982f83fd6
[xttitle] Add option xttitle_short_format
Mikael Berthe <mikael@lilotux.net>
parents:
1832
diff
changeset
|
86 |
unread, (unread > 1 ? 's' : ' '), all_unread, muc_unread); |
8ad982f83fd6
[xttitle] Add option xttitle_short_format
Mikael Berthe <mikael@lilotux.net>
parents:
1832
diff
changeset
|
87 |
} else { |
8ad982f83fd6
[xttitle] Add option xttitle_short_format
Mikael Berthe <mikael@lilotux.net>
parents:
1832
diff
changeset
|
88 |
if (unread) |
8ad982f83fd6
[xttitle] Add option xttitle_short_format
Mikael Berthe <mikael@lilotux.net>
parents:
1832
diff
changeset
|
89 |
snprintf(buf, sizeof(buf), "MCabber -- %u message%c", unread, |
8ad982f83fd6
[xttitle] Add option xttitle_short_format
Mikael Berthe <mikael@lilotux.net>
parents:
1832
diff
changeset
|
90 |
(unread > 1 ? 's' : ' ')); |
8ad982f83fd6
[xttitle] Add option xttitle_short_format
Mikael Berthe <mikael@lilotux.net>
parents:
1832
diff
changeset
|
91 |
else |
8ad982f83fd6
[xttitle] Add option xttitle_short_format
Mikael Berthe <mikael@lilotux.net>
parents:
1832
diff
changeset
|
92 |
snprintf(buf, sizeof(buf), "MCabber -- No message"); |
8ad982f83fd6
[xttitle] Add option xttitle_short_format
Mikael Berthe <mikael@lilotux.net>
parents:
1832
diff
changeset
|
93 |
} |
1827 | 94 |
} |
95 |
||
1841
8ad982f83fd6
[xttitle] Add option xttitle_short_format
Mikael Berthe <mikael@lilotux.net>
parents:
1832
diff
changeset
|
96 |
// Update the terminal title |
8ad982f83fd6
[xttitle] Add option xttitle_short_format
Mikael Berthe <mikael@lilotux.net>
parents:
1832
diff
changeset
|
97 |
printf("\033]0;%s\007", buf); |
8ad982f83fd6
[xttitle] Add option xttitle_short_format
Mikael Berthe <mikael@lilotux.net>
parents:
1832
diff
changeset
|
98 |
|
1827 | 99 |
return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS; |
100 |
} |
|
101 |
||
102 |
// Initialization |
|
103 |
static void xttitle_init(void) |
|
104 |
{ |
|
105 |
// Add hook handler for unread message data |
|
106 |
unread_list_hid = hk_add_handler(unread_list_hh, HOOK_UNREAD_LIST_CHANGE, |
|
107 |
G_PRIORITY_DEFAULT_IDLE, NULL); |
|
1841
8ad982f83fd6
[xttitle] Add option xttitle_short_format
Mikael Berthe <mikael@lilotux.net>
parents:
1832
diff
changeset
|
108 |
// Default title |
8ad982f83fd6
[xttitle] Add option xttitle_short_format
Mikael Berthe <mikael@lilotux.net>
parents:
1832
diff
changeset
|
109 |
printf("\033]0;MCabber\007"); |
1827 | 110 |
} |
111 |
||
112 |
// Uninitialization |
|
113 |
static void xttitle_uninit(void) |
|
114 |
{ |
|
115 |
// Unregister handler |
|
116 |
hk_del_handler(HOOK_UNREAD_LIST_CHANGE, unread_list_hid); |
|
1841
8ad982f83fd6
[xttitle] Add option xttitle_short_format
Mikael Berthe <mikael@lilotux.net>
parents:
1832
diff
changeset
|
117 |
// Reset title |
8ad982f83fd6
[xttitle] Add option xttitle_short_format
Mikael Berthe <mikael@lilotux.net>
parents:
1832
diff
changeset
|
118 |
printf("\033]0;MCabber\007"); |
1827 | 119 |
} |
120 |
||
121 |
/* vim: set et cindent cinoptions=>2\:2(0 ts=2 sw=2: For Vim users... */ |