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