2008-05-07 [colin] 3.4.0cvs29
[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 GENERIC_UMPC
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 *tmp = NULL;
644                 gchar *parsed_buf;
645                 MsgInfo dummyinfo;
646                 PrefsAccount *account = account_get_default();
647
648                 if (escaped_string) {
649                         tmp = malloc(strlen(string)+1);
650                         pref_get_unescaped_pref(tmp, string);
651                 } else {
652                         tmp = g_strdup(string);
653                 }
654                 memset(&dummyinfo, 0, sizeof(MsgInfo));
655 #ifdef USE_ASPELL
656                 quote_fmt_init(&dummyinfo, NULL, NULL, TRUE, account, FALSE, NULL);
657 #else
658                 quote_fmt_init(&dummyinfo, NULL, NULL, TRUE, account, FALSE);
659 #endif
660                 quote_fmt_scan_string(tmp);
661                 quote_fmt_parse();
662                 g_free(tmp);
663                 parsed_buf = quote_fmt_get_buffer();
664                 if (!parsed_buf) {
665                         if (line)
666                                 *line = quote_fmt_get_line();
667                         return FALSE;
668                 }
669                 if (email) {
670                         const gchar *start = strrchr(parsed_buf, '<');
671                         const gchar *end = strrchr(parsed_buf, '>');
672                         const gchar *at = strrchr(parsed_buf, '@');
673                         const gchar *space = strrchr(parsed_buf, ' ');
674                         if (!at)
675                                 result = FALSE;
676                         if (at && space && (!start || !end || end < start || start < space))
677                                 result = FALSE;
678                 }
679                 quote_fmt_reset_vartable();
680         }
681         return result;
682 }
683
684 static gboolean prefs_template_list_view_set_row(gint row)
685 /* return TRUE if the row could be modified */
686 {
687         Template *tmpl;
688         gchar *name;
689         gchar *subject;
690         gchar *from;
691         gchar *to;
692         gchar *cc;
693         gchar *bcc;     
694         gchar *value;
695         GtkTextBuffer *buffer;
696         GtkTextIter start, end;
697         GtkTreeModel *model;
698         gint line;
699
700         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
701
702         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
703         gtk_text_buffer_get_start_iter(buffer, &start);
704         gtk_text_buffer_get_iter_at_offset(buffer, &end, -1);
705         value = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
706
707         if (value && *value == '\0') {
708                         g_free(value);
709                 value = NULL;
710                 }
711         if (!prefs_template_string_is_valid(value, &line, TRUE, FALSE)) {
712                 alertpanel_error(_("Template body format error at line %d."), line);
713                 g_free(value);
714                 return FALSE;
715         }
716
717         name = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_name),
718                                       0, -1);
719         if (*name == '\0') {
720                 alertpanel_error(_("Template name is not set."));
721                 g_free(value);
722                 return FALSE;
723         }
724         from = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_from),
725                                     0, -1);
726         to = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_to),
727                                     0, -1);
728         cc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_cc),
729                                     0, -1);
730         bcc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_bcc),
731                                     0, -1);
732         subject = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_subject),
733                                          0, -1);
734
735         if (from && *from == '\0') {
736                 g_free(from);
737                 from = NULL;
738         }
739         if (to && *to == '\0') {
740                 g_free(to);
741                 to = NULL;
742         }
743         if (cc && *cc == '\0') {
744                 g_free(cc);
745                 cc = NULL;
746         }
747         if (bcc && *bcc == '\0') {
748                 g_free(bcc);
749                 bcc = NULL;
750         }
751         if (subject && *subject == '\0') {
752                 g_free(subject);
753                 subject = NULL;
754         }
755
756         if (!prefs_template_string_is_valid(from, NULL, TRUE, TRUE)) {
757                 alertpanel_error(_("Template From format error."));
758                 g_free(from);
759                 g_free(value);
760                 return FALSE;
761         }
762         if (!prefs_template_string_is_valid(to, NULL, TRUE, TRUE)) {
763                 alertpanel_error(_("Template To format error."));
764                 g_free(to);
765                 g_free(value);
766                 return FALSE;
767         }
768         if (!prefs_template_string_is_valid(cc, NULL, TRUE, TRUE)) {
769                 alertpanel_error(_("Template Cc format error."));       
770                 g_free(cc);
771                 g_free(value);
772                 return FALSE;
773         }
774         if (!prefs_template_string_is_valid(bcc, NULL, TRUE, TRUE)) {
775                 alertpanel_error(_("Template Bcc format error."));      
776                 g_free(bcc);
777                 g_free(value);
778                 return FALSE;
779         }
780         if (!prefs_template_string_is_valid(subject, NULL, TRUE, FALSE)) {
781                 alertpanel_error(_("Template subject format error."));  
782                 g_free(subject);
783                 g_free(value);
784                 return FALSE;
785         }
786         
787         tmpl = g_new(Template, 1);
788         tmpl->load_filename = NULL;
789         tmpl->name = name;
790         tmpl->subject = subject;
791         tmpl->from = from;
792         tmpl->to = to;
793         tmpl->cc = cc;
794         tmpl->bcc = bcc;        
795         tmpl->value = value;
796
797         prefs_template_list_view_insert_template(templates.list_view,
798                                                  row, tmpl->name, tmpl);
799
800         return TRUE;
801 }
802
803 static void prefs_template_register_cb(void)
804 {
805         modified = !prefs_template_list_view_set_row(-1);
806         modified_list = TRUE;
807 }
808
809 static void prefs_template_substitute_cb(void)
810 {
811         Template *tmpl;
812         gint row;
813         GtkTreeIter iter;
814         GtkTreeModel *model;
815
816         row = gtkut_list_view_get_selected_row(templates.list_view);
817         if (row <= 0)
818                 return;
819
820         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
821         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
822                 return;
823
824         gtk_tree_model_get(model, &iter, TEMPL_DATA, &tmpl, -1);
825         if (!tmpl)
826                 return;
827
828         modified = !prefs_template_list_view_set_row(row);
829         modified_list = TRUE;
830 }
831
832 static void prefs_template_delete_cb(void)
833 {
834         Template *tmpl;
835         gint row;
836         GtkTreeIter iter;
837         GtkTreeModel *model;
838
839         row = gtkut_list_view_get_selected_row(templates.list_view);
840         if (row <= 0)
841                 return;
842
843         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
844         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
845                 return;
846
847         gtk_tree_model_get(model, &iter, TEMPL_DATA, &tmpl, -1);
848         if (!tmpl)
849                 return;
850
851         if (alertpanel(_("Delete template"),
852                        _("Do you really want to delete this template?"),
853                        GTK_STOCK_CANCEL, GTK_STOCK_DELETE,
854                        NULL) != G_ALERTALTERNATE)
855                 return;
856
857         gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
858         modified_list = TRUE;   
859 }
860
861 static void prefs_template_delete_all_cb(void)
862 {
863         GtkListStore *list_store;
864
865         if (alertpanel(_("Delete all templates"),
866                           _("Do you really want to delete all the templates?"),
867                           GTK_STOCK_CANCEL, "+"GTK_STOCK_DELETE, NULL) == G_ALERTDEFAULT)
868            return;
869
870         list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view)));
871         prefs_template_clear_list();
872         modified = FALSE;
873
874         prefs_template_reset_dialog();
875         modified_list = TRUE;
876 }
877
878 static void prefs_template_duplicate_cb(void)
879 {
880         Template *tmpl;
881         gint row;
882         GtkTreeIter iter;
883         GtkTreeModel *model;
884         
885         row = gtkut_list_view_get_selected_row(templates.list_view);
886         if (row <= 0)
887                 return;
888
889         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
890         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
891                 return;
892
893         gtk_tree_model_get(model, &iter, TEMPL_DATA, &tmpl, -1);
894         if (!tmpl)
895                 return;
896
897         modified_list = !prefs_template_list_view_set_row(-row-2);
898 }
899
900 static void prefs_template_clear_cb(void)
901 {
902    prefs_template_reset_dialog();
903 }
904
905 static void prefs_template_top_cb(void)
906 {
907         gint row;
908         GtkTreeIter top, sel;
909         GtkTreeModel *model;
910
911         row = gtkut_list_view_get_selected_row(templates.list_view);
912         if (row <= 1) 
913                 return;
914
915         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));            
916         
917         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, 0)
918         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
919                 return;
920
921         gtk_list_store_move_after(GTK_LIST_STORE(model), &sel, &top);
922         gtkut_list_view_select_row(templates.list_view, 1);
923         modified_list = TRUE;
924 }
925
926 static void prefs_template_up_cb(void)
927 {
928         gint row;
929         GtkTreeIter top, sel;
930         GtkTreeModel *model;
931
932         row = gtkut_list_view_get_selected_row(templates.list_view);
933         if (row <= 1) 
934                 return;
935                 
936         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));    
937
938         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row - 1)
939         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
940                 return;
941
942         gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
943         gtkut_list_view_select_row(templates.list_view, row - 1);
944         modified_list = TRUE;
945 }
946
947 static void prefs_template_down_cb(void)
948 {
949         gint row, n_rows;
950         GtkTreeIter top, sel;
951         GtkTreeModel *model;
952
953         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));    
954         n_rows = gtk_tree_model_iter_n_children(model, NULL);
955         row = gtkut_list_view_get_selected_row(templates.list_view);
956         if (row < 1 || row >= n_rows - 1)
957                 return;
958
959         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
960         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row + 1))
961                 return;
962                         
963         gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
964         gtkut_list_view_select_row(templates.list_view, row + 1);
965         modified_list = TRUE;
966 }
967
968 static void prefs_template_bottom_cb(void)
969 {
970         gint row, n_rows;
971         GtkTreeIter top, sel;
972         GtkTreeModel *model;
973
974         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));    
975         n_rows = gtk_tree_model_iter_n_children(model, NULL);
976         row = gtkut_list_view_get_selected_row(templates.list_view);
977         if (row < 1 || row >= n_rows - 1)
978                 return;
979
980         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
981         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, n_rows - 1))
982                 return;
983
984         gtk_list_store_move_after(GTK_LIST_STORE(model), &top, &sel);           
985         gtkut_list_view_select_row(templates.list_view, n_rows - 1);
986         modified_list = TRUE;
987 }
988
989 static GtkListStore* prefs_template_create_data_store(void)
990 {
991         return gtk_list_store_new(N_TEMPL_COLUMNS,
992                                   G_TYPE_STRING,        
993                                   G_TYPE_POINTER,
994                                   G_TYPE_AUTO_POINTER,
995                                   -1);
996 }
997
998 static void prefs_template_list_view_insert_template(GtkWidget *list_view,
999                                                      gint row,
1000                                                      const gchar *template,
1001                                                      Template *data)
1002 {
1003         GtkTreeIter iter;
1004         GtkTreeIter sibling;
1005         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
1006                                         (GTK_TREE_VIEW(list_view)));
1007
1008 /*      row -1 to add a new rule to store,
1009         row >=0 to change an existing row
1010         row <-1 insert a new row after (-row-2)
1011 */
1012         if (row >= 0 ) {
1013                 /* modify the existing */
1014                 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
1015                                                    &iter, NULL, row))
1016                         row = -1;
1017         } else if (row < -1 ) {
1018                 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
1019                                                    &sibling, NULL, -row-2))
1020                         row = -1;               
1021         }
1022
1023         if (row == -1 ) {
1024                 /* append new */
1025                 gtk_list_store_append(list_store, &iter);
1026                 gtk_list_store_set(list_store, &iter,
1027                                    TEMPL_TEXT, template,
1028                                    TEMPL_DATA, data,
1029                                    -1);
1030         } else if (row < -1) {
1031                 /* duplicate */
1032                 gtk_list_store_insert_after(list_store, &iter, &sibling);
1033                 gtk_list_store_set(list_store, &iter,
1034                                    TEMPL_TEXT, template,
1035                                    TEMPL_DATA, data,
1036                                    -1);
1037         } else {
1038                 /* change existing */
1039                 GAuto *auto_data =
1040                                         g_auto_pointer_new_with_free(data, (GFreeFunc) template_free);  
1041
1042                 /* if replacing data in an existing row, the auto pointer takes care
1043                  * of destroying the Template data */
1044                 gtk_list_store_set(list_store, &iter,
1045                                    TEMPL_TEXT, template,
1046                                    TEMPL_DATA, data,
1047                                    TEMPL_AUTO_DATA, auto_data,
1048                                    -1);
1049
1050                 g_auto_pointer_free(auto_data);
1051         }
1052 }
1053
1054 static GtkItemFactory *prefs_template_popup_factory = NULL;
1055 static GtkWidget *prefs_template_popup_menu = NULL;
1056
1057 static GtkItemFactoryEntry prefs_template_popup_entries[] =
1058 {
1059    {N_("/_Delete"),             NULL, prefs_template_delete_cb, 0, NULL, NULL},
1060    {N_("/Delete _all"), NULL, prefs_template_delete_all_cb, 0, NULL, NULL},
1061    {N_("/D_uplicate"),  NULL, prefs_template_duplicate_cb, 0, NULL, NULL},
1062 };
1063
1064 static gint prefs_template_list_btn_pressed(GtkWidget *widget, GdkEventButton *event,
1065                                    GtkTreeView *list_view)
1066 {
1067    if (event) {
1068            /* left- or right-button click */
1069            if (event->button == 1 || event->button == 3) {
1070                    GtkTreePath *path = NULL;
1071                    if (gtk_tree_view_get_path_at_pos( list_view, event->x, event->y,
1072                                                            &path, NULL, NULL, NULL)) {
1073                            prefs_template_select_row(list_view, path);
1074                    }
1075                    if (path)
1076                            gtk_tree_path_free(path);
1077            }
1078
1079            /* right-button click */
1080            if (event->button == 3) {
1081                    GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1082                    GtkTreeIter iter;
1083                    gboolean non_empty;
1084                    gint row;
1085
1086                    if (!prefs_template_popup_menu) {
1087                            gint n_entries = sizeof(prefs_template_popup_entries) /
1088                                            sizeof(prefs_template_popup_entries[0]);
1089                            prefs_template_popup_menu = menu_create_items(prefs_template_popup_entries,
1090                                                                  n_entries, "<PrefsTemplatePopupMenu>",
1091                                                                  &prefs_template_popup_factory, list_view);
1092                    }
1093
1094                    /* grey out some popup menu items if there is no selected row */
1095                    row = gtkut_list_view_get_selected_row(GTK_WIDGET(list_view));
1096                    menu_set_sensitive(prefs_template_popup_factory, "/Delete", (row > 0));
1097                    menu_set_sensitive(prefs_template_popup_factory, "/Duplicate", (row > 0));
1098
1099                    /* grey out seom popup menu items if there is no row
1100                           (not counting the (New) one at row 0) */
1101                    non_empty = gtk_tree_model_get_iter_first(model, &iter);
1102                    if (non_empty)
1103                            non_empty = gtk_tree_model_iter_next(model, &iter);
1104                    menu_set_sensitive(prefs_template_popup_factory, "/Delete all", non_empty);
1105
1106                    gtk_menu_popup(GTK_MENU(prefs_template_popup_menu), 
1107                                           NULL, NULL, NULL, NULL, 
1108                                           event->button, event->time);
1109            }
1110    }
1111    return FALSE;
1112 }
1113
1114 static gboolean prefs_template_list_popup_menu(GtkWidget *widget, gpointer data)
1115 {
1116    GtkTreeView *list_view = (GtkTreeView *)data;
1117    GdkEventButton event;
1118    
1119    event.button = 3;
1120    event.time = gtk_get_current_event_time();
1121    
1122    prefs_template_list_btn_pressed(NULL, &event, list_view);
1123
1124    return TRUE;
1125 }
1126
1127 static GtkWidget *prefs_template_list_view_create(void)
1128 {
1129         GtkTreeView *list_view;
1130         GtkTreeSelection *selector;
1131         GtkTreeModel *model;
1132
1133         model = GTK_TREE_MODEL(prefs_template_create_data_store());
1134         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
1135         g_object_unref(model);  
1136
1137 #ifndef MAEMO
1138         g_signal_connect(G_OBJECT(list_view), "popup-menu",
1139                          G_CALLBACK(prefs_template_list_popup_menu), list_view);
1140 #else
1141         gtk_widget_tap_and_hold_setup(GTK_WIDGET(list_view), NULL, NULL,
1142                         GTK_TAP_AND_HOLD_NONE | GTK_TAP_AND_HOLD_NO_INTERNALS);
1143         g_signal_connect(G_OBJECT(list_view), "tap-and-hold",
1144                          G_CALLBACK(prefs_template_list_popup_menu), list_view);
1145 #endif
1146         g_signal_connect(G_OBJECT(list_view), "button-press-event",
1147                         G_CALLBACK(prefs_template_list_btn_pressed), list_view);
1148         
1149         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
1150         gtk_tree_view_set_reorderable(list_view, TRUE);
1151
1152         selector = gtk_tree_view_get_selection(list_view);
1153         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1154
1155         /* create the columns */
1156         prefs_template_create_list_view_columns(GTK_WIDGET(list_view));
1157
1158         return GTK_WIDGET(list_view);
1159 }
1160
1161 static void prefs_template_create_list_view_columns(GtkWidget *list_view)
1162 {
1163         GtkTreeViewColumn *column;
1164         GtkCellRenderer *renderer;
1165
1166         renderer = gtk_cell_renderer_text_new();
1167         column = gtk_tree_view_column_new_with_attributes
1168                         (_("Current templates"),
1169                          renderer,
1170                          "text", TEMPL_TEXT,
1171                          NULL);
1172         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
1173 }
1174
1175  /*!
1176  *\brief        Triggered when a row has to be selected
1177  */
1178 static void prefs_template_select_row(GtkTreeView *list_view, GtkTreePath *path)
1179 {
1180         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1181         GtkTreeSelection *selection;
1182         Template *tmpl;
1183         Template tmpl_def;
1184         GtkTextBuffer *buffer;
1185         GtkTextIter iter;
1186         GtkTreeIter titer;
1187
1188         if (!model || !path || !gtk_tree_model_get_iter(model, &titer, path))
1189                 return;
1190
1191         /* select row */
1192         selection = gtk_tree_view_get_selection(list_view);
1193         gtk_tree_selection_select_path(selection, path);
1194
1195         tmpl_def.name = _("Template");
1196         tmpl_def.subject = "";
1197         tmpl_def.from = "";
1198         tmpl_def.to = "";
1199         tmpl_def.cc = "";
1200         tmpl_def.bcc = "";      
1201         tmpl_def.value = "";
1202
1203         gtk_tree_model_get(model, &titer, TEMPL_DATA, &tmpl, -1);
1204         if (!tmpl)
1205                 tmpl = &tmpl_def;
1206
1207         gtk_entry_set_text(GTK_ENTRY(templates.entry_name), tmpl->name);
1208         gtk_entry_set_text(GTK_ENTRY(templates.entry_from),
1209                            tmpl->from ? tmpl->from : "");
1210         gtk_entry_set_text(GTK_ENTRY(templates.entry_to),
1211                            tmpl->to ? tmpl->to : "");
1212         gtk_entry_set_text(GTK_ENTRY(templates.entry_cc),
1213                            tmpl->cc ? tmpl->cc : "");
1214         gtk_entry_set_text(GTK_ENTRY(templates.entry_bcc),
1215                            tmpl->bcc ? tmpl->bcc : "");                 
1216         gtk_entry_set_text(GTK_ENTRY(templates.entry_subject),
1217                            tmpl->subject ? tmpl->subject : "");
1218         
1219         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
1220         gtk_text_buffer_set_text(buffer, "", -1);
1221         gtk_text_buffer_get_start_iter(buffer, &iter);
1222         gtk_text_buffer_insert(buffer, &iter, tmpl->value, -1);
1223 }