2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2013 Hiroyuki Yamamoto and the Claws Mail team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 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/>.
22 #include "claws-features.h"
28 #include <glib/gi18n.h>
30 #include <gdk/gdkkeysyms.h>
37 #include "prefs_gtk.h"
38 #include "prefs_matcher.h"
39 #include "prefs_filtering.h"
40 #include "prefs_common.h"
41 #include "mainwindow.h"
42 #include "foldersel.h"
43 #include "manage_window.h"
47 #include "alertpanel.h"
49 #include "folder_item_prefs.h"
50 #include "filtering.h"
51 #include "addr_compl.h"
57 #include "matcher_parser.h"
59 #include "prefs_filtering_action.h"
62 PREFS_FILTERING_ENABLED
,
64 PREFS_FILTERING_ACCOUNT_ID
,
65 PREFS_FILTERING_ACCOUNT_NAME
,
68 N_PREFS_FILTERING_COLUMNS
76 GtkWidget
*name_entry
;
77 GtkWidget
*account_label
;
78 GtkWidget
*account_combobox
;
79 GtkListStore
*account_combobox_list
;
80 GtkWidget
*cond_entry
;
81 GtkWidget
*action_entry
;
83 GtkWidget
*cond_list_view
;
85 GtkTreeViewColumn
*account_name_column
;
88 typedef struct _Filtering Filtering
;
90 static Filtering filtering
;
92 static GSList
** p_processing_list
= NULL
;
94 /* widget creating functions */
95 static void prefs_filtering_create (void);
97 static void prefs_filtering_set_dialog (const gchar
*header
,
99 static void prefs_filtering_set_list (void);
101 /* callback functions */
102 static gboolean
prefs_filtering_search_func_cb (GtkTreeModel
*model
, gint column
,
103 const gchar
*key
, GtkTreeIter
*iter
,
104 gpointer search_data
);
105 static void prefs_filtering_register_cb (gpointer action
, gpointer data
);
106 static void prefs_filtering_substitute_cb (gpointer action
, gpointer data
);
107 static void prefs_filtering_delete_cb (gpointer action
, gpointer data
);
108 static void prefs_filtering_delete_all_cb(gpointer action
, gpointer data
);
109 static void prefs_filtering_clear_cb(gpointer action
, gpointer data
);
110 static void prefs_filtering_duplicate_cb(gpointer action
, gpointer data
);
111 static void prefs_filtering_top (gpointer action
, gpointer data
);
112 static void prefs_filtering_page_up (gpointer action
, gpointer data
);
113 static void prefs_filtering_up (gpointer action
, gpointer data
);
114 static void prefs_filtering_down (gpointer action
, gpointer data
);
115 static void prefs_filtering_page_down (gpointer action
, gpointer data
);
116 static void prefs_filtering_bottom (gpointer action
, gpointer data
);
117 static gint
prefs_filtering_deleted (GtkWidget
*widget
,
120 static void prefs_filtering_row_selected(GtkTreeSelection
*selection
,
121 GtkTreeView
*list_view
);
122 static gboolean
prefs_filtering_key_pressed(GtkWidget
*widget
,
125 static void prefs_filtering_cancel (gpointer action
, gpointer data
);
126 static void prefs_filtering_ok (gpointer action
, gpointer data
);
128 static void prefs_filtering_condition_define (gpointer action
, gpointer data
);
129 static void prefs_filtering_action_define (gpointer action
, gpointer data
);
130 static gint
prefs_filtering_list_view_set_row (gint row
, FilteringProp
* prop
);
132 static void prefs_filtering_reset_dialog (void);
133 static gboolean
prefs_filtering_rename_tag_func(GNode
*node
, gpointer data
);
134 static gboolean
prefs_filtering_rename_path_func(GNode
*node
, gpointer data
);
135 static gboolean
prefs_filtering_delete_path_func(GNode
*node
, gpointer data
);
137 static void delete_path(GSList
** p_filters
, const gchar
* path
);
140 static GtkListStore
* prefs_filtering_create_data_store (void);
141 static gint
prefs_filtering_list_view_insert_rule (GtkListStore
*list_store
,
146 const gchar
*account_name
,
149 static gchar
*prefs_filtering_list_view_get_rule (GtkWidget
*list
,
151 static void prefs_filtering_list_view_get_rule_info (GtkWidget
*list
,
157 static GtkWidget
*prefs_filtering_list_view_create (void);
158 static void prefs_filtering_create_list_view_columns (GtkWidget
*list_view
);
160 static void prefs_filtering_select_row(GtkTreeView
*list_view
, GtkTreePath
*path
);
162 static void prefs_filtering_account_option_menu_populate(void);
164 static gulong signal_id
= 0; /* filtering.help_btn clicked signal */
166 static int modified
= FALSE
;
168 void prefs_filtering_open(GSList
** p_processing
,
170 const gchar
*help_url_anchor
,
173 gboolean per_account_filtering
)
175 if (prefs_rc_is_readonly(FILTERING_RC
))
180 if (!filtering
.window
) {
181 prefs_filtering_create();
183 gtk_list_store_clear(filtering
.account_combobox_list
);
184 prefs_filtering_account_option_menu_populate();
187 gtk_tree_view_column_set_visible(filtering
.account_name_column
,
188 per_account_filtering
);
190 manage_window_set_transient(GTK_WINDOW(filtering
.window
));
191 gtk_widget_grab_focus(filtering
.ok_btn
);
194 gtk_window_set_title(GTK_WINDOW(filtering
.window
), title
);
196 gtk_window_set_title (GTK_WINDOW(filtering
.window
),
197 _("Filtering/Processing configuration"));
199 if (help_url_anchor
!= NULL
) {
200 if (signal_id
!= 0) {
201 g_signal_handler_disconnect(
202 G_OBJECT(filtering
.help_btn
),
206 signal_id
= g_signal_connect(G_OBJECT(filtering
.help_btn
),
208 G_CALLBACK(manual_open_with_anchor_cb
),
209 (gchar
*)help_url_anchor
);
212 gtk_widget_set_sensitive(filtering
.help_btn
, FALSE
);
215 p_processing_list
= p_processing
;
217 prefs_filtering_set_dialog(header
, key
);
218 if (per_account_filtering
) {
219 gtk_widget_show(filtering
.account_label
);
220 gtk_widget_show(filtering
.account_combobox
);
222 gtk_widget_hide(filtering
.account_label
);
223 gtk_widget_hide(filtering
.account_combobox
);
224 combobox_select_by_data(GTK_COMBO_BOX(filtering
.account_combobox
), 0);
227 gtk_widget_show(filtering
.window
);
228 gtk_window_set_modal(GTK_WINDOW(filtering
.window
), TRUE
);
230 start_address_completion(NULL
);
233 static void prefs_filtering_size_allocate_cb(GtkWidget
*widget
,
234 GtkAllocation
*allocation
)
236 cm_return_if_fail(allocation
!= NULL
);
238 prefs_common
.filteringwin_width
= allocation
->width
;
239 prefs_common
.filteringwin_height
= allocation
->height
;
242 /* prefs_filtering_close() - just to have one common exit point */
243 static void prefs_filtering_close(void)
247 end_address_completion();
249 store
= GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
250 (filtering
.cond_list_view
)));
251 gtk_list_store_clear(store
);
252 gtk_widget_hide(filtering
.window
);
253 gtk_window_set_modal(GTK_WINDOW(filtering
.window
), FALSE
);
257 static void prefs_filtering_account_option_menu_populate(void)
259 GList
*accounts
= NULL
;
262 accounts
= account_get_list();
264 cm_return_if_fail(accounts
!= NULL
);
266 COMBOBOX_ADD(filtering
.account_combobox_list
, C_("Filtering Account Menu", "All"), 0);
267 COMBOBOX_ADD(filtering
.account_combobox_list
, NULL
, 0);
268 for (; accounts
!= NULL
; accounts
= accounts
->next
) {
269 PrefsAccount
*ac
= (PrefsAccount
*)accounts
->data
;
271 COMBOBOX_ADD_ESCAPED(filtering
.account_combobox_list
, ac
->account_name
, ac
->account_id
);
275 static GtkWidget
*prefs_filtering_account_option_menu(Filtering
*filtering
)
277 GtkWidget
*optmenu
= NULL
;
278 GtkWidget
*optmenubox
= NULL
;
279 GtkListStore
*menu
= NULL
;
281 optmenubox
= gtk_event_box_new();
282 optmenu
= gtkut_sc_combobox_create(optmenubox
, FALSE
);
283 menu
= GTK_LIST_STORE(gtk_combo_box_get_model(GTK_COMBO_BOX(optmenu
)));
285 filtering
->account_combobox
= optmenu
;
286 filtering
->account_combobox_list
= menu
;
288 prefs_filtering_account_option_menu_populate();
293 static void prefs_filtering_create(void)
298 GtkWidget
*cancel_btn
;
300 GtkWidget
*confirm_area
;
307 GtkWidget
*name_label
;
308 GtkWidget
*name_entry
;
309 GtkWidget
*account_label
;
310 GtkWidget
*account_opt_menu
;
311 GtkWidget
*cond_label
;
312 GtkWidget
*cond_entry
;
314 GtkWidget
*action_label
;
315 GtkWidget
*action_entry
;
316 GtkWidget
*action_btn
;
319 GtkWidget
*subst_btn
;
321 GtkWidget
*clear_btn
;
323 GtkWidget
*cond_hbox
;
324 GtkWidget
*cond_scrolledwin
;
325 GtkWidget
*cond_list_view
;
332 GtkWidget
*page_up_btn
;
333 GtkWidget
*page_down_btn
;
335 GtkWidget
*bottom_btn
;
337 static GdkGeometry geometry
;
339 debug_print("Creating filtering configuration window...\n");
341 window
= gtkut_window_new(GTK_WINDOW_TOPLEVEL
, "prefs_filtering");
342 gtk_container_set_border_width (GTK_CONTAINER (window
), 8);
343 gtk_window_set_position (GTK_WINDOW (window
), GTK_WIN_POS_CENTER
);
344 gtk_window_set_resizable(GTK_WINDOW (window
), TRUE
);
346 vbox
= gtk_vbox_new (FALSE
, 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
, GTK_STOCK_CANCEL
,
352 &ok_btn
, GTK_STOCK_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_vbox_new (FALSE
, 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_table_new(4, 3, FALSE
);
379 gtk_table_set_row_spacings (GTK_TABLE (table
), VSPACING_NARROW_2
);
380 gtk_table_set_col_spacings (GTK_TABLE (table
), 4);
381 gtk_widget_show(table
);
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_misc_set_alignment (GTK_MISC (name_label
), 1, 0.5);
387 gtk_table_attach (GTK_TABLE (table
), name_label
, 0, 1, 0, 1,
388 (GtkAttachOptions
) (GTK_FILL
),
389 (GtkAttachOptions
) (0), 0, 0);
391 name_entry
= gtk_entry_new ();
392 gtk_widget_show (name_entry
);
393 gtk_table_attach (GTK_TABLE (table
), name_entry
, 1, 2, 0, 1,
394 (GtkAttachOptions
) (GTK_FILL
|GTK_EXPAND
),
395 (GtkAttachOptions
) (0), 0, 0);
397 account_label
= gtk_label_new (_("Account"));
398 gtk_widget_show (account_label
);
399 gtk_misc_set_alignment (GTK_MISC (account_label
), 1, 0.5);
400 gtk_table_attach (GTK_TABLE (table
), account_label
, 0, 1, 1, 2,
401 (GtkAttachOptions
) (GTK_FILL
),
402 (GtkAttachOptions
) (0), 0, 0);
404 account_opt_menu
= prefs_filtering_account_option_menu(&filtering
);
405 gtk_widget_show (account_opt_menu
);
406 gtk_table_attach (GTK_TABLE (table
), account_opt_menu
, 1, 2, 1, 2,
407 (GtkAttachOptions
) (GTK_FILL
|GTK_EXPAND
),
408 (GtkAttachOptions
) (0), 0, 0);
409 combobox_select_by_data(GTK_COMBO_BOX(filtering
.account_combobox
), 0);
411 cond_label
= gtk_label_new (_("Condition"));
412 gtk_widget_show (cond_label
);
413 gtk_misc_set_alignment (GTK_MISC (cond_label
), 1, 0.5);
414 gtk_table_attach (GTK_TABLE (table
), cond_label
, 0, 1, 2, 3,
415 (GtkAttachOptions
) (GTK_FILL
),
416 (GtkAttachOptions
) (0), 0, 0);
418 cond_entry
= gtk_entry_new ();
419 gtk_widget_show (cond_entry
);
420 gtk_table_attach (GTK_TABLE (table
), cond_entry
, 1, 2, 2, 3,
421 (GtkAttachOptions
) (GTK_FILL
|GTK_EXPAND
),
422 (GtkAttachOptions
) (0), 0, 0);
424 cond_btn
= gtk_button_new_with_mnemonic (_(" Def_ine... "));
425 gtk_widget_show (cond_btn
);
426 gtk_table_attach (GTK_TABLE (table
), cond_btn
, 2, 3, 2, 3,
427 (GtkAttachOptions
) (GTK_FILL
),
428 (GtkAttachOptions
) (0), 2, 2);
429 g_signal_connect(G_OBJECT (cond_btn
), "clicked",
430 G_CALLBACK(prefs_filtering_condition_define
),
433 action_label
= gtk_label_new (_("Action"));
434 gtk_widget_show (action_label
);
435 gtk_misc_set_alignment (GTK_MISC (action_label
), 1, 0.5);
436 gtk_table_attach (GTK_TABLE (table
), action_label
, 0, 1, 3, 4,
437 (GtkAttachOptions
) (GTK_FILL
),
438 (GtkAttachOptions
) (0), 0, 0);
440 action_entry
= gtk_entry_new ();
441 gtk_widget_show (action_entry
);
442 gtk_table_attach (GTK_TABLE (table
), action_entry
, 1, 2, 3, 4,
443 (GtkAttachOptions
) (GTK_FILL
|GTK_EXPAND
),
444 (GtkAttachOptions
) (0), 0, 0);
446 action_btn
= gtk_button_new_with_mnemonic (_(" De_fine... "));
447 gtk_widget_show (action_btn
);
448 gtk_table_attach (GTK_TABLE (table
), action_btn
, 2, 3, 3, 4,
449 (GtkAttachOptions
) (GTK_FILL
),
450 (GtkAttachOptions
) (0), 2, 2);
451 g_signal_connect(G_OBJECT (action_btn
), "clicked",
452 G_CALLBACK(prefs_filtering_action_define
),
455 /* register / substitute / delete */
456 reg_hbox
= gtk_hbox_new (FALSE
, 4);
457 gtk_widget_show (reg_hbox
);
458 gtk_box_pack_start (GTK_BOX (vbox1
), reg_hbox
, FALSE
, FALSE
, 0);
460 arrow
= gtk_arrow_new (GTK_ARROW_DOWN
, GTK_SHADOW_OUT
);
461 gtk_widget_show (arrow
);
462 gtk_box_pack_start (GTK_BOX (reg_hbox
), arrow
, FALSE
, FALSE
, 0);
463 gtk_widget_set_size_request (arrow
, -1, 16);
465 btn_hbox
= gtk_hbox_new (TRUE
, 4);
466 gtk_widget_show (btn_hbox
);
467 gtk_box_pack_start (GTK_BOX (reg_hbox
), btn_hbox
, FALSE
, FALSE
, 0);
469 reg_btn
= gtk_button_new_from_stock (GTK_STOCK_ADD
);
470 gtk_widget_show (reg_btn
);
471 gtk_box_pack_start (GTK_BOX (btn_hbox
), reg_btn
, FALSE
, TRUE
, 0);
472 g_signal_connect(G_OBJECT (reg_btn
), "clicked",
473 G_CALLBACK(prefs_filtering_register_cb
), NULL
);
474 CLAWS_SET_TIP(reg_btn
,
475 _("Append the new rule above to the list"));
477 subst_btn
= gtkut_get_replace_btn (_("_Replace"));
478 gtk_widget_show (subst_btn
);
479 gtk_box_pack_start (GTK_BOX (btn_hbox
), subst_btn
, FALSE
, TRUE
, 0);
480 g_signal_connect(G_OBJECT (subst_btn
), "clicked",
481 G_CALLBACK(prefs_filtering_substitute_cb
),
483 CLAWS_SET_TIP(subst_btn
,
484 _("Replace the selected rule in list with the rule above"));
486 del_btn
= gtk_button_new_with_mnemonic (_("D_elete"));
487 gtk_button_set_image(GTK_BUTTON(del_btn
),
488 gtk_image_new_from_stock(GTK_STOCK_REMOVE
,GTK_ICON_SIZE_BUTTON
));
489 gtk_box_pack_start (GTK_BOX (btn_hbox
), del_btn
, FALSE
, TRUE
, 0);
490 g_signal_connect(G_OBJECT (del_btn
), "clicked",
491 G_CALLBACK(prefs_filtering_delete_cb
), NULL
);
492 CLAWS_SET_TIP(del_btn
,
493 _("Delete the selected rule from the list"));
495 clear_btn
= gtk_button_new_with_mnemonic (_("C_lear"));
496 gtk_button_set_image(GTK_BUTTON(clear_btn
),
497 gtk_image_new_from_stock(GTK_STOCK_CLEAR
,GTK_ICON_SIZE_BUTTON
));
498 gtk_widget_show (clear_btn
);
499 gtk_box_pack_start (GTK_BOX (btn_hbox
), clear_btn
, FALSE
, TRUE
, 0);
500 g_signal_connect(G_OBJECT (clear_btn
), "clicked",
501 G_CALLBACK(prefs_filtering_clear_cb
), NULL
);
502 CLAWS_SET_TIP(clear_btn
,
503 _("Clear all the input fields in the dialog"));
505 cond_hbox
= gtk_hbox_new (FALSE
, 8);
506 gtk_widget_show (cond_hbox
);
507 gtk_box_pack_start (GTK_BOX (vbox
), cond_hbox
, TRUE
, TRUE
, 0);
509 cond_scrolledwin
= gtk_scrolled_window_new (NULL
, NULL
);
510 gtk_widget_show (cond_scrolledwin
);
511 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(cond_scrolledwin
),
512 GTK_SHADOW_ETCHED_IN
);
513 gtk_widget_set_size_request (cond_scrolledwin
, -1, 150);
514 gtk_box_pack_start (GTK_BOX (cond_hbox
), cond_scrolledwin
,
516 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (cond_scrolledwin
),
517 GTK_POLICY_AUTOMATIC
,
518 GTK_POLICY_AUTOMATIC
);
520 cond_list_view
= prefs_filtering_list_view_create();
521 gtk_widget_show (cond_list_view
);
522 gtk_container_add (GTK_CONTAINER (cond_scrolledwin
), cond_list_view
);
524 btn_vbox
= gtk_vbox_new (FALSE
, 8);
525 gtk_widget_show (btn_vbox
);
526 gtk_box_pack_start (GTK_BOX (cond_hbox
), btn_vbox
, FALSE
, FALSE
, 0);
528 top_btn
= gtk_button_new_from_stock (GTK_STOCK_GOTO_TOP
);
529 gtk_widget_show (top_btn
);
530 gtk_box_pack_start (GTK_BOX (btn_vbox
), top_btn
, FALSE
, FALSE
, 0);
531 g_signal_connect(G_OBJECT (top_btn
), "clicked",
532 G_CALLBACK(prefs_filtering_top
), NULL
);
533 CLAWS_SET_TIP(top_btn
,
534 _("Move the selected rule to the top"));
537 page_up_btn
= gtk_button_new_with_mnemonic (_("Page u_p"));
538 gtk_button_set_image(GTK_BUTTON(page_up_btn
),
539 gtk_image_new_from_stock(GTK_STOCK_GO_UP
,GTK_ICON_SIZE_BUTTON
));
540 gtk_widget_show (page_up_btn
);
541 gtk_box_pack_start (GTK_BOX (btn_vbox
), page_up_btn
, FALSE
, FALSE
, 0);
542 g_signal_connect(G_OBJECT (page_up_btn
), "clicked",
543 G_CALLBACK(prefs_filtering_page_up
), NULL
);
544 CLAWS_SET_TIP(page_up_btn
,
545 _("Move the selected rule one page up"));
548 up_btn
= gtk_button_new_from_stock (GTK_STOCK_GO_UP
);
549 gtk_widget_show (up_btn
);
550 gtk_box_pack_start (GTK_BOX (btn_vbox
), up_btn
, FALSE
, FALSE
, 0);
551 g_signal_connect(G_OBJECT (up_btn
), "clicked",
552 G_CALLBACK(prefs_filtering_up
), NULL
);
553 CLAWS_SET_TIP(up_btn
,
554 _("Move the selected rule up"));
556 down_btn
= gtk_button_new_from_stock (GTK_STOCK_GO_DOWN
);
557 gtk_widget_show (down_btn
);
558 gtk_box_pack_start (GTK_BOX (btn_vbox
), down_btn
, FALSE
, FALSE
, 0);
559 g_signal_connect(G_OBJECT (down_btn
), "clicked",
560 G_CALLBACK(prefs_filtering_down
), NULL
);
561 CLAWS_SET_TIP(down_btn
,
562 _("Move the selected rule down"));
565 page_down_btn
= gtk_button_new_with_mnemonic (_("Page dow_n"));
566 gtk_button_set_image(GTK_BUTTON(page_down_btn
),
567 gtk_image_new_from_stock(GTK_STOCK_GO_DOWN
,GTK_ICON_SIZE_BUTTON
));
568 gtk_widget_show (page_down_btn
);
569 gtk_box_pack_start (GTK_BOX (btn_vbox
), page_down_btn
, FALSE
, FALSE
, 0);
570 g_signal_connect(G_OBJECT (page_down_btn
), "clicked",
571 G_CALLBACK(prefs_filtering_page_down
), NULL
);
572 CLAWS_SET_TIP(page_down_btn
,
573 _("Move the selected rule one page down"));
576 bottom_btn
= gtk_button_new_from_stock (GTK_STOCK_GOTO_BOTTOM
);
577 gtk_widget_show (bottom_btn
);
578 gtk_box_pack_start (GTK_BOX (btn_vbox
), bottom_btn
, FALSE
, FALSE
, 0);
579 g_signal_connect(G_OBJECT (bottom_btn
), "clicked",
580 G_CALLBACK(prefs_filtering_bottom
), NULL
);
581 CLAWS_SET_TIP(bottom_btn
,
582 _("Move the selected rule to the bottom"));
584 if (!geometry
.min_height
) {
585 geometry
.min_width
= 500;
586 geometry
.min_height
= 460;
589 gtk_window_set_geometry_hints(GTK_WINDOW(window
), NULL
, &geometry
,
591 gtk_widget_set_size_request(window
, prefs_common
.filteringwin_width
,
592 prefs_common
.filteringwin_height
);
594 gtk_widget_show_all(window
);
596 filtering
.window
= window
;
597 filtering
.help_btn
= help_btn
;
598 filtering
.ok_btn
= ok_btn
;
600 filtering
.name_entry
= name_entry
;
601 filtering
.cond_entry
= cond_entry
;
602 filtering
.action_entry
= action_entry
;
603 filtering
.cond_list_view
= cond_list_view
;
604 filtering
.account_label
= account_label
;
607 static void rename_tag(GSList
* filters
,
608 const gchar
* old_tag
, const gchar
* new_tag
);
610 void prefs_filtering_rename_tag(const gchar
*old_tag
, const gchar
*new_tag
)
613 const gchar
*tags
[2] = {NULL
, NULL
};
616 for (cur
= folder_get_list() ; cur
!= NULL
; cur
= g_list_next(cur
)) {
618 folder
= (Folder
*) cur
->data
;
619 g_node_traverse(folder
->node
, G_PRE_ORDER
, G_TRAVERSE_ALL
, -1,
620 prefs_filtering_rename_tag_func
, tags
);
623 rename_tag(pre_global_processing
, old_tag
, new_tag
);
624 rename_tag(post_global_processing
, old_tag
, new_tag
);
625 rename_tag(filtering_rules
, old_tag
, new_tag
);
627 prefs_matcher_write_config();
631 static void rename_path(GSList
* filters
,
632 const gchar
* old_path
, const gchar
* new_path
);
634 void prefs_filtering_rename_path(const gchar
*old_path
, const gchar
*new_path
)
637 const gchar
*paths
[2] = {NULL
, NULL
};
640 for (cur
= folder_get_list() ; cur
!= NULL
; cur
= g_list_next(cur
)) {
642 folder
= (Folder
*) cur
->data
;
643 g_node_traverse(folder
->node
, G_PRE_ORDER
, G_TRAVERSE_ALL
, -1,
644 prefs_filtering_rename_path_func
, paths
);
647 rename_path(pre_global_processing
, old_path
, new_path
);
648 rename_path(post_global_processing
, old_path
, new_path
);
649 rename_path(filtering_rules
, old_path
, new_path
);
651 prefs_matcher_write_config();
654 static void rename_path(GSList
* filters
,
655 const gchar
* old_path
, const gchar
* new_path
)
659 for (cur
= filters
; cur
!= NULL
; cur
= cur
->next
) {
660 FilteringProp
*filtering
= (FilteringProp
*)cur
->data
;
661 filtering_action_list_rename_path(filtering
->action_list
,
666 static gboolean
prefs_filtering_rename_path_func(GNode
*node
, gpointer data
)
669 const gchar
* old_path
;
670 const gchar
* new_path
;
671 const gchar
** paths
;
678 cm_return_val_if_fail(old_path
!= NULL
, FALSE
);
679 cm_return_val_if_fail(new_path
!= NULL
, FALSE
);
680 cm_return_val_if_fail(node
!= NULL
, FALSE
);
683 if (!item
|| !item
->prefs
)
685 filters
= item
->prefs
->processing
;
687 rename_path(filters
, old_path
, new_path
);
692 static void rename_tag(GSList
* filters
,
693 const gchar
* old_tag
, const gchar
* new_tag
)
698 for (cur
= filters
; cur
!= NULL
; cur
= cur
->next
) {
699 FilteringProp
*filtering
= (FilteringProp
*)cur
->data
;
701 for(action_cur
= filtering
->action_list
; action_cur
!= NULL
;
702 action_cur
= action_cur
->next
) {
704 FilteringAction
*action
= action_cur
->data
;
706 if (action
->type
!= MATCHACTION_SET_TAG
&&
707 action
->type
!= MATCHACTION_UNSET_TAG
)
709 if (!action
->destination
)
711 if (!strcmp(action
->destination
, old_tag
)) {
712 g_free(action
->destination
);
713 action
->destination
= g_strdup(new_tag
);
719 static gboolean
prefs_filtering_rename_tag_func(GNode
*node
, gpointer data
)
722 const gchar
* old_tag
;
723 const gchar
* new_tag
;
731 cm_return_val_if_fail(old_tag
!= NULL
, FALSE
);
732 cm_return_val_if_fail(new_tag
!= NULL
, FALSE
);
733 cm_return_val_if_fail(node
!= NULL
, FALSE
);
736 if (!item
|| !item
->prefs
)
738 filters
= item
->prefs
->processing
;
740 rename_tag(filters
, old_tag
, new_tag
);
745 void prefs_filtering_delete_path(const gchar
*path
)
748 for (cur
= folder_get_list() ; cur
!= NULL
; cur
= g_list_next(cur
)) {
750 folder
= (Folder
*) cur
->data
;
751 g_node_traverse(folder
->node
, G_PRE_ORDER
, G_TRAVERSE_ALL
, -1,
752 prefs_filtering_delete_path_func
, (gchar
*)path
);
754 delete_path(&pre_global_processing
, path
);
755 delete_path(&post_global_processing
, path
);
756 delete_path(&filtering_rules
, path
);
758 prefs_matcher_write_config();
761 static void delete_path(GSList
** p_filters
, const gchar
* path
)
772 filters
= *p_filters
;
773 pathlen
= strlen(path
);
774 duplist
= g_slist_copy(filters
);
775 for (cur
= duplist
; cur
!= NULL
; cur
= g_slist_next(cur
)) {
776 FilteringProp
*filtering
= (FilteringProp
*) cur
->data
;
778 for(action_cur
= filtering
->action_list
; action_cur
!= NULL
;
779 action_cur
= action_cur
->next
) {
781 FilteringAction
*action
;
783 action
= action_cur
->data
;
785 if (action
->type
== MATCHACTION_SET_TAG
||
786 action
->type
== MATCHACTION_UNSET_TAG
)
788 if (!action
->destination
)
791 destlen
= strlen(action
->destination
);
793 if (destlen
> pathlen
) {
794 prefixlen
= destlen
- pathlen
;
795 suffix
= action
->destination
+ prefixlen
;
797 if (suffix
&& !strncmp(path
, suffix
, pathlen
)) {
798 filteringprop_free(filtering
);
799 filters
= g_slist_remove(filters
, filtering
);
801 } else if (strcmp(action
->destination
, path
) == 0) {
802 filteringprop_free(filtering
);
803 filters
= g_slist_remove(filters
, filtering
);
807 g_slist_free(duplist
);
809 * p_filters
= filters
;
812 static gboolean
prefs_filtering_delete_path_func(GNode
*node
, gpointer data
)
814 const gchar
*path
= data
;
818 cm_return_val_if_fail(path
!= NULL
, FALSE
);
819 cm_return_val_if_fail(node
!= NULL
, FALSE
);
822 if (!item
|| !item
->prefs
)
824 p_filters
= &item
->prefs
->processing
;
826 delete_path(p_filters
, path
);
831 static void prefs_filtering_clear_list(GtkListStore
*list_store
)
833 gtk_list_store_clear(list_store
);
835 /* add the place holder (New) at row 0 */
836 prefs_filtering_list_view_insert_rule(list_store
, -1,
845 static void prefs_filtering_set_dialog(const gchar
*header
, const gchar
*key
)
847 GtkTreeView
*list_view
= GTK_TREE_VIEW(filtering
.cond_list_view
);
849 GSList
* prefs_filtering
;
851 GtkListStore
*list_store
;
853 list_store
= GTK_LIST_STORE(gtk_tree_view_get_model(list_view
));
854 prefs_filtering_clear_list(list_store
);
856 prefs_filtering
= *p_processing_list
;
858 for(cur
= prefs_filtering
; cur
!= NULL
; cur
= g_slist_next(cur
)) {
859 FilteringProp
* prop
= (FilteringProp
*) cur
->data
;
860 gchar
*account_name
= NULL
;
862 if (prop
->account_id
> 0) {
863 PrefsAccount
*ac_prefs
= account_find_from_id(prop
->account_id
);
866 account_name
= ac_prefs
->account_name
;
868 if (account_name
== NULL
)
869 account_name
= (gchar
*)C_("Filtering Account Menu", "All");
871 cond_str
= filteringprop_to_string(prop
);
872 subst_char(cond_str
, '\t', ':');
874 prefs_filtering_list_view_insert_rule(list_store
, -1,
884 prefs_filtering_reset_dialog();
890 quoted_key
= matcher_quote_str(key
);
892 match_str
= g_strconcat(header
, " ", get_matchparser_tab_str(MATCHTYPE_MATCHCASE
),
893 " \"", quoted_key
, "\"", NULL
);
896 gtk_entry_set_text(GTK_ENTRY(filtering
.cond_entry
), match_str
);
901 static void prefs_filtering_reset_dialog(void)
903 gtk_entry_set_text(GTK_ENTRY(filtering
.name_entry
), "");
904 combobox_select_by_data(GTK_COMBO_BOX(filtering
.account_combobox
), 0);
905 gtk_entry_set_text(GTK_ENTRY(filtering
.cond_entry
), "");
906 gtk_entry_set_text(GTK_ENTRY(filtering
.action_entry
), "");
909 static gboolean
prefs_filtering_search_func_cb (GtkTreeModel
*model
, gint column
, const gchar
*key
,
910 GtkTreeIter
*iter
, gpointer search_data
)
916 gtk_tree_model_get (model
, iter
, column
, &store_string
, -1);
918 if (!store_string
|| !key
) return FALSE
;
921 retval
= (g_ascii_strncasecmp (key
, store_string
, strlen(key
)) != 0);
923 g_free(store_string
);
924 debug_print("selecting row\n");
925 path
= gtk_tree_model_get_path(model
, iter
);
926 prefs_filtering_select_row(GTK_TREE_VIEW(filtering
.cond_list_view
), path
);
927 gtk_tree_path_free(path
);
932 static void prefs_filtering_set_list(void)
937 gchar
* filtering_str
;
938 GSList
* prefs_filtering
;
940 prefs_filtering
= *p_processing_list
;
941 for (cur
= prefs_filtering
; cur
!= NULL
; cur
= g_slist_next(cur
))
942 filteringprop_free((FilteringProp
*) cur
->data
);
943 g_slist_free(prefs_filtering
);
944 prefs_filtering
= NULL
;
947 while (NULL
!= (filtering_str
= prefs_filtering_list_view_get_rule
948 (filtering
.cond_list_view
, row
))) {
949 /* FIXME: this strcmp() is bogus: "(New)" should never
950 * be inserted in the storage */
951 if (strcmp(filtering_str
, _("(New)")) != 0) {
956 prefs_filtering_list_view_get_rule_info(
957 filtering
.cond_list_view
, row
,
958 &enabled
, &name
, &account_id
);
959 prop
= matcher_parser_get_filtering(filtering_str
);
960 g_free(filtering_str
);
962 prop
->enabled
= enabled
;
963 if (prop
->name
!= NULL
)
966 prop
->account_id
= account_id
;
968 g_slist_append(prefs_filtering
, prop
);
975 *p_processing_list
= prefs_filtering
;
978 static gint
prefs_filtering_list_view_set_row(gint row
, FilteringProp
* prop
)
980 GtkTreeView
*list_view
= GTK_TREE_VIEW(filtering
.cond_list_view
);
982 GtkListStore
*list_store
;
985 gchar
*account_name
= (gchar
*)C_("Filtering Account Menu", "All");
986 gboolean enabled
= TRUE
;
989 str
= filteringprop_to_string(prop
);
996 account_id
= prop
->account_id
;
998 account_name
= account_find_from_id(account_id
)->account_name
;
999 enabled
= prop
->enabled
;
1002 list_store
= GTK_LIST_STORE(gtk_tree_view_get_model(list_view
));
1004 row
= prefs_filtering_list_view_insert_rule(list_store
, row
,
1017 static void prefs_filtering_condition_define_done(MatcherList
* matchers
)
1021 if (matchers
== NULL
)
1024 str
= matcherlist_to_string(matchers
);
1027 gtk_entry_set_text(GTK_ENTRY(filtering
.cond_entry
), str
);
1032 static void prefs_filtering_condition_define(gpointer action
, gpointer data
)
1035 MatcherList
* matchers
= NULL
;
1037 cond_str
= gtk_editable_get_chars(GTK_EDITABLE(filtering
.cond_entry
), 0, -1);
1039 if (*cond_str
!= '\0') {
1040 matchers
= matcher_parser_get_cond(cond_str
, NULL
);
1041 if (matchers
== NULL
)
1042 alertpanel_error(_("Condition string is not valid."));
1047 prefs_matcher_open(matchers
, prefs_filtering_condition_define_done
);
1049 if (matchers
!= NULL
)
1050 matcherlist_free(matchers
);
1053 static void prefs_filtering_action_define_done(GSList
* action_list
)
1057 if (action_list
== NULL
)
1060 str
= filteringaction_list_to_string(action_list
);
1063 gtk_entry_set_text(GTK_ENTRY(filtering
.action_entry
), str
);
1068 static void prefs_filtering_action_define(gpointer action
, gpointer data
)
1071 GSList
* action_list
= NULL
;
1073 action_str
= gtk_editable_get_chars(GTK_EDITABLE(filtering
.action_entry
), 0, -1);
1075 if (*action_str
!= '\0') {
1076 action_list
= matcher_parser_get_action_list(action_str
);
1077 if (action_list
== NULL
)
1078 alertpanel_error(_("Action string is not valid."));
1083 prefs_filtering_action_open(action_list
,
1084 prefs_filtering_action_define_done
);
1086 if (action_list
!= NULL
) {
1088 for(cur
= action_list
; cur
!= NULL
; cur
= cur
->next
) {
1089 filteringaction_free(cur
->data
);
1095 /* register / substitute delete buttons */
1098 static FilteringProp
* prefs_filtering_dialog_to_filtering(gboolean alert
)
1101 gboolean enabled
= TRUE
;
1102 gchar
* name
= NULL
;
1103 gint account_id
= 0;
1104 gchar
* cond_str
= NULL
;
1105 gchar
* action_str
= NULL
;
1106 FilteringProp
* prop
= NULL
;
1107 GSList
* action_list
;
1109 name
= gtk_editable_get_chars(GTK_EDITABLE(filtering
.name_entry
), 0, -1);
1111 account_id
= combobox_get_active_data(GTK_COMBO_BOX(filtering
.account_combobox
));
1113 cond_str
= gtk_editable_get_chars(GTK_EDITABLE(filtering
.cond_entry
), 0, -1);
1114 if (*cond_str
== '\0') {
1115 if(alert
== TRUE
) alertpanel_error(_("Condition string is empty."));
1119 action_str
= gtk_editable_get_chars(GTK_EDITABLE(filtering
.action_entry
), 0, -1);
1120 if (*action_str
== '\0') {
1121 if(alert
== TRUE
) alertpanel_error(_("Action string is empty."));
1125 cond
= matcher_parser_get_cond(cond_str
, NULL
);
1128 if(alert
== TRUE
) alertpanel_error(_("Condition string is not valid."));
1132 action_list
= matcher_parser_get_action_list(action_str
);
1135 if (action_list
== NULL
) {
1136 if(alert
== TRUE
) alertpanel_error(_("Action string is not valid."));
1140 prop
= filteringprop_new(enabled
, name
, account_id
, cond
, action_list
);
1149 static void prefs_filtering_register_cb(gpointer action
, gpointer data
)
1151 FilteringProp
*prop
;
1153 prop
= prefs_filtering_dialog_to_filtering(TRUE
);
1156 prefs_filtering_list_view_set_row(-1, prop
);
1158 filteringprop_free(prop
);
1160 prefs_filtering_reset_dialog();
1164 static void prefs_filtering_substitute_cb(gpointer action
, gpointer data
)
1166 gint selected_row
= gtkut_list_view_get_selected_row
1167 (filtering
.cond_list_view
);
1168 FilteringProp
*prop
;
1173 if (selected_row
<= 0)
1176 prop
= prefs_filtering_dialog_to_filtering(TRUE
);
1181 /* prop->emabled is always TRUE here, re-use the value from the selected row
1182 as we don't substitute this value from dialog */
1183 prefs_filtering_list_view_get_rule_info(
1184 filtering
.cond_list_view
, selected_row
,
1185 &enabled
, &name
, &account_id
);
1186 prop
->enabled
= enabled
;
1188 prefs_filtering_list_view_set_row(selected_row
, prop
);
1190 filteringprop_free(prop
);
1192 prefs_filtering_row_selected(gtk_tree_view_get_selection(
1193 GTK_TREE_VIEW(filtering
.cond_list_view
)),
1194 GTK_TREE_VIEW(filtering
.cond_list_view
));
1198 static void prefs_filtering_delete_cb(gpointer action
, gpointer data
)
1200 GtkTreeView
*list_view
= GTK_TREE_VIEW(filtering
.cond_list_view
);
1201 GtkTreeModel
*model
;
1205 selected_row
= gtkut_list_view_get_selected_row(filtering
.cond_list_view
);
1206 if (selected_row
<= 0)
1209 if (alertpanel(_("Delete rule"),
1210 _("Do you really want to delete this rule?"),
1211 GTK_STOCK_CANCEL
, "+"GTK_STOCK_DELETE
, NULL
) == G_ALERTDEFAULT
)
1214 model
= gtk_tree_view_get_model(list_view
);
1215 if (!gtk_tree_model_iter_nth_child(model
, &iter
, NULL
, selected_row
))
1218 gtk_list_store_remove(GTK_LIST_STORE(model
), &iter
);
1220 prefs_filtering_reset_dialog();
1224 static void prefs_filtering_delete_all_cb(gpointer action
, gpointer data
)
1226 GtkListStore
*list_store
;
1228 if (alertpanel(_("Delete all rules"),
1229 _("Do you really want to delete all the rules?"),
1230 GTK_STOCK_CANCEL
, "+"GTK_STOCK_DELETE
, NULL
) == G_ALERTDEFAULT
)
1233 list_store
= GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(filtering
.cond_list_view
)));
1234 prefs_filtering_clear_list(list_store
);
1236 prefs_filtering_reset_dialog();
1240 static void prefs_filtering_clear_cb(gpointer action
, gpointer data
)
1242 prefs_filtering_reset_dialog();
1245 static void prefs_filtering_duplicate_cb(gpointer action
, gpointer data
)
1247 gint selected_row
= gtkut_list_view_get_selected_row
1248 (filtering
.cond_list_view
);
1249 FilteringProp
*prop
;
1254 if (selected_row
<= 0)
1257 prop
= prefs_filtering_dialog_to_filtering(TRUE
);
1261 /* prop->emabled is always TRUE here, re-use the value from the selected row
1262 as we don't substitute this value from dialog */
1263 prefs_filtering_list_view_get_rule_info(
1264 filtering
.cond_list_view
, selected_row
,
1265 &enabled
, &name
, &account_id
);
1266 prop
->enabled
= enabled
;
1268 prefs_filtering_list_view_set_row(-selected_row
-2, prop
);
1270 filteringprop_free(prop
);
1272 prefs_filtering_reset_dialog();
1276 static void prefs_filtering_top(gpointer action
, gpointer data
)
1279 GtkTreeIter top
, sel
;
1280 GtkTreeModel
*model
;
1282 row
= gtkut_list_view_get_selected_row(filtering
.cond_list_view
);
1286 model
= gtk_tree_view_get_model(GTK_TREE_VIEW(filtering
.cond_list_view
));
1288 if (!gtk_tree_model_iter_nth_child(model
, &top
, NULL
, 0)
1289 || !gtk_tree_model_iter_nth_child(model
, &sel
, NULL
, row
))
1292 gtk_list_store_move_after(GTK_LIST_STORE(model
), &sel
, &top
);
1293 gtkut_list_view_select_row(filtering
.cond_list_view
, 1);
1297 static void prefs_filtering_page_up(gpointer action
, gpointer data
)
1299 gint row
, target_row
;
1300 GtkTreeIter selected
, target
;
1301 GtkTreeModel
*model
;
1303 GdkRectangle cell_rect
, view_rect
;
1305 model
= gtk_tree_view_get_model(GTK_TREE_VIEW(filtering
.cond_list_view
));
1306 row
= gtkut_list_view_get_selected_row(filtering
.cond_list_view
);
1310 if (!gtk_tree_model_iter_nth_child(model
, &selected
, NULL
, row
))
1313 /* compute number of rows per page (approximation) */
1314 path
= gtk_tree_model_get_path(model
, &selected
);
1315 gtk_tree_view_get_cell_area(GTK_TREE_VIEW(filtering
.cond_list_view
), path
, NULL
, &cell_rect
);
1316 gtk_tree_view_get_visible_rect(GTK_TREE_VIEW(filtering
.cond_list_view
), &view_rect
);
1317 gtk_tree_path_free(path
);
1318 target_row
= row
- (view_rect
.height
/cell_rect
.height
);
1322 if (!gtk_tree_model_iter_nth_child(model
, &target
, NULL
, target_row
))
1324 gtk_list_store_move_before(GTK_LIST_STORE(model
), &selected
, &target
);
1325 gtkut_list_view_select_row(filtering
.cond_list_view
, target_row
);
1328 static void prefs_filtering_up(gpointer action
, gpointer data
)
1331 GtkTreeIter top
, sel
;
1332 GtkTreeModel
*model
;
1334 row
= gtkut_list_view_get_selected_row(filtering
.cond_list_view
);
1338 model
= gtk_tree_view_get_model(GTK_TREE_VIEW(filtering
.cond_list_view
));
1340 if (!gtk_tree_model_iter_nth_child(model
, &top
, NULL
, row
- 1)
1341 || !gtk_tree_model_iter_nth_child(model
, &sel
, NULL
, row
))
1344 gtk_list_store_swap(GTK_LIST_STORE(model
), &top
, &sel
);
1345 gtkut_list_view_select_row(filtering
.cond_list_view
, row
- 1);
1349 static void prefs_filtering_down(gpointer action
, gpointer data
)
1352 GtkTreeIter top
, sel
;
1353 GtkTreeModel
*model
;
1355 model
= gtk_tree_view_get_model(GTK_TREE_VIEW(filtering
.cond_list_view
));
1356 n_rows
= gtk_tree_model_iter_n_children(model
, NULL
);
1357 row
= gtkut_list_view_get_selected_row(filtering
.cond_list_view
);
1358 if (row
< 1 || row
>= n_rows
- 1)
1361 if (!gtk_tree_model_iter_nth_child(model
, &top
, NULL
, row
)
1362 || !gtk_tree_model_iter_nth_child(model
, &sel
, NULL
, row
+ 1))
1365 gtk_list_store_swap(GTK_LIST_STORE(model
), &top
, &sel
);
1366 gtkut_list_view_select_row(filtering
.cond_list_view
, row
+ 1);
1370 static void prefs_filtering_page_down(gpointer action
, gpointer data
)
1372 gint row
, target_row
, n_rows
;
1373 GtkTreeIter selected
, target
;
1374 GtkTreeModel
*model
;
1376 GdkRectangle cell_rect
, view_rect
;
1378 model
= gtk_tree_view_get_model(GTK_TREE_VIEW(filtering
.cond_list_view
));
1379 n_rows
= gtk_tree_model_iter_n_children(model
, NULL
);
1380 row
= gtkut_list_view_get_selected_row(filtering
.cond_list_view
);
1381 if (row
< 1 || row
>= n_rows
-1)
1384 if (!gtk_tree_model_iter_nth_child(model
, &selected
, NULL
, row
))
1387 /* compute number of rows per page (approximation) */
1388 path
= gtk_tree_model_get_path(model
, &selected
);
1389 gtk_tree_view_get_cell_area(GTK_TREE_VIEW(filtering
.cond_list_view
), path
, NULL
, &cell_rect
);
1390 gtk_tree_view_get_visible_rect(GTK_TREE_VIEW(filtering
.cond_list_view
), &view_rect
);
1391 gtk_tree_path_free(path
);
1392 target_row
= row
+ (view_rect
.height
/cell_rect
.height
);
1393 if (target_row
> n_rows
-1)
1394 target_row
= n_rows
-1;
1396 if (!gtk_tree_model_iter_nth_child(model
, &target
, NULL
, target_row
))
1398 gtk_list_store_move_after(GTK_LIST_STORE(model
), &selected
, &target
);
1399 gtkut_list_view_select_row(filtering
.cond_list_view
, target_row
);
1403 static void prefs_filtering_bottom(gpointer action
, gpointer data
)
1406 GtkTreeIter top
, sel
;
1407 GtkTreeModel
*model
;
1409 model
= gtk_tree_view_get_model(GTK_TREE_VIEW(filtering
.cond_list_view
));
1410 n_rows
= gtk_tree_model_iter_n_children(model
, NULL
);
1411 row
= gtkut_list_view_get_selected_row(filtering
.cond_list_view
);
1412 if (row
< 1 || row
>= n_rows
- 1)
1415 if (!gtk_tree_model_iter_nth_child(model
, &top
, NULL
, row
)
1416 || !gtk_tree_model_iter_nth_child(model
, &sel
, NULL
, n_rows
- 1))
1419 gtk_list_store_move_after(GTK_LIST_STORE(model
), &top
, &sel
);
1420 gtkut_list_view_select_row(filtering
.cond_list_view
, n_rows
- 1);
1424 static void prefs_filtering_select_set(FilteringProp
*prop
)
1429 prefs_filtering_reset_dialog();
1431 matcher_str
= matcherlist_to_string(prop
->matchers
);
1432 if (matcher_str
== NULL
) {
1436 if (prop
->name
!= NULL
)
1437 gtk_entry_set_text(GTK_ENTRY(filtering
.name_entry
), prop
->name
);
1439 combobox_select_by_data(GTK_COMBO_BOX(filtering
.account_combobox
), prop
->account_id
);
1441 gtk_entry_set_text(GTK_ENTRY(filtering
.cond_entry
), matcher_str
);
1443 action_str
= filteringaction_list_to_string(prop
->action_list
);
1444 if (action_str
!= NULL
)
1445 gtk_entry_set_text(GTK_ENTRY(filtering
.action_entry
), action_str
);
1448 g_free(matcher_str
);
1451 static gint
prefs_filtering_deleted(GtkWidget
*widget
, GdkEventAny
*event
,
1454 prefs_filtering_cancel(NULL
, NULL
);
1458 static gboolean
prefs_filtering_key_pressed(GtkWidget
*widget
, GdkEventKey
*event
,
1461 if (event
&& event
->keyval
== GDK_KEY_Escape
) {
1462 prefs_filtering_cancel(NULL
, NULL
);
1468 static gboolean
prefs_filtering_check_mod(gboolean check_changed_list
)
1470 FilteringProp
* prop
;
1472 gchar
* filtering_str
;
1476 prop
= prefs_filtering_dialog_to_filtering(FALSE
);
1478 if (check_changed_list
) {
1479 if (modified
&& alertpanel(_("Filtering rules not saved"),
1480 _("The list of filtering rules have been modified. Close anyway?"),
1481 GTK_STOCK_CLOSE
, g_strconcat("+", _("_Continue editing"), NULL
),
1482 NULL
) != G_ALERTDEFAULT
) {
1487 /* check if a rule is being edited */
1489 str
= filteringprop_to_string(prop
);
1491 while (NULL
!= (filtering_str
= (prefs_filtering_list_view_get_rule
1492 (filtering
.cond_list_view
,
1494 if (strcmp(filtering_str
, str
) == 0)
1497 g_free(filtering_str
);
1500 if (!filtering_str
) {
1501 val
= alertpanel(_("Entry not saved"),
1502 _("The entry was not saved. Close anyway?"),
1503 GTK_STOCK_CLOSE
, g_strconcat("+", _("_Continue editing"),NULL
), NULL
);
1504 if (G_ALERTDEFAULT
!= val
) {
1505 g_free(filtering_str
);
1506 g_free(str
); /* fixed two leaks: huzzah! */
1507 filteringprop_free(prop
);
1512 g_free(filtering_str
);
1514 filteringprop_free(prop
); /* fixed a leak: huzzah! */
1516 gchar
*name
, *condition
, *action
;
1517 name
= gtk_editable_get_chars(GTK_EDITABLE(filtering
.name_entry
), 0, -1);
1518 condition
= gtk_editable_get_chars(GTK_EDITABLE(filtering
.cond_entry
), 0, -1);
1519 action
= gtk_editable_get_chars(GTK_EDITABLE(filtering
.action_entry
), 0, -1);
1521 strlen(condition
) ||
1523 val
= alertpanel(_("Entry not saved"),
1524 _("The entry was not saved. Close anyway?"),
1525 GTK_STOCK_CLOSE
, g_strconcat("+", _("_Continue editing"), NULL
), NULL
);
1526 if (G_ALERTDEFAULT
!= val
) {
1540 static void prefs_filtering_ok(gpointer action
, gpointer data
)
1542 if (prefs_filtering_check_mod(FALSE
))
1545 prefs_filtering_set_list();
1546 prefs_matcher_write_config();
1547 prefs_filtering_close();
1550 static void prefs_filtering_cancel(gpointer action
, gpointer data
)
1552 if (prefs_filtering_check_mod(TRUE
))
1555 prefs_matcher_read_config();
1556 prefs_filtering_close();
1559 static GtkListStore
* prefs_filtering_create_data_store(void)
1561 return gtk_list_store_new(N_PREFS_FILTERING_COLUMNS
,
1572 *\brief Insert filtering rule into store. Note that we access the
1573 * tree view / store by index, which is a bit suboptimal, but
1574 * at least it made GTK 2 porting easier.
1576 *\param list_store Store to operate on
1577 *\param row -1 to add a new rule to store,
1578 row >=0 to change an existing row
1579 row <-1 insert a new row after (-row-2)
1580 *\param enabled TRUE if rule is enabled
1581 *\param name The Name of rule
1582 *\param account_id The account ID
1583 *\param account_name The account name or All or (New)
1584 *\param rule String representation of rule
1585 *\param prop TRUE if valid filtering rule; if FALSE it's the first
1586 * entry in the store ("(New)").
1588 *\return int Row of inserted / changed rule.
1590 static gint
prefs_filtering_list_view_insert_rule(GtkListStore
*list_store
,
1595 const gchar
*account_name
,
1600 GtkTreeIter sibling
;
1602 /* check if valid row at all */
1604 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store
),
1607 } else if (row
< -1) {
1608 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store
),
1609 &sibling
, NULL
, -row
-2))
1615 gtk_list_store_append(list_store
, &iter
);
1616 gtk_list_store_set(list_store
, &iter
,
1617 PREFS_FILTERING_ENABLED
, enabled
,
1618 PREFS_FILTERING_NAME
, name
,
1619 PREFS_FILTERING_ACCOUNT_ID
, account_id
,
1620 PREFS_FILTERING_ACCOUNT_NAME
, account_name
,
1621 PREFS_FILTERING_RULE
, rule
,
1622 PREFS_FILTERING_PROP
, prop
,
1624 return gtk_tree_model_iter_n_children(GTK_TREE_MODEL(list_store
),
1626 } else if (row
< -1) {
1628 gtk_list_store_insert_after(list_store
, &iter
, &sibling
);
1629 gtk_list_store_set(list_store
, &iter
,
1630 PREFS_FILTERING_ENABLED
, enabled
,
1631 PREFS_FILTERING_NAME
, name
,
1632 PREFS_FILTERING_ACCOUNT_ID
, account_id
,
1633 PREFS_FILTERING_ACCOUNT_NAME
, account_name
,
1634 PREFS_FILTERING_RULE
, rule
,
1635 PREFS_FILTERING_PROP
, prop
,
1637 return gtk_tree_model_iter_n_children(GTK_TREE_MODEL(list_store
),
1640 /* change existing */
1641 gtk_list_store_set(list_store
, &iter
,
1642 PREFS_FILTERING_ENABLED
, enabled
,
1643 PREFS_FILTERING_NAME
, name
,
1644 PREFS_FILTERING_ACCOUNT_ID
, account_id
,
1645 PREFS_FILTERING_ACCOUNT_NAME
, account_name
,
1646 PREFS_FILTERING_RULE
, rule
,
1653 *\return gchar * Rule at specified row - should be freed.
1655 static gchar
*prefs_filtering_list_view_get_rule(GtkWidget
*list
, gint row
)
1657 GtkTreeView
*list_view
= GTK_TREE_VIEW(list
);
1658 GtkTreeModel
*model
= gtk_tree_view_get_model(list_view
);
1662 if (!gtk_tree_model_iter_nth_child(model
, &iter
, NULL
, row
))
1665 gtk_tree_model_get(model
, &iter
,
1666 PREFS_FILTERING_RULE
, &result
,
1672 static void prefs_filtering_list_view_get_rule_info(GtkWidget
*list
, gint row
,
1673 gboolean
*enabled
, gchar
**name
, gint
*account_id
)
1675 GtkTreeView
*list_view
= GTK_TREE_VIEW(list
);
1676 GtkTreeModel
*model
= gtk_tree_view_get_model(list_view
);
1682 if (gtk_tree_model_iter_nth_child(model
, &iter
, NULL
, row
)) {
1683 gtk_tree_model_get(model
, &iter
,
1684 PREFS_FILTERING_ENABLED
, enabled
,
1685 PREFS_FILTERING_NAME
, name
,
1686 PREFS_FILTERING_ACCOUNT_ID
, account_id
,
1691 static GtkActionGroup
*prefs_filtering_popup_action
= NULL
;
1692 static GtkWidget
*prefs_filtering_popup_menu
= NULL
;
1694 static GtkActionEntry prefs_filtering_popup_entries
[] =
1696 {"PrefsFilteringPopup", NULL
, "PrefsFilteringPopup" },
1697 {"PrefsFilteringPopup/Delete", NULL
, N_("_Delete"), NULL
, NULL
, G_CALLBACK(prefs_filtering_delete_cb
) },
1698 {"PrefsFilteringPopup/DeleteAll", NULL
, N_("Delete _all"), NULL
, NULL
, G_CALLBACK(prefs_filtering_delete_all_cb
) },
1699 {"PrefsFilteringPopup/Duplicate", NULL
, N_("D_uplicate"), NULL
, NULL
, G_CALLBACK(prefs_filtering_duplicate_cb
) },
1701 {"PrefsFilteringPopup/---", NULL
, "---", NULL
, NULL
, NULL
},
1702 {"PrefsFilteringPopup/PageUp", NULL
, N_("Move one page up"), NULL
, NULL
, G_CALLBACK(prefs_filtering_page_up
) },
1703 {"PrefsFilteringPopup/PageDown", NULL
, N_("Move one page down"), NULL
, NULL
, G_CALLBACK(prefs_filtering_page_down
) },
1707 static void prefs_filtering_row_selected(GtkTreeSelection
*selection
,
1708 GtkTreeView
*list_view
)
1712 GtkTreeModel
*model
;
1714 if (!gtk_tree_selection_get_selected(selection
, &model
, &iter
))
1717 path
= gtk_tree_model_get_path(model
, &iter
);
1718 prefs_filtering_select_row(list_view
, path
);
1719 gtk_tree_path_free(path
);
1722 static gint
prefs_filtering_list_btn_pressed(GtkWidget
*widget
, GdkEventButton
*event
,
1723 GtkTreeView
*list_view
)
1726 /* left- or right-button click */
1727 if (event
->button
== 1 || event
->button
== 3) {
1728 GtkTreePath
*path
= NULL
;
1730 if (gtk_tree_view_get_path_at_pos( list_view
, event
->x
, event
->y
,
1731 &path
, NULL
, NULL
, NULL
)) {
1732 prefs_filtering_select_row(list_view
, path
);
1735 gtk_tree_path_free(path
);
1738 /* right-button click */
1739 if (event
->button
== 3) {
1740 GtkTreeModel
*model
= gtk_tree_view_get_model(list_view
);
1745 if (!prefs_filtering_popup_menu
) {
1746 prefs_filtering_popup_action
= cm_menu_create_action_group("PrefsFilteringPopup", prefs_filtering_popup_entries
,
1747 G_N_ELEMENTS(prefs_filtering_popup_entries
), (gpointer
)list_view
);
1748 MENUITEM_ADDUI("/Menus", "PrefsFilteringPopup", "PrefsFilteringPopup", GTK_UI_MANAGER_MENU
)
1749 MENUITEM_ADDUI("/Menus/PrefsFilteringPopup", "Delete", "PrefsFilteringPopup/Delete", GTK_UI_MANAGER_MENUITEM
)
1750 MENUITEM_ADDUI("/Menus/PrefsFilteringPopup", "DeleteAll", "PrefsFilteringPopup/DeleteAll", GTK_UI_MANAGER_MENUITEM
)
1751 MENUITEM_ADDUI("/Menus/PrefsFilteringPopup", "Duplicate", "PrefsFilteringPopup/Duplicate", GTK_UI_MANAGER_MENUITEM
)
1753 MENUITEM_ADDUI("/Menus/PrefsFilteringPopup", "Separator1", "PrefsFilteringPopup/---", GTK_UI_MANAGER_SEPARATOR
)
1754 MENUITEM_ADDUI("/Menus/PrefsFilteringPopup", "PageUp", "PrefsFilteringPopup/PageUp", GTK_UI_MANAGER_MENUITEM
)
1755 MENUITEM_ADDUI("/Menus/PrefsFilteringPopup", "PageDown", "PrefsFilteringPopup/PageDown", GTK_UI_MANAGER_MENUITEM
)
1757 prefs_filtering_popup_menu
= gtk_menu_item_get_submenu(GTK_MENU_ITEM(
1758 gtk_ui_manager_get_widget(gtkut_ui_manager(), "/Menus/PrefsFilteringPopup")) );
1761 /* grey out some popup menu items if there is no selected row */
1762 row
= gtkut_list_view_get_selected_row(GTK_WIDGET(list_view
));
1763 cm_menu_set_sensitive("PrefsFilteringPopup/Delete", (row
> 0));
1764 cm_menu_set_sensitive("PrefsFilteringPopup/Duplicate", (row
> 0));
1766 /* grey out seom popup menu items if there is no row
1767 (not counting the (New) one at row 0) */
1768 non_empty
= gtk_tree_model_get_iter_first(model
, &iter
);
1770 non_empty
= gtk_tree_model_iter_next(model
, &iter
);
1771 cm_menu_set_sensitive("PrefsFilteringPopup/DeleteAll", non_empty
);
1773 gtk_menu_popup(GTK_MENU(prefs_filtering_popup_menu
),
1774 NULL
, NULL
, NULL
, NULL
,
1775 event
->button
, event
->time
);
1781 static gboolean
prefs_filtering_list_popup_menu(GtkWidget
*widget
, gpointer data
)
1783 GtkTreeView
*list_view
= (GtkTreeView
*)data
;
1784 GdkEventButton event
;
1787 event
.time
= gtk_get_current_event_time();
1789 prefs_filtering_list_btn_pressed(NULL
, &event
, list_view
);
1795 *\brief Create list view for filtering
1797 static GtkWidget
*prefs_filtering_list_view_create(void)
1799 GtkTreeView
*list_view
;
1800 GtkTreeSelection
*selector
;
1802 list_view
= GTK_TREE_VIEW(gtk_tree_view_new_with_model(GTK_TREE_MODEL
1803 (prefs_filtering_create_data_store())));
1805 g_object_set(list_view
, "allow-checkbox-mode", FALSE
, NULL
);
1808 g_signal_connect(G_OBJECT(list_view
), "popup-menu",
1809 G_CALLBACK(prefs_filtering_list_popup_menu
), list_view
);
1810 g_signal_connect(G_OBJECT(list_view
), "button-press-event",
1811 G_CALLBACK(prefs_filtering_list_btn_pressed
), list_view
);
1813 gtk_tree_view_set_rules_hint(list_view
, prefs_common
.use_stripes_everywhere
);
1814 gtk_tree_view_set_reorderable(list_view
, TRUE
);
1816 selector
= gtk_tree_view_get_selection(list_view
);
1817 gtk_tree_selection_set_mode(selector
, GTK_SELECTION_BROWSE
);
1818 g_signal_connect(G_OBJECT(selector
), "changed",
1819 G_CALLBACK(prefs_filtering_row_selected
), list_view
);
1821 /* create the columns */
1822 prefs_filtering_create_list_view_columns(GTK_WIDGET(list_view
));
1824 return GTK_WIDGET(list_view
);
1827 static void prefs_filtering_enable_toggled(GtkCellRendererToggle
*widget
,
1829 GtkWidget
*list_view
)
1832 GtkTreeModel
*model
= gtk_tree_view_get_model(GTK_TREE_VIEW(list_view
));
1833 gboolean enabled
= TRUE
;
1835 if (!gtk_tree_model_get_iter_from_string(model
, &iter
, path
))
1838 gtk_tree_model_get(model
, &iter
,
1839 PREFS_FILTERING_ENABLED
, &enabled
,
1842 gtk_list_store_set(GTK_LIST_STORE(model
), &iter
,
1843 PREFS_FILTERING_ENABLED
, !enabled
,
1847 static void prefs_filtering_create_list_view_columns(GtkWidget
*list_view
)
1849 GtkTreeViewColumn
*column
;
1850 GtkCellRenderer
*renderer
;
1852 renderer
= gtk_cell_renderer_toggle_new();
1853 g_object_set(renderer
,
1855 "activatable", TRUE
,
1857 column
= gtk_tree_view_column_new_with_attributes
1858 (_("Enable"), /* FIXME : Enable, Enabled, or 'E' ? */
1860 "active", PREFS_FILTERING_ENABLED
,
1862 gtk_tree_view_column_set_alignment (column
, 0.5);
1863 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view
), column
);
1864 g_signal_connect(G_OBJECT(renderer
), "toggled",
1865 G_CALLBACK(prefs_filtering_enable_toggled
),
1868 renderer
= gtk_cell_renderer_text_new();
1869 column
= gtk_tree_view_column_new_with_attributes
1872 "text", PREFS_FILTERING_NAME
,
1874 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view
), column
);
1875 gtk_tree_view_column_set_resizable(column
, TRUE
);
1877 renderer
= gtk_cell_renderer_text_new();
1878 column
= gtk_tree_view_column_new_with_attributes
1881 "text", PREFS_FILTERING_ACCOUNT_NAME
,
1883 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view
), column
);
1884 gtk_tree_view_column_set_resizable(column
, TRUE
);
1886 filtering
.account_name_column
= column
;
1888 renderer
= gtk_cell_renderer_text_new();
1889 column
= gtk_tree_view_column_new_with_attributes
1892 "text", PREFS_FILTERING_RULE
,
1895 gtk_tree_view_set_search_column(GTK_TREE_VIEW(list_view
), PREFS_FILTERING_NAME
);
1896 gtk_tree_view_set_search_equal_func(GTK_TREE_VIEW(list_view
), prefs_filtering_search_func_cb
, NULL
, NULL
);
1898 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view
), column
);
1902 *\brief Triggered when a row has to be selected
1904 static void prefs_filtering_select_row(GtkTreeView
*list_view
, GtkTreePath
*path
)
1906 GtkTreeModel
*model
= gtk_tree_view_get_model(list_view
);
1908 if (path
&& model
) {
1909 GtkTreeSelection
*selection
;
1910 gboolean has_prop
= FALSE
;
1914 selection
= gtk_tree_view_get_selection(list_view
);
1915 gtk_tree_selection_select_path(selection
, path
);
1917 /* update dialog from selection */
1918 gtk_tree_model_get_iter(model
, &iter
, path
);
1919 gtk_tree_model_get(model
, &iter
,
1920 PREFS_FILTERING_PROP
, &has_prop
,
1924 FilteringProp
*prop
;
1925 gchar
*filtering_str
= NULL
;
1927 gint account_id
= 0;
1929 gtk_tree_model_get(model
, &iter
,
1930 PREFS_FILTERING_RULE
, &filtering_str
,
1932 gtk_tree_model_get(model
, &iter
,
1933 PREFS_FILTERING_NAME
, &name
,
1935 gtk_tree_model_get(model
, &iter
,
1936 PREFS_FILTERING_ACCOUNT_ID
, &account_id
,
1939 prop
= matcher_parser_get_filtering(filtering_str
);
1941 if (prop
->name
!= NULL
)
1943 prop
->name
= g_strdup(name
);
1944 prop
->account_id
= account_id
;
1945 prefs_filtering_select_set(prop
);
1946 filteringprop_free(prop
);
1949 g_free(filtering_str
);
1951 prefs_filtering_reset_dialog();