Revert previous (mis-)commit.
[claws.git] / src / prefs_template.c
1 /*
2  * Claws Mail 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 the new template above to the list"), 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 the selected template in list with the template above"), 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 the selected template from the 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 the input fields in the 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), window);
335         gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), desc_btn,
336                         _("Show information on configuring templates"), NULL);
337
338         /* templates list */
339         hbox4 = gtk_hbox_new(FALSE, 8);
340         gtk_widget_show(hbox4);
341         gtk_box_pack_start(GTK_BOX(vbox2), hbox4, TRUE, TRUE, 0);
342
343         scroll1 = gtk_scrolled_window_new(NULL, NULL);
344         gtk_widget_show(scroll1);
345         gtk_box_pack_start(GTK_BOX(hbox4), scroll1, TRUE, TRUE, 0);
346         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll1),
347                                        GTK_POLICY_AUTOMATIC,
348                                        GTK_POLICY_AUTOMATIC);
349                                        
350         vbox3 = gtk_vbox_new(FALSE, 8);
351         gtk_widget_show(vbox3);
352         gtk_box_pack_start(GTK_BOX(hbox4), vbox3, FALSE, FALSE, 0);
353
354         top_btn = gtk_button_new_from_stock(GTK_STOCK_GOTO_TOP);
355         gtk_widget_show(top_btn);
356         gtk_box_pack_start(GTK_BOX(vbox3), top_btn, FALSE, FALSE, 0);
357         g_signal_connect(G_OBJECT(top_btn), "clicked",
358                          G_CALLBACK(prefs_template_top_cb), NULL);
359         gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), top_btn,
360                         _("Move the selected template to the top"), NULL);
361
362         PACK_VSPACER(vbox3, spc_vbox, VSPACING_NARROW_2);
363
364         up_btn = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
365         gtk_widget_show(up_btn);
366         gtk_box_pack_start (GTK_BOX(vbox3), up_btn, FALSE, FALSE, 0);
367         g_signal_connect(G_OBJECT(up_btn), "clicked",
368                          G_CALLBACK(prefs_template_up_cb), NULL);
369         gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), up_btn,
370                         _("Move the selected template up"), NULL);
371
372         down_btn = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
373         gtk_widget_show (down_btn);
374         gtk_box_pack_start(GTK_BOX (vbox3), down_btn, FALSE, FALSE, 0);
375         g_signal_connect(G_OBJECT (down_btn), "clicked",
376                          G_CALLBACK(prefs_template_down_cb), NULL);
377         gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), down_btn,
378                         _("Move the selected template down"), NULL);
379
380         PACK_VSPACER(vbox3, spc_vbox, VSPACING_NARROW_2);
381
382         bottom_btn = gtk_button_new_from_stock(GTK_STOCK_GOTO_BOTTOM);
383         gtk_widget_show(bottom_btn);
384         gtk_box_pack_start(GTK_BOX(vbox3), bottom_btn, FALSE, FALSE, 0);
385         g_signal_connect(G_OBJECT(bottom_btn), "clicked",
386                          G_CALLBACK(prefs_template_bottom_cb), NULL);
387         gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), bottom_btn,
388                         _("Move the selected template to the bottom"), NULL);
389
390         list_view = prefs_template_list_view_create();
391         gtk_widget_show(list_view);
392         gtk_widget_set_size_request(scroll1, -1, 140);
393         gtk_container_add(GTK_CONTAINER(scroll1), list_view);
394
395         /* help | cancel | ok */
396         gtkut_stock_button_set_create_with_help(&confirm_area, &help_btn,
397                         &cancel_btn, GTK_STOCK_CANCEL,
398                         &ok_btn, GTK_STOCK_OK,
399                         NULL, NULL);
400         gtk_widget_show(confirm_area);
401         gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
402         gtk_widget_grab_default(ok_btn);
403
404         gtk_window_set_title(GTK_WINDOW(window), _("Template configuration"));
405
406         g_signal_connect(G_OBJECT(window), "delete_event",
407                          G_CALLBACK(prefs_template_deleted_cb), NULL);
408         g_signal_connect(G_OBJECT(window), "size_allocate",
409                          G_CALLBACK(prefs_template_size_allocate_cb), NULL);
410         g_signal_connect(G_OBJECT(window), "key_press_event",
411                          G_CALLBACK(prefs_template_key_pressed_cb), NULL);
412         MANAGE_WINDOW_SIGNALS_CONNECT(window);
413         g_signal_connect(G_OBJECT(ok_btn), "clicked",
414                          G_CALLBACK(prefs_template_ok_cb), NULL);
415         g_signal_connect(G_OBJECT(cancel_btn), "clicked",
416                          G_CALLBACK(prefs_template_cancel_cb), NULL);
417         g_signal_connect(G_OBJECT(help_btn), "clicked",
418                          G_CALLBACK(manual_open_with_anchor_cb),
419                          MANUAL_ANCHOR_TEMPLATES);
420
421         address_completion_start(window);
422
423         if (!geometry.min_height) {
424                 geometry.min_width = 500;
425                 geometry.min_height = 540;
426         }
427
428         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
429                                       GDK_HINT_MIN_SIZE);
430         gtk_widget_set_size_request(window, prefs_common.templateswin_width,
431                                     prefs_common.templateswin_height);
432
433         templates.window = window;
434         templates.ok_btn = ok_btn;
435         templates.list_view = list_view;
436         templates.text_value = text_value;
437 }
438
439 static void prefs_template_reset_dialog(void)
440 {
441         GtkTextBuffer *buffer;
442
443         gtk_entry_set_text(GTK_ENTRY(templates.entry_name), "");
444         gtk_entry_set_text(GTK_ENTRY(templates.entry_to), "");
445         gtk_entry_set_text(GTK_ENTRY(templates.entry_cc), "");
446         gtk_entry_set_text(GTK_ENTRY(templates.entry_bcc), "");                 
447         gtk_entry_set_text(GTK_ENTRY(templates.entry_subject), "");
448         
449         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
450         gtk_text_buffer_set_text(buffer, "", -1);
451 }
452
453 static void prefs_template_clear_list(void)
454 {
455         GtkListStore *store;
456         
457         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
458                                 (templates.list_view)));
459         gtk_list_store_clear(store);
460
461         prefs_template_list_view_insert_template(templates.list_view,
462                                                  -1, _("(New)"),
463                                                  NULL);
464 }
465
466 static void prefs_template_window_setup(void)
467 {
468         GSList *tmpl_list;
469         GSList *cur;
470         Template *tmpl;
471
472         manage_window_set_transient(GTK_WINDOW(templates.window));
473         gtk_widget_grab_focus(templates.ok_btn);
474
475         prefs_template_clear_list();
476         
477         tmpl_list = template_read_config();
478
479         for (cur = tmpl_list; cur != NULL; cur = cur->next) {
480                 tmpl = (Template *)cur->data;
481                 prefs_template_list_view_insert_template(templates.list_view,
482                                                          -1, tmpl->name, 
483                                                          tmpl);
484         }
485
486         prefs_template_reset_dialog();
487
488         g_slist_free(tmpl_list);
489 }
490
491 static gint prefs_template_deleted_cb(GtkWidget *widget, GdkEventAny *event,
492                                       gpointer data)
493 {
494         prefs_template_cancel_cb();
495         return TRUE;
496 }
497
498 static gboolean prefs_template_key_pressed_cb(GtkWidget *widget,
499                                               GdkEventKey *event, gpointer data)
500 {
501         if (event && event->keyval == GDK_Escape)
502                 prefs_template_cancel_cb();
503         else {
504                 GtkWidget *focused = gtkut_get_focused_child(
505                                         GTK_CONTAINER(widget));
506                 if (focused && GTK_IS_EDITABLE(focused)) {
507                         modified = TRUE;
508                 }
509         }
510         return FALSE;
511 }
512
513 static void prefs_template_ok_cb(void)
514 {
515         GSList *tmpl_list;
516         GtkListStore *store;
517
518         if (modified && alertpanel(_("Entry not saved"),
519                                  _("The entry was not saved. Close anyway?"),
520                                  GTK_STOCK_CLOSE, _("+_Continue editing"), 
521                                  NULL) != G_ALERTDEFAULT) {
522                 return;
523         } 
524         modified = FALSE;
525         modified_list = FALSE;
526         tmpl_list = prefs_template_get_list();
527         template_set_config(tmpl_list);
528         compose_reflect_prefs_all();
529         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
530                                 (templates.list_view)));
531         gtk_list_store_clear(store);
532         gtk_widget_hide(templates.window);
533         inc_unlock();
534 }
535
536 static void prefs_template_cancel_cb(void)
537 {
538         GtkListStore *store;
539
540         if (modified && alertpanel(_("Entry not saved"),
541                                  _("The entry was not saved. Close anyway?"),
542                                  GTK_STOCK_CLOSE, _("+_Continue editing"),
543                                  NULL) != G_ALERTDEFAULT) {
544                 return;
545         } else if (modified_list && alertpanel(_("Templates list not saved"),
546                                  _("The templates list has been modified. Close anyway?"),
547                                  GTK_STOCK_CLOSE, _("+_Continue editing"), 
548                                  NULL) != G_ALERTDEFAULT) {
549                 return;
550         }
551         modified = FALSE;
552         modified_list = FALSE;
553         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
554                                 (templates.list_view)));
555         gtk_list_store_clear(store);
556         gtk_widget_hide(templates.window);
557         inc_unlock();
558 }
559
560 /*!
561  *\brief        Request list for storage. New list is owned
562  *              by template.c...
563  */
564 static GSList *prefs_template_get_list(void)
565 {
566         GSList *tmpl_list = NULL;
567         Template *tmpl;
568         GtkTreeModel *model;
569         GtkTreeIter iter;
570
571         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
572         if (!gtk_tree_model_get_iter_first(model, &iter))
573                 return NULL;
574
575         do {
576                 gtk_tree_model_get(model, &iter,
577                                    TEMPL_DATA, &tmpl,
578                                    -1);
579                 
580                 if (tmpl) {
581                         Template *ntmpl;
582                         
583                         ntmpl = g_new(Template, 1);
584                         ntmpl->load_filename = NULL;
585                         ntmpl->name    = tmpl->name && *(tmpl->name) 
586                                          ? g_strdup(tmpl->name) 
587                                          : NULL;
588                         ntmpl->subject = tmpl->subject && *(tmpl->subject) 
589                                          ? g_strdup(tmpl->subject) 
590                                          : NULL;
591                         ntmpl->to      = tmpl->to && *(tmpl->to)
592                                          ? g_strdup(tmpl->to)
593                                          : NULL;
594                         ntmpl->cc      = tmpl->cc && *(tmpl->cc)
595                                          ? g_strdup(tmpl->cc)
596                                          : NULL;
597                         ntmpl->bcc     = tmpl->bcc && *(tmpl->bcc)
598                                          ? g_strdup(tmpl->bcc)
599                                          : NULL;        
600                         ntmpl->value   = tmpl->value && *(tmpl->value)
601                                          ? g_strdup(tmpl->value)
602                                          : NULL;
603                         tmpl_list = g_slist_append(tmpl_list, ntmpl);
604                 }                       
605         
606         } while (gtk_tree_model_iter_next(model, &iter)); 
607
608         return tmpl_list;
609 }
610
611 gboolean prefs_template_string_is_valid(gchar *string, gint *line, gboolean escaped_string)
612 {
613         if (string && *string != '\0') {
614                 gchar *parsed_buf;
615                 MsgInfo dummyinfo;
616
617                 memset(&dummyinfo, 0, sizeof(MsgInfo));
618 #ifdef USE_ASPELL
619                 quote_fmt_init(&dummyinfo, NULL, NULL, TRUE, NULL, escaped_string, NULL);
620 #else
621                 quote_fmt_init(&dummyinfo, NULL, NULL, TRUE, NULL, escaped_string);
622 #endif
623                 quote_fmt_scan_string(string);
624                 quote_fmt_parse();
625                 parsed_buf = quote_fmt_get_buffer();
626                 if (!parsed_buf) {
627                         if (line)
628                                 *line = quote_fmt_get_line();
629                         return FALSE;
630                 }
631                 quote_fmt_reset_vartable();
632         }
633         return TRUE;
634 }
635
636 static gboolean prefs_template_list_view_set_row(gint row)
637 /* return TRUE if the row could be modified */
638 {
639         Template *tmpl;
640         gchar *name;
641         gchar *subject;
642         gchar *to;
643         gchar *cc;
644         gchar *bcc;     
645         gchar *value;
646         GtkTextBuffer *buffer;
647         GtkTextIter start, end;
648         GtkTreeModel *model;
649         gint line;
650
651         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
652
653         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
654         gtk_text_buffer_get_start_iter(buffer, &start);
655         gtk_text_buffer_get_iter_at_offset(buffer, &end, -1);
656         value = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
657
658         if (value && *value == '\0') {
659                         g_free(value);
660                 value = NULL;
661                 }
662         if (!prefs_template_string_is_valid(value, &line, FALSE)) {
663                 alertpanel_error(_("Template body format error at line %d."), line);
664                 g_free(value);
665                 return FALSE;
666         }
667
668         name = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_name),
669                                       0, -1);
670         if (*name == '\0') {
671                 alertpanel_error(_("Template name is not set."));
672                 g_free(value);
673                 return FALSE;
674         }
675         to = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_to),
676                                     0, -1);
677         cc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_cc),
678                                     0, -1);
679         bcc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_bcc),
680                                     0, -1);
681         subject = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_subject),
682                                          0, -1);
683
684         if (to && *to == '\0') {
685                 g_free(to);
686                 to = NULL;
687         }
688         if (cc && *cc == '\0') {
689                 g_free(cc);
690                 cc = NULL;
691         }
692         if (bcc && *bcc == '\0') {
693                 g_free(bcc);
694                 bcc = NULL;
695         }
696         if (subject && *subject == '\0') {
697                 g_free(subject);
698                 subject = NULL;
699         }
700
701         if (!prefs_template_string_is_valid(to, NULL, FALSE)) {
702                 alertpanel_error(_("Template To format error."));
703                 g_free(to);
704                 g_free(value);
705                 return FALSE;
706         }
707         if (!prefs_template_string_is_valid(cc, NULL, FALSE)) {
708                 alertpanel_error(_("Template Cc format error."));       
709                 g_free(cc);
710                 g_free(value);
711                 return FALSE;
712         }
713         if (!prefs_template_string_is_valid(bcc, NULL, FALSE)) {
714                 alertpanel_error(_("Template Bcc format error."));      
715                 g_free(bcc);
716                 g_free(value);
717                 return FALSE;
718         }
719         if (!prefs_template_string_is_valid(subject, NULL, FALSE)) {
720                 alertpanel_error(_("Template subject format error."));  
721                 g_free(subject);
722                 g_free(value);
723                 return FALSE;
724         }
725         
726         tmpl = g_new(Template, 1);
727         tmpl->load_filename = NULL;
728         tmpl->name = name;
729         tmpl->subject = subject;
730         tmpl->to = to;
731         tmpl->cc = cc;
732         tmpl->bcc = bcc;        
733         tmpl->value = value;
734
735         prefs_template_list_view_insert_template(templates.list_view,
736                                                  row, tmpl->name, tmpl);
737
738         return TRUE;
739 }
740
741 static void prefs_template_register_cb(void)
742 {
743         modified = !prefs_template_list_view_set_row(-1);
744         modified_list = TRUE;
745 }
746
747 static void prefs_template_substitute_cb(void)
748 {
749         Template *tmpl;
750         gint row;
751         GtkTreeIter iter;
752         GtkTreeModel *model;
753
754         row = gtkut_list_view_get_selected_row(templates.list_view);
755         if (row <= 0)
756                 return;
757
758         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
759         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
760                 return;
761
762         gtk_tree_model_get(model, &iter, TEMPL_DATA, &tmpl, -1);
763         if (!tmpl)
764                 return;
765
766         modified = !prefs_template_list_view_set_row(row);
767         modified_list = TRUE;
768 }
769
770 static void prefs_template_delete_cb(void)
771 {
772         Template *tmpl;
773         gint row;
774         GtkTreeIter iter;
775         GtkTreeModel *model;
776
777         row = gtkut_list_view_get_selected_row(templates.list_view);
778         if (row <= 0)
779                 return;
780
781         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
782         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
783                 return;
784
785         gtk_tree_model_get(model, &iter, TEMPL_DATA, &tmpl, -1);
786         if (!tmpl)
787                 return;
788
789         if (alertpanel(_("Delete template"),
790                        _("Do you really want to delete this template?"),
791                        GTK_STOCK_CANCEL, GTK_STOCK_DELETE,
792                        NULL) != G_ALERTALTERNATE)
793                 return;
794
795         gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
796         modified_list = TRUE;   
797 }
798
799 static void prefs_template_delete_all_cb(void)
800 {
801         GtkListStore *list_store;
802
803         if (alertpanel(_("Delete all templates"),
804                           _("Do you really want to delete all the templates?"),
805                           GTK_STOCK_CANCEL, "+"GTK_STOCK_DELETE, NULL) == G_ALERTDEFAULT)
806            return;
807
808         list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view)));
809         prefs_template_clear_list();
810         modified = FALSE;
811
812         prefs_template_reset_dialog();
813         modified_list = TRUE;
814 }
815
816 static void prefs_template_duplicate_cb(void)
817 {
818         Template *tmpl;
819         gint row;
820         GtkTreeIter iter;
821         GtkTreeModel *model;
822         
823         row = gtkut_list_view_get_selected_row(templates.list_view);
824         if (row <= 0)
825                 return;
826
827         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
828         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
829                 return;
830
831         gtk_tree_model_get(model, &iter, TEMPL_DATA, &tmpl, -1);
832         if (!tmpl)
833                 return;
834
835         modified_list = !prefs_template_list_view_set_row(-row-2);
836 }
837
838 static void prefs_template_clear_cb(void)
839 {
840    prefs_template_reset_dialog();
841 }
842
843 static void prefs_template_top_cb(void)
844 {
845         gint row;
846         GtkTreeIter top, sel;
847         GtkTreeModel *model;
848
849         row = gtkut_list_view_get_selected_row(templates.list_view);
850         if (row <= 1) 
851                 return;
852
853         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));            
854         
855         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, 0)
856         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
857                 return;
858
859         gtk_list_store_move_after(GTK_LIST_STORE(model), &sel, &top);
860         gtkut_list_view_select_row(templates.list_view, 1);
861         modified_list = TRUE;
862 }
863
864 static void prefs_template_up_cb(void)
865 {
866         gint row;
867         GtkTreeIter top, sel;
868         GtkTreeModel *model;
869
870         row = gtkut_list_view_get_selected_row(templates.list_view);
871         if (row <= 1) 
872                 return;
873                 
874         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));    
875
876         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row - 1)
877         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
878                 return;
879
880         gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
881         gtkut_list_view_select_row(templates.list_view, row - 1);
882         modified_list = TRUE;
883 }
884
885 static void prefs_template_down_cb(void)
886 {
887         gint row, n_rows;
888         GtkTreeIter top, sel;
889         GtkTreeModel *model;
890
891         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));    
892         n_rows = gtk_tree_model_iter_n_children(model, NULL);
893         row = gtkut_list_view_get_selected_row(templates.list_view);
894         if (row < 1 || row >= n_rows - 1)
895                 return;
896
897         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
898         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row + 1))
899                 return;
900                         
901         gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
902         gtkut_list_view_select_row(templates.list_view, row + 1);
903         modified_list = TRUE;
904 }
905
906 static void prefs_template_bottom_cb(void)
907 {
908         gint row, n_rows;
909         GtkTreeIter top, sel;
910         GtkTreeModel *model;
911
912         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));    
913         n_rows = gtk_tree_model_iter_n_children(model, NULL);
914         row = gtkut_list_view_get_selected_row(templates.list_view);
915         if (row < 1 || row >= n_rows - 1)
916                 return;
917
918         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
919         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, n_rows - 1))
920                 return;
921
922         gtk_list_store_move_after(GTK_LIST_STORE(model), &top, &sel);           
923         gtkut_list_view_select_row(templates.list_view, n_rows - 1);
924         modified_list = TRUE;
925 }
926
927 static GtkListStore* prefs_template_create_data_store(void)
928 {
929         return gtk_list_store_new(N_TEMPL_COLUMNS,
930                                   G_TYPE_STRING,        
931                                   G_TYPE_POINTER,
932                                   G_TYPE_AUTO_POINTER,
933                                   -1);
934 }
935
936 static void prefs_template_list_view_insert_template(GtkWidget *list_view,
937                                                      gint row,
938                                                      const gchar *template,
939                                                      Template *data)
940 {
941         GtkTreeIter iter;
942         GtkTreeIter sibling;
943         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
944                                         (GTK_TREE_VIEW(list_view)));
945
946 /*      row -1 to add a new rule to store,
947         row >=0 to change an existing row
948         row <-1 insert a new row after (-row-2)
949 */
950         if (row >= 0 ) {
951                 /* modify the existing */
952                 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
953                                                    &iter, NULL, row))
954                         row = -1;
955         } else if (row < -1 ) {
956                 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
957                                                    &sibling, NULL, -row-2))
958                         row = -1;               
959         }
960
961         if (row == -1 ) {
962                 /* append new */
963                 gtk_list_store_append(list_store, &iter);
964                 gtk_list_store_set(list_store, &iter,
965                                    TEMPL_TEXT, template,
966                                    TEMPL_DATA, data,
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                                    TEMPL_TEXT, template,
973                                    TEMPL_DATA, data,
974                                    -1);
975         } else {
976                 /* change existing */
977                 GAuto *auto_data =
978                                         g_auto_pointer_new_with_free(data, (GFreeFunc) template_free);  
979
980                 /* if replacing data in an existing row, the auto pointer takes care
981                  * of destroying the Template data */
982                 gtk_list_store_set(list_store, &iter,
983                                    TEMPL_TEXT, template,
984                                    TEMPL_DATA, data,
985                                    TEMPL_AUTO_DATA, auto_data,
986                                    -1);
987
988                 g_auto_pointer_free(auto_data);
989         }
990 }
991
992 static GtkItemFactory *prefs_template_popup_factory = NULL;
993 static GtkWidget *prefs_template_popup_menu = NULL;
994
995 static GtkItemFactoryEntry prefs_template_popup_entries[] =
996 {
997    {N_("/_Delete"),             NULL, prefs_template_delete_cb, 0, NULL, NULL},
998    {N_("/Delete _all"), NULL, prefs_template_delete_all_cb, 0, NULL, NULL},
999    {N_("/D_uplicate"),  NULL, prefs_template_duplicate_cb, 0, NULL, NULL},
1000 };
1001
1002 static gint prefs_template_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_template_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_template_popup_menu) {
1025                            gint n_entries = sizeof(prefs_template_popup_entries) /
1026                                            sizeof(prefs_template_popup_entries[0]);
1027                            prefs_template_popup_menu = menu_create_items(prefs_template_popup_entries,
1028                                                                  n_entries, "<PrefsTemplatePopupMenu>",
1029                                                                  &prefs_template_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_template_popup_factory, "/Delete", (row > 0));
1035                    menu_set_sensitive(prefs_template_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_template_popup_factory, "/Delete all", non_empty);
1043
1044                    gtk_menu_popup(GTK_MENU(prefs_template_popup_menu), 
1045                                           NULL, NULL, NULL, NULL, 
1046                                           event->button, event->time);
1047            }
1048    }
1049    return FALSE;
1050 }
1051
1052 static gboolean prefs_template_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_template_list_btn_pressed(NULL, &event, list_view);
1061
1062    return TRUE;
1063 }
1064
1065 static GtkWidget *prefs_template_list_view_create(void)
1066 {
1067         GtkTreeView *list_view;
1068         GtkTreeSelection *selector;
1069         GtkTreeModel *model;
1070
1071         model = GTK_TREE_MODEL(prefs_template_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_template_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_template_list_popup_menu), list_view);
1083 #endif
1084         g_signal_connect(G_OBJECT(list_view), "button-press-event",
1085                         G_CALLBACK(prefs_template_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_template_create_list_view_columns(GTK_WIDGET(list_view));
1095
1096         return GTK_WIDGET(list_view);
1097 }
1098
1099 static void prefs_template_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 templates"),
1107                          renderer,
1108                          "text", TEMPL_TEXT,
1109                          NULL);
1110         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
1111 }
1112
1113  /*!
1114  *\brief        Triggered when a row has to be selected
1115  */
1116 static void prefs_template_select_row(GtkTreeView *list_view, GtkTreePath *path)
1117 {
1118         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1119         GtkTreeSelection *selection;
1120         Template *tmpl;
1121         Template tmpl_def;
1122         GtkTextBuffer *buffer;
1123         GtkTextIter iter;
1124         GtkTreeIter titer;
1125
1126         if (!model || !path || !gtk_tree_model_get_iter(model, &titer, 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         tmpl_def.name = _("Template");
1134         tmpl_def.subject = "";
1135         tmpl_def.to = "";
1136         tmpl_def.cc = "";
1137         tmpl_def.bcc = "";      
1138         tmpl_def.value = "";
1139
1140         gtk_tree_model_get(model, &titer, TEMPL_DATA, &tmpl, -1);
1141         if (!tmpl)
1142                 tmpl = &tmpl_def;
1143
1144         gtk_entry_set_text(GTK_ENTRY(templates.entry_name), tmpl->name);
1145         gtk_entry_set_text(GTK_ENTRY(templates.entry_to),
1146                            tmpl->to ? tmpl->to : "");
1147         gtk_entry_set_text(GTK_ENTRY(templates.entry_cc),
1148                            tmpl->cc ? tmpl->cc : "");
1149         gtk_entry_set_text(GTK_ENTRY(templates.entry_bcc),
1150                            tmpl->bcc ? tmpl->bcc : "");                 
1151         gtk_entry_set_text(GTK_ENTRY(templates.entry_subject),
1152                            tmpl->subject ? tmpl->subject : "");
1153         
1154         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
1155         gtk_text_buffer_set_text(buffer, "", -1);
1156         gtk_text_buffer_get_start_iter(buffer, &iter);
1157         gtk_text_buffer_insert(buffer, &iter, tmpl->value, -1);
1158 }