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