Fix CID 1491093: attrib leaked if attvalue is null
[claws.git] / src / prefs_filtering.c
blob6ada266c3f7b0142fae58d99297f0a2de3611cf3
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2024 the Claws Mail team and Hiroyuki Yamamoto
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 3 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, see <http://www.gnu.org/licenses/>.
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #include "claws-features.h"
22 #endif
24 #include "defs.h"
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <gtk/gtk.h>
29 #include <gdk/gdkkeysyms.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <errno.h>
35 #include "main.h"
36 #include "prefs_gtk.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 "folder_item_prefs.h"
49 #include "filtering.h"
50 #include "addr_compl.h"
51 #include "manual.h"
52 #include "combobox.h"
53 #include "menu.h"
54 #include "account.h"
56 #include "matcher_parser.h"
57 #include "matcher.h"
58 #include "prefs_filtering_action.h"
60 enum {
61 PREFS_FILTERING_ENABLED,
62 PREFS_FILTERING_NAME,
63 PREFS_FILTERING_ACCOUNT_ID,
64 PREFS_FILTERING_ACCOUNT_NAME,
65 PREFS_FILTERING_RULE,
66 PREFS_FILTERING_PROP,
67 N_PREFS_FILTERING_COLUMNS
70 struct _Filtering {
71 GtkWidget *window;
73 GtkWidget *help_btn;
74 GtkWidget *ok_btn;
75 GtkWidget *name_entry;
76 GtkWidget *account_label;
77 GtkWidget *account_combobox;
78 GtkListStore *account_combobox_list;
79 GtkWidget *cond_entry;
80 GtkWidget *action_entry;
82 GtkWidget *cond_list_view;
84 GtkTreeViewColumn *account_name_column;
87 typedef struct _Filtering Filtering;
89 static Filtering filtering;
91 static GSList ** p_processing_list = NULL;
93 /* widget creating functions */
94 static void prefs_filtering_create (void);
96 static void prefs_filtering_set_dialog (const gchar *header,
97 const gchar *key);
98 static void prefs_filtering_set_list (void);
100 /* callback functions */
101 static gboolean prefs_filtering_search_func_cb (GtkTreeModel *model, gint column,
102 const gchar *key, GtkTreeIter *iter,
103 gpointer search_data);
104 static void prefs_filtering_register_cb (gpointer action, gpointer data);
105 static void prefs_filtering_substitute_cb (gpointer action, gpointer data);
106 static void prefs_filtering_delete_cb (gpointer action, gpointer data);
107 static void prefs_filtering_delete_all_cb(gpointer action, gpointer data);
108 static void prefs_filtering_clear_cb(gpointer action, gpointer data);
109 static void prefs_filtering_duplicate_cb(gpointer action, gpointer data);
110 static void prefs_filtering_top (gpointer action, gpointer data);
111 static void prefs_filtering_page_up (gpointer action, gpointer data);
112 static void prefs_filtering_up (gpointer action, gpointer data);
113 static void prefs_filtering_down (gpointer action, gpointer data);
114 static void prefs_filtering_page_down (gpointer action, gpointer data);
115 static void prefs_filtering_bottom (gpointer action, gpointer data);
116 static gint prefs_filtering_deleted (GtkWidget *widget,
117 GdkEventAny *event,
118 gpointer data);
119 static void prefs_filtering_row_selected(GtkTreeSelection *selection,
120 GtkTreeView *list_view);
121 static gboolean prefs_filtering_key_pressed(GtkWidget *widget,
122 GdkEventKey *event,
123 gpointer data);
124 static void prefs_filtering_cancel (gpointer action, gpointer data);
125 static void prefs_filtering_ok (gpointer action, gpointer data);
127 static void prefs_filtering_condition_define (gpointer action, gpointer data);
128 static void prefs_filtering_action_define (gpointer action, gpointer data);
129 static gint prefs_filtering_list_view_set_row (gint row, FilteringProp * prop);
131 static void prefs_filtering_reset_dialog (void);
132 static gboolean prefs_filtering_rename_tag_func(GNode *node, gpointer data);
133 static gboolean prefs_filtering_rename_path_func(GNode *node, gpointer data);
134 static gboolean prefs_filtering_delete_path_func(GNode *node, gpointer data);
136 static void delete_path(GSList ** p_filters, const gchar * path);
139 static GtkListStore* prefs_filtering_create_data_store (void);
140 static gint prefs_filtering_list_view_insert_rule (GtkListStore *list_store,
141 gint row,
142 gboolean enabled,
143 const gchar *name,
144 gint account_id,
145 const gchar *account_name,
146 const gchar *rule,
147 gboolean prop);
148 static gchar *prefs_filtering_list_view_get_rule (GtkWidget *list,
149 gint row);
150 static void prefs_filtering_list_view_get_rule_info (GtkWidget *list,
151 gint row,
152 gboolean *enabled,
153 gchar **name,
154 gint *account_id);
156 static GtkWidget *prefs_filtering_list_view_create (void);
157 static void prefs_filtering_create_list_view_columns (GtkWidget *list_view);
159 static void prefs_filtering_select_row(GtkTreeView *list_view, GtkTreePath *path);
161 static void prefs_filtering_account_option_menu_populate(void);
163 static gulong signal_id = 0; /* filtering.help_btn clicked signal */
165 static int modified = FALSE;
167 void prefs_filtering_open(GSList ** p_processing,
168 const gchar *title,
169 const gchar *help_url_anchor,
170 const gchar *header,
171 const gchar *key,
172 gboolean per_account_filtering)
174 if (prefs_rc_is_readonly(FILTERING_RC))
175 return;
177 inc_lock();
179 if (!filtering.window) {
180 prefs_filtering_create();
181 } else {
182 gtk_list_store_clear(filtering.account_combobox_list);
183 prefs_filtering_account_option_menu_populate();
186 gtk_tree_view_column_set_visible(filtering.account_name_column,
187 per_account_filtering);
189 manage_window_set_transient(GTK_WINDOW(filtering.window));
190 gtk_widget_grab_focus(filtering.ok_btn);
192 if (title != NULL)
193 gtk_window_set_title(GTK_WINDOW(filtering.window), title);
194 else
195 gtk_window_set_title (GTK_WINDOW(filtering.window),
196 _("Filtering/Processing configuration"));
198 if (help_url_anchor != NULL) {
199 if (signal_id != 0) {
200 g_signal_handler_disconnect(
201 G_OBJECT(filtering.help_btn),
202 signal_id);
205 signal_id = g_signal_connect(G_OBJECT(filtering.help_btn),
206 "clicked",
207 G_CALLBACK(manual_open_with_anchor_cb),
208 (gchar*)help_url_anchor);
210 else {
211 gtk_widget_set_sensitive(filtering.help_btn, FALSE);
214 p_processing_list = p_processing;
216 prefs_filtering_set_dialog(header, key);
217 if (per_account_filtering) {
218 gtk_widget_show(filtering.account_label);
219 gtk_widget_show(filtering.account_combobox);
220 } else {
221 gtk_widget_hide(filtering.account_label);
222 gtk_widget_hide(filtering.account_combobox);
223 combobox_select_by_data(GTK_COMBO_BOX(filtering.account_combobox), 0);
226 gtk_widget_show(filtering.window);
227 gtk_window_set_modal(GTK_WINDOW(filtering.window), TRUE);
229 start_address_completion(NULL);
232 static void prefs_filtering_size_allocate_cb(GtkWidget *widget,
233 GtkAllocation *allocation)
235 cm_return_if_fail(allocation != NULL);
237 gtk_window_get_size(GTK_WINDOW(widget),
238 &prefs_common.filteringwin_width, &prefs_common.filteringwin_height);
241 /* prefs_filtering_close() - just to have one common exit point */
242 static void prefs_filtering_close(void)
244 GtkListStore *store;
246 end_address_completion();
248 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
249 (filtering.cond_list_view)));
250 gtk_list_store_clear(store);
251 gtk_widget_hide(filtering.window);
252 gtk_window_set_modal(GTK_WINDOW(filtering.window), FALSE);
253 inc_unlock();
256 static void prefs_filtering_account_option_menu_populate(void)
258 GList *accounts = NULL;
259 GtkTreeIter iter;
261 accounts = account_get_list();
263 cm_return_if_fail(accounts != NULL);
265 COMBOBOX_ADD(filtering.account_combobox_list, C_("Filtering Account Menu", "All"), 0);
266 COMBOBOX_ADD(filtering.account_combobox_list, NULL, 0);
267 for (; accounts != NULL; accounts = accounts->next) {
268 PrefsAccount *ac = (PrefsAccount *)accounts->data;
270 COMBOBOX_ADD_ESCAPED(filtering.account_combobox_list, ac->account_name, ac->account_id);
274 static GtkWidget *prefs_filtering_account_option_menu(Filtering *filtering)
276 GtkWidget *optmenu = NULL;
277 GtkWidget *optmenubox = NULL;
278 GtkListStore *menu = NULL;
280 optmenubox = gtk_event_box_new();
281 optmenu = gtkut_sc_combobox_create(optmenubox, FALSE);
282 menu = GTK_LIST_STORE(gtk_combo_box_get_model(GTK_COMBO_BOX(optmenu)));
284 filtering->account_combobox = optmenu;
285 filtering->account_combobox_list = menu;
287 prefs_filtering_account_option_menu_populate();
289 return optmenubox;
292 static void prefs_filtering_create(void)
294 GtkWidget *window;
295 GtkWidget *vbox;
296 GtkWidget *help_btn;
297 GtkWidget *cancel_btn;
298 GtkWidget *ok_btn;
299 GtkWidget *confirm_area;
301 GtkWidget *vbox1;
302 GtkWidget *reg_hbox;
303 GtkWidget *arrow;
304 GtkWidget *btn_hbox;
306 GtkWidget *name_label;
307 GtkWidget *name_entry;
308 GtkWidget *account_label;
309 GtkWidget *account_opt_menu;
310 GtkWidget *cond_label;
311 GtkWidget *cond_entry;
312 GtkWidget *cond_btn;
313 GtkWidget *action_label;
314 GtkWidget *action_entry;
315 GtkWidget *action_btn;
317 GtkWidget *reg_btn;
318 GtkWidget *subst_btn;
319 GtkWidget *del_btn;
320 GtkWidget *clear_btn;
322 GtkWidget *cond_hbox;
323 GtkWidget *cond_scrolledwin;
324 GtkWidget *cond_list_view;
326 GtkWidget *btn_vbox;
327 GtkWidget *top_btn;
328 GtkWidget *up_btn;
329 GtkWidget *down_btn;
330 #ifndef GENERIC_UMPC
331 GtkWidget *page_up_btn;
332 GtkWidget *page_down_btn;
333 #endif
334 GtkWidget *bottom_btn;
335 GtkWidget *table;
336 static GdkGeometry geometry;
338 debug_print("Creating filtering configuration window...\n");
340 window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "prefs_filtering");
341 gtk_container_set_border_width (GTK_CONTAINER (window), 8);
342 gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
343 gtk_window_set_resizable(GTK_WINDOW (window), TRUE);
344 gtk_window_set_type_hint(GTK_WINDOW(window), GDK_WINDOW_TYPE_HINT_DIALOG);
346 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 6);
347 gtk_widget_show (vbox);
348 gtk_container_add (GTK_CONTAINER (window), vbox);
350 gtkut_stock_button_set_create_with_help(&confirm_area, &help_btn,
351 &cancel_btn, NULL, _("_Cancel"),
352 &ok_btn, NULL, _("_OK"),
353 NULL, NULL, NULL);
354 gtk_widget_show (confirm_area);
355 gtk_box_pack_end (GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
356 gtk_widget_grab_default (ok_btn);
358 gtk_window_set_title (GTK_WINDOW(window),
359 _("Filtering/Processing configuration"));
361 g_signal_connect(G_OBJECT(window), "delete_event",
362 G_CALLBACK(prefs_filtering_deleted), NULL);
363 g_signal_connect(G_OBJECT(window), "size_allocate",
364 G_CALLBACK(prefs_filtering_size_allocate_cb), NULL);
365 g_signal_connect(G_OBJECT(window), "key_press_event",
366 G_CALLBACK(prefs_filtering_key_pressed), NULL);
367 MANAGE_WINDOW_SIGNALS_CONNECT (window);
368 g_signal_connect(G_OBJECT(ok_btn), "clicked",
369 G_CALLBACK(prefs_filtering_ok), NULL);
370 g_signal_connect(G_OBJECT(cancel_btn), "clicked",
371 G_CALLBACK(prefs_filtering_cancel), NULL);
373 vbox1 = gtk_box_new(GTK_ORIENTATION_VERTICAL, VSPACING);
374 gtk_widget_show (vbox1);
375 gtk_box_pack_start (GTK_BOX (vbox), vbox1, FALSE, TRUE, 0);
376 gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
378 table = gtk_grid_new();
379 gtk_widget_show(table);
380 gtk_grid_set_row_spacing(GTK_GRID(table), VSPACING_NARROW_2);
381 gtk_grid_set_column_spacing(GTK_GRID(table), 4);
382 gtk_box_pack_start (GTK_BOX (vbox1), table, TRUE, TRUE, 0);
384 name_label = gtk_label_new (_("Name"));
385 gtk_widget_show (name_label);
386 gtk_label_set_xalign (GTK_LABEL (name_label), 1.0);
387 gtk_grid_attach(GTK_GRID(table), name_label, 0, 0, 1, 1);
389 name_entry = gtk_entry_new ();
390 gtk_widget_show (name_entry);
391 gtk_grid_attach(GTK_GRID(table), name_entry, 1, 0, 1, 1);
392 gtk_widget_set_hexpand(name_entry, TRUE);
393 gtk_widget_set_halign(name_entry, GTK_ALIGN_FILL);
395 account_label = gtk_label_new (_("Account"));
396 gtk_widget_show (account_label);
397 gtk_label_set_xalign (GTK_LABEL (account_label), 1.0);
398 gtk_grid_attach(GTK_GRID(table), account_label, 0, 1, 1, 1);
400 account_opt_menu = prefs_filtering_account_option_menu(&filtering);
401 gtk_widget_show (account_opt_menu);
402 gtk_grid_attach(GTK_GRID(table), account_opt_menu, 1, 1, 1, 1);
403 combobox_select_by_data(GTK_COMBO_BOX(filtering.account_combobox), 0);
405 cond_label = gtk_label_new (_("Condition"));
406 gtk_widget_show (cond_label);
407 gtk_label_set_xalign (GTK_LABEL (cond_label), 1.0);
408 gtk_grid_attach(GTK_GRID(table), cond_label, 0, 2, 1, 1);
410 cond_entry = gtk_entry_new ();
411 gtk_widget_show (cond_entry);
412 gtk_grid_attach(GTK_GRID(table), cond_entry, 1, 2, 1, 1);
413 gtk_widget_set_hexpand(cond_entry, TRUE);
414 gtk_widget_set_halign(cond_entry, GTK_ALIGN_FILL);
416 cond_btn = gtk_button_new_with_mnemonic (_(" Def_ine... "));
417 gtk_widget_show (cond_btn);
418 gtk_grid_attach(GTK_GRID(table), cond_btn, 2, 2, 1, 1);
419 g_signal_connect(G_OBJECT (cond_btn), "clicked",
420 G_CALLBACK(prefs_filtering_condition_define),
421 NULL);
423 action_label = gtk_label_new (_("Action"));
424 gtk_widget_show (action_label);
425 gtk_label_set_xalign (GTK_LABEL (action_label), 1.0);
426 gtk_grid_attach(GTK_GRID(table), action_label, 0, 3, 1, 1);
428 action_entry = gtk_entry_new ();
429 gtk_widget_show (action_entry);
430 gtk_grid_attach(GTK_GRID(table), action_entry, 1, 3, 1, 1);
431 gtk_widget_set_hexpand(action_entry, TRUE);
432 gtk_widget_set_halign(action_entry, GTK_ALIGN_FILL);
434 action_btn = gtk_button_new_with_mnemonic (_(" De_fine... "));
435 gtk_widget_show (action_btn);
436 gtk_grid_attach(GTK_GRID(table), action_btn, 2, 3, 1, 1);
437 g_signal_connect(G_OBJECT (action_btn), "clicked",
438 G_CALLBACK(prefs_filtering_action_define),
439 NULL);
441 /* register / substitute / delete */
442 reg_hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 4);
443 gtk_widget_show (reg_hbox);
444 gtk_box_pack_start (GTK_BOX (vbox1), reg_hbox, FALSE, FALSE, 0);
446 arrow = gtk_image_new_from_icon_name("pan-down-symbolic", GTK_ICON_SIZE_MENU);
447 gtk_widget_show (arrow);
448 gtk_box_pack_start (GTK_BOX (reg_hbox), arrow, FALSE, FALSE, 0);
449 gtk_widget_set_size_request (arrow, -1, 16);
451 btn_hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 4);
452 gtk_widget_show (btn_hbox);
453 gtk_box_pack_start (GTK_BOX (reg_hbox), btn_hbox, FALSE, FALSE, 0);
455 reg_btn = gtkut_stock_button("list-add", _("_Add"));
456 gtk_widget_show (reg_btn);
457 gtk_box_pack_start (GTK_BOX (btn_hbox), reg_btn, FALSE, TRUE, 0);
458 g_signal_connect(G_OBJECT (reg_btn), "clicked",
459 G_CALLBACK(prefs_filtering_register_cb), NULL);
460 CLAWS_SET_TIP(reg_btn,
461 _("Append the new rule above to the list"));
463 subst_btn = gtkut_stock_button("edit-redo", _("_Replace"));
464 gtk_widget_show (subst_btn);
465 gtk_box_pack_start (GTK_BOX (btn_hbox), subst_btn, FALSE, TRUE, 0);
466 g_signal_connect(G_OBJECT (subst_btn), "clicked",
467 G_CALLBACK(prefs_filtering_substitute_cb),
468 NULL);
469 CLAWS_SET_TIP(subst_btn,
470 _("Replace the selected rule in list with the rule above"));
472 del_btn = gtkut_stock_button("edit-delete", _("D_elete"));
473 gtk_box_pack_start (GTK_BOX (btn_hbox), del_btn, FALSE, TRUE, 0);
474 g_signal_connect(G_OBJECT (del_btn), "clicked",
475 G_CALLBACK(prefs_filtering_delete_cb), NULL);
476 CLAWS_SET_TIP(del_btn,
477 _("Delete the selected rule from the list"));
479 clear_btn = gtkut_stock_button("edit-clear", _("C_lear"));
480 gtk_widget_show (clear_btn);
481 gtk_box_pack_start (GTK_BOX (btn_hbox), clear_btn, FALSE, TRUE, 0);
482 g_signal_connect(G_OBJECT (clear_btn), "clicked",
483 G_CALLBACK(prefs_filtering_clear_cb), NULL);
484 CLAWS_SET_TIP(clear_btn,
485 _("Clear all the input fields in the dialog"));
487 cond_hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
488 gtk_widget_show (cond_hbox);
489 gtk_box_pack_start (GTK_BOX (vbox), cond_hbox, TRUE, TRUE, 0);
491 cond_scrolledwin = gtk_scrolled_window_new (NULL, NULL);
492 gtk_widget_show (cond_scrolledwin);
493 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(cond_scrolledwin),
494 GTK_SHADOW_ETCHED_IN);
495 gtk_widget_set_size_request (cond_scrolledwin, -1, 150);
496 gtk_box_pack_start (GTK_BOX (cond_hbox), cond_scrolledwin,
497 TRUE, TRUE, 0);
498 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (cond_scrolledwin),
499 GTK_POLICY_AUTOMATIC,
500 GTK_POLICY_AUTOMATIC);
502 cond_list_view = prefs_filtering_list_view_create();
503 gtk_widget_show (cond_list_view);
504 gtk_container_add (GTK_CONTAINER (cond_scrolledwin), cond_list_view);
506 btn_vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 8);
507 gtk_widget_show (btn_vbox);
508 gtk_box_pack_start (GTK_BOX (cond_hbox), btn_vbox, FALSE, FALSE, 0);
510 top_btn = gtkut_stock_button("go-top", _("_Top"));
511 gtk_widget_show (top_btn);
512 gtk_box_pack_start (GTK_BOX (btn_vbox), top_btn, FALSE, FALSE, 0);
513 g_signal_connect(G_OBJECT (top_btn), "clicked",
514 G_CALLBACK(prefs_filtering_top), NULL);
515 CLAWS_SET_TIP(top_btn,
516 _("Move the selected rule to the top"));
518 #ifndef GENERIC_UMPC
519 page_up_btn = gtk_button_new_with_mnemonic (_("Page u_p"));
520 gtk_button_set_image(GTK_BUTTON(page_up_btn),
521 gtk_image_new_from_icon_name("go-up",GTK_ICON_SIZE_BUTTON));
522 gtk_widget_show (page_up_btn);
523 gtk_box_pack_start (GTK_BOX (btn_vbox), page_up_btn, FALSE, FALSE, 0);
524 g_signal_connect(G_OBJECT (page_up_btn), "clicked",
525 G_CALLBACK(prefs_filtering_page_up), NULL);
526 CLAWS_SET_TIP(page_up_btn,
527 _("Move the selected rule one page up"));
528 #endif
530 up_btn = gtkut_stock_button("go-up", _("_Up"));
531 gtk_widget_show (up_btn);
532 gtk_box_pack_start (GTK_BOX (btn_vbox), up_btn, FALSE, FALSE, 0);
533 g_signal_connect(G_OBJECT (up_btn), "clicked",
534 G_CALLBACK(prefs_filtering_up), NULL);
535 CLAWS_SET_TIP(up_btn,
536 _("Move the selected rule up"));
538 down_btn = gtkut_stock_button("go-down", _("_Down"));
539 gtk_widget_show (down_btn);
540 gtk_box_pack_start (GTK_BOX (btn_vbox), down_btn, FALSE, FALSE, 0);
541 g_signal_connect(G_OBJECT (down_btn), "clicked",
542 G_CALLBACK(prefs_filtering_down), NULL);
543 CLAWS_SET_TIP(down_btn,
544 _("Move the selected rule down"));
546 #ifndef GENERIC_UMPC
547 page_down_btn = gtk_button_new_with_mnemonic (_("Page dow_n"));
548 gtk_button_set_image(GTK_BUTTON(page_down_btn),
549 gtk_image_new_from_icon_name("go-down",GTK_ICON_SIZE_BUTTON));
550 gtk_widget_show (page_down_btn);
551 gtk_box_pack_start (GTK_BOX (btn_vbox), page_down_btn, FALSE, FALSE, 0);
552 g_signal_connect(G_OBJECT (page_down_btn), "clicked",
553 G_CALLBACK(prefs_filtering_page_down), NULL);
554 CLAWS_SET_TIP(page_down_btn,
555 _("Move the selected rule one page down"));
556 #endif
558 bottom_btn = gtkut_stock_button("go-bottom", _("_Bottom"));
559 gtk_widget_show (bottom_btn);
560 gtk_box_pack_start (GTK_BOX (btn_vbox), bottom_btn, FALSE, FALSE, 0);
561 g_signal_connect(G_OBJECT (bottom_btn), "clicked",
562 G_CALLBACK(prefs_filtering_bottom), NULL);
563 CLAWS_SET_TIP(bottom_btn,
564 _("Move the selected rule to the bottom"));
566 if (!geometry.min_height) {
567 geometry.min_width = 500;
568 geometry.min_height = 460;
571 gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
572 GDK_HINT_MIN_SIZE);
573 gtk_window_set_default_size(GTK_WINDOW(window), prefs_common.filteringwin_width,
574 prefs_common.filteringwin_height);
576 gtk_widget_show_all(window);
578 filtering.window = window;
579 filtering.help_btn = help_btn;
580 filtering.ok_btn = ok_btn;
582 filtering.name_entry = name_entry;
583 filtering.cond_entry = cond_entry;
584 filtering.action_entry = action_entry;
585 filtering.cond_list_view = cond_list_view;
586 filtering.account_label = account_label;
589 static void rename_tag(GSList * filters,
590 const gchar * old_tag, const gchar * new_tag);
592 void prefs_filtering_rename_tag(const gchar *old_tag, const gchar *new_tag)
594 GList * cur;
595 const gchar *tags[2] = {NULL, NULL};
596 tags[0] = old_tag;
597 tags[1] = new_tag;
598 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
599 Folder *folder;
600 folder = (Folder *) cur->data;
601 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
602 prefs_filtering_rename_tag_func, tags);
605 rename_tag(pre_global_processing, old_tag, new_tag);
606 rename_tag(post_global_processing, old_tag, new_tag);
607 rename_tag(filtering_rules, old_tag, new_tag);
609 prefs_matcher_write_config();
613 static void rename_path(GSList * filters,
614 const gchar * old_path, const gchar * new_path);
616 void prefs_filtering_rename_path(const gchar *old_path, const gchar *new_path)
618 GList * cur;
619 const gchar *paths[2] = {NULL, NULL};
620 paths[0] = old_path;
621 paths[1] = new_path;
622 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
623 Folder *folder;
624 folder = (Folder *) cur->data;
625 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
626 prefs_filtering_rename_path_func, paths);
629 rename_path(pre_global_processing, old_path, new_path);
630 rename_path(post_global_processing, old_path, new_path);
631 rename_path(filtering_rules, old_path, new_path);
633 prefs_matcher_write_config();
636 static void rename_path(GSList * filters,
637 const gchar * old_path, const gchar * new_path)
639 GSList * cur;
641 for (cur = filters; cur != NULL; cur = cur->next) {
642 FilteringProp *filtering = (FilteringProp *)cur->data;
643 filtering_action_list_rename_path(filtering->action_list,
644 old_path, new_path);
648 static gboolean prefs_filtering_rename_path_func(GNode *node, gpointer data)
650 GSList *filters;
651 const gchar * old_path;
652 const gchar * new_path;
653 const gchar ** paths;
654 FolderItem *item;
656 paths = data;
657 old_path = paths[0];
658 new_path = paths[1];
660 cm_return_val_if_fail(old_path != NULL, FALSE);
661 cm_return_val_if_fail(new_path != NULL, FALSE);
662 cm_return_val_if_fail(node != NULL, FALSE);
664 item = node->data;
665 if (!item || !item->prefs)
666 return FALSE;
667 filters = item->prefs->processing;
669 rename_path(filters, old_path, new_path);
671 return FALSE;
674 static void rename_tag(GSList * filters,
675 const gchar * old_tag, const gchar * new_tag)
677 GSList * action_cur;
678 GSList * cur;
680 for (cur = filters; cur != NULL; cur = cur->next) {
681 FilteringProp *filtering = (FilteringProp *)cur->data;
683 for(action_cur = filtering->action_list ; action_cur != NULL ;
684 action_cur = action_cur->next) {
686 FilteringAction *action = action_cur->data;
688 if (action->type != MATCHACTION_SET_TAG &&
689 action->type != MATCHACTION_UNSET_TAG)
690 continue;
691 if (!action->destination)
692 continue;
693 if (!strcmp(action->destination, old_tag)) {
694 g_free(action->destination);
695 action->destination = g_strdup(new_tag);
701 static gboolean prefs_filtering_rename_tag_func(GNode *node, gpointer data)
703 GSList *filters;
704 const gchar * old_tag;
705 const gchar * new_tag;
706 const gchar ** tags;
707 FolderItem *item;
709 tags = data;
710 old_tag = tags[0];
711 new_tag = tags[1];
713 cm_return_val_if_fail(old_tag != NULL, FALSE);
714 cm_return_val_if_fail(new_tag != NULL, FALSE);
715 cm_return_val_if_fail(node != NULL, FALSE);
717 item = node->data;
718 if (!item || !item->prefs)
719 return FALSE;
720 filters = item->prefs->processing;
722 rename_tag(filters, old_tag, new_tag);
724 return FALSE;
727 void prefs_filtering_delete_path(const gchar *path)
729 GList * cur;
730 for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
731 Folder *folder;
732 folder = (Folder *) cur->data;
733 g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
734 prefs_filtering_delete_path_func, (gchar *)path);
736 delete_path(&pre_global_processing, path);
737 delete_path(&post_global_processing, path);
738 delete_path(&filtering_rules, path);
740 prefs_matcher_write_config();
743 static void delete_path(GSList ** p_filters, const gchar * path)
745 GSList * filters;
746 GSList * duplist;
747 gchar *suffix;
748 gint destlen;
749 gint prefixlen;
750 gint pathlen;
751 GSList * action_cur;
752 GSList * cur;
754 filters = *p_filters;
755 pathlen = strlen(path);
756 duplist = g_slist_copy(filters);
757 for (cur = duplist ; cur != NULL; cur = g_slist_next(cur)) {
758 FilteringProp *filtering = (FilteringProp *) cur->data;
760 for(action_cur = filtering->action_list ; action_cur != NULL ;
761 action_cur = action_cur->next) {
763 FilteringAction *action;
765 action = action_cur->data;
767 if (action->type == MATCHACTION_SET_TAG ||
768 action->type == MATCHACTION_UNSET_TAG)
769 continue;
770 if (!action->destination)
771 continue;
773 destlen = strlen(action->destination);
775 if (destlen > pathlen) {
776 prefixlen = destlen - pathlen;
777 suffix = action->destination + prefixlen;
779 if ((suffix && !strncmp(path, suffix, pathlen)) && filtering) {
780 filters = g_slist_remove(filters, filtering);
781 filteringprop_free(filtering);
782 filtering = NULL;
784 } else if ((strcmp(action->destination, path) == 0) && filtering) {
785 filters = g_slist_remove(filters, filtering);
786 filteringprop_free(filtering);
787 filtering = NULL;
791 g_slist_free(duplist);
793 * p_filters = filters;
796 static gboolean prefs_filtering_delete_path_func(GNode *node, gpointer data)
798 const gchar *path = data;
799 FolderItem *item;
800 GSList ** p_filters;
802 cm_return_val_if_fail(path != NULL, FALSE);
803 cm_return_val_if_fail(node != NULL, FALSE);
805 item = node->data;
806 if (!item || !item->prefs)
807 return FALSE;
808 p_filters = &item->prefs->processing;
810 delete_path(p_filters, path);
812 return FALSE;
815 static void prefs_filtering_clear_list(GtkListStore *list_store)
817 gtk_list_store_clear(list_store);
819 /* add the place holder (New) at row 0 */
820 prefs_filtering_list_view_insert_rule(list_store, -1,
821 FALSE,
822 _("(New)"),
824 _("(New)"),
825 _("(New)"),
826 FALSE);
829 static void prefs_filtering_set_dialog(const gchar *header, const gchar *key)
831 GtkTreeView *list_view = GTK_TREE_VIEW(filtering.cond_list_view);
832 GSList *cur;
833 GSList * prefs_filtering;
834 gchar *cond_str;
835 GtkListStore *list_store;
837 list_store = GTK_LIST_STORE(gtk_tree_view_get_model(list_view));
838 prefs_filtering_clear_list(list_store);
840 prefs_filtering = *p_processing_list;
842 for(cur = prefs_filtering ; cur != NULL ; cur = g_slist_next(cur)) {
843 FilteringProp * prop = (FilteringProp *) cur->data;
844 gchar *account_name = NULL;
846 if (prop->account_id > 0) {
847 PrefsAccount *ac_prefs = account_find_from_id(prop->account_id);
849 if (ac_prefs)
850 account_name = ac_prefs->account_name;
852 if (account_name == NULL)
853 account_name = (gchar *)C_("Filtering Account Menu", "All");
855 cond_str = filteringprop_to_string(prop);
856 subst_char(cond_str, '\t', ':');
858 prefs_filtering_list_view_insert_rule(list_store, -1,
859 prop->enabled,
860 prop->name,
861 prop->account_id,
862 account_name,
863 cond_str, TRUE);
865 g_free(cond_str);
868 prefs_filtering_reset_dialog();
870 if (header && key) {
871 gchar * quoted_key;
872 gchar *match_str;
874 quoted_key = matcher_quote_str(key);
876 match_str = g_strconcat(header, " ", get_matchparser_tab_str(MATCHTYPE_MATCHCASE),
877 " \"", quoted_key, "\"", NULL);
878 g_free(quoted_key);
880 gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), match_str);
881 g_free(match_str);
885 static void prefs_filtering_reset_dialog(void)
887 gtk_entry_set_text(GTK_ENTRY(filtering.name_entry), "");
888 combobox_select_by_data(GTK_COMBO_BOX(filtering.account_combobox), 0);
889 gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), "");
890 gtk_entry_set_text(GTK_ENTRY(filtering.action_entry), "");
893 static gboolean prefs_filtering_search_func_cb (GtkTreeModel *model, gint column, const gchar *key,
894 GtkTreeIter *iter, gpointer search_data)
896 gchar *store_string;
897 gboolean retval;
898 GtkTreePath *path;
900 gtk_tree_model_get (model, iter, column, &store_string, -1);
902 if (!store_string || !key) return FALSE;
905 retval = (g_ascii_strncasecmp (key, store_string, strlen(key)) != 0);
907 g_free(store_string);
908 debug_print("selecting row\n");
909 path = gtk_tree_model_get_path(model, iter);
910 prefs_filtering_select_row(GTK_TREE_VIEW(filtering.cond_list_view), path);
911 gtk_tree_path_free(path);
913 return retval;
916 static void prefs_filtering_set_list(void)
918 gint row = 1;
919 FilteringProp *prop;
920 GSList * cur;
921 gchar * filtering_str;
922 GSList * prefs_filtering;
924 prefs_filtering = *p_processing_list;
925 for (cur = prefs_filtering ; cur != NULL ; cur = g_slist_next(cur))
926 filteringprop_free((FilteringProp *) cur->data);
927 g_slist_free(prefs_filtering);
928 prefs_filtering = NULL;
931 while (NULL != (filtering_str = prefs_filtering_list_view_get_rule
932 (filtering.cond_list_view, row))) {
933 /* FIXME: this strcmp() is bogus: "(New)" should never
934 * be inserted in the storage */
935 if (strcmp(filtering_str, _("(New)")) != 0) {
936 gboolean enabled;
937 gchar *name;
938 gint account_id = 0;
940 prefs_filtering_list_view_get_rule_info(
941 filtering.cond_list_view, row,
942 &enabled, &name, &account_id);
943 prop = matcher_parser_get_filtering(filtering_str);
944 if (prop) {
945 prop->enabled = enabled;
946 if (prop->name != NULL)
947 g_free(prop->name);
948 prop->name = name;
949 prop->account_id = account_id;
950 prefs_filtering =
951 g_slist_append(prefs_filtering, prop);
955 g_free(filtering_str);
956 row++;
959 *p_processing_list = prefs_filtering;
962 static gint prefs_filtering_list_view_set_row(gint row, FilteringProp * prop)
964 GtkTreeView *list_view = GTK_TREE_VIEW(filtering.cond_list_view);
965 gchar *str = NULL;
966 GtkListStore *list_store;
967 gchar *name = NULL;
968 gint account_id = 0;
969 gchar *account_name = (gchar *)C_("Filtering Account Menu", "All");
970 gboolean enabled = TRUE;
972 if (prop)
973 str = filteringprop_to_string(prop);
974 if (str == NULL)
975 return -1;
977 if (prop) {
978 if (prop->name)
979 name = prop->name;
980 account_id = prop->account_id;
981 if (account_id > 0)
982 account_name = account_find_from_id(account_id)->account_name;
983 enabled = prop->enabled;
986 list_store = GTK_LIST_STORE(gtk_tree_view_get_model(list_view));
988 row = prefs_filtering_list_view_insert_rule(list_store, row,
989 enabled,
990 name,
991 account_id,
992 account_name,
993 str,
994 prop != NULL);
996 g_free(str);
998 return row;
1001 static void prefs_filtering_condition_define_done(MatcherList * matchers)
1003 gchar * str;
1005 if (matchers == NULL)
1006 return;
1008 str = matcherlist_to_string(matchers);
1010 if (str != NULL) {
1011 gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), str);
1012 g_free(str);
1016 static void prefs_filtering_condition_define(gpointer action, gpointer data)
1018 gchar * cond_str;
1019 MatcherList * matchers = NULL;
1021 cond_str = gtk_editable_get_chars(GTK_EDITABLE(filtering.cond_entry), 0, -1);
1023 if (*cond_str != '\0') {
1024 matchers = matcher_parser_get_cond(cond_str, NULL);
1025 if (matchers == NULL)
1026 alertpanel_error(_("Condition string is not valid."));
1029 g_free(cond_str);
1031 prefs_matcher_open(matchers, prefs_filtering_condition_define_done);
1033 if (matchers != NULL)
1034 matcherlist_free(matchers);
1037 static void prefs_filtering_action_define_done(GSList * action_list)
1039 gchar * str;
1041 if (action_list == NULL)
1042 return;
1044 str = filteringaction_list_to_string(action_list);
1046 if (str != NULL) {
1047 gtk_entry_set_text(GTK_ENTRY(filtering.action_entry), str);
1048 g_free(str);
1052 static void prefs_filtering_action_define(gpointer action, gpointer data)
1054 gchar * action_str;
1055 GSList * action_list = NULL;
1057 action_str = gtk_editable_get_chars(GTK_EDITABLE(filtering.action_entry), 0, -1);
1059 if (*action_str != '\0') {
1060 action_list = matcher_parser_get_action_list(action_str);
1061 if (action_list == NULL)
1062 alertpanel_error(_("Action string is not valid."));
1065 g_free(action_str);
1067 prefs_filtering_action_open(action_list,
1068 prefs_filtering_action_define_done);
1070 if (action_list != NULL) {
1071 GSList * cur;
1072 for(cur = action_list ; cur != NULL ; cur = cur->next) {
1073 filteringaction_free(cur->data);
1079 /* register / substitute delete buttons */
1082 static FilteringProp * prefs_filtering_dialog_to_filtering(gboolean alert)
1084 MatcherList * cond;
1085 gboolean enabled = TRUE;
1086 gchar * name = NULL;
1087 gint account_id = 0;
1088 gchar * cond_str = NULL;
1089 gchar * action_str = NULL;
1090 FilteringProp * prop = NULL;
1091 GSList * action_list;
1093 name = gtk_editable_get_chars(GTK_EDITABLE(filtering.name_entry), 0, -1);
1095 account_id = combobox_get_active_data(GTK_COMBO_BOX(filtering.account_combobox));
1097 cond_str = gtk_editable_get_chars(GTK_EDITABLE(filtering.cond_entry), 0, -1);
1098 if (*cond_str == '\0') {
1099 if(alert == TRUE) alertpanel_error(_("Condition string is empty."));
1100 goto fail;
1103 action_str = gtk_editable_get_chars(GTK_EDITABLE(filtering.action_entry), 0, -1);
1104 if (*action_str == '\0') {
1105 if(alert == TRUE) alertpanel_error(_("Action string is empty."));
1106 goto fail;
1109 cond = matcher_parser_get_cond(cond_str, NULL);
1111 if (cond == NULL) {
1112 if(alert == TRUE) alertpanel_error(_("Condition string is not valid."));
1113 goto fail;
1116 action_list = matcher_parser_get_action_list(action_str);
1119 if (action_list == NULL) {
1120 if(alert == TRUE) alertpanel_error(_("Action string is not valid."));
1121 goto fail;
1124 prop = filteringprop_new(enabled, name, account_id, cond, action_list);
1126 fail:
1127 g_free(name);
1128 g_free(cond_str);
1129 g_free(action_str);
1130 return prop;
1133 static void prefs_filtering_register_cb(gpointer action, gpointer data)
1135 FilteringProp *prop;
1137 prop = prefs_filtering_dialog_to_filtering(TRUE);
1138 if (prop == NULL)
1139 return;
1140 prefs_filtering_list_view_set_row(-1, prop);
1142 filteringprop_free(prop);
1144 prefs_filtering_reset_dialog();
1145 modified = TRUE;
1148 static void prefs_filtering_substitute_cb(gpointer action, gpointer data)
1150 gint selected_row = gtkut_list_view_get_selected_row
1151 (filtering.cond_list_view);
1152 FilteringProp *prop;
1153 gboolean enabled;
1154 gchar *name;
1155 gint account_id;
1157 if (selected_row <= 0)
1158 return;
1160 prop = prefs_filtering_dialog_to_filtering(TRUE);
1162 if (prop == NULL)
1163 return;
1165 /* prop->emabled is always TRUE here, re-use the value from the selected row
1166 as we don't substitute this value from dialog */
1167 prefs_filtering_list_view_get_rule_info(
1168 filtering.cond_list_view, selected_row,
1169 &enabled, &name, &account_id);
1170 g_free(name); /* We're not using this. */
1171 prop->enabled = enabled;
1173 prefs_filtering_list_view_set_row(selected_row, prop);
1175 filteringprop_free(prop);
1177 prefs_filtering_row_selected(gtk_tree_view_get_selection(
1178 GTK_TREE_VIEW(filtering.cond_list_view)),
1179 GTK_TREE_VIEW(filtering.cond_list_view));
1180 modified = TRUE;
1183 static void prefs_filtering_delete_cb(gpointer action, gpointer data)
1185 GtkTreeView *list_view = GTK_TREE_VIEW(filtering.cond_list_view);
1186 GtkTreeModel *model;
1187 GtkTreeIter iter;
1188 gint selected_row;
1190 selected_row = gtkut_list_view_get_selected_row(filtering.cond_list_view);
1191 if (selected_row <= 0)
1192 return;
1194 if (alertpanel(_("Delete rule"),
1195 _("Do you really want to delete this rule?"),
1196 NULL, _("_Cancel"), "edit-delete", _("D_elete"), NULL, NULL,
1197 ALERTFOCUS_SECOND) == G_ALERTDEFAULT)
1198 return;
1200 model = gtk_tree_view_get_model(list_view);
1201 if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, selected_row))
1202 return;
1204 gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
1206 prefs_filtering_reset_dialog();
1207 modified = TRUE;
1210 static void prefs_filtering_delete_all_cb(gpointer action, gpointer data)
1212 GtkListStore *list_store;
1214 if (alertpanel(_("Delete all rules"),
1215 _("Do you really want to delete all the rules?"),
1216 NULL, _("_Cancel"), "edit-delete", _("D_elete"), NULL, NULL,
1217 ALERTFOCUS_SECOND) == G_ALERTDEFAULT)
1218 return;
1220 list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view)));
1221 prefs_filtering_clear_list(list_store);
1223 prefs_filtering_reset_dialog();
1224 modified = TRUE;
1227 static void prefs_filtering_clear_cb(gpointer action, gpointer data)
1229 prefs_filtering_reset_dialog();
1232 static void prefs_filtering_duplicate_cb(gpointer action, gpointer data)
1234 gint selected_row = gtkut_list_view_get_selected_row
1235 (filtering.cond_list_view);
1236 FilteringProp *prop;
1237 gboolean enabled;
1238 gchar *name;
1239 gint account_id;
1241 if (selected_row <= 0)
1242 return;
1244 prop = prefs_filtering_dialog_to_filtering(TRUE);
1245 if (prop == NULL)
1246 return;
1248 /* prop->emabled is always TRUE here, re-use the value from the selected row
1249 as we don't substitute this value from dialog */
1250 prefs_filtering_list_view_get_rule_info(
1251 filtering.cond_list_view, selected_row,
1252 &enabled, &name, &account_id);
1253 g_free(name); /* We're not using this. */
1254 prop->enabled = enabled;
1256 prefs_filtering_list_view_set_row(-selected_row-2, prop);
1258 filteringprop_free(prop);
1260 prefs_filtering_reset_dialog();
1261 modified = TRUE;
1264 static void prefs_filtering_top(gpointer action, gpointer data)
1266 gint row;
1267 GtkTreeIter top, sel;
1268 GtkTreeModel *model;
1270 row = gtkut_list_view_get_selected_row(filtering.cond_list_view);
1271 if (row <= 1)
1272 return;
1274 model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));
1276 if (!gtk_tree_model_iter_nth_child(model, &top, NULL, 0)
1277 || !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
1278 return;
1280 gtk_list_store_move_after(GTK_LIST_STORE(model), &sel, &top);
1281 gtkut_list_view_select_row(filtering.cond_list_view, 1);
1282 modified = TRUE;
1285 static void prefs_filtering_page_up(gpointer action, gpointer data)
1287 gint row, target_row;
1288 GtkTreeIter selected, target;
1289 GtkTreeModel *model;
1290 GtkTreePath *path;
1291 GdkRectangle cell_rect, view_rect;
1293 model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));
1294 row = gtkut_list_view_get_selected_row(filtering.cond_list_view);
1295 if (row <= 1)
1296 return;
1298 if (!gtk_tree_model_iter_nth_child(model, &selected, NULL, row))
1299 return;
1301 /* compute number of rows per page (approximation) */
1302 path = gtk_tree_model_get_path(model, &selected);
1303 gtk_tree_view_get_cell_area(GTK_TREE_VIEW(filtering.cond_list_view), path, NULL, &cell_rect);
1304 gtk_tree_view_get_visible_rect(GTK_TREE_VIEW(filtering.cond_list_view), &view_rect);
1305 gtk_tree_path_free(path);
1306 target_row = row - (view_rect.height/cell_rect.height);
1307 if (target_row < 1)
1308 target_row = 1;
1310 if (!gtk_tree_model_iter_nth_child(model, &target, NULL, target_row))
1311 return;
1312 gtk_list_store_move_before(GTK_LIST_STORE(model), &selected, &target);
1313 gtkut_list_view_select_row(filtering.cond_list_view, target_row);
1314 modified = TRUE;
1316 static void prefs_filtering_up(gpointer action, gpointer data)
1318 gint row;
1319 GtkTreeIter top, sel;
1320 GtkTreeModel *model;
1322 row = gtkut_list_view_get_selected_row(filtering.cond_list_view);
1323 if (row <= 1)
1324 return;
1326 model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));
1328 if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row - 1)
1329 || !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
1330 return;
1332 gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
1333 gtkut_list_view_select_row(filtering.cond_list_view, row - 1);
1334 modified = TRUE;
1337 static void prefs_filtering_down(gpointer action, gpointer data)
1339 gint row, n_rows;
1340 GtkTreeIter top, sel;
1341 GtkTreeModel *model;
1343 model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));
1344 n_rows = gtk_tree_model_iter_n_children(model, NULL);
1345 row = gtkut_list_view_get_selected_row(filtering.cond_list_view);
1346 if (row < 1 || row >= n_rows - 1)
1347 return;
1349 if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
1350 || !gtk_tree_model_iter_nth_child(model, &sel, NULL, row + 1))
1351 return;
1353 gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
1354 gtkut_list_view_select_row(filtering.cond_list_view, row + 1);
1355 modified = TRUE;
1358 static void prefs_filtering_page_down(gpointer action, gpointer data)
1360 gint row, target_row, n_rows;
1361 GtkTreeIter selected, target;
1362 GtkTreeModel *model;
1363 GtkTreePath *path;
1364 GdkRectangle cell_rect, view_rect;
1366 model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));
1367 n_rows = gtk_tree_model_iter_n_children(model, NULL);
1368 row = gtkut_list_view_get_selected_row(filtering.cond_list_view);
1369 if (row < 1 || row >= n_rows -1)
1370 return;
1372 if (!gtk_tree_model_iter_nth_child(model, &selected, NULL, row))
1373 return;
1375 /* compute number of rows per page (approximation) */
1376 path = gtk_tree_model_get_path(model, &selected);
1377 gtk_tree_view_get_cell_area(GTK_TREE_VIEW(filtering.cond_list_view), path, NULL, &cell_rect);
1378 gtk_tree_view_get_visible_rect(GTK_TREE_VIEW(filtering.cond_list_view), &view_rect);
1379 gtk_tree_path_free(path);
1380 target_row = row + (view_rect.height/cell_rect.height);
1381 if (target_row > n_rows-1)
1382 target_row = n_rows-1;
1384 if (!gtk_tree_model_iter_nth_child(model, &target, NULL, target_row))
1385 return;
1386 gtk_list_store_move_after(GTK_LIST_STORE(model), &selected, &target);
1387 gtkut_list_view_select_row(filtering.cond_list_view, target_row);
1388 modified = TRUE;
1391 static void prefs_filtering_bottom(gpointer action, gpointer data)
1393 gint row, n_rows;
1394 GtkTreeIter top, sel;
1395 GtkTreeModel *model;
1397 model = gtk_tree_view_get_model(GTK_TREE_VIEW(filtering.cond_list_view));
1398 n_rows = gtk_tree_model_iter_n_children(model, NULL);
1399 row = gtkut_list_view_get_selected_row(filtering.cond_list_view);
1400 if (row < 1 || row >= n_rows - 1)
1401 return;
1403 if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
1404 || !gtk_tree_model_iter_nth_child(model, &sel, NULL, n_rows - 1))
1405 return;
1407 gtk_list_store_move_after(GTK_LIST_STORE(model), &top, &sel);
1408 gtkut_list_view_select_row(filtering.cond_list_view, n_rows - 1);
1409 modified = TRUE;
1412 static void prefs_filtering_select_set(FilteringProp *prop)
1414 gchar *matcher_str;
1415 gchar *action_str;
1417 prefs_filtering_reset_dialog();
1419 matcher_str = matcherlist_to_string(prop->matchers);
1420 if (matcher_str == NULL) {
1421 return;
1424 if (prop->name != NULL)
1425 gtk_entry_set_text(GTK_ENTRY(filtering.name_entry), prop->name);
1427 combobox_select_by_data(GTK_COMBO_BOX(filtering.account_combobox), prop->account_id);
1429 gtk_entry_set_text(GTK_ENTRY(filtering.cond_entry), matcher_str);
1431 action_str = filteringaction_list_to_string(prop->action_list);
1432 if (action_str != NULL)
1433 gtk_entry_set_text(GTK_ENTRY(filtering.action_entry), action_str);
1435 g_free(action_str);
1436 g_free(matcher_str);
1439 static gint prefs_filtering_deleted(GtkWidget *widget, GdkEventAny *event,
1440 gpointer data)
1442 prefs_filtering_cancel(NULL, NULL);
1443 return TRUE;
1446 static gboolean prefs_filtering_key_pressed(GtkWidget *widget, GdkEventKey *event,
1447 gpointer data)
1449 if (event && event->keyval == GDK_KEY_Escape) {
1450 prefs_filtering_cancel(NULL, NULL);
1451 return TRUE;
1453 return FALSE;
1456 static gboolean prefs_filtering_check_mod(gboolean check_changed_list)
1458 FilteringProp * prop;
1459 gchar * str;
1460 gchar * filtering_str;
1461 gint row = 1;
1462 AlertValue val;
1464 prop = prefs_filtering_dialog_to_filtering(FALSE);
1466 if (check_changed_list) {
1467 if (modified && alertpanel(_("Filtering rules not saved"),
1468 _("The list of filtering rules have been modified. Close anyway?"),
1469 "window-close", _("_Close"), NULL, _("_Continue editing"),
1470 NULL, NULL, ALERTFOCUS_SECOND) != G_ALERTDEFAULT) {
1471 if (prop != NULL)
1472 filteringprop_free(prop);
1473 return TRUE;
1477 /* check if a rule is being edited */
1478 if (prop != NULL) {
1479 str = filteringprop_to_string(prop);
1481 while (NULL != (filtering_str = (prefs_filtering_list_view_get_rule
1482 (filtering.cond_list_view,
1483 row)))) {
1484 if (strcmp(filtering_str, str) == 0)
1485 break;
1486 row++;
1487 g_free(filtering_str);
1490 if (!filtering_str) {
1491 val = alertpanel(_("Entry not saved"),
1492 _("The entry was not saved. Close anyway?"),
1493 "window-close", _("_Close"), NULL, _("_Continue editing"),
1494 NULL, NULL, ALERTFOCUS_SECOND);
1495 if (G_ALERTDEFAULT != val) {
1496 g_free(filtering_str);
1497 g_free(str); /* fixed two leaks: huzzah! */
1498 filteringprop_free(prop);
1499 return TRUE;
1503 g_free(filtering_str);
1504 g_free(str);
1505 filteringprop_free(prop); /* fixed a leak: huzzah! */
1506 } else {
1507 gchar *name, *condition, *action;
1508 name = gtk_editable_get_chars(GTK_EDITABLE(filtering.name_entry), 0, -1);
1509 condition = gtk_editable_get_chars(GTK_EDITABLE(filtering.cond_entry), 0, -1);
1510 action = gtk_editable_get_chars(GTK_EDITABLE(filtering.action_entry), 0, -1);
1511 if (strlen(name) ||
1512 strlen(condition) ||
1513 strlen(action)) {
1514 val = alertpanel(_("Entry not saved"),
1515 _("The entry was not saved. Close anyway?"),
1516 "window-close", _("_Close"), NULL, _("_Continue editing"),
1517 NULL, NULL, ALERTFOCUS_SECOND);
1518 if (G_ALERTDEFAULT != val) {
1519 g_free(name);
1520 g_free(condition);
1521 g_free(action);
1522 return TRUE;
1525 g_free(name);
1526 g_free(condition);
1527 g_free(action);
1529 return FALSE;
1532 static void prefs_filtering_ok(gpointer action, gpointer data)
1534 if (prefs_filtering_check_mod(FALSE))
1535 return;
1536 modified = FALSE;
1537 prefs_filtering_set_list();
1538 prefs_matcher_write_config();
1539 prefs_filtering_close();
1542 static void prefs_filtering_cancel(gpointer action, gpointer data)
1544 if (prefs_filtering_check_mod(TRUE))
1545 return;
1546 modified = FALSE;
1547 prefs_matcher_read_config();
1548 prefs_filtering_close();
1551 static GtkListStore* prefs_filtering_create_data_store(void)
1553 return gtk_list_store_new(N_PREFS_FILTERING_COLUMNS,
1554 G_TYPE_BOOLEAN,
1555 G_TYPE_STRING,
1556 G_TYPE_INT,
1557 G_TYPE_STRING,
1558 G_TYPE_STRING,
1559 G_TYPE_BOOLEAN,
1560 -1);
1564 *\brief Insert filtering rule into store. Note that we access the
1565 * tree view / store by index, which is a bit suboptimal, but
1566 * at least it made GTK 2 porting easier.
1568 *\param list_store Store to operate on
1569 *\param row -1 to add a new rule to store,
1570 row >=0 to change an existing row
1571 row <-1 insert a new row after (-row-2)
1572 *\param enabled TRUE if rule is enabled
1573 *\param name The Name of rule
1574 *\param account_id The account ID
1575 *\param account_name The account name or All or (New)
1576 *\param rule String representation of rule
1577 *\param prop TRUE if valid filtering rule; if FALSE it's the first
1578 * entry in the store ("(New)").
1580 *\return int Row of inserted / changed rule.
1582 static gint prefs_filtering_list_view_insert_rule(GtkListStore *list_store,
1583 gint row,
1584 gboolean enabled,
1585 const gchar *name,
1586 gint account_id,
1587 const gchar *account_name,
1588 const gchar *rule,
1589 gboolean prop)
1591 GtkTreeIter iter;
1592 GtkTreeIter sibling;
1594 /* check if valid row at all */
1595 if (row >= 0) {
1596 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
1597 &iter, NULL, row))
1598 row = -1;
1599 } else if (row < -1) {
1600 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
1601 &sibling, NULL, -row-2))
1602 row = -1;
1605 if (row == -1 ) {
1606 /* append new */
1607 gtk_list_store_append(list_store, &iter);
1608 gtk_list_store_set(list_store, &iter,
1609 PREFS_FILTERING_ENABLED, enabled,
1610 PREFS_FILTERING_NAME, name,
1611 PREFS_FILTERING_ACCOUNT_ID, account_id,
1612 PREFS_FILTERING_ACCOUNT_NAME, account_name,
1613 PREFS_FILTERING_RULE, rule,
1614 PREFS_FILTERING_PROP, prop,
1615 -1);
1616 return gtk_tree_model_iter_n_children(GTK_TREE_MODEL(list_store),
1617 NULL) - 1;
1618 } else if (row < -1) {
1619 /* duplicate */
1620 gtk_list_store_insert_after(list_store, &iter, &sibling);
1621 gtk_list_store_set(list_store, &iter,
1622 PREFS_FILTERING_ENABLED, enabled,
1623 PREFS_FILTERING_NAME, name,
1624 PREFS_FILTERING_ACCOUNT_ID, account_id,
1625 PREFS_FILTERING_ACCOUNT_NAME, account_name,
1626 PREFS_FILTERING_RULE, rule,
1627 PREFS_FILTERING_PROP, prop,
1628 -1);
1629 return gtk_tree_model_iter_n_children(GTK_TREE_MODEL(list_store),
1630 NULL) - 1;
1631 } else {
1632 /* change existing */
1633 gtk_list_store_set(list_store, &iter,
1634 PREFS_FILTERING_ENABLED, enabled,
1635 PREFS_FILTERING_NAME, name,
1636 PREFS_FILTERING_ACCOUNT_ID, account_id,
1637 PREFS_FILTERING_ACCOUNT_NAME, account_name,
1638 PREFS_FILTERING_RULE, rule,
1639 -1);
1640 return row;
1645 *\return gchar * Rule at specified row - should be freed.
1647 static gchar *prefs_filtering_list_view_get_rule(GtkWidget *list, gint row)
1649 GtkTreeView *list_view = GTK_TREE_VIEW(list);
1650 GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1651 GtkTreeIter iter;
1652 gchar *result;
1654 if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
1655 return NULL;
1657 gtk_tree_model_get(model, &iter,
1658 PREFS_FILTERING_RULE, &result,
1659 -1);
1661 return result;
1664 static void prefs_filtering_list_view_get_rule_info(GtkWidget *list, gint row,
1665 gboolean *enabled, gchar **name, gint *account_id)
1667 GtkTreeView *list_view = GTK_TREE_VIEW(list);
1668 GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1669 GtkTreeIter iter;
1671 *enabled = TRUE;
1672 *name = NULL;
1674 if (gtk_tree_model_iter_nth_child(model, &iter, NULL, row)) {
1675 gtk_tree_model_get(model, &iter,
1676 PREFS_FILTERING_ENABLED, enabled,
1677 PREFS_FILTERING_NAME, name,
1678 PREFS_FILTERING_ACCOUNT_ID, account_id,
1679 -1);
1683 static GtkActionGroup *prefs_filtering_popup_action = NULL;
1684 static GtkWidget *prefs_filtering_popup_menu = NULL;
1686 static GtkActionEntry prefs_filtering_popup_entries[] =
1688 {"PrefsFilteringPopup", NULL, "PrefsFilteringPopup", NULL, NULL, NULL },
1689 {"PrefsFilteringPopup/Delete", NULL, N_("_Delete"), NULL, NULL, G_CALLBACK(prefs_filtering_delete_cb) },
1690 {"PrefsFilteringPopup/DeleteAll", NULL, N_("Delete _all"), NULL, NULL, G_CALLBACK(prefs_filtering_delete_all_cb) },
1691 {"PrefsFilteringPopup/Duplicate", NULL, N_("D_uplicate"), NULL, NULL, G_CALLBACK(prefs_filtering_duplicate_cb) },
1692 #ifdef GENERIC_UMPC
1693 {"PrefsFilteringPopup/---", NULL, "---", NULL, NULL, NULL },
1694 {"PrefsFilteringPopup/PageUp", NULL, N_("Move one page up"), NULL, NULL, G_CALLBACK(prefs_filtering_page_up) },
1695 {"PrefsFilteringPopup/PageDown", NULL, N_("Move one page down"), NULL, NULL, G_CALLBACK(prefs_filtering_page_down) },
1696 #endif
1699 static void prefs_filtering_row_selected(GtkTreeSelection *selection,
1700 GtkTreeView *list_view)
1702 GtkTreePath *path;
1703 GtkTreeIter iter;
1704 GtkTreeModel *model;
1706 if (!gtk_tree_selection_get_selected(selection, &model, &iter))
1707 return;
1709 path = gtk_tree_model_get_path(model, &iter);
1710 prefs_filtering_select_row(list_view, path);
1711 gtk_tree_path_free(path);
1714 static gint prefs_filtering_list_btn_pressed(GtkWidget *widget, GdkEventButton *event,
1715 GtkTreeView *list_view)
1717 if (event) {
1718 /* left- or right-button click */
1719 if (event->button == 1 || event->button == 3) {
1720 GtkTreePath *path = NULL;
1722 if (gtk_tree_view_get_path_at_pos( list_view, event->x, event->y,
1723 &path, NULL, NULL, NULL)) {
1724 prefs_filtering_select_row(list_view, path);
1726 if (path)
1727 gtk_tree_path_free(path);
1730 /* right-button click */
1731 if (event->button == 3) {
1732 GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1733 GtkTreeIter iter;
1734 gboolean non_empty;
1735 gint row;
1737 if (!prefs_filtering_popup_menu) {
1738 prefs_filtering_popup_action = cm_menu_create_action_group("PrefsFilteringPopup", prefs_filtering_popup_entries,
1739 G_N_ELEMENTS(prefs_filtering_popup_entries), (gpointer)list_view);
1740 MENUITEM_ADDUI("/Menus", "PrefsFilteringPopup", "PrefsFilteringPopup", GTK_UI_MANAGER_MENU)
1741 MENUITEM_ADDUI("/Menus/PrefsFilteringPopup", "Delete", "PrefsFilteringPopup/Delete", GTK_UI_MANAGER_MENUITEM)
1742 MENUITEM_ADDUI("/Menus/PrefsFilteringPopup", "DeleteAll", "PrefsFilteringPopup/DeleteAll", GTK_UI_MANAGER_MENUITEM)
1743 MENUITEM_ADDUI("/Menus/PrefsFilteringPopup", "Duplicate", "PrefsFilteringPopup/Duplicate", GTK_UI_MANAGER_MENUITEM)
1744 #ifdef GENERIC_UMPC
1745 MENUITEM_ADDUI("/Menus/PrefsFilteringPopup", "Separator1", "PrefsFilteringPopup/---", GTK_UI_MANAGER_SEPARATOR)
1746 MENUITEM_ADDUI("/Menus/PrefsFilteringPopup", "PageUp", "PrefsFilteringPopup/PageUp", GTK_UI_MANAGER_MENUITEM)
1747 MENUITEM_ADDUI("/Menus/PrefsFilteringPopup", "PageDown", "PrefsFilteringPopup/PageDown", GTK_UI_MANAGER_MENUITEM)
1748 #endif
1749 prefs_filtering_popup_menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
1750 gtk_ui_manager_get_widget(gtkut_ui_manager(), "/Menus/PrefsFilteringPopup")) );
1753 /* grey out some popup menu items if there is no selected row */
1754 row = gtkut_list_view_get_selected_row(GTK_WIDGET(list_view));
1755 cm_menu_set_sensitive("PrefsFilteringPopup/Delete", (row > 0));
1756 cm_menu_set_sensitive("PrefsFilteringPopup/Duplicate", (row > 0));
1758 /* grey out seom popup menu items if there is no row
1759 (not counting the (New) one at row 0) */
1760 non_empty = gtk_tree_model_get_iter_first(model, &iter);
1761 if (non_empty)
1762 non_empty = gtk_tree_model_iter_next(model, &iter);
1763 cm_menu_set_sensitive("PrefsFilteringPopup/DeleteAll", non_empty);
1765 gtk_menu_popup_at_pointer(GTK_MENU(prefs_filtering_popup_menu), NULL);
1768 return FALSE;
1771 static gboolean prefs_filtering_list_popup_menu(GtkWidget *widget, gpointer data)
1773 GtkTreeView *list_view = (GtkTreeView *)data;
1774 GdkEventButton event;
1776 event.button = 3;
1777 event.time = gtk_get_current_event_time();
1779 prefs_filtering_list_btn_pressed(NULL, &event, list_view);
1781 return TRUE;
1785 *\brief Create list view for filtering
1787 static GtkWidget *prefs_filtering_list_view_create(void)
1789 GtkTreeView *list_view;
1790 GtkTreeSelection *selector;
1791 GtkListStore *store = prefs_filtering_create_data_store();
1793 list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(
1794 GTK_TREE_MODEL(store)));
1795 g_object_unref(store);
1796 #ifdef GENERIC_UMPC
1797 g_object_set(list_view, "allow-checkbox-mode", FALSE, NULL);
1798 #endif
1800 g_signal_connect(G_OBJECT(list_view), "popup-menu",
1801 G_CALLBACK(prefs_filtering_list_popup_menu), list_view);
1802 g_signal_connect(G_OBJECT(list_view), "button-press-event",
1803 G_CALLBACK(prefs_filtering_list_btn_pressed), list_view);
1805 gtk_tree_view_set_reorderable(list_view, TRUE);
1807 selector = gtk_tree_view_get_selection(list_view);
1808 gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1809 g_signal_connect(G_OBJECT(selector), "changed",
1810 G_CALLBACK(prefs_filtering_row_selected), list_view);
1812 /* create the columns */
1813 prefs_filtering_create_list_view_columns(GTK_WIDGET(list_view));
1815 return GTK_WIDGET(list_view);
1818 static void prefs_filtering_enable_toggled(GtkCellRendererToggle *widget,
1819 gchar *path,
1820 GtkWidget *list_view)
1822 GtkTreeIter iter;
1823 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list_view));
1824 gboolean enabled = TRUE;
1826 if (!gtk_tree_model_get_iter_from_string(model, &iter, path))
1827 return;
1829 gtk_tree_model_get(model, &iter,
1830 PREFS_FILTERING_ENABLED, &enabled,
1831 -1);
1833 gtk_list_store_set(GTK_LIST_STORE(model), &iter,
1834 PREFS_FILTERING_ENABLED, !enabled,
1835 -1);
1838 static void prefs_filtering_create_list_view_columns(GtkWidget *list_view)
1840 GtkTreeViewColumn *column;
1841 GtkCellRenderer *renderer;
1843 renderer = gtk_cell_renderer_toggle_new();
1844 g_object_set(renderer,
1845 "radio", FALSE,
1846 "activatable", TRUE,
1847 NULL);
1848 column = gtk_tree_view_column_new_with_attributes
1849 (_("Enable"), /* FIXME : Enable, Enabled, or 'E' ? */
1850 renderer,
1851 "active", PREFS_FILTERING_ENABLED,
1852 NULL);
1853 gtk_tree_view_column_set_alignment (column, 0.5);
1854 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1855 g_signal_connect(G_OBJECT(renderer), "toggled",
1856 G_CALLBACK(prefs_filtering_enable_toggled),
1857 list_view);
1859 renderer = gtk_cell_renderer_text_new();
1860 column = gtk_tree_view_column_new_with_attributes
1861 (_("Name"),
1862 renderer,
1863 "text", PREFS_FILTERING_NAME,
1864 NULL);
1865 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1866 gtk_tree_view_column_set_resizable(column, TRUE);
1868 renderer = gtk_cell_renderer_text_new();
1869 column = gtk_tree_view_column_new_with_attributes
1870 (_("Account"),
1871 renderer,
1872 "text", PREFS_FILTERING_ACCOUNT_NAME,
1873 NULL);
1874 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1875 gtk_tree_view_column_set_resizable(column, TRUE);
1877 filtering.account_name_column = column;
1879 renderer = gtk_cell_renderer_text_new();
1880 column = gtk_tree_view_column_new_with_attributes
1881 (_("Rule"),
1882 renderer,
1883 "text", PREFS_FILTERING_RULE,
1884 NULL);
1886 gtk_tree_view_set_search_column(GTK_TREE_VIEW(list_view), PREFS_FILTERING_NAME);
1887 gtk_tree_view_set_search_equal_func(GTK_TREE_VIEW(list_view), prefs_filtering_search_func_cb , NULL, NULL);
1889 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1893 *\brief Triggered when a row has to be selected
1895 static void prefs_filtering_select_row(GtkTreeView *list_view, GtkTreePath *path)
1897 GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1899 if (path && model) {
1900 GtkTreeSelection *selection;
1901 gboolean has_prop = FALSE;
1902 GtkTreeIter iter;
1904 /* select row */
1905 selection = gtk_tree_view_get_selection(list_view);
1906 gtk_tree_selection_select_path(selection, path);
1908 /* update dialog from selection */
1909 gtk_tree_model_get_iter(model, &iter, path);
1910 gtk_tree_model_get(model, &iter,
1911 PREFS_FILTERING_PROP, &has_prop,
1912 -1);
1914 if (has_prop) {
1915 FilteringProp *prop;
1916 gchar *filtering_str = NULL;
1917 gchar *name = NULL;
1918 gint account_id = 0;
1920 gtk_tree_model_get(model, &iter,
1921 PREFS_FILTERING_RULE, &filtering_str,
1922 PREFS_FILTERING_NAME, &name,
1923 PREFS_FILTERING_ACCOUNT_ID, &account_id,
1924 -1);
1926 prop = matcher_parser_get_filtering(filtering_str);
1927 if (prop) {
1928 if (prop->name != NULL)
1929 g_free(prop->name);
1930 prop->name = g_strdup(name);
1931 prop->account_id = account_id;
1932 prefs_filtering_select_set(prop);
1933 filteringprop_free(prop);
1935 g_free(name);
1936 g_free(filtering_str);
1937 } else
1938 prefs_filtering_reset_dialog();