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