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"
51 PREFS_ACTIONS_STRING, /*!< string pointer managed by list store,
52 * and never touched or retrieved by
54 PREFS_ACTIONS_DATA, /*!< pointer to string that is not managed by
55 * the list store, and which is retrieved
56 * and touched by us */
57 PREFS_ACTIONS_VALID, /*!< contains a valid action, otherwise "(New)" */
58 N_PREFS_ACTIONS_COLUMNS
67 GtkWidget *name_entry;
70 GtkWidget *actions_list_view;
73 static int modified = FALSE;
74 static int modified_list = FALSE;
76 /* widget creating functions */
77 static void prefs_actions_create (MainWindow *mainwin);
78 static void prefs_actions_set_dialog (void);
79 static gint prefs_actions_clist_set_row (gint row);
81 /* callback functions */
82 static void prefs_actions_info_cb (GtkWidget *w,
84 static void prefs_actions_register_cb (GtkWidget *w,
86 static void prefs_actions_substitute_cb (GtkWidget *w,
88 static void prefs_actions_delete_cb (GtkWidget *w,
90 static void prefs_actions_delete_all_cb (GtkWidget *w,
92 static void prefs_actions_clear_cb (void);
93 static void prefs_actions_duplicate_cb (GtkWidget *w,
95 static void prefs_actions_up (GtkWidget *w,
97 static void prefs_actions_down (GtkWidget *w,
99 static gint prefs_actions_deleted (GtkWidget *widget,
102 static gboolean prefs_actions_key_pressed(GtkWidget *widget,
105 static void prefs_actions_cancel (GtkWidget *w,
107 static void prefs_actions_ok (GtkWidget *w,
110 static GtkListStore* prefs_actions_create_data_store (void);
112 static void prefs_actions_list_view_insert_action (GtkWidget *list_view,
116 static GtkWidget *prefs_actions_list_view_create (void);
117 static void prefs_actions_create_list_view_columns (GtkWidget *list_view);
118 static void prefs_actions_select_row(GtkTreeView *list_view, GtkTreePath *path);
120 void prefs_actions_open(MainWindow *mainwin)
125 prefs_actions_create(mainwin);
127 manage_window_set_transient(GTK_WINDOW(actions.window));
128 gtk_widget_grab_focus(actions.ok_btn);
130 prefs_actions_set_dialog();
132 gtk_widget_show(actions.window);
136 *\brief Save Gtk object size to prefs dataset
138 static void prefs_actions_size_allocate_cb(GtkWidget *widget,
139 GtkAllocation *allocation)
141 g_return_if_fail(allocation != NULL);
143 prefs_common.actionswin_width = allocation->width;
144 prefs_common.actionswin_height = allocation->height;
147 static void prefs_actions_create(MainWindow *mainwin)
153 GtkWidget *cancel_btn;
154 GtkWidget *confirm_area;
159 GtkWidget *name_label;
160 GtkWidget *name_entry;
161 GtkWidget *cmd_label;
162 GtkWidget *cmd_entry;
168 GtkWidget *subst_btn;
170 GtkWidget *clear_btn;
172 GtkWidget *cond_hbox;
173 GtkWidget *cond_scrolledwin;
174 GtkWidget *cond_list_view;
181 static GdkGeometry geometry;
182 GtkTooltips *tooltips;
184 debug_print("Creating actions configuration window...\n");
186 tooltips = gtk_tooltips_new();
188 window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "prefs_actions");
190 gtk_container_set_border_width(GTK_CONTAINER (window), 8);
191 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
192 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
193 gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
195 vbox = gtk_vbox_new(FALSE, 6);
196 gtk_widget_show(vbox);
197 gtk_container_add(GTK_CONTAINER(window), vbox);
199 gtkut_stock_button_set_create_with_help(&confirm_area, &help_btn,
200 &cancel_btn, GTK_STOCK_CANCEL,
201 &ok_btn, GTK_STOCK_OK,
203 gtk_widget_show(confirm_area);
204 gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
205 gtk_widget_grab_default(ok_btn);
207 gtk_window_set_title(GTK_WINDOW(window), _("Actions configuration"));
208 g_signal_connect(G_OBJECT(window), "delete_event",
209 G_CALLBACK(prefs_actions_deleted), NULL);
210 g_signal_connect(G_OBJECT(window), "size_allocate",
211 G_CALLBACK(prefs_actions_size_allocate_cb), NULL);
212 g_signal_connect(G_OBJECT(window), "key_press_event",
213 G_CALLBACK(prefs_actions_key_pressed), NULL);
214 MANAGE_WINDOW_SIGNALS_CONNECT(window);
215 g_signal_connect(G_OBJECT(ok_btn), "clicked",
216 G_CALLBACK(prefs_actions_ok), mainwin);
217 g_signal_connect(G_OBJECT(cancel_btn), "clicked",
218 G_CALLBACK(prefs_actions_cancel), NULL);
219 g_signal_connect(G_OBJECT(help_btn), "clicked",
220 G_CALLBACK(manual_open_with_anchor_cb),
221 MANUAL_ANCHOR_ACTIONS);
223 vbox1 = gtk_vbox_new(FALSE, VSPACING);
224 gtk_widget_show(vbox1);
225 gtk_box_pack_start(GTK_BOX(vbox), vbox1, TRUE, TRUE, 0);
226 gtk_container_set_border_width(GTK_CONTAINER(vbox1), 2);
228 table = gtk_table_new(2, 2, FALSE);
229 gtk_table_set_row_spacings (GTK_TABLE (table), VSPACING_NARROW_2);
230 gtk_table_set_col_spacings (GTK_TABLE (table), 4);
231 gtk_widget_show(table);
232 gtk_box_pack_start (GTK_BOX (vbox1), table, FALSE, FALSE, 0);
234 name_label = gtk_label_new (_("Menu name"));
235 gtk_widget_show (name_label);
236 gtk_misc_set_alignment (GTK_MISC (name_label), 1, 0.5);
237 gtk_table_attach (GTK_TABLE (table), name_label, 0, 1, 0, 1,
238 (GtkAttachOptions) (GTK_FILL),
239 (GtkAttachOptions) (0), 0, 0);
241 name_entry = gtk_entry_new ();
242 gtk_widget_show (name_entry);
243 gtk_table_attach (GTK_TABLE (table), name_entry, 1, 2, 0, 1,
244 (GtkAttachOptions) (GTK_FILL|GTK_EXPAND),
245 (GtkAttachOptions) (0), 0, 0);
247 cmd_label = gtk_label_new (_("Command line"));
248 gtk_widget_show (cmd_label);
249 gtk_misc_set_alignment (GTK_MISC (cmd_label), 1, 0.5);
250 gtk_table_attach (GTK_TABLE (table), cmd_label, 0, 1, 1, 2,
251 (GtkAttachOptions) (GTK_FILL),
252 (GtkAttachOptions) (0), 0, 0);
254 cmd_entry = gtk_entry_new ();
255 gtk_widget_show (cmd_entry);
256 gtk_table_attach (GTK_TABLE (table), cmd_entry, 1, 2, 1, 2,
257 (GtkAttachOptions) (GTK_FILL|GTK_EXPAND),
258 (GtkAttachOptions) (0), 0, 0);
260 /* register / substitute / delete */
262 reg_hbox = gtk_hbox_new(FALSE, 4);
263 gtk_widget_show(reg_hbox);
264 gtk_box_pack_start(GTK_BOX(vbox1), reg_hbox, FALSE, FALSE, 0);
266 arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
267 gtk_widget_show(arrow);
268 gtk_box_pack_start(GTK_BOX(reg_hbox), arrow, FALSE, FALSE, 0);
269 gtk_widget_set_size_request(arrow, -1, 16);
271 btn_hbox = gtk_hbox_new(TRUE, 4);
272 gtk_widget_show(btn_hbox);
273 gtk_box_pack_start(GTK_BOX(reg_hbox), btn_hbox, FALSE, FALSE, 0);
275 reg_btn = gtk_button_new_from_stock(GTK_STOCK_ADD);
276 gtk_widget_show(reg_btn);
277 gtk_box_pack_start(GTK_BOX(btn_hbox), reg_btn, FALSE, TRUE, 0);
278 g_signal_connect(G_OBJECT(reg_btn), "clicked",
279 G_CALLBACK(prefs_actions_register_cb), NULL);
280 gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), reg_btn,
281 _("Append new action to list, as defined above"), NULL);
283 subst_btn = gtkut_get_replace_btn(_("Replace"));
284 gtk_widget_show(subst_btn);
285 gtk_box_pack_start(GTK_BOX(btn_hbox), subst_btn, FALSE, TRUE, 0);
286 g_signal_connect(G_OBJECT(subst_btn), "clicked",
287 G_CALLBACK(prefs_actions_substitute_cb), NULL);
288 gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), subst_btn,
289 _("Replace selected action in list with the above data"), NULL);
291 del_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
292 gtk_widget_show(del_btn);
293 gtk_box_pack_start(GTK_BOX(btn_hbox), del_btn, FALSE, TRUE, 0);
294 g_signal_connect(G_OBJECT(del_btn), "clicked",
295 G_CALLBACK(prefs_actions_delete_cb), NULL);
296 gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), del_btn,
297 _("Delete selected action from list"), NULL);
299 clear_btn = gtk_button_new_from_stock (GTK_STOCK_CLEAR);
300 gtk_widget_show (clear_btn);
301 gtk_box_pack_start (GTK_BOX (btn_hbox), clear_btn, FALSE, TRUE, 0);
302 g_signal_connect(G_OBJECT (clear_btn), "clicked",
303 G_CALLBACK(prefs_actions_clear_cb), NULL);
304 gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), clear_btn,
305 _("Clear all input fields in dialog"), NULL);
307 #if GTK_CHECK_VERSION(2, 8, 0)
308 info_btn = gtk_button_new_from_stock(GTK_STOCK_INFO);
310 info_btn = gtk_button_new_with_label(_("Info..."));
312 gtk_widget_show(info_btn);
313 gtk_box_pack_end(GTK_BOX(reg_hbox), info_btn, FALSE, FALSE, 0);
314 g_signal_connect(G_OBJECT(info_btn), "clicked",
315 G_CALLBACK(prefs_actions_info_cb), GTK_WINDOW(window));
316 gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), info_btn,
317 _("Click here to get further information "
318 "about actions menu name restrictions and "
319 "command line syntax"), NULL);
321 cond_hbox = gtk_hbox_new(FALSE, 8);
322 gtk_widget_show(cond_hbox);
323 gtk_box_pack_start(GTK_BOX(vbox1), cond_hbox, TRUE, TRUE, 0);
325 cond_scrolledwin = gtk_scrolled_window_new(NULL, NULL);
326 gtk_widget_show(cond_scrolledwin);
327 gtk_widget_set_size_request(cond_scrolledwin, -1, 150);
328 gtk_box_pack_start(GTK_BOX(cond_hbox), cond_scrolledwin,
330 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (cond_scrolledwin),
331 GTK_POLICY_AUTOMATIC,
332 GTK_POLICY_AUTOMATIC);
334 cond_list_view = prefs_actions_list_view_create();
335 gtk_widget_show(cond_list_view);
336 gtk_container_add(GTK_CONTAINER (cond_scrolledwin), cond_list_view);
338 btn_vbox = gtk_vbox_new(FALSE, 8);
339 gtk_widget_show(btn_vbox);
340 gtk_box_pack_start(GTK_BOX(cond_hbox), btn_vbox, FALSE, FALSE, 0);
342 up_btn = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
343 gtk_widget_show(up_btn);
344 gtk_box_pack_start(GTK_BOX(btn_vbox), up_btn, FALSE, FALSE, 0);
345 g_signal_connect(G_OBJECT(up_btn), "clicked",
346 G_CALLBACK(prefs_actions_up), NULL);
347 gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), up_btn,
348 _("Move up selected action in list"), NULL);
350 down_btn = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
351 gtk_widget_show(down_btn);
352 gtk_box_pack_start(GTK_BOX(btn_vbox), down_btn, FALSE, FALSE, 0);
353 g_signal_connect(G_OBJECT(down_btn), "clicked",
354 G_CALLBACK(prefs_actions_down), NULL);
355 gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), down_btn,
356 _("Move down selected action in list"), NULL);
358 if (!geometry.min_height) {
359 geometry.min_width = 486;
360 geometry.min_height = 322;
363 gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
365 gtk_widget_set_size_request(window, prefs_common.actionswin_width,
366 prefs_common.actionswin_height);
368 gtk_widget_show(window);
370 actions.window = window;
371 actions.ok_btn = ok_btn;
373 actions.name_entry = name_entry;
374 actions.cmd_entry = cmd_entry;
376 actions.actions_list_view = cond_list_view;
379 static void prefs_actions_reset_dialog(void)
381 gtk_entry_set_text(GTK_ENTRY(actions.name_entry), "");
382 gtk_entry_set_text(GTK_ENTRY(actions.cmd_entry), "");
385 void prefs_actions_read_config(void)
389 gchar buf[PREFSBUFSIZE];
392 debug_print("Reading actions configurations...\n");
394 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACTIONS_RC, NULL);
395 if ((fp = g_fopen(rcpath, "rb")) == NULL) {
396 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
402 while (prefs_common.actions_list != NULL) {
403 act = (gchar *)prefs_common.actions_list->data;
404 prefs_common.actions_list =
405 g_slist_remove(prefs_common.actions_list, act);
409 while (fgets(buf, sizeof(buf), fp) != NULL) {
410 const gchar *src_codeset = conv_get_locale_charset_str();
411 const gchar *dest_codeset = CS_UTF_8;
414 tmp = conv_codeset_strdup(buf, src_codeset, dest_codeset);
416 g_warning("Failed to convert character set of action configuration\n");
421 act = strstr(tmp, ": ");
423 action_get_type(&act[2]) != ACTION_ERROR)
424 prefs_common.actions_list =
425 g_slist_append(prefs_common.actions_list,
433 void prefs_actions_write_config(void)
439 debug_print("Writing actions configuration...\n");
441 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACTIONS_RC, NULL);
442 if ((pfile= prefs_write_open(rcpath)) == NULL) {
443 g_warning("Failed to write configuration to file\n");
448 for (cur = prefs_common.actions_list; cur != NULL; cur = cur->next) {
449 gchar *tmp = (gchar *)cur->data;
450 const gchar *src_codeset = CS_UTF_8;
451 const gchar *dest_codeset = conv_get_locale_charset_str();
454 act = conv_codeset_strdup(tmp, src_codeset, dest_codeset);
456 g_warning("Failed to convert character set of action configuration\n");
460 if (fputs(act, pfile->fp) == EOF ||
461 fputc('\n', pfile->fp) == EOF) {
462 FILE_OP_ERROR(rcpath, "fputs || fputc");
463 prefs_file_close_revert(pfile);
472 if (prefs_file_close(pfile) < 0) {
473 g_warning("failed to write configuration to file\n");
478 static void prefs_actions_clear_list(GtkListStore *list_store)
480 gtk_list_store_clear(list_store);
482 prefs_actions_list_view_insert_action(actions.actions_list_view,
483 -1, _("(New)"), FALSE);
486 static void prefs_actions_set_dialog(void)
490 GtkTreeSelection *selection;
493 store = GTK_LIST_STORE(gtk_tree_view_get_model
494 (GTK_TREE_VIEW(actions.actions_list_view)));
496 prefs_actions_clear_list(store);
498 for (cur = prefs_common.actions_list; cur != NULL; cur = cur->next) {
499 gchar *action = (gchar *) cur->data;
501 prefs_actions_list_view_insert_action(actions.actions_list_view,
505 /* select first entry */
506 selection = gtk_tree_view_get_selection
507 (GTK_TREE_VIEW(actions.actions_list_view));
508 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store),
510 gtk_tree_selection_select_iter(selection, &iter);
513 static void prefs_actions_set_list(void)
518 g_slist_free(prefs_common.actions_list);
519 prefs_common.actions_list = NULL;
521 store = GTK_LIST_STORE(gtk_tree_view_get_model
522 (GTK_TREE_VIEW(actions.actions_list_view)));
524 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter)) {
529 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter,
530 PREFS_ACTIONS_DATA, &action,
531 PREFS_ACTIONS_VALID, &is_valid,
535 prefs_common.actions_list =
536 g_slist_append(prefs_common.actions_list,
539 } while (gtk_tree_model_iter_next(GTK_TREE_MODEL(store),
544 #define GET_ENTRY(entry) \
545 entry_text = gtk_entry_get_text(GTK_ENTRY(entry))
547 static gint prefs_actions_clist_set_row(gint row)
549 const gchar *entry_text;
551 gchar action[PREFSBUFSIZE];
555 store = GTK_LIST_STORE(gtk_tree_view_get_model
556 (GTK_TREE_VIEW(actions.actions_list_view)));
558 GET_ENTRY(actions.name_entry);
559 if (entry_text[0] == '\0') {
560 alertpanel_error(_("Menu name is not set."));
564 if (entry_text[0] == '/') {
565 alertpanel_error(_("A leading '/' is not allowed in the menu name."));
569 if (strchr(entry_text, ':')) {
570 alertpanel_error(_("Colon ':' is not allowed in the menu name."));
574 strncpy(action, entry_text, PREFSBUFSIZE - 1);
576 while (strstr(action, "//")) {
577 char *to_move = strstr(action, "//")+1;
578 char *where = strstr(action, "//");
579 int old_len = strlen(action);
580 memmove(where, to_move, strlen(to_move));
581 action[old_len-1] = '\0';
586 /* Keep space for the ': ' delimiter */
587 len = strlen(action) + 2;
588 if (len >= PREFSBUFSIZE - 1) {
589 alertpanel_error(_("Menu name is too long."));
593 strcat(action, ": ");
595 GET_ENTRY(actions.cmd_entry);
597 if (entry_text[0] == '\0') {
598 alertpanel_error(_("Command line not set."));
602 if (len + strlen(entry_text) >= PREFSBUFSIZE - 1) {
603 alertpanel_error(_("Menu name and command are too long."));
607 if (action_get_type(entry_text) == ACTION_ERROR) {
609 message = g_markup_printf_escaped(_("The command\n%s\nhas a syntax error."),
611 alertpanel_error(message);
616 strcat(action, entry_text);
618 new_action = g_strdup(action);
619 prefs_actions_list_view_insert_action(actions.actions_list_view,
620 row, new_action, TRUE);
622 prefs_actions_set_list();
627 /* callback functions */
629 static void prefs_actions_register_cb(GtkWidget *w, gpointer data)
631 prefs_actions_clist_set_row(-1);
634 modified_list = TRUE;
637 static void prefs_actions_substitute_cb(GtkWidget *w, gpointer data)
641 row = gtkut_list_view_get_selected_row(actions.actions_list_view);
645 prefs_actions_clist_set_row(row);
648 modified_list = TRUE;
651 static void prefs_actions_delete_cb(GtkWidget *w, gpointer data)
658 row = gtkut_list_view_get_selected_row(actions.actions_list_view);
662 if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection
663 (GTK_TREE_VIEW(actions.actions_list_view)),
667 if (alertpanel(_("Delete action"),
668 _("Do you really want to delete this action?"),
669 GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL) != G_ALERTALTERNATE)
672 /* XXX: Here's the reason why we need to store the original
673 * pointer: we search the slist for it. */
674 gtk_tree_model_get(model, &sel,
675 PREFS_ACTIONS_DATA, &action,
677 gtk_list_store_remove(GTK_LIST_STORE(model), &sel);
679 prefs_common.actions_list = g_slist_remove(prefs_common.actions_list,
681 modified_list = TRUE;
684 static void prefs_actions_delete_all_cb(GtkWidget *w, gpointer data)
686 GtkListStore *list_store;
688 if (alertpanel(_("Delete all actions"),
689 _("Do you really want to delete all actions?"),
690 GTK_STOCK_CANCEL, "+"GTK_STOCK_DELETE, NULL) == G_ALERTDEFAULT)
693 list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(actions.actions_list_view)));
694 prefs_actions_clear_list(list_store);
697 prefs_actions_reset_dialog();
698 modified_list = TRUE;
701 static void prefs_actions_clear_cb(void)
705 prefs_actions_reset_dialog();
706 row = gtkut_list_view_get_selected_row(actions.actions_list_view);
713 static void prefs_actions_duplicate_cb(GtkWidget *w, gpointer data)
717 row = gtkut_list_view_get_selected_row(actions.actions_list_view);
721 modified_list = !prefs_actions_clist_set_row(-row-2);
724 static void prefs_actions_up(GtkWidget *w, gpointer data)
726 GtkTreePath *prev, *sel, *try;
728 GtkListStore *store = NULL;
729 GtkTreeModel *model = NULL;
732 if (!gtk_tree_selection_get_selected
733 (gtk_tree_view_get_selection
734 (GTK_TREE_VIEW(actions.actions_list_view)),
738 store = (GtkListStore *)model;
739 sel = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &isel);
743 /* no move if we're at row 0 or 1, looks phony, but other
744 * solutions are more convoluted... */
745 try = gtk_tree_path_copy(sel);
746 if (!gtk_tree_path_prev(try) || !gtk_tree_path_prev(try)) {
747 gtk_tree_path_free(try);
748 gtk_tree_path_free(sel);
751 gtk_tree_path_free(try);
753 prev = gtk_tree_path_copy(sel);
754 if (!gtk_tree_path_prev(prev)) {
755 gtk_tree_path_free(prev);
756 gtk_tree_path_free(sel);
760 gtk_tree_model_get_iter(GTK_TREE_MODEL(store),
762 gtk_tree_path_free(sel);
763 gtk_tree_path_free(prev);
765 gtk_list_store_swap(store, &iprev, &isel);
766 prefs_actions_set_list();
767 modified_list = TRUE;
770 static void prefs_actions_down(GtkWidget *w, gpointer data)
772 GtkListStore *store = NULL;
773 GtkTreeModel *model = NULL;
774 GtkTreeIter next, sel;
777 if (!gtk_tree_selection_get_selected
778 (gtk_tree_view_get_selection
779 (GTK_TREE_VIEW(actions.actions_list_view)),
783 store = (GtkListStore *)model;
784 try = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &sel);
788 /* no move when we're at row 0 */
789 if (!gtk_tree_path_prev(try)) {
790 gtk_tree_path_free(try);
793 gtk_tree_path_free(try);
796 if (!gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &next))
799 gtk_list_store_swap(store, &next, &sel);
800 prefs_actions_set_list();
801 modified_list = TRUE;
804 static gint prefs_actions_deleted(GtkWidget *widget, GdkEventAny *event,
807 prefs_actions_cancel(widget, data);
811 static gboolean prefs_actions_key_pressed(GtkWidget *widget, GdkEventKey *event,
814 if (event && event->keyval == GDK_Escape)
815 prefs_actions_cancel(widget, data);
817 GtkWidget *focused = gtkut_get_focused_child(
818 GTK_CONTAINER(widget));
819 if (focused && GTK_IS_EDITABLE(focused)) {
826 static void prefs_actions_cancel(GtkWidget *w, gpointer data)
830 if (modified && alertpanel(_("Entry not saved"),
831 _("The entry was not saved. Close anyway?"),
832 GTK_STOCK_CLOSE, _("+_Continue editing"),
833 NULL) != G_ALERTDEFAULT) {
835 } else if (modified_list && alertpanel(_("Actions list not saved"),
836 _("The actions list has been modified. Close anyway?"),
837 GTK_STOCK_CLOSE, _("+_Continue editing"),
838 NULL) != G_ALERTDEFAULT) {
842 modified_list = FALSE;
843 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
844 (actions.actions_list_view)));
845 gtk_list_store_clear(store);
846 prefs_actions_read_config();
847 gtk_widget_hide(actions.window);
851 static void prefs_actions_ok(GtkWidget *widget, gpointer data)
853 MainWindow *mainwin = (MainWindow *) data;
856 MessageView *msgview;
860 if (modified && alertpanel(_("Entry not saved"),
861 _("The entry was not saved. Close anyway?"),
862 GTK_STOCK_CLOSE, _("+_Continue editing"),
863 NULL) != G_ALERTDEFAULT) {
867 modified_list = FALSE;
868 prefs_actions_set_list();
869 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
870 (actions.actions_list_view)));
871 gtk_list_store_clear(store);
872 prefs_actions_write_config();
874 /* Update mainwindow actions menu */
875 main_window_update_actions_menu(mainwin);
877 /* Update separated message view actions menu */
878 list = messageview_get_msgview_list();
879 for (iter = list; iter; iter = iter->next) {
880 msgview = (MessageView *) iter->data;
881 messageview_update_actions_menu(msgview);
884 /* Update compose windows actions menu */
885 list = compose_get_compose_list();
886 for (iter = list; iter; iter = iter->next) {
887 compose = (Compose *) iter->data;
888 compose_update_actions_menu(compose);
891 gtk_widget_hide(actions.window);
896 * Strings describing action format strings
898 * When adding new lines, remember to put one string for each line
900 static gchar *actions_desc_strings[] = {
901 N_("<span weight=\"bold\" underline=\"single\">Menu name:</span>"), NULL,
902 N_("Use / in menu name to make submenus."), NULL,
904 N_("<span weight=\"bold\" underline=\"single\">Command line:</span>"), NULL,
905 N_("<span weight=\"bold\">Begin with:</span>"), NULL,
906 " |", N_("to send message body or selection to command's standard input"),
907 " >", N_("to send user provided text to command's standard input"),
908 " *", N_("to send user provided hidden text to command's standard input"),
909 N_("<span weight=\"bold\">End with:</span>"), NULL,
910 " |", N_("to replace message body or selection with command's standard output"),
911 " >", N_("to insert command's standard output without replacing old text"),
912 " &", N_("to run command asynchronously"),
913 N_("<span weight=\"bold\">Use:</span>"), NULL,
914 " %f", N_("for the file of the selected message in RFC822/2822 format "),
915 " %F", N_("for the list of the files of the selected messages in RFC822/2822 format"),
916 " %p", N_("for the file of the selected decoded message MIME part"),
917 " %u", N_("for a user provided argument"),
918 " %h", N_("for a user provided hidden argument (e.g. password)"),
919 " %s", N_("for the text selection"),
920 " %as{}", N_("apply filtering actions between {} to selected messages"),
921 " %%", N_("for a literal %"),
926 static DescriptionWindow actions_desc_win = {
931 N_("The Actions feature is a way for the user to launch "
932 "external commands to process a complete message file or just "
933 "one of its parts."),
938 static void prefs_actions_info_cb(GtkWidget *w, GtkWidget *window)
940 actions_desc_win.parent = window;
941 description_window_create(&actions_desc_win);
944 static GtkListStore* prefs_actions_create_data_store(void)
946 return gtk_list_store_new(N_PREFS_ACTIONS_COLUMNS,
953 static void prefs_actions_list_view_insert_action(GtkWidget *list_view,
960 GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
961 (GTK_TREE_VIEW(list_view)));
963 /* row -1 to add a new rule to store,
964 row >=0 to change an existing row
965 row <-1 insert a new row after (-row-2)
968 /* modify the existing */
969 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
972 } else if (row < -1 ) {
973 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
974 &sibling, NULL, -row-2))
980 gtk_list_store_append(list_store, &iter);
981 gtk_list_store_set(list_store, &iter,
982 PREFS_ACTIONS_STRING, action,
983 PREFS_ACTIONS_DATA, action,
984 PREFS_ACTIONS_VALID, is_valid,
986 } else if (row < -1) {
988 gtk_list_store_insert_after(list_store, &iter, &sibling);
989 gtk_list_store_set(list_store, &iter,
990 PREFS_ACTIONS_STRING, action,
991 PREFS_ACTIONS_DATA, action,
992 PREFS_ACTIONS_VALID, is_valid,
995 /* change existing */
998 gtk_tree_model_get(GTK_TREE_MODEL(list_store), &iter,
999 PREFS_ACTIONS_DATA, &old_action,
1003 gtk_list_store_set(list_store, &iter,
1004 PREFS_ACTIONS_STRING, action,
1005 PREFS_ACTIONS_DATA, action,
1010 static GtkItemFactory *prefs_actions_popup_factory = NULL;
1011 static GtkWidget *prefs_actions_popup_menu = NULL;
1013 static GtkItemFactoryEntry prefs_actions_popup_entries[] =
1015 {N_("/_Delete"), NULL, prefs_actions_delete_cb, 0, NULL, NULL},
1016 {N_("/Delete _all"), NULL, prefs_actions_delete_all_cb, 0, NULL, NULL},
1017 {N_("/D_uplicate"), NULL, prefs_actions_duplicate_cb, 0, NULL, NULL},
1020 static gint prefs_actions_list_btn_pressed(GtkWidget *widget, GdkEventButton *event,
1021 GtkTreeView *list_view)
1024 /* left- or right-button click */
1025 if (event->button == 1 || event->button == 3) {
1026 GtkTreePath *path = NULL;
1027 if (gtk_tree_view_get_path_at_pos( list_view, event->x, event->y,
1028 &path, NULL, NULL, NULL)) {
1029 prefs_actions_select_row(list_view, path);
1032 gtk_tree_path_free(path);
1035 /* right-button click */
1036 if (event->button == 3) {
1037 GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1042 if (!prefs_actions_popup_menu) {
1043 gint n_entries = sizeof(prefs_actions_popup_entries) /
1044 sizeof(prefs_actions_popup_entries[0]);
1045 prefs_actions_popup_menu = menu_create_items(prefs_actions_popup_entries,
1046 n_entries, "<PrefsActionsPopupMenu>",
1047 &prefs_actions_popup_factory, list_view);
1050 /* grey out some popup menu items if there is no selected row */
1051 row = gtkut_list_view_get_selected_row(GTK_WIDGET(list_view));
1052 menu_set_sensitive(prefs_actions_popup_factory, "/Delete", (row > 0));
1053 menu_set_sensitive(prefs_actions_popup_factory, "/Duplicate", (row > 0));
1055 /* grey out seom popup menu items if there is no row
1056 (not counting the (New) one at row 0) */
1057 non_empty = gtk_tree_model_get_iter_first(model, &iter);
1059 non_empty = gtk_tree_model_iter_next(model, &iter);
1060 menu_set_sensitive(prefs_actions_popup_factory, "/Delete all", non_empty);
1062 gtk_menu_popup(GTK_MENU(prefs_actions_popup_menu),
1063 NULL, NULL, NULL, NULL,
1064 event->button, event->time);
1070 static gboolean prefs_actions_list_popup_menu(GtkWidget *widget, gpointer data)
1072 GtkTreeView *list_view = (GtkTreeView *)data;
1073 GdkEventButton event;
1076 event.time = gtk_get_current_event_time();
1078 prefs_actions_list_btn_pressed(NULL, &event, list_view);
1083 static GtkWidget *prefs_actions_list_view_create(void)
1085 GtkTreeView *list_view;
1086 GtkTreeSelection *selector;
1087 GtkTreeModel *model;
1089 model = GTK_TREE_MODEL(prefs_actions_create_data_store());
1090 list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
1091 g_object_unref(model);
1094 g_signal_connect(G_OBJECT(list_view), "popup-menu",
1095 G_CALLBACK(prefs_actions_list_popup_menu), list_view);
1097 gtk_widget_tap_and_hold_setup(GTK_WIDGET(list_view), NULL, NULL,
1098 GTK_TAP_AND_HOLD_NONE | GTK_TAP_AND_HOLD_NO_INTERNALS);
1099 g_signal_connect(G_OBJECT(list_view), "tap-and-hold",
1100 G_CALLBACK(prefs_actions_list_popup_menu), list_view);
1102 g_signal_connect(G_OBJECT(list_view), "button-press-event",
1103 G_CALLBACK(prefs_actions_list_btn_pressed), list_view);
1105 gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
1106 gtk_tree_view_set_reorderable(list_view, TRUE);
1108 selector = gtk_tree_view_get_selection(list_view);
1109 gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1111 /* create the columns */
1112 prefs_actions_create_list_view_columns(GTK_WIDGET(list_view));
1114 return GTK_WIDGET(list_view);
1117 static void prefs_actions_create_list_view_columns(GtkWidget *list_view)
1119 GtkTreeViewColumn *column;
1120 GtkCellRenderer *renderer;
1122 renderer = gtk_cell_renderer_text_new();
1123 column = gtk_tree_view_column_new_with_attributes
1124 (_("Current actions"),
1126 "text", PREFS_ACTIONS_STRING,
1128 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1131 #define ENTRY_SET_TEXT(entry, str) \
1132 gtk_entry_set_text(GTK_ENTRY(entry), str ? str : "")
1134 static void prefs_actions_select_row(GtkTreeView *list_view, GtkTreePath *path)
1136 GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1137 GtkTreeSelection *selection;
1140 gchar buf[PREFSBUFSIZE];
1144 if (!model || !path || !gtk_tree_model_get_iter(model, &iter, path))
1148 selection = gtk_tree_view_get_selection(list_view);
1149 gtk_tree_selection_select_path(selection, path);
1151 gtk_tree_model_get(model, &iter,
1152 PREFS_ACTIONS_VALID, &is_valid,
1153 PREFS_ACTIONS_DATA, &action,
1156 prefs_actions_reset_dialog();
1160 strncpy(buf, action, PREFSBUFSIZE - 1);
1161 buf[PREFSBUFSIZE - 1] = '\0';
1162 cmd = strstr(buf, ": ");
1165 ENTRY_SET_TEXT(actions.cmd_entry, &cmd[2]);
1170 ENTRY_SET_TEXT(actions.name_entry, buf);