8990dea946cfc7a34e5cad96b39484abf11cad28
[claws.git] / src / prefs_template.c
1 /*
2  * Sylpheed templates subsystem 
3  * Copyright (C) 2001 Alexander Barinov
4  * Copyright (C) 2001-2007 Hiroyuki Yamamoto and the Claws Mail team
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  * 
19  */
20
21 #include "defs.h"
22
23 #include <glib.h>
24 #include <glib/gi18n.h>
25 #include <gtk/gtk.h>
26 #include <gdk/gdkkeysyms.h>
27 #include <string.h>
28 #include <dirent.h>
29 #include <sys/stat.h>
30
31 #include "template.h"
32 #include "main.h"
33 #include "prefs_gtk.h"
34 #include "inc.h"
35 #include "utils.h"
36 #include "gtkutils.h"
37 #include "alertpanel.h"
38 #include "manage_window.h"
39 #include "compose.h"
40 #include "addr_compl.h"
41 #include "quote_fmt.h"
42 #include "prefs_common.h"
43 #include "manual.h"
44 #include "gtkutils.h"
45 #include "menu.h"
46
47 enum {
48         TEMPL_TEXT,
49         TEMPL_DATA,
50         TEMPL_AUTO_DATA,        /*!< auto pointer */
51         N_TEMPL_COLUMNS
52 };
53
54 static struct Templates {
55         GtkWidget *window;
56         GtkWidget *ok_btn;
57         GtkWidget *list_view;
58         GtkWidget *entry_name;
59         GtkWidget *entry_subject;
60         GtkWidget *entry_to;
61         GtkWidget *entry_cc;    
62         GtkWidget *entry_bcc;
63         GtkWidget *text_value;
64 } templates;
65
66 static int modified = FALSE;
67 static int modified_list = FALSE;
68 /* widget creating functions */
69 static void prefs_template_window_create        (void);
70 static void prefs_template_window_setup         (void);
71
72 static GSList *prefs_template_get_list          (void);
73
74 /* callbacks */
75 static gint prefs_template_deleted_cb           (GtkWidget      *widget,
76                                                  GdkEventAny    *event,
77                                                  gpointer        data);
78 static gboolean prefs_template_key_pressed_cb   (GtkWidget      *widget,
79                                                  GdkEventKey    *event,
80                                                  gpointer        data);
81 static void prefs_template_cancel_cb            (void);
82 static void prefs_template_ok_cb                (void);
83 static void prefs_template_register_cb          (void);
84 static void prefs_template_substitute_cb        (void);
85 static void prefs_template_delete_cb            (void);
86 static void prefs_template_delete_all_cb        (void);
87 static void prefs_template_clear_cb             (void);
88 static void prefs_template_duplicate_cb (void);
89 static void prefs_template_top_cb               (void);
90 static void prefs_template_up_cb                (void);
91 static void prefs_template_down_cb      (void);
92 static void prefs_template_bottom_cb    (void);
93
94 static GtkListStore* prefs_template_create_data_store   (void);
95 static void prefs_template_list_view_insert_template    (GtkWidget *list_view,
96                                                          gint row,
97                                                          const gchar *template,
98                                                          Template *data);
99 static GtkWidget *prefs_template_list_view_create       (void);
100 static void prefs_template_create_list_view_columns     (GtkWidget *list_view);
101 static void prefs_template_select_row(GtkTreeView *list_view, GtkTreePath *path);
102
103 /* Called from mainwindow.c */
104 void prefs_template_open(void)
105 {
106         inc_lock();
107
108         if (!templates.window)
109                 prefs_template_window_create();
110
111         prefs_template_window_setup();
112         gtk_widget_show(templates.window);
113 }
114
115 /*!
116  *\brief        Save Gtk object size to prefs dataset
117  */
118 static void prefs_template_size_allocate_cb(GtkWidget *widget,
119                                          GtkAllocation *allocation)
120 {
121         g_return_if_fail(allocation != NULL);
122
123         prefs_common.templateswin_width = allocation->width;
124         prefs_common.templateswin_height = allocation->height;
125 }
126
127 static void prefs_template_window_create(void)
128 {
129         /* window structure ;) */
130         GtkWidget *window;
131         GtkWidget *vbox;
132         GtkWidget *scrolled_window;
133         GtkWidget   *vpaned;
134         GtkWidget     *vbox1;
135         GtkWidget       *table; /* including : entry_[name|to|cc|bcc|subject] */
136         GtkWidget       *scroll2;
137         GtkWidget         *text_value;
138         GtkWidget     *vbox2;
139         GtkWidget       *hbox2;
140         GtkWidget         *arrow1;
141         GtkWidget         *hbox3;
142         GtkWidget           *reg_btn;
143         GtkWidget           *subst_btn;
144         GtkWidget           *del_btn;
145         GtkWidget           *clear_btn;
146         GtkWidget         *desc_btn;
147         GtkWidget       *hbox4;
148         GtkWidget         *scroll1;
149         GtkWidget           *list_view;
150         GtkWidget         *vbox3;
151         GtkWidget           *spc_vbox;
152         GtkWidget           *top_btn;
153         GtkWidget           *up_btn;
154         GtkWidget           *down_btn;
155         GtkWidget           *bottom_btn;
156         GtkWidget       *confirm_area;
157         GtkWidget         *help_btn;
158         GtkWidget         *cancel_btn;
159         GtkWidget         *ok_btn;
160         static GdkGeometry geometry;
161         gint i;
162         GtkTooltips *tooltips;
163
164         debug_print("Creating templates configuration window...\n");
165
166         /* main window */
167         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "prefs_template");
168         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
169         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
170         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
171
172         vbox = gtk_vbox_new(FALSE, 8);
173         gtk_widget_show(vbox);
174         gtk_container_add(GTK_CONTAINER(window), vbox);
175         gtk_container_set_border_width(GTK_CONTAINER(vbox), 4);
176
177         scrolled_window = gtk_scrolled_window_new (NULL, NULL);
178         gtk_widget_show(scrolled_window);
179         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
180                                         GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
181         gtk_container_add(GTK_CONTAINER(vbox), scrolled_window);
182
183         /* vpaned to separate template settings from templates list */
184         vpaned = gtk_vpaned_new();
185         gtk_widget_show(vpaned);
186         gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled_window),
187                                               vpaned);
188
189         /* vbox to handle template name and content */
190         vbox1 = gtk_vbox_new(FALSE, 6);
191         gtk_widget_show(vbox1);
192         gtk_container_set_border_width(GTK_CONTAINER(vbox1), 8);
193         gtk_paned_pack1(GTK_PANED(vpaned), vbox1, FALSE, FALSE);
194
195         table = gtk_table_new(5, 2, FALSE);
196         gtk_table_set_row_spacings (GTK_TABLE (table), VSPACING_NARROW_2);
197         gtk_table_set_col_spacings (GTK_TABLE (table), 4);
198         gtk_widget_show(table);
199         gtk_box_pack_start (GTK_BOX (vbox1), table, FALSE, FALSE, 0);
200
201         tooltips = gtk_tooltips_new();
202
203         struct
204         {
205                 gchar *label;
206                 GtkWidget **entry;
207                 gboolean compl;
208                 gchar *tooltips;
209         } tab[] = {
210                 {_("Name"),     &templates.entry_name,          FALSE,
211                         _("This name is used as the Menu item")},
212                 {_("To"),       &templates.entry_to,            TRUE,   NULL},
213                 {_("Cc"),       &templates.entry_cc,            TRUE,   NULL},
214                 {_("Bcc"),      &templates.entry_bcc,           TRUE,   NULL},
215                 {_("Subject"),  &templates.entry_subject,       FALSE,  NULL},
216                 {NULL,          NULL,                           FALSE,  NULL}
217         };
218
219         for (i=0; tab[i].label; i++) {
220
221                 GtkWidget *label;
222
223                 label = gtk_label_new(tab[i].label);
224                 gtk_widget_show(label);
225                 gtk_table_attach(GTK_TABLE(table), label, 0, 1, i, (i + 1),
226                                 (GtkAttachOptions) (GTK_FILL),
227                                 (GtkAttachOptions) 0, 0, 0);
228                 gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
229
230                 *(tab[i].entry) = gtk_entry_new();
231                 gtk_widget_show(*(tab[i].entry));
232                 gtk_table_attach(GTK_TABLE(table), *(tab[i].entry), 1, 2, i, (i + 1),
233                                 (GtkAttachOptions) (GTK_EXPAND|GTK_SHRINK|GTK_FILL),
234                                 (GtkAttachOptions) 0, 0, 0);
235                 gtk_tooltips_set_tip(tooltips, *(tab[i].entry), tab[i].tooltips, NULL);
236
237                 if (tab[i].compl)
238                         address_completion_register_entry(
239                                 GTK_ENTRY(*(tab[i].entry)), TRUE);
240         }
241
242         /* template content */
243         scroll2 = gtk_scrolled_window_new(NULL, NULL);
244         gtk_widget_show(scroll2);
245         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll2),
246                                        GTK_POLICY_AUTOMATIC,
247                                        GTK_POLICY_AUTOMATIC);
248         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll2),
249                                             GTK_SHADOW_IN);
250         gtk_box_pack_start(GTK_BOX(vbox1), scroll2, TRUE, TRUE, 0);
251
252         text_value = gtk_text_view_new();
253         if (prefs_common.textfont) {
254                 PangoFontDescription *font_desc;
255
256                 font_desc = pango_font_description_from_string
257                                                 (prefs_common.textfont);
258                 if (font_desc) {
259                         gtk_widget_modify_font(text_value, font_desc);
260                         pango_font_description_free(font_desc);
261                 }
262         }
263         gtk_widget_show(text_value);
264 #ifndef MAEMO
265         gtk_widget_set_size_request(text_value, -1, 120);
266 #else
267         gtk_widget_set_size_request(text_value, -1, 60);
268 #endif
269         gtk_container_add(GTK_CONTAINER(scroll2), text_value);
270         gtk_text_view_set_editable(GTK_TEXT_VIEW(text_value), TRUE);
271         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text_value), GTK_WRAP_WORD);
272
273         /* vbox for buttons and templates list */
274         vbox2 = gtk_vbox_new(FALSE, 6);
275         gtk_widget_show(vbox2);
276         gtk_container_set_border_width(GTK_CONTAINER(vbox2), 8);
277         gtk_paned_pack2(GTK_PANED(vpaned), vbox2, TRUE, FALSE);
278
279         /* register | substitute | delete */
280         hbox2 = gtk_hbox_new(FALSE, 4);
281         gtk_widget_show(hbox2);
282         gtk_box_pack_start(GTK_BOX(vbox2), hbox2, FALSE, FALSE, 0);
283
284         arrow1 = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
285         gtk_widget_show(arrow1);
286         gtk_box_pack_start(GTK_BOX(hbox2), arrow1, FALSE, FALSE, 0);
287         gtk_widget_set_size_request(arrow1, -1, 16);
288
289         hbox3 = gtk_hbox_new(TRUE, 4);
290         gtk_widget_show(hbox3);
291         gtk_box_pack_start(GTK_BOX(hbox2), hbox3, FALSE, FALSE, 0);
292
293         reg_btn = gtk_button_new_from_stock(GTK_STOCK_ADD);
294         gtk_widget_show(reg_btn);
295         gtk_box_pack_start(GTK_BOX(hbox3), reg_btn, FALSE, TRUE, 0);
296         g_signal_connect(G_OBJECT (reg_btn), "clicked",
297                          G_CALLBACK (prefs_template_register_cb), NULL);
298         gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), reg_btn,
299                         _("Append new template to list, as defined above"), NULL);
300
301         subst_btn = gtkut_get_replace_btn(_("Replace"));
302         gtk_widget_show(subst_btn);
303         gtk_box_pack_start(GTK_BOX(hbox3), subst_btn, FALSE, TRUE, 0);
304         g_signal_connect(G_OBJECT(subst_btn), "clicked",
305                          G_CALLBACK(prefs_template_substitute_cb),
306                          NULL);
307         gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), subst_btn,
308                         _("Replace selected template in list with the above data"), NULL);
309
310         del_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
311         gtk_widget_show(del_btn);
312         gtk_box_pack_start(GTK_BOX(hbox3), del_btn, FALSE, TRUE, 0);
313         g_signal_connect(G_OBJECT(del_btn), "clicked",
314                          G_CALLBACK(prefs_template_delete_cb), NULL);
315         gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), del_btn,
316                         _("Delete selected template from list"), NULL);
317
318         clear_btn = gtk_button_new_from_stock (GTK_STOCK_CLEAR);
319         gtk_widget_show (clear_btn);
320         gtk_box_pack_start (GTK_BOX (hbox3), clear_btn, FALSE, TRUE, 0);
321         g_signal_connect(G_OBJECT (clear_btn), "clicked",
322                         G_CALLBACK(prefs_template_clear_cb), NULL);
323         gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), clear_btn,
324                         _("Clear all input fields in dialog"), NULL);
325
326 #if GTK_CHECK_VERSION(2, 8, 0)
327         desc_btn = gtk_button_new_from_stock(GTK_STOCK_INFO);
328 #else
329         desc_btn = gtk_button_new_with_label(_(" Symbols... "));
330 #endif
331         gtk_widget_show(desc_btn);
332         gtk_box_pack_end(GTK_BOX(hbox2), desc_btn, FALSE, FALSE, 0);
333         g_signal_connect(G_OBJECT(desc_btn), "clicked",
334                          G_CALLBACK(quote_fmt_quote_description), NULL);
335         gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), desc_btn,
336                         _("Click here to get further information "
337                           "about templates format syntax"), NULL);
338
339         /* templates list */
340         hbox4 = gtk_hbox_new(FALSE, 8);
341         gtk_widget_show(hbox4);
342         gtk_box_pack_start(GTK_BOX(vbox2), hbox4, TRUE, TRUE, 0);
343
344         scroll1 = gtk_scrolled_window_new(NULL, NULL);
345         gtk_widget_show(scroll1);
346         gtk_box_pack_start(GTK_BOX(hbox4), scroll1, TRUE, TRUE, 0);
347         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll1),
348                                        GTK_POLICY_AUTOMATIC,
349                                        GTK_POLICY_AUTOMATIC);
350                                        
351         vbox3 = gtk_vbox_new(FALSE, 8);
352         gtk_widget_show(vbox3);
353         gtk_box_pack_start(GTK_BOX(hbox4), vbox3, FALSE, FALSE, 0);
354
355         top_btn = gtk_button_new_from_stock(GTK_STOCK_GOTO_TOP);
356         gtk_widget_show(top_btn);
357         gtk_box_pack_start(GTK_BOX(vbox3), top_btn, FALSE, FALSE, 0);
358         g_signal_connect(G_OBJECT(top_btn), "clicked",
359                          G_CALLBACK(prefs_template_top_cb), NULL);
360         gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), top_btn,
361                         _("Move selected template to top of list"), NULL);
362
363         PACK_VSPACER(vbox3, spc_vbox, VSPACING_NARROW_2);
364
365         up_btn = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
366         gtk_widget_show(up_btn);
367         gtk_box_pack_start (GTK_BOX(vbox3), up_btn, FALSE, FALSE, 0);
368         g_signal_connect(G_OBJECT(up_btn), "clicked",
369                          G_CALLBACK(prefs_template_up_cb), NULL);
370         gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), up_btn,
371                         _("Move up selected template in list"), NULL);
372
373         down_btn = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
374         gtk_widget_show (down_btn);
375         gtk_box_pack_start(GTK_BOX (vbox3), down_btn, FALSE, FALSE, 0);
376         g_signal_connect(G_OBJECT (down_btn), "clicked",
377                          G_CALLBACK(prefs_template_down_cb), NULL);
378         gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), down_btn,
379                         _("Move down selected template in list"), NULL);
380
381         PACK_VSPACER(vbox3, spc_vbox, VSPACING_NARROW_2);
382
383         bottom_btn = gtk_button_new_from_stock(GTK_STOCK_GOTO_BOTTOM);
384         gtk_widget_show(bottom_btn);
385         gtk_box_pack_start(GTK_BOX(vbox3), bottom_btn, FALSE, FALSE, 0);
386         g_signal_connect(G_OBJECT(bottom_btn), "clicked",
387                          G_CALLBACK(prefs_template_bottom_cb), NULL);
388         gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), bottom_btn,
389                         _("Move selected template to bottom of list"), NULL);
390
391         list_view = prefs_template_list_view_create();
392         gtk_widget_show(list_view);
393         gtk_widget_set_size_request(scroll1, -1, 140);
394         gtk_container_add(GTK_CONTAINER(scroll1), list_view);
395
396         /* help | cancel | ok */
397         gtkut_stock_button_set_create_with_help(&confirm_area, &help_btn,
398                         &cancel_btn, GTK_STOCK_CANCEL,
399                         &ok_btn, GTK_STOCK_OK,
400                         NULL, NULL);
401         gtk_widget_show(confirm_area);
402         gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
403         gtk_widget_grab_default(ok_btn);
404
405         gtk_window_set_title(GTK_WINDOW(window), _("Template configuration"));
406
407         g_signal_connect(G_OBJECT(window), "delete_event",
408                          G_CALLBACK(prefs_template_deleted_cb), NULL);
409         g_signal_connect(G_OBJECT(window), "size_allocate",
410                          G_CALLBACK(prefs_template_size_allocate_cb), NULL);
411         g_signal_connect(G_OBJECT(window), "key_press_event",
412                          G_CALLBACK(prefs_template_key_pressed_cb), NULL);
413         MANAGE_WINDOW_SIGNALS_CONNECT(window);
414         g_signal_connect(G_OBJECT(ok_btn), "clicked",
415                          G_CALLBACK(prefs_template_ok_cb), NULL);
416         g_signal_connect(G_OBJECT(cancel_btn), "clicked",
417                          G_CALLBACK(prefs_template_cancel_cb), NULL);
418         g_signal_connect(G_OBJECT(help_btn), "clicked",
419                          G_CALLBACK(manual_open_with_anchor_cb),
420                          MANUAL_ANCHOR_TEMPLATES);
421
422         address_completion_start(window);
423
424         if (!geometry.min_height) {
425                 geometry.min_width = 500;
426                 geometry.min_height = 540;
427         }
428
429         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
430                                       GDK_HINT_MIN_SIZE);
431         gtk_widget_set_size_request(window, prefs_common.templateswin_width,
432                                     prefs_common.templateswin_height);
433
434         templates.window = window;
435         templates.ok_btn = ok_btn;
436         templates.list_view = list_view;
437         templates.text_value = text_value;
438 }
439
440 static void prefs_template_reset_dialog(void)
441 {
442         GtkTextBuffer *buffer;
443
444         gtk_entry_set_text(GTK_ENTRY(templates.entry_name), "");
445         gtk_entry_set_text(GTK_ENTRY(templates.entry_to), "");
446         gtk_entry_set_text(GTK_ENTRY(templates.entry_cc), "");
447         gtk_entry_set_text(GTK_ENTRY(templates.entry_bcc), "");                 
448         gtk_entry_set_text(GTK_ENTRY(templates.entry_subject), "");
449         
450         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
451         gtk_text_buffer_set_text(buffer, "", -1);
452 }
453
454 static void prefs_template_clear_list(void)
455 {
456         GtkListStore *store;
457         
458         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
459                                 (templates.list_view)));
460         gtk_list_store_clear(store);
461
462         prefs_template_list_view_insert_template(templates.list_view,
463                                                  -1, _("(New)"),
464                                                  NULL);
465 }
466
467 static void prefs_template_window_setup(void)
468 {
469         GSList *tmpl_list;
470         GSList *cur;
471         Template *tmpl;
472
473         manage_window_set_transient(GTK_WINDOW(templates.window));
474         gtk_widget_grab_focus(templates.ok_btn);
475
476         prefs_template_clear_list();
477         
478         tmpl_list = template_read_config();
479
480         for (cur = tmpl_list; cur != NULL; cur = cur->next) {
481                 tmpl = (Template *)cur->data;
482                 prefs_template_list_view_insert_template(templates.list_view,
483                                                          -1, tmpl->name, 
484                                                          tmpl);
485         }
486
487         prefs_template_reset_dialog();
488
489         g_slist_free(tmpl_list);
490 }
491
492 static gint prefs_template_deleted_cb(GtkWidget *widget, GdkEventAny *event,
493                                       gpointer data)
494 {
495         prefs_template_cancel_cb();
496         return TRUE;
497 }
498
499 static gboolean prefs_template_key_pressed_cb(GtkWidget *widget,
500                                               GdkEventKey *event, gpointer data)
501 {
502         if (event && event->keyval == GDK_Escape)
503                 prefs_template_cancel_cb();
504         else {
505                 GtkWidget *focused = gtkut_get_focused_child(
506                                         GTK_CONTAINER(widget));
507                 if (focused && GTK_IS_EDITABLE(focused)) {
508                         modified = TRUE;
509                 }
510         }
511         return FALSE;
512 }
513
514 static void prefs_template_ok_cb(void)
515 {
516         GSList *tmpl_list;
517         GtkListStore *store;
518
519         if (modified && alertpanel(_("Entry not saved"),
520                                  _("The entry was not saved. Close anyway?"),
521                                  GTK_STOCK_CLOSE, _("+_Continue editing"), 
522                                  NULL) != G_ALERTDEFAULT) {
523                 return;
524         } 
525         modified = FALSE;
526         modified_list = FALSE;
527         tmpl_list = prefs_template_get_list();
528         template_set_config(tmpl_list);
529         compose_reflect_prefs_all();
530         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
531                                 (templates.list_view)));
532         gtk_list_store_clear(store);
533         gtk_widget_hide(templates.window);
534         inc_unlock();
535 }
536
537 static void prefs_template_cancel_cb(void)
538 {
539         GtkListStore *store;
540
541         if (modified && alertpanel(_("Entry not saved"),
542                                  _("The entry was not saved. Close anyway?"),
543                                  GTK_STOCK_CLOSE, _("+_Continue editing"),
544                                  NULL) != G_ALERTDEFAULT) {
545                 return;
546         } else if (modified_list && alertpanel(_("Templates list not saved"),
547                                  _("The templates list has been modified. Close anyway?"),
548                                  GTK_STOCK_CLOSE, _("+_Continue editing"), 
549                                  NULL) != G_ALERTDEFAULT) {
550                 return;
551         }
552         modified = FALSE;
553         modified_list = FALSE;
554         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
555                                 (templates.list_view)));
556         gtk_list_store_clear(store);
557         gtk_widget_hide(templates.window);
558         inc_unlock();
559 }
560
561 /*!
562  *\brief        Request list for storage. New list is owned
563  *              by template.c...
564  */
565 static GSList *prefs_template_get_list(void)
566 {
567         GSList *tmpl_list = NULL;
568         Template *tmpl;
569         GtkTreeModel *model;
570         GtkTreeIter iter;
571
572         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
573         if (!gtk_tree_model_get_iter_first(model, &iter))
574                 return NULL;
575
576         do {
577                 gtk_tree_model_get(model, &iter,
578                                    TEMPL_DATA, &tmpl,
579                                    -1);
580                 
581                 if (tmpl) {
582                         Template *ntmpl;
583                         
584                         ntmpl = g_new(Template, 1);
585                         ntmpl->load_filename = NULL;
586                         ntmpl->name    = tmpl->name && *(tmpl->name) 
587                                          ? g_strdup(tmpl->name) 
588                                          : NULL;
589                         ntmpl->subject = tmpl->subject && *(tmpl->subject) 
590                                          ? g_strdup(tmpl->subject) 
591                                          : NULL;
592                         ntmpl->to      = tmpl->to && *(tmpl->to)
593                                          ? g_strdup(tmpl->to)
594                                          : NULL;
595                         ntmpl->cc      = tmpl->cc && *(tmpl->cc)
596                                          ? g_strdup(tmpl->cc)
597                                          : NULL;
598                         ntmpl->bcc     = tmpl->bcc && *(tmpl->bcc)
599                                          ? g_strdup(tmpl->bcc)
600                                          : NULL;        
601                         ntmpl->value   = tmpl->value && *(tmpl->value)
602                                          ? g_strdup(tmpl->value)
603                                          : NULL;
604                         tmpl_list = g_slist_append(tmpl_list, ntmpl);
605                 }                       
606         
607         } while (gtk_tree_model_iter_next(model, &iter)); 
608
609         return tmpl_list;
610 }
611
612 gboolean prefs_template_string_is_valid(gchar *string, gint *line)
613 {
614         if (string && *string != '\0') {
615                 gchar *parsed_buf;
616                 MsgInfo dummyinfo;
617
618                 memset(&dummyinfo, 0, sizeof(MsgInfo));
619 #ifdef USE_ASPELL
620                 quote_fmt_init(&dummyinfo, NULL, NULL, TRUE, NULL, NULL);
621 #else
622                 quote_fmt_init(&dummyinfo, NULL, NULL, TRUE, NULL);
623 #endif
624                 quote_fmt_scan_string(string);
625                 quote_fmt_parse();
626                 parsed_buf = quote_fmt_get_buffer();
627                 if (!parsed_buf) {
628                         if (line)
629                                 *line = quote_fmt_get_line();
630                         return FALSE;
631                 }
632                 quote_fmt_reset_vartable();
633         }
634         return TRUE;
635 }
636
637 static gboolean prefs_template_list_view_set_row(gint row)
638 /* return TRUE if the row could be modified */
639 {
640         Template *tmpl;
641         gchar *name;
642         gchar *subject;
643         gchar *to;
644         gchar *cc;
645         gchar *bcc;     
646         gchar *value;
647         GtkTextBuffer *buffer;
648         GtkTextIter start, end;
649         GtkTreeModel *model;
650         gint line;
651
652         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
653
654         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
655         gtk_text_buffer_get_start_iter(buffer, &start);
656         gtk_text_buffer_get_iter_at_offset(buffer, &end, -1);
657         value = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
658
659         if (value && *value == '\0') {
660                         g_free(value);
661                 value = NULL;
662                 }
663         if (!prefs_template_string_is_valid(value, &line)) {
664                 alertpanel_error(_("Template body format error at line %d."), line);
665                 g_free(value);
666                 return FALSE;
667         }
668
669         name = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_name),
670                                       0, -1);
671         if (*name == '\0') {
672                 alertpanel_error(_("Template name is not set."));
673                 g_free(value);
674                 return FALSE;
675         }
676         to = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_to),
677                                     0, -1);
678         cc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_cc),
679                                     0, -1);
680         bcc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_bcc),
681                                     0, -1);
682         subject = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_subject),
683                                          0, -1);
684
685         if (to && *to == '\0') {
686                 g_free(to);
687                 to = NULL;
688         }
689         if (cc && *cc == '\0') {
690                 g_free(cc);
691                 cc = NULL;
692         }
693         if (bcc && *bcc == '\0') {
694                 g_free(bcc);
695                 bcc = NULL;
696         }
697         if (subject && *subject == '\0') {
698                 g_free(subject);
699                 subject = NULL;
700         }
701
702         if (!prefs_template_string_is_valid(to, NULL)) {
703                 alertpanel_error(_("Template To format error."));
704                 g_free(to);
705                 g_free(value);
706                 return FALSE;
707         }
708         if (!prefs_template_string_is_valid(cc, NULL)) {
709                 alertpanel_error(_("Template Cc format error."));       
710                 g_free(cc);
711                 g_free(value);
712                 return FALSE;
713         }
714         if (!prefs_template_string_is_valid(bcc, NULL)) {
715                 alertpanel_error(_("Template Bcc format error."));      
716                 g_free(bcc);
717                 g_free(value);
718                 return FALSE;
719         }
720         if (!prefs_template_string_is_valid(subject, NULL)) {
721                 alertpanel_error(_("Template subject format error."));  
722                 g_free(subject);
723                 g_free(value);
724                 return FALSE;
725         }
726         
727         tmpl = g_new(Template, 1);
728         tmpl->load_filename = NULL;
729         tmpl->name = name;
730         tmpl->subject = subject;
731         tmpl->to = to;
732         tmpl->cc = cc;
733         tmpl->bcc = bcc;        
734         tmpl->value = value;
735
736         prefs_template_list_view_insert_template(templates.list_view,
737                                                  row, tmpl->name, tmpl);
738
739         return TRUE;
740 }
741
742 static void prefs_template_register_cb(void)
743 {
744         modified = !prefs_template_list_view_set_row(-1);
745         modified_list = TRUE;
746 }
747
748 static void prefs_template_substitute_cb(void)
749 {
750         Template *tmpl;
751         gint row;
752         GtkTreeIter iter;
753         GtkTreeModel *model;
754
755         row = gtkut_list_view_get_selected_row(templates.list_view);
756         if (row <= 0)
757                 return;
758
759         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
760         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
761                 return;
762
763         gtk_tree_model_get(model, &iter, TEMPL_DATA, &tmpl, -1);
764         if (!tmpl)
765                 return;
766
767         modified = !prefs_template_list_view_set_row(row);
768         modified_list = TRUE;
769 }
770
771 static void prefs_template_delete_cb(void)
772 {
773         Template *tmpl;
774         gint row;
775         GtkTreeIter iter;
776         GtkTreeModel *model;
777
778         row = gtkut_list_view_get_selected_row(templates.list_view);
779         if (row <= 0)
780                 return;
781
782         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
783         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
784                 return;
785
786         gtk_tree_model_get(model, &iter, TEMPL_DATA, &tmpl, -1);
787         if (!tmpl)
788                 return;
789
790         if (alertpanel(_("Delete template"),
791                        _("Do you really want to delete this template?"),
792                        GTK_STOCK_CANCEL, GTK_STOCK_DELETE,
793                        NULL) != G_ALERTALTERNATE)
794                 return;
795
796         gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
797         modified_list = TRUE;   
798 }
799
800 static void prefs_template_delete_all_cb(void)
801 {
802         GtkListStore *list_store;
803
804         if (alertpanel(_("Delete all templates"),
805                           _("Do you really want to delete all templates?"),
806                           GTK_STOCK_CANCEL, "+"GTK_STOCK_DELETE, NULL) == G_ALERTDEFAULT)
807            return;
808
809         list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view)));
810         prefs_template_clear_list();
811         modified = FALSE;
812
813         prefs_template_reset_dialog();
814         modified_list = TRUE;
815 }
816
817 static void prefs_template_duplicate_cb(void)
818 {
819         Template *tmpl;
820         gint row;
821         GtkTreeIter iter;
822         GtkTreeModel *model;
823         
824         row = gtkut_list_view_get_selected_row(templates.list_view);
825         if (row <= 0)
826                 return;
827
828         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
829         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
830                 return;
831
832         gtk_tree_model_get(model, &iter, TEMPL_DATA, &tmpl, -1);
833         if (!tmpl)
834                 return;
835
836         modified_list = !prefs_template_list_view_set_row(-row-2);
837 }
838
839 static void prefs_template_clear_cb(void)
840 {
841    prefs_template_reset_dialog();
842 }
843
844 static void prefs_template_top_cb(void)
845 {
846         gint row;
847         GtkTreeIter top, sel;
848         GtkTreeModel *model;
849
850         row = gtkut_list_view_get_selected_row(templates.list_view);
851         if (row <= 1) 
852                 return;
853
854         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));            
855         
856         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, 0)
857         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
858                 return;
859
860         gtk_list_store_move_after(GTK_LIST_STORE(model), &sel, &top);
861         gtkut_list_view_select_row(templates.list_view, 1);
862         modified_list = TRUE;
863 }
864
865 static void prefs_template_up_cb(void)
866 {
867         gint row;
868         GtkTreeIter top, sel;
869         GtkTreeModel *model;
870
871         row = gtkut_list_view_get_selected_row(templates.list_view);
872         if (row <= 1) 
873                 return;
874                 
875         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));    
876
877         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row - 1)
878         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
879                 return;
880
881         gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
882         gtkut_list_view_select_row(templates.list_view, row - 1);
883         modified_list = TRUE;
884 }
885
886 static void prefs_template_down_cb(void)
887 {
888         gint row, n_rows;
889         GtkTreeIter top, sel;
890         GtkTreeModel *model;
891
892         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));    
893         n_rows = gtk_tree_model_iter_n_children(model, NULL);
894         row = gtkut_list_view_get_selected_row(templates.list_view);
895         if (row < 1 || row >= n_rows - 1)
896                 return;
897
898         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
899         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row + 1))
900                 return;
901                         
902         gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
903         gtkut_list_view_select_row(templates.list_view, row + 1);
904         modified_list = TRUE;
905 }
906
907 static void prefs_template_bottom_cb(void)
908 {
909         gint row, n_rows;
910         GtkTreeIter top, sel;
911         GtkTreeModel *model;
912
913         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));    
914         n_rows = gtk_tree_model_iter_n_children(model, NULL);
915         row = gtkut_list_view_get_selected_row(templates.list_view);
916         if (row < 1 || row >= n_rows - 1)
917                 return;
918
919         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
920         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, n_rows - 1))
921                 return;
922
923         gtk_list_store_move_after(GTK_LIST_STORE(model), &top, &sel);           
924         gtkut_list_view_select_row(templates.list_view, n_rows - 1);
925         modified_list = TRUE;
926 }
927
928 static GtkListStore* prefs_template_create_data_store(void)
929 {
930         return gtk_list_store_new(N_TEMPL_COLUMNS,
931                                   G_TYPE_STRING,        
932                                   G_TYPE_POINTER,
933                                   G_TYPE_AUTO_POINTER,
934                                   -1);
935 }
936
937 static void prefs_template_list_view_insert_template(GtkWidget *list_view,
938                                                      gint row,
939                                                      const gchar *template,
940                                                      Template *data)
941 {
942         GtkTreeIter iter;
943         GtkTreeIter sibling;
944         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
945                                         (GTK_TREE_VIEW(list_view)));
946
947 /*      row -1 to add a new rule to store,
948         row >=0 to change an existing row
949         row <-1 insert a new row after (-row-2)
950 */
951         if (row >= 0 ) {
952                 /* modify the existing */
953                 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
954                                                    &iter, NULL, row))
955                         row = -1;
956         } else if (row < -1 ) {
957                 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
958                                                    &sibling, NULL, -row-2))
959                         row = -1;               
960         }
961
962         if (row == -1 ) {
963                 /* append new */
964                 gtk_list_store_append(list_store, &iter);
965                 gtk_list_store_set(list_store, &iter,
966                                    TEMPL_TEXT, template,
967                                    TEMPL_DATA, data,
968                                    -1);
969         } else if (row < -1) {
970                 /* duplicate */
971                 gtk_list_store_insert_after(list_store, &iter, &sibling);
972                 gtk_list_store_set(list_store, &iter,
973                                    TEMPL_TEXT, template,
974                                    TEMPL_DATA, data,
975                                    -1);
976         } else {
977                 /* change existing */
978                 GAuto *auto_data =
979                                         g_auto_pointer_new_with_free(data, (GFreeFunc) template_free);  
980
981                 /* if replacing data in an existing row, the auto pointer takes care
982                  * of destroying the Template data */
983                 gtk_list_store_set(list_store, &iter,
984                                    TEMPL_TEXT, template,
985                                    TEMPL_DATA, data,
986                                    TEMPL_AUTO_DATA, auto_data,
987                                    -1);
988
989                 g_auto_pointer_free(auto_data);
990         }
991 }
992
993 static GtkItemFactory *prefs_template_popup_factory = NULL;
994 static GtkWidget *prefs_template_popup_menu = NULL;
995
996 static GtkItemFactoryEntry prefs_template_popup_entries[] =
997 {
998    {N_("/_Delete"),             NULL, prefs_template_delete_cb, 0, NULL, NULL},
999    {N_("/Delete _all"), NULL, prefs_template_delete_all_cb, 0, NULL, NULL},
1000    {N_("/D_uplicate"),  NULL, prefs_template_duplicate_cb, 0, NULL, NULL},
1001 };
1002
1003 static gint prefs_template_list_btn_pressed(GtkWidget *widget, GdkEventButton *event,
1004                                    GtkTreeView *list_view)
1005 {
1006    if (event) {
1007            /* left- or right-button click */
1008            if (event->button == 1 || event->button == 3) {
1009                    GtkTreePath *path = NULL;
1010                    if (gtk_tree_view_get_path_at_pos( list_view, event->x, event->y,
1011                                                            &path, NULL, NULL, NULL)) {
1012                            prefs_template_select_row(list_view, path);
1013                    }
1014                    if (path)
1015                            gtk_tree_path_free(path);
1016            }
1017
1018            /* right-button click */
1019            if (event->button == 3) {
1020                    GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1021                    GtkTreeIter iter;
1022                    gboolean non_empty;
1023                    gint row;
1024
1025                    if (!prefs_template_popup_menu) {
1026                            gint n_entries = sizeof(prefs_template_popup_entries) /
1027                                            sizeof(prefs_template_popup_entries[0]);
1028                            prefs_template_popup_menu = menu_create_items(prefs_template_popup_entries,
1029                                                                  n_entries, "<PrefsTemplatePopupMenu>",
1030                                                                  &prefs_template_popup_factory, list_view);
1031                    }
1032
1033                    /* grey out some popup menu items if there is no selected row */
1034                    row = gtkut_list_view_get_selected_row(GTK_WIDGET(list_view));
1035                    menu_set_sensitive(prefs_template_popup_factory, "/Delete", (row > 0));
1036                    menu_set_sensitive(prefs_template_popup_factory, "/Duplicate", (row > 0));
1037
1038                    /* grey out seom popup menu items if there is no row
1039                           (not counting the (New) one at row 0) */
1040                    non_empty = gtk_tree_model_get_iter_first(model, &iter);
1041                    if (non_empty)
1042                            non_empty = gtk_tree_model_iter_next(model, &iter);
1043                    menu_set_sensitive(prefs_template_popup_factory, "/Delete all", non_empty);
1044
1045                    gtk_menu_popup(GTK_MENU(prefs_template_popup_menu), 
1046                                           NULL, NULL, NULL, NULL, 
1047                                           event->button, event->time);
1048            }
1049    }
1050    return FALSE;
1051 }
1052
1053 static gboolean prefs_template_list_popup_menu(GtkWidget *widget, gpointer data)
1054 {
1055    GtkTreeView *list_view = (GtkTreeView *)data;
1056    GdkEventButton event;
1057    
1058    event.button = 3;
1059    event.time = gtk_get_current_event_time();
1060    
1061    prefs_template_list_btn_pressed(NULL, &event, list_view);
1062
1063    return TRUE;
1064 }
1065
1066 static GtkWidget *prefs_template_list_view_create(void)
1067 {
1068         GtkTreeView *list_view;
1069         GtkTreeSelection *selector;
1070         GtkTreeModel *model;
1071
1072         model = GTK_TREE_MODEL(prefs_template_create_data_store());
1073         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
1074         g_object_unref(model);  
1075
1076 #ifndef MAEMO
1077         g_signal_connect(G_OBJECT(list_view), "popup-menu",
1078                          G_CALLBACK(prefs_template_list_popup_menu), list_view);
1079 #else
1080         gtk_widget_tap_and_hold_setup(GTK_WIDGET(list_view), NULL, NULL,
1081                         GTK_TAP_AND_HOLD_NONE | GTK_TAP_AND_HOLD_NO_INTERNALS);
1082         g_signal_connect(G_OBJECT(list_view), "tap-and-hold",
1083                          G_CALLBACK(prefs_template_list_popup_menu), list_view);
1084 #endif
1085         g_signal_connect(G_OBJECT(list_view), "button-press-event",
1086                         G_CALLBACK(prefs_template_list_btn_pressed), list_view);
1087         
1088         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
1089         gtk_tree_view_set_reorderable(list_view, TRUE);
1090
1091         selector = gtk_tree_view_get_selection(list_view);
1092         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1093
1094         /* create the columns */
1095         prefs_template_create_list_view_columns(GTK_WIDGET(list_view));
1096
1097         return GTK_WIDGET(list_view);
1098 }
1099
1100 static void prefs_template_create_list_view_columns(GtkWidget *list_view)
1101 {
1102         GtkTreeViewColumn *column;
1103         GtkCellRenderer *renderer;
1104
1105         renderer = gtk_cell_renderer_text_new();
1106         column = gtk_tree_view_column_new_with_attributes
1107                         (_("Current templates"),
1108                          renderer,
1109                          "text", TEMPL_TEXT,
1110                          NULL);
1111         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
1112 }
1113
1114  /*!
1115  *\brief        Triggered when a row has to be selected
1116  */
1117 static void prefs_template_select_row(GtkTreeView *list_view, GtkTreePath *path)
1118 {
1119         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1120         GtkTreeSelection *selection;
1121         Template *tmpl;
1122         Template tmpl_def;
1123         GtkTextBuffer *buffer;
1124         GtkTextIter iter;
1125         GtkTreeIter titer;
1126
1127         if (!model || !path || !gtk_tree_model_get_iter(model, &titer, path))
1128                 return;
1129
1130         /* select row */
1131         selection = gtk_tree_view_get_selection(list_view);
1132         gtk_tree_selection_select_path(selection, path);
1133
1134         tmpl_def.name = _("Template");
1135         tmpl_def.subject = "";
1136         tmpl_def.to = "";
1137         tmpl_def.cc = "";
1138         tmpl_def.bcc = "";      
1139         tmpl_def.value = "";
1140
1141         gtk_tree_model_get(model, &titer, TEMPL_DATA, &tmpl, -1);
1142         if (!tmpl)
1143                 tmpl = &tmpl_def;
1144
1145         gtk_entry_set_text(GTK_ENTRY(templates.entry_name), tmpl->name);
1146         gtk_entry_set_text(GTK_ENTRY(templates.entry_to),
1147                            tmpl->to ? tmpl->to : "");
1148         gtk_entry_set_text(GTK_ENTRY(templates.entry_cc),
1149                            tmpl->cc ? tmpl->cc : "");
1150         gtk_entry_set_text(GTK_ENTRY(templates.entry_bcc),
1151                            tmpl->bcc ? tmpl->bcc : "");                 
1152         gtk_entry_set_text(GTK_ENTRY(templates.entry_subject),
1153                            tmpl->subject ? tmpl->subject : "");
1154         
1155         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
1156         gtk_text_buffer_set_text(buffer, "", -1);
1157         gtk_text_buffer_get_start_iter(buffer, &iter);
1158         gtk_text_buffer_insert(buffer, &iter, tmpl->value, -1);
1159 }