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