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