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