Added SILC Thread Queue API
[crypto.git] / apps / irssi / src / fe-common / core / window-activity.c
1 /*
2  window-activity.c : irssi
3
4     Copyright (C) 1999-2000 Timo Sirainen
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "module.h"
22 #include "signals.h"
23 #include "levels.h"
24 #include "servers.h"
25 #include "channels.h"
26 #include "misc.h"
27 #include "settings.h"
28
29 #include "fe-windows.h"
30 #include "window-items.h"
31 #include "nicklist.h"
32 #include "hilight-text.h"
33 #include "formats.h"
34
35 static char **hide_targets;
36 static int hide_level, msg_level, hilight_level;
37
38 void window_activity(WINDOW_REC *window, int data_level,
39                      const char *hilight_color)
40 {
41         int old_data_level;
42
43         old_data_level = window->data_level;
44         if (data_level == 0 || window->data_level < data_level) {
45                 window->data_level = data_level;
46                 g_free_not_null(window->hilight_color);
47                 window->hilight_color = g_strdup(hilight_color);
48                 signal_emit("window hilight", 1, window);
49         }
50
51         signal_emit("window activity", 2, window,
52                     GINT_TO_POINTER(old_data_level));
53 }
54
55 void window_item_activity(WI_ITEM_REC *item, int data_level,
56                           const char *hilight_color)
57 {
58         int old_data_level;
59
60         old_data_level = item->data_level;
61         if (data_level == 0 || item->data_level < data_level) {
62                 item->data_level = data_level;
63                 g_free_not_null(item->hilight_color);
64                 item->hilight_color = g_strdup(hilight_color);
65                 signal_emit("window item hilight", 1, item);
66         }
67
68         signal_emit("window item activity", 2, item,
69                     GINT_TO_POINTER(old_data_level));
70 }
71
72 #define hide_target_activity(data_level, target) \
73         ((target) != NULL && hide_targets != NULL && \
74         strarray_find(hide_targets, target) != -1)
75
76 static void sig_hilight_text(TEXT_DEST_REC *dest, const char *msg)
77 {
78         WI_ITEM_REC *item;
79         char *tagtarget;
80         int data_level;
81
82         if (dest->window == active_win || (dest->level & hide_level))
83                 return;
84
85         if (dest->level & hilight_level) {
86                 data_level = DATA_LEVEL_HILIGHT+dest->hilight_priority;
87         } else {
88                 data_level = (dest->level & msg_level) ?
89                         DATA_LEVEL_MSG : DATA_LEVEL_TEXT;
90         }
91
92         if ((dest->level & MSGLEVEL_HILIGHT) == 0 && dest->target != NULL) {
93                 /* check for both target and tag/target */
94                 if (hide_target_activity(data_level, dest->target))
95                         return;
96
97                 tagtarget = dest->server_tag == NULL ? NULL :
98                         g_strdup_printf("%s/%s", dest->server_tag,
99                                         dest->target);
100                 if (hide_target_activity(data_level, tagtarget)) {
101                         g_free(tagtarget);
102                         return;
103                 }
104                 g_free(tagtarget);
105         }
106
107         if (dest->target != NULL) {
108                 item = window_item_find(dest->server, dest->target);
109                 if (item != NULL) {
110                         window_item_activity(item, data_level,
111                                              dest->hilight_color);
112                 }
113         }
114         window_activity(dest->window, data_level, dest->hilight_color);
115 }
116
117 static void sig_dehilight_window(WINDOW_REC *window)
118 {
119         GSList *tmp;
120
121         g_return_if_fail(window != NULL);
122
123         if (window->data_level != 0) {
124                 window_activity(window, 0, NULL);
125                 for (tmp = window->items; tmp != NULL; tmp = tmp->next)
126                         window_item_activity(tmp->data, 0, NULL);
127         }
128 }
129
130 static void read_settings(void)
131 {
132         const char *targets;
133
134         if (hide_targets != NULL)
135                 g_strfreev(hide_targets);
136
137         targets = settings_get_str("activity_hide_targets");
138         hide_targets = *targets == '\0' ? NULL :
139                 g_strsplit(targets, " ", -1);
140
141         hide_level = MSGLEVEL_NEVER | MSGLEVEL_NO_ACT |
142                 settings_get_level("activity_hide_level");
143         msg_level = settings_get_level("activity_msg_level");
144         hilight_level = MSGLEVEL_HILIGHT |
145                 settings_get_level("activity_hilight_level");
146 }
147
148 void window_activity_init(void)
149 {
150         settings_add_str("lookandfeel", "activity_hide_targets", "");
151         settings_add_level("lookandfeel", "activity_hide_level", "");
152         settings_add_level("lookandfeel", "activity_msg_level", "PUBLIC");
153         settings_add_level("lookandfeel", "activity_hilight_level", "MSGS DCCMSGS");
154
155         read_settings();
156         signal_add("print text", (SIGNAL_FUNC) sig_hilight_text);
157         signal_add("window changed", (SIGNAL_FUNC) sig_dehilight_window);
158         signal_add("window dehilight", (SIGNAL_FUNC) sig_dehilight_window);
159         signal_add("setup changed", (SIGNAL_FUNC) read_settings);
160 }
161
162 void window_activity_deinit(void)
163 {
164         if (hide_targets != NULL)
165                 g_strfreev(hide_targets);
166
167         signal_remove("print text", (SIGNAL_FUNC) sig_hilight_text);
168         signal_remove("window changed", (SIGNAL_FUNC) sig_dehilight_window);
169         signal_remove("window dehilight", (SIGNAL_FUNC) sig_dehilight_window);
170         signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
171 }