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