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