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
);
345 gtk_window_set_type_hint(GTK_WINDOW(window
), GDK_WINDOW_TYPE_HINT_DIALOG
);
347 vbox
= gtk_vbox_new (FALSE
, 6);
348 gtk_widget_show (vbox
);
349 gtk_container_add (GTK_CONTAINER (window
), vbox
);
351 gtkut_stock_button_set_create_with_help(&confirm_area
, &help_btn
,
352 &cancel_btn
, GTK_STOCK_CANCEL
,
353 &ok_btn
, GTK_STOCK_OK
,
355 gtk_widget_show (confirm_area
);
356 gtk_box_pack_end (GTK_BOX(vbox
), confirm_area
, FALSE
, FALSE
, 0);
357 gtk_widget_grab_default (ok_btn
);
359 gtk_window_set_title (GTK_WINDOW(window
),
360 _("Filtering/Processing configuration"));
362 g_signal_connect(G_OBJECT(window
), "delete_event",
363 G_CALLBACK(prefs_filtering_deleted
), NULL
);
364 g_signal_connect(G_OBJECT(window
), "size_allocate",
365 G_CALLBACK(prefs_filtering_size_allocate_cb
), NULL
);
366 g_signal_connect(G_OBJECT(window
), "key_press_event",
367 G_CALLBACK(prefs_filtering_key_pressed
), NULL
);
368 MANAGE_WINDOW_SIGNALS_CONNECT (window
);
369 g_signal_connect(G_OBJECT(ok_btn
), "clicked",
370 G_CALLBACK(prefs_filtering_ok
), NULL
);
371 g_signal_connect(G_OBJECT(cancel_btn
), "clicked",
372 G_CALLBACK(prefs_filtering_cancel
), NULL
);
374 vbox1
= gtk_vbox_new (FALSE
, VSPACING
);
375 gtk_widget_show (vbox1
);
376 gtk_box_pack_start (GTK_BOX (vbox
), vbox1
, FALSE
, TRUE
, 0);
377 gtk_container_set_border_width (GTK_CONTAINER (vbox1
), 2);
379 table
= gtk_table_new(4, 3, FALSE
);
380 gtk_table_set_row_spacings (GTK_TABLE (table
), VSPACING_NARROW_2
);
381 gtk_table_set_col_spacings (GTK_TABLE (table
), 4);
382 gtk_widget_show(table
);
383 gtk_box_pack_start (GTK_BOX (vbox1
), table
, TRUE
, TRUE
, 0);
385 name_label
= gtk_label_new (_("Name"));
386 gtk_widget_show (name_label
);
387 gtk_misc_set_alignment (GTK_MISC (name_label
), 1, 0.5);
388 gtk_table_attach (GTK_TABLE (table
), name_label
, 0, 1, 0, 1,
389 (GtkAttachOptions
) (GTK_FILL
),
390 (GtkAttachOptions
) (0), 0, 0);
392 name_entry
= gtk_entry_new ();
393 gtk_widget_show (name_entry
);
394 gtk_table_attach (GTK_TABLE (table
), name_entry
, 1, 2, 0, 1,
395 (GtkAttachOptions
) (GTK_FILL
|GTK_EXPAND
),
396 (GtkAttachOptions
) (0), 0, 0);
398 account_label
= gtk_label_new (_("Account"));
399 gtk_widget_show (account_label
);
400 gtk_misc_set_alignment (GTK_MISC (account_label
), 1, 0.5);
401 gtk_table_attach (GTK_TABLE (table
), account_label
, 0, 1, 1, 2,
402 (GtkAttachOptions
) (GTK_FILL
),
403 (GtkAttachOptions
) (0), 0, 0);
405 account_opt_menu
= prefs_filtering_account_option_menu(&filtering
);
406 gtk_widget_show (account_opt_menu
);
407 gtk_table_attach (GTK_TABLE (table
), account_opt_menu
, 1, 2, 1, 2,
408 (GtkAttachOptions
) (GTK_FILL
|GTK_EXPAND
),
409 (GtkAttachOptions
) (0), 0, 0);
410 combobox_select_by_data(GTK_COMBO_BOX(filtering
.account_combobox
), 0);
412 cond_label
= gtk_label_new (_("Condition"));
413 gtk_widget_show (cond_label
);
414 gtk_misc_set_alignment (GTK_MISC (cond_label
), 1, 0.5);
415 gtk_table_attach (GTK_TABLE (table
), cond_label
, 0, 1, 2, 3,
416 (GtkAttachOptions
) (GTK_FILL
),
417 (GtkAttachOptions
) (0), 0, 0);
419 cond_entry
= gtk_entry_new ();
420 gtk_widget_show (cond_entry
);
421 gtk_table_attach (GTK_TABLE (table
), cond_entry
, 1, 2, 2, 3,
422 (GtkAttachOptions
) (GTK_FILL
|GTK_EXPAND
),
423 (GtkAttachOptions
) (0), 0, 0);
425 cond_btn
= gtk_button_new_with_mnemonic (_(" Def_ine... "));
426 gtk_widget_show (cond_btn
);
427 gtk_table_attach (GTK_TABLE (table
), cond_btn
, 2, 3, 2, 3,
428 (GtkAttachOptions
) (GTK_FILL
),
429 (GtkAttachOptions
) (0), 2, 2);
430 g_signal_connect(G_OBJECT (cond_btn
), "clicked",
431 G_CALLBACK(prefs_filtering_condition_define
),
434 action_label
= gtk_label_new (_("Action"));
435 gtk_widget_show (action_label
);
436 gtk_misc_set_alignment (GTK_MISC (action_label
), 1, 0.5);
437 gtk_table_attach (GTK_TABLE (table
), action_label
, 0, 1, 3, 4,
438 (GtkAttachOptions
) (GTK_FILL
),
439 (GtkAttachOptions
) (0), 0, 0);
441 action_entry
= gtk_entry_new ();
442 gtk_widget_show (action_entry
);
443 gtk_table_attach (GTK_TABLE (table
), action_entry
, 1, 2, 3, 4,
444 (GtkAttachOptions
) (GTK_FILL
|GTK_EXPAND
),
445 (GtkAttachOptions
) (0), 0, 0);
447 action_btn
= gtk_button_new_with_mnemonic (_(" De_fine... "));
448 gtk_widget_show (action_btn
);
449 gtk_table_attach (GTK_TABLE (table
), action_btn
, 2, 3, 3, 4,
450 (GtkAttachOptions
) (GTK_FILL
),
451 (GtkAttachOptions
) (0), 2, 2);
452 g_signal_connect(G_OBJECT (action_btn
), "clicked",
453 G_CALLBACK(prefs_filtering_action_define
),
456 /* register / substitute / delete */
457 reg_hbox
= gtk_hbox_new (FALSE
, 4);
458 gtk_widget_show (reg_hbox
);
459 gtk_box_pack_start (GTK_BOX (vbox1
), reg_hbox
, FALSE
, FALSE
, 0);
461 arrow
= gtk_arrow_new (GTK_ARROW_DOWN
, GTK_SHADOW_OUT
);
462 gtk_widget_show (arrow
);
463 gtk_box_pack_start (GTK_BOX (reg_hbox
), arrow
, FALSE
, FALSE
, 0);
464 gtk_widget_set_size_request (arrow
, -1, 16);
466 btn_hbox
= gtk_hbox_new (TRUE
, 4);
467 gtk_widget_show (btn_hbox
);
468 gtk_box_pack_start (GTK_BOX (reg_hbox
), btn_hbox
, FALSE
, FALSE
, 0);
470 reg_btn
= gtk_button_new_from_stock (GTK_STOCK_ADD
);
471 gtk_widget_show (reg_btn
);
472 gtk_box_pack_start (GTK_BOX (btn_hbox
), reg_btn
, FALSE
, TRUE
, 0);
473 g_signal_connect(G_OBJECT (reg_btn
), "clicked",
474 G_CALLBACK(prefs_filtering_register_cb
), NULL
);
475 CLAWS_SET_TIP(reg_btn
,
476 _("Append the new rule above to the list"));
478 subst_btn
= gtkut_get_replace_btn (_("_Replace"));
479 gtk_widget_show (subst_btn
);
480 gtk_box_pack_start (GTK_BOX (btn_hbox
), subst_btn
, FALSE
, TRUE
, 0);
481 g_signal_connect(G_OBJECT (subst_btn
), "clicked",
482 G_CALLBACK(prefs_filtering_substitute_cb
),
484 CLAWS_SET_TIP(subst_btn
,
485 _("Replace the selected rule in list with the rule above"));
487 del_btn
= gtk_button_new_with_mnemonic (_("D_elete"));
488 gtk_button_set_image(GTK_BUTTON(del_btn
),
489 gtk_image_new_from_stock(GTK_STOCK_REMOVE
,GTK_ICON_SIZE_BUTTON
));
490 gtk_box_pack_start (GTK_BOX (btn_hbox
), del_btn
, FALSE
, TRUE
, 0);
491 g_signal_connect(G_OBJECT (del_btn
), "clicked",
492 G_CALLBACK(prefs_filtering_delete_cb
), NULL
);
493 CLAWS_SET_TIP(del_btn
,
494 _("Delete the selected rule from the list"));
496 clear_btn
= gtk_button_new_with_mnemonic (_("C_lear"));
497 gtk_button_set_image(GTK_BUTTON(clear_btn
),
498 gtk_image_new_from_stock(GTK_STOCK_CLEAR
,GTK_ICON_SIZE_BUTTON
));
499 gtk_widget_show (clear_btn
);
500 gtk_box_pack_start (GTK_BOX (btn_hbox
), clear_btn
, FALSE
, TRUE
, 0);
501 g_signal_connect(G_OBJECT (clear_btn
), "clicked",
502 G_CALLBACK(prefs_filtering_clear_cb
), NULL
);
503 CLAWS_SET_TIP(clear_btn
,
504 _("Clear all the input fields in the dialog"));
506 cond_hbox
= gtk_hbox_new (FALSE
, 8);
507 gtk_widget_show (cond_hbox
);
508 gtk_box_pack_start (GTK_BOX (vbox
), cond_hbox
, TRUE
, TRUE
, 0);
510 cond_scrolledwin
= gtk_scrolled_window_new (NULL
, NULL
);
511 gtk_widget_show (cond_scrolledwin
);
512 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(cond_scrolledwin
),
513 GTK_SHADOW_ETCHED_IN
);
514 gtk_widget_set_size_request (cond_scrolledwin
, -1, 150);
515 gtk_box_pack_start (GTK_BOX (cond_hbox
), cond_scrolledwin
,
517 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (cond_scrolledwin
),
518 GTK_POLICY_AUTOMATIC
,
519 GTK_POLICY_AUTOMATIC
);
521 cond_list_view
= prefs_filtering_list_view_create();
522 gtk_widget_show (cond_list_view
);
523 gtk_container_add (GTK_CONTAINER (cond_scrolledwin
), cond_list_view
);
525 btn_vbox
= gtk_vbox_new (FALSE
, 8);
526 gtk_widget_show (btn_vbox
);
527 gtk_box_pack_start (GTK_BOX (cond_hbox
), btn_vbox
, FALSE
, FALSE
, 0);
529 top_btn
= gtk_button_new_from_stock (GTK_STOCK_GOTO_TOP
);
530 gtk_widget_show (top_btn
);
531 gtk_box_pack_start (GTK_BOX (btn_vbox
), top_btn
, FALSE
, FALSE
, 0);
532 g_signal_connect(G_OBJECT (top_btn
), "clicked",
533 G_CALLBACK(prefs_filtering_top
), NULL
);
534 CLAWS_SET_TIP(top_btn
,
535 _("Move the selected rule to the top"));
538 page_up_btn
= gtk_button_new_with_mnemonic (_("Page u_p"));
539 gtk_button_set_image(GTK_BUTTON(page_up_btn
),
540 gtk_image_new_from_stock(GTK_STOCK_GO_UP
,GTK_ICON_SIZE_BUTTON
));
541 gtk_widget_show (page_up_btn
);
542 gtk_box_pack_start (GTK_BOX (btn_vbox
), page_up_btn
, FALSE
, FALSE
, 0);
543 g_signal_connect(G_OBJECT (page_up_btn
), "clicked",
544 G_CALLBACK(prefs_filtering_page_up
), NULL
);
545 CLAWS_SET_TIP(page_up_btn
,
546 _("Move the selected rule one page up"));
549 up_btn
= gtk_button_new_from_stock (GTK_STOCK_GO_UP
);
550 gtk_widget_show (up_btn
);
551 gtk_box_pack_start (GTK_BOX (btn_vbox
), up_btn
, FALSE
, FALSE
, 0);
552 g_signal_connect(G_OBJECT (up_btn
), "clicked",
553 G_CALLBACK(prefs_filtering_up
), NULL
);
554 CLAWS_SET_TIP(up_btn
,
555 _("Move the selected rule up"));
557 down_btn
= gtk_button_new_from_stock (GTK_STOCK_GO_DOWN
);
558 gtk_widget_show (down_btn
);
559 gtk_box_pack_start (GTK_BOX (btn_vbox
), down_btn
, FALSE
, FALSE
, 0);
560 g_signal_connect(G_OBJECT (down_btn
), "clicked",
561 G_CALLBACK(prefs_filtering_down
), NULL
);
562 CLAWS_SET_TIP(down_btn
,
563 _("Move the selected rule down"));
566 page_down_btn
= gtk_button_new_with_mnemonic (_("Page dow_n"));
567 gtk_button_set_image(GTK_BUTTON(page_down_btn
),
568 gtk_image_new_from_stock(GTK_STOCK_GO_DOWN
,GTK_ICON_SIZE_BUTTON
));
569 gtk_widget_show (page_down_btn
);
570 gtk_box_pack_start (GTK_BOX (btn_vbox
), page_down_btn
, FALSE
, FALSE
, 0);
571 g_signal_connect(G_OBJECT (page_down_btn
), "clicked",
572 G_CALLBACK(prefs_filtering_page_down
), NULL
);
573 CLAWS_SET_TIP(page_down_btn
,
574 _("Move the selected rule one page down"));
577 bottom_btn
= gtk_button_new_from_stock (GTK_STOCK_GOTO_BOTTOM
);
578 gtk_widget_show (bottom_btn
);
579 gtk_box_pack_start (GTK_BOX (btn_vbox
), bottom_btn
, FALSE
, FALSE
, 0);
580 g_signal_connect(G_OBJECT (bottom_btn
), "clicked",
581 G_CALLBACK(prefs_filtering_bottom
), NULL
);
582 CLAWS_SET_TIP(bottom_btn
,
583 _("Move the selected rule to the bottom"));
585 if (!geometry
.min_height
) {
586 geometry
.min_width
= 500;
587 geometry
.min_height
= 460;
590 gtk_window_set_geometry_hints(GTK_WINDOW(window
), NULL
, &geometry
,
592 gtk_widget_set_size_request(window
, prefs_common
.filteringwin_width
,
593 prefs_common
.filteringwin_height
);
595 gtk_widget_show_all(window
);
597 filtering
.window
= window
;
598 filtering
.help_btn
= help_btn
;
599 filtering
.ok_btn
= ok_btn
;
601 filtering
.name_entry
= name_entry
;
602 filtering
.cond_entry
= cond_entry
;
603 filtering
.action_entry
= action_entry
;
604 filtering
.cond_list_view
= cond_list_view
;
605 filtering
.account_label
= account_label
;
608 static void rename_tag(GSList
* filters
,
609 const gchar
* old_tag
, const gchar
* new_tag
);
611 void prefs_filtering_rename_tag(const gchar
*old_tag
, const gchar
*new_tag
)
614 const gchar
*tags
[2] = {NULL
, NULL
};
617 for (cur
= folder_get_list() ; cur
!= NULL
; cur
= g_list_next(cur
)) {
619 folder
= (Folder
*) cur
->data
;
620 g_node_traverse(folder
->node
, G_PRE_ORDER
, G_TRAVERSE_ALL
, -1,
621 prefs_filtering_rename_tag_func
, tags
);
624 rename_tag(pre_global_processing
, old_tag
, new_tag
);
625 rename_tag(post_global_processing
, old_tag
, new_tag
);
626 rename_tag(filtering_rules
, old_tag
, new_tag
);
628 prefs_matcher_write_config();
632 static void rename_path(GSList
* filters
,
633 const gchar
* old_path
, const gchar
* new_path
);
635 void prefs_filtering_rename_path(const gchar
*old_path
, const gchar
*new_path
)
638 const gchar
*paths
[2] = {NULL
, NULL
};
641 for (cur
= folder_get_list() ; cur
!= NULL
; cur
= g_list_next(cur
)) {
643 folder
= (Folder
*) cur
->data
;
644 g_node_traverse(folder
->node
, G_PRE_ORDER
, G_TRAVERSE_ALL
, -1,
645 prefs_filtering_rename_path_func
, paths
);
648 rename_path(pre_global_processing
, old_path
, new_path
);
649 rename_path(post_global_processing
, old_path
, new_path
);
650 rename_path(filtering_rules
, old_path
, new_path
);
652 prefs_matcher_write_config();
655 static void rename_path(GSList
* filters
,
656 const gchar
* old_path
, const gchar
* new_path
)
660 for (cur
= filters
; cur
!= NULL
; cur
= cur
->next
) {
661 FilteringProp
*filtering
= (FilteringProp
*)cur
->data
;
662 filtering_action_list_rename_path(filtering
->action_list
,
667 static gboolean
prefs_filtering_rename_path_func(GNode
*node
, gpointer data
)
670 const gchar
* old_path
;
671 const gchar
* new_path
;
672 const gchar
** paths
;
679 cm_return_val_if_fail(old_path
!= NULL
, FALSE
);
680 cm_return_val_if_fail(new_path
!= NULL
, FALSE
);
681 cm_return_val_if_fail(node
!= NULL
, FALSE
);
684 if (!item
|| !item
->prefs
)
686 filters
= item
->prefs
->processing
;
688 rename_path(filters
, old_path
, new_path
);
693 static void rename_tag(GSList
* filters
,
694 const gchar
* old_tag
, const gchar
* new_tag
)
699 for (cur
= filters
; cur
!= NULL
; cur
= cur
->next
) {
700 FilteringProp
*filtering
= (FilteringProp
*)cur
->data
;
702 for(action_cur
= filtering
->action_list
; action_cur
!= NULL
;
703 action_cur
= action_cur
->next
) {
705 FilteringAction
*action
= action_cur
->data
;
707 if (action
->type
!= MATCHACTION_SET_TAG
&&
708 action
->type
!= MATCHACTION_UNSET_TAG
)
710 if (!action
->destination
)
712 if (!strcmp(action
->destination
, old_tag
)) {
713 g_free(action
->destination
);
714 action
->destination
= g_strdup(new_tag
);
720 static gboolean
prefs_filtering_rename_tag_func(GNode
*node
, gpointer data
)
723 const gchar
* old_tag
;
724 const gchar
* new_tag
;
732 cm_return_val_if_fail(old_tag
!= NULL
, FALSE
);
733 cm_return_val_if_fail(new_tag
!= NULL
, FALSE
);
734 cm_return_val_if_fail(node
!= NULL
, FALSE
);
737 if (!item
|| !item
->prefs
)
739 filters
= item
->prefs
->processing
;
741 rename_tag(filters
, old_tag
, new_tag
);
746 void prefs_filtering_delete_path(const gchar
*path
)
749 for (cur
= folder_get_list() ; cur
!= NULL
; cur
= g_list_next(cur
)) {
751 folder
= (Folder
*) cur
->data
;
752 g_node_traverse(folder
->node
, G_PRE_ORDER
, G_TRAVERSE_ALL
, -1,
753 prefs_filtering_delete_path_func
, (gchar
*)path
);
755 delete_path(&pre_global_processing
, path
);
756 delete_path(&post_global_processing
, path
);
757 delete_path(&filtering_rules
, path
);
759 prefs_matcher_write_config();
762 static void delete_path(GSList
** p_filters
, const gchar
* path
)
773 filters
= *p_filters
;
774 pathlen
= strlen(path
);
775 duplist
= g_slist_copy(filters
);
776 for (cur
= duplist
; cur
!= NULL
; cur
= g_slist_next(cur
)) {
777 FilteringProp
*filtering
= (FilteringProp
*) cur
->data
;
779 for(action_cur
= filtering
->action_list
; action_cur
!= NULL
;
780 action_cur
= action_cur
->next
) {
782 FilteringAction
*action
;
784 action
= action_cur
->data
;
786 if (action
->type
== MATCHACTION_SET_TAG
||
787 action
->type
== MATCHACTION_UNSET_TAG
)
789 if (!action
->destination
)
792 destlen
= strlen(action
->destination
);
794 if (destlen
> pathlen
) {
795 prefixlen
= destlen
- pathlen
;
796 suffix
= action
->destination
+ prefixlen
;
798 if (suffix
&& !strncmp(path
, suffix
, pathlen
)) {
799 filteringprop_free(filtering
);
800 filters
= g_slist_remove(filters
, filtering
);
802 } else if (strcmp(action
->destination
, path
) == 0) {
803 filteringprop_free(filtering
);
804 filters
= g_slist_remove(filters
, filtering
);
808 g_slist_free(duplist
);
810 * p_filters
= filters
;
813 static gboolean
prefs_filtering_delete_path_func(GNode
*node
, gpointer data
)
815 const gchar
*path
= data
;
819 cm_return_val_if_fail(path
!= NULL
, FALSE
);
820 cm_return_val_if_fail(node
!= NULL
, FALSE
);
823 if (!item
|| !item
->prefs
)
825 p_filters
= &item
->prefs
->processing
;
827 delete_path(p_filters
, path
);
832 static void prefs_filtering_clear_list(GtkListStore
*list_store
)
834 gtk_list_store_clear(list_store
);
836 /* add the place holder (New) at row 0 */
837 prefs_filtering_list_view_insert_rule(list_store
, -1,
846 static void prefs_filtering_set_dialog(const gchar
*header
, const gchar
*key
)
848 GtkTreeView
*list_view
= GTK_TREE_VIEW(filtering
.cond_list_view
);
850 GSList
* prefs_filtering
;
852 GtkListStore
*list_store
;
854 list_store
= GTK_LIST_STORE(gtk_tree_view_get_model(list_view
));
855 prefs_filtering_clear_list(list_store
);
857 prefs_filtering
= *p_processing_list
;
859 for(cur
= prefs_filtering
; cur
!= NULL
; cur
= g_slist_next(cur
)) {
860 FilteringProp
* prop
= (FilteringProp
*) cur
->data
;
861 gchar
*account_name
= NULL
;
863 if (prop
->account_id
> 0) {
864 PrefsAccount
*ac_prefs
= account_find_from_id(prop
->account_id
);
867 account_name
= ac_prefs
->account_name
;
869 if (account_name
== NULL
)
870 account_name
= (gchar
*)C_("Filtering Account Menu", "All");
872 cond_str
= filteringprop_to_string(prop
);
873 subst_char(cond_str
, '\t', ':');
875 prefs_filtering_list_view_insert_rule(list_store
, -1,
885 prefs_filtering_reset_dialog();
891 quoted_key
= matcher_quote_str(key
);
893 match_str
= g_strconcat(header
, " ", get_matchparser_tab_str(MATCHTYPE_MATCHCASE
),
894 " \"", quoted_key
, "\"", NULL
);
897 gtk_entry_set_text(GTK_ENTRY(filtering
.cond_entry
), match_str
);
902 static void prefs_filtering_reset_dialog(void)
904 gtk_entry_set_text(GTK_ENTRY(filtering
.name_entry
), "");
905 combobox_select_by_data(GTK_COMBO_BOX(filtering
.account_combobox
), 0);
906 gtk_entry_set_text(GTK_ENTRY(filtering
.cond_entry
), "");
907 gtk_entry_set_text(GTK_ENTRY(filtering
.action_entry
), "");
910 static gboolean
prefs_filtering_search_func_cb (GtkTreeModel
*model
, gint column
, const gchar
*key
,
911 GtkTreeIter
*iter
, gpointer search_data
)
917 gtk_tree_model_get (model
, iter
, column
, &store_string
, -1);
919 if (!store_string
|| !key
) return FALSE
;
922 retval
= (g_ascii_strncasecmp (key
, store_string
, strlen(key
)) != 0);
924 g_free(store_string
);
925 debug_print("selecting row\n");
926 path
= gtk_tree_model_get_path(model
, iter
);
927 prefs_filtering_select_row(GTK_TREE_VIEW(filtering
.cond_list_view
), path
);
928 gtk_tree_path_free(path
);
933 static void prefs_filtering_set_list(void)
938 gchar
* filtering_str
;
939 GSList
* prefs_filtering
;
941 prefs_filtering
= *p_processing_list
;
942 for (cur
= prefs_filtering
; cur
!= NULL
; cur
= g_slist_next(cur
))
943 filteringprop_free((FilteringProp
*) cur
->data
);
944 g_slist_free(prefs_filtering
);
945 prefs_filtering
= NULL
;
948 while (NULL
!= (filtering_str
= prefs_filtering_list_view_get_rule
949 (filtering
.cond_list_view
, row
))) {
950 /* FIXME: this strcmp() is bogus: "(New)" should never
951 * be inserted in the storage */
952 if (strcmp(filtering_str
, _("(New)")) != 0) {
957 prefs_filtering_list_view_get_rule_info(
958 filtering
.cond_list_view
, row
,
959 &enabled
, &name
, &account_id
);
960 prop
= matcher_parser_get_filtering(filtering_str
);
962 prop
->enabled
= enabled
;
963 if (prop
->name
!= NULL
)
966 prop
->account_id
= account_id
;
968 g_slist_append(prefs_filtering
, prop
);
972 g_free(filtering_str
);
976 *p_processing_list
= prefs_filtering
;
979 static gint
prefs_filtering_list_view_set_row(gint row
, FilteringProp
* prop
)
981 GtkTreeView
*list_view
= GTK_TREE_VIEW(filtering
.cond_list_view
);
983 GtkListStore
*list_store
;
986 gchar
*account_name
= (gchar
*)C_("Filtering Account Menu", "All");
987 gboolean enabled
= TRUE
;
990 str
= filteringprop_to_string(prop
);
997 account_id
= prop
->account_id
;
999 account_name
= account_find_from_id(account_id
)->account_name
;
1000 enabled
= prop
->enabled
;
1003 list_store
= GTK_LIST_STORE(gtk_tree_view_get_model(list_view
));
1005 row
= prefs_filtering_list_view_insert_rule(list_store
, row
,
1018 static void prefs_filtering_condition_define_done(MatcherList
* matchers
)
1022 if (matchers
== NULL
)
1025 str
= matcherlist_to_string(matchers
);
1028 gtk_entry_set_text(GTK_ENTRY(filtering
.cond_entry
), str
);
1033 static void prefs_filtering_condition_define(gpointer action
, gpointer data
)
1036 MatcherList
* matchers
= NULL
;
1038 cond_str
= gtk_editable_get_chars(GTK_EDITABLE(filtering
.cond_entry
), 0, -1);
1040 if (*cond_str
!= '\0') {
1041 matchers
= matcher_parser_get_cond(cond_str
, NULL
);
1042 if (matchers
== NULL
)
1043 alertpanel_error(_("Condition string is not valid."));
1048 prefs_matcher_open(matchers
, prefs_filtering_condition_define_done
);
1050 if (matchers
!= NULL
)
1051 matcherlist_free(matchers
);
1054 static void prefs_filtering_action_define_done(GSList
* action_list
)
1058 if (action_list
== NULL
)
1061 str
= filteringaction_list_to_string(action_list
);
1064 gtk_entry_set_text(GTK_ENTRY(filtering
.action_entry
), str
);
1069 static void prefs_filtering_action_define(gpointer action
, gpointer data
)
1072 GSList
* action_list
= NULL
;
1074 action_str
= gtk_editable_get_chars(GTK_EDITABLE(filtering
.action_entry
), 0, -1);
1076 if (*action_str
!= '\0') {
1077 action_list
= matcher_parser_get_action_list(action_str
);
1078 if (action_list
== NULL
)
1079 alertpanel_error(_("Action string is not valid."));
1084 prefs_filtering_action_open(action_list
,
1085 prefs_filtering_action_define_done
);
1087 if (action_list
!= NULL
) {
1089 for(cur
= action_list
; cur
!= NULL
; cur
= cur
->next
) {
1090 filteringaction_free(cur
->data
);
1096 /* register / substitute delete buttons */
1099 static FilteringProp
* prefs_filtering_dialog_to_filtering(gboolean alert
)
1102 gboolean enabled
= TRUE
;
1103 gchar
* name
= NULL
;
1104 gint account_id
= 0;
1105 gchar
* cond_str
= NULL
;
1106 gchar
* action_str
= NULL
;
1107 FilteringProp
* prop
= NULL
;
1108 GSList
* action_list
;
1110 name
= gtk_editable_get_chars(GTK_EDITABLE(filtering
.name_entry
), 0, -1);
1112 account_id
= combobox_get_active_data(GTK_COMBO_BOX(filtering
.account_combobox
));
1114 cond_str
= gtk_editable_get_chars(GTK_EDITABLE(filtering
.cond_entry
), 0, -1);
1115 if (*cond_str
== '\0') {
1116 if(alert
== TRUE
) alertpanel_error(_("Condition string is empty."));
1120 action_str
= gtk_editable_get_chars(GTK_EDITABLE(filtering
.action_entry
), 0, -1);
1121 if (*action_str
== '\0') {
1122 if(alert
== TRUE
) alertpanel_error(_("Action string is empty."));
1126 cond
= matcher_parser_get_cond(cond_str
, NULL
);
1129 if(alert
== TRUE
) alertpanel_error(_("Condition string is not valid."));
1133 action_list
= matcher_parser_get_action_list(action_str
);
1136 if (action_list
== NULL
) {
1137 if(alert
== TRUE
) alertpanel_error(_("Action string is not valid."));
1141 prop
= filteringprop_new(enabled
, name
, account_id
, cond
, action_list
);
1150 static void prefs_filtering_register_cb(gpointer action
, gpointer data
)
1152 FilteringProp
*prop
;
1154 prop
= prefs_filtering_dialog_to_filtering(TRUE
);
1157 prefs_filtering_list_view_set_row(-1, prop
);
1159 filteringprop_free(prop
);
1161 prefs_filtering_reset_dialog();
1165 static void prefs_filtering_substitute_cb(gpointer action
, gpointer data
)
1167 gint selected_row
= gtkut_list_view_get_selected_row
1168 (filtering
.cond_list_view
);
1169 FilteringProp
*prop
;
1174 if (selected_row
<= 0)
1177 prop
= prefs_filtering_dialog_to_filtering(TRUE
);
1182 /* prop->emabled is always TRUE here, re-use the value from the selected row
1183 as we don't substitute this value from dialog */
1184 prefs_filtering_list_view_get_rule_info(
1185 filtering
.cond_list_view
, selected_row
,
1186 &enabled
, &name
, &account_id
);
1187 g_free(name
); /* We're not using this. */
1188 prop
->enabled
= enabled
;
1190 prefs_filtering_list_view_set_row(selected_row
, prop
);
1192 filteringprop_free(prop
);
1194 prefs_filtering_row_selected(gtk_tree_view_get_selection(
1195 GTK_TREE_VIEW(filtering
.cond_list_view
)),
1196 GTK_TREE_VIEW(filtering
.cond_list_view
));
1200 static void prefs_filtering_delete_cb(gpointer action
, gpointer data
)
1202 GtkTreeView
*list_view
= GTK_TREE_VIEW(filtering
.cond_list_view
);
1203 GtkTreeModel
*model
;
1207 selected_row
= gtkut_list_view_get_selected_row(filtering
.cond_list_view
);
1208 if (selected_row
<= 0)
1211 if (alertpanel(_("Delete rule"),
1212 _("Do you really want to delete this rule?"),
1213 GTK_STOCK_CANCEL
, GTK_STOCK_DELETE
, NULL
, ALERTFOCUS_SECOND
) == G_ALERTDEFAULT
)
1216 model
= gtk_tree_view_get_model(list_view
);
1217 if (!gtk_tree_model_iter_nth_child(model
, &iter
, NULL
, selected_row
))
1220 gtk_list_store_remove(GTK_LIST_STORE(model
), &iter
);
1222 prefs_filtering_reset_dialog();
1226 static void prefs_filtering_delete_all_cb(gpointer action
, gpointer data
)
1228 GtkListStore
*list_store
;
1230 if (alertpanel(_("Delete all rules"),
1231 _("Do you really want to delete all the rules?"),
1232 GTK_STOCK_CANCEL
, GTK_STOCK_DELETE
, NULL
, ALERTFOCUS_SECOND
) == G_ALERTDEFAULT
)
1235 list_store
= GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(filtering
.cond_list_view
)));
1236 prefs_filtering_clear_list(list_store
);
1238 prefs_filtering_reset_dialog();
1242 static void prefs_filtering_clear_cb(gpointer action
, gpointer data
)
1244 prefs_filtering_reset_dialog();
1247 static void prefs_filtering_duplicate_cb(gpointer action
, gpointer data
)
1249 gint selected_row
= gtkut_list_view_get_selected_row
1250 (filtering
.cond_list_view
);
1251 FilteringProp
*prop
;
1256 if (selected_row
<= 0)
1259 prop
= prefs_filtering_dialog_to_filtering(TRUE
);
1263 /* prop->emabled is always TRUE here, re-use the value from the selected row
1264 as we don't substitute this value from dialog */
1265 prefs_filtering_list_view_get_rule_info(
1266 filtering
.cond_list_view
, selected_row
,
1267 &enabled
, &name
, &account_id
);
1268 g_free(name
); /* We're not using this. */
1269 prop
->enabled
= enabled
;
1271 prefs_filtering_list_view_set_row(-selected_row
-2, prop
);
1273 filteringprop_free(prop
);
1275 prefs_filtering_reset_dialog();
1279 static void prefs_filtering_top(gpointer action
, gpointer data
)
1282 GtkTreeIter top
, sel
;
1283 GtkTreeModel
*model
;
1285 row
= gtkut_list_view_get_selected_row(filtering
.cond_list_view
);
1289 model
= gtk_tree_view_get_model(GTK_TREE_VIEW(filtering
.cond_list_view
));
1291 if (!gtk_tree_model_iter_nth_child(model
, &top
, NULL
, 0)
1292 || !gtk_tree_model_iter_nth_child(model
, &sel
, NULL
, row
))
1295 gtk_list_store_move_after(GTK_LIST_STORE(model
), &sel
, &top
);
1296 gtkut_list_view_select_row(filtering
.cond_list_view
, 1);
1300 static void prefs_filtering_page_up(gpointer action
, gpointer data
)
1302 gint row
, target_row
;
1303 GtkTreeIter selected
, target
;
1304 GtkTreeModel
*model
;
1306 GdkRectangle cell_rect
, view_rect
;
1308 model
= gtk_tree_view_get_model(GTK_TREE_VIEW(filtering
.cond_list_view
));
1309 row
= gtkut_list_view_get_selected_row(filtering
.cond_list_view
);
1313 if (!gtk_tree_model_iter_nth_child(model
, &selected
, NULL
, row
))
1316 /* compute number of rows per page (approximation) */
1317 path
= gtk_tree_model_get_path(model
, &selected
);
1318 gtk_tree_view_get_cell_area(GTK_TREE_VIEW(filtering
.cond_list_view
), path
, NULL
, &cell_rect
);
1319 gtk_tree_view_get_visible_rect(GTK_TREE_VIEW(filtering
.cond_list_view
), &view_rect
);
1320 gtk_tree_path_free(path
);
1321 target_row
= row
- (view_rect
.height
/cell_rect
.height
);
1325 if (!gtk_tree_model_iter_nth_child(model
, &target
, NULL
, target_row
))
1327 gtk_list_store_move_before(GTK_LIST_STORE(model
), &selected
, &target
);
1328 gtkut_list_view_select_row(filtering
.cond_list_view
, target_row
);
1331 static void prefs_filtering_up(gpointer action
, gpointer data
)
1334 GtkTreeIter top
, sel
;
1335 GtkTreeModel
*model
;
1337 row
= gtkut_list_view_get_selected_row(filtering
.cond_list_view
);
1341 model
= gtk_tree_view_get_model(GTK_TREE_VIEW(filtering
.cond_list_view
));
1343 if (!gtk_tree_model_iter_nth_child(model
, &top
, NULL
, row
- 1)
1344 || !gtk_tree_model_iter_nth_child(model
, &sel
, NULL
, row
))
1347 gtk_list_store_swap(GTK_LIST_STORE(model
), &top
, &sel
);
1348 gtkut_list_view_select_row(filtering
.cond_list_view
, row
- 1);
1352 static void prefs_filtering_down(gpointer action
, gpointer data
)
1355 GtkTreeIter top
, sel
;
1356 GtkTreeModel
*model
;
1358 model
= gtk_tree_view_get_model(GTK_TREE_VIEW(filtering
.cond_list_view
));
1359 n_rows
= gtk_tree_model_iter_n_children(model
, NULL
);
1360 row
= gtkut_list_view_get_selected_row(filtering
.cond_list_view
);
1361 if (row
< 1 || row
>= n_rows
- 1)
1364 if (!gtk_tree_model_iter_nth_child(model
, &top
, NULL
, row
)
1365 || !gtk_tree_model_iter_nth_child(model
, &sel
, NULL
, row
+ 1))
1368 gtk_list_store_swap(GTK_LIST_STORE(model
), &top
, &sel
);
1369 gtkut_list_view_select_row(filtering
.cond_list_view
, row
+ 1);
1373 static void prefs_filtering_page_down(gpointer action
, gpointer data
)
1375 gint row
, target_row
, n_rows
;
1376 GtkTreeIter selected
, target
;
1377 GtkTreeModel
*model
;
1379 GdkRectangle cell_rect
, view_rect
;
1381 model
= gtk_tree_view_get_model(GTK_TREE_VIEW(filtering
.cond_list_view
));
1382 n_rows
= gtk_tree_model_iter_n_children(model
, NULL
);
1383 row
= gtkut_list_view_get_selected_row(filtering
.cond_list_view
);
1384 if (row
< 1 || row
>= n_rows
-1)
1387 if (!gtk_tree_model_iter_nth_child(model
, &selected
, NULL
, row
))
1390 /* compute number of rows per page (approximation) */
1391 path
= gtk_tree_model_get_path(model
, &selected
);
1392 gtk_tree_view_get_cell_area(GTK_TREE_VIEW(filtering
.cond_list_view
), path
, NULL
, &cell_rect
);
1393 gtk_tree_view_get_visible_rect(GTK_TREE_VIEW(filtering
.cond_list_view
), &view_rect
);
1394 gtk_tree_path_free(path
);
1395 target_row
= row
+ (view_rect
.height
/cell_rect
.height
);
1396 if (target_row
> n_rows
-1)
1397 target_row
= n_rows
-1;
1399 if (!gtk_tree_model_iter_nth_child(model
, &target
, NULL
, target_row
))
1401 gtk_list_store_move_after(GTK_LIST_STORE(model
), &selected
, &target
);
1402 gtkut_list_view_select_row(filtering
.cond_list_view
, target_row
);
1406 static void prefs_filtering_bottom(gpointer action
, gpointer data
)
1409 GtkTreeIter top
, sel
;
1410 GtkTreeModel
*model
;
1412 model
= gtk_tree_view_get_model(GTK_TREE_VIEW(filtering
.cond_list_view
));
1413 n_rows
= gtk_tree_model_iter_n_children(model
, NULL
);
1414 row
= gtkut_list_view_get_selected_row(filtering
.cond_list_view
);
1415 if (row
< 1 || row
>= n_rows
- 1)
1418 if (!gtk_tree_model_iter_nth_child(model
, &top
, NULL
, row
)
1419 || !gtk_tree_model_iter_nth_child(model
, &sel
, NULL
, n_rows
- 1))
1422 gtk_list_store_move_after(GTK_LIST_STORE(model
), &top
, &sel
);
1423 gtkut_list_view_select_row(filtering
.cond_list_view
, n_rows
- 1);
1427 static void prefs_filtering_select_set(FilteringProp
*prop
)
1432 prefs_filtering_reset_dialog();
1434 matcher_str
= matcherlist_to_string(prop
->matchers
);
1435 if (matcher_str
== NULL
) {
1439 if (prop
->name
!= NULL
)
1440 gtk_entry_set_text(GTK_ENTRY(filtering
.name_entry
), prop
->name
);
1442 combobox_select_by_data(GTK_COMBO_BOX(filtering
.account_combobox
), prop
->account_id
);
1444 gtk_entry_set_text(GTK_ENTRY(filtering
.cond_entry
), matcher_str
);
1446 action_str
= filteringaction_list_to_string(prop
->action_list
);
1447 if (action_str
!= NULL
)
1448 gtk_entry_set_text(GTK_ENTRY(filtering
.action_entry
), action_str
);
1451 g_free(matcher_str
);
1454 static gint
prefs_filtering_deleted(GtkWidget
*widget
, GdkEventAny
*event
,
1457 prefs_filtering_cancel(NULL
, NULL
);
1461 static gboolean
prefs_filtering_key_pressed(GtkWidget
*widget
, GdkEventKey
*event
,
1464 if (event
&& event
->keyval
== GDK_KEY_Escape
) {
1465 prefs_filtering_cancel(NULL
, NULL
);
1471 static gboolean
prefs_filtering_check_mod(gboolean check_changed_list
)
1473 FilteringProp
* prop
;
1475 gchar
* filtering_str
;
1479 prop
= prefs_filtering_dialog_to_filtering(FALSE
);
1481 if (check_changed_list
) {
1482 if (modified
&& alertpanel(_("Filtering rules not saved"),
1483 _("The list of filtering rules have been modified. Close anyway?"),
1484 GTK_STOCK_CLOSE
, _("_Continue editing"), NULL
,
1485 ALERTFOCUS_SECOND
) != G_ALERTDEFAULT
) {
1490 /* check if a rule is being edited */
1492 str
= filteringprop_to_string(prop
);
1494 while (NULL
!= (filtering_str
= (prefs_filtering_list_view_get_rule
1495 (filtering
.cond_list_view
,
1497 if (strcmp(filtering_str
, str
) == 0)
1500 g_free(filtering_str
);
1503 if (!filtering_str
) {
1504 val
= alertpanel(_("Entry not saved"),
1505 _("The entry was not saved. Close anyway?"),
1506 GTK_STOCK_CLOSE
, _("_Continue editing"), NULL
, ALERTFOCUS_SECOND
);
1507 if (G_ALERTDEFAULT
!= val
) {
1508 g_free(filtering_str
);
1509 g_free(str
); /* fixed two leaks: huzzah! */
1510 filteringprop_free(prop
);
1515 g_free(filtering_str
);
1517 filteringprop_free(prop
); /* fixed a leak: huzzah! */
1519 gchar
*name
, *condition
, *action
;
1520 name
= gtk_editable_get_chars(GTK_EDITABLE(filtering
.name_entry
), 0, -1);
1521 condition
= gtk_editable_get_chars(GTK_EDITABLE(filtering
.cond_entry
), 0, -1);
1522 action
= gtk_editable_get_chars(GTK_EDITABLE(filtering
.action_entry
), 0, -1);
1524 strlen(condition
) ||
1526 val
= alertpanel(_("Entry not saved"),
1527 _("The entry was not saved. Close anyway?"),
1528 GTK_STOCK_CLOSE
, _("_Continue editing"), NULL
, ALERTFOCUS_SECOND
);
1529 if (G_ALERTDEFAULT
!= val
) {
1543 static void prefs_filtering_ok(gpointer action
, gpointer data
)
1545 if (prefs_filtering_check_mod(FALSE
))
1548 prefs_filtering_set_list();
1549 prefs_matcher_write_config();
1550 prefs_filtering_close();
1553 static void prefs_filtering_cancel(gpointer action
, gpointer data
)
1555 if (prefs_filtering_check_mod(TRUE
))
1558 prefs_matcher_read_config();
1559 prefs_filtering_close();
1562 static GtkListStore
* prefs_filtering_create_data_store(void)
1564 return gtk_list_store_new(N_PREFS_FILTERING_COLUMNS
,
1575 *\brief Insert filtering rule into store. Note that we access the
1576 * tree view / store by index, which is a bit suboptimal, but
1577 * at least it made GTK 2 porting easier.
1579 *\param list_store Store to operate on
1580 *\param row -1 to add a new rule to store,
1581 row >=0 to change an existing row
1582 row <-1 insert a new row after (-row-2)
1583 *\param enabled TRUE if rule is enabled
1584 *\param name The Name of rule
1585 *\param account_id The account ID
1586 *\param account_name The account name or All or (New)
1587 *\param rule String representation of rule
1588 *\param prop TRUE if valid filtering rule; if FALSE it's the first
1589 * entry in the store ("(New)").
1591 *\return int Row of inserted / changed rule.
1593 static gint
prefs_filtering_list_view_insert_rule(GtkListStore
*list_store
,
1598 const gchar
*account_name
,
1603 GtkTreeIter sibling
;
1605 /* check if valid row at all */
1607 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store
),
1610 } else if (row
< -1) {
1611 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store
),
1612 &sibling
, NULL
, -row
-2))
1618 gtk_list_store_append(list_store
, &iter
);
1619 gtk_list_store_set(list_store
, &iter
,
1620 PREFS_FILTERING_ENABLED
, enabled
,
1621 PREFS_FILTERING_NAME
, name
,
1622 PREFS_FILTERING_ACCOUNT_ID
, account_id
,
1623 PREFS_FILTERING_ACCOUNT_NAME
, account_name
,
1624 PREFS_FILTERING_RULE
, rule
,
1625 PREFS_FILTERING_PROP
, prop
,
1627 return gtk_tree_model_iter_n_children(GTK_TREE_MODEL(list_store
),
1629 } else if (row
< -1) {
1631 gtk_list_store_insert_after(list_store
, &iter
, &sibling
);
1632 gtk_list_store_set(list_store
, &iter
,
1633 PREFS_FILTERING_ENABLED
, enabled
,
1634 PREFS_FILTERING_NAME
, name
,
1635 PREFS_FILTERING_ACCOUNT_ID
, account_id
,
1636 PREFS_FILTERING_ACCOUNT_NAME
, account_name
,
1637 PREFS_FILTERING_RULE
, rule
,
1638 PREFS_FILTERING_PROP
, prop
,
1640 return gtk_tree_model_iter_n_children(GTK_TREE_MODEL(list_store
),
1643 /* change existing */
1644 gtk_list_store_set(list_store
, &iter
,
1645 PREFS_FILTERING_ENABLED
, enabled
,
1646 PREFS_FILTERING_NAME
, name
,
1647 PREFS_FILTERING_ACCOUNT_ID
, account_id
,
1648 PREFS_FILTERING_ACCOUNT_NAME
, account_name
,
1649 PREFS_FILTERING_RULE
, rule
,
1656 *\return gchar * Rule at specified row - should be freed.
1658 static gchar
*prefs_filtering_list_view_get_rule(GtkWidget
*list
, gint row
)
1660 GtkTreeView
*list_view
= GTK_TREE_VIEW(list
);
1661 GtkTreeModel
*model
= gtk_tree_view_get_model(list_view
);
1665 if (!gtk_tree_model_iter_nth_child(model
, &iter
, NULL
, row
))
1668 gtk_tree_model_get(model
, &iter
,
1669 PREFS_FILTERING_RULE
, &result
,
1675 static void prefs_filtering_list_view_get_rule_info(GtkWidget
*list
, gint row
,
1676 gboolean
*enabled
, gchar
**name
, gint
*account_id
)
1678 GtkTreeView
*list_view
= GTK_TREE_VIEW(list
);
1679 GtkTreeModel
*model
= gtk_tree_view_get_model(list_view
);
1685 if (gtk_tree_model_iter_nth_child(model
, &iter
, NULL
, row
)) {
1686 gtk_tree_model_get(model
, &iter
,
1687 PREFS_FILTERING_ENABLED
, enabled
,
1688 PREFS_FILTERING_NAME
, name
,
1689 PREFS_FILTERING_ACCOUNT_ID
, account_id
,
1694 static GtkActionGroup
*prefs_filtering_popup_action
= NULL
;
1695 static GtkWidget
*prefs_filtering_popup_menu
= NULL
;
1697 static GtkActionEntry prefs_filtering_popup_entries
[] =
1699 {"PrefsFilteringPopup", NULL
, "PrefsFilteringPopup", NULL
, NULL
, NULL
},
1700 {"PrefsFilteringPopup/Delete", NULL
, N_("_Delete"), NULL
, NULL
, G_CALLBACK(prefs_filtering_delete_cb
) },
1701 {"PrefsFilteringPopup/DeleteAll", NULL
, N_("Delete _all"), NULL
, NULL
, G_CALLBACK(prefs_filtering_delete_all_cb
) },
1702 {"PrefsFilteringPopup/Duplicate", NULL
, N_("D_uplicate"), NULL
, NULL
, G_CALLBACK(prefs_filtering_duplicate_cb
) },
1704 {"PrefsFilteringPopup/---", NULL
, "---", NULL
, NULL
, NULL
},
1705 {"PrefsFilteringPopup/PageUp", NULL
, N_("Move one page up"), NULL
, NULL
, G_CALLBACK(prefs_filtering_page_up
) },
1706 {"PrefsFilteringPopup/PageDown", NULL
, N_("Move one page down"), NULL
, NULL
, G_CALLBACK(prefs_filtering_page_down
) },
1710 static void prefs_filtering_row_selected(GtkTreeSelection
*selection
,
1711 GtkTreeView
*list_view
)
1715 GtkTreeModel
*model
;
1717 if (!gtk_tree_selection_get_selected(selection
, &model
, &iter
))
1720 path
= gtk_tree_model_get_path(model
, &iter
);
1721 prefs_filtering_select_row(list_view
, path
);
1722 gtk_tree_path_free(path
);
1725 static gint
prefs_filtering_list_btn_pressed(GtkWidget
*widget
, GdkEventButton
*event
,
1726 GtkTreeView
*list_view
)
1729 /* left- or right-button click */
1730 if (event
->button
== 1 || event
->button
== 3) {
1731 GtkTreePath
*path
= NULL
;
1733 if (gtk_tree_view_get_path_at_pos( list_view
, event
->x
, event
->y
,
1734 &path
, NULL
, NULL
, NULL
)) {
1735 prefs_filtering_select_row(list_view
, path
);
1738 gtk_tree_path_free(path
);
1741 /* right-button click */
1742 if (event
->button
== 3) {
1743 GtkTreeModel
*model
= gtk_tree_view_get_model(list_view
);
1748 if (!prefs_filtering_popup_menu
) {
1749 prefs_filtering_popup_action
= cm_menu_create_action_group("PrefsFilteringPopup", prefs_filtering_popup_entries
,
1750 G_N_ELEMENTS(prefs_filtering_popup_entries
), (gpointer
)list_view
);
1751 MENUITEM_ADDUI("/Menus", "PrefsFilteringPopup", "PrefsFilteringPopup", GTK_UI_MANAGER_MENU
)
1752 MENUITEM_ADDUI("/Menus/PrefsFilteringPopup", "Delete", "PrefsFilteringPopup/Delete", GTK_UI_MANAGER_MENUITEM
)
1753 MENUITEM_ADDUI("/Menus/PrefsFilteringPopup", "DeleteAll", "PrefsFilteringPopup/DeleteAll", GTK_UI_MANAGER_MENUITEM
)
1754 MENUITEM_ADDUI("/Menus/PrefsFilteringPopup", "Duplicate", "PrefsFilteringPopup/Duplicate", GTK_UI_MANAGER_MENUITEM
)
1756 MENUITEM_ADDUI("/Menus/PrefsFilteringPopup", "Separator1", "PrefsFilteringPopup/---", GTK_UI_MANAGER_SEPARATOR
)
1757 MENUITEM_ADDUI("/Menus/PrefsFilteringPopup", "PageUp", "PrefsFilteringPopup/PageUp", GTK_UI_MANAGER_MENUITEM
)
1758 MENUITEM_ADDUI("/Menus/PrefsFilteringPopup", "PageDown", "PrefsFilteringPopup/PageDown", GTK_UI_MANAGER_MENUITEM
)
1760 prefs_filtering_popup_menu
= gtk_menu_item_get_submenu(GTK_MENU_ITEM(
1761 gtk_ui_manager_get_widget(gtkut_ui_manager(), "/Menus/PrefsFilteringPopup")) );
1764 /* grey out some popup menu items if there is no selected row */
1765 row
= gtkut_list_view_get_selected_row(GTK_WIDGET(list_view
));
1766 cm_menu_set_sensitive("PrefsFilteringPopup/Delete", (row
> 0));
1767 cm_menu_set_sensitive("PrefsFilteringPopup/Duplicate", (row
> 0));
1769 /* grey out seom popup menu items if there is no row
1770 (not counting the (New) one at row 0) */
1771 non_empty
= gtk_tree_model_get_iter_first(model
, &iter
);
1773 non_empty
= gtk_tree_model_iter_next(model
, &iter
);
1774 cm_menu_set_sensitive("PrefsFilteringPopup/DeleteAll", non_empty
);
1776 gtk_menu_popup(GTK_MENU(prefs_filtering_popup_menu
),
1777 NULL
, NULL
, NULL
, NULL
,
1778 event
->button
, event
->time
);
1784 static gboolean
prefs_filtering_list_popup_menu(GtkWidget
*widget
, gpointer data
)
1786 GtkTreeView
*list_view
= (GtkTreeView
*)data
;
1787 GdkEventButton event
;
1790 event
.time
= gtk_get_current_event_time();
1792 prefs_filtering_list_btn_pressed(NULL
, &event
, list_view
);
1798 *\brief Create list view for filtering
1800 static GtkWidget
*prefs_filtering_list_view_create(void)
1802 GtkTreeView
*list_view
;
1803 GtkTreeSelection
*selector
;
1804 GtkListStore
*store
= prefs_filtering_create_data_store();
1806 list_view
= GTK_TREE_VIEW(gtk_tree_view_new_with_model(
1807 GTK_TREE_MODEL(store
)));
1808 g_object_unref(store
);
1810 g_object_set(list_view
, "allow-checkbox-mode", FALSE
, NULL
);
1813 g_signal_connect(G_OBJECT(list_view
), "popup-menu",
1814 G_CALLBACK(prefs_filtering_list_popup_menu
), list_view
);
1815 g_signal_connect(G_OBJECT(list_view
), "button-press-event",
1816 G_CALLBACK(prefs_filtering_list_btn_pressed
), list_view
);
1818 gtk_tree_view_set_rules_hint(list_view
, prefs_common
.use_stripes_everywhere
);
1819 gtk_tree_view_set_reorderable(list_view
, TRUE
);
1821 selector
= gtk_tree_view_get_selection(list_view
);
1822 gtk_tree_selection_set_mode(selector
, GTK_SELECTION_BROWSE
);
1823 g_signal_connect(G_OBJECT(selector
), "changed",
1824 G_CALLBACK(prefs_filtering_row_selected
), list_view
);
1826 /* create the columns */
1827 prefs_filtering_create_list_view_columns(GTK_WIDGET(list_view
));
1829 return GTK_WIDGET(list_view
);
1832 static void prefs_filtering_enable_toggled(GtkCellRendererToggle
*widget
,
1834 GtkWidget
*list_view
)
1837 GtkTreeModel
*model
= gtk_tree_view_get_model(GTK_TREE_VIEW(list_view
));
1838 gboolean enabled
= TRUE
;
1840 if (!gtk_tree_model_get_iter_from_string(model
, &iter
, path
))
1843 gtk_tree_model_get(model
, &iter
,
1844 PREFS_FILTERING_ENABLED
, &enabled
,
1847 gtk_list_store_set(GTK_LIST_STORE(model
), &iter
,
1848 PREFS_FILTERING_ENABLED
, !enabled
,
1852 static void prefs_filtering_create_list_view_columns(GtkWidget
*list_view
)
1854 GtkTreeViewColumn
*column
;
1855 GtkCellRenderer
*renderer
;
1857 renderer
= gtk_cell_renderer_toggle_new();
1858 g_object_set(renderer
,
1860 "activatable", TRUE
,
1862 column
= gtk_tree_view_column_new_with_attributes
1863 (_("Enable"), /* FIXME : Enable, Enabled, or 'E' ? */
1865 "active", PREFS_FILTERING_ENABLED
,
1867 gtk_tree_view_column_set_alignment (column
, 0.5);
1868 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view
), column
);
1869 g_signal_connect(G_OBJECT(renderer
), "toggled",
1870 G_CALLBACK(prefs_filtering_enable_toggled
),
1873 renderer
= gtk_cell_renderer_text_new();
1874 column
= gtk_tree_view_column_new_with_attributes
1877 "text", PREFS_FILTERING_NAME
,
1879 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view
), column
);
1880 gtk_tree_view_column_set_resizable(column
, TRUE
);
1882 renderer
= gtk_cell_renderer_text_new();
1883 column
= gtk_tree_view_column_new_with_attributes
1886 "text", PREFS_FILTERING_ACCOUNT_NAME
,
1888 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view
), column
);
1889 gtk_tree_view_column_set_resizable(column
, TRUE
);
1891 filtering
.account_name_column
= column
;
1893 renderer
= gtk_cell_renderer_text_new();
1894 column
= gtk_tree_view_column_new_with_attributes
1897 "text", PREFS_FILTERING_RULE
,
1900 gtk_tree_view_set_search_column(GTK_TREE_VIEW(list_view
), PREFS_FILTERING_NAME
);
1901 gtk_tree_view_set_search_equal_func(GTK_TREE_VIEW(list_view
), prefs_filtering_search_func_cb
, NULL
, NULL
);
1903 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view
), column
);
1907 *\brief Triggered when a row has to be selected
1909 static void prefs_filtering_select_row(GtkTreeView
*list_view
, GtkTreePath
*path
)
1911 GtkTreeModel
*model
= gtk_tree_view_get_model(list_view
);
1913 if (path
&& model
) {
1914 GtkTreeSelection
*selection
;
1915 gboolean has_prop
= FALSE
;
1919 selection
= gtk_tree_view_get_selection(list_view
);
1920 gtk_tree_selection_select_path(selection
, path
);
1922 /* update dialog from selection */
1923 gtk_tree_model_get_iter(model
, &iter
, path
);
1924 gtk_tree_model_get(model
, &iter
,
1925 PREFS_FILTERING_PROP
, &has_prop
,
1929 FilteringProp
*prop
;
1930 gchar
*filtering_str
= NULL
;
1932 gint account_id
= 0;
1934 gtk_tree_model_get(model
, &iter
,
1935 PREFS_FILTERING_RULE
, &filtering_str
,
1936 PREFS_FILTERING_NAME
, &name
,
1937 PREFS_FILTERING_ACCOUNT_ID
, &account_id
,
1940 prop
= matcher_parser_get_filtering(filtering_str
);
1942 if (prop
->name
!= NULL
)
1944 prop
->name
= g_strdup(name
);
1945 prop
->account_id
= account_id
;
1946 prefs_filtering_select_set(prop
);
1947 filteringprop_free(prop
);
1950 g_free(filtering_str
);
1952 prefs_filtering_reset_dialog();