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