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