2007-03-21 [wwp] 2.8.1cvs31
[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                            _("Clip 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 debug log"));
153
154         PACK_CHECK_BUTTON (vbox_filteringdebug_log, checkbtn_filteringdebug,
155                            _("Enable debugging 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), checkbtn_filteringdebug,
162                              _("If checked, turns on debugging of filtering and processing rules.\n"
163                                         "Debug log is available from 'Tools/Filtering debug window'.\n"
164                                         "Caution: enabling this option will slow down the filtering/processing, "
165                                         "this might be critical when applying many rules upon thousands of messages."),
166                              NULL);
167
168         vbox_filteringdebug = gtkut_get_options_frame(vbox_filteringdebug_log, &frame_filteringdebug,
169                                                         _("Debugging of filtering/processing rules when.."));
170
171         PACK_CHECK_BUTTON (vbox_filteringdebug, checkbtn_filteringdebug_inc,
172                            _("filtering at incorporation"));
173         hbox_filteringdebug_inc = gtk_hbox_new (FALSE, 8);
174         gtk_container_add (GTK_CONTAINER (vbox_filteringdebug), hbox_filteringdebug_inc);
175         gtk_widget_show (hbox_filteringdebug_inc);
176
177         PACK_CHECK_BUTTON (vbox_filteringdebug, checkbtn_filteringdebug_manual,
178                            _("manually filtering"));
179         hbox_filteringdebug_manual = gtk_hbox_new (FALSE, 8);
180         gtk_container_add (GTK_CONTAINER (vbox_filteringdebug), hbox_filteringdebug_manual);
181         gtk_widget_show (hbox_filteringdebug_manual);
182
183         PACK_CHECK_BUTTON (vbox_filteringdebug, checkbtn_filteringdebug_folder_proc,
184                            _("processing folders"));
185         hbox_filteringdebug_folder_proc = gtk_hbox_new (FALSE, 8);
186         gtk_container_add (GTK_CONTAINER (vbox_filteringdebug), hbox_filteringdebug_folder_proc);
187         gtk_widget_show (hbox_filteringdebug_folder_proc);
188
189         PACK_CHECK_BUTTON (vbox_filteringdebug, checkbtn_filteringdebug_pre_proc,
190                            _("pre-processing folders"));
191         hbox_filteringdebug_pre_proc = gtk_hbox_new (FALSE, 8);
192         gtk_container_add (GTK_CONTAINER (vbox_filteringdebug), hbox_filteringdebug_pre_proc);
193         gtk_widget_show (hbox_filteringdebug_pre_proc);
194
195         PACK_CHECK_BUTTON (vbox_filteringdebug, checkbtn_filteringdebug_post_proc,
196                            _("post-processing folders"));
197         hbox_filteringdebug_post_proc = gtk_hbox_new (FALSE, 8);
198         gtk_container_add (GTK_CONTAINER (vbox_filteringdebug), hbox_filteringdebug_post_proc);
199         gtk_widget_show (hbox_filteringdebug_post_proc);
200
201         SET_TOGGLE_SENSITIVITY(checkbtn_filteringdebug, checkbtn_filteringdebug_inc);
202         SET_TOGGLE_SENSITIVITY(checkbtn_filteringdebug, checkbtn_filteringdebug_manual);
203         SET_TOGGLE_SENSITIVITY(checkbtn_filteringdebug, checkbtn_filteringdebug_folder_proc);
204         SET_TOGGLE_SENSITIVITY(checkbtn_filteringdebug, checkbtn_filteringdebug_pre_proc);
205         SET_TOGGLE_SENSITIVITY(checkbtn_filteringdebug, checkbtn_filteringdebug_post_proc);
206
207         hbox_filteringdebug_level = gtk_hbox_new (FALSE, 8);
208         gtk_widget_show (hbox_filteringdebug_level);
209         gtk_box_pack_start(GTK_BOX (vbox_filteringdebug_log), hbox_filteringdebug_level, FALSE, FALSE, 0);
210
211         label_debug_level = gtk_label_new (_("Debug level"));
212         gtk_widget_show (label_debug_level);
213         gtk_box_pack_start(GTK_BOX(hbox_filteringdebug_level), label_debug_level, FALSE, FALSE, 0);
214
215         optmenu_filteringdebug_level = gtk_option_menu_new ();
216         gtk_widget_show (optmenu_filteringdebug_level);
217         
218         menu = gtk_menu_new ();
219         MENUITEM_ADD (menu, menuitem, _("Low"), 0);
220         MENUITEM_ADD (menu, menuitem, _("Medium"), 1);
221         MENUITEM_ADD (menu, menuitem, _("High"), 2);
222
223         gtk_option_menu_set_menu (GTK_OPTION_MENU (optmenu_filteringdebug_level), menu);
224         gtk_box_pack_start(GTK_BOX(hbox_filteringdebug_level), optmenu_filteringdebug_level, FALSE, FALSE, 0);
225
226         filteringdebug_level_tooltip = gtk_tooltips_new();
227         gtk_tooltips_set_tip(GTK_TOOLTIPS(filteringdebug_level_tooltip), optmenu_filteringdebug_level,
228                              _("Select the level of detail displayed if debugging is enabled.\n"
229                                 "Choose low level to see when rules are applied, what conditions match or not and what actions are performed.\n"
230                                 "Choose medium level to see more detail about the message that is being processed, and why rules are skipped.\n"
231                                 "Choose high level to explicitely show the reason why all rules are skipped or not, and why all conditions are matching or not.\n"
232                                 "Caution: the higher the level is, the more it will impact the performances."),
233                              NULL);
234
235         PACK_CHECK_BUTTON (vbox_filteringdebug_log, checkbtn_debug_cliplog,
236                            _("Clip the log size"));
237         hbox_debug_cliplog = gtk_hbox_new (FALSE, 8);
238         gtk_container_add (GTK_CONTAINER (vbox_filteringdebug_log), hbox_debug_cliplog);
239         gtk_widget_show (hbox_debug_cliplog);
240         
241         debug_loglength_label = gtk_label_new (_("Log window length"));
242         gtk_box_pack_start (GTK_BOX (hbox_debug_cliplog), debug_loglength_label,
243                             FALSE, TRUE, 0);
244         gtk_widget_show (GTK_WIDGET (debug_loglength_label));
245         
246         debug_loglength_tooltip = gtk_tooltips_new();
247
248         spinbtn_debug_loglength_adj = gtk_adjustment_new (500, 0, G_MAXINT, 1, 10, 10);
249         spinbtn_debug_loglength = gtk_spin_button_new
250                 (GTK_ADJUSTMENT (spinbtn_debug_loglength_adj), 1, 0);
251         gtk_widget_show (spinbtn_debug_loglength);
252         gtk_box_pack_start (GTK_BOX (hbox_debug_cliplog), spinbtn_debug_loglength,
253                             FALSE, FALSE, 0);
254         gtk_widget_set_size_request (GTK_WIDGET (spinbtn_debug_loglength), 64, -1);
255         gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spinbtn_debug_loglength), TRUE);
256
257         gtk_tooltips_set_tip(GTK_TOOLTIPS(debug_loglength_tooltip), spinbtn_debug_loglength,
258                              _("0 to stop logging in the log window"),
259                              NULL);
260
261         label = gtk_label_new(_("lines"));
262         gtk_widget_show (label);
263         gtk_box_pack_start(GTK_BOX(hbox_debug_cliplog), label, FALSE, FALSE, 0);
264
265         SET_TOGGLE_SENSITIVITY(checkbtn_debug_cliplog, debug_loglength_label);
266         SET_TOGGLE_SENSITIVITY(checkbtn_debug_cliplog, spinbtn_debug_loglength);
267         SET_TOGGLE_SENSITIVITY(checkbtn_debug_cliplog, label);
268
269         SET_TOGGLE_SENSITIVITY(checkbtn_filteringdebug, optmenu_filteringdebug_level);
270         SET_TOGGLE_SENSITIVITY(checkbtn_filteringdebug, checkbtn_debug_cliplog);
271         SET_TOGGLE_SENSITIVITY(checkbtn_filteringdebug, label_debug_level);
272         SET_TOGGLE_SENSITIVITY(checkbtn_filteringdebug, debug_loglength_label);
273         SET_TOGGLE_SENSITIVITY(checkbtn_filteringdebug, spinbtn_debug_loglength);
274         SET_TOGGLE_SENSITIVITY(checkbtn_filteringdebug, label);
275
276         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_cliplog), 
277                 prefs_common.cliplog);
278         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_debug_cliplog), 
279                 prefs_common.filtering_debug_cliplog);
280         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_filteringdebug), 
281                 prefs_common.enable_filtering_debug);
282         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_filteringdebug_inc), 
283                 prefs_common.enable_filtering_debug_inc);
284         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_filteringdebug_manual), 
285                 prefs_common.enable_filtering_debug_manual);
286         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_filteringdebug_folder_proc), 
287                 prefs_common.enable_filtering_debug_folder_proc);
288         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_filteringdebug_pre_proc), 
289                 prefs_common.enable_filtering_debug_pre_proc);
290         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_filteringdebug_post_proc), 
291                 prefs_common.enable_filtering_debug_post_proc);
292
293         gtk_spin_button_set_value(GTK_SPIN_BUTTON(spinbtn_loglength),
294                 prefs_common.loglength);
295         gtk_spin_button_set_value(GTK_SPIN_BUTTON(spinbtn_debug_loglength),
296                 prefs_common.filtering_debug_loglength);
297
298         gtk_option_menu_set_history(GTK_OPTION_MENU(optmenu_filteringdebug_level),
299                         prefs_common.filtering_debug_level);
300
301         prefs_logging->checkbtn_cliplog = checkbtn_cliplog;
302         prefs_logging->spinbtn_loglength = spinbtn_loglength;
303         prefs_logging->checkbtn_debug_cliplog = checkbtn_debug_cliplog;
304         prefs_logging->spinbtn_debug_loglength = spinbtn_debug_loglength;
305         prefs_logging->checkbtn_filteringdebug = checkbtn_filteringdebug;
306         prefs_logging->checkbtn_filteringdebug_inc = checkbtn_filteringdebug_inc;
307         prefs_logging->checkbtn_filteringdebug_manual = checkbtn_filteringdebug_manual;
308         prefs_logging->checkbtn_filteringdebug_folder_proc = checkbtn_filteringdebug_folder_proc;
309         prefs_logging->checkbtn_filteringdebug_pre_proc = checkbtn_filteringdebug_pre_proc;
310         prefs_logging->checkbtn_filteringdebug_post_proc = checkbtn_filteringdebug_post_proc;
311         prefs_logging->optmenu_filteringdebug_level = optmenu_filteringdebug_level;
312
313         prefs_logging->page.widget = vbox1;
314 }
315
316 static void prefs_logging_save(PrefsPage *_page)
317 {
318         LoggingPage *page = (LoggingPage *) _page;
319         MainWindow *mainwindow;
320         gboolean filtering_debug_enabled;
321         GtkWidget *menu;
322         GtkWidget *menuitem;
323
324         menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(page->optmenu_filteringdebug_level));
325         menuitem = gtk_menu_get_active(GTK_MENU(menu));
326         prefs_common.filtering_debug_level = GPOINTER_TO_INT
327                         (g_object_get_data(G_OBJECT(menuitem), MENU_VAL_ID));
328
329         prefs_common.cliplog = gtk_toggle_button_get_active(
330                 GTK_TOGGLE_BUTTON(page->checkbtn_cliplog));
331         prefs_common.loglength = gtk_spin_button_get_value_as_int(
332                 GTK_SPIN_BUTTON(page->spinbtn_loglength));
333         prefs_common.filtering_debug_cliplog = gtk_toggle_button_get_active(
334                 GTK_TOGGLE_BUTTON(page->checkbtn_debug_cliplog));
335         prefs_common.filtering_debug_loglength = gtk_spin_button_get_value_as_int(
336                 GTK_SPIN_BUTTON(page->spinbtn_debug_loglength));
337         filtering_debug_enabled = prefs_common.enable_filtering_debug;
338         prefs_common.enable_filtering_debug = gtk_toggle_button_get_active(
339                 GTK_TOGGLE_BUTTON(page->checkbtn_filteringdebug));
340         if (filtering_debug_enabled != prefs_common.enable_filtering_debug) {
341                 if (prefs_common.enable_filtering_debug)
342                         log_message(LOG_DEBUG_FILTERING, _("filtering debug enabled\n"));
343                 else
344                         log_message(LOG_DEBUG_FILTERING, _("filtering debug disabled\n"));
345         }
346         prefs_common.enable_filtering_debug_inc = gtk_toggle_button_get_active(
347                 GTK_TOGGLE_BUTTON(page->checkbtn_filteringdebug_inc));
348         prefs_common.enable_filtering_debug_manual = gtk_toggle_button_get_active(
349                 GTK_TOGGLE_BUTTON(page->checkbtn_filteringdebug_manual));
350         prefs_common.enable_filtering_debug_folder_proc = gtk_toggle_button_get_active(
351                 GTK_TOGGLE_BUTTON(page->checkbtn_filteringdebug_folder_proc));
352         prefs_common.enable_filtering_debug_pre_proc = gtk_toggle_button_get_active(
353                 GTK_TOGGLE_BUTTON(page->checkbtn_filteringdebug_pre_proc));
354         prefs_common.enable_filtering_debug_post_proc = gtk_toggle_button_get_active(
355                 GTK_TOGGLE_BUTTON(page->checkbtn_filteringdebug_post_proc));
356
357         mainwindow = mainwindow_get_mainwindow();
358         log_window_set_clipping(mainwindow->logwin, prefs_common.cliplog,
359                                 prefs_common.loglength);
360         log_window_set_clipping(mainwindow->filtering_debugwin, prefs_common.filtering_debug_cliplog,
361                                 prefs_common.filtering_debug_loglength);
362 }
363
364 static void prefs_logging_destroy_widget(PrefsPage *_page)
365 {
366 }
367
368 LoggingPage *prefs_logging;
369
370 void prefs_logging_init(void)
371 {
372         LoggingPage *page;
373         static gchar *path[3];
374
375         path[0] = _("Other");
376         path[1] = _("Logging");
377         path[2] = NULL;
378
379         page = g_new0(LoggingPage, 1);
380         page->page.path = path;
381         page->page.create_widget = prefs_logging_create_widget;
382         page->page.destroy_widget = prefs_logging_destroy_widget;
383         page->page.save_page = prefs_logging_save;
384         page->page.weight = 5.0;
385         prefs_gtk_register_page((PrefsPage *) page);
386         prefs_logging = page;
387 }
388
389 void prefs_logging_done(void)
390 {
391         prefs_gtk_unregister_page((PrefsPage *) prefs_logging);
392         g_free(prefs_logging);
393 }