2007-05-03 [wwp] 2.9.1cvs40
[claws.git] / src / prefs_template.c
1 /*
2  * Sylpheed templates subsystem 
3  * Copyright (C) 2001 Alexander Barinov
4  * Copyright (C) 2001-2007 Hiroyuki Yamamoto and the Claws Mail 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 = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "prefs_template");
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         if (prefs_common.textfont) {
229                 PangoFontDescription *font_desc;
230
231                 font_desc = pango_font_description_from_string
232                                                 (prefs_common.textfont);
233                 if (font_desc) {
234                         gtk_widget_modify_font(text_value, font_desc);
235                         pango_font_description_free(font_desc);
236                 }
237         }
238         gtk_widget_show(text_value);
239         gtk_widget_set_size_request(text_value, -1, 120);
240         gtk_container_add(GTK_CONTAINER(scroll2), text_value);
241         gtk_text_view_set_editable(GTK_TEXT_VIEW(text_value), TRUE);
242         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text_value), GTK_WRAP_WORD);
243
244         /* vbox for buttons and templates list */
245         vbox2 = gtk_vbox_new(FALSE, 6);
246         gtk_widget_show(vbox2);
247         gtk_container_set_border_width(GTK_CONTAINER(vbox2), 8);
248         gtk_paned_pack2(GTK_PANED(vpaned), vbox2, TRUE, FALSE);
249
250         /* register | substitute | delete */
251         hbox2 = gtk_hbox_new(FALSE, 4);
252         gtk_widget_show(hbox2);
253         gtk_box_pack_start(GTK_BOX(vbox2), hbox2, FALSE, FALSE, 0);
254
255         arrow1 = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
256         gtk_widget_show(arrow1);
257         gtk_box_pack_start(GTK_BOX(hbox2), arrow1, FALSE, FALSE, 0);
258         gtk_widget_set_size_request(arrow1, -1, 16);
259
260         hbox3 = gtk_hbox_new(TRUE, 4);
261         gtk_widget_show(hbox3);
262         gtk_box_pack_start(GTK_BOX(hbox2), hbox3, FALSE, FALSE, 0);
263
264         reg_btn = gtk_button_new_from_stock(GTK_STOCK_ADD);
265         gtk_widget_show(reg_btn);
266         gtk_box_pack_start(GTK_BOX(hbox3), reg_btn, FALSE, TRUE, 0);
267         g_signal_connect(G_OBJECT (reg_btn), "clicked",
268                          G_CALLBACK (prefs_template_register_cb), NULL);
269
270         subst_btn = gtkut_get_replace_btn(_("Replace"));
271         gtk_widget_show(subst_btn);
272         gtk_box_pack_start(GTK_BOX(hbox3), subst_btn, FALSE, TRUE, 0);
273         g_signal_connect(G_OBJECT(subst_btn), "clicked",
274                          G_CALLBACK(prefs_template_substitute_cb),
275                          NULL);
276
277         del_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
278         gtk_widget_show(del_btn);
279         gtk_box_pack_start(GTK_BOX(hbox3), del_btn, FALSE, TRUE, 0);
280         g_signal_connect(G_OBJECT(del_btn), "clicked",
281                          G_CALLBACK(prefs_template_delete_cb), NULL);
282
283 #if GTK_CHECK_VERSION(2, 8, 0)
284         desc_btn = gtk_button_new_from_stock(GTK_STOCK_INFO);
285 #else
286         desc_btn = gtk_button_new_with_label(_(" Symbols... "));
287 #endif
288         gtk_widget_show(desc_btn);
289         gtk_box_pack_end(GTK_BOX(hbox2), desc_btn, FALSE, FALSE, 0);
290         g_signal_connect(G_OBJECT(desc_btn), "clicked",
291                          G_CALLBACK(quote_fmt_quote_description), NULL);
292
293         /* templates list */
294         scroll1 = gtk_scrolled_window_new(NULL, NULL);
295         gtk_widget_show(scroll1);
296         gtk_box_pack_start(GTK_BOX(vbox2), scroll1, TRUE, TRUE, 0);
297         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll1),
298                                        GTK_POLICY_AUTOMATIC,
299                                        GTK_POLICY_AUTOMATIC);
300                                        
301         list_view = prefs_template_list_view_create();
302         gtk_widget_show(list_view);
303         gtk_widget_set_size_request(scroll1, -1, 140);
304         gtk_container_add(GTK_CONTAINER(scroll1), list_view);
305
306         /* help | cancel | ok */
307         gtkut_stock_button_set_create_with_help(&confirm_area, &help_btn,
308                         &cancel_btn, GTK_STOCK_CANCEL,
309                         &ok_btn, GTK_STOCK_OK,
310                         NULL, NULL);
311         gtk_widget_show(confirm_area);
312         gtk_box_pack_end(GTK_BOX(vbox2), confirm_area, FALSE, FALSE, 0);
313         gtk_widget_grab_default(ok_btn);
314
315         gtk_window_set_title(GTK_WINDOW(window), _("Template configuration"));
316
317         g_signal_connect(G_OBJECT(window), "delete_event",
318                          G_CALLBACK(prefs_template_deleted_cb), NULL);
319         g_signal_connect(G_OBJECT(window), "size_allocate",
320                          G_CALLBACK(prefs_template_size_allocate_cb), NULL);
321         g_signal_connect(G_OBJECT(window), "key_press_event",
322                          G_CALLBACK(prefs_template_key_pressed_cb), NULL);
323         MANAGE_WINDOW_SIGNALS_CONNECT(window);
324         g_signal_connect(G_OBJECT(ok_btn), "clicked",
325                          G_CALLBACK(prefs_template_ok_cb), NULL);
326         g_signal_connect(G_OBJECT(cancel_btn), "clicked",
327                          G_CALLBACK(prefs_template_cancel_cb), NULL);
328         g_signal_connect(G_OBJECT(help_btn), "clicked",
329                          G_CALLBACK(manual_open_with_anchor_cb),
330                          MANUAL_ANCHOR_TEMPLATES);
331
332         address_completion_start(window);
333
334         if (!geometry.min_height) {
335                 geometry.min_width = 440;
336                 geometry.min_height = 500;
337         }
338
339         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
340                                       GDK_HINT_MIN_SIZE);
341         gtk_widget_set_size_request(window, prefs_common.templateswin_width,
342                                     prefs_common.templateswin_height);
343
344         templates.window = window;
345         templates.ok_btn = ok_btn;
346         templates.list_view = list_view;
347         templates.text_value = text_value;
348 }
349
350 static void prefs_template_window_setup(void)
351 {
352         GSList *tmpl_list;
353         GSList *cur;
354         Template *tmpl;
355         GtkListStore *store;
356         GtkTextBuffer *buffer;
357         
358         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
359                                 (templates.list_view)));
360
361         manage_window_set_transient(GTK_WINDOW(templates.window));
362         gtk_widget_grab_focus(templates.ok_btn);
363
364         gtk_list_store_clear(store);
365
366         prefs_template_list_view_insert_template(templates.list_view,
367                                                  NULL, _("(New)"),
368                                                  NULL, FALSE);
369         
370         tmpl_list = template_read_config();
371
372         for (cur = tmpl_list; cur != NULL; cur = cur->next) {
373                 tmpl = (Template *)cur->data;
374                 prefs_template_list_view_insert_template(templates.list_view,
375                                                          NULL, tmpl->name, 
376                                                          tmpl, FALSE);
377         }
378
379         gtk_entry_set_text(GTK_ENTRY(templates.entry_name), "");
380         gtk_entry_set_text(GTK_ENTRY(templates.entry_to), "");
381         gtk_entry_set_text(GTK_ENTRY(templates.entry_cc), "");
382         gtk_entry_set_text(GTK_ENTRY(templates.entry_bcc), "");                 
383         gtk_entry_set_text(GTK_ENTRY(templates.entry_subject), "");
384         
385         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
386         gtk_text_buffer_set_text(buffer, "", -1);
387
388         g_slist_free(tmpl_list);
389 }
390
391 static void prefs_template_clear(void)
392 {
393         GtkListStore *store;
394
395         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
396                                 (templates.list_view)));
397         gtk_list_store_clear(store);
398 }
399
400 static gint prefs_template_deleted_cb(GtkWidget *widget, GdkEventAny *event,
401                                       gpointer data)
402 {
403         prefs_template_cancel_cb();
404         return TRUE;
405 }
406
407 static gboolean prefs_template_key_pressed_cb(GtkWidget *widget,
408                                               GdkEventKey *event, gpointer data)
409 {
410         if (event && event->keyval == GDK_Escape)
411                 prefs_template_cancel_cb();
412         else {
413                 GtkWidget *focused = gtkut_get_focused_child(
414                                         GTK_CONTAINER(widget));
415                 if (focused && GTK_IS_EDITABLE(focused)) {
416                         modified = TRUE;
417                 }
418         }
419         return FALSE;
420 }
421
422 static void prefs_template_ok_cb(void)
423 {
424         GSList *tmpl_list;
425
426         if (modified && alertpanel(_("Entry not saved"),
427                                  _("The entry was not saved. Close anyway?"),
428                                  GTK_STOCK_CLOSE, _("+_Continue editing"), 
429                                  NULL) != G_ALERTDEFAULT) {
430                 return;
431         }
432         modified = FALSE;
433         tmpl_list = prefs_template_get_list();
434         template_set_config(tmpl_list);
435         compose_reflect_prefs_all();
436         prefs_template_clear();
437         gtk_widget_hide(templates.window);
438         inc_unlock();
439 }
440
441 static void prefs_template_cancel_cb(void)
442 {
443         if (modified && alertpanel(_("Entry not saved"),
444                                  _("The entry was not saved. Close anyway?"),
445                                  GTK_STOCK_CLOSE, _("+_Continue editing"),
446                                  NULL) != G_ALERTDEFAULT) {
447                 return;
448         }
449         modified = FALSE;
450         prefs_template_clear();
451         gtk_widget_hide(templates.window);
452         inc_unlock();
453 }
454
455 /*!
456  *\brief        Request list for storage. New list is owned
457  *              by template.c...
458  */
459 static GSList *prefs_template_get_list(void)
460 {
461         GSList *tmpl_list = NULL;
462         Template *tmpl;
463         GtkTreeModel *model;
464         GtkTreeIter iter;
465
466         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
467         if (!gtk_tree_model_get_iter_first(model, &iter))
468                 return NULL;
469
470         do {
471                 gtk_tree_model_get(model, &iter,
472                                    TEMPL_DATA, &tmpl,
473                                    -1);
474                 
475                 if (tmpl) {
476                         Template *ntmpl;
477                         
478                         ntmpl = g_new(Template, 1);
479                         ntmpl->name    = tmpl->name && *(tmpl->name) 
480                                          ? g_strdup(tmpl->name) 
481                                          : NULL;
482                         ntmpl->subject = tmpl->subject && *(tmpl->subject) 
483                                          ? g_strdup(tmpl->subject) 
484                                          : NULL;
485                         ntmpl->to      = tmpl->to && *(tmpl->to)
486                                          ? g_strdup(tmpl->to)
487                                          : NULL;
488                         ntmpl->cc      = tmpl->cc && *(tmpl->cc)
489                                          ? g_strdup(tmpl->cc)
490                                          : NULL;
491                         ntmpl->bcc     = tmpl->bcc && *(tmpl->bcc)
492                                          ? g_strdup(tmpl->bcc)
493                                          : NULL;        
494                         ntmpl->value   = tmpl->value && *(tmpl->value)
495                                          ? g_strdup(tmpl->value)
496                                          : NULL;
497                         tmpl_list = g_slist_append(tmpl_list, ntmpl);
498                 }                       
499         
500         } while (gtk_tree_model_iter_next(model, &iter)); 
501
502         return tmpl_list;
503 }
504
505 gboolean prefs_template_string_is_valid(gchar *string)
506 {
507         if (string && *string != '\0') {
508                 gchar *parsed_buf;
509                 MsgInfo dummyinfo;
510
511                 memset(&dummyinfo, 0, sizeof(MsgInfo));
512                 quote_fmt_init(&dummyinfo, NULL, NULL, TRUE, NULL);
513                 quote_fmt_scan_string(string);
514                 quote_fmt_parse();
515                 parsed_buf = quote_fmt_get_buffer();
516                 if (!parsed_buf)
517                         return FALSE;
518                 quote_fmt_reset_vartable();
519         }
520         return TRUE;
521 }
522
523 static gboolean prefs_template_list_view_set_row(GtkTreeIter *row)
524 /* return TRUE if the row could be modified */
525 {
526         Template *tmpl;
527         gchar *name;
528         gchar *subject;
529         gchar *to;
530         gchar *cc;
531         gchar *bcc;     
532         gchar *value;
533         GtkTextBuffer *buffer;
534         GtkTextIter start, end;
535         GtkTreeModel *model;
536
537         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
538
539         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
540         gtk_text_buffer_get_start_iter(buffer, &start);
541         gtk_text_buffer_get_iter_at_offset(buffer, &end, -1);
542         value = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
543
544         if (value && *value == '\0') {
545                         g_free(value);
546                 value = NULL;
547                 }
548         if (!prefs_template_string_is_valid(value)) {
549                 alertpanel_error(_("Template body format error."));     
550                 g_free(value);
551                 return FALSE;
552         }
553
554         name = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_name),
555                                       0, -1);
556         if (*name == '\0') {
557                 alertpanel_error(_("Template name is not set."));
558                 return FALSE;
559         }
560         to = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_to),
561                                     0, -1);
562         cc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_cc),
563                                     0, -1);
564         bcc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_bcc),
565                                     0, -1);
566         subject = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_subject),
567                                          0, -1);
568
569         if (to && *to == '\0') {
570                 g_free(to);
571                 to = NULL;
572         }
573         if (cc && *cc == '\0') {
574                 g_free(cc);
575                 cc = NULL;
576         }
577         if (bcc && *bcc == '\0') {
578                 g_free(bcc);
579                 bcc = NULL;
580         }
581         if (subject && *subject == '\0') {
582                 g_free(subject);
583                 subject = NULL;
584         }
585
586         if (!prefs_template_string_is_valid(to)) {
587                 alertpanel_error(_("Template To format error."));
588                 g_free(to);
589                 return FALSE;
590         }
591         if (!prefs_template_string_is_valid(cc)) {
592                 alertpanel_error(_("Template Cc format error."));       
593                 g_free(cc);
594                 return FALSE;
595         }
596         if (!prefs_template_string_is_valid(bcc)) {
597                 alertpanel_error(_("Template Bcc format error."));      
598                 g_free(bcc);
599                 return FALSE;
600         }
601         if (!prefs_template_string_is_valid(subject)) {
602                 alertpanel_error(_("Template subject format error."));  
603                 g_free(subject);
604                 return FALSE;
605         }
606         
607         tmpl = g_new(Template, 1);
608         tmpl->name = name;
609         tmpl->subject = subject;
610         tmpl->to = to;
611         tmpl->cc = cc;
612         tmpl->bcc = bcc;        
613         tmpl->value = value;
614
615         prefs_template_list_view_insert_template(templates.list_view,
616                                                  row, tmpl->name, tmpl, TRUE);
617         return TRUE;
618 }
619
620 static void prefs_template_register_cb(void)
621 {
622         modified = !prefs_template_list_view_set_row(NULL);
623 }
624
625 static void prefs_template_substitute_cb(void)
626 {
627         Template *tmpl;
628         GtkTreeIter row;
629         GtkTreeSelection *selection;
630         GtkTreeModel *model;
631
632         selection = gtk_tree_view_get_selection
633                         (GTK_TREE_VIEW(templates.list_view));
634         
635         if (!gtk_tree_selection_get_selected(selection, &model, &row))
636                 return;
637
638         gtk_tree_model_get(model, &row, 
639                            TEMPL_DATA, &tmpl,
640                            -1);
641
642         if (!tmpl) return;
643
644         modified = !prefs_template_list_view_set_row(&row);
645 }
646
647 static void prefs_template_delete_cb(void)
648 {
649         Template *tmpl;
650         GtkTreeIter row;
651         GtkTreeSelection *selection;
652         GtkTreeModel *model;
653
654         selection = gtk_tree_view_get_selection
655                         (GTK_TREE_VIEW(templates.list_view));
656         
657         if (!gtk_tree_selection_get_selected(selection, &model, &row))
658                 return;
659
660         gtk_tree_model_get(model, &row, 
661                            TEMPL_DATA, &tmpl,
662                            -1);
663
664         if (!tmpl) 
665                 return;
666
667         if (alertpanel(_("Delete template"),
668                        _("Do you really want to delete this template?"),
669                        GTK_STOCK_CANCEL, GTK_STOCK_DELETE,
670                        NULL) != G_ALERTALTERNATE)
671                 return;
672
673         gtk_list_store_remove(GTK_LIST_STORE(model), &row);             
674 }
675
676 static GtkListStore* prefs_template_create_data_store(void)
677 {
678         return gtk_list_store_new(N_TEMPL_COLUMNS,
679                                   G_TYPE_STRING,        
680                                   G_TYPE_POINTER,
681                                   G_TYPE_AUTO_POINTER,
682                                   -1);
683 }
684
685 static gboolean prefs_template_list_view_find_insertion_point(GtkTreeModel* model,
686                                                         const gchar* template,
687                                                         GtkTreeIter* iter)
688 {
689         GtkTreeIter i;
690         gchar *text = NULL;
691
692         if ((gtk_tree_model_get_iter_first(model, &i))) {
693                 for ( ; gtk_tree_model_iter_next(model, &i); ) {
694                         gtk_tree_model_get(model, &i, TEMPL_TEXT, &text, -1);
695                         if (strcmp(text, template) >= 0) {
696                                 g_free(text);
697                                 *iter = i;
698                                 return TRUE;
699                         }
700                         g_free(text);
701                 }
702         }
703         /* cannot add before (need to append) */
704         return FALSE;
705 }
706
707 static void prefs_template_list_view_insert_template(GtkWidget *list_view,
708                                                      GtkTreeIter *row_iter,
709                                                      const gchar *template,
710                                                      Template *data,
711                                                          gboolean sorted)
712 {
713         GtkTreeIter iter;
714         GtkTreeModel *model = gtk_tree_view_get_model
715                                         (GTK_TREE_VIEW(list_view));
716         GtkListStore *list_store = GTK_LIST_STORE(model);
717         GAuto *auto_data;                                       
718
719         if (row_iter == NULL) {
720                 if (sorted) {
721                         GtkTreeIter insertion_point;
722
723                         /* insert sorted */
724                         if (prefs_template_list_view_find_insertion_point(model,
725                                         template, &insertion_point)) {
726                                 gtk_list_store_insert_before(list_store, &iter,
727                                                 &insertion_point);
728                         } else {
729                                 gtk_list_store_append(list_store, &iter);
730                         }
731                 } else {
732                         /* append new */
733                         gtk_list_store_append(list_store, &iter);
734                 }
735         } else {
736                 /* modify the existing */
737                 iter = *row_iter;
738
739                 if (sorted) {
740                         GtkTreeIter insertion_point;
741                         gchar *text = NULL;
742
743                         /* force re-sorting if template's name changed */
744                         gtk_tree_model_get(model, row_iter, TEMPL_TEXT, &text, -1);
745                         if (strcmp(text, template) != 0) {
746
747                                 /* move the modified template */
748                                 if (prefs_template_list_view_find_insertion_point(model,
749                                                 template, &insertion_point)) {
750                                         gtk_list_store_move_before(list_store, row_iter,
751                                                         &insertion_point);
752                                 } else {
753                                         /* move to the end */
754                                         gtk_list_store_move_before(list_store, row_iter,
755                                                         NULL);
756                                 }
757                         }
758                         g_free(text);
759                 }
760         }
761                 
762         auto_data = g_auto_pointer_new_with_free(data, (GFreeFunc) template_free);  
763
764         /* if replacing data in an existing row, the auto pointer takes care
765          * of destroying the Template data */
766         gtk_list_store_set(list_store, &iter,
767                            TEMPL_TEXT, template,
768                            TEMPL_DATA, data,
769                            TEMPL_AUTO_DATA, auto_data,
770                            -1);
771
772         g_auto_pointer_free(auto_data);                    
773 }
774
775 static GtkWidget *prefs_template_list_view_create(void)
776 {
777         GtkTreeView *list_view;
778         GtkTreeSelection *selector;
779         GtkTreeModel *model;
780
781         model = GTK_TREE_MODEL(prefs_template_create_data_store());
782         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
783         g_object_unref(model);  
784         
785         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
786         
787         selector = gtk_tree_view_get_selection(list_view);
788         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
789         gtk_tree_selection_set_select_function(selector, prefs_template_selected,
790                                                NULL, NULL);
791
792         /* create the columns */
793         prefs_template_create_list_view_columns(GTK_WIDGET(list_view));
794
795         return GTK_WIDGET(list_view);
796 }
797
798 static void prefs_template_create_list_view_columns(GtkWidget *list_view)
799 {
800         GtkTreeViewColumn *column;
801         GtkCellRenderer *renderer;
802
803         renderer = gtk_cell_renderer_text_new();
804         column = gtk_tree_view_column_new_with_attributes
805                         (_("Current templates"),
806                          renderer,
807                          "text", TEMPL_TEXT,
808                          NULL);
809         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
810 }
811
812 static gboolean prefs_template_selected(GtkTreeSelection *selector,
813                                         GtkTreeModel *model, 
814                                         GtkTreePath *path,
815                                         gboolean currently_selected,
816                                         gpointer data)
817 {
818         Template *tmpl;
819         Template tmpl_def;
820         GtkTextBuffer *buffer;
821         GtkTextIter iter;
822         GtkTreeIter titer;
823
824         if (currently_selected)
825                 return TRUE;
826
827         if (!gtk_tree_model_get_iter(model, &titer, path))
828                 return TRUE;
829
830         tmpl_def.name = _("Template");
831         tmpl_def.subject = "";
832         tmpl_def.to = "";
833         tmpl_def.cc = "";
834         tmpl_def.bcc = "";      
835         tmpl_def.value = "";
836
837         gtk_tree_model_get(model, &titer,
838                            TEMPL_DATA, &tmpl,
839                            -1);
840
841         if (!tmpl) 
842                 tmpl = &tmpl_def;
843
844         gtk_entry_set_text(GTK_ENTRY(templates.entry_name), tmpl->name);
845         gtk_entry_set_text(GTK_ENTRY(templates.entry_to),
846                            tmpl->to ? tmpl->to : "");
847         gtk_entry_set_text(GTK_ENTRY(templates.entry_cc),
848                            tmpl->cc ? tmpl->cc : "");
849         gtk_entry_set_text(GTK_ENTRY(templates.entry_bcc),
850                            tmpl->bcc ? tmpl->bcc : "");                 
851         gtk_entry_set_text(GTK_ENTRY(templates.entry_subject),
852                            tmpl->subject ? tmpl->subject : "");
853         
854         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
855         gtk_text_buffer_set_text(buffer, "", -1);
856         gtk_text_buffer_get_start_iter(buffer, &iter);
857         gtk_text_buffer_insert(buffer, &iter, tmpl->value, -1);
858
859         return TRUE;
860 }