2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 Hiroyuki Yamamoto & 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/>.
27 #include <glib/gi18n.h>
29 #include <gdk/gdkkeysyms.h>
35 #include "prefs_gtk.h"
39 #include "manage_window.h"
40 #include "mainwindow.h"
41 #include "prefs_common.h"
42 #include "alertpanel.h"
43 #include "prefs_actions.h"
45 #include "description_window.h"
49 #include "filtering.h"
50 #include "prefs_filtering_action.h"
51 #include "matcher_parser.h"
54 PREFS_ACTIONS_STRING, /*!< string pointer managed by list store,
55 * and never touched or retrieved by
57 PREFS_ACTIONS_DATA, /*!< pointer to string that is not managed by
58 * the list store, and which is retrieved
59 * and touched by us */
60 PREFS_ACTIONS_VALID, /*!< contains a valid action, otherwise "(New)" */
61 N_PREFS_ACTIONS_COLUMNS
69 GtkWidget *filter_btn;
70 GtkWidget *name_entry;
73 GtkWidget *shell_radiobtn;
74 GtkWidget *filter_radiobtn;
76 GtkWidget *actions_list_view;
79 static int modified = FALSE;
80 static int modified_list = FALSE;
82 /* widget creating functions */
83 static void prefs_actions_create (MainWindow *mainwin);
84 static void prefs_actions_set_dialog (void);
85 static gint prefs_actions_clist_set_row (gint row);
87 /* callback functions */
88 static void prefs_actions_info_cb (GtkWidget *w,
90 static void prefs_actions_register_cb (GtkWidget *w,
92 static void prefs_actions_substitute_cb (GtkWidget *w,
94 static void prefs_actions_delete_cb (GtkWidget *w,
96 static void prefs_actions_delete_all_cb (GtkWidget *w,
98 static void prefs_actions_clear_cb (void);
99 static void prefs_actions_duplicate_cb (GtkWidget *w,
101 static void prefs_actions_up (GtkWidget *w,
103 static void prefs_actions_down (GtkWidget *w,
105 static gint prefs_actions_deleted (GtkWidget *widget,
108 static gboolean prefs_actions_key_pressed(GtkWidget *widget,
111 static void prefs_actions_cancel (GtkWidget *w,
113 static void prefs_actions_ok (GtkWidget *w,
116 static GtkListStore* prefs_actions_create_data_store (void);
118 static void prefs_actions_list_view_insert_action (GtkWidget *list_view,
122 static GtkWidget *prefs_actions_list_view_create (void);
123 static void prefs_actions_create_list_view_columns (GtkWidget *list_view);
124 static void prefs_actions_select_row(GtkTreeView *list_view, GtkTreePath *path);
126 static void prefs_action_filter_radiobtn_cb(GtkWidget *widget, gpointer data);
127 static void prefs_action_shell_radiobtn_cb(GtkWidget *widget, gpointer data);
128 static void prefs_action_filterbtn_cb(GtkWidget *widget, gpointer data);
129 static void prefs_action_define_filter_done(GSList * action_list);
132 void prefs_actions_open(MainWindow *mainwin)
137 prefs_actions_create(mainwin);
139 manage_window_set_transient(GTK_WINDOW(actions.window));
140 gtk_widget_grab_focus(actions.ok_btn);
142 prefs_actions_set_dialog();
144 gtk_widget_show(actions.window);
148 *\brief Save Gtk object size to prefs dataset
150 static void prefs_actions_size_allocate_cb(GtkWidget *widget,
151 GtkAllocation *allocation)
153 g_return_if_fail(allocation != NULL);
155 prefs_common.actionswin_width = allocation->width;
156 prefs_common.actionswin_height = allocation->height;
159 static void prefs_actions_create(MainWindow *mainwin)
163 GtkWidget *filter_hbox;
166 GtkWidget *cancel_btn;
167 GtkWidget *confirm_area;
172 GtkWidget *shell_radiobtn;
173 GtkWidget *filter_radiobtn;
175 GtkWidget *name_label;
176 GtkWidget *name_entry;
177 GtkWidget *cmd_label;
178 GtkWidget *cmd_entry;
179 GtkWidget *filter_btn;
185 GtkWidget *subst_btn;
187 GtkWidget *clear_btn;
189 GtkWidget *cond_hbox;
190 GtkWidget *cond_scrolledwin;
191 GtkWidget *cond_list_view;
198 static GdkGeometry geometry;
199 GtkTooltips *tooltips;
201 debug_print("Creating actions configuration window...\n");
203 tooltips = gtk_tooltips_new();
205 window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "prefs_actions");
207 gtk_container_set_border_width(GTK_CONTAINER (window), 8);
208 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
209 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
210 gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
212 vbox = gtk_vbox_new(FALSE, 6);
213 gtk_widget_show(vbox);
214 gtk_container_add(GTK_CONTAINER(window), vbox);
216 gtkut_stock_button_set_create_with_help(&confirm_area, &help_btn,
217 &cancel_btn, GTK_STOCK_CANCEL,
218 &ok_btn, GTK_STOCK_OK,
220 gtk_widget_show(confirm_area);
221 gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
222 gtk_widget_grab_default(ok_btn);
224 gtk_window_set_title(GTK_WINDOW(window), _("Actions configuration"));
225 g_signal_connect(G_OBJECT(window), "delete_event",
226 G_CALLBACK(prefs_actions_deleted), NULL);
227 g_signal_connect(G_OBJECT(window), "size_allocate",
228 G_CALLBACK(prefs_actions_size_allocate_cb), NULL);
229 g_signal_connect(G_OBJECT(window), "key_press_event",
230 G_CALLBACK(prefs_actions_key_pressed), NULL);
231 MANAGE_WINDOW_SIGNALS_CONNECT(window);
232 g_signal_connect(G_OBJECT(ok_btn), "clicked",
233 G_CALLBACK(prefs_actions_ok), mainwin);
234 g_signal_connect(G_OBJECT(cancel_btn), "clicked",
235 G_CALLBACK(prefs_actions_cancel), NULL);
236 g_signal_connect(G_OBJECT(help_btn), "clicked",
237 G_CALLBACK(manual_open_with_anchor_cb),
238 MANUAL_ANCHOR_ACTIONS);
240 vbox1 = gtk_vbox_new(FALSE, VSPACING);
241 gtk_widget_show(vbox1);
242 gtk_box_pack_start(GTK_BOX(vbox), vbox1, TRUE, TRUE, 0);
243 gtk_container_set_border_width(GTK_CONTAINER(vbox1), 2);
245 table = gtk_table_new(3, 2, FALSE);
246 gtk_table_set_row_spacings (GTK_TABLE (table), VSPACING_NARROW_2);
247 gtk_table_set_col_spacings (GTK_TABLE (table), 4);
248 gtk_widget_show(table);
249 gtk_box_pack_start (GTK_BOX (vbox1), table, FALSE, FALSE, 0);
251 name_label = gtk_label_new (_("Menu name"));
252 gtk_widget_show (name_label);
253 gtk_misc_set_alignment (GTK_MISC (name_label), 1, 0.5);
254 gtk_table_attach (GTK_TABLE (table), name_label, 0, 1, 0, 1,
255 (GtkAttachOptions) (GTK_FILL),
256 (GtkAttachOptions) (0), 0, 0);
258 name_entry = gtk_entry_new ();
259 gtk_widget_show (name_entry);
260 gtk_table_attach (GTK_TABLE (table), name_entry, 1, 2, 0, 1,
261 (GtkAttachOptions) (GTK_FILL|GTK_EXPAND),
262 (GtkAttachOptions) (0), 0, 0);
264 cmd_label = gtk_label_new (_("Command"));
265 gtk_widget_show (cmd_label);
266 gtk_misc_set_alignment (GTK_MISC (cmd_label), 1, 0.5);
267 gtk_table_attach (GTK_TABLE (table), cmd_label, 0, 1, 2, 3,
268 (GtkAttachOptions) (GTK_FILL),
269 (GtkAttachOptions) (0), 0, 0);
271 cmd_entry = gtk_entry_new ();
272 gtk_widget_show (cmd_entry);
273 gtk_table_attach (GTK_TABLE (table), cmd_entry, 1, 2, 2, 3,
274 (GtkAttachOptions) (GTK_FILL|GTK_EXPAND),
275 (GtkAttachOptions) (0), 0, 0);
277 /* radio buttons for filter actions or shell */
278 filter_hbox = gtk_hbox_new(FALSE,4);
279 gtk_table_attach(GTK_TABLE(table), filter_hbox, 1, 2, 3, 4,
280 (GtkAttachOptions) (GTK_FILL|GTK_EXPAND),
281 (GtkAttachOptions) (0), 0, 0);
282 gtk_widget_show(filter_hbox);
284 shell_radiobtn = gtk_radio_button_new_with_label(NULL, _("Shell command"));
285 gtk_box_pack_start(GTK_BOX(filter_hbox), shell_radiobtn, FALSE, FALSE, 0);
286 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(shell_radiobtn), TRUE);
287 gtk_widget_show(shell_radiobtn);
289 g_signal_connect(G_OBJECT(shell_radiobtn), "clicked",
290 G_CALLBACK(prefs_action_shell_radiobtn_cb), NULL);
293 gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(shell_radiobtn),
295 gtk_box_pack_start(GTK_BOX(filter_hbox), filter_radiobtn, FALSE, FALSE, 0);
296 gtk_widget_show(filter_radiobtn);
297 g_signal_connect(G_OBJECT(filter_radiobtn), "clicked",
298 G_CALLBACK(prefs_action_filter_radiobtn_cb), NULL);
300 filter_btn = gtk_button_new_with_label(_("Edit filter action"));
301 gtk_box_pack_start(GTK_BOX(filter_hbox), filter_btn, FALSE, FALSE, 0);
302 gtk_widget_set_sensitive(filter_btn, FALSE);
303 g_signal_connect(G_OBJECT(filter_btn), "clicked",
304 G_CALLBACK(prefs_action_filterbtn_cb), NULL);
305 gtk_widget_show(filter_btn);
307 /* register / substitute / delete */
309 reg_hbox = gtk_hbox_new(FALSE, 4);
310 gtk_widget_show(reg_hbox);
311 gtk_box_pack_start(GTK_BOX(vbox1), reg_hbox, FALSE, FALSE, 0);
313 arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
314 gtk_widget_show(arrow);
315 gtk_box_pack_start(GTK_BOX(reg_hbox), arrow, FALSE, FALSE, 0);
316 gtk_widget_set_size_request(arrow, -1, 16);
318 btn_hbox = gtk_hbox_new(TRUE, 4);
319 gtk_widget_show(btn_hbox);
320 gtk_box_pack_start(GTK_BOX(reg_hbox), btn_hbox, FALSE, FALSE, 0);
322 reg_btn = gtk_button_new_from_stock(GTK_STOCK_ADD);
323 gtk_widget_show(reg_btn);
324 gtk_box_pack_start(GTK_BOX(btn_hbox), reg_btn, FALSE, TRUE, 0);
325 g_signal_connect(G_OBJECT(reg_btn), "clicked",
326 G_CALLBACK(prefs_actions_register_cb), NULL);
327 gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), reg_btn,
328 _("Append the new action above to the list"), NULL);
330 subst_btn = gtkut_get_replace_btn(_("Replace"));
331 gtk_widget_show(subst_btn);
332 gtk_box_pack_start(GTK_BOX(btn_hbox), subst_btn, FALSE, TRUE, 0);
333 g_signal_connect(G_OBJECT(subst_btn), "clicked",
334 G_CALLBACK(prefs_actions_substitute_cb), NULL);
335 gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), subst_btn,
336 _("Replace the selected action in list with the action above"), NULL);
338 del_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
339 gtk_widget_show(del_btn);
340 gtk_box_pack_start(GTK_BOX(btn_hbox), del_btn, FALSE, TRUE, 0);
341 g_signal_connect(G_OBJECT(del_btn), "clicked",
342 G_CALLBACK(prefs_actions_delete_cb), NULL);
343 gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), del_btn,
344 _("Delete the selected action from the list"), NULL);
346 clear_btn = gtk_button_new_from_stock (GTK_STOCK_CLEAR);
347 gtk_widget_show (clear_btn);
348 gtk_box_pack_start (GTK_BOX (btn_hbox), clear_btn, FALSE, TRUE, 0);
349 g_signal_connect(G_OBJECT (clear_btn), "clicked",
350 G_CALLBACK(prefs_actions_clear_cb), NULL);
351 gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), clear_btn,
352 _("Clear all the input fields in the dialog"), NULL);
354 #if GTK_CHECK_VERSION(2, 8, 0)
355 info_btn = gtk_button_new_from_stock(GTK_STOCK_INFO);
357 info_btn = gtk_button_new_with_label(_("Info..."));
359 gtk_widget_show(info_btn);
360 gtk_box_pack_end(GTK_BOX(reg_hbox), info_btn, FALSE, FALSE, 0);
361 g_signal_connect(G_OBJECT(info_btn), "clicked",
362 G_CALLBACK(prefs_actions_info_cb), GTK_WINDOW(window));
363 gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), info_btn,
364 _("Show information on configuring actions"), NULL);
366 cond_hbox = gtk_hbox_new(FALSE, 8);
367 gtk_widget_show(cond_hbox);
368 gtk_box_pack_start(GTK_BOX(vbox1), cond_hbox, TRUE, TRUE, 0);
370 cond_scrolledwin = gtk_scrolled_window_new(NULL, NULL);
371 gtk_widget_show(cond_scrolledwin);
372 gtk_widget_set_size_request(cond_scrolledwin, -1, 150);
373 gtk_box_pack_start(GTK_BOX(cond_hbox), cond_scrolledwin,
375 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (cond_scrolledwin),
376 GTK_POLICY_AUTOMATIC,
377 GTK_POLICY_AUTOMATIC);
378 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(cond_scrolledwin),
379 GTK_SHADOW_ETCHED_IN);
381 cond_list_view = prefs_actions_list_view_create();
382 gtk_widget_show(cond_list_view);
383 gtk_container_add(GTK_CONTAINER (cond_scrolledwin), cond_list_view);
385 btn_vbox = gtk_vbox_new(FALSE, 8);
386 gtk_widget_show(btn_vbox);
387 gtk_box_pack_start(GTK_BOX(cond_hbox), btn_vbox, FALSE, FALSE, 0);
389 up_btn = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
390 gtk_widget_show(up_btn);
391 gtk_box_pack_start(GTK_BOX(btn_vbox), up_btn, FALSE, FALSE, 0);
392 g_signal_connect(G_OBJECT(up_btn), "clicked",
393 G_CALLBACK(prefs_actions_up), NULL);
394 gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), up_btn,
395 _("Move the selected action up"), NULL);
397 down_btn = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
398 gtk_widget_show(down_btn);
399 gtk_box_pack_start(GTK_BOX(btn_vbox), down_btn, FALSE, FALSE, 0);
400 g_signal_connect(G_OBJECT(down_btn), "clicked",
401 G_CALLBACK(prefs_actions_down), NULL);
402 gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), down_btn,
403 _("Move selected action down"), NULL);
405 if (!geometry.min_height) {
406 geometry.min_width = 486;
407 geometry.min_height = 322;
410 gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
412 gtk_widget_set_size_request(window, prefs_common.actionswin_width,
413 prefs_common.actionswin_height);
415 gtk_widget_show(window);
417 actions.window = window;
418 actions.ok_btn = ok_btn;
419 actions.info_btn = info_btn;
421 actions.name_entry = name_entry;
422 actions.cmd_entry = cmd_entry;
423 actions.filter_btn = filter_btn;
424 actions.shell_radiobtn = shell_radiobtn;
425 actions.filter_radiobtn = filter_radiobtn;
427 actions.actions_list_view = cond_list_view;
430 static void prefs_actions_reset_dialog(void)
432 gtk_entry_set_text(GTK_ENTRY(actions.name_entry), "");
433 gtk_entry_set_text(GTK_ENTRY(actions.cmd_entry), "");
436 void prefs_actions_read_config(void)
440 gchar buf[PREFSBUFSIZE];
443 debug_print("Reading actions configurations...\n");
445 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACTIONS_RC, NULL);
446 if ((fp = g_fopen(rcpath, "rb")) == NULL) {
447 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
453 while (prefs_common.actions_list != NULL) {
454 act = (gchar *)prefs_common.actions_list->data;
455 prefs_common.actions_list =
456 g_slist_remove(prefs_common.actions_list, act);
460 while (fgets(buf, sizeof(buf), fp) != NULL) {
461 const gchar *src_codeset = conv_get_locale_charset_str();
462 const gchar *dest_codeset = CS_UTF_8;
465 tmp = conv_codeset_strdup(buf, src_codeset, dest_codeset);
467 g_warning("Failed to convert character set of action configuration\n");
472 act = strstr(tmp, ": ");
474 action_get_type(&act[2]) != ACTION_ERROR)
475 prefs_common.actions_list =
476 g_slist_append(prefs_common.actions_list,
484 void prefs_actions_write_config(void)
490 debug_print("Writing actions configuration...\n");
492 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACTIONS_RC, NULL);
493 if ((pfile= prefs_write_open(rcpath)) == NULL) {
494 g_warning("Failed to write configuration to file\n");
499 for (cur = prefs_common.actions_list; cur != NULL; cur = cur->next) {
500 gchar *tmp = (gchar *)cur->data;
501 const gchar *src_codeset = CS_UTF_8;
502 const gchar *dest_codeset = conv_get_locale_charset_str();
505 act = conv_codeset_strdup(tmp, src_codeset, dest_codeset);
507 g_warning("Failed to convert character set of action configuration\n");
511 if (fputs(act, pfile->fp) == EOF ||
512 fputc('\n', pfile->fp) == EOF) {
513 FILE_OP_ERROR(rcpath, "fputs || fputc");
514 prefs_file_close_revert(pfile);
523 if (prefs_file_close(pfile) < 0) {
524 g_warning("failed to write configuration to file\n");
529 static void prefs_actions_clear_list(GtkListStore *list_store)
531 gtk_list_store_clear(list_store);
533 prefs_actions_list_view_insert_action(actions.actions_list_view,
534 -1, _("(New)"), FALSE);
537 static void prefs_actions_set_dialog(void)
541 GtkTreeSelection *selection;
544 store = GTK_LIST_STORE(gtk_tree_view_get_model
545 (GTK_TREE_VIEW(actions.actions_list_view)));
547 prefs_actions_clear_list(store);
549 for (cur = prefs_common.actions_list; cur != NULL; cur = cur->next) {
550 gchar *action = (gchar *) cur->data;
552 prefs_actions_list_view_insert_action(actions.actions_list_view,
556 /* select first entry */
557 selection = gtk_tree_view_get_selection
558 (GTK_TREE_VIEW(actions.actions_list_view));
559 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store),
561 gtk_tree_selection_select_iter(selection, &iter);
564 static void prefs_actions_set_list(void)
569 g_slist_free(prefs_common.actions_list);
570 prefs_common.actions_list = NULL;
572 store = GTK_LIST_STORE(gtk_tree_view_get_model
573 (GTK_TREE_VIEW(actions.actions_list_view)));
575 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter)) {
580 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter,
581 PREFS_ACTIONS_DATA, &action,
582 PREFS_ACTIONS_VALID, &is_valid,
586 prefs_common.actions_list =
587 g_slist_append(prefs_common.actions_list,
590 } while (gtk_tree_model_iter_next(GTK_TREE_MODEL(store),
595 #define GET_ENTRY(entry) \
596 entry_text = gtk_entry_get_text(GTK_ENTRY(entry))
598 static gint prefs_actions_clist_set_row(gint row)
600 const gchar *entry_text;
602 gchar action[PREFSBUFSIZE];
606 store = GTK_LIST_STORE(gtk_tree_view_get_model
607 (GTK_TREE_VIEW(actions.actions_list_view)));
609 GET_ENTRY(actions.name_entry);
610 if (entry_text[0] == '\0') {
611 alertpanel_error(_("Menu name is not set."));
615 if (entry_text[0] == '/') {
616 alertpanel_error(_("A leading '/' is not allowed in the menu name."));
620 if (strchr(entry_text, ':')) {
621 alertpanel_error(_("Colon ':' is not allowed in the menu name."));
625 strncpy(action, entry_text, PREFSBUFSIZE - 1);
627 while (strstr(action, "//")) {
628 char *to_move = strstr(action, "//")+1;
629 char *where = strstr(action, "//");
630 int old_len = strlen(action);
631 memmove(where, to_move, strlen(to_move));
632 action[old_len-1] = '\0';
637 /* Keep space for the ': ' delimiter */
638 len = strlen(action) + 2;
639 if (len >= PREFSBUFSIZE - 1) {
640 alertpanel_error(_("Menu name is too long."));
644 strcat(action, ": ");
646 GET_ENTRY(actions.cmd_entry);
648 if (entry_text[0] == '\0') {
649 alertpanel_error(_("Command line not set."));
653 if (len + strlen(entry_text) >= PREFSBUFSIZE - 1) {
654 alertpanel_error(_("Menu name and command are too long."));
658 if (action_get_type(entry_text) == ACTION_ERROR) {
660 message = g_markup_printf_escaped(_("The command\n%s\nhas a syntax error."),
662 alertpanel_error(message);
667 strcat(action, entry_text);
669 new_action = g_strdup(action);
670 prefs_actions_list_view_insert_action(actions.actions_list_view,
671 row, new_action, TRUE);
673 prefs_actions_set_list();
678 /* callback functions */
680 static void prefs_actions_register_cb(GtkWidget *w, gpointer data)
682 prefs_actions_clist_set_row(-1);
685 modified_list = TRUE;
688 static void prefs_actions_substitute_cb(GtkWidget *w, gpointer data)
692 row = gtkut_list_view_get_selected_row(actions.actions_list_view);
696 prefs_actions_clist_set_row(row);
699 modified_list = TRUE;
702 static void prefs_actions_delete_cb(GtkWidget *w, gpointer data)
709 row = gtkut_list_view_get_selected_row(actions.actions_list_view);
713 if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection
714 (GTK_TREE_VIEW(actions.actions_list_view)),
718 if (alertpanel(_("Delete action"),
719 _("Do you really want to delete this action?"),
720 GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL) != G_ALERTALTERNATE)
723 /* XXX: Here's the reason why we need to store the original
724 * pointer: we search the slist for it. */
725 gtk_tree_model_get(model, &sel,
726 PREFS_ACTIONS_DATA, &action,
728 gtk_list_store_remove(GTK_LIST_STORE(model), &sel);
730 prefs_common.actions_list = g_slist_remove(prefs_common.actions_list,
732 modified_list = TRUE;
735 static void prefs_actions_delete_all_cb(GtkWidget *w, gpointer data)
737 GtkListStore *list_store;
739 if (alertpanel(_("Delete all actions"),
740 _("Do you really want to delete all the actions?"),
741 GTK_STOCK_CANCEL, "+"GTK_STOCK_DELETE, NULL) == G_ALERTDEFAULT)
744 list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(actions.actions_list_view)));
745 prefs_actions_clear_list(list_store);
748 prefs_actions_reset_dialog();
749 modified_list = TRUE;
752 static void prefs_actions_clear_cb(void)
756 prefs_actions_reset_dialog();
757 row = gtkut_list_view_get_selected_row(actions.actions_list_view);
764 static void prefs_actions_duplicate_cb(GtkWidget *w, gpointer data)
768 row = gtkut_list_view_get_selected_row(actions.actions_list_view);
772 modified_list = !prefs_actions_clist_set_row(-row-2);
775 static void prefs_actions_up(GtkWidget *w, gpointer data)
777 GtkTreePath *prev, *sel, *try;
779 GtkListStore *store = NULL;
780 GtkTreeModel *model = NULL;
783 if (!gtk_tree_selection_get_selected
784 (gtk_tree_view_get_selection
785 (GTK_TREE_VIEW(actions.actions_list_view)),
789 store = (GtkListStore *)model;
790 sel = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &isel);
794 /* no move if we're at row 0 or 1, looks phony, but other
795 * solutions are more convoluted... */
796 try = gtk_tree_path_copy(sel);
797 if (!gtk_tree_path_prev(try) || !gtk_tree_path_prev(try)) {
798 gtk_tree_path_free(try);
799 gtk_tree_path_free(sel);
802 gtk_tree_path_free(try);
804 prev = gtk_tree_path_copy(sel);
805 if (!gtk_tree_path_prev(prev)) {
806 gtk_tree_path_free(prev);
807 gtk_tree_path_free(sel);
811 gtk_tree_model_get_iter(GTK_TREE_MODEL(store),
813 gtk_tree_path_free(sel);
814 gtk_tree_path_free(prev);
816 gtk_list_store_swap(store, &iprev, &isel);
817 prefs_actions_set_list();
818 modified_list = TRUE;
821 static void prefs_actions_down(GtkWidget *w, gpointer data)
823 GtkListStore *store = NULL;
824 GtkTreeModel *model = NULL;
825 GtkTreeIter next, sel;
828 if (!gtk_tree_selection_get_selected
829 (gtk_tree_view_get_selection
830 (GTK_TREE_VIEW(actions.actions_list_view)),
834 store = (GtkListStore *)model;
835 try = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &sel);
839 /* no move when we're at row 0 */
840 if (!gtk_tree_path_prev(try)) {
841 gtk_tree_path_free(try);
844 gtk_tree_path_free(try);
847 if (!gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &next))
850 gtk_list_store_swap(store, &next, &sel);
851 prefs_actions_set_list();
852 modified_list = TRUE;
855 static gint prefs_actions_deleted(GtkWidget *widget, GdkEventAny *event,
858 prefs_actions_cancel(widget, data);
862 static gboolean prefs_actions_key_pressed(GtkWidget *widget, GdkEventKey *event,
865 if (event && event->keyval == GDK_Escape)
866 prefs_actions_cancel(widget, data);
868 GtkWidget *focused = gtkut_get_focused_child(
869 GTK_CONTAINER(widget));
870 if (focused && GTK_IS_EDITABLE(focused)) {
877 static void prefs_actions_cancel(GtkWidget *w, gpointer data)
881 if (modified && alertpanel(_("Entry not saved"),
882 _("The entry was not saved. Close anyway?"),
883 GTK_STOCK_CLOSE, _("+_Continue editing"),
884 NULL) != G_ALERTDEFAULT) {
886 } else if (modified_list && alertpanel(_("Actions list not saved"),
887 _("The actions list has been modified. Close anyway?"),
888 GTK_STOCK_CLOSE, _("+_Continue editing"),
889 NULL) != G_ALERTDEFAULT) {
893 modified_list = FALSE;
894 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
895 (actions.actions_list_view)));
896 gtk_list_store_clear(store);
897 prefs_actions_read_config();
898 gtk_widget_hide(actions.window);
902 static void prefs_actions_ok(GtkWidget *widget, gpointer data)
904 MainWindow *mainwin = (MainWindow *) data;
907 MessageView *msgview;
911 if (modified && alertpanel(_("Entry not saved"),
912 _("The entry was not saved. Close anyway?"),
913 GTK_STOCK_CLOSE, _("+_Continue editing"),
914 NULL) != G_ALERTDEFAULT) {
918 modified_list = FALSE;
919 prefs_actions_set_list();
920 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
921 (actions.actions_list_view)));
922 gtk_list_store_clear(store);
923 prefs_actions_write_config();
925 /* Update mainwindow actions menu */
926 main_window_update_actions_menu(mainwin);
928 /* Update separated message view actions menu */
929 list = messageview_get_msgview_list();
930 for (iter = list; iter; iter = iter->next) {
931 msgview = (MessageView *) iter->data;
932 messageview_update_actions_menu(msgview);
935 /* Update compose windows actions menu */
936 list = compose_get_compose_list();
937 for (iter = list; iter; iter = iter->next) {
938 compose = (Compose *) iter->data;
939 compose_update_actions_menu(compose);
942 gtk_widget_hide(actions.window);
947 * Strings describing action format strings
949 * When adding new lines, remember to put one string for each line
951 static gchar *actions_desc_strings[] = {
952 N_("<span weight=\"bold\" underline=\"single\">Menu name:</span>"), NULL,
953 N_("Use / in menu name to make submenus."), NULL,
955 N_("<span weight=\"bold\" underline=\"single\">Command line:</span>"), NULL,
956 N_("<span weight=\"bold\">Begin with:</span>"), NULL,
957 " |", N_("to send message body or selection to command's standard input"),
958 " >", N_("to send user provided text to command's standard input"),
959 " *", N_("to send user provided hidden text to command's standard input"),
960 N_("<span weight=\"bold\">End with:</span>"), NULL,
961 " |", N_("to replace message body or selection with command's standard output"),
962 " >", N_("to insert command's standard output without replacing old text"),
963 " &", N_("to run command asynchronously"),
964 N_("<span weight=\"bold\">Use:</span>"), NULL,
965 " %f", N_("for the file of the selected message in RFC822/2822 format "),
966 " %F", N_("for the list of the files of the selected messages in RFC822/2822 format"),
967 " %p", N_("for the file of the selected decoded message MIME part"),
968 " %u", N_("for a user provided argument"),
969 " %h", N_("for a user provided hidden argument (e.g. password)"),
970 " %s", N_("for the text selection"),
971 " %as{}", N_("apply filtering actions between {} to selected messages"),
972 " %%", N_("for a literal %"),
977 static DescriptionWindow actions_desc_win = {
982 N_("The Actions feature is a way for the user to launch "
983 "external commands to process a complete message file or just "
984 "one of its parts."),
989 static void prefs_actions_info_cb(GtkWidget *w, GtkWidget *window)
991 actions_desc_win.parent = window;
992 description_window_create(&actions_desc_win);
995 static GtkListStore* prefs_actions_create_data_store(void)
997 return gtk_list_store_new(N_PREFS_ACTIONS_COLUMNS,
1004 static void prefs_actions_list_view_insert_action(GtkWidget *list_view,
1010 GtkTreeIter sibling;
1011 GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
1012 (GTK_TREE_VIEW(list_view)));
1014 /* row -1 to add a new rule to store,
1015 row >=0 to change an existing row
1016 row <-1 insert a new row after (-row-2)
1019 /* modify the existing */
1020 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
1023 } else if (row < -1 ) {
1024 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
1025 &sibling, NULL, -row-2))
1031 gtk_list_store_append(list_store, &iter);
1032 gtk_list_store_set(list_store, &iter,
1033 PREFS_ACTIONS_STRING, action,
1034 PREFS_ACTIONS_DATA, action,
1035 PREFS_ACTIONS_VALID, is_valid,
1037 } else if (row < -1) {
1039 gtk_list_store_insert_after(list_store, &iter, &sibling);
1040 gtk_list_store_set(list_store, &iter,
1041 PREFS_ACTIONS_STRING, action,
1042 PREFS_ACTIONS_DATA, action,
1043 PREFS_ACTIONS_VALID, is_valid,
1046 /* change existing */
1049 gtk_tree_model_get(GTK_TREE_MODEL(list_store), &iter,
1050 PREFS_ACTIONS_DATA, &old_action,
1054 gtk_list_store_set(list_store, &iter,
1055 PREFS_ACTIONS_STRING, action,
1056 PREFS_ACTIONS_DATA, action,
1061 static GtkItemFactory *prefs_actions_popup_factory = NULL;
1062 static GtkWidget *prefs_actions_popup_menu = NULL;
1064 static GtkItemFactoryEntry prefs_actions_popup_entries[] =
1066 {N_("/_Delete"), NULL, prefs_actions_delete_cb, 0, NULL, NULL},
1067 {N_("/Delete _all"), NULL, prefs_actions_delete_all_cb, 0, NULL, NULL},
1068 {N_("/D_uplicate"), NULL, prefs_actions_duplicate_cb, 0, NULL, NULL},
1071 static gint prefs_actions_list_btn_pressed(GtkWidget *widget, GdkEventButton *event,
1072 GtkTreeView *list_view)
1075 /* left- or right-button click */
1076 if (event->button == 1 || event->button == 3) {
1077 GtkTreePath *path = NULL;
1078 if (gtk_tree_view_get_path_at_pos( list_view, event->x, event->y,
1079 &path, NULL, NULL, NULL)) {
1080 prefs_actions_select_row(list_view, path);
1083 gtk_tree_path_free(path);
1086 /* right-button click */
1087 if (event->button == 3) {
1088 GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1093 if (!prefs_actions_popup_menu) {
1094 gint n_entries = sizeof(prefs_actions_popup_entries) /
1095 sizeof(prefs_actions_popup_entries[0]);
1096 prefs_actions_popup_menu = menu_create_items(prefs_actions_popup_entries,
1097 n_entries, "<PrefsActionsPopupMenu>",
1098 &prefs_actions_popup_factory, list_view);
1101 /* grey out some popup menu items if there is no selected row */
1102 row = gtkut_list_view_get_selected_row(GTK_WIDGET(list_view));
1103 menu_set_sensitive(prefs_actions_popup_factory, "/Delete", (row > 0));
1104 menu_set_sensitive(prefs_actions_popup_factory, "/Duplicate", (row > 0));
1106 /* grey out seom popup menu items if there is no row
1107 (not counting the (New) one at row 0) */
1108 non_empty = gtk_tree_model_get_iter_first(model, &iter);
1110 non_empty = gtk_tree_model_iter_next(model, &iter);
1111 menu_set_sensitive(prefs_actions_popup_factory, "/Delete all", non_empty);
1113 gtk_menu_popup(GTK_MENU(prefs_actions_popup_menu),
1114 NULL, NULL, NULL, NULL,
1115 event->button, event->time);
1121 static gboolean prefs_actions_list_popup_menu(GtkWidget *widget, gpointer data)
1123 GtkTreeView *list_view = (GtkTreeView *)data;
1124 GdkEventButton event;
1127 event.time = gtk_get_current_event_time();
1129 prefs_actions_list_btn_pressed(NULL, &event, list_view);
1134 static GtkWidget *prefs_actions_list_view_create(void)
1136 GtkTreeView *list_view;
1137 GtkTreeSelection *selector;
1138 GtkTreeModel *model;
1140 model = GTK_TREE_MODEL(prefs_actions_create_data_store());
1141 list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
1142 g_object_unref(model);
1145 g_signal_connect(G_OBJECT(list_view), "popup-menu",
1146 G_CALLBACK(prefs_actions_list_popup_menu), list_view);
1148 gtk_widget_tap_and_hold_setup(GTK_WIDGET(list_view), NULL, NULL,
1149 GTK_TAP_AND_HOLD_NONE | GTK_TAP_AND_HOLD_NO_INTERNALS);
1150 g_signal_connect(G_OBJECT(list_view), "tap-and-hold",
1151 G_CALLBACK(prefs_actions_list_popup_menu), list_view);
1153 g_signal_connect(G_OBJECT(list_view), "button-press-event",
1154 G_CALLBACK(prefs_actions_list_btn_pressed), list_view);
1156 gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
1157 gtk_tree_view_set_reorderable(list_view, TRUE);
1159 selector = gtk_tree_view_get_selection(list_view);
1160 gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1162 /* create the columns */
1163 prefs_actions_create_list_view_columns(GTK_WIDGET(list_view));
1165 return GTK_WIDGET(list_view);
1168 static void prefs_actions_create_list_view_columns(GtkWidget *list_view)
1170 GtkTreeViewColumn *column;
1171 GtkCellRenderer *renderer;
1173 renderer = gtk_cell_renderer_text_new();
1174 column = gtk_tree_view_column_new_with_attributes
1175 (_("Current actions"),
1177 "text", PREFS_ACTIONS_STRING,
1179 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1182 #define ENTRY_SET_TEXT(entry, str) \
1183 gtk_entry_set_text(GTK_ENTRY(entry), str ? str : "")
1185 static void prefs_actions_select_row(GtkTreeView *list_view, GtkTreePath *path)
1187 GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1188 GtkTreeSelection *selection;
1191 gchar buf[PREFSBUFSIZE];
1195 if (!model || !path || !gtk_tree_model_get_iter(model, &iter, path))
1199 selection = gtk_tree_view_get_selection(list_view);
1200 gtk_tree_selection_select_path(selection, path);
1202 gtk_tree_model_get(model, &iter,
1203 PREFS_ACTIONS_VALID, &is_valid,
1204 PREFS_ACTIONS_DATA, &action,
1207 prefs_actions_reset_dialog();
1211 strncpy(buf, action, PREFSBUFSIZE - 1);
1212 buf[PREFSBUFSIZE - 1] = '\0';
1213 cmd = strstr(buf, ": ");
1216 ENTRY_SET_TEXT(actions.cmd_entry, &cmd[2]);
1221 ENTRY_SET_TEXT(actions.name_entry, buf);
1223 if(g_str_has_prefix(&cmd[2], "%as{") == TRUE)
1224 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(
1225 actions.filter_radiobtn), TRUE);
1227 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(
1228 actions.shell_radiobtn), TRUE);
1233 static void prefs_action_filter_radiobtn_cb(GtkWidget *widget, gpointer data)
1235 if(actions.filter_btn)
1236 gtk_widget_set_sensitive(actions.filter_btn, TRUE);
1237 if(actions.cmd_entry)
1238 gtk_widget_set_sensitive(actions.cmd_entry,FALSE);
1239 if(actions.info_btn)
1240 gtk_widget_set_sensitive(actions.info_btn,FALSE);
1243 static void prefs_action_shell_radiobtn_cb(GtkWidget *widget, gpointer data)
1245 if(actions.filter_btn)
1246 gtk_widget_set_sensitive(actions.filter_btn, FALSE);
1247 if(actions.cmd_entry)
1248 gtk_widget_set_sensitive(actions.cmd_entry,TRUE);
1249 if(actions.info_btn)
1250 gtk_widget_set_sensitive(actions.info_btn,TRUE);
1253 static void prefs_action_filterbtn_cb(GtkWidget *widget, gpointer data)
1255 gchar *action_str, **tokens;
1256 GSList *action_list = NULL, *cur;
1258 if(modified && alertpanel(_("Entry was modified"),
1259 _("Opening the filter action dialog will clear current modifications "
1260 "of the command line."),
1261 GTK_STOCK_CANCEL, _("+_Continue editing"), NULL) == G_ALERTDEFAULT)
1264 action_str = gtk_editable_get_chars(GTK_EDITABLE(actions.cmd_entry), 0, -1);
1265 tokens = g_strsplit_set(action_str, "{}", 5);
1267 if (tokens[1] && *tokens[1] != '\0') {
1268 action_list = matcher_parser_get_action_list(tokens[1]);
1269 if (action_list == NULL)
1270 alertpanel_error(_("Action string is not valid."));
1273 prefs_filtering_action_open(action_list, prefs_action_define_filter_done);
1275 if (action_list != NULL) {
1276 for(cur = action_list ; cur != NULL ; cur = cur->next)
1277 filteringaction_free(cur->data);
1284 static void prefs_action_define_filter_done(GSList * action_list)
1288 if(action_list == NULL)
1291 str = filteringaction_list_to_string(action_list);
1295 cmd = g_strdup_printf("%%as{%s}",str);
1297 gtk_entry_set_text(GTK_ENTRY(actions.cmd_entry), cmd);