bc258cb074bf210cbc09f13b1efdb13d2960ebf3
[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 3 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, see <http://www.gnu.org/licenses/>.
18  * 
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 #include "gtkutils.h"
45
46 enum {
47         TEMPL_TEXT,
48         TEMPL_DATA,
49         TEMPL_AUTO_DATA,        /*!< auto pointer */
50         N_TEMPL_COLUMNS
51 };
52
53 static struct Templates {
54         GtkWidget *window;
55         GtkWidget *ok_btn;
56         GtkWidget *list_view;
57         GtkWidget *entry_name;
58         GtkWidget *entry_subject;
59         GtkWidget *entry_to;
60         GtkWidget *entry_cc;    
61         GtkWidget *entry_bcc;
62         GtkWidget *text_value;
63 } templates;
64
65 static int modified = FALSE;
66 static int modified_list = FALSE;
67 /* widget creating functions */
68 static void prefs_template_window_create        (void);
69 static void prefs_template_window_setup         (void);
70 static void prefs_template_clear                (void);
71
72 static GSList *prefs_template_get_list          (void);
73
74 /* callbacks */
75 static gint prefs_template_deleted_cb           (GtkWidget      *widget,
76                                                  GdkEventAny    *event,
77                                                  gpointer        data);
78 static gboolean prefs_template_key_pressed_cb   (GtkWidget      *widget,
79                                                  GdkEventKey    *event,
80                                                  gpointer        data);
81 static void prefs_template_cancel_cb            (void);
82 static void prefs_template_ok_cb                (void);
83 static void prefs_template_register_cb          (void);
84 static void prefs_template_substitute_cb        (void);
85 static void prefs_template_delete_cb            (void);
86 static void prefs_template_top_cb               (void);
87 static void prefs_template_up_cb                (void);
88 static void prefs_template_down_cb      (void);
89 static void prefs_template_bottom_cb    (void);
90
91 static GtkListStore* prefs_template_create_data_store   (void);
92 static void prefs_template_list_view_insert_template    (GtkWidget *list_view,
93                                                          GtkTreeIter *row_iter,
94                                                          const gchar *template,
95                                                          Template *data);
96 static GtkWidget *prefs_template_list_view_create       (void);
97 static void prefs_template_create_list_view_columns     (GtkWidget *list_view);
98 static gboolean prefs_template_selected                 (GtkTreeSelection *selector,
99                                                          GtkTreeModel *model, 
100                                                          GtkTreePath *path,
101                                                          gboolean currently_selected,
102                                                          gpointer data);
103
104 /* Called from mainwindow.c */
105 void prefs_template_open(void)
106 {
107         inc_lock();
108
109         if (!templates.window)
110                 prefs_template_window_create();
111
112         prefs_template_window_setup();
113         gtk_widget_show(templates.window);
114 }
115
116 /*!
117  *\brief        Save Gtk object size to prefs dataset
118  */
119 static void prefs_template_size_allocate_cb(GtkWidget *widget,
120                                          GtkAllocation *allocation)
121 {
122         g_return_if_fail(allocation != NULL);
123
124         prefs_common.templateswin_width = allocation->width;
125         prefs_common.templateswin_height = allocation->height;
126 }
127
128 static void prefs_template_window_create(void)
129 {
130         /* window structure ;) */
131         GtkWidget *window;
132         GtkWidget *vbox;
133         GtkWidget *scrolled_window;
134         GtkWidget   *vpaned;
135         GtkWidget     *vbox1;
136         GtkWidget       *table; /* including : entry_[name|to|cc|bcc|subject] */
137         GtkWidget       *scroll2;
138         GtkWidget         *text_value;
139         GtkWidget     *vbox2;
140         GtkWidget       *hbox2;
141         GtkWidget         *arrow1;
142         GtkWidget         *hbox3;
143         GtkWidget           *reg_btn;
144         GtkWidget           *subst_btn;
145         GtkWidget           *del_btn;
146         GtkWidget         *desc_btn;
147         GtkWidget       *hbox4;
148         GtkWidget         *scroll1;
149         GtkWidget           *list_view;
150         GtkWidget         *vbox3;
151         GtkWidget           *spc_vbox;
152         GtkWidget           *top_btn;
153         GtkWidget           *up_btn;
154         GtkWidget           *down_btn;
155         GtkWidget           *bottom_btn;
156         GtkWidget       *confirm_area;
157         GtkWidget         *help_btn;
158         GtkWidget         *cancel_btn;
159         GtkWidget         *ok_btn;
160         static GdkGeometry geometry;
161         gint i;
162         GtkTooltips *tooltips;
163
164         debug_print("Creating templates configuration window...\n");
165
166         /* main window */
167         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "prefs_template");
168         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
169         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
170         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
171
172         vbox = gtk_vbox_new(FALSE, 8);
173         gtk_widget_show(vbox);
174         gtk_container_add(GTK_CONTAINER(window), vbox);
175         gtk_container_set_border_width(GTK_CONTAINER(vbox), 4);
176
177         scrolled_window = gtk_scrolled_window_new (NULL, NULL);
178         gtk_widget_show(scrolled_window);
179         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
180                                         GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
181         gtk_container_add(GTK_CONTAINER(vbox), scrolled_window);
182
183         /* vpaned to separate template settings from templates list */
184         vpaned = gtk_vpaned_new();
185         gtk_widget_show(vpaned);
186         gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled_window),
187                                               vpaned);
188
189         /* vbox to handle template name and content */
190         vbox1 = gtk_vbox_new(FALSE, 6);
191         gtk_widget_show(vbox1);
192         gtk_container_set_border_width(GTK_CONTAINER(vbox1), 8);
193         gtk_paned_pack1(GTK_PANED(vpaned), vbox1, FALSE, FALSE);
194
195         table = gtk_table_new(5, 2, FALSE);
196         gtk_table_set_row_spacings (GTK_TABLE (table), VSPACING_NARROW_2);
197         gtk_table_set_col_spacings (GTK_TABLE (table), 4);
198         gtk_widget_show(table);
199         gtk_box_pack_start (GTK_BOX (vbox1), table, FALSE, FALSE, 0);
200
201         tooltips = gtk_tooltips_new();
202
203         struct
204         {
205                 gchar *label;
206                 GtkWidget **entry;
207                 gboolean compl;
208                 gchar *tooltips;
209         } tab[] = {
210                 {_("Name"),     &templates.entry_name,          FALSE,
211                         _("This name is used as the Menu item")},
212                 {_("To"),       &templates.entry_to,            TRUE,   NULL},
213                 {_("Cc"),       &templates.entry_cc,            TRUE,   NULL},
214                 {_("Bcc"),      &templates.entry_bcc,           TRUE,   NULL},
215                 {_("Subject"),  &templates.entry_subject,       FALSE,  NULL},
216                 {NULL,          NULL,                           FALSE,  NULL}
217         };
218
219         for (i=0; tab[i].label; i++) {
220
221                 GtkWidget *label;
222
223                 label = gtk_label_new(tab[i].label);
224                 gtk_widget_show(label);
225                 gtk_table_attach(GTK_TABLE(table), label, 0, 1, i, (i + 1),
226                                 (GtkAttachOptions) (GTK_FILL),
227                                 (GtkAttachOptions) 0, 0, 0);
228                 gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
229
230                 *(tab[i].entry) = gtk_entry_new();
231                 gtk_widget_show(*(tab[i].entry));
232                 gtk_table_attach(GTK_TABLE(table), *(tab[i].entry), 1, 2, i, (i + 1),
233                                 (GtkAttachOptions) (GTK_EXPAND|GTK_SHRINK|GTK_FILL),
234                                 (GtkAttachOptions) 0, 0, 0);
235                 gtk_tooltips_set_tip(tooltips, *(tab[i].entry), tab[i].tooltips, NULL);
236
237                 if (tab[i].compl)
238                         address_completion_register_entry(
239                                 GTK_ENTRY(*(tab[i].entry)), TRUE);
240         }
241
242         /* template content */
243         scroll2 = gtk_scrolled_window_new(NULL, NULL);
244         gtk_widget_show(scroll2);
245         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll2),
246                                        GTK_POLICY_AUTOMATIC,
247                                        GTK_POLICY_AUTOMATIC);
248         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll2),
249                                             GTK_SHADOW_IN);
250         gtk_box_pack_start(GTK_BOX(vbox1), scroll2, TRUE, TRUE, 0);
251
252         text_value = gtk_text_view_new();
253         if (prefs_common.textfont) {
254                 PangoFontDescription *font_desc;
255
256                 font_desc = pango_font_description_from_string
257                                                 (prefs_common.textfont);
258                 if (font_desc) {
259                         gtk_widget_modify_font(text_value, font_desc);
260                         pango_font_description_free(font_desc);
261                 }
262         }
263         gtk_widget_show(text_value);
264 #ifndef MAEMO
265         gtk_widget_set_size_request(text_value, -1, 120);
266 #else
267         gtk_widget_set_size_request(text_value, -1, 60);
268 #endif
269         gtk_container_add(GTK_CONTAINER(scroll2), text_value);
270         gtk_text_view_set_editable(GTK_TEXT_VIEW(text_value), TRUE);
271         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text_value), GTK_WRAP_WORD);
272
273         /* vbox for buttons and templates list */
274         vbox2 = gtk_vbox_new(FALSE, 6);
275         gtk_widget_show(vbox2);
276         gtk_container_set_border_width(GTK_CONTAINER(vbox2), 8);
277         gtk_paned_pack2(GTK_PANED(vpaned), vbox2, TRUE, FALSE);
278
279         /* register | substitute | delete */
280         hbox2 = gtk_hbox_new(FALSE, 4);
281         gtk_widget_show(hbox2);
282         gtk_box_pack_start(GTK_BOX(vbox2), hbox2, FALSE, FALSE, 0);
283
284         arrow1 = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
285         gtk_widget_show(arrow1);
286         gtk_box_pack_start(GTK_BOX(hbox2), arrow1, FALSE, FALSE, 0);
287         gtk_widget_set_size_request(arrow1, -1, 16);
288
289         hbox3 = gtk_hbox_new(TRUE, 4);
290         gtk_widget_show(hbox3);
291         gtk_box_pack_start(GTK_BOX(hbox2), hbox3, FALSE, FALSE, 0);
292
293         reg_btn = gtk_button_new_from_stock(GTK_STOCK_ADD);
294         gtk_widget_show(reg_btn);
295         gtk_box_pack_start(GTK_BOX(hbox3), reg_btn, FALSE, TRUE, 0);
296         g_signal_connect(G_OBJECT (reg_btn), "clicked",
297                          G_CALLBACK (prefs_template_register_cb), NULL);
298
299         subst_btn = gtkut_get_replace_btn(_("Replace"));
300         gtk_widget_show(subst_btn);
301         gtk_box_pack_start(GTK_BOX(hbox3), subst_btn, FALSE, TRUE, 0);
302         g_signal_connect(G_OBJECT(subst_btn), "clicked",
303                          G_CALLBACK(prefs_template_substitute_cb),
304                          NULL);
305
306         del_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
307         gtk_widget_show(del_btn);
308         gtk_box_pack_start(GTK_BOX(hbox3), del_btn, FALSE, TRUE, 0);
309         g_signal_connect(G_OBJECT(del_btn), "clicked",
310                          G_CALLBACK(prefs_template_delete_cb), NULL);
311
312 #if GTK_CHECK_VERSION(2, 8, 0)
313         desc_btn = gtk_button_new_from_stock(GTK_STOCK_INFO);
314 #else
315         desc_btn = gtk_button_new_with_label(_(" Symbols... "));
316 #endif
317         gtk_widget_show(desc_btn);
318         gtk_box_pack_end(GTK_BOX(hbox2), desc_btn, FALSE, FALSE, 0);
319         g_signal_connect(G_OBJECT(desc_btn), "clicked",
320                          G_CALLBACK(quote_fmt_quote_description), NULL);
321
322         /* templates list */
323         hbox4 = gtk_hbox_new(FALSE, 8);
324         gtk_widget_show(hbox4);
325         gtk_box_pack_start(GTK_BOX(vbox2), hbox4, TRUE, TRUE, 0);
326
327         scroll1 = gtk_scrolled_window_new(NULL, NULL);
328         gtk_widget_show(scroll1);
329         gtk_box_pack_start(GTK_BOX(hbox4), scroll1, TRUE, TRUE, 0);
330         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll1),
331                                        GTK_POLICY_AUTOMATIC,
332                                        GTK_POLICY_AUTOMATIC);
333                                        
334         vbox3 = gtk_vbox_new(FALSE, 8);
335         gtk_widget_show(vbox3);
336         gtk_box_pack_start(GTK_BOX(hbox4), vbox3, FALSE, FALSE, 0);
337
338         top_btn = gtk_button_new_from_stock(GTK_STOCK_GOTO_TOP);
339         gtk_widget_show(top_btn);
340         gtk_box_pack_start(GTK_BOX(vbox3), top_btn, FALSE, FALSE, 0);
341         g_signal_connect(G_OBJECT(top_btn), "clicked",
342                          G_CALLBACK(prefs_template_top_cb), NULL);
343
344         PACK_VSPACER(vbox3, spc_vbox, VSPACING_NARROW_2);
345
346         up_btn = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
347         gtk_widget_show(up_btn);
348         gtk_box_pack_start (GTK_BOX(vbox3), up_btn, FALSE, FALSE, 0);
349         g_signal_connect(G_OBJECT(up_btn), "clicked",
350                          G_CALLBACK(prefs_template_up_cb), NULL);
351
352         down_btn = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
353         gtk_widget_show (down_btn);
354         gtk_box_pack_start(GTK_BOX (vbox3), down_btn, FALSE, FALSE, 0);
355         g_signal_connect(G_OBJECT (down_btn), "clicked",
356                          G_CALLBACK(prefs_template_down_cb), NULL);
357
358         PACK_VSPACER(vbox3, spc_vbox, VSPACING_NARROW_2);
359
360         bottom_btn = gtk_button_new_from_stock(GTK_STOCK_GOTO_BOTTOM);
361         gtk_widget_show(bottom_btn);
362         gtk_box_pack_start(GTK_BOX(vbox3), bottom_btn, FALSE, FALSE, 0);
363         g_signal_connect(G_OBJECT(bottom_btn), "clicked",
364                          G_CALLBACK(prefs_template_bottom_cb), NULL);
365
366         list_view = prefs_template_list_view_create();
367         gtk_widget_show(list_view);
368         gtk_widget_set_size_request(scroll1, -1, 140);
369         gtk_container_add(GTK_CONTAINER(scroll1), list_view);
370
371         /* help | cancel | ok */
372         gtkut_stock_button_set_create_with_help(&confirm_area, &help_btn,
373                         &cancel_btn, GTK_STOCK_CANCEL,
374                         &ok_btn, GTK_STOCK_OK,
375                         NULL, NULL);
376         gtk_widget_show(confirm_area);
377         gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
378         gtk_widget_grab_default(ok_btn);
379
380         gtk_window_set_title(GTK_WINDOW(window), _("Template configuration"));
381
382         g_signal_connect(G_OBJECT(window), "delete_event",
383                          G_CALLBACK(prefs_template_deleted_cb), NULL);
384         g_signal_connect(G_OBJECT(window), "size_allocate",
385                          G_CALLBACK(prefs_template_size_allocate_cb), NULL);
386         g_signal_connect(G_OBJECT(window), "key_press_event",
387                          G_CALLBACK(prefs_template_key_pressed_cb), NULL);
388         MANAGE_WINDOW_SIGNALS_CONNECT(window);
389         g_signal_connect(G_OBJECT(ok_btn), "clicked",
390                          G_CALLBACK(prefs_template_ok_cb), NULL);
391         g_signal_connect(G_OBJECT(cancel_btn), "clicked",
392                          G_CALLBACK(prefs_template_cancel_cb), NULL);
393         g_signal_connect(G_OBJECT(help_btn), "clicked",
394                          G_CALLBACK(manual_open_with_anchor_cb),
395                          MANUAL_ANCHOR_TEMPLATES);
396
397         address_completion_start(window);
398
399         if (!geometry.min_height) {
400                 geometry.min_width = 480;
401                 geometry.min_height = 540;
402         }
403
404         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
405                                       GDK_HINT_MIN_SIZE);
406         gtk_widget_set_size_request(window, prefs_common.templateswin_width,
407                                     prefs_common.templateswin_height);
408
409         templates.window = window;
410         templates.ok_btn = ok_btn;
411         templates.list_view = list_view;
412         templates.text_value = text_value;
413 }
414
415 static void prefs_template_window_setup(void)
416 {
417         GSList *tmpl_list;
418         GSList *cur;
419         Template *tmpl;
420         GtkListStore *store;
421         GtkTextBuffer *buffer;
422         
423         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
424                                 (templates.list_view)));
425
426         manage_window_set_transient(GTK_WINDOW(templates.window));
427         gtk_widget_grab_focus(templates.ok_btn);
428
429         gtk_list_store_clear(store);
430
431         prefs_template_list_view_insert_template(templates.list_view,
432                                                  NULL, _("(New)"),
433                                                  NULL);
434         
435         tmpl_list = template_read_config();
436
437         for (cur = tmpl_list; cur != NULL; cur = cur->next) {
438                 tmpl = (Template *)cur->data;
439                 prefs_template_list_view_insert_template(templates.list_view,
440                                                          NULL, tmpl->name, 
441                                                          tmpl);
442         }
443
444         gtk_entry_set_text(GTK_ENTRY(templates.entry_name), "");
445         gtk_entry_set_text(GTK_ENTRY(templates.entry_to), "");
446         gtk_entry_set_text(GTK_ENTRY(templates.entry_cc), "");
447         gtk_entry_set_text(GTK_ENTRY(templates.entry_bcc), "");                 
448         gtk_entry_set_text(GTK_ENTRY(templates.entry_subject), "");
449         
450         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
451         gtk_text_buffer_set_text(buffer, "", -1);
452
453         g_slist_free(tmpl_list);
454 }
455
456 static void prefs_template_clear(void)
457 {
458         GtkListStore *store;
459
460         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
461                                 (templates.list_view)));
462         gtk_list_store_clear(store);
463 }
464
465 static gint prefs_template_deleted_cb(GtkWidget *widget, GdkEventAny *event,
466                                       gpointer data)
467 {
468         prefs_template_cancel_cb();
469         return TRUE;
470 }
471
472 static gboolean prefs_template_key_pressed_cb(GtkWidget *widget,
473                                               GdkEventKey *event, gpointer data)
474 {
475         if (event && event->keyval == GDK_Escape)
476                 prefs_template_cancel_cb();
477         else {
478                 GtkWidget *focused = gtkut_get_focused_child(
479                                         GTK_CONTAINER(widget));
480                 if (focused && GTK_IS_EDITABLE(focused)) {
481                         modified = TRUE;
482                 }
483         }
484         return FALSE;
485 }
486
487 static void prefs_template_ok_cb(void)
488 {
489         GSList *tmpl_list;
490
491         if (modified && alertpanel(_("Entry not saved"),
492                                  _("The entry was not saved. Close anyway?"),
493                                  GTK_STOCK_CLOSE, _("+_Continue editing"), 
494                                  NULL) != G_ALERTDEFAULT) {
495                 return;
496         } else if (modified_list && alertpanel(_("Templates list not saved"),
497                                  _("The templates list has been modified. Close anyway?"),
498                                  GTK_STOCK_CLOSE, _("+_Continue editing"), 
499                                  NULL) != G_ALERTDEFAULT) {
500                 return;
501         }
502         modified = FALSE;
503         modified_list = FALSE;
504         tmpl_list = prefs_template_get_list();
505         template_set_config(tmpl_list);
506         compose_reflect_prefs_all();
507         prefs_template_clear();
508         gtk_widget_hide(templates.window);
509         inc_unlock();
510 }
511
512 static void prefs_template_cancel_cb(void)
513 {
514         if (modified && alertpanel(_("Entry not saved"),
515                                  _("The entry was not saved. Close anyway?"),
516                                  GTK_STOCK_CLOSE, _("+_Continue editing"),
517                                  NULL) != G_ALERTDEFAULT) {
518                 return;
519         } else if (modified_list && alertpanel(_("Templates list not saved"),
520                                  _("The templates list has been modified. Close anyway?"),
521                                  GTK_STOCK_CLOSE, _("+_Continue editing"), 
522                                  NULL) != G_ALERTDEFAULT) {
523                 return;
524         }
525         modified = FALSE;
526         modified_list = FALSE;
527         prefs_template_clear();
528         gtk_widget_hide(templates.window);
529         inc_unlock();
530 }
531
532 /*!
533  *\brief        Request list for storage. New list is owned
534  *              by template.c...
535  */
536 static GSList *prefs_template_get_list(void)
537 {
538         GSList *tmpl_list = NULL;
539         Template *tmpl;
540         GtkTreeModel *model;
541         GtkTreeIter iter;
542
543         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
544         if (!gtk_tree_model_get_iter_first(model, &iter))
545                 return NULL;
546
547         do {
548                 gtk_tree_model_get(model, &iter,
549                                    TEMPL_DATA, &tmpl,
550                                    -1);
551                 
552                 if (tmpl) {
553                         Template *ntmpl;
554                         
555                         ntmpl = g_new(Template, 1);
556                         ntmpl->load_filename = NULL;
557                         ntmpl->name    = tmpl->name && *(tmpl->name) 
558                                          ? g_strdup(tmpl->name) 
559                                          : NULL;
560                         ntmpl->subject = tmpl->subject && *(tmpl->subject) 
561                                          ? g_strdup(tmpl->subject) 
562                                          : NULL;
563                         ntmpl->to      = tmpl->to && *(tmpl->to)
564                                          ? g_strdup(tmpl->to)
565                                          : NULL;
566                         ntmpl->cc      = tmpl->cc && *(tmpl->cc)
567                                          ? g_strdup(tmpl->cc)
568                                          : NULL;
569                         ntmpl->bcc     = tmpl->bcc && *(tmpl->bcc)
570                                          ? g_strdup(tmpl->bcc)
571                                          : NULL;        
572                         ntmpl->value   = tmpl->value && *(tmpl->value)
573                                          ? g_strdup(tmpl->value)
574                                          : NULL;
575                         tmpl_list = g_slist_append(tmpl_list, ntmpl);
576                 }                       
577         
578         } while (gtk_tree_model_iter_next(model, &iter)); 
579
580         return tmpl_list;
581 }
582
583 gboolean prefs_template_string_is_valid(gchar *string, gint *line)
584 {
585         if (string && *string != '\0') {
586                 gchar *parsed_buf;
587                 MsgInfo dummyinfo;
588
589                 memset(&dummyinfo, 0, sizeof(MsgInfo));
590 #ifdef USE_ASPELL
591                 quote_fmt_init(&dummyinfo, NULL, NULL, TRUE, NULL, NULL);
592 #else
593                 quote_fmt_init(&dummyinfo, NULL, NULL, TRUE, NULL);
594 #endif
595                 quote_fmt_scan_string(string);
596                 quote_fmt_parse();
597                 parsed_buf = quote_fmt_get_buffer();
598                 if (!parsed_buf) {
599                         if (line)
600                                 *line = quote_fmt_get_line();
601                         return FALSE;
602                 }
603                 quote_fmt_reset_vartable();
604         }
605         return TRUE;
606 }
607
608 static gboolean prefs_template_list_view_set_row(GtkTreeIter *row)
609 /* return TRUE if the row could be modified */
610 {
611         Template *tmpl;
612         gchar *name;
613         gchar *subject;
614         gchar *to;
615         gchar *cc;
616         gchar *bcc;     
617         gchar *value;
618         GtkTextBuffer *buffer;
619         GtkTextIter start, end;
620         GtkTreeModel *model;
621         gint line;
622
623         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
624
625         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
626         gtk_text_buffer_get_start_iter(buffer, &start);
627         gtk_text_buffer_get_iter_at_offset(buffer, &end, -1);
628         value = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
629
630         if (value && *value == '\0') {
631                         g_free(value);
632                 value = NULL;
633                 }
634         if (!prefs_template_string_is_valid(value, &line)) {
635                 alertpanel_error(_("Template body format error at line %d."), line);
636                 g_free(value);
637                 return FALSE;
638         }
639
640         name = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_name),
641                                       0, -1);
642         if (*name == '\0') {
643                 alertpanel_error(_("Template name is not set."));
644                 g_free(value);
645                 return FALSE;
646         }
647         to = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_to),
648                                     0, -1);
649         cc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_cc),
650                                     0, -1);
651         bcc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_bcc),
652                                     0, -1);
653         subject = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_subject),
654                                          0, -1);
655
656         if (to && *to == '\0') {
657                 g_free(to);
658                 to = NULL;
659         }
660         if (cc && *cc == '\0') {
661                 g_free(cc);
662                 cc = NULL;
663         }
664         if (bcc && *bcc == '\0') {
665                 g_free(bcc);
666                 bcc = NULL;
667         }
668         if (subject && *subject == '\0') {
669                 g_free(subject);
670                 subject = NULL;
671         }
672
673         if (!prefs_template_string_is_valid(to, NULL)) {
674                 alertpanel_error(_("Template To format error."));
675                 g_free(to);
676                 g_free(value);
677                 return FALSE;
678         }
679         if (!prefs_template_string_is_valid(cc, NULL)) {
680                 alertpanel_error(_("Template Cc format error."));       
681                 g_free(cc);
682                 g_free(value);
683                 return FALSE;
684         }
685         if (!prefs_template_string_is_valid(bcc, NULL)) {
686                 alertpanel_error(_("Template Bcc format error."));      
687                 g_free(bcc);
688                 g_free(value);
689                 return FALSE;
690         }
691         if (!prefs_template_string_is_valid(subject, NULL)) {
692                 alertpanel_error(_("Template subject format error."));  
693                 g_free(subject);
694                 g_free(value);
695                 return FALSE;
696         }
697         
698         tmpl = g_new(Template, 1);
699         tmpl->load_filename = NULL;
700         tmpl->name = name;
701         tmpl->subject = subject;
702         tmpl->to = to;
703         tmpl->cc = cc;
704         tmpl->bcc = bcc;        
705         tmpl->value = value;
706
707         prefs_template_list_view_insert_template(templates.list_view,
708                                                  row, tmpl->name, tmpl);
709
710         return TRUE;
711 }
712
713 static void prefs_template_register_cb(void)
714 {
715         modified = !prefs_template_list_view_set_row(NULL);
716         modified_list = TRUE;
717 }
718
719 static void prefs_template_substitute_cb(void)
720 {
721         Template *tmpl;
722         GtkTreeIter row;
723         GtkTreeSelection *selection;
724         GtkTreeModel *model;
725
726         selection = gtk_tree_view_get_selection
727                         (GTK_TREE_VIEW(templates.list_view));
728         
729         if (!gtk_tree_selection_get_selected(selection, &model, &row))
730                 return;
731
732         gtk_tree_model_get(model, &row, 
733                            TEMPL_DATA, &tmpl,
734                            -1);
735
736         if (!tmpl) return;
737
738         modified = !prefs_template_list_view_set_row(&row);
739         modified_list = TRUE;
740 }
741
742 static void prefs_template_delete_cb(void)
743 {
744         Template *tmpl;
745         GtkTreeIter row;
746         GtkTreeSelection *selection;
747         GtkTreeModel *model;
748
749         selection = gtk_tree_view_get_selection
750                         (GTK_TREE_VIEW(templates.list_view));
751         
752         if (!gtk_tree_selection_get_selected(selection, &model, &row))
753                 return;
754
755         gtk_tree_model_get(model, &row, 
756                            TEMPL_DATA, &tmpl,
757                            -1);
758
759         if (!tmpl) 
760                 return;
761
762         if (alertpanel(_("Delete template"),
763                        _("Do you really want to delete this template?"),
764                        GTK_STOCK_CANCEL, GTK_STOCK_DELETE,
765                        NULL) != G_ALERTALTERNATE)
766                 return;
767
768         gtk_list_store_remove(GTK_LIST_STORE(model), &row);
769         modified_list = TRUE;   
770 }
771
772 static void prefs_template_top_cb(void)
773 {
774         gint row;
775         GtkTreeIter top, sel;
776         GtkTreeModel *model;
777
778         row = gtkut_list_view_get_selected_row(templates.list_view);
779         if (row <= 1) 
780                 return;
781
782         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));            
783         
784         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, 0)
785         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
786                 return;
787
788         gtk_list_store_move_after(GTK_LIST_STORE(model), &sel, &top);
789         gtkut_list_view_select_row(templates.list_view, 1);
790         modified_list = TRUE;
791 }
792
793 static void prefs_template_up_cb(void)
794 {
795         gint row;
796         GtkTreeIter top, sel;
797         GtkTreeModel *model;
798
799         row = gtkut_list_view_get_selected_row(templates.list_view);
800         if (row <= 1) 
801                 return;
802                 
803         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));    
804
805         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row - 1)
806         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
807                 return;
808
809         gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
810         gtkut_list_view_select_row(templates.list_view, row - 1);
811         modified_list = TRUE;
812 }
813
814 static void prefs_template_down_cb(void)
815 {
816         gint row, n_rows;
817         GtkTreeIter top, sel;
818         GtkTreeModel *model;
819
820         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));    
821         n_rows = gtk_tree_model_iter_n_children(model, NULL);
822         row = gtkut_list_view_get_selected_row(templates.list_view);
823         if (row < 1 || row >= n_rows - 1)
824                 return;
825
826         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
827         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row + 1))
828                 return;
829                         
830         gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
831         gtkut_list_view_select_row(templates.list_view, row + 1);
832         modified_list = TRUE;
833 }
834
835 static void prefs_template_bottom_cb(void)
836 {
837         gint row, n_rows;
838         GtkTreeIter top, sel;
839         GtkTreeModel *model;
840
841         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));    
842         n_rows = gtk_tree_model_iter_n_children(model, NULL);
843         row = gtkut_list_view_get_selected_row(templates.list_view);
844         if (row < 1 || row >= n_rows - 1)
845                 return;
846
847         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
848         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, n_rows - 1))
849                 return;
850
851         gtk_list_store_move_after(GTK_LIST_STORE(model), &top, &sel);           
852         gtkut_list_view_select_row(templates.list_view, n_rows - 1);
853         modified_list = TRUE;
854 }
855
856 static GtkListStore* prefs_template_create_data_store(void)
857 {
858         return gtk_list_store_new(N_TEMPL_COLUMNS,
859                                   G_TYPE_STRING,        
860                                   G_TYPE_POINTER,
861                                   G_TYPE_AUTO_POINTER,
862                                   -1);
863 }
864
865 static void prefs_template_list_view_insert_template(GtkWidget *list_view,
866                                                      GtkTreeIter *row_iter,
867                                                      const gchar *template,
868                                                      Template *data)
869 {
870         GtkTreeIter iter;
871         GtkTreeModel *model = gtk_tree_view_get_model
872                                         (GTK_TREE_VIEW(list_view));
873         GtkListStore *list_store = GTK_LIST_STORE(model);
874         GAuto *auto_data;                                       
875
876         if (row_iter == NULL) {
877                 /* append new */
878                 gtk_list_store_append(list_store, &iter);
879         } else {
880                 /* modify the existing */
881                 iter = *row_iter;
882         }
883                 
884         auto_data = g_auto_pointer_new_with_free(data, (GFreeFunc) template_free);  
885
886         /* if replacing data in an existing row, the auto pointer takes care
887          * of destroying the Template data */
888         gtk_list_store_set(list_store, &iter,
889                            TEMPL_TEXT, template,
890                            TEMPL_DATA, data,
891                            TEMPL_AUTO_DATA, auto_data,
892                            -1);
893
894         g_auto_pointer_free(auto_data);                    
895 }
896
897 static GtkWidget *prefs_template_list_view_create(void)
898 {
899         GtkTreeView *list_view;
900         GtkTreeSelection *selector;
901         GtkTreeModel *model;
902
903         model = GTK_TREE_MODEL(prefs_template_create_data_store());
904         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
905         g_object_unref(model);  
906         
907         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
908         gtk_tree_view_set_reorderable(list_view, TRUE);
909
910         selector = gtk_tree_view_get_selection(list_view);
911         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
912         gtk_tree_selection_set_select_function(selector, prefs_template_selected,
913                                                NULL, NULL);
914
915         /* create the columns */
916         prefs_template_create_list_view_columns(GTK_WIDGET(list_view));
917
918         return GTK_WIDGET(list_view);
919 }
920
921 static void prefs_template_create_list_view_columns(GtkWidget *list_view)
922 {
923         GtkTreeViewColumn *column;
924         GtkCellRenderer *renderer;
925
926         renderer = gtk_cell_renderer_text_new();
927         column = gtk_tree_view_column_new_with_attributes
928                         (_("Current templates"),
929                          renderer,
930                          "text", TEMPL_TEXT,
931                          NULL);
932         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
933 }
934
935 static gboolean prefs_template_selected(GtkTreeSelection *selector,
936                                         GtkTreeModel *model, 
937                                         GtkTreePath *path,
938                                         gboolean currently_selected,
939                                         gpointer data)
940 {
941         Template *tmpl;
942         Template tmpl_def;
943         GtkTextBuffer *buffer;
944         GtkTextIter iter;
945         GtkTreeIter titer;
946
947         if (currently_selected)
948                 return TRUE;
949
950         if (!gtk_tree_model_get_iter(model, &titer, path))
951                 return TRUE;
952
953         tmpl_def.name = _("Template");
954         tmpl_def.subject = "";
955         tmpl_def.to = "";
956         tmpl_def.cc = "";
957         tmpl_def.bcc = "";      
958         tmpl_def.value = "";
959
960         gtk_tree_model_get(model, &titer,
961                            TEMPL_DATA, &tmpl,
962                            -1);
963
964         if (!tmpl) 
965                 tmpl = &tmpl_def;
966
967         gtk_entry_set_text(GTK_ENTRY(templates.entry_name), tmpl->name);
968         gtk_entry_set_text(GTK_ENTRY(templates.entry_to),
969                            tmpl->to ? tmpl->to : "");
970         gtk_entry_set_text(GTK_ENTRY(templates.entry_cc),
971                            tmpl->cc ? tmpl->cc : "");
972         gtk_entry_set_text(GTK_ENTRY(templates.entry_bcc),
973                            tmpl->bcc ? tmpl->bcc : "");                 
974         gtk_entry_set_text(GTK_ENTRY(templates.entry_subject),
975                            tmpl->subject ? tmpl->subject : "");
976         
977         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
978         gtk_text_buffer_set_text(buffer, "", -1);
979         gtk_text_buffer_get_start_iter(buffer, &iter);
980         gtk_text_buffer_insert(buffer, &iter, tmpl->value, -1);
981
982         return TRUE;
983 }