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