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/>.
21 #include "claws-features.h"
27 #include <glib/gi18n.h>
29 #include <gdk/gdkkeysyms.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"
46 #include "alertpanel.h"
48 #include "folder_item_prefs.h"
49 #include "filtering.h"
50 #include "addr_compl.h"
56 #include "matcher_parser.h"
58 #include "prefs_filtering_action.h"
61 PREFS_FILTERING_ENABLED
,
63 PREFS_FILTERING_ACCOUNT_ID
,
64 PREFS_FILTERING_ACCOUNT_NAME
,
67 N_PREFS_FILTERING_COLUMNS
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
,
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
,
119 static void prefs_filtering_row_selected(GtkTreeSelection
*selection
,
120 GtkTreeView
*list_view
);
121 static gboolean
prefs_filtering_key_pressed(GtkWidget
*widget
,
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
,
145 const gchar
*account_name
,
148 static gchar
*prefs_filtering_list_view_get_rule (GtkWidget
*list
,
150 static void prefs_filtering_list_view_get_rule_info (GtkWidget
*list
,
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
,
169 const gchar
*help_url_anchor
,
172 gboolean per_account_filtering
)
174 if (prefs_rc_is_readonly(FILTERING_RC
))
179 if (!filtering
.window
) {
180 prefs_filtering_create();
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
);
193 gtk_window_set_title(GTK_WINDOW(filtering
.window
), title
);
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
),
205 signal_id
= g_signal_connect(G_OBJECT(filtering
.help_btn
),
207 G_CALLBACK(manual_open_with_anchor_cb
),
208 (gchar
*)help_url_anchor
);
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
);
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)
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
);
256 static void prefs_filtering_account_option_menu_populate(void)
258 GList
*accounts
= NULL
;
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();
292 static void prefs_filtering_create(void)
297 GtkWidget
*cancel_btn
;
299 GtkWidget
*confirm_area
;
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
;
313 GtkWidget
*action_label
;
314 GtkWidget
*action_entry
;
315 GtkWidget
*action_btn
;
318 GtkWidget
*subst_btn
;
320 GtkWidget
*clear_btn
;
322 GtkWidget
*cond_hbox
;
323 GtkWidget
*cond_scrolledwin
;
324 GtkWidget
*cond_list_view
;
331 GtkWidget
*page_up_btn
;
332 GtkWidget
*page_down_btn
;
334 GtkWidget
*bottom_btn
;
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"),
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
),
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
),
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
),
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
,
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"));
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"));
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"));
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"));
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
,
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
)
595 const gchar
*tags
[2] = {NULL
, NULL
};
598 for (cur
= folder_get_list() ; cur
!= NULL
; cur
= g_list_next(cur
)) {
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
)
619 const gchar
*paths
[2] = {NULL
, NULL
};
622 for (cur
= folder_get_list() ; cur
!= NULL
; cur
= g_list_next(cur
)) {
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
)
641 for (cur
= filters
; cur
!= NULL
; cur
= cur
->next
) {
642 FilteringProp
*filtering
= (FilteringProp
*)cur
->data
;
643 filtering_action_list_rename_path(filtering
->action_list
,
648 static gboolean
prefs_filtering_rename_path_func(GNode
*node
, gpointer data
)
651 const gchar
* old_path
;
652 const gchar
* new_path
;
653 const gchar
** paths
;
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
);
665 if (!item
|| !item
->prefs
)
667 filters
= item
->prefs
->processing
;
669 rename_path(filters
, old_path
, new_path
);
674 static void rename_tag(GSList
* filters
,
675 const gchar
* old_tag
, const gchar
* new_tag
)
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
)
691 if (!action
->destination
)
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
)
704 const gchar
* old_tag
;
705 const gchar
* new_tag
;
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
);
718 if (!item
|| !item
->prefs
)
720 filters
= item
->prefs
->processing
;
722 rename_tag(filters
, old_tag
, new_tag
);
727 void prefs_filtering_delete_path(const gchar
*path
)
730 for (cur
= folder_get_list() ; cur
!= NULL
; cur
= g_list_next(cur
)) {
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
)
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
)
770 if (!action
->destination
)
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
);
784 } else if ((strcmp(action
->destination
, path
) == 0) && filtering
) {
785 filters
= g_slist_remove(filters
, filtering
);
786 filteringprop_free(filtering
);
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
;
802 cm_return_val_if_fail(path
!= NULL
, FALSE
);
803 cm_return_val_if_fail(node
!= NULL
, FALSE
);
806 if (!item
|| !item
->prefs
)
808 p_filters
= &item
->prefs
->processing
;
810 delete_path(p_filters
, path
);
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,
829 static void prefs_filtering_set_dialog(const gchar
*header
, const gchar
*key
)
831 GtkTreeView
*list_view
= GTK_TREE_VIEW(filtering
.cond_list_view
);
833 GSList
* prefs_filtering
;
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
);
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,
868 prefs_filtering_reset_dialog();
874 quoted_key
= matcher_quote_str(key
);
876 match_str
= g_strconcat(header
, " ", get_matchparser_tab_str(MATCHTYPE_MATCHCASE
),
877 " \"", quoted_key
, "\"", NULL
);
880 gtk_entry_set_text(GTK_ENTRY(filtering
.cond_entry
), 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
)
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
);
916 static void prefs_filtering_set_list(void)
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) {
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
);
945 prop
->enabled
= enabled
;
946 if (prop
->name
!= NULL
)
949 prop
->account_id
= account_id
;
951 g_slist_append(prefs_filtering
, prop
);
955 g_free(filtering_str
);
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
);
966 GtkListStore
*list_store
;
969 gchar
*account_name
= (gchar
*)C_("Filtering Account Menu", "All");
970 gboolean enabled
= TRUE
;
973 str
= filteringprop_to_string(prop
);
980 account_id
= prop
->account_id
;
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
,
1001 static void prefs_filtering_condition_define_done(MatcherList
* matchers
)
1005 if (matchers
== NULL
)
1008 str
= matcherlist_to_string(matchers
);
1011 gtk_entry_set_text(GTK_ENTRY(filtering
.cond_entry
), str
);
1016 static void prefs_filtering_condition_define(gpointer action
, gpointer data
)
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."));
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
)
1041 if (action_list
== NULL
)
1044 str
= filteringaction_list_to_string(action_list
);
1047 gtk_entry_set_text(GTK_ENTRY(filtering
.action_entry
), str
);
1052 static void prefs_filtering_action_define(gpointer action
, gpointer data
)
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."));
1067 prefs_filtering_action_open(action_list
,
1068 prefs_filtering_action_define_done
);
1070 if (action_list
!= NULL
) {
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
)
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."));
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."));
1109 cond
= matcher_parser_get_cond(cond_str
, NULL
);
1112 if(alert
== TRUE
) alertpanel_error(_("Condition string is not valid."));
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."));
1124 prop
= filteringprop_new(enabled
, name
, account_id
, cond
, action_list
);
1133 static void prefs_filtering_register_cb(gpointer action
, gpointer data
)
1135 FilteringProp
*prop
;
1137 prop
= prefs_filtering_dialog_to_filtering(TRUE
);
1140 prefs_filtering_list_view_set_row(-1, prop
);
1142 filteringprop_free(prop
);
1144 prefs_filtering_reset_dialog();
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
;
1157 if (selected_row
<= 0)
1160 prop
= prefs_filtering_dialog_to_filtering(TRUE
);
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
));
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
;
1190 selected_row
= gtkut_list_view_get_selected_row(filtering
.cond_list_view
);
1191 if (selected_row
<= 0)
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
)
1200 model
= gtk_tree_view_get_model(list_view
);
1201 if (!gtk_tree_model_iter_nth_child(model
, &iter
, NULL
, selected_row
))
1204 gtk_list_store_remove(GTK_LIST_STORE(model
), &iter
);
1206 prefs_filtering_reset_dialog();
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
)
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();
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
;
1241 if (selected_row
<= 0)
1244 prop
= prefs_filtering_dialog_to_filtering(TRUE
);
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();
1264 static void prefs_filtering_top(gpointer action
, gpointer data
)
1267 GtkTreeIter top
, sel
;
1268 GtkTreeModel
*model
;
1270 row
= gtkut_list_view_get_selected_row(filtering
.cond_list_view
);
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
))
1280 gtk_list_store_move_after(GTK_LIST_STORE(model
), &sel
, &top
);
1281 gtkut_list_view_select_row(filtering
.cond_list_view
, 1);
1285 static void prefs_filtering_page_up(gpointer action
, gpointer data
)
1287 gint row
, target_row
;
1288 GtkTreeIter selected
, target
;
1289 GtkTreeModel
*model
;
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
);
1298 if (!gtk_tree_model_iter_nth_child(model
, &selected
, NULL
, row
))
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
);
1310 if (!gtk_tree_model_iter_nth_child(model
, &target
, NULL
, target_row
))
1312 gtk_list_store_move_before(GTK_LIST_STORE(model
), &selected
, &target
);
1313 gtkut_list_view_select_row(filtering
.cond_list_view
, target_row
);
1316 static void prefs_filtering_up(gpointer action
, gpointer data
)
1319 GtkTreeIter top
, sel
;
1320 GtkTreeModel
*model
;
1322 row
= gtkut_list_view_get_selected_row(filtering
.cond_list_view
);
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
))
1332 gtk_list_store_swap(GTK_LIST_STORE(model
), &top
, &sel
);
1333 gtkut_list_view_select_row(filtering
.cond_list_view
, row
- 1);
1337 static void prefs_filtering_down(gpointer action
, gpointer data
)
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)
1349 if (!gtk_tree_model_iter_nth_child(model
, &top
, NULL
, row
)
1350 || !gtk_tree_model_iter_nth_child(model
, &sel
, NULL
, row
+ 1))
1353 gtk_list_store_swap(GTK_LIST_STORE(model
), &top
, &sel
);
1354 gtkut_list_view_select_row(filtering
.cond_list_view
, row
+ 1);
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
;
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)
1372 if (!gtk_tree_model_iter_nth_child(model
, &selected
, NULL
, row
))
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
))
1386 gtk_list_store_move_after(GTK_LIST_STORE(model
), &selected
, &target
);
1387 gtkut_list_view_select_row(filtering
.cond_list_view
, target_row
);
1391 static void prefs_filtering_bottom(gpointer action
, gpointer data
)
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)
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))
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);
1412 static void prefs_filtering_select_set(FilteringProp
*prop
)
1417 prefs_filtering_reset_dialog();
1419 matcher_str
= matcherlist_to_string(prop
->matchers
);
1420 if (matcher_str
== NULL
) {
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
);
1436 g_free(matcher_str
);
1439 static gint
prefs_filtering_deleted(GtkWidget
*widget
, GdkEventAny
*event
,
1442 prefs_filtering_cancel(NULL
, NULL
);
1446 static gboolean
prefs_filtering_key_pressed(GtkWidget
*widget
, GdkEventKey
*event
,
1449 if (event
&& event
->keyval
== GDK_KEY_Escape
) {
1450 prefs_filtering_cancel(NULL
, NULL
);
1456 static gboolean
prefs_filtering_check_mod(gboolean check_changed_list
)
1458 FilteringProp
* prop
;
1460 gchar
* filtering_str
;
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
) {
1472 filteringprop_free(prop
);
1477 /* check if a rule is being edited */
1479 str
= filteringprop_to_string(prop
);
1481 while (NULL
!= (filtering_str
= (prefs_filtering_list_view_get_rule
1482 (filtering
.cond_list_view
,
1484 if (strcmp(filtering_str
, str
) == 0)
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
);
1503 g_free(filtering_str
);
1505 filteringprop_free(prop
); /* fixed a leak: huzzah! */
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);
1512 strlen(condition
) ||
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
) {
1532 static void prefs_filtering_ok(gpointer action
, gpointer data
)
1534 if (prefs_filtering_check_mod(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
))
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
,
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
,
1587 const gchar
*account_name
,
1592 GtkTreeIter sibling
;
1594 /* check if valid row at all */
1596 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store
),
1599 } else if (row
< -1) {
1600 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store
),
1601 &sibling
, NULL
, -row
-2))
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
,
1616 return gtk_tree_model_iter_n_children(GTK_TREE_MODEL(list_store
),
1618 } else if (row
< -1) {
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
,
1629 return gtk_tree_model_iter_n_children(GTK_TREE_MODEL(list_store
),
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
,
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
);
1654 if (!gtk_tree_model_iter_nth_child(model
, &iter
, NULL
, row
))
1657 gtk_tree_model_get(model
, &iter
,
1658 PREFS_FILTERING_RULE
, &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
);
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
,
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
) },
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
) },
1699 static void prefs_filtering_row_selected(GtkTreeSelection
*selection
,
1700 GtkTreeView
*list_view
)
1704 GtkTreeModel
*model
;
1706 if (!gtk_tree_selection_get_selected(selection
, &model
, &iter
))
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
)
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
);
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
);
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
)
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
)
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
);
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
);
1771 static gboolean
prefs_filtering_list_popup_menu(GtkWidget
*widget
, gpointer data
)
1773 GtkTreeView
*list_view
= (GtkTreeView
*)data
;
1774 GdkEventButton event
;
1777 event
.time
= gtk_get_current_event_time();
1779 prefs_filtering_list_btn_pressed(NULL
, &event
, list_view
);
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
);
1797 g_object_set(list_view
, "allow-checkbox-mode", FALSE
, NULL
);
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
,
1820 GtkWidget
*list_view
)
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
))
1829 gtk_tree_model_get(model
, &iter
,
1830 PREFS_FILTERING_ENABLED
, &enabled
,
1833 gtk_list_store_set(GTK_LIST_STORE(model
), &iter
,
1834 PREFS_FILTERING_ENABLED
, !enabled
,
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
,
1846 "activatable", TRUE
,
1848 column
= gtk_tree_view_column_new_with_attributes
1849 (_("Enable"), /* FIXME : Enable, Enabled, or 'E' ? */
1851 "active", PREFS_FILTERING_ENABLED
,
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
),
1859 renderer
= gtk_cell_renderer_text_new();
1860 column
= gtk_tree_view_column_new_with_attributes
1863 "text", PREFS_FILTERING_NAME
,
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
1872 "text", PREFS_FILTERING_ACCOUNT_NAME
,
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
1883 "text", PREFS_FILTERING_RULE
,
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
;
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
,
1915 FilteringProp
*prop
;
1916 gchar
*filtering_str
= 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
,
1926 prop
= matcher_parser_get_filtering(filtering_str
);
1928 if (prop
->name
!= NULL
)
1930 prop
->name
= g_strdup(name
);
1931 prop
->account_id
= account_id
;
1932 prefs_filtering_select_set(prop
);
1933 filteringprop_free(prop
);
1936 g_free(filtering_str
);
1938 prefs_filtering_reset_dialog();