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