2006-10-10 [wwp] 2.5.3cvs29
[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 "prefs_gtk.h"
34 #include "inc.h"
35 #include "utils.h"
36 #include "gtkutils.h"
37 #include "alertpanel.h"
38 #include "manage_window.h"
39 #include "compose.h"
40 #include "addr_compl.h"
41 #include "quote_fmt.h"
42 #include "prefs_common.h"
43 #include "manual.h"
44
45 enum {
46         TEMPL_TEXT,
47         TEMPL_DATA,
48         TEMPL_AUTO_DATA,        /*!< auto pointer */
49         N_TEMPL_COLUMNS
50 };
51
52 static struct Templates {
53         GtkWidget *window;
54         GtkWidget *ok_btn;
55         GtkWidget *list_view;
56         GtkWidget *entry_name;
57         GtkWidget *entry_subject;
58         GtkWidget *entry_to;
59         GtkWidget *entry_cc;    
60         GtkWidget *entry_bcc;
61         GtkWidget *text_value;
62 } templates;
63
64 static int modified = FALSE;
65
66 /* widget creating functions */
67 static void prefs_template_window_create        (void);
68 static void prefs_template_window_setup         (void);
69 static void prefs_template_clear                (void);
70
71 static GSList *prefs_template_get_list          (void);
72
73 /* callbacks */
74 static gint prefs_template_deleted_cb           (GtkWidget      *widget,
75                                                  GdkEventAny    *event,
76                                                  gpointer        data);
77 static gboolean prefs_template_key_pressed_cb   (GtkWidget      *widget,
78                                                  GdkEventKey    *event,
79                                                  gpointer        data);
80 static void prefs_template_cancel_cb            (void);
81 static void prefs_template_ok_cb                (void);
82 static void prefs_template_register_cb          (void);
83 static void prefs_template_substitute_cb        (void);
84 static void prefs_template_delete_cb            (void);
85
86 static GtkListStore* prefs_template_create_data_store   (void);
87 static void prefs_template_list_view_insert_template    (GtkWidget *list_view,
88                                                          GtkTreeIter *row_iter,
89                                                          const gchar *template,
90                                                          Template *data,
91                                                          gboolean sorted);
92 static GtkWidget *prefs_template_list_view_create       (void);
93 static void prefs_template_create_list_view_columns     (GtkWidget *list_view);
94 static gboolean prefs_template_selected                 (GtkTreeSelection *selector,
95                                                          GtkTreeModel *model, 
96                                                          GtkTreePath *path,
97                                                          gboolean currently_selected,
98                                                          gpointer data);
99
100 /* Called from mainwindow.c */
101 void prefs_template_open(void)
102 {
103         inc_lock();
104
105         if (!templates.window)
106                 prefs_template_window_create();
107
108         prefs_template_window_setup();
109         gtk_widget_show(templates.window);
110 }
111
112 /*!
113  *\brief        Save Gtk object size to prefs dataset
114  */
115 static void prefs_template_size_allocate_cb(GtkWidget *widget,
116                                          GtkAllocation *allocation)
117 {
118         g_return_if_fail(allocation != NULL);
119
120         prefs_common.templateswin_width = allocation->width;
121         prefs_common.templateswin_height = allocation->height;
122 }
123
124 static void prefs_template_window_create(void)
125 {
126         /* window structure ;) */
127         GtkWidget *window;
128         GtkWidget   *vpaned;
129         GtkWidget     *vbox1;
130         GtkWidget       *table; /* including : entry_[name|to|cc|bcc|subject] */
131         GtkWidget       *scroll2;
132         GtkWidget         *text_value;
133         GtkWidget     *vbox2;
134         GtkWidget       *hbox2;
135         GtkWidget         *arrow1;
136         GtkWidget         *hbox3;
137         GtkWidget           *reg_btn;
138         GtkWidget           *subst_btn;
139         GtkWidget           *del_btn;
140         GtkWidget         *desc_btn;
141         GtkWidget       *scroll1;
142         GtkWidget         *list_view;
143         GtkWidget       *confirm_area;
144         GtkWidget         *help_btn;
145         GtkWidget         *cancel_btn;
146         GtkWidget         *ok_btn;
147         static GdkGeometry geometry;
148         gint i;
149         GtkTooltips *tooltips;
150
151         debug_print("Creating templates configuration window...\n");
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
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         table = gtk_table_new(5, 2, FALSE);
171         gtk_table_set_row_spacings (GTK_TABLE (table), VSPACING_NARROW_2);
172         gtk_table_set_col_spacings (GTK_TABLE (table), 4);
173         gtk_widget_show(table);
174         gtk_box_pack_start (GTK_BOX (vbox1), table, FALSE, FALSE, 0);
175
176         tooltips = gtk_tooltips_new();
177
178         struct
179         {
180                 gchar *label;
181                 GtkWidget **entry;
182                 gboolean compl;
183                 gchar *tooltips;
184         } tab[] = {
185                 {_("Name"),     &templates.entry_name,          FALSE,
186                         _("This name is used as the Menu item")},
187                 {_("To"),       &templates.entry_to,            TRUE,   NULL},
188                 {_("Cc"),       &templates.entry_cc,            TRUE,   NULL},
189                 {_("Bcc"),      &templates.entry_bcc,           TRUE,   NULL},
190                 {_("Subject"),  &templates.entry_subject,       FALSE,  NULL},
191                 {NULL,          NULL,                           FALSE,  NULL}
192         };
193
194         for (i=0; tab[i].label; i++) {
195
196                 GtkWidget *label;
197
198                 label = gtk_label_new(tab[i].label);
199                 gtk_widget_show(label);
200                 gtk_table_attach(GTK_TABLE(table), label, 0, 1, i, (i + 1),
201                                 (GtkAttachOptions) (GTK_FILL),
202                                 (GtkAttachOptions) 0, 0, 0);
203                 gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
204
205                 *(tab[i].entry) = gtk_entry_new();
206                 gtk_widget_show(*(tab[i].entry));
207                 gtk_table_attach(GTK_TABLE(table), *(tab[i].entry), 1, 2, i, (i + 1),
208                                 (GtkAttachOptions) (GTK_EXPAND|GTK_SHRINK|GTK_FILL),
209                                 (GtkAttachOptions) 0, 0, 0);
210                 gtk_tooltips_set_tip(tooltips, *(tab[i].entry), tab[i].tooltips, NULL);
211
212                 if (tab[i].compl)
213                         address_completion_register_entry(
214                                 GTK_ENTRY(*(tab[i].entry)), TRUE);
215         }
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.text_value = text_value;
338 }
339
340 static void prefs_template_window_setup(void)
341 {
342         GSList *tmpl_list;
343         GSList *cur;
344         Template *tmpl;
345         GtkListStore *store;
346
347         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
348                                 (templates.list_view)));
349
350         manage_window_set_transient(GTK_WINDOW(templates.window));
351         gtk_widget_grab_focus(templates.ok_btn);
352
353         gtk_list_store_clear(store);
354
355         prefs_template_list_view_insert_template(templates.list_view,
356                                                  NULL, _("(New)"),
357                                                  NULL, FALSE);
358         
359         tmpl_list = template_read_config();
360
361         for (cur = tmpl_list; cur != NULL; cur = cur->next) {
362                 tmpl = (Template *)cur->data;
363                 prefs_template_list_view_insert_template(templates.list_view,
364                                                          NULL, tmpl->name, 
365                                                          tmpl, FALSE);
366         }
367
368         g_slist_free(tmpl_list);
369 }
370
371 static void prefs_template_clear(void)
372 {
373         GtkListStore *store;
374
375         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
376                                 (templates.list_view)));
377         gtk_list_store_clear(store);
378 }
379
380 static gint prefs_template_deleted_cb(GtkWidget *widget, GdkEventAny *event,
381                                       gpointer data)
382 {
383         prefs_template_cancel_cb();
384         return TRUE;
385 }
386
387 static gboolean prefs_template_key_pressed_cb(GtkWidget *widget,
388                                               GdkEventKey *event, gpointer data)
389 {
390         if (event && event->keyval == GDK_Escape)
391                 prefs_template_cancel_cb();
392         else {
393                 GtkWidget *focused = gtkut_get_focused_child(
394                                         GTK_CONTAINER(widget));
395                 if (focused && GTK_IS_EDITABLE(focused)) {
396                         modified = TRUE;
397                 }
398         }
399         return FALSE;
400 }
401
402 static void prefs_template_ok_cb(void)
403 {
404         GSList *tmpl_list;
405
406         if (modified && alertpanel(_("Entry not saved"),
407                                  _("The entry was not saved. Close anyway?"),
408                                  GTK_STOCK_CLOSE, _("+_Continue editing"), 
409                                  NULL) != G_ALERTDEFAULT) {
410                 return;
411         }
412         modified = FALSE;
413         tmpl_list = prefs_template_get_list();
414         template_set_config(tmpl_list);
415         compose_reflect_prefs_all();
416         prefs_template_clear();
417         gtk_widget_hide(templates.window);
418         inc_unlock();
419 }
420
421 static void prefs_template_cancel_cb(void)
422 {
423         if (modified && alertpanel(_("Entry not saved"),
424                                  _("The entry was not saved. Close anyway?"),
425                                  GTK_STOCK_CLOSE, _("+_Continue editing"),
426                                  NULL) != G_ALERTDEFAULT) {
427                 return;
428         }
429         modified = FALSE;
430         prefs_template_clear();
431         gtk_widget_hide(templates.window);
432         inc_unlock();
433 }
434
435 /*!
436  *\brief        Request list for storage. New list is owned
437  *              by template.c...
438  */
439 static GSList *prefs_template_get_list(void)
440 {
441         GSList *tmpl_list = NULL;
442         Template *tmpl;
443         GtkTreeModel *model;
444         GtkTreeIter iter;
445
446         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
447         if (!gtk_tree_model_get_iter_first(model, &iter))
448                 return NULL;
449
450         do {
451                 gtk_tree_model_get(model, &iter,
452                                    TEMPL_DATA, &tmpl,
453                                    -1);
454                 
455                 if (tmpl) {
456                         Template *ntmpl;
457                         
458                         ntmpl = g_new(Template, 1);
459                         ntmpl->name    = tmpl->name && *(tmpl->name) 
460                                          ? g_strdup(tmpl->name) 
461                                          : NULL;
462                         ntmpl->subject = tmpl->subject && *(tmpl->subject) 
463                                          ? g_strdup(tmpl->subject) 
464                                          : NULL;
465                         ntmpl->to      = tmpl->to && *(tmpl->to)
466                                          ? g_strdup(tmpl->to)
467                                          : NULL;
468                         ntmpl->cc      = tmpl->cc && *(tmpl->cc)
469                                          ? g_strdup(tmpl->cc)
470                                          : NULL;
471                         ntmpl->bcc     = tmpl->bcc && *(tmpl->bcc)
472                                          ? g_strdup(tmpl->bcc)
473                                          : NULL;        
474                         ntmpl->value   = tmpl->value && *(tmpl->value)
475                                          ? g_strdup(tmpl->value)
476                                          : NULL;
477                         tmpl_list = g_slist_append(tmpl_list, ntmpl);
478                 }                       
479         
480         } while (gtk_tree_model_iter_next(model, &iter)); 
481
482         return tmpl_list;
483 }
484
485 static gboolean prefs_template_list_view_set_row(GtkTreeIter *row)
486 /* return TRUE if the row could be modified */
487 {
488         Template *tmpl;
489         gchar *name;
490         gchar *subject;
491         gchar *to;
492         gchar *cc;
493         gchar *bcc;     
494         gchar *value;
495         GtkTextBuffer *buffer;
496         GtkTextIter start, end;
497         GtkTreeModel *model;
498
499         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
500
501         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
502         gtk_text_buffer_get_start_iter(buffer, &start);
503         gtk_text_buffer_get_iter_at_offset(buffer, &end, -1);
504         value = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
505
506         if (value && *value != '\0') {
507                 gchar *parsed_buf;
508                 MsgInfo dummyinfo;
509
510                 memset(&dummyinfo, 0, sizeof(MsgInfo));
511                 quote_fmt_init(&dummyinfo, NULL, NULL, TRUE, NULL);
512                 quote_fmt_scan_string(value);
513                 quote_fmt_parse();
514                 parsed_buf = quote_fmt_get_buffer();
515                 if (!parsed_buf) {
516                         alertpanel_error(_("Template format error."));
517                         g_free(value);
518                         return FALSE;
519                 }
520                 quote_fmt_reset_vartable();
521         }
522
523         name = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_name),
524                                       0, -1);
525         if (name[0] == '\0') {
526                 alertpanel_error(_("Template name is not set."));
527                 return FALSE;
528         }
529         subject = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_subject),
530                                          0, -1);
531         to = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_to),
532                                     0, -1);
533         cc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_cc),
534                                     0, -1);
535         bcc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_bcc),
536                                     0, -1);
537
538         if (subject && *subject == '\0') {
539                 g_free(subject);
540                 subject = NULL;
541         }
542         if (to && *to == '\0') {
543                 g_free(to);
544                 to = NULL;
545         }
546         if (cc && *cc == '\0') {
547                 g_free(cc);
548                 cc = NULL;
549         }
550         if (bcc && *bcc == '\0') {
551                 g_free(bcc);
552                 bcc = NULL;
553         }
554         
555         tmpl = g_new(Template, 1);
556         tmpl->name = name;
557         tmpl->subject = subject;
558         tmpl->to = to;
559         tmpl->cc = cc;
560         tmpl->bcc = bcc;        
561         tmpl->value = value;
562
563         prefs_template_list_view_insert_template(templates.list_view,
564                                                  row, tmpl->name, tmpl, TRUE);
565         return TRUE;
566 }
567
568 static void prefs_template_register_cb(void)
569 {
570         modified = !prefs_template_list_view_set_row(NULL);
571 }
572
573 static void prefs_template_substitute_cb(void)
574 {
575         Template *tmpl;
576         GtkTreeIter row;
577         GtkTreeSelection *selection;
578         GtkTreeModel *model;
579
580         selection = gtk_tree_view_get_selection
581                         (GTK_TREE_VIEW(templates.list_view));
582         
583         if (!gtk_tree_selection_get_selected(selection, &model, &row))
584                 return;
585
586         gtk_tree_model_get(model, &row, 
587                            TEMPL_DATA, &tmpl,
588                            -1);
589
590         if (!tmpl) return;
591
592         modified = !prefs_template_list_view_set_row(&row);
593 }
594
595 static void prefs_template_delete_cb(void)
596 {
597         Template *tmpl;
598         GtkTreeIter row;
599         GtkTreeSelection *selection;
600         GtkTreeModel *model;
601
602         selection = gtk_tree_view_get_selection
603                         (GTK_TREE_VIEW(templates.list_view));
604         
605         if (!gtk_tree_selection_get_selected(selection, &model, &row))
606                 return;
607
608         gtk_tree_model_get(model, &row, 
609                            TEMPL_DATA, &tmpl,
610                            -1);
611
612         if (!tmpl) 
613                 return;
614
615         if (alertpanel(_("Delete template"),
616                        _("Do you really want to delete this template?"),
617                        GTK_STOCK_CANCEL, GTK_STOCK_DELETE,
618                        NULL) != G_ALERTALTERNATE)
619                 return;
620
621         gtk_list_store_remove(GTK_LIST_STORE(model), &row);             
622 }
623
624 static GtkListStore* prefs_template_create_data_store(void)
625 {
626         return gtk_list_store_new(N_TEMPL_COLUMNS,
627                                   G_TYPE_STRING,        
628                                   G_TYPE_POINTER,
629                                   G_TYPE_AUTO_POINTER,
630                                   -1);
631 }
632
633 static gboolean prefs_template_list_view_find_insertion_point(GtkTreeModel* model,
634                                                         const gchar* template,
635                                                         GtkTreeIter* iter)
636 {
637         GtkTreeIter i;
638         gchar *text = NULL;
639
640         if ((gtk_tree_model_get_iter_first(model, &i))) {
641                 for ( ; gtk_tree_model_iter_next(model, &i); ) {
642                         gtk_tree_model_get(model, &i, TEMPL_TEXT, &text, -1);
643                         if (strcmp(text, template) >= 0) {
644                                 g_free(text);
645                                 *iter = i;
646                                 return TRUE;
647                         }
648                         g_free(text);
649                 }
650         }
651         /* cannot add before (need to append) */
652         return FALSE;
653 }
654
655 static void prefs_template_list_view_insert_template(GtkWidget *list_view,
656                                                      GtkTreeIter *row_iter,
657                                                      const gchar *template,
658                                                      Template *data,
659                                                          gboolean sorted)
660 {
661         GtkTreeIter iter;
662         GtkTreeModel *model = gtk_tree_view_get_model
663                                         (GTK_TREE_VIEW(list_view));
664         GtkListStore *list_store = GTK_LIST_STORE(model);
665         GAuto *auto_data;                                       
666
667         if (row_iter == NULL) {
668                 if (sorted) {
669                         GtkTreeIter insertion_point;
670
671                         /* insert sorted */
672                         if (prefs_template_list_view_find_insertion_point(model,
673                                         template, &insertion_point)) {
674                                 gtk_list_store_insert_before(list_store, &iter,
675                                                 &insertion_point);
676                         } else {
677                                 gtk_list_store_append(list_store, &iter);
678                         }
679                 } else {
680                         /* append new */
681                         gtk_list_store_append(list_store, &iter);
682                 }
683         } else {
684                 /* modify the existing */
685                 iter = *row_iter;
686
687                 if (sorted) {
688                         GtkTreeIter insertion_point;
689                         gchar *text = NULL;
690
691                         /* force re-sorting if template's name changed */
692                         gtk_tree_model_get(model, row_iter, TEMPL_TEXT, &text, -1);
693                         if (strcmp(text, template) != 0) {
694
695                                 /* move the modified template */
696                                 if (prefs_template_list_view_find_insertion_point(model,
697                                                 template, &insertion_point)) {
698                                         gtk_list_store_move_before(list_store, row_iter,
699                                                         &insertion_point);
700                                 } else {
701                                         /* move to the end */
702                                         gtk_list_store_move_before(list_store, row_iter,
703                                                         NULL);
704                                 }
705                         }
706                         g_free(text);
707                 }
708         }
709                 
710         auto_data = g_auto_pointer_new_with_free(data, (GFreeFunc) template_free);  
711
712         /* if replacing data in an existing row, the auto pointer takes care
713          * of destroying the Template data */
714         gtk_list_store_set(list_store, &iter,
715                            TEMPL_TEXT, template,
716                            TEMPL_DATA, data,
717                            TEMPL_AUTO_DATA, auto_data,
718                            -1);
719
720         g_auto_pointer_free(auto_data);                    
721 }
722
723 static GtkWidget *prefs_template_list_view_create(void)
724 {
725         GtkTreeView *list_view;
726         GtkTreeSelection *selector;
727         GtkTreeModel *model;
728
729         model = GTK_TREE_MODEL(prefs_template_create_data_store());
730         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
731         g_object_unref(model);  
732         
733         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
734         
735         selector = gtk_tree_view_get_selection(list_view);
736         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
737         gtk_tree_selection_set_select_function(selector, prefs_template_selected,
738                                                NULL, NULL);
739
740         /* create the columns */
741         prefs_template_create_list_view_columns(GTK_WIDGET(list_view));
742
743         return GTK_WIDGET(list_view);
744 }
745
746 static void prefs_template_create_list_view_columns(GtkWidget *list_view)
747 {
748         GtkTreeViewColumn *column;
749         GtkCellRenderer *renderer;
750
751         renderer = gtk_cell_renderer_text_new();
752         column = gtk_tree_view_column_new_with_attributes
753                         (_("Current templates"),
754                          renderer,
755                          "text", TEMPL_TEXT,
756                          NULL);
757         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
758 }
759
760 static gboolean prefs_template_selected(GtkTreeSelection *selector,
761                                         GtkTreeModel *model, 
762                                         GtkTreePath *path,
763                                         gboolean currently_selected,
764                                         gpointer data)
765 {
766         Template *tmpl;
767         Template tmpl_def;
768         GtkTextBuffer *buffer;
769         GtkTextIter iter;
770         GtkTreeIter titer;
771
772         if (currently_selected)
773                 return TRUE;
774
775         if (!gtk_tree_model_get_iter(model, &titer, path))
776                 return TRUE;
777
778         tmpl_def.name = _("Template");
779         tmpl_def.subject = "";
780         tmpl_def.to = "";
781         tmpl_def.cc = "";
782         tmpl_def.bcc = "";      
783         tmpl_def.value = "";
784
785         gtk_tree_model_get(model, &titer,
786                            TEMPL_DATA, &tmpl,
787                            -1);
788
789         if (!tmpl) 
790                 tmpl = &tmpl_def;
791
792         gtk_entry_set_text(GTK_ENTRY(templates.entry_name), tmpl->name);
793         gtk_entry_set_text(GTK_ENTRY(templates.entry_to),
794                            tmpl->to ? tmpl->to : "");
795         gtk_entry_set_text(GTK_ENTRY(templates.entry_cc),
796                            tmpl->cc ? tmpl->cc : "");
797         gtk_entry_set_text(GTK_ENTRY(templates.entry_bcc),
798                            tmpl->bcc ? tmpl->bcc : "");                 
799         gtk_entry_set_text(GTK_ENTRY(templates.entry_subject),
800                            tmpl->subject ? tmpl->subject : "");
801         
802         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
803         gtk_text_buffer_set_text(buffer, "", -1);
804         gtk_text_buffer_get_start_iter(buffer, &iter);
805         gtk_text_buffer_insert(buffer, &iter, tmpl->value, -1);
806
807         return TRUE;
808 }