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