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