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