2005-07-11 [paul] 1.9.12cvs34
[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_AUTOMATIC,
203                                        GTK_POLICY_AUTOMATIC);
204         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll2),
205                                             GTK_SHADOW_IN);
206         gtk_box_pack_start(GTK_BOX(vbox1), scroll2, TRUE, TRUE, 0);
207
208         text_value = gtk_text_view_new();
209         gtk_widget_show(text_value);
210         gtk_widget_set_size_request(text_value, -1, 120);
211         gtk_container_add(GTK_CONTAINER(scroll2), text_value);
212         gtk_text_view_set_editable(GTK_TEXT_VIEW(text_value), TRUE);
213         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text_value), GTK_WRAP_WORD);
214
215         /* vbox for buttons and templates list */
216         vbox2 = gtk_vbox_new(FALSE, 6);
217         gtk_widget_show(vbox2);
218         gtk_container_set_border_width(GTK_CONTAINER(vbox2), 8);
219         gtk_paned_pack2(GTK_PANED(vpaned), vbox2, TRUE, FALSE);
220
221         /* register | substitute | delete */
222         hbox2 = gtk_hbox_new(FALSE, 4);
223         gtk_widget_show(hbox2);
224         gtk_box_pack_start(GTK_BOX(vbox2), hbox2, FALSE, FALSE, 0);
225
226         arrow1 = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
227         gtk_widget_show(arrow1);
228         gtk_box_pack_start(GTK_BOX(hbox2), arrow1, FALSE, FALSE, 0);
229         gtk_widget_set_size_request(arrow1, -1, 16);
230
231         hbox3 = gtk_hbox_new(TRUE, 4);
232         gtk_widget_show(hbox3);
233         gtk_box_pack_start(GTK_BOX(hbox2), hbox3, FALSE, FALSE, 0);
234
235         reg_btn = gtk_button_new_from_stock(GTK_STOCK_ADD);
236         gtk_widget_show(reg_btn);
237         gtk_box_pack_start(GTK_BOX(hbox3), reg_btn, FALSE, TRUE, 0);
238         g_signal_connect(G_OBJECT (reg_btn), "clicked",
239                          G_CALLBACK (prefs_template_register_cb), NULL);
240
241         subst_btn = gtk_button_new_with_label(_("  Replace  "));
242         gtk_widget_show(subst_btn);
243         gtk_box_pack_start(GTK_BOX(hbox3), subst_btn, FALSE, TRUE, 0);
244         g_signal_connect(G_OBJECT(subst_btn), "clicked",
245                          G_CALLBACK(prefs_template_substitute_cb),
246                          NULL);
247
248         del_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
249         gtk_widget_show(del_btn);
250         gtk_box_pack_start(GTK_BOX(hbox3), del_btn, FALSE, TRUE, 0);
251         g_signal_connect(G_OBJECT(del_btn), "clicked",
252                          G_CALLBACK(prefs_template_delete_cb), NULL);
253
254         desc_btn = gtk_button_new_with_label(_(" Symbols "));
255         gtk_widget_show(desc_btn);
256         gtk_box_pack_end(GTK_BOX(hbox2), desc_btn, FALSE, FALSE, 0);
257         g_signal_connect(G_OBJECT(desc_btn), "clicked",
258                          G_CALLBACK(quote_fmt_quote_description), NULL);
259
260         /* templates list */
261         scroll1 = gtk_scrolled_window_new(NULL, NULL);
262         gtk_widget_show(scroll1);
263         gtk_box_pack_start(GTK_BOX(vbox2), scroll1, TRUE, TRUE, 0);
264         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll1),
265                                        GTK_POLICY_AUTOMATIC,
266                                        GTK_POLICY_AUTOMATIC);
267                                        
268         list_view = prefs_template_list_view_create();
269         gtk_widget_show(list_view);
270         gtk_widget_set_size_request(scroll1, -1, 140);
271         gtk_container_add(GTK_CONTAINER(scroll1), list_view);
272
273         /* ok | cancel */
274         gtkut_stock_button_set_create(&confirm_area, &ok_btn, GTK_STOCK_OK,
275                                 &cancel_btn, GTK_STOCK_CANCEL, NULL, NULL);
276         gtk_widget_show(confirm_area);
277         gtk_box_pack_end(GTK_BOX(vbox2), confirm_area, FALSE, FALSE, 0);
278         gtk_widget_grab_default(ok_btn);
279
280         gtk_window_set_title(GTK_WINDOW(window), _("Template configuration"));
281
282         g_signal_connect(G_OBJECT(window), "delete_event",
283                          G_CALLBACK(prefs_template_deleted_cb), NULL);
284         g_signal_connect(G_OBJECT(window), "key_press_event",
285                          G_CALLBACK(prefs_template_key_pressed_cb), NULL);
286         MANAGE_WINDOW_SIGNALS_CONNECT(window);
287         g_signal_connect(G_OBJECT(ok_btn), "clicked",
288                          G_CALLBACK(prefs_template_ok_cb), NULL);
289         g_signal_connect(G_OBJECT(cancel_btn), "clicked",
290                          G_CALLBACK(prefs_template_cancel_cb), NULL);
291
292         address_completion_start(window);
293
294         templates.window = window;
295         templates.ok_btn = ok_btn;
296         templates.list_view = list_view;
297         templates.entry_name = entry_name;
298         templates.entry_subject = entry_subject;
299         templates.entry_to = entry_to;
300         templates.entry_cc = entry_cc;
301         templates.entry_bcc = entry_bcc;        
302         templates.text_value = text_value;
303 }
304
305 static void prefs_template_window_setup(void)
306 {
307         GSList *tmpl_list;
308         GSList *cur;
309         Template *tmpl;
310         GtkListStore *store;
311
312         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
313                                 (templates.list_view)));
314
315         manage_window_set_transient(GTK_WINDOW(templates.window));
316         gtk_widget_grab_focus(templates.ok_btn);
317
318         gtk_list_store_clear(store);
319
320         prefs_template_list_view_insert_template(templates.list_view,
321                                                  NULL, _("(New)"),
322                                                  NULL);
323         
324         tmpl_list = template_read_config();
325
326         for (cur = tmpl_list; cur != NULL; cur = cur->next) {
327                 tmpl = (Template *)cur->data;
328                 prefs_template_list_view_insert_template(templates.list_view,
329                                                          NULL, tmpl->name, 
330                                                          tmpl);
331         }
332
333         g_slist_free(tmpl_list);
334 }
335
336 static void prefs_template_clear(void)
337 {
338         GtkListStore *store;
339
340         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
341                                 (templates.list_view)));
342         gtk_list_store_clear(store);
343 }
344
345 static gint prefs_template_deleted_cb(GtkWidget *widget, GdkEventAny *event,
346                                       gpointer data)
347 {
348         prefs_template_cancel_cb();
349         return TRUE;
350 }
351
352 static gboolean prefs_template_key_pressed_cb(GtkWidget *widget,
353                                               GdkEventKey *event, gpointer data)
354 {
355         if (event && event->keyval == GDK_Escape)
356                 prefs_template_cancel_cb();
357         else {
358                 GtkWidget *focused = gtkut_get_focused_child(
359                                         GTK_CONTAINER(widget));
360                 if (focused && GTK_IS_EDITABLE(focused)) {
361                         modified = TRUE;
362                 }
363         }
364         return FALSE;
365 }
366
367 static void prefs_template_ok_cb(void)
368 {
369         GSList *tmpl_list;
370
371         if (modified && alertpanel(_("Entry not saved"),
372                                  _("The entry was not saved. Close anyway?"),
373                                  _("Yes"), _("No"), NULL) != G_ALERTDEFAULT) {
374                 return;
375         }
376         modified = FALSE;
377         tmpl_list = prefs_template_get_list();
378         template_set_config(tmpl_list);
379         compose_reflect_prefs_all();
380         prefs_template_clear();
381         gtk_widget_hide(templates.window);
382         inc_unlock();
383 }
384
385 static void prefs_template_cancel_cb(void)
386 {
387         if (modified && alertpanel(_("Entry not saved"),
388                                  _("The entry was not saved. Close anyway?"),
389                                  _("Yes"), _("No"), NULL) != G_ALERTDEFAULT) {
390                 return;
391         }
392         modified = FALSE;
393         prefs_template_clear();
394         gtk_widget_hide(templates.window);
395         inc_unlock();
396 }
397
398 /*!
399  *\brief        Request list for storage. New list is owned
400  *              by template.c...
401  */
402 static GSList *prefs_template_get_list(void)
403 {
404         GSList *tmpl_list = NULL;
405         Template *tmpl;
406         GtkTreeModel *model;
407         GtkTreeIter iter;
408
409         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
410         if (!gtk_tree_model_get_iter_first(model, &iter))
411                 return NULL;
412
413         do {
414                 gtk_tree_model_get(model, &iter,
415                                    TEMPL_DATA, &tmpl,
416                                    -1);
417                 
418                 if (tmpl) {
419                         Template *ntmpl;
420                         
421                         ntmpl = g_new(Template, 1);
422                         ntmpl->name    = tmpl->name && *(tmpl->name) 
423                                          ? g_strdup(tmpl->name) 
424                                          : NULL;
425                         ntmpl->subject = tmpl->subject && *(tmpl->subject) 
426                                          ? g_strdup(tmpl->subject) 
427                                          : NULL;
428                         ntmpl->to      = tmpl->to && *(tmpl->to)
429                                          ? g_strdup(tmpl->to)
430                                          : NULL;
431                         ntmpl->cc      = tmpl->cc && *(tmpl->cc)
432                                          ? g_strdup(tmpl->cc)
433                                          : NULL;
434                         ntmpl->bcc     = tmpl->bcc && *(tmpl->bcc)
435                                          ? g_strdup(tmpl->bcc)
436                                          : NULL;        
437                         ntmpl->value   = tmpl->value && *(tmpl->value)
438                                          ? g_strdup(tmpl->value)
439                                          : NULL;
440                         tmpl_list = g_slist_append(tmpl_list, ntmpl);
441                 }                       
442         
443         } while (gtk_tree_model_iter_next(model, &iter)); 
444
445         return tmpl_list;
446 }
447
448 static void prefs_template_list_view_set_row(GtkTreeIter *row)
449 {
450         Template *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, prefs_common.enable_rules_hint);
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