bb78b5d76e7322fa58f3fe26f816394e8c420785
[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
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         }
479         modified = FALSE;
480         tmpl_list = prefs_template_get_list();
481         template_set_config(tmpl_list);
482         compose_reflect_prefs_all();
483         prefs_template_clear();
484         gtk_widget_hide(templates.window);
485         inc_unlock();
486 }
487
488 static void prefs_template_cancel_cb(void)
489 {
490         if (modified && alertpanel(_("Entry not saved"),
491                                  _("The entry was not saved. Close anyway?"),
492                                  GTK_STOCK_CLOSE, _("+_Continue editing"),
493                                  NULL) != G_ALERTDEFAULT) {
494                 return;
495         }
496         modified = FALSE;
497         prefs_template_clear();
498         gtk_widget_hide(templates.window);
499         inc_unlock();
500 }
501
502 /*!
503  *\brief        Request list for storage. New list is owned
504  *              by template.c...
505  */
506 static GSList *prefs_template_get_list(void)
507 {
508         GSList *tmpl_list = NULL;
509         Template *tmpl;
510         GtkTreeModel *model;
511         GtkTreeIter iter;
512
513         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
514         if (!gtk_tree_model_get_iter_first(model, &iter))
515                 return NULL;
516
517         do {
518                 gtk_tree_model_get(model, &iter,
519                                    TEMPL_DATA, &tmpl,
520                                    -1);
521                 
522                 if (tmpl) {
523                         Template *ntmpl;
524                         
525                         ntmpl = g_new(Template, 1);
526                         ntmpl->load_filename = NULL;
527                         ntmpl->name    = tmpl->name && *(tmpl->name) 
528                                          ? g_strdup(tmpl->name) 
529                                          : NULL;
530                         ntmpl->subject = tmpl->subject && *(tmpl->subject) 
531                                          ? g_strdup(tmpl->subject) 
532                                          : NULL;
533                         ntmpl->to      = tmpl->to && *(tmpl->to)
534                                          ? g_strdup(tmpl->to)
535                                          : NULL;
536                         ntmpl->cc      = tmpl->cc && *(tmpl->cc)
537                                          ? g_strdup(tmpl->cc)
538                                          : NULL;
539                         ntmpl->bcc     = tmpl->bcc && *(tmpl->bcc)
540                                          ? g_strdup(tmpl->bcc)
541                                          : NULL;        
542                         ntmpl->value   = tmpl->value && *(tmpl->value)
543                                          ? g_strdup(tmpl->value)
544                                          : NULL;
545                         tmpl_list = g_slist_append(tmpl_list, ntmpl);
546                 }                       
547         
548         } while (gtk_tree_model_iter_next(model, &iter)); 
549
550         return tmpl_list;
551 }
552
553 gboolean prefs_template_string_is_valid(gchar *string, gint *line)
554 {
555         if (string && *string != '\0') {
556                 gchar *parsed_buf;
557                 MsgInfo dummyinfo;
558
559                 memset(&dummyinfo, 0, sizeof(MsgInfo));
560 #ifdef USE_ASPELL
561                 quote_fmt_init(&dummyinfo, NULL, NULL, TRUE, NULL, NULL);
562 #else
563                 quote_fmt_init(&dummyinfo, NULL, NULL, TRUE, NULL);
564 #endif
565                 quote_fmt_scan_string(string);
566                 quote_fmt_parse();
567                 parsed_buf = quote_fmt_get_buffer();
568                 if (!parsed_buf) {
569                         if (line)
570                                 *line = quote_fmt_get_line();
571                         return FALSE;
572                 }
573                 quote_fmt_reset_vartable();
574         }
575         return TRUE;
576 }
577
578 static gboolean prefs_template_list_view_set_row(GtkTreeIter *row)
579 /* return TRUE if the row could be modified */
580 {
581         Template *tmpl;
582         gchar *name;
583         gchar *subject;
584         gchar *to;
585         gchar *cc;
586         gchar *bcc;     
587         gchar *value;
588         GtkTextBuffer *buffer;
589         GtkTextIter start, end;
590         GtkTreeModel *model;
591         gint line;
592
593         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
594
595         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
596         gtk_text_buffer_get_start_iter(buffer, &start);
597         gtk_text_buffer_get_iter_at_offset(buffer, &end, -1);
598         value = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
599
600         if (value && *value == '\0') {
601                         g_free(value);
602                 value = NULL;
603                 }
604         if (!prefs_template_string_is_valid(value, &line)) {
605                 alertpanel_error(_("Template body format error at line %d."), line);
606                 g_free(value);
607                 return FALSE;
608         }
609
610         name = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_name),
611                                       0, -1);
612         if (*name == '\0') {
613                 alertpanel_error(_("Template name is not set."));
614                 g_free(value);
615                 return FALSE;
616         }
617         to = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_to),
618                                     0, -1);
619         cc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_cc),
620                                     0, -1);
621         bcc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_bcc),
622                                     0, -1);
623         subject = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_subject),
624                                          0, -1);
625
626         if (to && *to == '\0') {
627                 g_free(to);
628                 to = NULL;
629         }
630         if (cc && *cc == '\0') {
631                 g_free(cc);
632                 cc = NULL;
633         }
634         if (bcc && *bcc == '\0') {
635                 g_free(bcc);
636                 bcc = NULL;
637         }
638         if (subject && *subject == '\0') {
639                 g_free(subject);
640                 subject = NULL;
641         }
642
643         if (!prefs_template_string_is_valid(to, NULL)) {
644                 alertpanel_error(_("Template To format error."));
645                 g_free(to);
646                 g_free(value);
647                 return FALSE;
648         }
649         if (!prefs_template_string_is_valid(cc, NULL)) {
650                 alertpanel_error(_("Template Cc format error."));       
651                 g_free(cc);
652                 g_free(value);
653                 return FALSE;
654         }
655         if (!prefs_template_string_is_valid(bcc, NULL)) {
656                 alertpanel_error(_("Template Bcc format error."));      
657                 g_free(bcc);
658                 g_free(value);
659                 return FALSE;
660         }
661         if (!prefs_template_string_is_valid(subject, NULL)) {
662                 alertpanel_error(_("Template subject format error."));  
663                 g_free(subject);
664                 g_free(value);
665                 return FALSE;
666         }
667         
668         tmpl = g_new(Template, 1);
669         tmpl->load_filename = NULL;
670         tmpl->name = name;
671         tmpl->subject = subject;
672         tmpl->to = to;
673         tmpl->cc = cc;
674         tmpl->bcc = bcc;        
675         tmpl->value = value;
676
677         prefs_template_list_view_insert_template(templates.list_view,
678                                                  row, tmpl->name, tmpl);
679
680         return TRUE;
681 }
682
683 static void prefs_template_register_cb(void)
684 {
685         modified = !prefs_template_list_view_set_row(NULL);
686 }
687
688 static void prefs_template_substitute_cb(void)
689 {
690         Template *tmpl;
691         GtkTreeIter row;
692         GtkTreeSelection *selection;
693         GtkTreeModel *model;
694
695         selection = gtk_tree_view_get_selection
696                         (GTK_TREE_VIEW(templates.list_view));
697         
698         if (!gtk_tree_selection_get_selected(selection, &model, &row))
699                 return;
700
701         gtk_tree_model_get(model, &row, 
702                            TEMPL_DATA, &tmpl,
703                            -1);
704
705         if (!tmpl) return;
706
707         modified = !prefs_template_list_view_set_row(&row);
708 }
709
710 static void prefs_template_delete_cb(void)
711 {
712         Template *tmpl;
713         GtkTreeIter row;
714         GtkTreeSelection *selection;
715         GtkTreeModel *model;
716
717         selection = gtk_tree_view_get_selection
718                         (GTK_TREE_VIEW(templates.list_view));
719         
720         if (!gtk_tree_selection_get_selected(selection, &model, &row))
721                 return;
722
723         gtk_tree_model_get(model, &row, 
724                            TEMPL_DATA, &tmpl,
725                            -1);
726
727         if (!tmpl) 
728                 return;
729
730         if (alertpanel(_("Delete template"),
731                        _("Do you really want to delete this template?"),
732                        GTK_STOCK_CANCEL, GTK_STOCK_DELETE,
733                        NULL) != G_ALERTALTERNATE)
734                 return;
735
736         gtk_list_store_remove(GTK_LIST_STORE(model), &row);             
737 }
738
739 static void prefs_template_top_cb(void)
740 {
741         gint row;
742         GtkTreeIter top, sel;
743         GtkTreeModel *model;
744
745         row = gtkut_list_view_get_selected_row(templates.list_view);
746         if (row <= 1) 
747                 return;
748
749         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));            
750         
751         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, 0)
752         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
753                 return;
754
755         gtk_list_store_move_after(GTK_LIST_STORE(model), &sel, &top);
756         gtkut_list_view_select_row(templates.list_view, 1);
757 }
758
759 static void prefs_template_up_cb(void)
760 {
761         gint row;
762         GtkTreeIter top, sel;
763         GtkTreeModel *model;
764
765         row = gtkut_list_view_get_selected_row(templates.list_view);
766         if (row <= 1) 
767                 return;
768                 
769         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));    
770
771         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row - 1)
772         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
773                 return;
774
775         gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
776         gtkut_list_view_select_row(templates.list_view, row - 1);
777 }
778
779 static void prefs_template_down_cb(void)
780 {
781         gint row, n_rows;
782         GtkTreeIter top, sel;
783         GtkTreeModel *model;
784
785         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));    
786         n_rows = gtk_tree_model_iter_n_children(model, NULL);
787         row = gtkut_list_view_get_selected_row(templates.list_view);
788         if (row < 1 || row >= n_rows - 1)
789                 return;
790
791         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
792         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row + 1))
793                 return;
794                         
795         gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
796         gtkut_list_view_select_row(templates.list_view, row + 1);
797 }
798
799 static void prefs_template_bottom_cb(void)
800 {
801         gint row, n_rows;
802         GtkTreeIter top, sel;
803         GtkTreeModel *model;
804
805         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));    
806         n_rows = gtk_tree_model_iter_n_children(model, NULL);
807         row = gtkut_list_view_get_selected_row(templates.list_view);
808         if (row < 1 || row >= n_rows - 1)
809                 return;
810
811         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
812         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, n_rows - 1))
813                 return;
814
815         gtk_list_store_move_after(GTK_LIST_STORE(model), &top, &sel);           
816         gtkut_list_view_select_row(templates.list_view, n_rows - 1);
817 }
818
819 static GtkListStore* prefs_template_create_data_store(void)
820 {
821         return gtk_list_store_new(N_TEMPL_COLUMNS,
822                                   G_TYPE_STRING,        
823                                   G_TYPE_POINTER,
824                                   G_TYPE_AUTO_POINTER,
825                                   -1);
826 }
827
828 static void prefs_template_list_view_insert_template(GtkWidget *list_view,
829                                                      GtkTreeIter *row_iter,
830                                                      const gchar *template,
831                                                      Template *data)
832 {
833         GtkTreeIter iter;
834         GtkTreeModel *model = gtk_tree_view_get_model
835                                         (GTK_TREE_VIEW(list_view));
836         GtkListStore *list_store = GTK_LIST_STORE(model);
837         GAuto *auto_data;                                       
838
839         if (row_iter == NULL) {
840                 /* append new */
841                 gtk_list_store_append(list_store, &iter);
842         } else {
843                 /* modify the existing */
844                 iter = *row_iter;
845         }
846                 
847         auto_data = g_auto_pointer_new_with_free(data, (GFreeFunc) template_free);  
848
849         /* if replacing data in an existing row, the auto pointer takes care
850          * of destroying the Template data */
851         gtk_list_store_set(list_store, &iter,
852                            TEMPL_TEXT, template,
853                            TEMPL_DATA, data,
854                            TEMPL_AUTO_DATA, auto_data,
855                            -1);
856
857         g_auto_pointer_free(auto_data);                    
858 }
859
860 static GtkWidget *prefs_template_list_view_create(void)
861 {
862         GtkTreeView *list_view;
863         GtkTreeSelection *selector;
864         GtkTreeModel *model;
865
866         model = GTK_TREE_MODEL(prefs_template_create_data_store());
867         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
868         g_object_unref(model);  
869         
870         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
871         gtk_tree_view_set_reorderable(list_view, TRUE);
872
873         selector = gtk_tree_view_get_selection(list_view);
874         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
875         gtk_tree_selection_set_select_function(selector, prefs_template_selected,
876                                                NULL, NULL);
877
878         /* create the columns */
879         prefs_template_create_list_view_columns(GTK_WIDGET(list_view));
880
881         return GTK_WIDGET(list_view);
882 }
883
884 static void prefs_template_create_list_view_columns(GtkWidget *list_view)
885 {
886         GtkTreeViewColumn *column;
887         GtkCellRenderer *renderer;
888
889         renderer = gtk_cell_renderer_text_new();
890         column = gtk_tree_view_column_new_with_attributes
891                         (_("Current templates"),
892                          renderer,
893                          "text", TEMPL_TEXT,
894                          NULL);
895         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
896 }
897
898 static gboolean prefs_template_selected(GtkTreeSelection *selector,
899                                         GtkTreeModel *model, 
900                                         GtkTreePath *path,
901                                         gboolean currently_selected,
902                                         gpointer data)
903 {
904         Template *tmpl;
905         Template tmpl_def;
906         GtkTextBuffer *buffer;
907         GtkTextIter iter;
908         GtkTreeIter titer;
909
910         if (currently_selected)
911                 return TRUE;
912
913         if (!gtk_tree_model_get_iter(model, &titer, path))
914                 return TRUE;
915
916         tmpl_def.name = _("Template");
917         tmpl_def.subject = "";
918         tmpl_def.to = "";
919         tmpl_def.cc = "";
920         tmpl_def.bcc = "";      
921         tmpl_def.value = "";
922
923         gtk_tree_model_get(model, &titer,
924                            TEMPL_DATA, &tmpl,
925                            -1);
926
927         if (!tmpl) 
928                 tmpl = &tmpl_def;
929
930         gtk_entry_set_text(GTK_ENTRY(templates.entry_name), tmpl->name);
931         gtk_entry_set_text(GTK_ENTRY(templates.entry_to),
932                            tmpl->to ? tmpl->to : "");
933         gtk_entry_set_text(GTK_ENTRY(templates.entry_cc),
934                            tmpl->cc ? tmpl->cc : "");
935         gtk_entry_set_text(GTK_ENTRY(templates.entry_bcc),
936                            tmpl->bcc ? tmpl->bcc : "");                 
937         gtk_entry_set_text(GTK_ENTRY(templates.entry_subject),
938                            tmpl->subject ? tmpl->subject : "");
939         
940         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
941         gtk_text_buffer_set_text(buffer, "", -1);
942         gtk_text_buffer_get_start_iter(buffer, &iter);
943         gtk_text_buffer_insert(buffer, &iter, tmpl->value, -1);
944
945         return TRUE;
946 }