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