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