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