a6eb2eed4525f73873f155bce9d5df846f88bd66
[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
49 enum {
50         PREFS_ACTIONS_STRING,   /*!< string pointer managed by list store, 
51                                  *   and never touched or retrieved by 
52                                  *   us */ 
53         PREFS_ACTIONS_DATA,     /*!< pointer to string that is not managed by 
54                                  *   the list store, and which is retrieved
55                                  *   and touched by us */
56         PREFS_ACTIONS_VALID,    /*!< contains a valid action, otherwise "(New)" */
57         N_PREFS_ACTIONS_COLUMNS
58 };
59
60 static struct Actions
61 {
62         GtkWidget *window;
63
64         GtkWidget *ok_btn;
65
66         GtkWidget *name_entry;
67         GtkWidget *cmd_entry;
68
69         GtkWidget *actions_list_view;
70 } actions;
71
72 static int modified = FALSE;
73 static int modified_list = FALSE;
74
75 /* widget creating functions */
76 static void prefs_actions_create        (MainWindow *mainwin);
77 static void prefs_actions_set_dialog    (void);
78 static gint prefs_actions_clist_set_row (GtkTreeIter *row);
79
80 /* callback functions */
81 static void prefs_actions_info_cb       (GtkWidget      *w,
82                                          GtkWidget      *window);
83 static void prefs_actions_register_cb   (GtkWidget      *w,
84                                          gpointer        data);
85 static void prefs_actions_substitute_cb (GtkWidget      *w,
86                                          gpointer        data);
87 static void prefs_actions_delete_cb     (GtkWidget      *w,
88                                          gpointer        data);
89 static void prefs_actions_up            (GtkWidget      *w,
90                                          gpointer        data);
91 static void prefs_actions_down          (GtkWidget      *w,
92                                          gpointer        data);
93 static gint prefs_actions_deleted       (GtkWidget      *widget,
94                                          GdkEventAny    *event,
95                                          gpointer       *data);
96 static gboolean prefs_actions_key_pressed(GtkWidget     *widget,
97                                           GdkEventKey   *event,
98                                           gpointer       data);
99 static void prefs_actions_cancel        (GtkWidget      *w,
100                                          gpointer        data);
101 static void prefs_actions_ok            (GtkWidget      *w,
102                                          gpointer        data);
103
104
105 static GtkListStore* prefs_actions_create_data_store    (void);
106
107 static void prefs_actions_list_view_insert_action       (GtkWidget *list_view,
108                                                          GtkTreeIter *row_iter,
109                                                          gchar *action,
110                                                          gboolean is_valid);
111 static GtkWidget *prefs_actions_list_view_create        (void);
112 static void prefs_actions_create_list_view_columns      (GtkWidget *list_view);
113 static gboolean prefs_actions_selected                  (GtkTreeSelection *selector,
114                                                          GtkTreeModel *model, 
115                                                          GtkTreePath *path,
116                                                          gboolean currently_selected,
117                                                          gpointer data);
118
119 void prefs_actions_open(MainWindow *mainwin)
120 {
121         inc_lock();
122
123         if (!actions.window)
124                 prefs_actions_create(mainwin);
125
126         manage_window_set_transient(GTK_WINDOW(actions.window));
127         gtk_widget_grab_focus(actions.ok_btn);
128
129         prefs_actions_set_dialog();
130
131         gtk_widget_show(actions.window);
132 }
133
134 /*!
135  *\brief        Save Gtk object size to prefs dataset
136  */
137 static void prefs_actions_size_allocate_cb(GtkWidget *widget,
138                                          GtkAllocation *allocation)
139 {
140         g_return_if_fail(allocation != NULL);
141
142         prefs_common.actionswin_width = allocation->width;
143         prefs_common.actionswin_height = allocation->height;
144 }
145
146 static void prefs_actions_create(MainWindow *mainwin)
147 {
148         GtkWidget *window;
149         GtkWidget *vbox;
150         GtkWidget *help_btn;
151         GtkWidget *ok_btn;
152         GtkWidget *cancel_btn;
153         GtkWidget *confirm_area;
154
155         GtkWidget *vbox1;
156         GtkWidget *table;
157
158         GtkWidget *name_label;
159         GtkWidget *name_entry;
160         GtkWidget *cmd_label;
161         GtkWidget *cmd_entry;
162
163         GtkWidget *reg_hbox;
164         GtkWidget *btn_hbox;
165         GtkWidget *arrow;
166         GtkWidget *reg_btn;
167         GtkWidget *subst_btn;
168         GtkWidget *del_btn;
169
170         GtkWidget *cond_hbox;
171         GtkWidget *cond_scrolledwin;
172         GtkWidget *cond_list_view;
173
174         GtkWidget *info_btn;
175
176         GtkWidget *btn_vbox;
177         GtkWidget *up_btn;
178         GtkWidget *down_btn;
179         static GdkGeometry geometry;
180
181         debug_print("Creating actions configuration window...\n");
182
183         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "prefs_actions");
184
185         gtk_container_set_border_width(GTK_CONTAINER (window), 8);
186         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
187         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
188         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
189
190         vbox = gtk_vbox_new(FALSE, 6);
191         gtk_widget_show(vbox);
192         gtk_container_add(GTK_CONTAINER(window), vbox);
193
194         gtkut_stock_button_set_create_with_help(&confirm_area, &help_btn,
195                         &cancel_btn, GTK_STOCK_CANCEL,
196                         &ok_btn, GTK_STOCK_OK,
197                         NULL, NULL);
198         gtk_widget_show(confirm_area);
199         gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
200         gtk_widget_grab_default(ok_btn);
201
202         gtk_window_set_title(GTK_WINDOW(window), _("Actions configuration"));
203         g_signal_connect(G_OBJECT(window), "delete_event",
204                          G_CALLBACK(prefs_actions_deleted), NULL);
205         g_signal_connect(G_OBJECT(window), "size_allocate",
206                          G_CALLBACK(prefs_actions_size_allocate_cb), NULL);
207         g_signal_connect(G_OBJECT(window), "key_press_event",
208                          G_CALLBACK(prefs_actions_key_pressed), NULL);
209         MANAGE_WINDOW_SIGNALS_CONNECT(window);
210         g_signal_connect(G_OBJECT(ok_btn), "clicked",
211                          G_CALLBACK(prefs_actions_ok), mainwin);
212         g_signal_connect(G_OBJECT(cancel_btn), "clicked",
213                          G_CALLBACK(prefs_actions_cancel), NULL);
214         g_signal_connect(G_OBJECT(help_btn), "clicked",
215                          G_CALLBACK(manual_open_with_anchor_cb),
216                          MANUAL_ANCHOR_ACTIONS);
217
218         vbox1 = gtk_vbox_new(FALSE, VSPACING);
219         gtk_widget_show(vbox1);
220         gtk_box_pack_start(GTK_BOX(vbox), vbox1, TRUE, TRUE, 0);
221         gtk_container_set_border_width(GTK_CONTAINER(vbox1), 2);
222
223         table = gtk_table_new(2, 2, FALSE);
224         gtk_table_set_row_spacings (GTK_TABLE (table), VSPACING_NARROW_2);
225         gtk_table_set_col_spacings (GTK_TABLE (table), 4);
226         gtk_widget_show(table);
227         gtk_box_pack_start (GTK_BOX (vbox1), table, FALSE, FALSE, 0);
228
229         name_label = gtk_label_new (_("Menu name"));
230         gtk_widget_show (name_label);
231         gtk_misc_set_alignment (GTK_MISC (name_label), 1, 0.5);
232         gtk_table_attach (GTK_TABLE (table), name_label, 0, 1, 0, 1,
233                           (GtkAttachOptions) (GTK_FILL),
234                           (GtkAttachOptions) (0), 0, 0);
235
236         name_entry = gtk_entry_new ();
237         gtk_widget_show (name_entry);
238         gtk_table_attach (GTK_TABLE (table), name_entry, 1, 2, 0, 1,
239                           (GtkAttachOptions) (GTK_FILL|GTK_EXPAND),
240                           (GtkAttachOptions) (0), 0, 0);
241
242         cmd_label = gtk_label_new (_("Command line"));
243         gtk_widget_show (cmd_label);
244         gtk_misc_set_alignment (GTK_MISC (cmd_label), 1, 0.5);
245         gtk_table_attach (GTK_TABLE (table), cmd_label, 0, 1, 1, 2,
246                           (GtkAttachOptions) (GTK_FILL),
247                           (GtkAttachOptions) (0), 0, 0);
248
249         cmd_entry = gtk_entry_new ();
250         gtk_widget_show (cmd_entry);
251         gtk_table_attach (GTK_TABLE (table), cmd_entry, 1, 2, 1, 2,
252                           (GtkAttachOptions) (GTK_FILL|GTK_EXPAND),
253                           (GtkAttachOptions) (0), 0, 0);
254
255         /* register / substitute / delete */
256
257         reg_hbox = gtk_hbox_new(FALSE, 4);
258         gtk_widget_show(reg_hbox);
259         gtk_box_pack_start(GTK_BOX(vbox1), reg_hbox, FALSE, FALSE, 0);
260
261         arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
262         gtk_widget_show(arrow);
263         gtk_box_pack_start(GTK_BOX(reg_hbox), arrow, FALSE, FALSE, 0);
264         gtk_widget_set_size_request(arrow, -1, 16);
265
266         btn_hbox = gtk_hbox_new(TRUE, 4);
267         gtk_widget_show(btn_hbox);
268         gtk_box_pack_start(GTK_BOX(reg_hbox), btn_hbox, FALSE, FALSE, 0);
269
270         reg_btn = gtk_button_new_from_stock(GTK_STOCK_ADD);
271         gtk_widget_show(reg_btn);
272         gtk_box_pack_start(GTK_BOX(btn_hbox), reg_btn, FALSE, TRUE, 0);
273         g_signal_connect(G_OBJECT(reg_btn), "clicked",
274                          G_CALLBACK(prefs_actions_register_cb), NULL);
275
276         subst_btn = gtkut_get_replace_btn(_("Replace"));
277         gtk_widget_show(subst_btn);
278         gtk_box_pack_start(GTK_BOX(btn_hbox), subst_btn, FALSE, TRUE, 0);
279         g_signal_connect(G_OBJECT(subst_btn), "clicked",
280                          G_CALLBACK(prefs_actions_substitute_cb),
281                          NULL);
282
283         del_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
284         gtk_widget_show(del_btn);
285         gtk_box_pack_start(GTK_BOX(btn_hbox), del_btn, FALSE, TRUE, 0);
286         g_signal_connect(G_OBJECT(del_btn), "clicked",
287                          G_CALLBACK(prefs_actions_delete_cb), NULL);
288
289 #if GTK_CHECK_VERSION(2, 8, 0)
290         info_btn = gtk_button_new_from_stock(GTK_STOCK_INFO);
291 #else
292         info_btn = gtk_button_new_with_label(_("Info..."));
293 #endif
294         gtk_widget_show(info_btn);
295         gtk_box_pack_end(GTK_BOX(reg_hbox), info_btn, FALSE, FALSE, 0);
296         g_signal_connect(G_OBJECT(info_btn), "clicked",
297                          G_CALLBACK(prefs_actions_info_cb), GTK_WINDOW(window));
298
299         cond_hbox = gtk_hbox_new(FALSE, 8);
300         gtk_widget_show(cond_hbox);
301         gtk_box_pack_start(GTK_BOX(vbox1), cond_hbox, TRUE, TRUE, 0);
302
303         cond_scrolledwin = gtk_scrolled_window_new(NULL, NULL);
304         gtk_widget_show(cond_scrolledwin);
305         gtk_widget_set_size_request(cond_scrolledwin, -1, 150);
306         gtk_box_pack_start(GTK_BOX(cond_hbox), cond_scrolledwin,
307                            TRUE, TRUE, 0);
308         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (cond_scrolledwin),
309                                        GTK_POLICY_AUTOMATIC,
310                                        GTK_POLICY_AUTOMATIC);
311
312         cond_list_view = prefs_actions_list_view_create();                                     
313         gtk_widget_show(cond_list_view);
314         gtk_container_add(GTK_CONTAINER (cond_scrolledwin), cond_list_view);
315
316         btn_vbox = gtk_vbox_new(FALSE, 8);
317         gtk_widget_show(btn_vbox);
318         gtk_box_pack_start(GTK_BOX(cond_hbox), btn_vbox, FALSE, FALSE, 0);
319
320         up_btn = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
321         gtk_widget_show(up_btn);
322         gtk_box_pack_start(GTK_BOX(btn_vbox), up_btn, FALSE, FALSE, 0);
323         g_signal_connect(G_OBJECT(up_btn), "clicked",
324                          G_CALLBACK(prefs_actions_up), NULL);
325
326         down_btn = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
327         gtk_widget_show(down_btn);
328         gtk_box_pack_start(GTK_BOX(btn_vbox), down_btn, FALSE, FALSE, 0);
329         g_signal_connect(G_OBJECT(down_btn), "clicked",
330                          G_CALLBACK(prefs_actions_down), NULL);
331
332         if (!geometry.min_height) {
333                 geometry.min_width = 486;
334                 geometry.min_height = 322;
335         }
336
337         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
338                                       GDK_HINT_MIN_SIZE);
339         gtk_widget_set_size_request(window, prefs_common.actionswin_width,
340                                     prefs_common.actionswin_height);
341
342         gtk_widget_show(window);
343
344         actions.window = window;
345         actions.ok_btn = ok_btn;
346
347         actions.name_entry = name_entry;
348         actions.cmd_entry  = cmd_entry;
349
350         actions.actions_list_view = cond_list_view;
351 }
352
353
354 void prefs_actions_read_config(void)
355 {
356         gchar *rcpath;
357         FILE *fp;
358         gchar buf[PREFSBUFSIZE];
359         gchar *act;
360
361         debug_print("Reading actions configurations...\n");
362
363         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACTIONS_RC, NULL);
364         if ((fp = g_fopen(rcpath, "rb")) == NULL) {
365                 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
366                 g_free(rcpath);
367                 return;
368         }
369         g_free(rcpath);
370
371         while (prefs_common.actions_list != NULL) {
372                 act = (gchar *)prefs_common.actions_list->data;
373                 prefs_common.actions_list =
374                         g_slist_remove(prefs_common.actions_list, act);
375                 g_free(act);
376         }
377
378         while (fgets(buf, sizeof(buf), fp) != NULL) {
379                 const gchar *src_codeset = conv_get_locale_charset_str();
380                 const gchar *dest_codeset = CS_UTF_8;
381                 gchar *tmp;
382
383                 tmp = conv_codeset_strdup(buf, src_codeset, dest_codeset);
384                 if (!tmp) {
385                         g_warning("Failed to convert character set of action configuration\n");
386                         tmp = g_strdup(buf);
387                 }
388
389                 g_strchomp(tmp);
390                 act = strstr(tmp, ": ");
391                 if (act && act[2] && 
392                     action_get_type(&act[2]) != ACTION_ERROR)
393                         prefs_common.actions_list =
394                                 g_slist_append(prefs_common.actions_list,
395                                                tmp);
396                 else
397                         g_free(tmp);
398         }
399         fclose(fp);
400 }
401
402 void prefs_actions_write_config(void)
403 {
404         gchar *rcpath;
405         PrefFile *pfile;
406         GSList *cur;
407
408         debug_print("Writing actions configuration...\n");
409
410         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACTIONS_RC, NULL);
411         if ((pfile= prefs_write_open(rcpath)) == NULL) {
412                 g_warning("Failed to write configuration to file\n");
413                 g_free(rcpath);
414                 return;
415         }
416
417         for (cur = prefs_common.actions_list; cur != NULL; cur = cur->next) {
418                 gchar *tmp = (gchar *)cur->data;
419                 const gchar *src_codeset = CS_UTF_8;
420                 const gchar *dest_codeset = conv_get_locale_charset_str();
421                 gchar *act;
422
423                 act = conv_codeset_strdup(tmp, src_codeset, dest_codeset);
424                 if (!act) {
425                         g_warning("Failed to convert character set of action configuration\n");
426                         act = g_strdup(act);
427                 }
428
429                 if (fputs(act, pfile->fp) == EOF ||
430                     fputc('\n', pfile->fp) == EOF) {
431                         FILE_OP_ERROR(rcpath, "fputs || fputc");
432                         prefs_file_close_revert(pfile);
433                         g_free(rcpath);
434                         return;
435                 }
436                 g_free(act);
437         }
438         
439         g_free(rcpath);
440
441         if (prefs_file_close(pfile) < 0) {
442                 g_warning("failed to write configuration to file\n");
443                 return;
444         }
445 }
446
447 static void prefs_actions_set_dialog(void)
448 {
449         GtkListStore *store;
450         GSList *cur;
451         GtkTreeSelection *selection;
452         GtkTreeIter iter;
453
454         store = GTK_LIST_STORE(gtk_tree_view_get_model
455                                 (GTK_TREE_VIEW(actions.actions_list_view)));
456         gtk_list_store_clear(store);
457
458         prefs_actions_list_view_insert_action(actions.actions_list_view,
459                                               NULL, _("(New)"), FALSE);
460
461         for (cur = prefs_common.actions_list; cur != NULL; cur = cur->next) {
462                 gchar *action = (gchar *) cur->data;
463                 
464                 prefs_actions_list_view_insert_action(actions.actions_list_view,
465                                                       NULL, action, TRUE);
466         }
467
468         /* select first entry */
469         selection = gtk_tree_view_get_selection
470                 (GTK_TREE_VIEW(actions.actions_list_view));
471         if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store),
472                                           &iter))
473                 gtk_tree_selection_select_iter(selection, &iter);
474 }
475
476 static void prefs_actions_set_list(void)
477 {
478         GtkTreeIter iter;
479         GtkListStore *store;
480         
481         g_slist_free(prefs_common.actions_list);
482         prefs_common.actions_list = NULL;
483
484         store = GTK_LIST_STORE(gtk_tree_view_get_model
485                                 (GTK_TREE_VIEW(actions.actions_list_view)));
486
487         if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter)) {
488                 do {
489                         gchar *action;
490                         gboolean is_valid;
491
492                         gtk_tree_model_get(GTK_TREE_MODEL(store), &iter,
493                                            PREFS_ACTIONS_DATA, &action,
494                                            PREFS_ACTIONS_VALID, &is_valid,
495                                            -1);
496                         
497                         if (is_valid) 
498                                 prefs_common.actions_list = 
499                                         g_slist_append(prefs_common.actions_list,
500                                                        action);
501
502                 } while (gtk_tree_model_iter_next(GTK_TREE_MODEL(store),
503                                                   &iter));
504         }
505 }
506
507 #define GET_ENTRY(entry) \
508         entry_text = gtk_entry_get_text(GTK_ENTRY(entry))
509
510 static gint prefs_actions_clist_set_row(GtkTreeIter *row)
511 {
512         const gchar *entry_text;
513         gint len;
514         gchar action[PREFSBUFSIZE];
515         gchar *new_action;
516         GtkListStore *store;
517
518         store = GTK_LIST_STORE(gtk_tree_view_get_model
519                                 (GTK_TREE_VIEW(actions.actions_list_view)));
520         
521
522         GET_ENTRY(actions.name_entry);
523         if (entry_text[0] == '\0') {
524                 alertpanel_error(_("Menu name is not set."));
525                 return -1;
526         }
527
528         if (entry_text[0] == '/') {
529                 alertpanel_error(_("A leading '/' is not allowed in the menu name."));
530                 return -1;
531         }
532
533         if (strchr(entry_text, ':')) {
534                 alertpanel_error(_("Colon ':' is not allowed in the menu name."));
535                 return -1;
536         }
537
538         strncpy(action, entry_text, PREFSBUFSIZE - 1);
539
540         while (strstr(action, "//")) {
541                 char *to_move = strstr(action, "//")+1;
542                 char *where = strstr(action, "//");
543                 int old_len = strlen(action);
544                 memmove(where, to_move, strlen(to_move));
545                 action[old_len-1] = '\0';
546         }
547         
548         g_strstrip(action);
549
550         /* Keep space for the ': ' delimiter */
551         len = strlen(action) + 2;
552         if (len >= PREFSBUFSIZE - 1) {
553                 alertpanel_error(_("Menu name is too long."));
554                 return -1;
555         }
556
557         strcat(action, ": ");
558
559         GET_ENTRY(actions.cmd_entry);
560
561         if (entry_text[0] == '\0') {
562                 alertpanel_error(_("Command line not set."));
563                 return -1;
564         }
565
566         if (len + strlen(entry_text) >= PREFSBUFSIZE - 1) {
567                 alertpanel_error(_("Menu name and command are too long."));
568                 return -1;
569         }
570
571         if (action_get_type(entry_text) == ACTION_ERROR) {
572                 gchar *message;
573                 message = g_markup_printf_escaped(_("The command\n%s\nhas a syntax error."),
574                                                 entry_text);
575                 alertpanel_error(message);
576                 g_free(message);
577                 return -1;
578         }
579
580         strcat(action, entry_text);
581
582         new_action = g_strdup(action);  
583         prefs_actions_list_view_insert_action(actions.actions_list_view,
584                                               row, new_action, TRUE);
585                                                 
586         prefs_actions_set_list();
587
588         return 0;
589 }
590
591 /* callback functions */
592
593 static void prefs_actions_register_cb(GtkWidget *w, gpointer data)
594 {
595         prefs_actions_clist_set_row(NULL);
596         modified = FALSE;
597         modified_list = TRUE;
598 }
599
600 static void prefs_actions_substitute_cb(GtkWidget *w, gpointer data)
601 {
602         GtkTreeIter isel, inew;
603         GtkTreePath *path_sel, *path_new;
604         GtkTreeSelection *selection = gtk_tree_view_get_selection
605                                         (GTK_TREE_VIEW(actions.actions_list_view));
606         GtkTreeModel *model;                                    
607
608         if (!gtk_tree_selection_get_selected(selection, &model, &isel))
609                 return;
610         if (!gtk_tree_model_get_iter_first(model, &inew))
611                 return;
612
613         path_sel = gtk_tree_model_get_path(model, &isel);
614         path_new = gtk_tree_model_get_path(model, &inew);
615
616         if (path_sel && path_new 
617         &&  gtk_tree_path_compare(path_sel, path_new) != 0)
618                 prefs_actions_clist_set_row(&isel);
619
620         gtk_tree_path_free(path_sel);
621         gtk_tree_path_free(path_new);
622         modified = FALSE;
623         modified_list = TRUE;
624 }
625
626 static void prefs_actions_delete_cb(GtkWidget *w, gpointer data)
627 {
628         GtkTreeIter sel;
629         GtkTreeModel *model;
630         gchar *action;
631
632         if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection
633                                 (GTK_TREE_VIEW(actions.actions_list_view)),
634                                 &model, &sel))
635                 return;                         
636
637         if (alertpanel(_("Delete action"),
638                        _("Do you really want to delete this action?"),
639                        GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL) != G_ALERTALTERNATE)
640                 return;
641
642         /* XXX: Here's the reason why we need to store the original 
643          * pointer: we search the slist for it. */
644         gtk_tree_model_get(model, &sel,
645                            PREFS_ACTIONS_DATA, &action,
646                            -1);
647         gtk_list_store_remove(GTK_LIST_STORE(model), &sel);
648
649         prefs_common.actions_list = g_slist_remove(prefs_common.actions_list,
650                                                    action);
651         modified_list = TRUE;
652 }
653
654 static void prefs_actions_up(GtkWidget *w, gpointer data)
655 {
656         GtkTreePath *prev, *sel, *try;
657         GtkTreeIter isel;
658         GtkListStore *store = NULL;
659         GtkTreeModel *model = NULL;
660         GtkTreeIter iprev;
661         
662         if (!gtk_tree_selection_get_selected
663                 (gtk_tree_view_get_selection
664                         (GTK_TREE_VIEW(actions.actions_list_view)),
665                  &model,        
666                  &isel))
667                 return;
668         store = (GtkListStore *)model;
669         sel = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &isel);
670         if (!sel)
671                 return;
672         
673         /* no move if we're at row 0 or 1, looks phony, but other
674          * solutions are more convoluted... */
675         try = gtk_tree_path_copy(sel);
676         if (!gtk_tree_path_prev(try) || !gtk_tree_path_prev(try)) {
677                 gtk_tree_path_free(try);
678                 gtk_tree_path_free(sel);
679                 return;
680         }
681         gtk_tree_path_free(try);
682
683         prev = gtk_tree_path_copy(sel);         
684         if (!gtk_tree_path_prev(prev)) {
685                 gtk_tree_path_free(prev);
686                 gtk_tree_path_free(sel);
687                 return;
688         }
689
690         gtk_tree_model_get_iter(GTK_TREE_MODEL(store),
691                                 &iprev, prev);
692         gtk_tree_path_free(sel);
693         gtk_tree_path_free(prev);
694
695         gtk_list_store_swap(store, &iprev, &isel);
696         prefs_actions_set_list();
697         modified_list = TRUE;
698 }
699
700 static void prefs_actions_down(GtkWidget *w, gpointer data)
701 {
702         GtkListStore *store = NULL;
703         GtkTreeModel *model = NULL;
704         GtkTreeIter next, sel;
705         GtkTreePath *try;
706         
707         if (!gtk_tree_selection_get_selected
708                 (gtk_tree_view_get_selection
709                         (GTK_TREE_VIEW(actions.actions_list_view)),
710                  &model,
711                  &sel))
712                 return;
713         store = (GtkListStore *)model;
714         try = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &sel);
715         if (!try) 
716                 return;
717
718         /* no move when we're at row 0 */
719         if (!gtk_tree_path_prev(try)) {
720                 gtk_tree_path_free(try);
721                 return;
722         }
723         gtk_tree_path_free(try);
724
725         next = sel;
726         if (!gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &next)) 
727                 return;
728
729         gtk_list_store_swap(store, &next, &sel);
730         prefs_actions_set_list();
731         modified_list = TRUE;
732 }
733
734 static gint prefs_actions_deleted(GtkWidget *widget, GdkEventAny *event,
735                                   gpointer *data)
736 {
737         prefs_actions_cancel(widget, data);
738         return TRUE;
739 }
740
741 static gboolean prefs_actions_key_pressed(GtkWidget *widget, GdkEventKey *event,
742                                           gpointer data)
743 {
744         if (event && event->keyval == GDK_Escape)
745                 prefs_actions_cancel(widget, data);
746         else {
747                 GtkWidget *focused = gtkut_get_focused_child(
748                                         GTK_CONTAINER(widget));
749                 if (focused && GTK_IS_EDITABLE(focused)) {
750                         modified = TRUE;
751                 }
752         }
753         return FALSE;
754 }
755
756 static void prefs_actions_cancel(GtkWidget *w, gpointer data)
757 {
758         if (modified && alertpanel(_("Entry not saved"),
759                                  _("The entry was not saved. Close anyway?"),
760                                  GTK_STOCK_CLOSE, _("+_Continue editing"),
761                                  NULL) != G_ALERTDEFAULT) {
762                 return;
763         } else if (modified_list && alertpanel(_("Actions list not saved"),
764                                  _("The actions list has been modified. Close anyway?"),
765                                  GTK_STOCK_CLOSE, _("+_Continue editing"), 
766                                  NULL) != G_ALERTDEFAULT) {
767                 return;
768         }
769         modified = FALSE;
770         modified_list = FALSE;
771         prefs_actions_read_config();
772         gtk_widget_hide(actions.window);
773         inc_unlock();
774 }
775
776 static void prefs_actions_ok(GtkWidget *widget, gpointer data)
777 {
778         MainWindow *mainwin = (MainWindow *) data;
779         GList *list;
780         GList *iter;
781         MessageView *msgview;
782         Compose *compose;
783
784         if (modified && alertpanel(_("Entry not saved"),
785                                  _("The entry was not saved. Close anyway?"),
786                                  GTK_STOCK_CLOSE, _("+_Continue editing"),
787                                  NULL) != G_ALERTDEFAULT) {
788                 return;
789         } else if (modified_list && alertpanel(_("Actions list not saved"),
790                                  _("The actions list has been modified. Close anyway?"),
791                                  GTK_STOCK_CLOSE, _("+_Continue editing"), 
792                                  NULL) != G_ALERTDEFAULT) {
793                 return;
794         }
795         modified = FALSE;
796         modified_list = FALSE;
797         prefs_actions_set_list();
798         prefs_actions_write_config();
799
800         /* Update mainwindow actions menu */
801         main_window_update_actions_menu(mainwin);
802
803         /* Update separated message view actions menu */
804         list = messageview_get_msgview_list();
805         for (iter = list; iter; iter = iter->next) {
806                 msgview = (MessageView *) iter->data;
807                 messageview_update_actions_menu(msgview);
808         }
809
810         /* Update compose windows actions menu */
811         list = compose_get_compose_list();
812         for (iter = list; iter; iter = iter->next) {
813                 compose = (Compose *) iter->data;
814                 compose_update_actions_menu(compose);
815         }
816
817         gtk_widget_hide(actions.window);
818         inc_unlock();
819 }
820
821 /*
822  * Strings describing action format strings
823  * 
824  * When adding new lines, remember to put one string for each line
825  */
826 static gchar *actions_desc_strings[] = {
827         N_("<span weight=\"bold\" underline=\"single\">Menu name:</span>"), NULL,
828         N_("Use / in menu name to make submenus."), NULL,
829         "", NULL,
830         N_("<span weight=\"bold\" underline=\"single\">Command line:</span>"), NULL,
831         N_("<span weight=\"bold\">Begin with:</span>"), NULL,
832         "     |",   N_("to send message body or selection to command's standard input"),
833         "     &gt;",   N_("to send user provided text to command's standard input"),
834         "     *",   N_("to send user provided hidden text to command's standard input"),
835         N_("<span weight=\"bold\">End with:</span>"), NULL,
836         "     |",   N_("to replace message body or selection with command's standard output"),
837         "     &gt;",   N_("to insert command's standard output without replacing old text"),
838         "     &amp;",   N_("to run command asynchronously"),
839         N_("<span weight=\"bold\">Use:</span>"), NULL, 
840         "     %f",  N_("for the file of the selected message in RFC822/2822 format "),
841         "     %F",  N_("for the list of the files of the selected messages in RFC822/2822 format"),
842         "     %p",  N_("for the file of the selected decoded message MIME part"),
843         "     %u",  N_("for a user provided argument"),
844         "     %h",  N_("for a user provided hidden argument (e.g. password)"),
845         "     %s",  N_("for the text selection"),
846         "  %as{}",  N_("apply filtering actions between {} to selected messages"),
847         "     %%",  N_("for a literal %"),
848         NULL, NULL
849 };
850
851
852 static DescriptionWindow actions_desc_win = { 
853         NULL,
854         NULL,
855         2,
856         N_("Actions"),
857         N_("The Actions feature is a way for the user to launch "
858            "external commands to process a complete message file or just "
859            "one of its parts."),
860         actions_desc_strings
861 };
862
863
864 static void prefs_actions_info_cb(GtkWidget *w, GtkWidget *window)
865 {
866         actions_desc_win.parent = window;
867         description_window_create(&actions_desc_win);
868 }
869
870 static GtkListStore* prefs_actions_create_data_store(void)
871 {
872         return gtk_list_store_new(N_PREFS_ACTIONS_COLUMNS,
873                                   G_TYPE_STRING,        
874                                   G_TYPE_POINTER,
875                                   G_TYPE_BOOLEAN,
876                                   -1);
877 }
878
879 static void prefs_actions_list_view_insert_action(GtkWidget *list_view,
880                                                   GtkTreeIter *row_iter,
881                                                   gchar *action,
882                                                   gboolean is_valid) 
883 {
884         GtkTreeIter iter;
885         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
886                                         (GTK_TREE_VIEW(list_view)));
887
888         if (row_iter == NULL) {
889                 /* append new */
890                 gtk_list_store_append(list_store, &iter);
891                 gtk_list_store_set(list_store, &iter,
892                                    PREFS_ACTIONS_STRING, action,
893                                    PREFS_ACTIONS_DATA, action,
894                                    PREFS_ACTIONS_VALID,  is_valid,
895                                    -1);
896         } else {
897                 /* change existing */
898                 gchar *old_action;
899
900                 gtk_tree_model_get(GTK_TREE_MODEL(list_store), row_iter,
901                                    PREFS_ACTIONS_DATA, &old_action,
902                                    -1);
903                 
904                 g_free(old_action);                             
905                 gtk_list_store_set(list_store, row_iter,
906                                    PREFS_ACTIONS_STRING, action,
907                                    PREFS_ACTIONS_DATA, action,
908                                    -1);
909         }
910 }
911
912 static GtkWidget *prefs_actions_list_view_create(void)
913 {
914         GtkTreeView *list_view;
915         GtkTreeSelection *selector;
916         GtkTreeModel *model;
917
918         model = GTK_TREE_MODEL(prefs_actions_create_data_store());
919         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
920         g_object_unref(model);  
921         
922         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
923         gtk_tree_view_set_reorderable(list_view, TRUE);
924
925         selector = gtk_tree_view_get_selection(list_view);
926         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
927         gtk_tree_selection_set_select_function(selector, prefs_actions_selected,
928                                                NULL, NULL);
929
930         /* create the columns */
931         prefs_actions_create_list_view_columns(GTK_WIDGET(list_view));
932
933         return GTK_WIDGET(list_view);
934 }
935
936 static void prefs_actions_create_list_view_columns(GtkWidget *list_view)
937 {
938         GtkTreeViewColumn *column;
939         GtkCellRenderer *renderer;
940
941         renderer = gtk_cell_renderer_text_new();
942         column = gtk_tree_view_column_new_with_attributes
943                 (_("Current actions"),
944                  renderer,
945                  "text", PREFS_ACTIONS_STRING,
946                  NULL);
947         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
948 }
949
950 #define ENTRY_SET_TEXT(entry, str) \
951         gtk_entry_set_text(GTK_ENTRY(entry), str ? str : "")
952
953 static gboolean prefs_actions_selected(GtkTreeSelection *selector,
954                                        GtkTreeModel *model, 
955                                        GtkTreePath *path,
956                                        gboolean currently_selected,
957                                        gpointer data)
958 {
959         gchar *action;
960         gchar *cmd;
961         gchar buf[PREFSBUFSIZE];
962         GtkTreeIter iter;
963         gboolean is_valid;
964
965         if (currently_selected)
966                 return TRUE;
967
968         if (!gtk_tree_model_get_iter(model, &iter, path))
969                 return TRUE;
970
971         gtk_tree_model_get(model, &iter, 
972                            PREFS_ACTIONS_VALID,  &is_valid,
973                            PREFS_ACTIONS_DATA, &action,
974                            -1);
975         if (!is_valid) {
976                 ENTRY_SET_TEXT(actions.name_entry, "");
977                 ENTRY_SET_TEXT(actions.cmd_entry, "");
978                 return TRUE;
979         }
980         
981         strncpy(buf, action, PREFSBUFSIZE - 1);
982         buf[PREFSBUFSIZE - 1] = 0x00;
983         cmd = strstr(buf, ": ");
984
985         if (cmd && cmd[2])
986                 ENTRY_SET_TEXT(actions.cmd_entry, &cmd[2]);
987         else
988                 return TRUE;
989
990         *cmd = 0x00;
991         ENTRY_SET_TEXT(actions.name_entry, buf);
992
993         return TRUE;
994 }
995