Merge Irssi 0.8.16-rc1
[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 along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 #include "fe-common-core.h"
35
36 static char **hide_targets;
37 static int hide_level, msg_level, hilight_level;
38
39 void window_activity(WINDOW_REC *window, int data_level,
40                      const char *hilight_color)
41 {
42         int old_data_level;
43
44         old_data_level = window->data_level;
45         if (data_level == 0 || window->data_level < data_level) {
46                 window->data_level = data_level;
47                 g_free_not_null(window->hilight_color);
48                 window->hilight_color = g_strdup(hilight_color);
49                 signal_emit("window hilight", 1, window);
50         }
51
52         signal_emit("window activity", 2, window,
53                     GINT_TO_POINTER(old_data_level));
54 }
55
56 void window_item_activity(WI_ITEM_REC *item, int data_level,
57                           const char *hilight_color)
58 {
59         int old_data_level;
60
61         old_data_level = item->data_level;
62         if (data_level == 0 || item->data_level < data_level) {
63                 item->data_level = data_level;
64                 g_free_not_null(item->hilight_color);
65                 item->hilight_color = g_strdup(hilight_color);
66                 signal_emit("window item hilight", 1, item);
67         }
68
69         signal_emit("window item activity", 2, item,
70                     GINT_TO_POINTER(old_data_level));
71 }
72
73 static void sig_hilight_text(TEXT_DEST_REC *dest, const char *msg)
74 {
75         WI_ITEM_REC *item;
76         int data_level;
77
78         if (dest->window == active_win || (dest->level & hide_level))
79                 return;
80
81         if (dest->level & hilight_level) {
82                 data_level = DATA_LEVEL_HILIGHT+dest->hilight_priority;
83         } else {
84                 data_level = (dest->level & msg_level) ?
85                         DATA_LEVEL_MSG : DATA_LEVEL_TEXT;
86         }
87
88         if (hide_targets != NULL && (dest->level & MSGLEVEL_HILIGHT) == 0 && dest->target != NULL) {
89                 /* check for both target and tag/target */
90                 if (strarray_find_dest(hide_targets, dest))
91                         return;
92         }
93
94         if (dest->target != NULL) {
95                 item = window_item_find(dest->server, dest->target);
96                 if (item != NULL) {
97                         window_item_activity(item, data_level,
98                                              dest->hilight_color);
99                 }
100         }
101         window_activity(dest->window, data_level, dest->hilight_color);
102 }
103
104 static void sig_dehilight_window(WINDOW_REC *window)
105 {
106         GSList *tmp;
107
108         g_return_if_fail(window != NULL);
109
110         if (window->data_level != 0) {
111                 window_activity(window, 0, NULL);
112                 for (tmp = window->items; tmp != NULL; tmp = tmp->next)
113                         window_item_activity(tmp->data, 0, NULL);
114         }
115 }
116
117 static void read_settings(void)
118 {
119         const char *targets;
120
121         if (hide_targets != NULL)
122                 g_strfreev(hide_targets);
123
124         targets = settings_get_str("activity_hide_targets");
125         hide_targets = *targets == '\0' ? NULL :
126                 g_strsplit(targets, " ", -1);
127
128         hide_level = MSGLEVEL_NEVER | MSGLEVEL_NO_ACT |
129                 settings_get_level("activity_hide_level");
130         msg_level = settings_get_level("activity_msg_level");
131         hilight_level = MSGLEVEL_HILIGHT |
132                 settings_get_level("activity_hilight_level");
133 }
134
135 void window_activity_init(void)
136 {
137         settings_add_str("lookandfeel", "activity_hide_targets", "");
138         settings_add_level("lookandfeel", "activity_hide_level", "");
139         settings_add_level("lookandfeel", "activity_msg_level", "PUBLIC");
140         settings_add_level("lookandfeel", "activity_hilight_level", "MSGS DCCMSGS");
141
142         read_settings();
143         signal_add("print text", (SIGNAL_FUNC) sig_hilight_text);
144         signal_add("window changed", (SIGNAL_FUNC) sig_dehilight_window);
145         signal_add("window dehilight", (SIGNAL_FUNC) sig_dehilight_window);
146         signal_add("setup changed", (SIGNAL_FUNC) read_settings);
147 }
148
149 void window_activity_deinit(void)
150 {
151         if (hide_targets != NULL)
152                 g_strfreev(hide_targets);
153
154         signal_remove("print text", (SIGNAL_FUNC) sig_hilight_text);
155         signal_remove("window changed", (SIGNAL_FUNC) sig_dehilight_window);
156         signal_remove("window dehilight", (SIGNAL_FUNC) sig_dehilight_window);
157         signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
158 }