2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2003 Hiroyuki Yamamoto and the Sylpheed-Claws team
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.
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.
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.
28 #include <gtk/gtkoptionmenu.h>
29 #include <gdk/gdkkeysyms.h>
37 #include "prefs_gtk.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"
47 #include "alertpanel.h"
49 #include "filtering.h"
50 #include "addr_compl.h"
51 #include "colorlabel.h"
53 #include "matcher_parser.h"
56 static struct Filtering {
60 GtkWidget *cond_entry;
61 GtkWidget *action_list;
62 GtkWidget *action_combo;
63 GtkWidget *account_label;
64 GtkWidget *account_list;
65 GtkWidget *account_combo;
66 GtkWidget *dest_entry;
68 GtkWidget *dest_label;
69 GtkWidget *exec_label;
72 GtkWidget *color_label;
73 GtkWidget *color_optmenu;
75 GtkWidget *cond_clist;
77 /* need this to make address completion entry work */
81 /* widget creating functions */
82 static void prefs_filtering_create (void);
84 static void prefs_filtering_set_dialog (const gchar *header,
86 static void prefs_filtering_set_list (void);
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,
100 static gint prefs_filtering_deleted (GtkWidget *widget,
103 static void prefs_filtering_key_pressed (GtkWidget *widget,
106 static void prefs_filtering_cancel (void);
107 static void prefs_filtering_ok (void);
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,
115 static void prefs_filtering_action_selection_changed(GtkList *list,
118 static void prefs_filtering_reset_dialog(void);
119 static gboolean prefs_filtering_rename_path_func(GNode *node, gpointer data);
120 static gboolean prefs_filtering_delete_path_func(GNode *node, gpointer data);
122 static FolderItem * cur_item = NULL;
130 ACTION_MARK_AS_READ = 5,
131 ACTION_MARK_AS_UNREAD = 6,
133 ACTION_FORWARD_AS_ATTACHMENT = 8,
139 static gint get_sel_from_list(GtkList * list)
145 if (list->selection == NULL)
148 sel = list->selection->data;
149 for(child = list->children ; child != NULL ;
150 child = g_list_next(child)) {
151 if (child->data == sel)
159 static gint get_account_id_from_list_id(gint list_id)
163 for (accounts = account_get_list() ; accounts != NULL;
164 accounts = accounts->next) {
165 PrefsAccount *ac = (PrefsAccount *)accounts->data;
168 return ac->account_id;
174 static gint get_list_id_from_account_id(gint account_id)
179 for (accounts = account_get_list() ; accounts != NULL;
180 accounts = accounts->next) {
181 PrefsAccount *ac = (PrefsAccount *)accounts->data;
183 if (account_id == ac->account_id)
190 static gint prefs_filtering_get_matching_from_action(gint action_id)
194 return MATCHACTION_MOVE;
196 return MATCHACTION_COPY;
198 return MATCHACTION_DELETE;
200 return MATCHACTION_MARK;
202 return MATCHACTION_UNMARK;
203 case ACTION_MARK_AS_READ:
204 return MATCHACTION_MARK_AS_READ;
205 case ACTION_MARK_AS_UNREAD:
206 return MATCHACTION_MARK_AS_UNREAD;
208 return MATCHACTION_FORWARD;
209 case ACTION_FORWARD_AS_ATTACHMENT:
210 return MATCHACTION_FORWARD_AS_ATTACHMENT;
211 case ACTION_REDIRECT:
212 return MATCHACTION_REDIRECT;
214 return MATCHACTION_EXECUTE;
216 return MATCHACTION_COLOR;
222 static gchar * action_text [] = {
229 N_("Mark as unread"),
231 N_("Forward as attachment"),
237 void prefs_filtering_open(FolderItem * item,
243 if (prefs_rc_is_readonly(FILTERING_RC))
248 if (!filtering.window) {
249 prefs_filtering_create();
252 manage_window_set_transient(GTK_WINDOW(filtering.window));
253 gtk_widget_grab_focus(filtering.ok_btn);
257 esckey = matcher_escape_str(key);
258 prefs_filtering_set_dialog(header, esckey);
261 gtk_widget_show(filtering.window);
263 start_address_completion();
266 /* prefs_filtering_close() - just to have one common exit point */
267 static void prefs_filtering_close(void)
269 end_address_completion();
271 gtk_widget_hide(filtering.window);
275 static void prefs_filtering_create(void)
280 GtkWidget *cancel_btn;
281 GtkWidget *confirm_area;
289 GtkWidget *cond_label;
290 GtkWidget *cond_entry;
292 GtkWidget *action_label;
293 GtkWidget *action_list;
294 GtkWidget *action_combo;
295 GtkWidget *account_label;
296 GtkWidget *account_list;
297 GtkWidget *account_combo;
298 GtkWidget *dest_label;
299 GtkWidget *exec_label;
300 GtkWidget *dest_entry;
303 GtkWidget *color_label;
304 GtkWidget *color_optmenu;
307 GtkWidget *subst_btn;
310 GtkWidget *cond_hbox;
311 GtkWidget *cond_scrolledwin;
312 GtkWidget *cond_clist;
327 debug_print("Creating filtering configuration window...\n");
329 window = gtk_window_new (GTK_WINDOW_DIALOG);
330 gtk_container_set_border_width (GTK_CONTAINER (window), 8);
331 gtk_window_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
332 gtk_window_set_modal (GTK_WINDOW (window), TRUE);
333 gtk_window_set_policy (GTK_WINDOW (window), FALSE, TRUE, FALSE);
335 vbox = gtk_vbox_new (FALSE, 6);
336 gtk_widget_show (vbox);
337 gtk_container_add (GTK_CONTAINER (window), vbox);
339 gtkut_button_set_create(&confirm_area, &ok_btn, _("OK"),
340 &cancel_btn, _("Cancel"), NULL, NULL);
341 gtk_widget_show (confirm_area);
342 gtk_box_pack_end (GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
343 gtk_widget_grab_default (ok_btn);
345 gtk_window_set_title (GTK_WINDOW(window),
346 _("Filtering/Processing configuration"));
348 gtk_signal_connect (GTK_OBJECT(window), "delete_event",
349 GTK_SIGNAL_FUNC(prefs_filtering_deleted), NULL);
350 gtk_signal_connect (GTK_OBJECT(window), "key_press_event",
351 GTK_SIGNAL_FUNC(prefs_filtering_key_pressed), NULL);
352 MANAGE_WINDOW_SIGNALS_CONNECT (window);
353 gtk_signal_connect (GTK_OBJECT(ok_btn), "clicked",
354 GTK_SIGNAL_FUNC(prefs_filtering_ok), NULL);
355 gtk_signal_connect (GTK_OBJECT(cancel_btn), "clicked",
356 GTK_SIGNAL_FUNC(prefs_filtering_cancel), NULL);
358 vbox1 = gtk_vbox_new (FALSE, VSPACING);
359 gtk_widget_show (vbox1);
360 gtk_box_pack_start (GTK_BOX (vbox), vbox1, TRUE, TRUE, 0);
361 gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
363 cond_label = gtk_label_new (_("Condition"));
364 gtk_widget_show (cond_label);
365 gtk_misc_set_alignment (GTK_MISC (cond_label), 0, 0.5);
366 gtk_box_pack_start (GTK_BOX (vbox1), cond_label, FALSE, FALSE, 0);
368 hbox1 = gtk_hbox_new (FALSE, VSPACING);
369 gtk_widget_show (vbox1);
370 gtk_box_pack_start (GTK_BOX (vbox1), hbox1, FALSE, FALSE, 0);
371 gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
373 cond_entry = gtk_entry_new ();
374 gtk_widget_show (cond_entry);
375 gtk_box_pack_start (GTK_BOX (hbox1), cond_entry, TRUE, TRUE, 0);
377 cond_btn = gtk_button_new_with_label (_("Define ..."));
378 gtk_widget_show (cond_btn);
379 gtk_box_pack_start (GTK_BOX (hbox1), cond_btn, FALSE, FALSE, 0);
380 gtk_signal_connect (GTK_OBJECT (cond_btn), "clicked",
381 GTK_SIGNAL_FUNC (prefs_filtering_condition_define),
384 hbox1 = gtk_hbox_new (FALSE, VSPACING);
385 gtk_widget_show (vbox1);
386 gtk_box_pack_start (GTK_BOX (vbox1), hbox1, FALSE, FALSE, 0);
387 gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
389 action_label = gtk_label_new (_("Action"));
390 gtk_widget_show (action_label);
391 gtk_misc_set_alignment (GTK_MISC (action_label), 0, 0.5);
392 gtk_box_pack_start (GTK_BOX (hbox1), action_label, FALSE, FALSE, 0);
394 action_combo = gtk_combo_new ();
395 gtk_widget_show (action_combo);
396 gtk_entry_set_editable(GTK_ENTRY(GTK_COMBO(action_combo)->entry),
401 for(i = 0 ; i < (gint) (sizeof(action_text) / sizeof(gchar *)) ;
403 combo_items = g_list_append(combo_items,
404 (gpointer) _(action_text[i]));
406 gtk_combo_set_popdown_strings(GTK_COMBO(action_combo), combo_items);
408 g_list_free(combo_items);
410 gtk_box_pack_start (GTK_BOX (hbox1), action_combo,
412 action_list = GTK_COMBO(action_combo)->list;
413 gtk_signal_connect (GTK_OBJECT (action_list), "select-child",
414 GTK_SIGNAL_FUNC (prefs_filtering_action_select),
417 gtk_signal_connect(GTK_OBJECT(action_list), "selection-changed",
418 GTK_SIGNAL_FUNC(prefs_filtering_action_selection_changed),
423 hbox1 = gtk_hbox_new (FALSE, VSPACING);
424 gtk_widget_show (vbox1);
425 gtk_box_pack_start (GTK_BOX (vbox1), hbox1, FALSE, FALSE, 0);
426 gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
428 account_label = gtk_label_new (_("Account"));
429 gtk_widget_show (account_label);
430 gtk_misc_set_alignment (GTK_MISC (account_label), 0, 0.5);
431 gtk_box_pack_start (GTK_BOX (hbox1), account_label, FALSE, FALSE, 0);
433 account_combo = gtk_combo_new ();
434 gtk_widget_show (account_combo);
437 for (accounts = account_get_list() ; accounts != NULL;
438 accounts = accounts->next) {
439 PrefsAccount *ac = (PrefsAccount *)accounts->data;
442 name = g_strdup_printf("%s <%s> (%s)",
443 ac->name, ac->address,
445 combo_items = g_list_append(combo_items, (gpointer) name);
448 gtk_combo_set_popdown_strings(GTK_COMBO(account_combo), combo_items);
450 for(cur = g_list_first(combo_items) ; cur != NULL ;
451 cur = g_list_next(cur))
453 g_list_free(combo_items);
455 gtk_box_pack_start (GTK_BOX (hbox1), account_combo,
457 account_list = GTK_COMBO(account_combo)->list;
458 gtk_entry_set_editable(GTK_ENTRY(GTK_COMBO(account_combo)->entry),
463 hbox1 = gtk_hbox_new (FALSE, VSPACING);
464 gtk_widget_show (vbox1);
465 gtk_box_pack_start (GTK_BOX (vbox1), hbox1, FALSE, FALSE, 0);
466 gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
468 dest_label = gtk_label_new (_("Destination"));
469 gtk_widget_show (dest_label);
470 gtk_misc_set_alignment (GTK_MISC (dest_label), 0, 0.5);
471 gtk_box_pack_start (GTK_BOX (hbox1), dest_label, FALSE, FALSE, 0);
473 exec_label = gtk_label_new (_("Execute"));
474 gtk_widget_show (exec_label);
475 gtk_misc_set_alignment (GTK_MISC (exec_label), 0, 0.5);
476 gtk_box_pack_start (GTK_BOX (hbox1), exec_label, FALSE, FALSE, 0);
478 color_label = gtk_label_new (_("Color"));
479 gtk_widget_show(color_label);
480 gtk_misc_set_alignment(GTK_MISC(color_label), 0, 0.5);
481 gtk_box_pack_start(GTK_BOX(hbox1), color_label, FALSE, FALSE, 0);
483 dest_entry = gtk_entry_new ();
484 gtk_widget_show (dest_entry);
485 gtk_box_pack_start (GTK_BOX (hbox1), dest_entry, TRUE, TRUE, 0);
487 color_optmenu = gtk_option_menu_new();
488 gtk_option_menu_set_menu(GTK_OPTION_MENU(color_optmenu),
489 colorlabel_create_color_menu());
490 gtk_box_pack_start(GTK_BOX(hbox1), color_optmenu, TRUE, TRUE, 0);
492 dest_btn = gtk_button_new_with_label (_("Select ..."));
493 gtk_widget_show (dest_btn);
494 gtk_box_pack_start (GTK_BOX (hbox1), dest_btn, FALSE, FALSE, 0);
495 gtk_signal_connect (GTK_OBJECT (dest_btn), "clicked",
496 GTK_SIGNAL_FUNC (prefs_filtering_select_dest),
499 exec_btn = gtk_button_new_with_label (_("Info ..."));
500 gtk_widget_show (exec_btn);
501 gtk_box_pack_start (GTK_BOX (hbox1), exec_btn, FALSE, FALSE, 0);
502 gtk_signal_connect (GTK_OBJECT (exec_btn), "clicked",
503 GTK_SIGNAL_FUNC (prefs_matcher_exec_info),
506 /* register / substitute / delete */
508 reg_hbox = gtk_hbox_new (FALSE, 4);
509 gtk_widget_show (reg_hbox);
510 gtk_box_pack_start (GTK_BOX (vbox1), reg_hbox, FALSE, FALSE, 0);
512 arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_OUT);
513 gtk_widget_show (arrow);
514 gtk_box_pack_start (GTK_BOX (reg_hbox), arrow, FALSE, FALSE, 0);
515 gtk_widget_set_usize (arrow, -1, 16);
517 btn_hbox = gtk_hbox_new (TRUE, 4);
518 gtk_widget_show (btn_hbox);
519 gtk_box_pack_start (GTK_BOX (reg_hbox), btn_hbox, FALSE, FALSE, 0);
521 reg_btn = gtk_button_new_with_label (_("Add"));
522 gtk_widget_show (reg_btn);
523 gtk_box_pack_start (GTK_BOX (btn_hbox), reg_btn, FALSE, TRUE, 0);
524 gtk_signal_connect (GTK_OBJECT (reg_btn), "clicked",
525 GTK_SIGNAL_FUNC (prefs_filtering_register_cb), NULL);
527 subst_btn = gtk_button_new_with_label (_(" Replace "));
528 gtk_widget_show (subst_btn);
529 gtk_box_pack_start (GTK_BOX (btn_hbox), subst_btn, FALSE, TRUE, 0);
530 gtk_signal_connect (GTK_OBJECT (subst_btn), "clicked",
531 GTK_SIGNAL_FUNC (prefs_filtering_substitute_cb),
534 del_btn = gtk_button_new_with_label (_("Delete"));
535 gtk_widget_show (del_btn);
536 gtk_box_pack_start (GTK_BOX (btn_hbox), del_btn, FALSE, TRUE, 0);
537 gtk_signal_connect (GTK_OBJECT (del_btn), "clicked",
538 GTK_SIGNAL_FUNC (prefs_filtering_delete_cb), NULL);
540 cond_hbox = gtk_hbox_new (FALSE, 8);
541 gtk_widget_show (cond_hbox);
542 gtk_box_pack_start (GTK_BOX (vbox1), cond_hbox, TRUE, TRUE, 0);
544 cond_scrolledwin = gtk_scrolled_window_new (NULL, NULL);
545 gtk_widget_show (cond_scrolledwin);
546 gtk_widget_set_usize (cond_scrolledwin, -1, 150);
547 gtk_box_pack_start (GTK_BOX (cond_hbox), cond_scrolledwin,
549 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (cond_scrolledwin),
550 GTK_POLICY_AUTOMATIC,
551 GTK_POLICY_AUTOMATIC);
553 title[0] = _("Current filtering/processing rules");
554 cond_clist = gtk_clist_new_with_titles(1, title);
555 gtk_widget_show (cond_clist);
556 gtk_container_add (GTK_CONTAINER (cond_scrolledwin), cond_clist);
557 gtk_clist_set_column_width (GTK_CLIST (cond_clist), 0, 80);
558 gtk_clist_set_selection_mode (GTK_CLIST (cond_clist),
559 GTK_SELECTION_BROWSE);
560 GTK_WIDGET_UNSET_FLAGS (GTK_CLIST (cond_clist)->column[0].button,
562 gtk_signal_connect (GTK_OBJECT (cond_clist), "select_row",
563 GTK_SIGNAL_FUNC (prefs_filtering_select), NULL);
565 btn_vbox = gtk_vbox_new (FALSE, 8);
566 gtk_widget_show (btn_vbox);
567 gtk_box_pack_start (GTK_BOX (cond_hbox), btn_vbox, FALSE, FALSE, 0);
569 up_btn = gtk_button_new_with_label (_("Up"));
570 gtk_widget_show (up_btn);
571 gtk_box_pack_start (GTK_BOX (btn_vbox), up_btn, FALSE, FALSE, 0);
572 gtk_signal_connect (GTK_OBJECT (up_btn), "clicked",
573 GTK_SIGNAL_FUNC (prefs_filtering_up), NULL);
575 down_btn = gtk_button_new_with_label (_("Down"));
576 gtk_widget_show (down_btn);
577 gtk_box_pack_start (GTK_BOX (btn_vbox), down_btn, FALSE, FALSE, 0);
578 gtk_signal_connect (GTK_OBJECT (down_btn), "clicked",
579 GTK_SIGNAL_FUNC (prefs_filtering_down), NULL);
581 gtk_widget_set_usize(window, 500, -1);
583 gtk_widget_show_all(window);
585 filtering.window = window;
586 filtering.ok_btn = ok_btn;
588 filtering.cond_entry = cond_entry;
589 filtering.action_list = action_list;
590 filtering.action_combo = action_combo;
591 filtering.account_label = account_label;
592 filtering.account_list = account_list;
593 filtering.account_combo = account_combo;
594 filtering.dest_entry = dest_entry;
595 filtering.dest_btn = dest_btn;
596 filtering.dest_label = dest_label;
597 filtering.exec_label = exec_label;
598 filtering.exec_btn = exec_btn;
600 filtering.cond_clist = cond_clist;
602 filtering.color_label = color_label;
603 filtering.color_optmenu = color_optmenu;
606 static void prefs_filtering_update_hscrollbar(void)
608 gint optwidth = gtk_clist_optimal_column_width(GTK_CLIST(filtering.cond_clist), 0);
609 gtk_clist_set_column_width(GTK_CLIST(filtering.cond_clist), 0, optwidth);
612 void prefs_filtering_rename_path(const gchar *old_path, const gchar *new_path)
615 gchar *paths[2] = {NULL, NULL};
616 paths[0] = (gchar*)old_path;
617 paths[1] = (gchar*)new_path;
618 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
620 folder = (Folder *) cur->data;
621 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
622 prefs_filtering_rename_path_func, paths);
624 prefs_filtering_rename_path_func(NULL, paths);
627 static gboolean prefs_filtering_rename_path_func(GNode *node, gpointer data)
630 gchar *old_path, *new_path;
635 gchar *old_path_with_sep;
641 old_path = ((gchar **)data)[0];
642 new_path = ((gchar **)data)[1];
644 g_return_val_if_fail(old_path != NULL, FALSE);
645 g_return_val_if_fail(new_path != NULL, FALSE);
647 oldpathlen = strlen(old_path);
648 old_path_with_sep = g_strconcat(old_path,G_DIR_SEPARATOR_S,NULL);
650 cur = global_processing;
653 if (!item || !item->prefs)
655 cur = item->prefs->processing;
658 for (; cur != NULL; cur = cur->next) {
659 FilteringProp *filtering = (FilteringProp *)cur->data;
660 FilteringAction *action = filtering->action;
662 if (!action->destination) continue;
664 destlen = strlen(action->destination);
666 if (destlen > oldpathlen) {
667 prefixlen = destlen - oldpathlen;
668 suffix = action->destination + prefixlen;
670 if (!strncmp(old_path, suffix, oldpathlen)) {
671 prefix = g_malloc0(prefixlen + 1);
672 strncpy2(prefix, action->destination, prefixlen);
674 base = suffix + oldpathlen;
675 while (*base == G_DIR_SEPARATOR) base++;
677 dest_path = g_strconcat(prefix,
681 dest_path = g_strconcat(prefix,
688 g_free(action->destination);
689 action->destination = dest_path;
690 } else { /* for non-leaf folders */
691 /* compare with trailing slash */
692 if (!strncmp(old_path_with_sep, action->destination, oldpathlen+1)) {
694 suffix = action->destination + oldpathlen + 1;
695 dest_path = g_strconcat(new_path,
698 g_free(action->destination);
699 action->destination = dest_path;
703 /* folder-moving a leaf */
704 if (!strcmp(old_path, action->destination)) {
705 dest_path = g_strdup(new_path);
706 g_free(action->destination);
707 action->destination = dest_path;
711 g_free(old_path_with_sep);
712 prefs_matcher_write_config();
717 void prefs_filtering_delete_path(const gchar *path)
720 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
722 folder = (Folder *) cur->data;
723 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
724 prefs_filtering_delete_path_func, (gchar *)path);
726 prefs_filtering_delete_path_func(NULL, (gchar *)path);
729 static gboolean prefs_filtering_delete_path_func(GNode *node, gpointer data)
733 gchar *path = (gchar *)data;
740 g_return_val_if_fail(path != NULL, FALSE);
742 pathlen = strlen(path);
744 cur = global_processing;
747 if (!item || !item->prefs)
749 cur = item->prefs->processing;
753 for (; cur != NULL; cur = cur->next) {
754 FilteringProp *filtering = (FilteringProp *)cur->data;
755 FilteringAction *action;
759 action = filtering->action;
762 if (!action->destination) continue;
764 destlen = strlen(action->destination);
766 if (destlen > pathlen) {
767 prefixlen = destlen - pathlen;
768 suffix = action->destination + prefixlen;
770 if (suffix && !strncmp(path, suffix, pathlen)) {
771 filteringprop_free(filtering);
772 orig = g_slist_remove(orig, filtering);
774 } else if (strcmp(action->destination, path) == 0) {
775 filteringprop_free(filtering);
776 orig = g_slist_remove(orig, filtering);
782 global_processing = orig;
785 if (!item || !item->prefs)
787 item->prefs->processing = orig;
790 prefs_matcher_write_config();
795 static void prefs_filtering_set_dialog(const gchar *header, const gchar *key)
797 GtkCList *clist = GTK_CLIST(filtering.cond_clist);
799 GSList * prefs_filtering;
803 gtk_clist_freeze(clist);
804 gtk_clist_clear(clist);
806 cond_str[0] = _("(New)");
807 row = gtk_clist_append(clist, cond_str);
808 gtk_clist_set_row_data(clist, row, NULL);
810 if (cur_item == NULL)
811 prefs_filtering = global_processing;
813 prefs_filtering = cur_item->prefs->processing;
815 for(cur = prefs_filtering ; cur != NULL ; cur = g_slist_next(cur)) {
816 FilteringProp * prop = (FilteringProp *) cur->data;
818 cond_str[0] = filteringprop_to_string(prop);
819 subst_char(cond_str[0], '\t', ':');
820 row = gtk_clist_append(clist, cond_str);
821 gtk_clist_set_row_data(clist, row, prop);
826 prefs_filtering_update_hscrollbar();
827 gtk_clist_thaw(clist);
829 prefs_filtering_reset_dialog();
832 gchar *match_str = g_strconcat(header, " ",
833 get_matchparser_tab_str(MATCHTYPE_MATCHCASE),
834 " \"", key, "\"", NULL);
835 gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), match_str);
840 static void prefs_filtering_reset_dialog(void)
842 gtk_list_select_item(GTK_LIST(filtering.action_list), 0);
843 gtk_list_select_item(GTK_LIST(filtering.account_list), 0);
844 gtk_entry_set_text(GTK_ENTRY(filtering.dest_entry), "");
845 gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), "");
848 static void prefs_filtering_set_list(void)
853 gchar * filtering_str;
854 GSList * prefs_filtering;
856 if (cur_item == NULL)
857 prefs_filtering = global_processing;
859 prefs_filtering = cur_item->prefs->processing;
861 for(cur = prefs_filtering ; cur != NULL ; cur = g_slist_next(cur))
862 filteringprop_free((FilteringProp *) cur->data);
863 g_slist_free(prefs_filtering);
864 prefs_filtering = NULL;
866 while (gtk_clist_get_text(GTK_CLIST(filtering.cond_clist),
867 row, 0, &filtering_str)) {
868 if (strcmp(filtering_str, _("(New)")) != 0) {
869 /* tmp = filtering_str; */
870 prop = matcher_parser_get_filtering(filtering_str);
873 g_slist_append(prefs_filtering, prop);
878 if (cur_item == NULL)
879 global_processing = prefs_filtering;
881 cur_item->prefs->processing = prefs_filtering;
884 static gint prefs_filtering_clist_set_row(gint row, FilteringProp * prop)
886 GtkCList *clist = GTK_CLIST(filtering.cond_clist);
891 cond_str[0] = _("(New)");
892 return gtk_clist_append(clist, cond_str);
895 str = filteringprop_to_string(prop);
902 row = gtk_clist_append(clist, cond_str);
904 gtk_clist_set_text(clist, row, 0, cond_str[0]);
910 static void prefs_filtering_condition_define_done(MatcherList * matchers)
914 if (matchers == NULL)
917 str = matcherlist_to_string(matchers);
920 gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), str);
925 static void prefs_filtering_condition_define(void)
928 MatcherList * matchers = NULL;
930 cond_str = gtk_entry_get_text(GTK_ENTRY(filtering.cond_entry));
932 if (*cond_str != '\0') {
933 matchers = matcher_parser_get_cond(cond_str);
934 if (matchers == NULL)
935 alertpanel_error(_("Condition string is not valid."));
938 prefs_matcher_open(matchers, prefs_filtering_condition_define_done);
940 if (matchers != NULL)
941 matcherlist_free(matchers);
945 /* register / substitute delete buttons */
948 static FilteringProp * prefs_filtering_dialog_to_filtering(gboolean alert)
952 FilteringProp * prop;
953 FilteringAction * action;
961 cond_str = gtk_entry_get_text(GTK_ENTRY(filtering.cond_entry));
962 if (*cond_str == '\0') {
963 if(alert == TRUE) alertpanel_error(_("Condition string is empty."));
967 action_id = get_sel_from_list(GTK_LIST(filtering.action_list));
968 action_type = prefs_filtering_get_matching_from_action(action_id);
969 list_id = get_sel_from_list(GTK_LIST(filtering.account_list));
970 account_id = get_account_id_from_list_id(list_id);
976 case ACTION_FORWARD_AS_ATTACHMENT:
977 case ACTION_REDIRECT:
979 destination = gtk_entry_get_text(GTK_ENTRY(filtering.dest_entry));
980 if (*destination == '\0') {
981 if(alert == TRUE) alertpanel_error(_("Destination is not set."));
986 labelcolor = colorlabel_get_color_menu_active_item(
987 gtk_option_menu_get_menu(GTK_OPTION_MENU(filtering.color_optmenu)));
995 action = filteringaction_new(action_type, account_id, destination, labelcolor);
997 cond = matcher_parser_get_cond(cond_str);
1000 if(alert == TRUE) alertpanel_error(_("Condition string is not valid."));
1001 filteringaction_free(action);
1005 prop = filteringprop_new(cond, action);
1010 static void prefs_filtering_register_cb(void)
1012 FilteringProp * prop;
1014 prop = prefs_filtering_dialog_to_filtering(TRUE);
1017 prefs_filtering_clist_set_row(-1, prop);
1019 filteringprop_free(prop);
1021 prefs_filtering_update_hscrollbar();
1024 static void prefs_filtering_substitute_cb(void)
1026 GtkCList *clist = GTK_CLIST(filtering.cond_clist);
1028 FilteringProp * prop;
1030 if (!clist->selection) return;
1032 row = GPOINTER_TO_INT(clist->selection->data);
1033 if (row == 0) return;
1035 prop = prefs_filtering_dialog_to_filtering(TRUE);
1038 prefs_filtering_clist_set_row(row, prop);
1040 filteringprop_free(prop);
1042 prefs_filtering_update_hscrollbar();
1045 static void prefs_filtering_delete_cb(void)
1047 GtkCList *clist = GTK_CLIST(filtering.cond_clist);
1050 if (!clist->selection) return;
1051 row = GPOINTER_TO_INT(clist->selection->data);
1052 if (row == 0) return;
1054 if (alertpanel(_("Delete rule"),
1055 _("Do you really want to delete this rule?"),
1056 _("Yes"), _("No"), NULL) == G_ALERTALTERNATE)
1059 gtk_clist_remove(clist, row);
1061 prefs_filtering_reset_dialog();
1063 prefs_filtering_update_hscrollbar();
1066 static void prefs_filtering_up(void)
1068 GtkCList *clist = GTK_CLIST(filtering.cond_clist);
1071 if (!clist->selection) return;
1073 row = GPOINTER_TO_INT(clist->selection->data);
1075 gtk_clist_row_move(clist, row, row - 1);
1076 if(gtk_clist_row_is_visible(clist, row - 1) != GTK_VISIBILITY_FULL) {
1077 gtk_clist_moveto(clist, row - 1, 0, 0, 0);
1082 static void prefs_filtering_down(void)
1084 GtkCList *clist = GTK_CLIST(filtering.cond_clist);
1087 if (!clist->selection) return;
1089 row = GPOINTER_TO_INT(clist->selection->data);
1090 if (row > 0 && row < clist->rows - 1) {
1091 gtk_clist_row_move(clist, row, row + 1);
1092 if(gtk_clist_row_is_visible(clist, row + 1) != GTK_VISIBILITY_FULL) {
1093 gtk_clist_moveto(clist, row + 1, 0, 1, 0);
1098 static void prefs_filtering_select_set(FilteringProp * prop)
1100 FilteringAction * action;
1101 gchar * matcher_str;
1104 prefs_filtering_reset_dialog();
1106 action = prop->action;
1108 matcher_str = matcherlist_to_string(prop->matchers);
1109 if (matcher_str == NULL) {
1110 filteringprop_free(prop);
1114 gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), matcher_str);
1116 if (action->destination)
1117 gtk_entry_set_text(GTK_ENTRY(filtering.dest_entry), action->destination);
1119 gtk_entry_set_text(GTK_ENTRY(filtering.dest_entry), "");
1121 switch(action->type) {
1122 case MATCHACTION_MOVE:
1123 gtk_list_select_item(GTK_LIST(filtering.action_list),
1126 case MATCHACTION_COPY:
1127 gtk_list_select_item(GTK_LIST(filtering.action_list),
1130 case MATCHACTION_DELETE:
1131 gtk_list_select_item(GTK_LIST(filtering.action_list),
1134 case MATCHACTION_MARK:
1135 gtk_list_select_item(GTK_LIST(filtering.action_list),
1138 case MATCHACTION_UNMARK:
1139 gtk_list_select_item(GTK_LIST(filtering.action_list),
1142 case MATCHACTION_MARK_AS_READ:
1143 gtk_list_select_item(GTK_LIST(filtering.action_list),
1144 ACTION_MARK_AS_READ);
1146 case MATCHACTION_MARK_AS_UNREAD:
1147 gtk_list_select_item(GTK_LIST(filtering.action_list),
1148 ACTION_MARK_AS_UNREAD);
1150 case MATCHACTION_FORWARD:
1151 list_id = get_list_id_from_account_id(action->account_id);
1152 gtk_list_select_item(GTK_LIST(filtering.action_list),
1154 gtk_list_select_item(GTK_LIST(filtering.account_list),
1157 case MATCHACTION_FORWARD_AS_ATTACHMENT:
1158 list_id = get_list_id_from_account_id(action->account_id);
1159 gtk_list_select_item(GTK_LIST(filtering.action_list),
1160 ACTION_FORWARD_AS_ATTACHMENT);
1161 gtk_list_select_item(GTK_LIST(filtering.account_list),
1164 case MATCHACTION_REDIRECT:
1165 list_id = get_list_id_from_account_id(action->account_id);
1166 gtk_list_select_item(GTK_LIST(filtering.action_list),
1168 gtk_list_select_item(GTK_LIST(filtering.account_list),
1171 case MATCHACTION_EXECUTE:
1172 gtk_list_select_item(GTK_LIST(filtering.action_list),
1175 case MATCHACTION_COLOR:
1176 gtk_list_select_item(GTK_LIST(filtering.action_list),
1178 gtk_option_menu_set_history(GTK_OPTION_MENU(filtering.color_optmenu), action->labelcolor);
1182 g_free(matcher_str);
1185 static void prefs_filtering_select(GtkCList *clist, gint row, gint column,
1188 FilteringProp * prop;
1189 gchar * filtering_str;
1192 prefs_filtering_reset_dialog();
1196 if (!gtk_clist_get_text(GTK_CLIST(filtering.cond_clist),
1197 row, 0, &filtering_str))
1200 prop = matcher_parser_get_filtering(filtering_str);
1204 prefs_filtering_select_set(prop);
1206 filteringprop_free(prop);
1209 static void prefs_filtering_select_dest(void)
1214 dest = foldersel_folder_sel(NULL, FOLDER_SEL_COPY, NULL);
1217 path = folder_item_get_identifier(dest);
1219 gtk_entry_set_text(GTK_ENTRY(filtering.dest_entry), path);
1223 static void prefs_filtering_action_selection_changed(GtkList *list,
1228 value = get_sel_from_list(GTK_LIST(filtering.action_list));
1230 if (filtering.current_action != value) {
1231 if (filtering.current_action == ACTION_FORWARD
1232 || filtering.current_action == ACTION_FORWARD_AS_ATTACHMENT) {
1233 debug_print("unregistering address completion entry\n");
1234 address_completion_unregister_entry(GTK_ENTRY(filtering.dest_entry));
1236 if (value == ACTION_FORWARD || value == ACTION_FORWARD_AS_ATTACHMENT) {
1237 debug_print("registering address completion entry\n");
1238 address_completion_register_entry(GTK_ENTRY(filtering.dest_entry));
1240 filtering.current_action = value;
1244 static void prefs_filtering_action_select(GtkList *list,
1250 value = get_sel_from_list(GTK_LIST(filtering.action_list));
1254 gtk_widget_show(filtering.account_label);
1255 gtk_widget_set_sensitive(filtering.account_label, FALSE);
1256 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1257 gtk_widget_show(filtering.dest_entry);
1258 gtk_widget_set_sensitive(filtering.dest_entry, TRUE);
1259 gtk_widget_show(filtering.dest_btn);
1260 gtk_widget_set_sensitive(filtering.dest_btn, TRUE);
1261 gtk_widget_show(filtering.dest_label);
1262 gtk_widget_set_sensitive(filtering.dest_label, TRUE);
1263 gtk_widget_hide(filtering.exec_label);
1264 gtk_widget_hide(filtering.exec_btn);
1265 gtk_widget_hide(filtering.color_optmenu);
1266 gtk_widget_hide(filtering.color_label);
1269 gtk_widget_show(filtering.account_label);
1270 gtk_widget_set_sensitive(filtering.account_label, FALSE);
1271 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1272 gtk_widget_show(filtering.dest_entry);
1273 gtk_widget_set_sensitive(filtering.dest_entry, TRUE);
1274 gtk_widget_show(filtering.dest_btn);
1275 gtk_widget_set_sensitive(filtering.dest_btn, TRUE);
1276 gtk_widget_show(filtering.dest_label);
1277 gtk_widget_set_sensitive(filtering.dest_label, TRUE);
1278 gtk_widget_hide(filtering.exec_label);
1279 gtk_widget_hide(filtering.exec_btn);
1280 gtk_widget_hide(filtering.color_optmenu);
1281 gtk_widget_hide(filtering.color_label);
1284 gtk_widget_show(filtering.account_label);
1285 gtk_widget_set_sensitive(filtering.account_label, FALSE);
1286 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1287 gtk_widget_show(filtering.dest_entry);
1288 gtk_widget_set_sensitive(filtering.dest_entry, FALSE);
1289 gtk_widget_show(filtering.dest_btn);
1290 gtk_widget_set_sensitive(filtering.dest_btn, FALSE);
1291 gtk_widget_show(filtering.dest_label);
1292 gtk_widget_set_sensitive(filtering.dest_label, FALSE);
1293 gtk_widget_hide(filtering.exec_label);
1294 gtk_widget_hide(filtering.exec_btn);
1295 gtk_widget_hide(filtering.color_optmenu);
1296 gtk_widget_hide(filtering.color_label);
1299 gtk_widget_show(filtering.account_label);
1300 gtk_widget_set_sensitive(filtering.account_label, FALSE);
1301 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1302 gtk_widget_show(filtering.dest_entry);
1303 gtk_widget_set_sensitive(filtering.dest_entry, FALSE);
1304 gtk_widget_show(filtering.dest_btn);
1305 gtk_widget_set_sensitive(filtering.dest_btn, FALSE);
1306 gtk_widget_show(filtering.dest_label);
1307 gtk_widget_set_sensitive(filtering.dest_label, FALSE);
1308 gtk_widget_hide(filtering.exec_label);
1309 gtk_widget_hide(filtering.exec_btn);
1310 gtk_widget_hide(filtering.color_optmenu);
1311 gtk_widget_hide(filtering.color_label);
1314 gtk_widget_show(filtering.account_label);
1315 gtk_widget_set_sensitive(filtering.account_label, FALSE);
1316 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1317 gtk_widget_show(filtering.dest_entry);
1318 gtk_widget_set_sensitive(filtering.dest_entry, FALSE);
1319 gtk_widget_show(filtering.dest_btn);
1320 gtk_widget_set_sensitive(filtering.dest_btn, FALSE);
1321 gtk_widget_show(filtering.dest_label);
1322 gtk_widget_set_sensitive(filtering.dest_label, FALSE);
1323 gtk_widget_hide(filtering.exec_label);
1324 gtk_widget_hide(filtering.exec_btn);
1325 gtk_widget_hide(filtering.color_optmenu);
1326 gtk_widget_hide(filtering.color_label);
1328 case ACTION_MARK_AS_READ:
1329 gtk_widget_show(filtering.account_label);
1330 gtk_widget_set_sensitive(filtering.account_label, FALSE);
1331 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1332 gtk_widget_show(filtering.dest_entry);
1333 gtk_widget_set_sensitive(filtering.dest_entry, FALSE);
1334 gtk_widget_show(filtering.dest_btn);
1335 gtk_widget_set_sensitive(filtering.dest_btn, FALSE);
1336 gtk_widget_show(filtering.dest_label);
1337 gtk_widget_set_sensitive(filtering.dest_label, FALSE);
1338 gtk_widget_hide(filtering.exec_label);
1339 gtk_widget_hide(filtering.exec_btn);
1340 gtk_widget_hide(filtering.color_optmenu);
1341 gtk_widget_hide(filtering.color_label);
1343 case ACTION_MARK_AS_UNREAD:
1344 gtk_widget_show(filtering.account_label);
1345 gtk_widget_set_sensitive(filtering.account_label, FALSE);
1346 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1347 gtk_widget_show(filtering.dest_entry);
1348 gtk_widget_set_sensitive(filtering.dest_entry, FALSE);
1349 gtk_widget_show(filtering.dest_btn);
1350 gtk_widget_set_sensitive(filtering.dest_btn, FALSE);
1351 gtk_widget_show(filtering.dest_label);
1352 gtk_widget_set_sensitive(filtering.dest_label, FALSE);
1353 gtk_widget_hide(filtering.exec_label);
1354 gtk_widget_hide(filtering.exec_btn);
1355 gtk_widget_hide(filtering.color_optmenu);
1356 gtk_widget_hide(filtering.color_label);
1358 case ACTION_FORWARD:
1359 gtk_widget_show(filtering.account_label);
1360 gtk_widget_set_sensitive(filtering.account_label, TRUE);
1361 gtk_widget_set_sensitive(filtering.account_combo, TRUE);
1362 gtk_widget_show(filtering.dest_entry);
1363 gtk_widget_set_sensitive(filtering.dest_entry, TRUE);
1364 gtk_widget_show(filtering.dest_btn);
1365 gtk_widget_set_sensitive(filtering.dest_btn, FALSE);
1366 gtk_widget_show(filtering.dest_label);
1367 gtk_widget_set_sensitive(filtering.dest_label, TRUE);
1368 gtk_widget_hide(filtering.exec_label);
1369 gtk_widget_hide(filtering.exec_btn);
1370 gtk_widget_hide(filtering.color_optmenu);
1371 gtk_widget_hide(filtering.color_label);
1373 case ACTION_FORWARD_AS_ATTACHMENT:
1374 gtk_widget_show(filtering.account_label);
1375 gtk_widget_set_sensitive(filtering.account_label, TRUE);
1376 gtk_widget_set_sensitive(filtering.account_combo, TRUE);
1377 gtk_widget_show(filtering.dest_entry);
1378 gtk_widget_set_sensitive(filtering.dest_entry, TRUE);
1379 gtk_widget_show(filtering.dest_btn);
1380 gtk_widget_set_sensitive(filtering.dest_btn, FALSE);
1381 gtk_widget_show(filtering.dest_label);
1382 gtk_widget_set_sensitive(filtering.dest_label, TRUE);
1383 gtk_widget_hide(filtering.exec_label);
1384 gtk_widget_hide(filtering.exec_btn);
1385 gtk_widget_hide(filtering.color_optmenu);
1386 gtk_widget_hide(filtering.color_label);
1388 case ACTION_REDIRECT:
1389 gtk_widget_show(filtering.account_label);
1390 gtk_widget_set_sensitive(filtering.account_label, TRUE);
1391 gtk_widget_set_sensitive(filtering.account_combo, TRUE);
1392 gtk_widget_show(filtering.dest_entry);
1393 gtk_widget_set_sensitive(filtering.dest_entry, TRUE);
1394 gtk_widget_show(filtering.dest_btn);
1395 gtk_widget_set_sensitive(filtering.dest_btn, FALSE);
1396 gtk_widget_show(filtering.dest_label);
1397 gtk_widget_set_sensitive(filtering.dest_label, TRUE);
1398 gtk_widget_hide(filtering.exec_label);
1399 gtk_widget_hide(filtering.exec_btn);
1400 gtk_widget_hide(filtering.color_optmenu);
1401 gtk_widget_hide(filtering.color_label);
1403 case ACTION_EXECUTE:
1404 gtk_widget_show(filtering.account_label);
1405 gtk_widget_set_sensitive(filtering.account_label, FALSE);
1406 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1407 gtk_widget_show(filtering.dest_entry);
1408 gtk_widget_set_sensitive(filtering.dest_entry, TRUE);
1409 gtk_widget_hide(filtering.dest_btn);
1410 gtk_widget_hide(filtering.dest_label);
1411 gtk_widget_show(filtering.exec_label);
1412 gtk_widget_set_sensitive(filtering.exec_btn, TRUE);
1413 gtk_widget_show(filtering.exec_btn);
1414 gtk_widget_hide(filtering.color_optmenu);
1415 gtk_widget_hide(filtering.color_label);
1418 gtk_widget_show(filtering.account_label);
1419 gtk_widget_set_sensitive(filtering.account_label, FALSE);
1420 gtk_widget_set_sensitive(filtering.account_combo, FALSE);
1421 gtk_widget_hide(filtering.dest_entry);
1422 gtk_widget_hide(filtering.dest_btn);
1423 gtk_widget_hide(filtering.dest_label);
1424 gtk_widget_hide(filtering.exec_label);
1425 gtk_widget_show(filtering.exec_btn);
1426 gtk_widget_set_sensitive(filtering.exec_btn, FALSE);
1427 gtk_widget_show(filtering.color_optmenu);
1428 gtk_widget_show(filtering.color_label);
1433 static gint prefs_filtering_deleted(GtkWidget *widget, GdkEventAny *event,
1436 prefs_filtering_cancel();
1440 static void prefs_filtering_key_pressed(GtkWidget *widget, GdkEventKey *event,
1443 if (event && event->keyval == GDK_Escape)
1444 prefs_filtering_cancel();
1447 static void prefs_filtering_ok(void)
1449 FilteringProp * prop;
1451 gchar * filtering_str;
1455 prop = prefs_filtering_dialog_to_filtering(FALSE);
1457 str = filteringprop_to_string(prop);
1459 while (gtk_clist_get_text(GTK_CLIST(filtering.cond_clist),
1460 row, 0, &filtering_str)) {
1461 if (strcmp(filtering_str, str) == 0) break;
1464 if (strcmp(filtering_str, str) != 0) {
1465 val = alertpanel(_("Entry not saved"),
1466 _("The entry was not saved. Close anyway?"),
1467 _("Yes"), _("No"), NULL);
1468 if (G_ALERTDEFAULT != val) {
1475 prefs_filtering_set_list();
1476 prefs_matcher_write_config();
1477 prefs_filtering_close();
1480 static void prefs_filtering_cancel(void)
1482 prefs_matcher_read_config();
1483 prefs_filtering_close();