2008-06-13 [wwp] 3.4.0cvs93
[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 #include "menu.h"
49 #include "filtering.h"
50 #include "prefs_filtering_action.h"
51 #include "matcher_parser.h"
52
53 enum {
54         PREFS_ACTIONS_STRING,   /*!< string pointer managed by list store, 
55                                  *   and never touched or retrieved by 
56                                  *   us */ 
57         PREFS_ACTIONS_DATA,     /*!< pointer to string that is not managed by 
58                                  *   the list store, and which is retrieved
59                                  *   and touched by us */
60         PREFS_ACTIONS_VALID,    /*!< contains a valid action, otherwise "(New)" */
61         N_PREFS_ACTIONS_COLUMNS
62 };
63
64 static struct Actions
65 {
66         GtkWidget *window;
67
68         GtkWidget *ok_btn;
69         GtkWidget *filter_btn;
70         GtkWidget *name_entry;
71         GtkWidget *cmd_entry;
72         GtkWidget *info_btn;
73         GtkWidget *shell_radiobtn;
74         GtkWidget *filter_radiobtn;
75         
76         GtkWidget *actions_list_view;
77 } actions;
78
79 static int modified = FALSE;
80 static int modified_list = FALSE;
81
82 /* widget creating functions */
83 static void prefs_actions_create        (MainWindow *mainwin);
84 static void prefs_actions_set_dialog    (void);
85 static gint prefs_actions_clist_set_row (gint row);
86
87 /* callback functions */
88 static void prefs_actions_info_cb       (GtkWidget      *w,
89                                          GtkWidget      *window);
90 static void prefs_actions_register_cb   (GtkWidget      *w,
91                                          gpointer        data);
92 static void prefs_actions_substitute_cb (GtkWidget      *w,
93                                          gpointer        data);
94 static void prefs_actions_delete_cb     (GtkWidget      *w,
95                                          gpointer        data);
96 static void prefs_actions_delete_all_cb (GtkWidget      *w,
97                                          gpointer        data);
98 static void prefs_actions_clear_cb      (void);
99 static void prefs_actions_duplicate_cb  (GtkWidget      *w,
100                                          gpointer        data);
101 static void prefs_actions_up            (GtkWidget      *w,
102                                          gpointer        data);
103 static void prefs_actions_down          (GtkWidget      *w,
104                                          gpointer        data);
105 static gint prefs_actions_deleted       (GtkWidget      *widget,
106                                          GdkEventAny    *event,
107                                          gpointer       *data);
108 static gboolean prefs_actions_key_pressed(GtkWidget     *widget,
109                                           GdkEventKey   *event,
110                                           gpointer       data);
111 static void prefs_actions_cancel        (GtkWidget      *w,
112                                          gpointer        data);
113 static void prefs_actions_ok            (GtkWidget      *w,
114                                          gpointer        data);
115
116 static GtkListStore* prefs_actions_create_data_store    (void);
117
118 static void prefs_actions_list_view_insert_action       (GtkWidget *list_view,
119                                                          gint row,
120                                                          gchar *action,
121                                                          gboolean is_valid);
122 static GtkWidget *prefs_actions_list_view_create        (void);
123 static void prefs_actions_create_list_view_columns      (GtkWidget *list_view);
124 static void prefs_actions_select_row(GtkTreeView *list_view, GtkTreePath *path);
125
126 static void prefs_action_filter_radiobtn_cb(GtkWidget *widget, gpointer data);
127 static void prefs_action_shell_radiobtn_cb(GtkWidget *widget, gpointer data);
128 static void prefs_action_filterbtn_cb(GtkWidget *widget, gpointer data);
129 static void prefs_action_define_filter_done(GSList * action_list);
130
131
132 void prefs_actions_open(MainWindow *mainwin)
133 {
134         inc_lock();
135
136         if (!actions.window)
137                 prefs_actions_create(mainwin);
138
139         manage_window_set_transient(GTK_WINDOW(actions.window));
140         gtk_widget_grab_focus(actions.ok_btn);
141
142         prefs_actions_set_dialog();
143
144         gtk_widget_show(actions.window);
145 }
146
147 /*!
148  *\brief        Save Gtk object size to prefs dataset
149  */
150 static void prefs_actions_size_allocate_cb(GtkWidget *widget,
151                                          GtkAllocation *allocation)
152 {
153         g_return_if_fail(allocation != NULL);
154
155         prefs_common.actionswin_width = allocation->width;
156         prefs_common.actionswin_height = allocation->height;
157 }
158
159 static void prefs_actions_create(MainWindow *mainwin)
160 {
161         GtkWidget *window;
162         GtkWidget *vbox;
163         GtkWidget *filter_hbox;
164         GtkWidget *help_btn;
165         GtkWidget *ok_btn;
166         GtkWidget *cancel_btn;
167         GtkWidget *confirm_area;
168
169         GtkWidget *vbox1;
170         GtkWidget *table;
171
172         GtkWidget *shell_radiobtn;
173         GtkWidget *filter_radiobtn;
174
175         GtkWidget *name_label;
176         GtkWidget *name_entry;
177         GtkWidget *cmd_label;
178         GtkWidget *cmd_entry;
179         GtkWidget *filter_btn;
180
181         GtkWidget *reg_hbox;
182         GtkWidget *btn_hbox;
183         GtkWidget *arrow;
184         GtkWidget *reg_btn;
185         GtkWidget *subst_btn;
186         GtkWidget *del_btn;
187         GtkWidget *clear_btn;
188
189         GtkWidget *cond_hbox;
190         GtkWidget *cond_scrolledwin;
191         GtkWidget *cond_list_view;
192
193         GtkWidget *info_btn;
194
195         GtkWidget *btn_vbox;
196         GtkWidget *up_btn;
197         GtkWidget *down_btn;
198         static GdkGeometry geometry;
199         GtkTooltips *tooltips;
200
201         debug_print("Creating actions configuration window...\n");
202
203         tooltips = gtk_tooltips_new();
204
205         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "prefs_actions");
206
207         gtk_container_set_border_width(GTK_CONTAINER (window), 8);
208         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
209         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
210         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
211
212         vbox = gtk_vbox_new(FALSE, 6);
213         gtk_widget_show(vbox);
214         gtk_container_add(GTK_CONTAINER(window), vbox);
215
216         gtkut_stock_button_set_create_with_help(&confirm_area, &help_btn,
217                         &cancel_btn, GTK_STOCK_CANCEL,
218                         &ok_btn, GTK_STOCK_OK,
219                         NULL, NULL);
220         gtk_widget_show(confirm_area);
221         gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
222         gtk_widget_grab_default(ok_btn);
223
224         gtk_window_set_title(GTK_WINDOW(window), _("Actions configuration"));
225         g_signal_connect(G_OBJECT(window), "delete_event",
226                          G_CALLBACK(prefs_actions_deleted), NULL);
227         g_signal_connect(G_OBJECT(window), "size_allocate",
228                          G_CALLBACK(prefs_actions_size_allocate_cb), NULL);
229         g_signal_connect(G_OBJECT(window), "key_press_event",
230                          G_CALLBACK(prefs_actions_key_pressed), NULL);
231         MANAGE_WINDOW_SIGNALS_CONNECT(window);
232         g_signal_connect(G_OBJECT(ok_btn), "clicked",
233                          G_CALLBACK(prefs_actions_ok), mainwin);
234         g_signal_connect(G_OBJECT(cancel_btn), "clicked",
235                          G_CALLBACK(prefs_actions_cancel), NULL);
236         g_signal_connect(G_OBJECT(help_btn), "clicked",
237                          G_CALLBACK(manual_open_with_anchor_cb),
238                          MANUAL_ANCHOR_ACTIONS);
239
240         vbox1 = gtk_vbox_new(FALSE, VSPACING);
241         gtk_widget_show(vbox1);
242         gtk_box_pack_start(GTK_BOX(vbox), vbox1, TRUE, TRUE, 0);
243         gtk_container_set_border_width(GTK_CONTAINER(vbox1), 2);        
244
245         table = gtk_table_new(3, 2, FALSE);
246         gtk_table_set_row_spacings (GTK_TABLE (table), VSPACING_NARROW_2);
247         gtk_table_set_col_spacings (GTK_TABLE (table), 4);
248         gtk_widget_show(table);
249         gtk_box_pack_start (GTK_BOX (vbox1), table, FALSE, FALSE, 0);
250
251         name_label = gtk_label_new (_("Menu name"));
252         gtk_widget_show (name_label);
253         gtk_misc_set_alignment (GTK_MISC (name_label), 1, 0.5);
254         gtk_table_attach (GTK_TABLE (table), name_label, 0, 1, 0, 1,
255                           (GtkAttachOptions) (GTK_FILL),
256                           (GtkAttachOptions) (0), 0, 0);
257
258         name_entry = gtk_entry_new ();
259         gtk_widget_show (name_entry);
260         gtk_table_attach (GTK_TABLE (table), name_entry, 1, 2, 0, 1,
261                           (GtkAttachOptions) (GTK_FILL|GTK_EXPAND),
262                           (GtkAttachOptions) (0), 0, 0);
263
264         cmd_label = gtk_label_new (_("Command"));
265         gtk_widget_show (cmd_label);
266         gtk_misc_set_alignment (GTK_MISC (cmd_label), 1, 0.5);
267         gtk_table_attach (GTK_TABLE (table), cmd_label, 0, 1, 2, 3,
268                           (GtkAttachOptions) (GTK_FILL),
269                           (GtkAttachOptions) (0), 0, 0);
270
271         cmd_entry = gtk_entry_new ();
272         gtk_widget_show (cmd_entry);
273         gtk_table_attach (GTK_TABLE (table), cmd_entry, 1, 2, 2, 3,
274                           (GtkAttachOptions) (GTK_FILL|GTK_EXPAND),
275                           (GtkAttachOptions) (0), 0, 0);
276
277         /* radio buttons for filter actions or shell */
278         filter_hbox = gtk_hbox_new(FALSE,4);
279         gtk_table_attach(GTK_TABLE(table), filter_hbox, 1, 2, 3, 4,
280                           (GtkAttachOptions) (GTK_FILL|GTK_EXPAND),
281                           (GtkAttachOptions) (0), 0, 0);
282         gtk_widget_show(filter_hbox);
283
284         shell_radiobtn = gtk_radio_button_new_with_label(NULL, _("Shell command"));
285         gtk_box_pack_start(GTK_BOX(filter_hbox), shell_radiobtn, FALSE, FALSE, 0);
286         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(shell_radiobtn), TRUE);
287         gtk_widget_show(shell_radiobtn);
288         
289         g_signal_connect(G_OBJECT(shell_radiobtn), "clicked",
290                          G_CALLBACK(prefs_action_shell_radiobtn_cb), NULL);
291
292         filter_radiobtn =
293                 gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(shell_radiobtn), 
294                                                             _("Filter action"));
295         gtk_box_pack_start(GTK_BOX(filter_hbox), filter_radiobtn, FALSE, FALSE, 0);
296         gtk_widget_show(filter_radiobtn);
297         g_signal_connect(G_OBJECT(filter_radiobtn), "clicked",
298                          G_CALLBACK(prefs_action_filter_radiobtn_cb), NULL);
299
300         filter_btn = gtk_button_new_with_label(_("Edit filter action"));
301         gtk_box_pack_start(GTK_BOX(filter_hbox), filter_btn, FALSE, FALSE, 0);
302         gtk_widget_set_sensitive(filter_btn, FALSE);
303         g_signal_connect(G_OBJECT(filter_btn), "clicked",
304                          G_CALLBACK(prefs_action_filterbtn_cb), NULL);
305         gtk_widget_show(filter_btn);
306
307         /* register / substitute / delete */
308
309         reg_hbox = gtk_hbox_new(FALSE, 4);
310         gtk_widget_show(reg_hbox);
311         gtk_box_pack_start(GTK_BOX(vbox1), reg_hbox, FALSE, FALSE, 0);
312
313         arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
314         gtk_widget_show(arrow);
315         gtk_box_pack_start(GTK_BOX(reg_hbox), arrow, FALSE, FALSE, 0);
316         gtk_widget_set_size_request(arrow, -1, 16);
317
318         btn_hbox = gtk_hbox_new(TRUE, 4);
319         gtk_widget_show(btn_hbox);
320         gtk_box_pack_start(GTK_BOX(reg_hbox), btn_hbox, FALSE, FALSE, 0);
321
322         reg_btn = gtk_button_new_from_stock(GTK_STOCK_ADD);
323         gtk_widget_show(reg_btn);
324         gtk_box_pack_start(GTK_BOX(btn_hbox), reg_btn, FALSE, TRUE, 0);
325         g_signal_connect(G_OBJECT(reg_btn), "clicked",
326                          G_CALLBACK(prefs_actions_register_cb), NULL);
327         gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), reg_btn,
328                         _("Append the new action above to the list"), NULL);
329
330         subst_btn = gtkut_get_replace_btn(_("Replace"));
331         gtk_widget_show(subst_btn);
332         gtk_box_pack_start(GTK_BOX(btn_hbox), subst_btn, FALSE, TRUE, 0);
333         g_signal_connect(G_OBJECT(subst_btn), "clicked",
334                          G_CALLBACK(prefs_actions_substitute_cb), NULL);
335         gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), subst_btn,
336                         _("Replace the selected action in list with the action above"), NULL);
337
338         del_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
339         gtk_widget_show(del_btn);
340         gtk_box_pack_start(GTK_BOX(btn_hbox), del_btn, FALSE, TRUE, 0);
341         g_signal_connect(G_OBJECT(del_btn), "clicked",
342                          G_CALLBACK(prefs_actions_delete_cb), NULL);
343         gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), del_btn,
344                         _("Delete the selected action from the list"), NULL);
345
346         clear_btn = gtk_button_new_from_stock (GTK_STOCK_CLEAR);
347         gtk_widget_show (clear_btn);
348         gtk_box_pack_start (GTK_BOX (btn_hbox), clear_btn, FALSE, TRUE, 0);
349         g_signal_connect(G_OBJECT (clear_btn), "clicked",
350                         G_CALLBACK(prefs_actions_clear_cb), NULL);
351         gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), clear_btn,
352                         _("Clear all the input fields in the dialog"), NULL);
353
354 #if GTK_CHECK_VERSION(2, 8, 0)
355         info_btn = gtk_button_new_from_stock(GTK_STOCK_INFO);
356 #else
357         info_btn = gtk_button_new_with_label(_("Info..."));
358 #endif
359         gtk_widget_show(info_btn);
360         gtk_box_pack_end(GTK_BOX(reg_hbox), info_btn, FALSE, FALSE, 0);
361         g_signal_connect(G_OBJECT(info_btn), "clicked",
362                          G_CALLBACK(prefs_actions_info_cb), GTK_WINDOW(window));
363         gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), info_btn,
364                         _("Show information on configuring actions"), NULL);
365
366         cond_hbox = gtk_hbox_new(FALSE, 8);
367         gtk_widget_show(cond_hbox);
368         gtk_box_pack_start(GTK_BOX(vbox1), cond_hbox, TRUE, TRUE, 0);
369
370         cond_scrolledwin = gtk_scrolled_window_new(NULL, NULL);
371         gtk_widget_show(cond_scrolledwin);
372         gtk_widget_set_size_request(cond_scrolledwin, -1, 150);
373         gtk_box_pack_start(GTK_BOX(cond_hbox), cond_scrolledwin,
374                            TRUE, TRUE, 0);
375         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (cond_scrolledwin),
376                                        GTK_POLICY_AUTOMATIC,
377                                        GTK_POLICY_AUTOMATIC);
378         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(cond_scrolledwin),
379                                             GTK_SHADOW_ETCHED_IN);
380
381         cond_list_view = prefs_actions_list_view_create();                                     
382         gtk_widget_show(cond_list_view);
383         gtk_container_add(GTK_CONTAINER (cond_scrolledwin), cond_list_view);
384
385         btn_vbox = gtk_vbox_new(FALSE, 8);
386         gtk_widget_show(btn_vbox);
387         gtk_box_pack_start(GTK_BOX(cond_hbox), btn_vbox, FALSE, FALSE, 0);
388
389         up_btn = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
390         gtk_widget_show(up_btn);
391         gtk_box_pack_start(GTK_BOX(btn_vbox), up_btn, FALSE, FALSE, 0);
392         g_signal_connect(G_OBJECT(up_btn), "clicked",
393                          G_CALLBACK(prefs_actions_up), NULL);
394         gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), up_btn,
395                         _("Move the selected action up"), NULL);
396
397         down_btn = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
398         gtk_widget_show(down_btn);
399         gtk_box_pack_start(GTK_BOX(btn_vbox), down_btn, FALSE, FALSE, 0);
400         g_signal_connect(G_OBJECT(down_btn), "clicked",
401                          G_CALLBACK(prefs_actions_down), NULL);
402         gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), down_btn,
403                         _("Move selected action down"), NULL);
404
405         if (!geometry.min_height) {
406                 geometry.min_width = 486;
407                 geometry.min_height = 322;
408         }
409
410         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
411                                       GDK_HINT_MIN_SIZE);
412         gtk_widget_set_size_request(window, prefs_common.actionswin_width,
413                                     prefs_common.actionswin_height);
414
415         gtk_widget_show(window);
416
417         actions.window = window;
418         actions.ok_btn = ok_btn;
419         actions.info_btn = info_btn;
420
421         actions.name_entry = name_entry;
422         actions.cmd_entry  = cmd_entry;
423         actions.filter_btn = filter_btn;
424         actions.shell_radiobtn = shell_radiobtn;
425         actions.filter_radiobtn = filter_radiobtn;
426         
427         actions.actions_list_view = cond_list_view;
428 }
429
430 static void prefs_actions_reset_dialog(void)
431 {
432         gtk_entry_set_text(GTK_ENTRY(actions.name_entry), "");
433         gtk_entry_set_text(GTK_ENTRY(actions.cmd_entry), "");
434 }
435
436 void prefs_actions_read_config(void)
437 {
438         gchar *rcpath;
439         FILE *fp;
440         gchar buf[PREFSBUFSIZE];
441         gchar *act;
442
443         debug_print("Reading actions configurations...\n");
444
445         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACTIONS_RC, NULL);
446         if ((fp = g_fopen(rcpath, "rb")) == NULL) {
447                 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
448                 g_free(rcpath);
449                 return;
450         }
451         g_free(rcpath);
452
453         while (prefs_common.actions_list != NULL) {
454                 act = (gchar *)prefs_common.actions_list->data;
455                 prefs_common.actions_list =
456                         g_slist_remove(prefs_common.actions_list, act);
457                 g_free(act);
458         }
459
460         while (fgets(buf, sizeof(buf), fp) != NULL) {
461                 const gchar *src_codeset = conv_get_locale_charset_str();
462                 const gchar *dest_codeset = CS_UTF_8;
463                 gchar *tmp;
464
465                 tmp = conv_codeset_strdup(buf, src_codeset, dest_codeset);
466                 if (!tmp) {
467                         g_warning("Failed to convert character set of action configuration\n");
468                         tmp = g_strdup(buf);
469                 }
470
471                 g_strchomp(tmp);
472                 act = strstr(tmp, ": ");
473                 if (act && act[2] && 
474                     action_get_type(&act[2]) != ACTION_ERROR)
475                         prefs_common.actions_list =
476                                 g_slist_append(prefs_common.actions_list,
477                                                tmp);
478                 else
479                         g_free(tmp);
480         }
481         fclose(fp);
482 }
483
484 void prefs_actions_write_config(void)
485 {
486         gchar *rcpath;
487         PrefFile *pfile;
488         GSList *cur;
489
490         debug_print("Writing actions configuration...\n");
491
492         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACTIONS_RC, NULL);
493         if ((pfile= prefs_write_open(rcpath)) == NULL) {
494                 g_warning("Failed to write configuration to file\n");
495                 g_free(rcpath);
496                 return;
497         }
498
499         for (cur = prefs_common.actions_list; cur != NULL; cur = cur->next) {
500                 gchar *tmp = (gchar *)cur->data;
501                 const gchar *src_codeset = CS_UTF_8;
502                 const gchar *dest_codeset = conv_get_locale_charset_str();
503                 gchar *act;
504
505                 act = conv_codeset_strdup(tmp, src_codeset, dest_codeset);
506                 if (!act) {
507                         g_warning("Failed to convert character set of action configuration\n");
508                         act = g_strdup(act);
509                 }
510
511                 if (fputs(act, pfile->fp) == EOF ||
512                     fputc('\n', pfile->fp) == EOF) {
513                         FILE_OP_ERROR(rcpath, "fputs || fputc");
514                         prefs_file_close_revert(pfile);
515                         g_free(rcpath);
516                         return;
517                 }
518                 g_free(act);
519         }
520         
521         g_free(rcpath);
522
523         if (prefs_file_close(pfile) < 0) {
524                 g_warning("failed to write configuration to file\n");
525                 return;
526         }
527 }
528
529 static void prefs_actions_clear_list(GtkListStore *list_store)
530 {
531         gtk_list_store_clear(list_store);
532
533         prefs_actions_list_view_insert_action(actions.actions_list_view,
534                                               -1, _("(New)"), FALSE);
535 }
536
537 static void prefs_actions_set_dialog(void)
538 {
539         GtkListStore *store;
540         GSList *cur;
541         GtkTreeSelection *selection;
542         GtkTreeIter iter;
543
544         store = GTK_LIST_STORE(gtk_tree_view_get_model
545                                 (GTK_TREE_VIEW(actions.actions_list_view)));
546
547         prefs_actions_clear_list(store);        
548
549         for (cur = prefs_common.actions_list; cur != NULL; cur = cur->next) {
550                 gchar *action = (gchar *) cur->data;
551                 
552                 prefs_actions_list_view_insert_action(actions.actions_list_view,
553                                                       -1, action, TRUE);
554         }
555
556         /* select first entry */
557         selection = gtk_tree_view_get_selection
558                 (GTK_TREE_VIEW(actions.actions_list_view));
559         if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store),
560                                           &iter))
561                 gtk_tree_selection_select_iter(selection, &iter);
562 }
563
564 static void prefs_actions_set_list(void)
565 {
566         GtkTreeIter iter;
567         GtkListStore *store;
568         
569         g_slist_free(prefs_common.actions_list);
570         prefs_common.actions_list = NULL;
571
572         store = GTK_LIST_STORE(gtk_tree_view_get_model
573                                 (GTK_TREE_VIEW(actions.actions_list_view)));
574
575         if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter)) {
576                 do {
577                         gchar *action;
578                         gboolean is_valid;
579
580                         gtk_tree_model_get(GTK_TREE_MODEL(store), &iter,
581                                            PREFS_ACTIONS_DATA, &action,
582                                            PREFS_ACTIONS_VALID, &is_valid,
583                                            -1);
584                         
585                         if (is_valid) 
586                                 prefs_common.actions_list = 
587                                         g_slist_append(prefs_common.actions_list,
588                                                        action);
589
590                 } while (gtk_tree_model_iter_next(GTK_TREE_MODEL(store),
591                                                   &iter));
592         }
593 }
594
595 #define GET_ENTRY(entry) \
596         entry_text = gtk_entry_get_text(GTK_ENTRY(entry))
597
598 static gint prefs_actions_clist_set_row(gint row)
599 {
600         const gchar *entry_text;
601         gint len;
602         gchar action[PREFSBUFSIZE];
603         gchar *new_action;
604         GtkListStore *store;
605
606         store = GTK_LIST_STORE(gtk_tree_view_get_model
607                                 (GTK_TREE_VIEW(actions.actions_list_view)));
608
609         GET_ENTRY(actions.name_entry);
610         if (entry_text[0] == '\0') {
611                 alertpanel_error(_("Menu name is not set."));
612                 return -1;
613         }
614
615         if (entry_text[0] == '/') {
616                 alertpanel_error(_("A leading '/' is not allowed in the menu name."));
617                 return -1;
618         }
619
620         if (strchr(entry_text, ':')) {
621                 alertpanel_error(_("Colon ':' is not allowed in the menu name."));
622                 return -1;
623         }
624
625         strncpy(action, entry_text, PREFSBUFSIZE - 1);
626
627         while (strstr(action, "//")) {
628                 char *to_move = strstr(action, "//")+1;
629                 char *where = strstr(action, "//");
630                 int old_len = strlen(action);
631                 memmove(where, to_move, strlen(to_move));
632                 action[old_len-1] = '\0';
633         }
634         
635         g_strstrip(action);
636
637         /* Keep space for the ': ' delimiter */
638         len = strlen(action) + 2;
639         if (len >= PREFSBUFSIZE - 1) {
640                 alertpanel_error(_("Menu name is too long."));
641                 return -1;
642         }
643
644         strcat(action, ": ");
645
646         GET_ENTRY(actions.cmd_entry);
647
648         if (entry_text[0] == '\0') {
649                 alertpanel_error(_("Command line not set."));
650                 return -1;
651         }
652
653         if (len + strlen(entry_text) >= PREFSBUFSIZE - 1) {
654                 alertpanel_error(_("Menu name and command are too long."));
655                 return -1;
656         }
657
658         if (action_get_type(entry_text) == ACTION_ERROR) {
659                 gchar *message;
660                 message = g_markup_printf_escaped(_("The command\n%s\nhas a syntax error."),
661                                                 entry_text);
662                 alertpanel_error(message);
663                 g_free(message);
664                 return -1;
665         }
666
667         strcat(action, entry_text);
668
669         new_action = g_strdup(action);  
670         prefs_actions_list_view_insert_action(actions.actions_list_view,
671                                               row, new_action, TRUE);
672                                                 
673         prefs_actions_set_list();
674
675         return 0;
676 }
677
678 /* callback functions */
679
680 static void prefs_actions_register_cb(GtkWidget *w, gpointer data)
681 {
682         prefs_actions_clist_set_row(-1);
683
684         modified = FALSE;
685         modified_list = TRUE;
686 }
687
688 static void prefs_actions_substitute_cb(GtkWidget *w, gpointer data)
689 {
690         gint row;
691
692         row = gtkut_list_view_get_selected_row(actions.actions_list_view);
693         if (row <= 0)
694                 return;
695
696         prefs_actions_clist_set_row(row);
697
698         modified = FALSE;
699         modified_list = TRUE;
700 }
701
702 static void prefs_actions_delete_cb(GtkWidget *w, gpointer data)
703 {
704         GtkTreeIter sel;
705         GtkTreeModel *model;
706         gchar *action;
707         gint row;
708
709         row = gtkut_list_view_get_selected_row(actions.actions_list_view);
710         if (row <= 0) 
711                 return; 
712
713         if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection
714                                 (GTK_TREE_VIEW(actions.actions_list_view)),
715                                 &model, &sel))
716                 return;                         
717
718         if (alertpanel(_("Delete action"),
719                        _("Do you really want to delete this action?"),
720                        GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL) != G_ALERTALTERNATE)
721                 return;
722
723         /* XXX: Here's the reason why we need to store the original 
724          * pointer: we search the slist for it. */
725         gtk_tree_model_get(model, &sel,
726                            PREFS_ACTIONS_DATA, &action,
727                            -1);
728         gtk_list_store_remove(GTK_LIST_STORE(model), &sel);
729
730         prefs_common.actions_list = g_slist_remove(prefs_common.actions_list,
731                                                    action);
732         modified_list = TRUE;
733 }
734
735 static void prefs_actions_delete_all_cb(GtkWidget *w, gpointer data)
736 {
737         GtkListStore *list_store;
738
739         if (alertpanel(_("Delete all actions"),
740                           _("Do you really want to delete all the actions?"),
741                           GTK_STOCK_CANCEL, "+"GTK_STOCK_DELETE, NULL) == G_ALERTDEFAULT)
742            return;
743
744         list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(actions.actions_list_view)));
745         prefs_actions_clear_list(list_store);
746         modified = FALSE;
747
748         prefs_actions_reset_dialog();
749         modified_list = TRUE;
750 }
751
752 static void prefs_actions_clear_cb(void)
753 {
754         gint row;
755
756         prefs_actions_reset_dialog();
757         row = gtkut_list_view_get_selected_row(actions.actions_list_view);
758         if (row < 1)
759                 modified = FALSE;
760         else
761                 modified = TRUE;
762 }
763
764 static void prefs_actions_duplicate_cb(GtkWidget *w, gpointer data)
765 {
766         gint row;
767         
768         row = gtkut_list_view_get_selected_row(actions.actions_list_view);
769         if (row <= 0)
770                 return;
771
772         modified_list = !prefs_actions_clist_set_row(-row-2);
773 }
774
775 static void prefs_actions_up(GtkWidget *w, gpointer data)
776 {
777         GtkTreePath *prev, *sel, *try;
778         GtkTreeIter isel;
779         GtkListStore *store = NULL;
780         GtkTreeModel *model = NULL;
781         GtkTreeIter iprev;
782         
783         if (!gtk_tree_selection_get_selected
784                 (gtk_tree_view_get_selection
785                         (GTK_TREE_VIEW(actions.actions_list_view)),
786                  &model,        
787                  &isel))
788                 return;
789         store = (GtkListStore *)model;
790         sel = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &isel);
791         if (!sel)
792                 return;
793         
794         /* no move if we're at row 0 or 1, looks phony, but other
795          * solutions are more convoluted... */
796         try = gtk_tree_path_copy(sel);
797         if (!gtk_tree_path_prev(try) || !gtk_tree_path_prev(try)) {
798                 gtk_tree_path_free(try);
799                 gtk_tree_path_free(sel);
800                 return;
801         }
802         gtk_tree_path_free(try);
803
804         prev = gtk_tree_path_copy(sel);         
805         if (!gtk_tree_path_prev(prev)) {
806                 gtk_tree_path_free(prev);
807                 gtk_tree_path_free(sel);
808                 return;
809         }
810
811         gtk_tree_model_get_iter(GTK_TREE_MODEL(store),
812                                 &iprev, prev);
813         gtk_tree_path_free(sel);
814         gtk_tree_path_free(prev);
815
816         gtk_list_store_swap(store, &iprev, &isel);
817         prefs_actions_set_list();
818         modified_list = TRUE;
819 }
820
821 static void prefs_actions_down(GtkWidget *w, gpointer data)
822 {
823         GtkListStore *store = NULL;
824         GtkTreeModel *model = NULL;
825         GtkTreeIter next, sel;
826         GtkTreePath *try;
827         
828         if (!gtk_tree_selection_get_selected
829                 (gtk_tree_view_get_selection
830                         (GTK_TREE_VIEW(actions.actions_list_view)),
831                  &model,
832                  &sel))
833                 return;
834         store = (GtkListStore *)model;
835         try = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &sel);
836         if (!try) 
837                 return;
838
839         /* no move when we're at row 0 */
840         if (!gtk_tree_path_prev(try)) {
841                 gtk_tree_path_free(try);
842                 return;
843         }
844         gtk_tree_path_free(try);
845
846         next = sel;
847         if (!gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &next)) 
848                 return;
849
850         gtk_list_store_swap(store, &next, &sel);
851         prefs_actions_set_list();
852         modified_list = TRUE;
853 }
854
855 static gint prefs_actions_deleted(GtkWidget *widget, GdkEventAny *event,
856                                   gpointer *data)
857 {
858         prefs_actions_cancel(widget, data);
859         return TRUE;
860 }
861
862 static gboolean prefs_actions_key_pressed(GtkWidget *widget, GdkEventKey *event,
863                                           gpointer data)
864 {
865         if (event && event->keyval == GDK_Escape)
866                 prefs_actions_cancel(widget, data);
867         else {
868                 GtkWidget *focused = gtkut_get_focused_child(
869                                         GTK_CONTAINER(widget));
870                 if (focused && GTK_IS_EDITABLE(focused)) {
871                         modified = TRUE;
872                 }
873         }
874         return FALSE;
875 }
876
877 static void prefs_actions_cancel(GtkWidget *w, gpointer data)
878 {
879         GtkListStore *store;
880
881         if (modified && alertpanel(_("Entry not saved"),
882                                  _("The entry was not saved. Close anyway?"),
883                                  GTK_STOCK_CLOSE, _("+_Continue editing"),
884                                  NULL) != G_ALERTDEFAULT) {
885                 return;
886         } else if (modified_list && alertpanel(_("Actions list not saved"),
887                                  _("The actions list has been modified. Close anyway?"),
888                                  GTK_STOCK_CLOSE, _("+_Continue editing"), 
889                                  NULL) != G_ALERTDEFAULT) {
890                 return;
891         }
892         modified = FALSE;
893         modified_list = FALSE;
894         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
895                                 (actions.actions_list_view)));
896         gtk_list_store_clear(store);
897         prefs_actions_read_config();
898         gtk_widget_hide(actions.window);
899         inc_unlock();
900 }
901
902 static void prefs_actions_ok(GtkWidget *widget, gpointer data)
903 {
904         MainWindow *mainwin = (MainWindow *) data;
905         GList *list;
906         GList *iter;
907         MessageView *msgview;
908         Compose *compose;
909         GtkListStore *store;
910
911         if (modified && alertpanel(_("Entry not saved"),
912                                  _("The entry was not saved. Close anyway?"),
913                                  GTK_STOCK_CLOSE, _("+_Continue editing"),
914                                  NULL) != G_ALERTDEFAULT) {
915                 return;
916         } 
917         modified = FALSE;
918         modified_list = FALSE;
919         prefs_actions_set_list();
920         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
921                                 (actions.actions_list_view)));
922         gtk_list_store_clear(store);
923         prefs_actions_write_config();
924
925         /* Update mainwindow actions menu */
926         main_window_update_actions_menu(mainwin);
927
928         /* Update separated message view actions menu */
929         list = messageview_get_msgview_list();
930         for (iter = list; iter; iter = iter->next) {
931                 msgview = (MessageView *) iter->data;
932                 messageview_update_actions_menu(msgview);
933         }
934
935         /* Update compose windows actions menu */
936         list = compose_get_compose_list();
937         for (iter = list; iter; iter = iter->next) {
938                 compose = (Compose *) iter->data;
939                 compose_update_actions_menu(compose);
940         }
941
942         gtk_widget_hide(actions.window);
943         inc_unlock();
944 }
945
946 /*
947  * Strings describing action format strings
948  * 
949  * When adding new lines, remember to put one string for each line
950  */
951 static gchar *actions_desc_strings[] = {
952         N_("<span weight=\"bold\" underline=\"single\">Menu name:</span>"), NULL,
953         N_("Use / in menu name to make submenus."), NULL,
954         "", NULL,
955         N_("<span weight=\"bold\" underline=\"single\">Command line:</span>"), NULL,
956         N_("<span weight=\"bold\">Begin with:</span>"), NULL,
957         "     |",   N_("to send message body or selection to command's standard input"),
958         "     &gt;",   N_("to send user provided text to command's standard input"),
959         "     *",   N_("to send user provided hidden text to command's standard input"),
960         N_("<span weight=\"bold\">End with:</span>"), NULL,
961         "     |",   N_("to replace message body or selection with command's standard output"),
962         "     &gt;",   N_("to insert command's standard output without replacing old text"),
963         "     &amp;",   N_("to run command asynchronously"),
964         N_("<span weight=\"bold\">Use:</span>"), NULL, 
965         "     %f",  N_("for the file of the selected message in RFC822/2822 format "),
966         "     %F",  N_("for the list of the files of the selected messages in RFC822/2822 format"),
967         "     %p",  N_("for the file of the selected decoded message MIME part"),
968         "     %u",  N_("for a user provided argument"),
969         "     %h",  N_("for a user provided hidden argument (e.g. password)"),
970         "     %s",  N_("for the text selection"),
971         "  %as{}",  N_("apply filtering actions between {} to selected messages"),
972         "     %%",  N_("for a literal %"),
973         NULL, NULL
974 };
975
976
977 static DescriptionWindow actions_desc_win = { 
978         NULL,
979         NULL,
980         2,
981         N_("Actions"),
982         N_("The Actions feature is a way for the user to launch "
983            "external commands to process a complete message file or just "
984            "one of its parts."),
985         actions_desc_strings
986 };
987
988
989 static void prefs_actions_info_cb(GtkWidget *w, GtkWidget *window)
990 {
991         actions_desc_win.parent = window;
992         description_window_create(&actions_desc_win);
993 }
994
995 static GtkListStore* prefs_actions_create_data_store(void)
996 {
997         return gtk_list_store_new(N_PREFS_ACTIONS_COLUMNS,
998                                   G_TYPE_STRING,        
999                                   G_TYPE_POINTER,
1000                                   G_TYPE_BOOLEAN,
1001                                   -1);
1002 }
1003
1004 static void prefs_actions_list_view_insert_action(GtkWidget *list_view,
1005                                                   gint row,
1006                                                   gchar *action,
1007                                                   gboolean is_valid) 
1008 {
1009         GtkTreeIter iter;
1010         GtkTreeIter sibling;
1011         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
1012                                         (GTK_TREE_VIEW(list_view)));
1013
1014 /*      row -1 to add a new rule to store,
1015         row >=0 to change an existing row
1016         row <-1 insert a new row after (-row-2)
1017 */
1018         if (row >= 0 ) {
1019                 /* modify the existing */
1020                 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
1021                                                    &iter, NULL, row))
1022                         row = -1;
1023         } else if (row < -1 ) {
1024                 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
1025                                                    &sibling, NULL, -row-2))
1026                         row = -1;               
1027         }
1028
1029         if (row == -1 ) {
1030                 /* append new */
1031                 gtk_list_store_append(list_store, &iter);
1032                 gtk_list_store_set(list_store, &iter,
1033                                    PREFS_ACTIONS_STRING, action,
1034                                    PREFS_ACTIONS_DATA, action,
1035                                    PREFS_ACTIONS_VALID,  is_valid,
1036                                    -1);
1037         } else if (row < -1) {
1038                 /* duplicate */
1039                 gtk_list_store_insert_after(list_store, &iter, &sibling);
1040                 gtk_list_store_set(list_store, &iter,
1041                                    PREFS_ACTIONS_STRING, action,
1042                                    PREFS_ACTIONS_DATA, action,
1043                                    PREFS_ACTIONS_VALID,  is_valid,
1044                                    -1);
1045         } else {
1046                 /* change existing */
1047                 gchar *old_action;
1048
1049                 gtk_tree_model_get(GTK_TREE_MODEL(list_store), &iter,
1050                                    PREFS_ACTIONS_DATA, &old_action,
1051                                    -1);
1052                 g_free(old_action);                             
1053
1054                 gtk_list_store_set(list_store, &iter,
1055                                    PREFS_ACTIONS_STRING, action,
1056                                    PREFS_ACTIONS_DATA, action,
1057                                    -1);
1058         }
1059 }
1060
1061 static GtkItemFactory *prefs_actions_popup_factory = NULL;
1062 static GtkWidget *prefs_actions_popup_menu = NULL;
1063
1064 static GtkItemFactoryEntry prefs_actions_popup_entries[] =
1065 {
1066    {N_("/_Delete"),             NULL, prefs_actions_delete_cb, 0, NULL, NULL},
1067    {N_("/Delete _all"), NULL, prefs_actions_delete_all_cb, 0, NULL, NULL},
1068    {N_("/D_uplicate"),  NULL, prefs_actions_duplicate_cb, 0, NULL, NULL},
1069 };
1070
1071 static gint prefs_actions_list_btn_pressed(GtkWidget *widget, GdkEventButton *event,
1072                                    GtkTreeView *list_view)
1073 {
1074    if (event) {
1075            /* left- or right-button click */
1076            if (event->button == 1 || event->button == 3) {
1077                    GtkTreePath *path = NULL;
1078                    if (gtk_tree_view_get_path_at_pos( list_view, event->x, event->y,
1079                                                            &path, NULL, NULL, NULL)) {
1080                            prefs_actions_select_row(list_view, path);
1081                    }
1082                    if (path)
1083                            gtk_tree_path_free(path);
1084            }
1085
1086            /* right-button click */
1087            if (event->button == 3) {
1088                    GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1089                    GtkTreeIter iter;
1090                    gboolean non_empty;
1091                    gint row;
1092
1093                    if (!prefs_actions_popup_menu) {
1094                            gint n_entries = sizeof(prefs_actions_popup_entries) /
1095                                            sizeof(prefs_actions_popup_entries[0]);
1096                            prefs_actions_popup_menu = menu_create_items(prefs_actions_popup_entries,
1097                                                                  n_entries, "<PrefsActionsPopupMenu>",
1098                                                                  &prefs_actions_popup_factory, list_view);
1099                    }
1100
1101                    /* grey out some popup menu items if there is no selected row */
1102                    row = gtkut_list_view_get_selected_row(GTK_WIDGET(list_view));
1103                    menu_set_sensitive(prefs_actions_popup_factory, "/Delete", (row > 0));
1104                    menu_set_sensitive(prefs_actions_popup_factory, "/Duplicate", (row > 0));
1105
1106                    /* grey out seom popup menu items if there is no row
1107                           (not counting the (New) one at row 0) */
1108                    non_empty = gtk_tree_model_get_iter_first(model, &iter);
1109                    if (non_empty)
1110                            non_empty = gtk_tree_model_iter_next(model, &iter);
1111                    menu_set_sensitive(prefs_actions_popup_factory, "/Delete all", non_empty);
1112
1113                    gtk_menu_popup(GTK_MENU(prefs_actions_popup_menu), 
1114                                           NULL, NULL, NULL, NULL, 
1115                                           event->button, event->time);
1116            }
1117    }
1118    return FALSE;
1119 }
1120
1121 static gboolean prefs_actions_list_popup_menu(GtkWidget *widget, gpointer data)
1122 {
1123    GtkTreeView *list_view = (GtkTreeView *)data;
1124    GdkEventButton event;
1125    
1126    event.button = 3;
1127    event.time = gtk_get_current_event_time();
1128    
1129    prefs_actions_list_btn_pressed(NULL, &event, list_view);
1130
1131    return TRUE;
1132 }
1133
1134 static GtkWidget *prefs_actions_list_view_create(void)
1135 {
1136         GtkTreeView *list_view;
1137         GtkTreeSelection *selector;
1138         GtkTreeModel *model;
1139
1140         model = GTK_TREE_MODEL(prefs_actions_create_data_store());
1141         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
1142         g_object_unref(model);  
1143         
1144 #ifndef MAEMO
1145         g_signal_connect(G_OBJECT(list_view), "popup-menu",
1146                          G_CALLBACK(prefs_actions_list_popup_menu), list_view);
1147 #else
1148         gtk_widget_tap_and_hold_setup(GTK_WIDGET(list_view), NULL, NULL,
1149                         GTK_TAP_AND_HOLD_NONE | GTK_TAP_AND_HOLD_NO_INTERNALS);
1150         g_signal_connect(G_OBJECT(list_view), "tap-and-hold",
1151                          G_CALLBACK(prefs_actions_list_popup_menu), list_view);
1152 #endif
1153         g_signal_connect(G_OBJECT(list_view), "button-press-event",
1154                         G_CALLBACK(prefs_actions_list_btn_pressed), list_view);
1155
1156         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
1157         gtk_tree_view_set_reorderable(list_view, TRUE);
1158
1159         selector = gtk_tree_view_get_selection(list_view);
1160         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1161
1162         /* create the columns */
1163         prefs_actions_create_list_view_columns(GTK_WIDGET(list_view));
1164
1165         return GTK_WIDGET(list_view);
1166 }
1167
1168 static void prefs_actions_create_list_view_columns(GtkWidget *list_view)
1169 {
1170         GtkTreeViewColumn *column;
1171         GtkCellRenderer *renderer;
1172
1173         renderer = gtk_cell_renderer_text_new();
1174         column = gtk_tree_view_column_new_with_attributes
1175                 (_("Current actions"),
1176                  renderer,
1177                  "text", PREFS_ACTIONS_STRING,
1178                  NULL);
1179         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
1180 }
1181
1182 #define ENTRY_SET_TEXT(entry, str) \
1183         gtk_entry_set_text(GTK_ENTRY(entry), str ? str : "")
1184
1185 static void prefs_actions_select_row(GtkTreeView *list_view, GtkTreePath *path)
1186 {
1187         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1188         GtkTreeSelection *selection;
1189         gchar *action;
1190         gchar *cmd;
1191         gchar buf[PREFSBUFSIZE];
1192         GtkTreeIter iter;
1193         gboolean is_valid;
1194
1195         if (!model || !path || !gtk_tree_model_get_iter(model, &iter, path))
1196                 return;
1197
1198         /* select row */
1199         selection = gtk_tree_view_get_selection(list_view);
1200         gtk_tree_selection_select_path(selection, path);
1201
1202         gtk_tree_model_get(model, &iter, 
1203                            PREFS_ACTIONS_VALID,  &is_valid,
1204                            PREFS_ACTIONS_DATA, &action,
1205                            -1);
1206         if (!is_valid) {
1207                 prefs_actions_reset_dialog();
1208                 return;
1209         }
1210         
1211         strncpy(buf, action, PREFSBUFSIZE - 1);
1212         buf[PREFSBUFSIZE - 1] = '\0';
1213         cmd = strstr(buf, ": ");
1214
1215         if (cmd && cmd[2])
1216                 ENTRY_SET_TEXT(actions.cmd_entry, &cmd[2]);
1217         else
1218                 return;
1219
1220         *cmd = '\0';
1221         ENTRY_SET_TEXT(actions.name_entry, buf);
1222
1223         if(g_str_has_prefix(&cmd[2], "%as{") == TRUE)
1224                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(
1225                                                 actions.filter_radiobtn), TRUE);
1226         else
1227                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(
1228                                                 actions.shell_radiobtn), TRUE);
1229
1230         return;
1231 }
1232
1233 static void prefs_action_filter_radiobtn_cb(GtkWidget *widget, gpointer data)
1234 {
1235         if(actions.filter_btn)
1236                 gtk_widget_set_sensitive(actions.filter_btn, TRUE);
1237         if(actions.cmd_entry)
1238                 gtk_widget_set_sensitive(actions.cmd_entry,FALSE);
1239         if(actions.info_btn)
1240                 gtk_widget_set_sensitive(actions.info_btn,FALSE);
1241 }
1242
1243 static void prefs_action_shell_radiobtn_cb(GtkWidget *widget, gpointer data)
1244 {
1245         if(actions.filter_btn)
1246                 gtk_widget_set_sensitive(actions.filter_btn, FALSE);
1247         if(actions.cmd_entry)
1248                 gtk_widget_set_sensitive(actions.cmd_entry,TRUE);
1249         if(actions.info_btn)
1250                 gtk_widget_set_sensitive(actions.info_btn,TRUE);
1251 }
1252
1253 static void prefs_action_filterbtn_cb(GtkWidget *widget, gpointer data)
1254 {
1255         gchar *action_str, **tokens;
1256         GSList *action_list = NULL, *cur;
1257
1258         if(modified && alertpanel(_("Entry was modified"),
1259                         _("Opening the filter action dialog will clear current modifications "
1260                         "of the command line."),
1261                         GTK_STOCK_CANCEL, _("+_Continue editing"), NULL) == G_ALERTDEFAULT)
1262                 return;
1263
1264         action_str = gtk_editable_get_chars(GTK_EDITABLE(actions.cmd_entry), 0, -1);
1265         tokens = g_strsplit_set(action_str, "{}", 5);
1266
1267         if (tokens[1] && *tokens[1] != '\0') {
1268                 action_list = matcher_parser_get_action_list(tokens[1]);
1269                 if (action_list == NULL)
1270                         alertpanel_error(_("Action string is not valid."));
1271         }
1272                 
1273         prefs_filtering_action_open(action_list, prefs_action_define_filter_done);
1274
1275         if (action_list != NULL) {
1276                 for(cur = action_list ; cur != NULL ; cur = cur->next)
1277                         filteringaction_free(cur->data);
1278         }
1279         
1280         g_free(action_str);
1281         g_strfreev(tokens);
1282 }
1283
1284 static void prefs_action_define_filter_done(GSList * action_list)
1285 {
1286         gchar *str;
1287
1288         if(action_list == NULL)
1289                 return;
1290
1291         str = filteringaction_list_to_string(action_list);
1292
1293         if (str != NULL) {
1294                 gchar *cmd;
1295                 cmd = g_strdup_printf("%%as{%s}",str);
1296                 g_free(str);
1297                 gtk_entry_set_text(GTK_ENTRY(actions.cmd_entry), cmd);
1298                 g_free(cmd);
1299                 modified = TRUE;
1300         }
1301 }