revise label colouring, add filter action for label colouring, fix filtering issues
[claws.git] / src / prefs_filtering.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <gtk/gtk.h>
28 #include <gtk/gtkoptionmenu.h>
29 #include <gdk/gdkkeysyms.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <errno.h>
34
35 #include "intl.h"
36 #include "main.h"
37 #include "prefs.h"
38 #include "prefs_matcher.h"
39 #include "prefs_filtering.h"
40 #include "prefs_common.h"
41 #include "mainwindow.h"
42 #include "foldersel.h"
43 #include "manage_window.h"
44 #include "inc.h"
45 #include "utils.h"
46 #include "gtkutils.h"
47 #include "alertpanel.h"
48 #include "folder.h"
49 #include "filtering.h"
50 #include "addr_compl.h"
51 #include "labelcolors.h"
52
53 static struct Filtering {
54         GtkWidget *window;
55
56         GtkWidget *ok_btn;
57         GtkWidget *cond_entry;
58         GtkWidget *action_list;
59         GtkWidget *action_combo;
60         GtkWidget *account_list;
61         GtkWidget *account_combo;
62         GtkWidget *dest_entry;
63         GtkWidget *dest_btn;
64         GtkWidget *dest_label;
65         GtkWidget *exec_label;
66         GtkWidget *exec_btn;
67
68         GtkWidget *color_label;
69         GtkWidget *color_optmenu;
70
71         GtkWidget *cond_clist;
72
73         /* need this to make address completion entry work */
74         gint current_action;
75 } filtering;
76
77 #define VSPACING                12
78 #define VSPACING_NARROW         4
79 #define DEFAULT_ENTRY_WIDTH     80
80 #define PREFSBUFSIZE            1024
81
82 /* widget creating functions */
83 static void prefs_filtering_create              (void);
84
85 static void prefs_filtering_set_dialog  (void);
86 static void prefs_filtering_set_list    (void);
87
88 /* callback functions */
89 /* static void prefs_filtering_select_dest_cb   (void); */
90 static void prefs_filtering_register_cb (void);
91 static void prefs_filtering_substitute_cb       (void);
92 static void prefs_filtering_delete_cb   (void);
93 static void prefs_filtering_up          (void);
94 static void prefs_filtering_down                (void);
95 static void prefs_filtering_select              (GtkCList       *clist,
96                                          gint            row,
97                                          gint            column,
98                                          GdkEvent       *event);
99
100 static gint prefs_filtering_deleted     (GtkWidget      *widget,
101                                          GdkEventAny    *event,
102                                          gpointer        data);
103 static void prefs_filtering_key_pressed (GtkWidget      *widget,
104                                          GdkEventKey    *event,
105                                          gpointer        data);
106 static void prefs_filtering_cancel              (void);
107 static void prefs_filtering_ok          (void);
108
109 static void prefs_filtering_condition_define    (void);
110 static gint prefs_filtering_clist_set_row(gint row, FilteringProp * prop);
111 static void prefs_filtering_select_dest(void);
112 static void prefs_filtering_action_select(GtkList *list,
113                                           GtkWidget *widget, 
114                                           gpointer user_data);
115 static void prefs_filtering_action_selection_changed(GtkList *list,
116                                                      gpointer user_data);
117                                           
118 static void prefs_filtering_reset_dialog(void);
119
120 enum {
121         ACTION_MOVE = 0,
122         ACTION_COPY = 1,
123         ACTION_DELETE = 2,
124         ACTION_MARK = 3,
125         ACTION_UNMARK = 4,
126         ACTION_MARK_AS_READ = 5,
127         ACTION_MARK_AS_UNREAD = 6,
128         ACTION_FORWARD = 7,
129         ACTION_FORWARD_AS_ATTACHMENT = 8,
130         ACTION_EXECUTE = 9,
131         ACTION_COLOR = 10
132 };
133
134 static gint get_sel_from_list(GtkList * list)
135 {
136         gint row = 0;
137         void * sel;
138         GList * child;
139
140         if (list->selection == NULL)
141                 return -1;
142
143         sel = list->selection->data;
144         for(child = list->children ; child != NULL ;
145             child = g_list_next(child)) {
146                 if (child->data == sel)
147                         return row;
148                 row ++;
149         }
150         
151         return row;
152 }
153
154 static gint get_account_id_from_list_id(gint list_id)
155 {
156         GList * accounts;
157
158         for (accounts = account_get_list() ; accounts != NULL;
159              accounts = accounts->next) {
160                 PrefsAccount *ac = (PrefsAccount *)accounts->data;
161
162                 if (list_id == 0)
163                         return ac->account_id;
164                 list_id--;
165         }
166         return 0;
167 }
168
169 static gint get_list_id_from_account_id(gint account_id)
170 {
171         GList * accounts;
172         gint list_id = 0;
173
174         for (accounts = account_get_list() ; accounts != NULL;
175              accounts = accounts->next) {
176                 PrefsAccount *ac = (PrefsAccount *)accounts->data;
177
178                 if (account_id == ac->account_id)
179                         return list_id;
180                 list_id++;
181         }
182         return 0;
183 }
184
185 static gint prefs_filtering_get_matching_from_action(gint action_id)
186 {
187         switch(action_id) {
188         case ACTION_MOVE:
189                 return MATCHING_ACTION_MOVE;
190         case ACTION_COPY:
191                 return MATCHING_ACTION_COPY;
192         case ACTION_DELETE:
193                 return MATCHING_ACTION_DELETE;
194         case ACTION_MARK:
195                 return MATCHING_ACTION_MARK;
196         case ACTION_UNMARK:
197                 return MATCHING_ACTION_UNMARK;
198         case ACTION_MARK_AS_READ:
199                 return MATCHING_ACTION_MARK_AS_READ;
200         case ACTION_MARK_AS_UNREAD:
201                 return MATCHING_ACTION_MARK_AS_UNREAD;
202         case ACTION_FORWARD:
203                 return MATCHING_ACTION_FORWARD;
204         case ACTION_FORWARD_AS_ATTACHMENT:
205                 return MATCHING_ACTION_FORWARD_AS_ATTACHMENT;
206         case ACTION_EXECUTE:
207                 return MATCHING_EXECUTE;
208         case ACTION_COLOR:
209                 return MATCHING_ACTION_COLOR;
210         default:
211                 return -1;
212         }
213 }
214
215 gchar * action_text [] = {
216         "Move", "Copy", "Delete",
217         "Mark", "Unmark", "Mark as read", "Mark as unread",
218         "Forward", "Forward as attachment", "Execute",
219         "Color"
220 };
221
222 void prefs_filtering_open(void)
223 {
224         if (prefs_rc_is_readonly(FILTERING_RC))
225                 return;
226
227         inc_autocheck_timer_remove();
228
229         if (!filtering.window) {
230                 prefs_filtering_create();
231         }
232
233         manage_window_set_transient(GTK_WINDOW(filtering.window));
234         gtk_widget_grab_focus(filtering.ok_btn);
235
236         prefs_filtering_set_dialog();
237
238         gtk_widget_show(filtering.window);
239
240         start_address_completion();
241 }
242
243 /* prefs_filtering_close() - just to have one common exit point */
244 static void prefs_filtering_close(void)
245 {
246         end_address_completion();
247         
248         gtk_widget_hide(filtering.window);
249 }
250
251 static void prefs_filtering_create(void)
252 {
253         GtkWidget *window;
254         GtkWidget *vbox;
255         GtkWidget *ok_btn;
256         GtkWidget *cancel_btn;
257         GtkWidget *confirm_area;
258
259         GtkWidget *vbox1;
260         GtkWidget *hbox1;
261         GtkWidget *reg_hbox;
262         GtkWidget *arrow;
263         GtkWidget *btn_hbox;
264
265         GtkWidget *cond_label;
266         GtkWidget *cond_entry;
267         GtkWidget *cond_btn;
268         GtkWidget *action_label;
269         GtkWidget *action_list;
270         GtkWidget *action_combo;
271         GtkWidget *account_label;
272         GtkWidget *account_list;
273         GtkWidget *account_combo;
274         GtkWidget *dest_label;
275         GtkWidget *exec_label;
276         GtkWidget *dest_entry;
277         GtkWidget *dest_btn;
278         GtkWidget *exec_btn;
279         GtkWidget *color_label;
280         GtkWidget *color_optmenu;
281
282         GtkWidget *reg_btn;
283         GtkWidget *subst_btn;
284         GtkWidget *del_btn;
285
286         GtkWidget *cond_hbox;
287         GtkWidget *cond_scrolledwin;
288         GtkWidget *cond_clist;
289
290         GtkWidget *btn_vbox;
291         GtkWidget *up_btn;
292         GtkWidget *down_btn;
293
294         GtkWidget *dummy;
295
296         GList *combo_items;
297         gint i;
298
299         GList *accounts;
300         GList * cur;
301
302         gchar *title[] = {_("Registered rules")};
303
304         debug_print(_("Creating filtering setting window...\n"));
305
306         window = gtk_window_new (GTK_WINDOW_DIALOG);
307         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
308         gtk_window_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
309         gtk_window_set_modal (GTK_WINDOW (window), TRUE);
310         gtk_window_set_policy (GTK_WINDOW (window), FALSE, TRUE, FALSE);
311
312         vbox = gtk_vbox_new (FALSE, 6);
313         gtk_widget_show (vbox);
314         gtk_container_add (GTK_CONTAINER (window), vbox);
315
316         gtkut_button_set_create(&confirm_area, &ok_btn, _("OK"),
317                                 &cancel_btn, _("Cancel"), NULL, NULL);
318         gtk_widget_show (confirm_area);
319         gtk_box_pack_end (GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
320         gtk_widget_grab_default (ok_btn);
321
322         gtk_window_set_title (GTK_WINDOW(window),
323                               _("Filtering setting"));
324         gtk_signal_connect (GTK_OBJECT(window), "delete_event",
325                             GTK_SIGNAL_FUNC(prefs_filtering_deleted), NULL);
326         gtk_signal_connect (GTK_OBJECT(window), "key_press_event",
327                             GTK_SIGNAL_FUNC(prefs_filtering_key_pressed), NULL);
328         gtk_signal_connect (GTK_OBJECT(window), "focus_in_event",
329                             GTK_SIGNAL_FUNC(manage_window_focus_in), NULL);
330         gtk_signal_connect (GTK_OBJECT(window), "focus_out_event",
331                             GTK_SIGNAL_FUNC(manage_window_focus_out), NULL);
332         gtk_signal_connect (GTK_OBJECT(ok_btn), "clicked",
333                             GTK_SIGNAL_FUNC(prefs_filtering_ok), NULL);
334         gtk_signal_connect (GTK_OBJECT(cancel_btn), "clicked",
335                             GTK_SIGNAL_FUNC(prefs_filtering_cancel), NULL);
336
337         vbox1 = gtk_vbox_new (FALSE, VSPACING);
338         gtk_widget_show (vbox1);
339         gtk_box_pack_start (GTK_BOX (vbox), vbox1, TRUE, TRUE, 0);
340         gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
341
342         cond_label = gtk_label_new (_("Condition"));
343         gtk_widget_show (cond_label);
344         gtk_misc_set_alignment (GTK_MISC (cond_label), 0, 0.5);
345         gtk_box_pack_start (GTK_BOX (vbox1), cond_label, FALSE, FALSE, 0);
346
347         hbox1 = gtk_hbox_new (FALSE, VSPACING);
348         gtk_widget_show (vbox1);
349         gtk_box_pack_start (GTK_BOX (vbox1), hbox1, FALSE, FALSE, 0);
350         gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
351
352         cond_entry = gtk_entry_new ();
353         gtk_widget_show (cond_entry);
354 //      gtk_widget_set_usize (cond_entry, 200, -1);
355         gtk_box_pack_start (GTK_BOX (hbox1), cond_entry, TRUE, TRUE, 0);
356
357         cond_btn = gtk_button_new_with_label (_("Define ..."));
358         gtk_widget_show (cond_btn);
359         gtk_box_pack_start (GTK_BOX (hbox1), cond_btn, FALSE, FALSE, 0);
360         gtk_signal_connect (GTK_OBJECT (cond_btn), "clicked",
361                             GTK_SIGNAL_FUNC (prefs_filtering_condition_define),
362                             NULL);
363
364         hbox1 = gtk_hbox_new (FALSE, VSPACING);
365         gtk_widget_show (vbox1);
366         gtk_box_pack_start (GTK_BOX (vbox1), hbox1, FALSE, FALSE, 0);
367         gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
368
369         action_label = gtk_label_new (_("Action"));
370         gtk_widget_show (action_label);
371         gtk_misc_set_alignment (GTK_MISC (action_label), 0, 0.5);
372         gtk_box_pack_start (GTK_BOX (hbox1), action_label, FALSE, FALSE, 0);
373
374         action_combo = gtk_combo_new ();
375         gtk_widget_show (action_combo);
376 //      gtk_widget_set_usize (action_combo, 200, -1);
377         gtk_entry_set_editable(GTK_ENTRY(GTK_COMBO(action_combo)->entry),
378                                FALSE);
379
380         combo_items = NULL;
381
382         for(i = 0 ; i < (gint) (sizeof(action_text) / sizeof(gchar *)) ;
383             i++) {
384                 combo_items = g_list_append(combo_items,
385                                             (gpointer) _(action_text[i]));
386         }
387         gtk_combo_set_popdown_strings(GTK_COMBO(action_combo), combo_items);
388
389         g_list_free(combo_items);
390
391         gtk_box_pack_start (GTK_BOX (hbox1), action_combo,
392                             TRUE, TRUE, 0);
393         action_list = GTK_COMBO(action_combo)->list;
394         gtk_signal_connect (GTK_OBJECT (action_list), "select-child",
395                             GTK_SIGNAL_FUNC (prefs_filtering_action_select),
396                             NULL);
397
398         gtk_signal_connect(GTK_OBJECT(action_list), "selection-changed",
399                            GTK_SIGNAL_FUNC(prefs_filtering_action_selection_changed),
400                            NULL);
401
402         /* accounts */
403
404         hbox1 = gtk_hbox_new (FALSE, VSPACING);
405         gtk_widget_show (vbox1);
406         gtk_box_pack_start (GTK_BOX (vbox1), hbox1, FALSE, FALSE, 0);
407         gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
408
409         account_label = gtk_label_new (_("Account"));
410         gtk_widget_show (account_label);
411         gtk_misc_set_alignment (GTK_MISC (account_label), 0, 0.5);
412         gtk_box_pack_start (GTK_BOX (hbox1), account_label, FALSE, FALSE, 0);
413
414         account_combo = gtk_combo_new ();
415         gtk_widget_show (account_combo);
416
417         combo_items = NULL;
418         for (accounts = account_get_list() ; accounts != NULL;
419              accounts = accounts->next) {
420                 PrefsAccount *ac = (PrefsAccount *)accounts->data;
421                 gchar *name;
422
423                 name = g_strdup_printf("%s <%s> (%s)",
424                                        ac->name, ac->address,
425                                        ac->account_name);
426                 combo_items = g_list_append(combo_items, (gpointer) name);
427         }
428
429         gtk_combo_set_popdown_strings(GTK_COMBO(account_combo), combo_items);
430
431         for(cur = g_list_first(combo_items) ; cur != NULL ;
432             cur = g_list_next(cur))
433                 g_free(cur->data);
434         g_list_free(combo_items);
435
436         gtk_box_pack_start (GTK_BOX (hbox1), account_combo,
437                             TRUE, TRUE, 0);
438         account_list = GTK_COMBO(account_combo)->list;
439         gtk_entry_set_editable(GTK_ENTRY(GTK_COMBO(account_combo)->entry),
440                                FALSE);
441
442         /* destination */
443
444         hbox1 = gtk_hbox_new (FALSE, VSPACING);
445         gtk_widget_show (vbox1);
446         gtk_box_pack_start (GTK_BOX (vbox1), hbox1, FALSE, FALSE, 0);
447         gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
448
449         dest_label = gtk_label_new (_("Destination"));
450         gtk_widget_show (dest_label);
451         gtk_misc_set_alignment (GTK_MISC (dest_label), 0, 0.5);
452         gtk_box_pack_start (GTK_BOX (hbox1), dest_label, FALSE, FALSE, 0);
453
454         exec_label = gtk_label_new (_("Execute"));
455         gtk_widget_show (exec_label);
456         gtk_misc_set_alignment (GTK_MISC (exec_label), 0, 0.5);
457         gtk_box_pack_start (GTK_BOX (hbox1), exec_label, FALSE, FALSE, 0);
458         
459         color_label = gtk_label_new (_("Color"));
460         gtk_widget_show(color_label);
461         gtk_misc_set_alignment(GTK_MISC(color_label), 0, 0.5);
462         gtk_box_pack_start(GTK_BOX(hbox1), color_label, FALSE, FALSE, 0);
463
464         dest_entry = gtk_entry_new ();
465         gtk_widget_show (dest_entry);
466 //      gtk_widget_set_usize (dest_entry, 200, -1);
467         gtk_box_pack_start (GTK_BOX (hbox1), dest_entry, TRUE, TRUE, 0);
468         
469         color_optmenu = gtk_option_menu_new();
470         gtk_option_menu_set_menu(GTK_OPTION_MENU(color_optmenu), 
471                                  labelcolors_create_color_menu());
472 //      gtk_widget_set_usize(color_optmenu, -1, -1);
473         gtk_box_pack_start(GTK_BOX(hbox1), color_optmenu, TRUE, TRUE, 0);
474
475         dest_btn = gtk_button_new_with_label (_("Select ..."));
476         gtk_widget_show (dest_btn);
477         gtk_box_pack_start (GTK_BOX (hbox1), dest_btn, FALSE, FALSE, 0);
478         gtk_signal_connect (GTK_OBJECT (dest_btn), "clicked",
479                             GTK_SIGNAL_FUNC (prefs_filtering_select_dest),
480                             NULL);
481
482         exec_btn = gtk_button_new_with_label (_("Info ..."));
483         gtk_widget_show (exec_btn);
484         gtk_box_pack_start (GTK_BOX (hbox1), exec_btn, FALSE, FALSE, 0);
485         gtk_signal_connect (GTK_OBJECT (exec_btn), "clicked",
486                             GTK_SIGNAL_FUNC (prefs_matcher_exec_info),
487                             NULL);
488
489 //      dummy = gtk_label_new("");
490 //      gtk_widget_show (dummy);
491 //      gtk_box_pack_start(GTK_BOX (hbox1), dummy, FALSE, FALSE, 0);
492         
493
494         /* register / substitute / delete */
495
496         reg_hbox = gtk_hbox_new (FALSE, 4);
497         gtk_widget_show (reg_hbox);
498         gtk_box_pack_start (GTK_BOX (vbox1), reg_hbox, FALSE, FALSE, 0);
499
500         arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_OUT);
501         gtk_widget_show (arrow);
502         gtk_box_pack_start (GTK_BOX (reg_hbox), arrow, FALSE, FALSE, 0);
503         gtk_widget_set_usize (arrow, -1, 16);
504
505         btn_hbox = gtk_hbox_new (TRUE, 4);
506         gtk_widget_show (btn_hbox);
507         gtk_box_pack_start (GTK_BOX (reg_hbox), btn_hbox, FALSE, FALSE, 0);
508
509         reg_btn = gtk_button_new_with_label (_("Register"));
510         gtk_widget_show (reg_btn);
511         gtk_box_pack_start (GTK_BOX (btn_hbox), reg_btn, FALSE, TRUE, 0);
512         gtk_signal_connect (GTK_OBJECT (reg_btn), "clicked",
513                             GTK_SIGNAL_FUNC (prefs_filtering_register_cb), NULL);
514
515         subst_btn = gtk_button_new_with_label (_(" Substitute "));
516         gtk_widget_show (subst_btn);
517         gtk_box_pack_start (GTK_BOX (btn_hbox), subst_btn, FALSE, TRUE, 0);
518         gtk_signal_connect (GTK_OBJECT (subst_btn), "clicked",
519                             GTK_SIGNAL_FUNC (prefs_filtering_substitute_cb),
520                             NULL);
521
522         del_btn = gtk_button_new_with_label (_("Delete"));
523         gtk_widget_show (del_btn);
524         gtk_box_pack_start (GTK_BOX (btn_hbox), del_btn, FALSE, TRUE, 0);
525         gtk_signal_connect (GTK_OBJECT (del_btn), "clicked",
526                             GTK_SIGNAL_FUNC (prefs_filtering_delete_cb), NULL);
527
528         cond_hbox = gtk_hbox_new (FALSE, 8);
529         gtk_widget_show (cond_hbox);
530         gtk_box_pack_start (GTK_BOX (vbox1), cond_hbox, TRUE, TRUE, 0);
531
532         cond_scrolledwin = gtk_scrolled_window_new (NULL, NULL);
533         gtk_widget_show (cond_scrolledwin);
534         gtk_widget_set_usize (cond_scrolledwin, -1, 150);
535         gtk_box_pack_start (GTK_BOX (cond_hbox), cond_scrolledwin,
536                             TRUE, TRUE, 0);
537         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (cond_scrolledwin),
538                                         GTK_POLICY_AUTOMATIC,
539                                         GTK_POLICY_AUTOMATIC);
540
541         cond_clist = gtk_clist_new_with_titles(1, title);
542         gtk_widget_show (cond_clist);
543         gtk_container_add (GTK_CONTAINER (cond_scrolledwin), cond_clist);
544         gtk_clist_set_column_width (GTK_CLIST (cond_clist), 0, 80);
545         gtk_clist_set_selection_mode (GTK_CLIST (cond_clist),
546                                       GTK_SELECTION_BROWSE);
547         GTK_WIDGET_UNSET_FLAGS (GTK_CLIST (cond_clist)->column[0].button,
548                                 GTK_CAN_FOCUS);
549         gtk_signal_connect (GTK_OBJECT (cond_clist), "select_row",
550                             GTK_SIGNAL_FUNC (prefs_filtering_select), NULL);
551
552         btn_vbox = gtk_vbox_new (FALSE, 8);
553         gtk_widget_show (btn_vbox);
554         gtk_box_pack_start (GTK_BOX (cond_hbox), btn_vbox, FALSE, FALSE, 0);
555
556         up_btn = gtk_button_new_with_label (_("Up"));
557         gtk_widget_show (up_btn);
558         gtk_box_pack_start (GTK_BOX (btn_vbox), up_btn, FALSE, FALSE, 0);
559         gtk_signal_connect (GTK_OBJECT (up_btn), "clicked",
560                             GTK_SIGNAL_FUNC (prefs_filtering_up), NULL);
561
562         down_btn = gtk_button_new_with_label (_("Down"));
563         gtk_widget_show (down_btn);
564         gtk_box_pack_start (GTK_BOX (btn_vbox), down_btn, FALSE, FALSE, 0);
565         gtk_signal_connect (GTK_OBJECT (down_btn), "clicked",
566                             GTK_SIGNAL_FUNC (prefs_filtering_down), NULL);
567
568         gtk_widget_set_usize(window, 500, -1);
569
570         gtk_widget_show_all(window);
571
572         filtering.window    = window;
573         filtering.ok_btn = ok_btn;
574
575         filtering.cond_entry = cond_entry;
576         filtering.action_list = action_list;
577         filtering.action_combo = action_combo;
578         filtering.account_list = account_list;
579         filtering.account_combo = account_combo;
580         filtering.dest_entry = dest_entry;
581         filtering.dest_btn = dest_btn;
582         filtering.dest_label = dest_label;
583         filtering.exec_label = exec_label;
584         filtering.exec_btn = exec_btn;
585
586         filtering.cond_clist   = cond_clist;
587
588         filtering.color_label   = color_label;
589         filtering.color_optmenu = color_optmenu;
590 }
591
592 static void prefs_filtering_update_hscrollbar(void)
593 {
594         gint optwidth = gtk_clist_optimal_column_width(GTK_CLIST(filtering.cond_clist), 0);
595         gtk_clist_set_column_width(GTK_CLIST(filtering.cond_clist), 0, optwidth);
596 }
597
598 static void prefs_filtering_set_dialog(void)
599 {
600         GtkCList *clist = GTK_CLIST(filtering.cond_clist);
601         GSList *cur;
602         
603         gtk_clist_freeze(clist);
604         gtk_clist_clear(clist);
605
606         prefs_filtering_clist_set_row(-1, NULL);
607         for(cur = prefs_filtering ; cur != NULL ; cur = g_slist_next(cur)) {
608                 FilteringProp * prop = (FilteringProp *) cur->data;
609
610                 prefs_filtering_clist_set_row(-1, prop);
611         }
612
613         prefs_filtering_update_hscrollbar();
614         gtk_clist_thaw(clist);
615
616         prefs_filtering_reset_dialog();
617 }
618
619 static void prefs_filtering_reset_dialog(void)
620 {
621         gtk_list_select_item(GTK_LIST(filtering.action_list), 0);
622         gtk_list_select_item(GTK_LIST(filtering.account_list), 0);
623         gtk_entry_set_text(GTK_ENTRY(filtering.dest_entry), "");
624         gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), "");
625 }
626
627 static void prefs_filtering_set_list(void)
628 {
629         gint row = 1;
630         FilteringProp *prop;
631         GSList * cur;
632         gchar * filtering_str;
633         gchar * tmp;
634
635         for(cur = prefs_filtering ; cur != NULL ; cur = g_slist_next(cur))
636                 filteringprop_free((FilteringProp *) cur->data);
637         g_slist_free(prefs_filtering);
638         prefs_filtering = NULL;
639
640         while (gtk_clist_get_text(GTK_CLIST(filtering.cond_clist),
641                                   row, 0, &filtering_str)) {
642                 if (strcmp(filtering_str, _("(New)")) != 0) {
643                         tmp = filtering_str;
644                         prop = filteringprop_parse(&tmp);
645                         if (prop != NULL)
646                                 prefs_filtering = g_slist_append(prefs_filtering,
647                                                                prop);
648                 }
649                 row++;
650         }
651 }
652
653 static gint prefs_filtering_clist_set_row(gint row, FilteringProp * prop)
654 {
655         GtkCList *clist = GTK_CLIST(filtering.cond_clist);
656         gchar * str;
657         gchar *cond_str[1];
658
659         if (prop == NULL) {
660                 cond_str[0] = _("(New)");
661                 return gtk_clist_append(clist, cond_str);
662         }
663
664         str = filteringprop_to_string(prop);
665         if (str == NULL) {
666                 return -1;
667         }
668         cond_str[0] = str;
669
670         if (row < 0)
671                 row = gtk_clist_append(clist, cond_str);
672         else
673                 gtk_clist_set_text(clist, row, 0, cond_str[0]);
674         g_free(str);
675
676         return row;
677 }
678
679 static void prefs_filtering_condition_define_done(MatcherList * matchers)
680 {
681         gchar * str;
682
683         if (matchers == NULL)
684                 return;
685
686         str = matcherlist_to_string(matchers);
687
688         if (str != NULL) {
689                 gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), str);
690                 g_free(str);
691         }
692 }
693
694 static void prefs_filtering_condition_define(void)
695 {
696         gchar * cond_str;
697         MatcherList * matchers = NULL;
698
699         cond_str = gtk_entry_get_text(GTK_ENTRY(filtering.cond_entry));
700
701         if (*cond_str != '\0') {
702                 gchar * tmp;
703                 
704                 tmp = cond_str;
705                 matchers = matcherlist_parse(&tmp);
706                 if (tmp == NULL)
707                         alertpanel_error(_("Match string is not valid."));
708         }
709
710         prefs_matcher_open(matchers, prefs_filtering_condition_define_done);
711
712         if (matchers != NULL)
713                 matcherlist_free(matchers);
714 }
715
716
717 /* register / substitute delete buttons */
718
719
720 static FilteringProp * prefs_filtering_dialog_to_filtering(void)
721 {
722         MatcherList * cond;
723         gchar * cond_str;
724         FilteringProp * prop;
725         FilteringAction * action;
726         gchar * tmp;
727         gint list_id;
728         gint action_id;
729         gint action_type;
730         gint account_id;
731         gchar * destination;
732         gint labelcolor;
733         
734         cond_str = gtk_entry_get_text(GTK_ENTRY(filtering.cond_entry));
735         if (*cond_str == '\0') {
736                 alertpanel_error(_("Score is not set."));
737                 return NULL;
738         }
739
740         action_id = get_sel_from_list(GTK_LIST(filtering.action_list));
741         action_type = prefs_filtering_get_matching_from_action(action_id);
742         list_id = get_sel_from_list(GTK_LIST(filtering.account_list));
743         account_id = get_account_id_from_list_id(list_id);
744
745         switch (action_id) {
746         case ACTION_MOVE:
747         case ACTION_COPY:
748         case ACTION_FORWARD:
749         case ACTION_FORWARD_AS_ATTACHMENT:
750         case ACTION_EXECUTE:
751                 destination = gtk_entry_get_text(GTK_ENTRY(filtering.dest_entry));
752                 if (*destination == '\0') {
753                         alertpanel_error(_("Destination is not set."));
754                         return NULL;
755                 }
756                 break;
757         case ACTION_COLOR:
758                 labelcolor = labelcolors_get_color_menu_active_item(
759                         gtk_option_menu_get_menu(GTK_OPTION_MENU(filtering.color_optmenu)));
760                 destination = NULL;     
761                 break;
762         default:
763                 destination = NULL;
764                 break;
765         }
766         
767         action = filteringaction_new(action_type, account_id, destination, labelcolor);
768
769         tmp = cond_str;
770         cond = matcherlist_parse(&tmp);
771
772         if (tmp == NULL) {
773                 alertpanel_error(_("Match string is not valid."));
774                 filteringaction_free(action);
775                 return NULL;
776         }
777
778         prop = filteringprop_new(cond, action);
779
780         return prop;
781 }
782
783 static void prefs_filtering_register_cb(void)
784 {
785         FilteringProp * prop;
786         
787         prop = prefs_filtering_dialog_to_filtering();
788         if (prop == NULL)
789                 return;
790         prefs_filtering_clist_set_row(-1, prop);
791
792         filteringprop_free(prop);
793         
794         prefs_filtering_update_hscrollbar();
795 }
796
797 static void prefs_filtering_substitute_cb(void)
798 {
799         GtkCList *clist = GTK_CLIST(filtering.cond_clist);
800         gint row;
801         FilteringProp * prop;
802         
803         if (!clist->selection) return;
804
805         row = GPOINTER_TO_INT(clist->selection->data);
806         if (row == 0) return;
807
808         prop = prefs_filtering_dialog_to_filtering();
809         if (prop == NULL)
810                 return;
811         prefs_filtering_clist_set_row(row, prop);
812
813         filteringprop_free(prop);
814         
815         prefs_filtering_update_hscrollbar();
816 }
817
818 static void prefs_filtering_delete_cb(void)
819 {
820         GtkCList *clist = GTK_CLIST(filtering.cond_clist);
821         gint row;
822
823         if (!clist->selection) return;
824         row = GPOINTER_TO_INT(clist->selection->data);
825         if (row == 0) return;
826
827         if (alertpanel(_("Delete rule"),
828                        _("Do you really want to delete this rule?"),
829                        _("Yes"), _("No"), NULL) == G_ALERTALTERNATE)
830                 return;
831
832         gtk_clist_remove(clist, row);
833
834         prefs_filtering_update_hscrollbar();
835 }
836
837 static void prefs_filtering_up(void)
838 {
839         GtkCList *clist = GTK_CLIST(filtering.cond_clist);
840         gint row;
841
842         if (!clist->selection) return;
843
844         row = GPOINTER_TO_INT(clist->selection->data);
845         if (row > 1) {
846                 gtk_clist_row_move(clist, row, row - 1);
847         }
848 }
849
850 static void prefs_filtering_down(void)
851 {
852         GtkCList *clist = GTK_CLIST(filtering.cond_clist);
853         gint row;
854
855         if (!clist->selection) return;
856
857         row = GPOINTER_TO_INT(clist->selection->data);
858         if (row > 0 && row < clist->rows - 1) {
859                 gtk_clist_row_move(clist, row, row + 1);
860         }
861 }
862
863 static void prefs_filtering_select_set(FilteringProp * prop)
864 {
865         FilteringAction * action;
866         gchar * matcher_str;
867         gint list_id;
868
869         prefs_filtering_reset_dialog();
870
871         action = prop->action;
872
873         matcher_str = matcherlist_to_string(prop->matchers);
874         if (matcher_str == NULL) {
875                 filteringprop_free(prop);
876                 return;
877         }
878
879         gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), matcher_str);
880         
881         if (action->destination)
882                 gtk_entry_set_text(GTK_ENTRY(filtering.dest_entry), action->destination);
883         else
884                 gtk_entry_set_text(GTK_ENTRY(filtering.dest_entry), "");
885
886         switch(action->type) {
887         case MATCHING_ACTION_MOVE:
888                 gtk_list_select_item(GTK_LIST(filtering.action_list),
889                                      ACTION_MOVE);
890                 break;
891         case MATCHING_ACTION_COPY:
892                 gtk_list_select_item(GTK_LIST(filtering.action_list),
893                                      ACTION_COPY);
894                 break;
895         case MATCHING_ACTION_DELETE:
896                 gtk_list_select_item(GTK_LIST(filtering.action_list),
897                                      ACTION_DELETE);
898                 break;
899         case MATCHING_ACTION_MARK:
900                 gtk_list_select_item(GTK_LIST(filtering.action_list),
901                                      ACTION_MARK);
902                 break;
903         case MATCHING_ACTION_UNMARK:
904                 gtk_list_select_item(GTK_LIST(filtering.action_list),
905                                      ACTION_UNMARK);
906                 break;
907         case MATCHING_ACTION_MARK_AS_READ:
908                 gtk_list_select_item(GTK_LIST(filtering.action_list),
909                                      ACTION_MARK_AS_READ);
910                 break;
911         case MATCHING_ACTION_MARK_AS_UNREAD:
912                 gtk_list_select_item(GTK_LIST(filtering.action_list),
913                                      ACTION_MARK_AS_UNREAD);
914                 break;
915         case MATCHING_ACTION_FORWARD:
916                 gtk_list_select_item(GTK_LIST(filtering.action_list),
917                                      ACTION_FORWARD);
918                 list_id = get_list_id_from_account_id(action->account_id);
919                 gtk_list_select_item(GTK_LIST(filtering.account_list),
920                                      list_id);
921                 break;
922         case MATCHING_ACTION_FORWARD_AS_ATTACHMENT:
923                 list_id = get_list_id_from_account_id(action->account_id);
924                 gtk_list_select_item(GTK_LIST(filtering.action_list),
925                                      ACTION_FORWARD_AS_ATTACHMENT);
926                 gtk_list_select_item(GTK_LIST(filtering.account_list),
927                                      list_id);
928                 break;
929         case MATCHING_EXECUTE:
930                 gtk_list_select_item(GTK_LIST(filtering.action_list),
931                                      ACTION_EXECUTE);
932                 break;
933         case MATCHING_ACTION_COLOR:
934                 gtk_list_select_item(GTK_LIST(filtering.action_list),
935                                      ACTION_COLOR);
936                 gtk_option_menu_set_history(GTK_OPTION_MENU(filtering.color_optmenu), action->labelcolor);     
937                 break;
938         }
939
940         g_free(matcher_str);
941 }
942
943 static void prefs_filtering_select(GtkCList *clist, gint row, gint column,
944                                 GdkEvent *event)
945 {
946         FilteringProp * prop;
947         gchar * tmp;
948         gchar * filtering_str;
949
950         if (row == 0) {
951                 prefs_filtering_reset_dialog();
952                 return;
953         }
954
955         if (!gtk_clist_get_text(GTK_CLIST(filtering.cond_clist),
956                                 row, 0, &filtering_str))
957                 return;
958         
959         tmp = filtering_str;
960         prop = filteringprop_parse(&tmp);
961         if (tmp == NULL)
962                 return;
963
964         prefs_filtering_select_set(prop);
965
966         filteringprop_free(prop);
967 }
968
969 static void prefs_filtering_select_dest(void)
970 {
971         FolderItem *dest;
972         gchar * path;
973
974         dest = foldersel_folder_sel(NULL, NULL);
975         if (!dest) return;
976
977         path = folder_item_get_identifier(dest);
978
979         gtk_entry_set_text(GTK_ENTRY(filtering.dest_entry), path);
980         g_free(path);
981 }
982
983 static void prefs_filtering_action_selection_changed(GtkList *list,
984                                                      gpointer user_data)
985 {
986         gint value;
987
988         value = get_sel_from_list(GTK_LIST(filtering.action_list));
989
990         if (filtering.current_action != value) {
991                 if (filtering.current_action == ACTION_FORWARD 
992                 ||  filtering.current_action == ACTION_FORWARD_AS_ATTACHMENT) {
993                         debug_print("unregistering address completion entry\n");
994                         address_completion_unregister_entry(GTK_ENTRY(filtering.dest_entry));
995                 }
996                 if (value == ACTION_FORWARD || value == ACTION_FORWARD_AS_ATTACHMENT) {
997                         debug_print("registering address completion entry\n");
998                         address_completion_register_entry(GTK_ENTRY(filtering.dest_entry));
999                 }
1000                 filtering.current_action = value;
1001         }
1002 }
1003
1004 static void prefs_filtering_action_select(GtkList *list,
1005                                           GtkWidget *widget,
1006                                           gpointer user_data)
1007 {
1008         gint value;
1009
1010         value = get_sel_from_list(GTK_LIST(filtering.action_list));
1011
1012         switch (value) {
1013         case ACTION_MOVE:
1014                 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1015                 gtk_widget_show(filtering.dest_entry);
1016                 gtk_widget_set_sensitive(filtering.dest_entry, TRUE);
1017                 gtk_widget_show(filtering.dest_btn);
1018                 gtk_widget_show(filtering.dest_label);
1019                 gtk_widget_hide(filtering.exec_label);
1020                 gtk_widget_hide(filtering.exec_btn);
1021                 gtk_widget_hide(filtering.color_optmenu);
1022                 gtk_widget_hide(filtering.color_label);
1023                 break;
1024         case ACTION_COPY:
1025                 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1026                 gtk_widget_show(filtering.dest_entry);
1027                 gtk_widget_set_sensitive(filtering.dest_entry, TRUE);
1028                 gtk_widget_show(filtering.dest_btn);
1029                 gtk_widget_show(filtering.dest_label);
1030                 gtk_widget_hide(filtering.exec_label);
1031                 gtk_widget_hide(filtering.exec_btn);
1032                 gtk_widget_hide(filtering.color_optmenu);
1033                 gtk_widget_hide(filtering.color_label);
1034                 break;
1035         case ACTION_DELETE:
1036                 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1037                 gtk_widget_show(filtering.dest_entry);
1038                 gtk_widget_set_sensitive(filtering.dest_entry, FALSE);
1039                 gtk_widget_hide(filtering.dest_btn);
1040                 gtk_widget_show(filtering.dest_label);
1041                 gtk_widget_hide(filtering.exec_label);
1042                 gtk_widget_hide(filtering.exec_btn);
1043                 gtk_widget_hide(filtering.color_optmenu);
1044                 gtk_widget_hide(filtering.color_label);
1045                 break;
1046         case ACTION_MARK:
1047                 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1048                 gtk_widget_show(filtering.dest_entry);
1049                 gtk_widget_set_sensitive(filtering.dest_entry, FALSE);
1050                 gtk_widget_hide(filtering.dest_btn);
1051                 gtk_widget_show(filtering.dest_label);
1052                 gtk_widget_hide(filtering.exec_label);
1053                 gtk_widget_hide(filtering.exec_btn);
1054                 gtk_widget_hide(filtering.color_optmenu);
1055                 gtk_widget_hide(filtering.color_label);
1056                 break;
1057         case ACTION_UNMARK:
1058                 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1059                 gtk_widget_show(filtering.dest_entry);
1060                 gtk_widget_set_sensitive(filtering.dest_entry, FALSE);
1061                 gtk_widget_hide(filtering.dest_btn);
1062                 gtk_widget_show(filtering.dest_label);
1063                 gtk_widget_hide(filtering.exec_label);
1064                 gtk_widget_hide(filtering.exec_btn);
1065                 gtk_widget_hide(filtering.color_optmenu);
1066                 gtk_widget_hide(filtering.color_label);
1067                 break;
1068         case ACTION_MARK_AS_READ:
1069                 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1070                 gtk_widget_show(filtering.dest_entry);
1071                 gtk_widget_set_sensitive(filtering.dest_entry, FALSE);
1072                 gtk_widget_hide(filtering.dest_btn);
1073                 gtk_widget_show(filtering.dest_label);
1074                 gtk_widget_hide(filtering.exec_label);
1075                 gtk_widget_hide(filtering.exec_btn);
1076                 gtk_widget_hide(filtering.color_optmenu);
1077                 gtk_widget_hide(filtering.color_label);
1078                 break;
1079         case ACTION_MARK_AS_UNREAD:
1080                 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1081                 gtk_widget_show(filtering.dest_entry);
1082                 gtk_widget_set_sensitive(filtering.dest_entry, FALSE);
1083                 gtk_widget_hide(filtering.dest_btn);
1084                 gtk_widget_show(filtering.dest_label);
1085                 gtk_widget_hide(filtering.exec_label);
1086                 gtk_widget_hide(filtering.exec_btn);
1087                 gtk_widget_hide(filtering.color_optmenu);
1088                 gtk_widget_hide(filtering.color_label);
1089                 break;
1090         case ACTION_FORWARD:
1091                 gtk_widget_set_sensitive(filtering.account_combo, TRUE);
1092                 gtk_widget_show(filtering.dest_entry);
1093                 gtk_widget_set_sensitive(filtering.dest_entry, TRUE);
1094                 gtk_widget_hide(filtering.dest_btn);
1095                 gtk_widget_show(filtering.dest_label);
1096                 gtk_widget_hide(filtering.exec_label);
1097                 gtk_widget_hide(filtering.exec_btn);
1098                 gtk_widget_hide(filtering.color_optmenu);
1099                 gtk_widget_hide(filtering.color_label);
1100                 break;
1101         case ACTION_FORWARD_AS_ATTACHMENT:
1102                 gtk_widget_set_sensitive(filtering.account_combo, TRUE);
1103                 gtk_widget_show(filtering.dest_entry);
1104                 gtk_widget_set_sensitive(filtering.dest_entry, TRUE);
1105                 gtk_widget_hide(filtering.dest_btn);
1106                 gtk_widget_show(filtering.dest_label);
1107                 gtk_widget_hide(filtering.exec_label);
1108                 gtk_widget_hide(filtering.exec_btn);
1109                 gtk_widget_hide(filtering.color_optmenu);
1110                 gtk_widget_hide(filtering.color_label);
1111                 break;
1112         case ACTION_EXECUTE:
1113                 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1114                 gtk_widget_show(filtering.dest_entry);
1115                 gtk_widget_set_sensitive(filtering.dest_entry, TRUE);
1116                 gtk_widget_hide(filtering.dest_btn);
1117                 gtk_widget_hide(filtering.dest_label);
1118                 gtk_widget_show(filtering.exec_label);
1119                 gtk_widget_set_sensitive(filtering.exec_btn, FALSE);
1120                 gtk_widget_show(filtering.exec_btn);
1121                 gtk_widget_hide(filtering.color_optmenu);
1122                 gtk_widget_hide(filtering.color_label);
1123                 break;
1124         case ACTION_COLOR:
1125                 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1126                 gtk_widget_hide(filtering.dest_entry);
1127                 gtk_widget_hide(filtering.dest_btn);
1128                 gtk_widget_hide(filtering.dest_label);
1129                 gtk_widget_hide(filtering.exec_label);
1130                 gtk_widget_show(filtering.exec_btn);
1131                 gtk_widget_set_sensitive(filtering.exec_btn, FALSE);
1132                 gtk_widget_show(filtering.color_optmenu);
1133                 gtk_widget_show(filtering.color_label);
1134                 break;
1135         }
1136 }
1137
1138 static gint prefs_filtering_deleted(GtkWidget *widget, GdkEventAny *event,
1139                                  gpointer data)
1140 {
1141         prefs_filtering_cancel();
1142         return TRUE;
1143 }
1144
1145 static void prefs_filtering_key_pressed(GtkWidget *widget, GdkEventKey *event,
1146                                      gpointer data)
1147 {
1148         if (event && event->keyval == GDK_Escape)
1149                 prefs_filtering_cancel();
1150 }
1151
1152 static void prefs_filtering_ok(void)
1153 {
1154         prefs_filtering_set_list();
1155         prefs_filtering_write_config();
1156         prefs_filtering_close();
1157 }
1158
1159 static void prefs_filtering_cancel(void)
1160 {
1161         prefs_filtering_read_config();
1162         prefs_filtering_close();
1163 }