2008-01-23 [paul] 3.2.0cvs64
[claws.git] / src / prefs_actions.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 Hiroyuki Yamamoto & The Claws Mail Team
4  *
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.
9  *
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.
14  *
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/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <gtk/gtk.h>
29 #include <gdk/gdkkeysyms.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <errno.h>
34
35 #include "prefs_gtk.h"
36 #include "inc.h"
37 #include "utils.h"
38 #include "gtkutils.h"
39 #include "manage_window.h"
40 #include "mainwindow.h"
41 #include "prefs_common.h"
42 #include "alertpanel.h"
43 #include "prefs_actions.h"
44 #include "action.h"
45 #include "description_window.h"
46 #include "gtkutils.h"
47 #include "manual.h"
48 #include "menu.h"
49
50 enum {
51         PREFS_ACTIONS_STRING,   /*!< string pointer managed by list store, 
52                                  *   and never touched or retrieved by 
53                                  *   us */ 
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
59 };
60
61 static struct Actions
62 {
63         GtkWidget *window;
64
65         GtkWidget *ok_btn;
66
67         GtkWidget *name_entry;
68         GtkWidget *cmd_entry;
69
70         GtkWidget *actions_list_view;
71 } actions;
72
73 static int modified = FALSE;
74 static int modified_list = FALSE;
75
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);
80
81 /* callback functions */
82 static void prefs_actions_info_cb       (GtkWidget      *w,
83                                          GtkWidget      *window);
84 static void prefs_actions_register_cb   (GtkWidget      *w,
85                                          gpointer        data);
86 static void prefs_actions_substitute_cb (GtkWidget      *w,
87                                          gpointer        data);
88 static void prefs_actions_delete_cb     (GtkWidget      *w,
89                                          gpointer        data);
90 static void prefs_actions_delete_all_cb (GtkWidget      *w,
91                                          gpointer        data);
92 static void prefs_actions_clear_cb      (void);
93 static void prefs_actions_duplicate_cb  (GtkWidget      *w,
94                                          gpointer        data);
95 static void prefs_actions_up            (GtkWidget      *w,
96                                          gpointer        data);
97 static void prefs_actions_down          (GtkWidget      *w,
98                                          gpointer        data);
99 static gint prefs_actions_deleted       (GtkWidget      *widget,
100                                          GdkEventAny    *event,
101                                          gpointer       *data);
102 static gboolean prefs_actions_key_pressed(GtkWidget     *widget,
103                                           GdkEventKey   *event,
104                                           gpointer       data);
105 static void prefs_actions_cancel        (GtkWidget      *w,
106                                          gpointer        data);
107 static void prefs_actions_ok            (GtkWidget      *w,
108                                          gpointer        data);
109
110 static GtkListStore* prefs_actions_create_data_store    (void);
111
112 static void prefs_actions_list_view_insert_action       (GtkWidget *list_view,
113                                                          gint row,
114                                                          gchar *action,
115                                                          gboolean is_valid);
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);
119
120 void prefs_actions_open(MainWindow *mainwin)
121 {
122         inc_lock();
123
124         if (!actions.window)
125                 prefs_actions_create(mainwin);
126
127         manage_window_set_transient(GTK_WINDOW(actions.window));
128         gtk_widget_grab_focus(actions.ok_btn);
129
130         prefs_actions_set_dialog();
131
132         gtk_widget_show(actions.window);
133 }
134
135 /*!
136  *\brief        Save Gtk object size to prefs dataset
137  */
138 static void prefs_actions_size_allocate_cb(GtkWidget *widget,
139                                          GtkAllocation *allocation)
140 {
141         g_return_if_fail(allocation != NULL);
142
143         prefs_common.actionswin_width = allocation->width;
144         prefs_common.actionswin_height = allocation->height;
145 }
146
147 static void prefs_actions_create(MainWindow *mainwin)
148 {
149         GtkWidget *window;
150         GtkWidget *vbox;
151         GtkWidget *help_btn;
152         GtkWidget *ok_btn;
153         GtkWidget *cancel_btn;
154         GtkWidget *confirm_area;
155
156         GtkWidget *vbox1;
157         GtkWidget *table;
158
159         GtkWidget *name_label;
160         GtkWidget *name_entry;
161         GtkWidget *cmd_label;
162         GtkWidget *cmd_entry;
163
164         GtkWidget *reg_hbox;
165         GtkWidget *btn_hbox;
166         GtkWidget *arrow;
167         GtkWidget *reg_btn;
168         GtkWidget *subst_btn;
169         GtkWidget *del_btn;
170         GtkWidget *clear_btn;
171
172         GtkWidget *cond_hbox;
173         GtkWidget *cond_scrolledwin;
174         GtkWidget *cond_list_view;
175
176         GtkWidget *info_btn;
177
178         GtkWidget *btn_vbox;
179         GtkWidget *up_btn;
180         GtkWidget *down_btn;
181         static GdkGeometry geometry;
182         GtkTooltips *tooltips;
183
184         debug_print("Creating actions configuration window...\n");
185
186         tooltips = gtk_tooltips_new();
187
188         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "prefs_actions");
189
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);
194
195         vbox = gtk_vbox_new(FALSE, 6);
196         gtk_widget_show(vbox);
197         gtk_container_add(GTK_CONTAINER(window), vbox);
198
199         gtkut_stock_button_set_create_with_help(&confirm_area, &help_btn,
200                         &cancel_btn, GTK_STOCK_CANCEL,
201                         &ok_btn, GTK_STOCK_OK,
202                         NULL, NULL);
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);
206
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);
222
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);
227
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);
233
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);
240
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);
246
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);
253
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);
259
260         /* register / substitute / delete */
261
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);
265
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);
270
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);
274
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);
282
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);
290
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);
298
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);
306
307 #if GTK_CHECK_VERSION(2, 8, 0)
308         info_btn = gtk_button_new_from_stock(GTK_STOCK_INFO);
309 #else
310         info_btn = gtk_button_new_with_label(_("Info..."));
311 #endif
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);
320
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);
324
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,
329                            TRUE, TRUE, 0);
330         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (cond_scrolledwin),
331                                        GTK_POLICY_AUTOMATIC,
332                                        GTK_POLICY_AUTOMATIC);
333
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);
337
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);
341
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);
349
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);
357
358         if (!geometry.min_height) {
359                 geometry.min_width = 486;
360                 geometry.min_height = 322;
361         }
362
363         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
364                                       GDK_HINT_MIN_SIZE);
365         gtk_widget_set_size_request(window, prefs_common.actionswin_width,
366                                     prefs_common.actionswin_height);
367
368         gtk_widget_show(window);
369
370         actions.window = window;
371         actions.ok_btn = ok_btn;
372
373         actions.name_entry = name_entry;
374         actions.cmd_entry  = cmd_entry;
375
376         actions.actions_list_view = cond_list_view;
377 }
378
379 static void prefs_actions_reset_dialog(void)
380 {
381         gtk_entry_set_text(GTK_ENTRY(actions.name_entry), "");
382         gtk_entry_set_text(GTK_ENTRY(actions.cmd_entry), "");
383 }
384
385 void prefs_actions_read_config(void)
386 {
387         gchar *rcpath;
388         FILE *fp;
389         gchar buf[PREFSBUFSIZE];
390         gchar *act;
391
392         debug_print("Reading actions configurations...\n");
393
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");
397                 g_free(rcpath);
398                 return;
399         }
400         g_free(rcpath);
401
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);
406                 g_free(act);
407         }
408
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;
412                 gchar *tmp;
413
414                 tmp = conv_codeset_strdup(buf, src_codeset, dest_codeset);
415                 if (!tmp) {
416                         g_warning("Failed to convert character set of action configuration\n");
417                         tmp = g_strdup(buf);
418                 }
419
420                 g_strchomp(tmp);
421                 act = strstr(tmp, ": ");
422                 if (act && act[2] && 
423                     action_get_type(&act[2]) != ACTION_ERROR)
424                         prefs_common.actions_list =
425                                 g_slist_append(prefs_common.actions_list,
426                                                tmp);
427                 else
428                         g_free(tmp);
429         }
430         fclose(fp);
431 }
432
433 void prefs_actions_write_config(void)
434 {
435         gchar *rcpath;
436         PrefFile *pfile;
437         GSList *cur;
438
439         debug_print("Writing actions configuration...\n");
440
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");
444                 g_free(rcpath);
445                 return;
446         }
447
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();
452                 gchar *act;
453
454                 act = conv_codeset_strdup(tmp, src_codeset, dest_codeset);
455                 if (!act) {
456                         g_warning("Failed to convert character set of action configuration\n");
457                         act = g_strdup(act);
458                 }
459
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);
464                         g_free(rcpath);
465                         return;
466                 }
467                 g_free(act);
468         }
469         
470         g_free(rcpath);
471
472         if (prefs_file_close(pfile) < 0) {
473                 g_warning("failed to write configuration to file\n");
474                 return;
475         }
476 }
477
478 static void prefs_actions_clear_list(GtkListStore *list_store)
479 {
480         gtk_list_store_clear(list_store);
481
482         prefs_actions_list_view_insert_action(actions.actions_list_view,
483                                               -1, _("(New)"), FALSE);
484 }
485
486 static void prefs_actions_set_dialog(void)
487 {
488         GtkListStore *store;
489         GSList *cur;
490         GtkTreeSelection *selection;
491         GtkTreeIter iter;
492
493         store = GTK_LIST_STORE(gtk_tree_view_get_model
494                                 (GTK_TREE_VIEW(actions.actions_list_view)));
495
496         prefs_actions_clear_list(store);        
497
498         for (cur = prefs_common.actions_list; cur != NULL; cur = cur->next) {
499                 gchar *action = (gchar *) cur->data;
500                 
501                 prefs_actions_list_view_insert_action(actions.actions_list_view,
502                                                       -1, action, TRUE);
503         }
504
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),
509                                           &iter))
510                 gtk_tree_selection_select_iter(selection, &iter);
511 }
512
513 static void prefs_actions_set_list(void)
514 {
515         GtkTreeIter iter;
516         GtkListStore *store;
517         
518         g_slist_free(prefs_common.actions_list);
519         prefs_common.actions_list = NULL;
520
521         store = GTK_LIST_STORE(gtk_tree_view_get_model
522                                 (GTK_TREE_VIEW(actions.actions_list_view)));
523
524         if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter)) {
525                 do {
526                         gchar *action;
527                         gboolean is_valid;
528
529                         gtk_tree_model_get(GTK_TREE_MODEL(store), &iter,
530                                            PREFS_ACTIONS_DATA, &action,
531                                            PREFS_ACTIONS_VALID, &is_valid,
532                                            -1);
533                         
534                         if (is_valid) 
535                                 prefs_common.actions_list = 
536                                         g_slist_append(prefs_common.actions_list,
537                                                        action);
538
539                 } while (gtk_tree_model_iter_next(GTK_TREE_MODEL(store),
540                                                   &iter));
541         }
542 }
543
544 #define GET_ENTRY(entry) \
545         entry_text = gtk_entry_get_text(GTK_ENTRY(entry))
546
547 static gint prefs_actions_clist_set_row(gint row)
548 {
549         const gchar *entry_text;
550         gint len;
551         gchar action[PREFSBUFSIZE];
552         gchar *new_action;
553         GtkListStore *store;
554
555         store = GTK_LIST_STORE(gtk_tree_view_get_model
556                                 (GTK_TREE_VIEW(actions.actions_list_view)));
557
558         GET_ENTRY(actions.name_entry);
559         if (entry_text[0] == '\0') {
560                 alertpanel_error(_("Menu name is not set."));
561                 return -1;
562         }
563
564         if (entry_text[0] == '/') {
565                 alertpanel_error(_("A leading '/' is not allowed in the menu name."));
566                 return -1;
567         }
568
569         if (strchr(entry_text, ':')) {
570                 alertpanel_error(_("Colon ':' is not allowed in the menu name."));
571                 return -1;
572         }
573
574         strncpy(action, entry_text, PREFSBUFSIZE - 1);
575
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';
582         }
583         
584         g_strstrip(action);
585
586         /* Keep space for the ': ' delimiter */
587         len = strlen(action) + 2;
588         if (len >= PREFSBUFSIZE - 1) {
589                 alertpanel_error(_("Menu name is too long."));
590                 return -1;
591         }
592
593         strcat(action, ": ");
594
595         GET_ENTRY(actions.cmd_entry);
596
597         if (entry_text[0] == '\0') {
598                 alertpanel_error(_("Command line not set."));
599                 return -1;
600         }
601
602         if (len + strlen(entry_text) >= PREFSBUFSIZE - 1) {
603                 alertpanel_error(_("Menu name and command are too long."));
604                 return -1;
605         }
606
607         if (action_get_type(entry_text) == ACTION_ERROR) {
608                 gchar *message;
609                 message = g_markup_printf_escaped(_("The command\n%s\nhas a syntax error."),
610                                                 entry_text);
611                 alertpanel_error(message);
612                 g_free(message);
613                 return -1;
614         }
615
616         strcat(action, entry_text);
617
618         new_action = g_strdup(action);  
619         prefs_actions_list_view_insert_action(actions.actions_list_view,
620                                               row, new_action, TRUE);
621                                                 
622         prefs_actions_set_list();
623
624         return 0;
625 }
626
627 /* callback functions */
628
629 static void prefs_actions_register_cb(GtkWidget *w, gpointer data)
630 {
631         prefs_actions_clist_set_row(-1);
632
633         modified = FALSE;
634         modified_list = TRUE;
635 }
636
637 static void prefs_actions_substitute_cb(GtkWidget *w, gpointer data)
638 {
639         gint row;
640
641         row = gtkut_list_view_get_selected_row(actions.actions_list_view);
642         if (row <= 0)
643                 return;
644
645         prefs_actions_clist_set_row(row);
646
647         modified = FALSE;
648         modified_list = TRUE;
649 }
650
651 static void prefs_actions_delete_cb(GtkWidget *w, gpointer data)
652 {
653         GtkTreeIter sel;
654         GtkTreeModel *model;
655         gchar *action;
656         gint row;
657
658         row = gtkut_list_view_get_selected_row(actions.actions_list_view);
659         if (row <= 0) 
660                 return; 
661
662         if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection
663                                 (GTK_TREE_VIEW(actions.actions_list_view)),
664                                 &model, &sel))
665                 return;                         
666
667         if (alertpanel(_("Delete action"),
668                        _("Do you really want to delete this action?"),
669                        GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL) != G_ALERTALTERNATE)
670                 return;
671
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,
676                            -1);
677         gtk_list_store_remove(GTK_LIST_STORE(model), &sel);
678
679         prefs_common.actions_list = g_slist_remove(prefs_common.actions_list,
680                                                    action);
681         modified_list = TRUE;
682 }
683
684 static void prefs_actions_delete_all_cb(GtkWidget *w, gpointer data)
685 {
686         GtkListStore *list_store;
687
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)
691            return;
692
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);
695         modified = FALSE;
696
697         prefs_actions_reset_dialog();
698         modified_list = TRUE;
699 }
700
701 static void prefs_actions_clear_cb(void)
702 {
703         gint row;
704
705         prefs_actions_reset_dialog();
706         row = gtkut_list_view_get_selected_row(actions.actions_list_view);
707         if (row < 1)
708                 modified = FALSE;
709         else
710                 modified = TRUE;
711 }
712
713 static void prefs_actions_duplicate_cb(GtkWidget *w, gpointer data)
714 {
715         gint row;
716         
717         row = gtkut_list_view_get_selected_row(actions.actions_list_view);
718         if (row <= 0)
719                 return;
720
721         modified_list = !prefs_actions_clist_set_row(-row-2);
722 }
723
724 static void prefs_actions_up(GtkWidget *w, gpointer data)
725 {
726         GtkTreePath *prev, *sel, *try;
727         GtkTreeIter isel;
728         GtkListStore *store = NULL;
729         GtkTreeModel *model = NULL;
730         GtkTreeIter iprev;
731         
732         if (!gtk_tree_selection_get_selected
733                 (gtk_tree_view_get_selection
734                         (GTK_TREE_VIEW(actions.actions_list_view)),
735                  &model,        
736                  &isel))
737                 return;
738         store = (GtkListStore *)model;
739         sel = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &isel);
740         if (!sel)
741                 return;
742         
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);
749                 return;
750         }
751         gtk_tree_path_free(try);
752
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);
757                 return;
758         }
759
760         gtk_tree_model_get_iter(GTK_TREE_MODEL(store),
761                                 &iprev, prev);
762         gtk_tree_path_free(sel);
763         gtk_tree_path_free(prev);
764
765         gtk_list_store_swap(store, &iprev, &isel);
766         prefs_actions_set_list();
767         modified_list = TRUE;
768 }
769
770 static void prefs_actions_down(GtkWidget *w, gpointer data)
771 {
772         GtkListStore *store = NULL;
773         GtkTreeModel *model = NULL;
774         GtkTreeIter next, sel;
775         GtkTreePath *try;
776         
777         if (!gtk_tree_selection_get_selected
778                 (gtk_tree_view_get_selection
779                         (GTK_TREE_VIEW(actions.actions_list_view)),
780                  &model,
781                  &sel))
782                 return;
783         store = (GtkListStore *)model;
784         try = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &sel);
785         if (!try) 
786                 return;
787
788         /* no move when we're at row 0 */
789         if (!gtk_tree_path_prev(try)) {
790                 gtk_tree_path_free(try);
791                 return;
792         }
793         gtk_tree_path_free(try);
794
795         next = sel;
796         if (!gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &next)) 
797                 return;
798
799         gtk_list_store_swap(store, &next, &sel);
800         prefs_actions_set_list();
801         modified_list = TRUE;
802 }
803
804 static gint prefs_actions_deleted(GtkWidget *widget, GdkEventAny *event,
805                                   gpointer *data)
806 {
807         prefs_actions_cancel(widget, data);
808         return TRUE;
809 }
810
811 static gboolean prefs_actions_key_pressed(GtkWidget *widget, GdkEventKey *event,
812                                           gpointer data)
813 {
814         if (event && event->keyval == GDK_Escape)
815                 prefs_actions_cancel(widget, data);
816         else {
817                 GtkWidget *focused = gtkut_get_focused_child(
818                                         GTK_CONTAINER(widget));
819                 if (focused && GTK_IS_EDITABLE(focused)) {
820                         modified = TRUE;
821                 }
822         }
823         return FALSE;
824 }
825
826 static void prefs_actions_cancel(GtkWidget *w, gpointer data)
827 {
828         GtkListStore *store;
829
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) {
834                 return;
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) {
839                 return;
840         }
841         modified = FALSE;
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);
848         inc_unlock();
849 }
850
851 static void prefs_actions_ok(GtkWidget *widget, gpointer data)
852 {
853         MainWindow *mainwin = (MainWindow *) data;
854         GList *list;
855         GList *iter;
856         MessageView *msgview;
857         Compose *compose;
858         GtkListStore *store;
859
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) {
864                 return;
865         } 
866         modified = FALSE;
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();
873
874         /* Update mainwindow actions menu */
875         main_window_update_actions_menu(mainwin);
876
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);
882         }
883
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);
889         }
890
891         gtk_widget_hide(actions.window);
892         inc_unlock();
893 }
894
895 /*
896  * Strings describing action format strings
897  * 
898  * When adding new lines, remember to put one string for each line
899  */
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,
903         "", 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         "     &gt;",   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         "     &gt;",   N_("to insert command's standard output without replacing old text"),
912         "     &amp;",   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 %"),
922         NULL, NULL
923 };
924
925
926 static DescriptionWindow actions_desc_win = { 
927         NULL,
928         NULL,
929         2,
930         N_("Actions"),
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."),
934         actions_desc_strings
935 };
936
937
938 static void prefs_actions_info_cb(GtkWidget *w, GtkWidget *window)
939 {
940         actions_desc_win.parent = window;
941         description_window_create(&actions_desc_win);
942 }
943
944 static GtkListStore* prefs_actions_create_data_store(void)
945 {
946         return gtk_list_store_new(N_PREFS_ACTIONS_COLUMNS,
947                                   G_TYPE_STRING,        
948                                   G_TYPE_POINTER,
949                                   G_TYPE_BOOLEAN,
950                                   -1);
951 }
952
953 static void prefs_actions_list_view_insert_action(GtkWidget *list_view,
954                                                   gint row,
955                                                   gchar *action,
956                                                   gboolean is_valid) 
957 {
958         GtkTreeIter iter;
959         GtkTreeIter sibling;
960         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
961                                         (GTK_TREE_VIEW(list_view)));
962
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)
966 */
967         if (row >= 0 ) {
968                 /* modify the existing */
969                 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
970                                                    &iter, NULL, row))
971                         row = -1;
972         } else if (row < -1 ) {
973                 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
974                                                    &sibling, NULL, -row-2))
975                         row = -1;               
976         }
977
978         if (row == -1 ) {
979                 /* append new */
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,
985                                    -1);
986         } else if (row < -1) {
987                 /* duplicate */
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,
993                                    -1);
994         } else {
995                 /* change existing */
996                 gchar *old_action;
997
998                 gtk_tree_model_get(GTK_TREE_MODEL(list_store), &iter,
999                                    PREFS_ACTIONS_DATA, &old_action,
1000                                    -1);
1001                 g_free(old_action);                             
1002
1003                 gtk_list_store_set(list_store, &iter,
1004                                    PREFS_ACTIONS_STRING, action,
1005                                    PREFS_ACTIONS_DATA, action,
1006                                    -1);
1007         }
1008 }
1009
1010 static GtkItemFactory *prefs_actions_popup_factory = NULL;
1011 static GtkWidget *prefs_actions_popup_menu = NULL;
1012
1013 static GtkItemFactoryEntry prefs_actions_popup_entries[] =
1014 {
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},
1018 };
1019
1020 static gint prefs_actions_list_btn_pressed(GtkWidget *widget, GdkEventButton *event,
1021                                    GtkTreeView *list_view)
1022 {
1023    if (event) {
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);
1030                    }
1031                    if (path)
1032                            gtk_tree_path_free(path);
1033            }
1034
1035            /* right-button click */
1036            if (event->button == 3) {
1037                    GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1038                    GtkTreeIter iter;
1039                    gboolean non_empty;
1040                    gint row;
1041
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);
1048                    }
1049
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));
1054
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);
1058                    if (non_empty)
1059                            non_empty = gtk_tree_model_iter_next(model, &iter);
1060                    menu_set_sensitive(prefs_actions_popup_factory, "/Delete all", non_empty);
1061
1062                    gtk_menu_popup(GTK_MENU(prefs_actions_popup_menu), 
1063                                           NULL, NULL, NULL, NULL, 
1064                                           event->button, event->time);
1065            }
1066    }
1067    return FALSE;
1068 }
1069
1070 static gboolean prefs_actions_list_popup_menu(GtkWidget *widget, gpointer data)
1071 {
1072    GtkTreeView *list_view = (GtkTreeView *)data;
1073    GdkEventButton event;
1074    
1075    event.button = 3;
1076    event.time = gtk_get_current_event_time();
1077    
1078    prefs_actions_list_btn_pressed(NULL, &event, list_view);
1079
1080    return TRUE;
1081 }
1082
1083 static GtkWidget *prefs_actions_list_view_create(void)
1084 {
1085         GtkTreeView *list_view;
1086         GtkTreeSelection *selector;
1087         GtkTreeModel *model;
1088
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);  
1092         
1093 #ifndef MAEMO
1094         g_signal_connect(G_OBJECT(list_view), "popup-menu",
1095                          G_CALLBACK(prefs_actions_list_popup_menu), list_view);
1096 #else
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);
1101 #endif
1102         g_signal_connect(G_OBJECT(list_view), "button-press-event",
1103                         G_CALLBACK(prefs_actions_list_btn_pressed), list_view);
1104
1105         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
1106         gtk_tree_view_set_reorderable(list_view, TRUE);
1107
1108         selector = gtk_tree_view_get_selection(list_view);
1109         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1110
1111         /* create the columns */
1112         prefs_actions_create_list_view_columns(GTK_WIDGET(list_view));
1113
1114         return GTK_WIDGET(list_view);
1115 }
1116
1117 static void prefs_actions_create_list_view_columns(GtkWidget *list_view)
1118 {
1119         GtkTreeViewColumn *column;
1120         GtkCellRenderer *renderer;
1121
1122         renderer = gtk_cell_renderer_text_new();
1123         column = gtk_tree_view_column_new_with_attributes
1124                 (_("Current actions"),
1125                  renderer,
1126                  "text", PREFS_ACTIONS_STRING,
1127                  NULL);
1128         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
1129 }
1130
1131 #define ENTRY_SET_TEXT(entry, str) \
1132         gtk_entry_set_text(GTK_ENTRY(entry), str ? str : "")
1133
1134 static void prefs_actions_select_row(GtkTreeView *list_view, GtkTreePath *path)
1135 {
1136         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1137         GtkTreeSelection *selection;
1138         gchar *action;
1139         gchar *cmd;
1140         gchar buf[PREFSBUFSIZE];
1141         GtkTreeIter iter;
1142         gboolean is_valid;
1143
1144         if (!model || !path || !gtk_tree_model_get_iter(model, &iter, path))
1145                 return;
1146
1147         /* select row */
1148         selection = gtk_tree_view_get_selection(list_view);
1149         gtk_tree_selection_select_path(selection, path);
1150
1151         gtk_tree_model_get(model, &iter, 
1152                            PREFS_ACTIONS_VALID,  &is_valid,
1153                            PREFS_ACTIONS_DATA, &action,
1154                            -1);
1155         if (!is_valid) {
1156                 prefs_actions_reset_dialog();
1157                 return;
1158         }
1159         
1160         strncpy(buf, action, PREFSBUFSIZE - 1);
1161         buf[PREFSBUFSIZE - 1] = '\0';
1162         cmd = strstr(buf, ": ");
1163
1164         if (cmd && cmd[2])
1165                 ENTRY_SET_TEXT(actions.cmd_entry, &cmd[2]);
1166         else
1167                 return;
1168
1169         *cmd = '\0';
1170         ENTRY_SET_TEXT(actions.name_entry, buf);
1171
1172         return;
1173 }