2007-06-04 [wwp] 2.9.2cvs36
[claws.git] / src / prefs_logging.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 Tristan Chabredier <wwp@claws-mail.org> and 
4  * the Claws Mail team
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #endif
24
25 #include "defs.h"
26
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #include <glib.h>
31 #include <glib/gi18n.h>
32 #include <gtk/gtk.h>
33
34 #include "prefs_common.h"
35 #include "prefs_gtk.h"
36
37 #include "gtk/gtkutils.h"
38 #include "gtk/prefswindow.h"
39 #include "gtk/menu.h"
40
41 #include "manage_window.h"
42
43 #include "log.h"
44
45 typedef struct _LoggingPage
46 {
47         PrefsPage page;
48
49         GtkWidget *window;
50
51         GtkWidget *checkbtn_cliplog;
52         GtkWidget *spinbtn_loglength;
53         GtkWidget *checkbtn_debug_cliplog;
54         GtkWidget *spinbtn_debug_loglength;
55         GtkWidget *checkbtn_filteringdebug;
56         GtkWidget *checkbtn_filteringdebug_inc;
57         GtkWidget *checkbtn_filteringdebug_manual;
58         GtkWidget *checkbtn_filteringdebug_folder_proc;
59         GtkWidget *checkbtn_filteringdebug_pre_proc;
60         GtkWidget *checkbtn_filteringdebug_post_proc;
61         GtkWidget *optmenu_filteringdebug_level;
62 } LoggingPage;
63
64 static void prefs_logging_create_widget(PrefsPage *_page, GtkWindow *window, 
65                                   gpointer data)
66 {
67         LoggingPage *prefs_logging = (LoggingPage *) _page;
68         
69         GtkWidget *vbox1;
70
71         GtkWidget *frame_logging;
72         GtkWidget *vbox_logging;
73         GtkWidget *hbox_cliplog;
74         GtkWidget *checkbtn_cliplog;
75         GtkWidget *loglength_label;
76         GtkWidget *spinbtn_loglength;
77         GtkObject *spinbtn_loglength_adj;
78         GtkTooltips *loglength_tooltip;
79         GtkWidget *label;
80         GtkWidget *vbox_filteringdebug_log;
81         GtkWidget *hbox_debug_cliplog;
82         GtkWidget *checkbtn_debug_cliplog;
83         GtkWidget *debug_loglength_label;
84         GtkWidget *spinbtn_debug_loglength;
85         GtkObject *spinbtn_debug_loglength_adj;
86         GtkTooltips *debug_loglength_tooltip;
87         GtkWidget *hbox_filteringdebug;
88         GtkWidget *checkbtn_filteringdebug;
89         GtkWidget *frame_filteringdebug;
90         GtkWidget *vbox_filteringdebug;
91         GtkWidget *hbox_filteringdebug_inc;
92         GtkWidget *checkbtn_filteringdebug_inc;
93         GtkWidget *hbox_filteringdebug_manual;
94         GtkWidget *checkbtn_filteringdebug_manual;
95         GtkWidget *hbox_filteringdebug_folder_proc;
96         GtkWidget *checkbtn_filteringdebug_folder_proc;
97         GtkWidget *hbox_filteringdebug_pre_proc;
98         GtkWidget *checkbtn_filteringdebug_pre_proc;
99         GtkWidget *hbox_filteringdebug_post_proc;
100         GtkWidget *checkbtn_filteringdebug_post_proc;
101         GtkTooltips *filteringdebug_tooltip;
102         GtkWidget *hbox_filteringdebug_level;
103         GtkWidget *label_debug_level;
104         GtkWidget *optmenu_filteringdebug_level;
105         GtkWidget *menu;
106         GtkWidget *menuitem;
107         GtkTooltips *filteringdebug_level_tooltip;
108
109         vbox1 = gtk_vbox_new (FALSE, VSPACING);
110         gtk_widget_show (vbox1);
111         gtk_container_set_border_width (GTK_CONTAINER (vbox1), VBOX_BORDER);
112
113         /* Protocol log */
114         vbox_logging = gtkut_get_options_frame(vbox1, &frame_logging, _("Protocol log"));
115
116         PACK_CHECK_BUTTON (vbox_logging, checkbtn_cliplog,
117                            _("Restrict the log size"));
118         hbox_cliplog = gtk_hbox_new (FALSE, 8);
119         gtk_container_add (GTK_CONTAINER (vbox_logging), hbox_cliplog);
120         gtk_widget_show (hbox_cliplog);
121         
122         loglength_label = gtk_label_new (_("Log window length"));
123         gtk_box_pack_start (GTK_BOX (hbox_cliplog), loglength_label,
124                             FALSE, TRUE, 0);
125         gtk_widget_show (GTK_WIDGET (loglength_label));
126         
127         loglength_tooltip = gtk_tooltips_new();
128
129         spinbtn_loglength_adj = gtk_adjustment_new (500, 0, G_MAXINT, 1, 10, 10);
130         spinbtn_loglength = gtk_spin_button_new
131                 (GTK_ADJUSTMENT (spinbtn_loglength_adj), 1, 0);
132         gtk_widget_show (spinbtn_loglength);
133         gtk_box_pack_start (GTK_BOX (hbox_cliplog), spinbtn_loglength,
134                             FALSE, FALSE, 0);
135         gtk_widget_set_size_request (GTK_WIDGET (spinbtn_loglength), 64, -1);
136         gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spinbtn_loglength), TRUE);
137
138         gtk_tooltips_set_tip(GTK_TOOLTIPS(loglength_tooltip), spinbtn_loglength,
139                              _("0 to stop logging in the log window"),
140                              NULL);
141
142         label = gtk_label_new(_("lines"));
143         gtk_widget_show (label);
144         gtk_box_pack_start(GTK_BOX(hbox_cliplog), label, FALSE, FALSE, 0);
145
146         SET_TOGGLE_SENSITIVITY(checkbtn_cliplog, loglength_label);
147         SET_TOGGLE_SENSITIVITY(checkbtn_cliplog, spinbtn_loglength);
148         SET_TOGGLE_SENSITIVITY(checkbtn_cliplog, label);
149
150         /* Filtering/processing debug log */
151         vbox_filteringdebug_log = gtkut_get_options_frame(vbox1,
152                                 &frame_logging, _("Filtering/processing log"));
153
154         PACK_CHECK_BUTTON (vbox_filteringdebug_log, checkbtn_filteringdebug,
155                            _("Enable logging of filtering/processing rules"));
156         hbox_filteringdebug = gtk_hbox_new (FALSE, 8);
157         gtk_container_add (GTK_CONTAINER (vbox_filteringdebug_log), hbox_filteringdebug);
158         gtk_widget_show (hbox_filteringdebug);
159
160         filteringdebug_tooltip = gtk_tooltips_new();
161         gtk_tooltips_set_tip(GTK_TOOLTIPS(filteringdebug_tooltip),
162                              checkbtn_filteringdebug,
163                              _("If checked, turns on logging of filtering and processing rules.\n"
164                                 "The log is accessible from 'Tools/Filtering log'.\n"
165                                 "Caution: enabling this option will slow down the filtering/processing, "
166                                 "this might be critical when applying many rules upon thousands of "
167                                 "messages."),
168                                 NULL);
169
170         vbox_filteringdebug = gtkut_get_options_frame(vbox_filteringdebug_log, &frame_filteringdebug,
171                                                         _("Log filtering/processing when..."));
172
173         PACK_CHECK_BUTTON (vbox_filteringdebug, checkbtn_filteringdebug_inc,
174                            _("filtering at incorporation"));
175         hbox_filteringdebug_inc = gtk_hbox_new (FALSE, 8);
176         gtk_container_add (GTK_CONTAINER (vbox_filteringdebug), hbox_filteringdebug_inc);
177         gtk_widget_show (hbox_filteringdebug_inc);
178
179         PACK_CHECK_BUTTON (vbox_filteringdebug, checkbtn_filteringdebug_manual,
180                            _("manually filtering"));
181         hbox_filteringdebug_manual = gtk_hbox_new (FALSE, 8);
182         gtk_container_add (GTK_CONTAINER (vbox_filteringdebug), hbox_filteringdebug_manual);
183         gtk_widget_show (hbox_filteringdebug_manual);
184
185         PACK_CHECK_BUTTON (vbox_filteringdebug, checkbtn_filteringdebug_folder_proc,
186                            _("processing folders"));
187         hbox_filteringdebug_folder_proc = gtk_hbox_new (FALSE, 8);
188         gtk_container_add (GTK_CONTAINER (vbox_filteringdebug), hbox_filteringdebug_folder_proc);
189         gtk_widget_show (hbox_filteringdebug_folder_proc);
190
191         PACK_CHECK_BUTTON (vbox_filteringdebug, checkbtn_filteringdebug_pre_proc,
192                            _("pre-processing folders"));
193         hbox_filteringdebug_pre_proc = gtk_hbox_new (FALSE, 8);
194         gtk_container_add (GTK_CONTAINER (vbox_filteringdebug), hbox_filteringdebug_pre_proc);
195         gtk_widget_show (hbox_filteringdebug_pre_proc);
196
197         PACK_CHECK_BUTTON (vbox_filteringdebug, checkbtn_filteringdebug_post_proc,
198                            _("post-processing folders"));
199         hbox_filteringdebug_post_proc = gtk_hbox_new (FALSE, 8);
200         gtk_container_add (GTK_CONTAINER (vbox_filteringdebug), hbox_filteringdebug_post_proc);
201         gtk_widget_show (hbox_filteringdebug_post_proc);
202
203         SET_TOGGLE_SENSITIVITY(checkbtn_filteringdebug, checkbtn_filteringdebug_inc);
204         SET_TOGGLE_SENSITIVITY(checkbtn_filteringdebug, checkbtn_filteringdebug_manual);
205         SET_TOGGLE_SENSITIVITY(checkbtn_filteringdebug, checkbtn_filteringdebug_folder_proc);
206         SET_TOGGLE_SENSITIVITY(checkbtn_filteringdebug, checkbtn_filteringdebug_pre_proc);
207         SET_TOGGLE_SENSITIVITY(checkbtn_filteringdebug, checkbtn_filteringdebug_post_proc);
208
209         hbox_filteringdebug_level = gtk_hbox_new (FALSE, 8);
210         gtk_widget_show (hbox_filteringdebug_level);
211         gtk_box_pack_start(GTK_BOX (vbox_filteringdebug_log), hbox_filteringdebug_level, FALSE, FALSE, 0);
212
213         label_debug_level = gtk_label_new (_("Log level"));
214         gtk_widget_show (label_debug_level);
215         gtk_box_pack_start(GTK_BOX(hbox_filteringdebug_level), label_debug_level, FALSE, FALSE, 0);
216
217         optmenu_filteringdebug_level = gtk_option_menu_new ();
218         gtk_widget_show (optmenu_filteringdebug_level);
219         
220         menu = gtk_menu_new ();
221         MENUITEM_ADD (menu, menuitem, _("Low"), 0);
222         MENUITEM_ADD (menu, menuitem, _("Medium"), 1);
223         MENUITEM_ADD (menu, menuitem, _("High"), 2);
224
225         gtk_option_menu_set_menu (GTK_OPTION_MENU (optmenu_filteringdebug_level), menu);
226         gtk_box_pack_start(GTK_BOX(hbox_filteringdebug_level), optmenu_filteringdebug_level, FALSE, FALSE, 0);
227
228         filteringdebug_level_tooltip = gtk_tooltips_new();
229         gtk_tooltips_set_tip(GTK_TOOLTIPS(filteringdebug_level_tooltip),
230                              optmenu_filteringdebug_level,
231                              _("Select the level of detail of the logging.\n"
232                                 "Choose Low to see when rules are applied, which "
233                                 "conditions match or don't match and what actions are "
234                                 "performed.\n"
235                                 "Choose Medium to see more details about the message "
236                                 "that is being processed, and why rules are skipped.\n"
237                                 "Choose High to explicitly show the reason why all "
238                                 "rules are processed or skipped, and why all conditions "
239                                 "are matched or not matched.\n"
240                                 "Caution: the higher the level, the greater the "
241                                 "impact on performance."),
242                              NULL);
243
244         PACK_CHECK_BUTTON (vbox_filteringdebug_log, checkbtn_debug_cliplog,
245                            _("Restrict the log size"));
246         hbox_debug_cliplog = gtk_hbox_new (FALSE, 8);
247         gtk_container_add (GTK_CONTAINER (vbox_filteringdebug_log), hbox_debug_cliplog);
248         gtk_widget_show (hbox_debug_cliplog);
249         
250         debug_loglength_label = gtk_label_new (_("Log window length"));
251         gtk_box_pack_start (GTK_BOX (hbox_debug_cliplog), debug_loglength_label,
252                             FALSE, TRUE, 0);
253         gtk_widget_show (GTK_WIDGET (debug_loglength_label));
254         
255         debug_loglength_tooltip = gtk_tooltips_new();
256
257         spinbtn_debug_loglength_adj = gtk_adjustment_new (500, 0, G_MAXINT, 1, 10, 10);
258         spinbtn_debug_loglength = gtk_spin_button_new
259                 (GTK_ADJUSTMENT (spinbtn_debug_loglength_adj), 1, 0);
260         gtk_widget_show (spinbtn_debug_loglength);
261         gtk_box_pack_start (GTK_BOX (hbox_debug_cliplog), spinbtn_debug_loglength,
262                             FALSE, FALSE, 0);
263         gtk_widget_set_size_request (GTK_WIDGET (spinbtn_debug_loglength), 64, -1);
264         gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spinbtn_debug_loglength), TRUE);
265
266         gtk_tooltips_set_tip(GTK_TOOLTIPS(debug_loglength_tooltip), spinbtn_debug_loglength,
267                              _("0 to stop logging in the log window"),
268                              NULL);
269
270         label = gtk_label_new(_("lines"));
271         gtk_widget_show (label);
272         gtk_box_pack_start(GTK_BOX(hbox_debug_cliplog), label, FALSE, FALSE, 0);
273
274         SET_TOGGLE_SENSITIVITY(checkbtn_debug_cliplog, debug_loglength_label);
275         SET_TOGGLE_SENSITIVITY(checkbtn_debug_cliplog, spinbtn_debug_loglength);
276         SET_TOGGLE_SENSITIVITY(checkbtn_debug_cliplog, label);
277
278         SET_TOGGLE_SENSITIVITY(checkbtn_filteringdebug, optmenu_filteringdebug_level);
279         SET_TOGGLE_SENSITIVITY(checkbtn_filteringdebug, checkbtn_debug_cliplog);
280         SET_TOGGLE_SENSITIVITY(checkbtn_filteringdebug, label_debug_level);
281
282         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_cliplog), 
283                 prefs_common.cliplog);
284         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_debug_cliplog), 
285                 prefs_common.filtering_debug_cliplog);
286         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_filteringdebug), 
287                 prefs_common.enable_filtering_debug);
288         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_filteringdebug_inc), 
289                 prefs_common.enable_filtering_debug_inc);
290         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_filteringdebug_manual), 
291                 prefs_common.enable_filtering_debug_manual);
292         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_filteringdebug_folder_proc), 
293                 prefs_common.enable_filtering_debug_folder_proc);
294         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_filteringdebug_pre_proc), 
295                 prefs_common.enable_filtering_debug_pre_proc);
296         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_filteringdebug_post_proc), 
297                 prefs_common.enable_filtering_debug_post_proc);
298
299         gtk_spin_button_set_value(GTK_SPIN_BUTTON(spinbtn_loglength),
300                 prefs_common.loglength);
301         gtk_spin_button_set_value(GTK_SPIN_BUTTON(spinbtn_debug_loglength),
302                 prefs_common.filtering_debug_loglength);
303
304         gtk_option_menu_set_history(GTK_OPTION_MENU(optmenu_filteringdebug_level),
305                         prefs_common.filtering_debug_level);
306
307         prefs_logging->checkbtn_cliplog = checkbtn_cliplog;
308         prefs_logging->spinbtn_loglength = spinbtn_loglength;
309         prefs_logging->checkbtn_debug_cliplog = checkbtn_debug_cliplog;
310         prefs_logging->spinbtn_debug_loglength = spinbtn_debug_loglength;
311         prefs_logging->checkbtn_filteringdebug = checkbtn_filteringdebug;
312         prefs_logging->checkbtn_filteringdebug_inc = checkbtn_filteringdebug_inc;
313         prefs_logging->checkbtn_filteringdebug_manual = checkbtn_filteringdebug_manual;
314         prefs_logging->checkbtn_filteringdebug_folder_proc = checkbtn_filteringdebug_folder_proc;
315         prefs_logging->checkbtn_filteringdebug_pre_proc = checkbtn_filteringdebug_pre_proc;
316         prefs_logging->checkbtn_filteringdebug_post_proc = checkbtn_filteringdebug_post_proc;
317         prefs_logging->optmenu_filteringdebug_level = optmenu_filteringdebug_level;
318
319         prefs_logging->page.widget = vbox1;
320 }
321
322 static void prefs_logging_save(PrefsPage *_page)
323 {
324         LoggingPage *page = (LoggingPage *) _page;
325         MainWindow *mainwindow;
326         gboolean filtering_debug_enabled;
327         GtkWidget *menu;
328         GtkWidget *menuitem;
329
330         menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(page->optmenu_filteringdebug_level));
331         menuitem = gtk_menu_get_active(GTK_MENU(menu));
332         prefs_common.filtering_debug_level = GPOINTER_TO_INT
333                         (g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID));
334
335         prefs_common.cliplog = gtk_toggle_button_get_active(
336                 GTK_TOGGLE_BUTTON(page->checkbtn_cliplog));
337         prefs_common.loglength = gtk_spin_button_get_value_as_int(
338                 GTK_SPIN_BUTTON(page->spinbtn_loglength));
339         prefs_common.filtering_debug_cliplog = gtk_toggle_button_get_active(
340                 GTK_TOGGLE_BUTTON(page->checkbtn_debug_cliplog));
341         prefs_common.filtering_debug_loglength = gtk_spin_button_get_value_as_int(
342                 GTK_SPIN_BUTTON(page->spinbtn_debug_loglength));
343         filtering_debug_enabled = prefs_common.enable_filtering_debug;
344         prefs_common.enable_filtering_debug = gtk_toggle_button_get_active(
345                 GTK_TOGGLE_BUTTON(page->checkbtn_filteringdebug));
346         if (filtering_debug_enabled != prefs_common.enable_filtering_debug) {
347                 if (prefs_common.enable_filtering_debug)
348                         log_message(LOG_DEBUG_FILTERING, _("filtering log enabled\n"));
349                 else
350                         log_message(LOG_DEBUG_FILTERING, _("filtering log disabled\n"));
351         }
352         prefs_common.enable_filtering_debug_inc = gtk_toggle_button_get_active(
353                 GTK_TOGGLE_BUTTON(page->checkbtn_filteringdebug_inc));
354         prefs_common.enable_filtering_debug_manual = gtk_toggle_button_get_active(
355                 GTK_TOGGLE_BUTTON(page->checkbtn_filteringdebug_manual));
356         prefs_common.enable_filtering_debug_folder_proc = gtk_toggle_button_get_active(
357                 GTK_TOGGLE_BUTTON(page->checkbtn_filteringdebug_folder_proc));
358         prefs_common.enable_filtering_debug_pre_proc = gtk_toggle_button_get_active(
359                 GTK_TOGGLE_BUTTON(page->checkbtn_filteringdebug_pre_proc));
360         prefs_common.enable_filtering_debug_post_proc = gtk_toggle_button_get_active(
361                 GTK_TOGGLE_BUTTON(page->checkbtn_filteringdebug_post_proc));
362
363         mainwindow = mainwindow_get_mainwindow();
364         log_window_set_clipping(mainwindow->logwin, prefs_common.cliplog,
365                                 prefs_common.loglength);
366         log_window_set_clipping(mainwindow->filtering_debugwin, prefs_common.filtering_debug_cliplog,
367                                 prefs_common.filtering_debug_loglength);
368 }
369
370 static void prefs_logging_destroy_widget(PrefsPage *_page)
371 {
372 }
373
374 LoggingPage *prefs_logging;
375
376 void prefs_logging_init(void)
377 {
378         LoggingPage *page;
379         static gchar *path[3];
380
381         path[0] = _("Other");
382         path[1] = _("Logging");
383         path[2] = NULL;
384
385         page = g_new0(LoggingPage, 1);
386         page->page.path = path;
387         page->page.create_widget = prefs_logging_create_widget;
388         page->page.destroy_widget = prefs_logging_destroy_widget;
389         page->page.save_page = prefs_logging_save;
390         page->page.weight = 5.0;
391         prefs_gtk_register_page((PrefsPage *) page);
392         prefs_logging = page;
393 }
394
395 void prefs_logging_done(void)
396 {
397         prefs_gtk_unregister_page((PrefsPage *) prefs_logging);
398         g_free(prefs_logging);
399 }