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