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