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