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