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