sync with sylpheed 0.7.0cvs38
[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[] = {_("Registered rules")};
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         cond_clist = gtk_clist_new_with_titles(1, title);
546         gtk_widget_show (cond_clist);
547         gtk_container_add (GTK_CONTAINER (cond_scrolledwin), cond_clist);
548         gtk_clist_set_column_width (GTK_CLIST (cond_clist), 0, 80);
549         gtk_clist_set_selection_mode (GTK_CLIST (cond_clist),
550                                       GTK_SELECTION_BROWSE);
551         GTK_WIDGET_UNSET_FLAGS (GTK_CLIST (cond_clist)->column[0].button,
552                                 GTK_CAN_FOCUS);
553         gtk_signal_connect (GTK_OBJECT (cond_clist), "select_row",
554                             GTK_SIGNAL_FUNC (prefs_filtering_select), NULL);
555
556         btn_vbox = gtk_vbox_new (FALSE, 8);
557         gtk_widget_show (btn_vbox);
558         gtk_box_pack_start (GTK_BOX (cond_hbox), btn_vbox, FALSE, FALSE, 0);
559
560         up_btn = gtk_button_new_with_label (_("Up"));
561         gtk_widget_show (up_btn);
562         gtk_box_pack_start (GTK_BOX (btn_vbox), up_btn, FALSE, FALSE, 0);
563         gtk_signal_connect (GTK_OBJECT (up_btn), "clicked",
564                             GTK_SIGNAL_FUNC (prefs_filtering_up), NULL);
565
566         down_btn = gtk_button_new_with_label (_("Down"));
567         gtk_widget_show (down_btn);
568         gtk_box_pack_start (GTK_BOX (btn_vbox), down_btn, FALSE, FALSE, 0);
569         gtk_signal_connect (GTK_OBJECT (down_btn), "clicked",
570                             GTK_SIGNAL_FUNC (prefs_filtering_down), NULL);
571
572         gtk_widget_set_usize(window, 500, -1);
573
574         gtk_widget_show_all(window);
575
576         filtering.window    = window;
577         filtering.ok_btn = ok_btn;
578
579         filtering.cond_entry = cond_entry;
580         filtering.action_list = action_list;
581         filtering.action_combo = action_combo;
582         filtering.account_list = account_list;
583         filtering.account_combo = account_combo;
584         filtering.dest_entry = dest_entry;
585         filtering.dest_btn = dest_btn;
586         filtering.dest_label = dest_label;
587         filtering.exec_label = exec_label;
588         filtering.exec_btn = exec_btn;
589
590         filtering.cond_clist   = cond_clist;
591
592         filtering.color_label   = color_label;
593         filtering.color_optmenu = color_optmenu;
594 }
595
596 static void prefs_filtering_update_hscrollbar(void)
597 {
598         gint optwidth = gtk_clist_optimal_column_width(GTK_CLIST(filtering.cond_clist), 0);
599         gtk_clist_set_column_width(GTK_CLIST(filtering.cond_clist), 0, optwidth);
600 }
601
602 static void prefs_filtering_set_dialog(void)
603 {
604         GtkCList *clist = GTK_CLIST(filtering.cond_clist);
605         GSList *cur;
606         GSList * prefs_filtering;
607         
608         gtk_clist_freeze(clist);
609         gtk_clist_clear(clist);
610
611         prefs_filtering_clist_set_row(-1, NULL);
612
613         if (cur_item == NULL)
614                 prefs_filtering = global_processing;
615         else
616                 prefs_filtering = cur_item->prefs->processing;
617
618         for(cur = prefs_filtering ; cur != NULL ; cur = g_slist_next(cur)) {
619                 FilteringProp * prop = (FilteringProp *) cur->data;
620
621                 prefs_filtering_clist_set_row(-1, prop);
622         }
623
624         prefs_filtering_update_hscrollbar();
625         gtk_clist_thaw(clist);
626
627         prefs_filtering_reset_dialog();
628 }
629
630 static void prefs_filtering_reset_dialog(void)
631 {
632         gtk_list_select_item(GTK_LIST(filtering.action_list), 0);
633         gtk_list_select_item(GTK_LIST(filtering.account_list), 0);
634         gtk_entry_set_text(GTK_ENTRY(filtering.dest_entry), "");
635         gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), "");
636 }
637
638 static void prefs_filtering_set_list(void)
639 {
640         gint row = 1;
641         FilteringProp *prop;
642         GSList * cur;
643         gchar * filtering_str;
644         gchar * tmp;
645         GSList * prefs_filtering;
646
647         if (cur_item == NULL)
648                 prefs_filtering = global_processing;
649         else
650                 prefs_filtering = cur_item->prefs->processing;
651
652         for(cur = prefs_filtering ; cur != NULL ; cur = g_slist_next(cur))
653                 filteringprop_free((FilteringProp *) cur->data);
654         g_slist_free(prefs_filtering);
655         prefs_filtering = NULL;
656
657         while (gtk_clist_get_text(GTK_CLIST(filtering.cond_clist),
658                                   row, 0, &filtering_str)) {
659                 if (strcmp(filtering_str, _("(New)")) != 0) {
660                         /* tmp = filtering_str; */
661                         prop = matcher_parser_get_filtering(filtering_str);
662                         if (prop != NULL)
663                                 prefs_filtering =
664                                         g_slist_append(prefs_filtering, prop);
665                 }
666                 row++;
667         }
668
669         if (cur_item == NULL)
670                 global_processing = prefs_filtering;
671         else
672                 cur_item->prefs->processing = prefs_filtering;
673 }
674
675 static gint prefs_filtering_clist_set_row(gint row, FilteringProp * prop)
676 {
677         GtkCList *clist = GTK_CLIST(filtering.cond_clist);
678         gchar * str;
679         gchar *cond_str[1];
680
681         if (prop == NULL) {
682                 cond_str[0] = _("(New)");
683                 return gtk_clist_append(clist, cond_str);
684         }
685
686         str = filteringprop_to_string(prop);
687         if (str == NULL) {
688                 return -1;
689         }
690         cond_str[0] = str;
691
692         if (row < 0)
693                 row = gtk_clist_append(clist, cond_str);
694         else
695                 gtk_clist_set_text(clist, row, 0, cond_str[0]);
696         g_free(str);
697
698         return row;
699 }
700
701 static void prefs_filtering_condition_define_done(MatcherList * matchers)
702 {
703         gchar * str;
704
705         if (matchers == NULL)
706                 return;
707
708         str = matcherlist_to_string(matchers);
709
710         if (str != NULL) {
711                 gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), str);
712                 g_free(str);
713         }
714 }
715
716 static void prefs_filtering_condition_define(void)
717 {
718         gchar * cond_str;
719         MatcherList * matchers = NULL;
720
721         cond_str = gtk_entry_get_text(GTK_ENTRY(filtering.cond_entry));
722
723         if (*cond_str != '\0') {
724                 gchar * tmp;
725                 
726                 /* tmp = cond_str; */
727                 matchers = matcher_parser_get_cond(cond_str);
728                 if (matchers == NULL)
729                         alertpanel_error(_("Match string is not valid."));
730         }
731
732         prefs_matcher_open(matchers, prefs_filtering_condition_define_done);
733
734         if (matchers != NULL)
735                 matcherlist_free(matchers);
736 }
737
738
739 /* register / substitute delete buttons */
740
741
742 static FilteringProp * prefs_filtering_dialog_to_filtering(void)
743 {
744         MatcherList * cond;
745         gchar * cond_str;
746         FilteringProp * prop;
747         FilteringAction * action;
748         gchar * tmp;
749         gint list_id;
750         gint action_id;
751         gint action_type;
752         gint account_id;
753         gchar * destination;
754         gint labelcolor;
755         
756         cond_str = gtk_entry_get_text(GTK_ENTRY(filtering.cond_entry));
757         if (*cond_str == '\0') {
758                 alertpanel_error(_("Score is not set."));
759                 return NULL;
760         }
761
762         action_id = get_sel_from_list(GTK_LIST(filtering.action_list));
763         action_type = prefs_filtering_get_matching_from_action(action_id);
764         list_id = get_sel_from_list(GTK_LIST(filtering.account_list));
765         account_id = get_account_id_from_list_id(list_id);
766
767         switch (action_id) {
768         case ACTION_MOVE:
769         case ACTION_COPY:
770         case ACTION_FORWARD:
771         case ACTION_FORWARD_AS_ATTACHMENT:
772         case ACTION_BOUNCE:
773         case ACTION_EXECUTE:
774                 destination = gtk_entry_get_text(GTK_ENTRY(filtering.dest_entry));
775                 if (*destination == '\0') {
776                         alertpanel_error(_("Destination is not set."));
777                         return NULL;
778                 }
779                 break;
780         case ACTION_COLOR:
781                 labelcolor = colorlabel_get_color_menu_active_item(
782                         gtk_option_menu_get_menu(GTK_OPTION_MENU(filtering.color_optmenu)));
783                 destination = NULL;     
784                 break;
785         default:
786                 destination = NULL;
787                 break;
788         }
789         
790         action = filteringaction_new(action_type, account_id, destination, labelcolor);
791
792         cond = matcher_parser_get_cond(cond_str);
793
794         if (cond == NULL) {
795                 alertpanel_error(_("Match string is not valid."));
796                 filteringaction_free(action);
797                 return NULL;
798         }
799
800         prop = filteringprop_new(cond, action);
801
802         return prop;
803 }
804
805 static void prefs_filtering_register_cb(void)
806 {
807         FilteringProp * prop;
808         
809         prop = prefs_filtering_dialog_to_filtering();
810         if (prop == NULL)
811                 return;
812         prefs_filtering_clist_set_row(-1, prop);
813
814         filteringprop_free(prop);
815         
816         prefs_filtering_update_hscrollbar();
817 }
818
819 static void prefs_filtering_substitute_cb(void)
820 {
821         GtkCList *clist = GTK_CLIST(filtering.cond_clist);
822         gint row;
823         FilteringProp * prop;
824         
825         if (!clist->selection) return;
826
827         row = GPOINTER_TO_INT(clist->selection->data);
828         if (row == 0) return;
829
830         prop = prefs_filtering_dialog_to_filtering();
831         if (prop == NULL)
832                 return;
833         prefs_filtering_clist_set_row(row, prop);
834
835         filteringprop_free(prop);
836         
837         prefs_filtering_update_hscrollbar();
838 }
839
840 static void prefs_filtering_delete_cb(void)
841 {
842         GtkCList *clist = GTK_CLIST(filtering.cond_clist);
843         gint row;
844
845         if (!clist->selection) return;
846         row = GPOINTER_TO_INT(clist->selection->data);
847         if (row == 0) return;
848
849         if (alertpanel(_("Delete rule"),
850                        _("Do you really want to delete this rule?"),
851                        _("Yes"), _("No"), NULL) == G_ALERTALTERNATE)
852                 return;
853
854         gtk_clist_remove(clist, row);
855
856         prefs_filtering_update_hscrollbar();
857 }
858
859 static void prefs_filtering_up(void)
860 {
861         GtkCList *clist = GTK_CLIST(filtering.cond_clist);
862         gint row;
863
864         if (!clist->selection) return;
865
866         row = GPOINTER_TO_INT(clist->selection->data);
867         if (row > 1) {
868                 gtk_clist_row_move(clist, row, row - 1);
869                 if(gtk_clist_row_is_visible(clist, row - 1) != GTK_VISIBILITY_FULL) {
870                         gtk_clist_moveto(clist, row - 1, 0, 0, 0);
871                 } 
872         }
873 }
874
875 static void prefs_filtering_down(void)
876 {
877         GtkCList *clist = GTK_CLIST(filtering.cond_clist);
878         gint row;
879
880         if (!clist->selection) return;
881
882         row = GPOINTER_TO_INT(clist->selection->data);
883         if (row > 0 && row < clist->rows - 1) {
884                 gtk_clist_row_move(clist, row, row + 1);
885                 if(gtk_clist_row_is_visible(clist, row + 1) != GTK_VISIBILITY_FULL) {
886                         gtk_clist_moveto(clist, row + 1, 0, 1, 0);
887                 } 
888         }
889 }
890
891 static void prefs_filtering_select_set(FilteringProp * prop)
892 {
893         FilteringAction * action;
894         gchar * matcher_str;
895         gint list_id;
896
897         prefs_filtering_reset_dialog();
898
899         action = prop->action;
900
901         matcher_str = matcherlist_to_string(prop->matchers);
902         if (matcher_str == NULL) {
903                 filteringprop_free(prop);
904                 return;
905         }
906
907         gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), matcher_str);
908         
909         if (action->destination)
910                 gtk_entry_set_text(GTK_ENTRY(filtering.dest_entry), action->destination);
911         else
912                 gtk_entry_set_text(GTK_ENTRY(filtering.dest_entry), "");
913
914         switch(action->type) {
915         case MATCHACTION_MOVE:
916                 gtk_list_select_item(GTK_LIST(filtering.action_list),
917                                      ACTION_MOVE);
918                 break;
919         case MATCHACTION_COPY:
920                 gtk_list_select_item(GTK_LIST(filtering.action_list),
921                                      ACTION_COPY);
922                 break;
923         case MATCHACTION_DELETE:
924                 gtk_list_select_item(GTK_LIST(filtering.action_list),
925                                      ACTION_DELETE);
926                 break;
927         case MATCHACTION_MARK:
928                 gtk_list_select_item(GTK_LIST(filtering.action_list),
929                                      ACTION_MARK);
930                 break;
931         case MATCHACTION_UNMARK:
932                 gtk_list_select_item(GTK_LIST(filtering.action_list),
933                                      ACTION_UNMARK);
934                 break;
935         case MATCHACTION_MARK_AS_READ:
936                 gtk_list_select_item(GTK_LIST(filtering.action_list),
937                                      ACTION_MARK_AS_READ);
938                 break;
939         case MATCHACTION_MARK_AS_UNREAD:
940                 gtk_list_select_item(GTK_LIST(filtering.action_list),
941                                      ACTION_MARK_AS_UNREAD);
942                 break;
943         case MATCHACTION_FORWARD:
944                 list_id = get_list_id_from_account_id(action->account_id);
945                 gtk_list_select_item(GTK_LIST(filtering.action_list),
946                                      ACTION_FORWARD);
947                 gtk_list_select_item(GTK_LIST(filtering.account_list),
948                                      list_id);
949                 break;
950         case MATCHACTION_FORWARD_AS_ATTACHMENT:
951                 list_id = get_list_id_from_account_id(action->account_id);
952                 gtk_list_select_item(GTK_LIST(filtering.action_list),
953                                      ACTION_FORWARD_AS_ATTACHMENT);
954                 gtk_list_select_item(GTK_LIST(filtering.account_list),
955                                      list_id);
956                 break;
957         case MATCHACTION_BOUNCE:
958                 list_id = get_list_id_from_account_id(action->account_id);
959                 gtk_list_select_item(GTK_LIST(filtering.action_list),
960                                      ACTION_BOUNCE);
961                 gtk_list_select_item(GTK_LIST(filtering.account_list),
962                                      list_id);
963                 break;
964         case MATCHACTION_EXECUTE:
965                 gtk_list_select_item(GTK_LIST(filtering.action_list),
966                                      ACTION_EXECUTE);
967                 break;
968         case MATCHACTION_COLOR:
969                 gtk_list_select_item(GTK_LIST(filtering.action_list),
970                                      ACTION_COLOR);
971                 gtk_option_menu_set_history(GTK_OPTION_MENU(filtering.color_optmenu), action->labelcolor);     
972                 break;
973         }
974
975         g_free(matcher_str);
976 }
977
978 static void prefs_filtering_select(GtkCList *clist, gint row, gint column,
979                                 GdkEvent *event)
980 {
981         FilteringProp * prop;
982         gchar * tmp;
983         gchar * filtering_str;
984
985         if (row == 0) {
986                 prefs_filtering_reset_dialog();
987                 return;
988         }
989
990         if (!gtk_clist_get_text(GTK_CLIST(filtering.cond_clist),
991                                 row, 0, &filtering_str))
992                 return;
993         
994         prop = matcher_parser_get_filtering(filtering_str);
995         if (prop == NULL)
996                 return;
997
998         prefs_filtering_select_set(prop);
999
1000         filteringprop_free(prop);
1001 }
1002
1003 static void prefs_filtering_select_dest(void)
1004 {
1005         FolderItem *dest;
1006         gchar * path;
1007
1008         dest = foldersel_folder_sel(NULL, FOLDER_SEL_COPY, NULL);
1009         if (!dest) return;
1010
1011         path = folder_item_get_identifier(dest);
1012
1013         gtk_entry_set_text(GTK_ENTRY(filtering.dest_entry), path);
1014         g_free(path);
1015 }
1016
1017 static void prefs_filtering_action_selection_changed(GtkList *list,
1018                                                      gpointer user_data)
1019 {
1020         gint value;
1021
1022         value = get_sel_from_list(GTK_LIST(filtering.action_list));
1023
1024         if (filtering.current_action != value) {
1025                 if (filtering.current_action == ACTION_FORWARD 
1026                 ||  filtering.current_action == ACTION_FORWARD_AS_ATTACHMENT) {
1027                         debug_print("unregistering address completion entry\n");
1028                         address_completion_unregister_entry(GTK_ENTRY(filtering.dest_entry));
1029                 }
1030                 if (value == ACTION_FORWARD || value == ACTION_FORWARD_AS_ATTACHMENT) {
1031                         debug_print("registering address completion entry\n");
1032                         address_completion_register_entry(GTK_ENTRY(filtering.dest_entry));
1033                 }
1034                 filtering.current_action = value;
1035         }
1036 }
1037
1038 static void prefs_filtering_action_select(GtkList *list,
1039                                           GtkWidget *widget,
1040                                           gpointer user_data)
1041 {
1042         gint value;
1043
1044         value = get_sel_from_list(GTK_LIST(filtering.action_list));
1045
1046         switch (value) {
1047         case ACTION_MOVE:
1048                 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1049                 gtk_widget_show(filtering.dest_entry);
1050                 gtk_widget_set_sensitive(filtering.dest_entry, TRUE);
1051                 gtk_widget_show(filtering.dest_btn);
1052                 gtk_widget_show(filtering.dest_label);
1053                 gtk_widget_hide(filtering.exec_label);
1054                 gtk_widget_hide(filtering.exec_btn);
1055                 gtk_widget_hide(filtering.color_optmenu);
1056                 gtk_widget_hide(filtering.color_label);
1057                 break;
1058         case ACTION_COPY:
1059                 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1060                 gtk_widget_show(filtering.dest_entry);
1061                 gtk_widget_set_sensitive(filtering.dest_entry, TRUE);
1062                 gtk_widget_show(filtering.dest_btn);
1063                 gtk_widget_show(filtering.dest_label);
1064                 gtk_widget_hide(filtering.exec_label);
1065                 gtk_widget_hide(filtering.exec_btn);
1066                 gtk_widget_hide(filtering.color_optmenu);
1067                 gtk_widget_hide(filtering.color_label);
1068                 break;
1069         case ACTION_DELETE:
1070                 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1071                 gtk_widget_show(filtering.dest_entry);
1072                 gtk_widget_set_sensitive(filtering.dest_entry, FALSE);
1073                 gtk_widget_hide(filtering.dest_btn);
1074                 gtk_widget_show(filtering.dest_label);
1075                 gtk_widget_hide(filtering.exec_label);
1076                 gtk_widget_hide(filtering.exec_btn);
1077                 gtk_widget_hide(filtering.color_optmenu);
1078                 gtk_widget_hide(filtering.color_label);
1079                 break;
1080         case ACTION_MARK:
1081                 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1082                 gtk_widget_show(filtering.dest_entry);
1083                 gtk_widget_set_sensitive(filtering.dest_entry, FALSE);
1084                 gtk_widget_hide(filtering.dest_btn);
1085                 gtk_widget_show(filtering.dest_label);
1086                 gtk_widget_hide(filtering.exec_label);
1087                 gtk_widget_hide(filtering.exec_btn);
1088                 gtk_widget_hide(filtering.color_optmenu);
1089                 gtk_widget_hide(filtering.color_label);
1090                 break;
1091         case ACTION_UNMARK:
1092                 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1093                 gtk_widget_show(filtering.dest_entry);
1094                 gtk_widget_set_sensitive(filtering.dest_entry, FALSE);
1095                 gtk_widget_hide(filtering.dest_btn);
1096                 gtk_widget_show(filtering.dest_label);
1097                 gtk_widget_hide(filtering.exec_label);
1098                 gtk_widget_hide(filtering.exec_btn);
1099                 gtk_widget_hide(filtering.color_optmenu);
1100                 gtk_widget_hide(filtering.color_label);
1101                 break;
1102         case ACTION_MARK_AS_READ:
1103                 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1104                 gtk_widget_show(filtering.dest_entry);
1105                 gtk_widget_set_sensitive(filtering.dest_entry, FALSE);
1106                 gtk_widget_hide(filtering.dest_btn);
1107                 gtk_widget_show(filtering.dest_label);
1108                 gtk_widget_hide(filtering.exec_label);
1109                 gtk_widget_hide(filtering.exec_btn);
1110                 gtk_widget_hide(filtering.color_optmenu);
1111                 gtk_widget_hide(filtering.color_label);
1112                 break;
1113         case ACTION_MARK_AS_UNREAD:
1114                 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1115                 gtk_widget_show(filtering.dest_entry);
1116                 gtk_widget_set_sensitive(filtering.dest_entry, FALSE);
1117                 gtk_widget_hide(filtering.dest_btn);
1118                 gtk_widget_show(filtering.dest_label);
1119                 gtk_widget_hide(filtering.exec_label);
1120                 gtk_widget_hide(filtering.exec_btn);
1121                 gtk_widget_hide(filtering.color_optmenu);
1122                 gtk_widget_hide(filtering.color_label);
1123                 break;
1124         case ACTION_FORWARD:
1125                 gtk_widget_set_sensitive(filtering.account_combo, TRUE);
1126                 gtk_widget_show(filtering.dest_entry);
1127                 gtk_widget_set_sensitive(filtering.dest_entry, TRUE);
1128                 gtk_widget_hide(filtering.dest_btn);
1129                 gtk_widget_show(filtering.dest_label);
1130                 gtk_widget_hide(filtering.exec_label);
1131                 gtk_widget_hide(filtering.exec_btn);
1132                 gtk_widget_hide(filtering.color_optmenu);
1133                 gtk_widget_hide(filtering.color_label);
1134                 break;
1135         case ACTION_FORWARD_AS_ATTACHMENT:
1136                 gtk_widget_set_sensitive(filtering.account_combo, TRUE);
1137                 gtk_widget_show(filtering.dest_entry);
1138                 gtk_widget_set_sensitive(filtering.dest_entry, TRUE);
1139                 gtk_widget_hide(filtering.dest_btn);
1140                 gtk_widget_show(filtering.dest_label);
1141                 gtk_widget_hide(filtering.exec_label);
1142                 gtk_widget_hide(filtering.exec_btn);
1143                 gtk_widget_hide(filtering.color_optmenu);
1144                 gtk_widget_hide(filtering.color_label);
1145                 break;
1146         case ACTION_BOUNCE:
1147                 gtk_widget_set_sensitive(filtering.account_combo, TRUE);
1148                 gtk_widget_show(filtering.dest_entry);
1149                 gtk_widget_set_sensitive(filtering.dest_entry, TRUE);
1150                 gtk_widget_hide(filtering.dest_btn);
1151                 gtk_widget_show(filtering.dest_label);
1152                 gtk_widget_hide(filtering.exec_label);
1153                 gtk_widget_hide(filtering.exec_btn);
1154                 gtk_widget_hide(filtering.color_optmenu);
1155                 gtk_widget_hide(filtering.color_label);
1156                 break;
1157         case ACTION_EXECUTE:
1158                 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1159                 gtk_widget_show(filtering.dest_entry);
1160                 gtk_widget_set_sensitive(filtering.dest_entry, TRUE);
1161                 gtk_widget_hide(filtering.dest_btn);
1162                 gtk_widget_hide(filtering.dest_label);
1163                 gtk_widget_show(filtering.exec_label);
1164                 gtk_widget_set_sensitive(filtering.exec_btn, TRUE);
1165                 gtk_widget_show(filtering.exec_btn);
1166                 gtk_widget_hide(filtering.color_optmenu);
1167                 gtk_widget_hide(filtering.color_label);
1168                 break;
1169         case ACTION_COLOR:
1170                 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1171                 gtk_widget_hide(filtering.dest_entry);
1172                 gtk_widget_hide(filtering.dest_btn);
1173                 gtk_widget_hide(filtering.dest_label);
1174                 gtk_widget_hide(filtering.exec_label);
1175                 gtk_widget_show(filtering.exec_btn);
1176                 gtk_widget_set_sensitive(filtering.exec_btn, FALSE);
1177                 gtk_widget_show(filtering.color_optmenu);
1178                 gtk_widget_show(filtering.color_label);
1179                 break;
1180         }
1181 }
1182
1183 static gint prefs_filtering_deleted(GtkWidget *widget, GdkEventAny *event,
1184                                  gpointer data)
1185 {
1186         prefs_filtering_cancel();
1187         return TRUE;
1188 }
1189
1190 static void prefs_filtering_key_pressed(GtkWidget *widget, GdkEventKey *event,
1191                                      gpointer data)
1192 {
1193         if (event && event->keyval == GDK_Escape)
1194                 prefs_filtering_cancel();
1195 }
1196
1197 static void prefs_filtering_ok(void)
1198 {
1199         prefs_filtering_set_list();
1200         prefs_matcher_write_config();
1201         prefs_filtering_close();
1202 }
1203
1204 static void prefs_filtering_cancel(void)
1205 {
1206         prefs_matcher_read_config();
1207         prefs_filtering_close();
1208 }