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