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