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