7238e50b263225863ec7c77917337b3b0dc41739
[silc.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         int data_level;
80
81         if (dest->window == active_win || (dest->level & hide_level))
82                 return;
83
84         if (dest->level & hilight_level) {
85                 data_level = DATA_LEVEL_HILIGHT+dest->hilight_priority;
86         } else {
87                 data_level = (dest->level & msg_level) ?
88                         DATA_LEVEL_MSG : DATA_LEVEL_TEXT;
89         }
90
91         if ((dest->level & MSGLEVEL_HILIGHT) == 0 &&
92             hide_target_activity(data_level, dest->target))
93                 return;
94
95         if (dest->target != NULL) {
96                 item = window_item_find(dest->server, dest->target);
97                 if (item != NULL) {
98                         window_item_activity(item, data_level,
99                                              dest->hilight_color);
100                 }
101         }
102         window_activity(dest->window, data_level, dest->hilight_color);
103 }
104
105 static void sig_dehilight_window(WINDOW_REC *window)
106 {
107         GSList *tmp;
108
109         g_return_if_fail(window != NULL);
110
111         if (window->data_level != 0) {
112                 window_activity(window, 0, NULL);
113                 for (tmp = window->items; tmp != NULL; tmp = tmp->next)
114                         window_item_activity(tmp->data, 0, NULL);
115         }
116 }
117
118 static void read_settings(void)
119 {
120         const char *targets;
121
122         if (hide_targets != NULL)
123                 g_strfreev(hide_targets);
124
125         targets = settings_get_str("activity_hide_targets");
126         hide_targets = *targets == '\0' ? NULL :
127                 g_strsplit(targets, " ", -1);
128
129         hide_level = MSGLEVEL_NEVER | MSGLEVEL_NO_ACT |
130                 level2bits(settings_get_str("activity_hide_level"));
131         msg_level = level2bits(settings_get_str("activity_msg_level"));
132         hilight_level = MSGLEVEL_HILIGHT |
133                 level2bits(settings_get_str("activity_hilight_level"));
134 }
135
136 void window_activity_init(void)
137 {
138         settings_add_str("lookandfeel", "activity_hide_targets", "");
139         settings_add_str("lookandfeel", "activity_hide_level", "");
140         settings_add_str("lookandfeel", "activity_msg_level", "PUBLIC");
141         settings_add_str("lookandfeel", "activity_hilight_level", "MSGS DCCMSGS");
142
143         read_settings();
144         signal_add("print text", (SIGNAL_FUNC) sig_hilight_text);
145         signal_add("window changed", (SIGNAL_FUNC) sig_dehilight_window);
146         signal_add("window dehilight", (SIGNAL_FUNC) sig_dehilight_window);
147         signal_add("setup changed", (SIGNAL_FUNC) read_settings);
148 }
149
150 void window_activity_deinit(void)
151 {
152         if (hide_targets != NULL)
153                 g_strfreev(hide_targets);
154
155         signal_remove("print text", (SIGNAL_FUNC) sig_hilight_text);
156         signal_remove("window changed", (SIGNAL_FUNC) sig_dehilight_window);
157         signal_remove("window dehilight", (SIGNAL_FUNC) sig_dehilight_window);
158         signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
159 }