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