30a5b1c77a606432eb8fcdd95249b6b5db6ffbf1
[claws.git] / src / prefs_template.c
1 /*
2  * Sylpheed templates subsystem 
3  * Copyright (C) 2001 Alexander Barinov
4  * Copyright (C) 2001-2002 Hiroyuki Yamamoto
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 2 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, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #include "defs.h"
22
23 #include <glib.h>
24 #include <gtk/gtk.h>
25 #include <gdk/gdkkeysyms.h>
26 #include <string.h>
27 #include <dirent.h>
28 #include <sys/stat.h>
29
30 #include "intl.h"
31 #include "template.h"
32 #include "main.h"
33 #include "inc.h"
34 #include "utils.h"
35 #include "gtkutils.h"
36 #include "alertpanel.h"
37 #include "manage_window.h"
38 #include "compose.h"
39 #include "addr_compl.h"
40 #include "quote_fmt.h"
41
42 enum {
43         TEMPL_TEXT,
44         TEMPL_DATA,
45         TEMPL_AUTO_DATA,        /*!< auto pointer */
46         N_TEMPL_COLUMNS
47 };
48
49 static struct Templates {
50         GtkWidget *window;
51         GtkWidget *ok_btn;
52         GtkWidget *list_view;
53         GtkWidget *entry_name;
54         GtkWidget *entry_subject;
55         GtkWidget *entry_to;
56         GtkWidget *entry_cc;    
57         GtkWidget *entry_bcc;
58         GtkWidget *text_value;
59 } templates;
60
61 /* widget creating functions */
62 static void prefs_template_window_create        (void);
63 static void prefs_template_window_setup         (void);
64 static void prefs_template_clear                (void);
65
66 static GSList *prefs_template_get_list          (void);
67
68 /* callbacks */
69 static gint prefs_template_deleted_cb           (GtkWidget      *widget,
70                                                  GdkEventAny    *event,
71                                                  gpointer        data);
72 static gboolean prefs_template_key_pressed_cb   (GtkWidget      *widget,
73                                                  GdkEventKey    *event,
74                                                  gpointer        data);
75 static void prefs_template_cancel_cb            (void);
76 static void prefs_template_ok_cb                (void);
77 static void prefs_template_register_cb          (void);
78 static void prefs_template_substitute_cb        (void);
79 static void prefs_template_delete_cb            (void);
80
81 static GtkListStore* prefs_template_create_data_store   (void);
82 static void prefs_template_list_view_insert_template    (GtkWidget *list_view,
83                                                          GtkTreeIter *row_iter,
84                                                          const gchar *template,
85                                                          Template *data);
86 static GtkWidget *prefs_template_list_view_create       (void);
87 static void prefs_template_create_list_view_columns     (GtkWidget *list_view);
88 static gboolean prefs_template_selected                 (GtkTreeSelection *selector,
89                                                          GtkTreeModel *model, 
90                                                          GtkTreePath *path,
91                                                          gboolean currently_selected,
92                                                          gpointer data);
93
94 /* Called from mainwindow.c */
95 void prefs_template_open(void)
96 {
97         inc_lock();
98
99         if (!templates.window)
100                 prefs_template_window_create();
101
102         prefs_template_window_setup();
103         gtk_widget_show(templates.window);
104 }
105
106 #define ADD_ENTRY(entry, str, row) \
107 { \
108         label1 = gtk_label_new(str); \
109         gtk_widget_show(label1); \
110         gtk_table_attach(GTK_TABLE(table), label1, 0, 1, row, (row + 1), \
111                          GTK_FILL, 0, 0, 0); \
112         gtk_misc_set_alignment(GTK_MISC(label1), 1, 0.5); \
113  \
114         entry = gtk_entry_new(); \
115         gtk_widget_show(entry); \
116         gtk_table_attach(GTK_TABLE(table), entry, 1, 2, row, (row + 1), \
117                          GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0); \
118 }
119
120 static void prefs_template_window_create(void)
121 {
122         /* window structure ;) */
123         GtkWidget *window;
124         GtkWidget   *vpaned;
125         GtkWidget     *vbox1;
126         GtkWidget       *hbox1;
127         GtkWidget         *label1;
128         GtkWidget         *entry_name;
129         GtkWidget       *table;
130         GtkWidget         *entry_to;
131         GtkWidget         *entry_cc;
132         GtkWidget         *entry_bcc;           
133         GtkWidget         *entry_subject;
134         GtkWidget       *scroll2;
135         GtkWidget         *text_value;
136         GtkWidget     *vbox2;
137         GtkWidget       *hbox2;
138         GtkWidget         *arrow1;
139         GtkWidget         *hbox3;
140         GtkWidget           *reg_btn;
141         GtkWidget           *subst_btn;
142         GtkWidget           *del_btn;
143         GtkWidget         *desc_btn;
144         GtkWidget       *scroll1;
145         GtkWidget         *list_view;
146         GtkWidget       *confirm_area;
147         GtkWidget         *ok_btn;
148         GtkWidget         *cancel_btn;
149
150         /* main window */
151         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
152         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
153         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
154         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
155         gtk_window_set_default_size(GTK_WINDOW(window), 400, -1);
156
157         /* vpaned to separate template settings from templates list */
158         vpaned = gtk_vpaned_new();
159         gtk_widget_show(vpaned);
160         gtk_container_add(GTK_CONTAINER(window), vpaned);
161
162         /* vbox to handle template name and content */
163         vbox1 = gtk_vbox_new(FALSE, 6);
164         gtk_widget_show(vbox1);
165         gtk_container_set_border_width(GTK_CONTAINER(vbox1), 8);
166         gtk_paned_pack1(GTK_PANED(vpaned), vbox1, FALSE, FALSE);
167
168         hbox1 = gtk_hbox_new(FALSE, 8);
169         gtk_widget_show(hbox1);
170         gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, FALSE, 0);
171
172         label1 = gtk_label_new(_("Template name"));
173         gtk_widget_show(label1);
174         gtk_box_pack_start(GTK_BOX(hbox1), label1, FALSE, FALSE, 0);
175
176         entry_name = gtk_entry_new();
177         gtk_widget_show(entry_name);
178         gtk_box_pack_start(GTK_BOX(hbox1), entry_name, TRUE, TRUE, 0);
179
180         /* table for headers */
181         table = gtk_table_new(2, 2, FALSE);
182         gtk_widget_show(table);
183         gtk_box_pack_start(GTK_BOX(vbox1), table, FALSE, FALSE, 0);
184         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
185         gtk_table_set_col_spacings(GTK_TABLE(table), 4);
186
187         ADD_ENTRY(entry_to, _("To:"), 0);
188         address_completion_register_entry(GTK_ENTRY(entry_to));
189         ADD_ENTRY(entry_cc, _("Cc:"), 1)
190         ADD_ENTRY(entry_bcc, _("Bcc:"), 2)      
191         ADD_ENTRY(entry_subject, _("Subject:"), 3);
192
193 #undef ADD_ENTRY
194
195         /* template content */
196         scroll2 = gtk_scrolled_window_new(NULL, NULL);
197         gtk_widget_show(scroll2);
198         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll2),
199                                        GTK_POLICY_NEVER,
200                                        GTK_POLICY_AUTOMATIC);
201         gtk_box_pack_start(GTK_BOX(vbox1), scroll2, TRUE, TRUE, 0);
202
203         text_value = gtk_text_view_new();
204         gtk_widget_show(text_value);
205         gtk_widget_set_size_request(text_value, -1, 120);
206         gtk_container_add(GTK_CONTAINER(scroll2), text_value);
207         gtk_text_view_set_editable(GTK_TEXT_VIEW(text_value), TRUE);
208         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text_value), GTK_WRAP_WORD);
209
210         /* vbox for buttons and templates list */
211         vbox2 = gtk_vbox_new(FALSE, 6);
212         gtk_widget_show(vbox2);
213         gtk_container_set_border_width(GTK_CONTAINER(vbox2), 8);
214         gtk_paned_pack2(GTK_PANED(vpaned), vbox2, TRUE, FALSE);
215
216         /* register | substitute | delete */
217         hbox2 = gtk_hbox_new(FALSE, 4);
218         gtk_widget_show(hbox2);
219         gtk_box_pack_start(GTK_BOX(vbox2), hbox2, FALSE, FALSE, 0);
220
221         arrow1 = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
222         gtk_widget_show(arrow1);
223         gtk_box_pack_start(GTK_BOX(hbox2), arrow1, FALSE, FALSE, 0);
224         gtk_widget_set_size_request(arrow1, -1, 16);
225
226         hbox3 = gtk_hbox_new(TRUE, 4);
227         gtk_widget_show(hbox3);
228         gtk_box_pack_start(GTK_BOX(hbox2), hbox3, FALSE, FALSE, 0);
229
230         reg_btn = gtk_button_new_from_stock(GTK_STOCK_ADD);
231         gtk_widget_show(reg_btn);
232         gtk_box_pack_start(GTK_BOX(hbox3), reg_btn, FALSE, TRUE, 0);
233         g_signal_connect(G_OBJECT (reg_btn), "clicked",
234                          G_CALLBACK (prefs_template_register_cb), NULL);
235
236         subst_btn = gtk_button_new_with_label(_("  Replace  "));
237         gtk_widget_show(subst_btn);
238         gtk_box_pack_start(GTK_BOX(hbox3), subst_btn, FALSE, TRUE, 0);
239         g_signal_connect(G_OBJECT(subst_btn), "clicked",
240                          G_CALLBACK(prefs_template_substitute_cb),
241                          NULL);
242
243         del_btn = gtk_button_new_from_stock(GTK_STOCK_REMOVE);
244         gtk_widget_show(del_btn);
245         gtk_box_pack_start(GTK_BOX(hbox3), del_btn, FALSE, TRUE, 0);
246         g_signal_connect(G_OBJECT(del_btn), "clicked",
247                          G_CALLBACK(prefs_template_delete_cb), NULL);
248
249         desc_btn = gtk_button_new_with_label(_(" Symbols "));
250         gtk_widget_show(desc_btn);
251         gtk_box_pack_end(GTK_BOX(hbox2), desc_btn, FALSE, FALSE, 0);
252         g_signal_connect(G_OBJECT(desc_btn), "clicked",
253                          G_CALLBACK(quote_fmt_quote_description), NULL);
254
255         /* templates list */
256         scroll1 = gtk_scrolled_window_new(NULL, NULL);
257         gtk_widget_show(scroll1);
258         gtk_box_pack_start(GTK_BOX(vbox2), scroll1, TRUE, TRUE, 0);
259         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll1),
260                                        GTK_POLICY_AUTOMATIC,
261                                        GTK_POLICY_AUTOMATIC);
262                                        
263         list_view = prefs_template_list_view_create();
264         gtk_widget_show(list_view);
265         gtk_widget_set_size_request(scroll1, -1, 140);
266         gtk_container_add(GTK_CONTAINER(scroll1), list_view);
267
268         /* ok | cancel */
269         gtkut_button_set_create_stock(&confirm_area, &ok_btn, GTK_STOCK_OK,
270                                       &cancel_btn, GTK_STOCK_CANCEL, NULL, NULL);
271         gtk_widget_show(confirm_area);
272         gtk_box_pack_end(GTK_BOX(vbox2), confirm_area, FALSE, FALSE, 0);
273         gtk_widget_grab_default(ok_btn);
274
275         gtk_window_set_title(GTK_WINDOW(window), _("Template configuration"));
276
277         g_signal_connect(G_OBJECT(window), "delete_event",
278                          G_CALLBACK(prefs_template_deleted_cb), NULL);
279         g_signal_connect(G_OBJECT(window), "key_press_event",
280                          G_CALLBACK(prefs_template_key_pressed_cb), NULL);
281         MANAGE_WINDOW_SIGNALS_CONNECT(window);
282         g_signal_connect(G_OBJECT(ok_btn), "clicked",
283                          G_CALLBACK(prefs_template_ok_cb), NULL);
284         g_signal_connect(G_OBJECT(cancel_btn), "clicked",
285                          G_CALLBACK(prefs_template_cancel_cb), NULL);
286
287         address_completion_start(window);
288
289         templates.window = window;
290         templates.ok_btn = ok_btn;
291         templates.list_view = list_view;
292         templates.entry_name = entry_name;
293         templates.entry_subject = entry_subject;
294         templates.entry_to = entry_to;
295         templates.entry_cc = entry_cc;
296         templates.entry_bcc = entry_bcc;        
297         templates.text_value = text_value;
298 }
299
300 static void prefs_template_window_setup(void)
301 {
302         GSList *tmpl_list;
303         GSList *cur;
304         gchar *title[1];
305         gint row;
306         Template *tmpl;
307         GtkListStore *store;
308
309         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
310                                 (templates.list_view)));
311
312         manage_window_set_transient(GTK_WINDOW(templates.window));
313         gtk_widget_grab_focus(templates.ok_btn);
314
315         gtk_list_store_clear(store);
316
317         prefs_template_list_view_insert_template(templates.list_view,
318                                                  NULL, _("(New)"),
319                                                  NULL);
320         
321         tmpl_list = template_read_config();
322
323         for (cur = tmpl_list; cur != NULL; cur = cur->next) {
324                 tmpl = (Template *)cur->data;
325                 prefs_template_list_view_insert_template(templates.list_view,
326                                                          NULL, tmpl->name, 
327                                                          tmpl);
328         }
329
330         g_slist_free(tmpl_list);
331 }
332
333 static void prefs_template_clear(void)
334 {
335         GtkListStore *store;
336
337         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
338                                 (templates.list_view)));
339         gtk_list_store_clear(store);
340 }
341
342 static gint prefs_template_deleted_cb(GtkWidget *widget, GdkEventAny *event,
343                                       gpointer data)
344 {
345         prefs_template_cancel_cb();
346         return TRUE;
347 }
348
349 static gboolean prefs_template_key_pressed_cb(GtkWidget *widget,
350                                               GdkEventKey *event, gpointer data)
351 {
352         if (event && event->keyval == GDK_Escape)
353                 prefs_template_cancel_cb();
354         return FALSE;
355 }
356
357 static void prefs_template_ok_cb(void)
358 {
359         GSList *tmpl_list;
360
361         tmpl_list = prefs_template_get_list();
362         template_set_config(tmpl_list);
363         compose_reflect_prefs_all();
364         prefs_template_clear();
365         gtk_widget_hide(templates.window);
366         inc_unlock();
367 }
368
369 static void prefs_template_cancel_cb(void)
370 {
371         prefs_template_clear();
372         gtk_widget_hide(templates.window);
373         inc_unlock();
374 }
375
376 /*!
377  *\brief        Request list for storage. New list is owned
378  *              by template.c...
379  */
380 static GSList *prefs_template_get_list(void)
381 {
382         GSList *tmpl_list = NULL;
383         Template *tmpl;
384         GtkTreeModel *model;
385         GtkTreeIter iter;
386
387         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
388         if (!gtk_tree_model_get_iter_first(model, &iter))
389                 return NULL;
390
391         do {
392                 gtk_tree_model_get(model, &iter,
393                                    TEMPL_DATA, &tmpl,
394                                    -1);
395                 
396                 if (tmpl) {
397                         Template *ntmpl;
398                         
399                         ntmpl = g_new(Template, 1);
400                         ntmpl->name    = tmpl->name && *(tmpl->name) 
401                                          ? g_strdup(tmpl->name) 
402                                          : NULL;
403                         ntmpl->subject = tmpl->subject && *(tmpl->subject) 
404                                          ? g_strdup(tmpl->subject) 
405                                          : NULL;
406                         ntmpl->to      = tmpl->to && *(tmpl->to)
407                                          ? g_strdup(tmpl->to)
408                                          : NULL;
409                         ntmpl->cc      = tmpl->cc && *(tmpl->cc)
410                                          ? g_strdup(tmpl->cc)
411                                          : NULL;
412                         ntmpl->bcc     = tmpl->bcc && *(tmpl->bcc)
413                                          ? g_strdup(tmpl->bcc)
414                                          : NULL;        
415                         ntmpl->value   = tmpl->value && *(tmpl->value)
416                                          ? g_strdup(tmpl->value)
417                                          : NULL;
418                         tmpl_list = g_slist_append(tmpl_list, ntmpl);
419                 }                       
420         
421         } while (gtk_tree_model_iter_next(model, &iter)); 
422
423         return tmpl_list;
424 }
425
426 static void prefs_template_list_view_set_row(GtkTreeIter *row)
427 {
428         Template *tmpl;
429         Template *tmp_tmpl;
430         gchar *name;
431         gchar *subject;
432         gchar *to;
433         gchar *cc;
434         gchar *bcc;     
435         gchar *value;
436         GtkTextBuffer *buffer;
437         GtkTextIter start, end;
438         GtkTreeModel *model;
439
440         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
441
442         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
443         gtk_text_buffer_get_start_iter(buffer, &start);
444         gtk_text_buffer_get_iter_at_offset(buffer, &end, -1);
445         value = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
446
447         if (value && *value != '\0') {
448                 gchar *parsed_buf;
449                 MsgInfo dummyinfo;
450
451                 memset(&dummyinfo, 0, sizeof(MsgInfo));
452                 quote_fmt_init(&dummyinfo, NULL, NULL);
453                 quote_fmt_scan_string(value);
454                 quote_fmt_parse();
455                 parsed_buf = quote_fmt_get_buffer();
456                 if (!parsed_buf) {
457                         alertpanel_error(_("Template format error."));
458                         g_free(value);
459                         return;
460                 }
461         }
462
463         name = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_name),
464                                       0, -1);
465         subject = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_subject),
466                                          0, -1);
467         to = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_to),
468                                     0, -1);
469         cc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_cc),
470                                     0, -1);
471         bcc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_bcc),
472                                     0, -1);
473
474         if (subject && *subject == '\0') {
475                 g_free(subject);
476                 subject = NULL;
477         }
478         if (to && *to == '\0') {
479                 g_free(to);
480                 to = NULL;
481         }
482         if (cc && *cc == '\0') {
483                 g_free(cc);
484                 cc = NULL;
485         }
486         if (bcc && *bcc == '\0') {
487                 g_free(bcc);
488                 bcc = NULL;
489         }
490         
491         tmpl = g_new(Template, 1);
492         tmpl->name = name;
493         tmpl->subject = subject;
494         tmpl->to = to;
495         tmpl->cc = cc;
496         tmpl->bcc = bcc;        
497         tmpl->value = value;
498
499         prefs_template_list_view_insert_template(templates.list_view,
500                                                  row, tmpl->name, tmpl);
501 }
502
503 static void prefs_template_register_cb(void)
504 {
505         prefs_template_list_view_set_row(NULL);
506 }
507
508 static void prefs_template_substitute_cb(void)
509 {
510         Template *tmpl;
511         GtkTreeIter row;
512         GtkTreeSelection *selection;
513         GtkTreeModel *model;
514
515         selection = gtk_tree_view_get_selection
516                         (GTK_TREE_VIEW(templates.list_view));
517         
518         if (!gtk_tree_selection_get_selected(selection, &model, &row))
519                 return;
520
521         gtk_tree_model_get(model, &row, 
522                            TEMPL_DATA, &tmpl,
523                            -1);
524
525         if (!tmpl) return;
526
527         prefs_template_list_view_set_row(&row);
528 }
529
530 static void prefs_template_delete_cb(void)
531 {
532         Template *tmpl;
533         GtkTreeIter row;
534         GtkTreeSelection *selection;
535         GtkTreeModel *model;
536
537         selection = gtk_tree_view_get_selection
538                         (GTK_TREE_VIEW(templates.list_view));
539         
540         if (!gtk_tree_selection_get_selected(selection, &model, &row))
541                 return;
542
543         gtk_tree_model_get(model, &row, 
544                            TEMPL_DATA, &tmpl,
545                            -1);
546
547         if (!tmpl) 
548                 return;
549
550         if (alertpanel(_("Delete template"),
551                        _("Do you really want to delete this template?"),
552                        _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
553                 return;
554
555         gtk_list_store_remove(GTK_LIST_STORE(model), &row);             
556 }
557
558 static GtkListStore* prefs_template_create_data_store(void)
559 {
560         return gtk_list_store_new(N_TEMPL_COLUMNS,
561                                   G_TYPE_STRING,        
562                                   G_TYPE_POINTER,
563                                   G_TYPE_AUTO_POINTER,
564                                   -1);
565 }
566
567 static void prefs_template_list_view_insert_template(GtkWidget *list_view,
568                                                      GtkTreeIter *row_iter,
569                                                      const gchar *template,
570                                                      Template *data)
571 {
572         GtkTreeIter iter;
573         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
574                                         (GTK_TREE_VIEW(list_view)));
575         GAuto *auto_data;                                       
576
577         if (row_iter == NULL) {
578                 /* append new */
579                 gtk_list_store_append(list_store, &iter);
580         } else {
581                 iter = *row_iter;
582         }
583
584         auto_data = g_auto_pointer_new_with_free(data, (GFreeFunc) template_free);  
585
586         /* if replacing data in an existing row, the auto pointer takes care
587          * of destroying the Template data */
588         gtk_list_store_set(list_store, &iter,
589                            TEMPL_TEXT, template,
590                            TEMPL_DATA, data,
591                            TEMPL_AUTO_DATA, auto_data,
592                            -1);
593
594         g_auto_pointer_free(auto_data);                    
595 }
596
597 static GtkWidget *prefs_template_list_view_create(void)
598 {
599         GtkTreeView *list_view;
600         GtkTreeSelection *selector;
601         GtkTreeModel *model;
602
603         model = GTK_TREE_MODEL(prefs_template_create_data_store());
604         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
605         g_object_unref(model);  
606         
607         gtk_tree_view_set_rules_hint(list_view, TRUE);
608         
609         selector = gtk_tree_view_get_selection(list_view);
610         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
611         gtk_tree_selection_set_select_function(selector, prefs_template_selected,
612                                                NULL, NULL);
613
614         /* create the columns */
615         prefs_template_create_list_view_columns(GTK_WIDGET(list_view));
616
617         return GTK_WIDGET(list_view);
618 }
619
620 static void prefs_template_create_list_view_columns(GtkWidget *list_view)
621 {
622         GtkTreeViewColumn *column;
623         GtkCellRenderer *renderer;
624
625         renderer = gtk_cell_renderer_text_new();
626         column = gtk_tree_view_column_new_with_attributes
627                         (_("Current templates"),
628                          renderer,
629                          "text", TEMPL_TEXT,
630                          NULL);
631         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
632 }
633
634 static gboolean prefs_template_selected(GtkTreeSelection *selector,
635                                         GtkTreeModel *model, 
636                                         GtkTreePath *path,
637                                         gboolean currently_selected,
638                                         gpointer data)
639 {
640         Template *tmpl;
641         Template tmpl_def;
642         GtkTextBuffer *buffer;
643         GtkTextIter iter;
644         GtkTreeIter titer;
645
646         if (currently_selected)
647                 return TRUE;
648
649         if (!gtk_tree_model_get_iter(model, &titer, path))
650                 return TRUE;
651
652         tmpl_def.name = _("Template");
653         tmpl_def.subject = "";
654         tmpl_def.to = "";
655         tmpl_def.cc = "";
656         tmpl_def.bcc = "";      
657         tmpl_def.value = "";
658
659         gtk_tree_model_get(model, &titer,
660                            TEMPL_DATA, &tmpl,
661                            -1);
662
663         if (!tmpl) 
664                 tmpl = &tmpl_def;
665
666         gtk_entry_set_text(GTK_ENTRY(templates.entry_name), tmpl->name);
667         gtk_entry_set_text(GTK_ENTRY(templates.entry_to),
668                            tmpl->to ? tmpl->to : "");
669         gtk_entry_set_text(GTK_ENTRY(templates.entry_cc),
670                            tmpl->cc ? tmpl->cc : "");
671         gtk_entry_set_text(GTK_ENTRY(templates.entry_bcc),
672                            tmpl->bcc ? tmpl->bcc : "");                 
673         gtk_entry_set_text(GTK_ENTRY(templates.entry_subject),
674                            tmpl->subject ? tmpl->subject : "");
675         
676         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
677         gtk_text_buffer_set_text(buffer, "", -1);
678         gtk_text_buffer_get_start_iter(buffer, &iter);
679         gtk_text_buffer_insert(buffer, &iter, tmpl->value, -1);
680
681         return TRUE;
682 }
683
684