a4dc4fadb7f8f4e9dec2505379d475dd931335b6
[claws.git] / src / prefs_template.c
1 /*
2  * Claws Mail templates subsystem 
3  * Copyright (C) 2001 Alexander Barinov
4  * Copyright (C) 2001-2011 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 #include "menu.h"
46
47 enum {
48         TEMPL_TEXT,
49         TEMPL_DATA,
50         TEMPL_AUTO_DATA,        /*!< auto pointer */
51         N_TEMPL_COLUMNS
52 };
53
54 static struct Templates {
55         GtkWidget *window;
56         GtkWidget *ok_btn;
57         GtkWidget *list_view;
58         GtkWidget *entry_name;
59         GtkWidget *entry_subject;
60         GtkWidget *entry_from;
61         GtkWidget *entry_to;
62         GtkWidget *entry_cc;    
63         GtkWidget *entry_bcc;
64         GtkWidget *text_value;
65 } templates;
66
67 static int modified = FALSE;
68 static int modified_list = FALSE;
69
70 static struct
71 {
72         gchar *label;
73         GtkWidget **entry;
74         gboolean compl;
75         gchar *tooltips;
76 } widgets_table[] = {
77         {N_("Name"),    &templates.entry_name,          FALSE,
78                 N_("This name is used as the Menu item")},
79         {"From",        &templates.entry_from,          TRUE,
80                 N_("Override composing account's From header. This doesn't change the composing account.")},
81         {"To",          &templates.entry_to,            TRUE,   NULL},
82         {"Cc",          &templates.entry_cc,            TRUE,   NULL},
83         {"Bcc",         &templates.entry_bcc,           TRUE,   NULL},
84         {"Subject",     &templates.entry_subject,       FALSE,  NULL},
85         {NULL,          NULL,                           FALSE,  NULL}
86 };
87
88
89 /* widget creating functions */
90 static void prefs_template_window_create        (void);
91 static void prefs_template_window_setup         (void);
92
93 static GSList *prefs_template_get_list          (void);
94
95 /* callbacks */
96 static gint prefs_template_deleted_cb           (GtkWidget      *widget,
97                                                  GdkEventAny    *event,
98                                                  gpointer        data);
99 static gboolean prefs_template_key_pressed_cb   (GtkWidget      *widget,
100                                                  GdkEventKey    *event,
101                                                  gpointer        data);
102 static gboolean prefs_template_search_func_cb (GtkTreeModel *model, gint column, 
103                                                 const gchar *key, GtkTreeIter *iter, 
104                                                 gpointer search_data);
105
106 static void prefs_template_cancel_cb            (gpointer action, gpointer data);
107 static void prefs_template_ok_cb                (gpointer action, gpointer data);
108 static void prefs_template_register_cb          (gpointer action, gpointer data);
109 static void prefs_template_substitute_cb        (gpointer action, gpointer data);
110 static void prefs_template_delete_cb            (gpointer action, gpointer data);
111 static void prefs_template_delete_all_cb        (gpointer action, gpointer data);
112 static void prefs_template_clear_cb             (gpointer action, gpointer data);
113 static void prefs_template_duplicate_cb         (gpointer action, gpointer data);
114 static void prefs_template_top_cb               (gpointer action, gpointer data);
115 static void prefs_template_up_cb                (gpointer action, gpointer data);
116 static void prefs_template_down_cb              (gpointer action, gpointer data);
117 static void prefs_template_bottom_cb            (gpointer action, gpointer data);
118
119 static GtkListStore* prefs_template_create_data_store   (void);
120 static void prefs_template_list_view_insert_template    (GtkWidget *list_view,
121                                                          gint row,
122                                                          const gchar *template,
123                                                          Template *data);
124 static GtkWidget *prefs_template_list_view_create       (void);
125 static void prefs_template_create_list_view_columns     (GtkWidget *list_view);
126 static void prefs_template_select_row(GtkTreeView *list_view, GtkTreePath *path);
127
128 /* Called from mainwindow.c */
129 void prefs_template_open(void)
130 {
131         inc_lock();
132
133         prefs_template_window_create();
134
135         prefs_template_window_setup();
136         gtk_widget_show(templates.window);
137         gtk_window_set_modal(GTK_WINDOW(templates.window), TRUE);
138 }
139
140 /*!
141  *\brief        Save Gtk object size to prefs dataset
142  */
143 static void prefs_template_size_allocate_cb(GtkWidget *widget,
144                                          GtkAllocation *allocation)
145 {
146         cm_return_if_fail(allocation != NULL);
147
148         prefs_common.templateswin_width = allocation->width;
149         prefs_common.templateswin_height = allocation->height;
150 }
151
152 static void prefs_template_window_create(void)
153 {
154         /* window structure ;) */
155         GtkWidget *window;
156         GtkWidget *vbox;
157         GtkWidget *scrolled_window;
158         GtkWidget   *vpaned;
159         GtkWidget     *vbox1;
160         GtkWidget       *table; /* including : entry_[name|from|to|cc|bcc|subject] */
161         GtkWidget       *scroll2;
162         GtkWidget         *text_value;
163         GtkWidget     *vbox2;
164         GtkWidget       *hbox2;
165         GtkWidget         *arrow1;
166         GtkWidget         *hbox3;
167         GtkWidget           *reg_btn;
168         GtkWidget           *subst_btn;
169         GtkWidget           *del_btn;
170         GtkWidget           *clear_btn;
171         GtkWidget         *desc_btn;
172         GtkWidget       *hbox4;
173         GtkWidget         *scroll1;
174         GtkWidget           *list_view;
175         GtkWidget         *vbox3;
176         GtkWidget           *spc_vbox;
177         GtkWidget           *top_btn;
178         GtkWidget           *up_btn;
179         GtkWidget           *down_btn;
180         GtkWidget           *bottom_btn;
181         GtkWidget       *confirm_area;
182         GtkWidget         *help_btn;
183         GtkWidget         *cancel_btn;
184         GtkWidget         *ok_btn;
185         static GdkGeometry geometry;
186         gint i;
187
188         debug_print("Creating templates configuration window...\n");
189
190         /* main window */
191         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "prefs_template");
192         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
193         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
194
195         vbox = gtk_vbox_new(FALSE, 8);
196         gtk_widget_show(vbox);
197         gtk_container_add(GTK_CONTAINER(window), vbox);
198         gtk_container_set_border_width(GTK_CONTAINER(vbox), 4);
199
200         scrolled_window = gtk_scrolled_window_new (NULL, NULL);
201         gtk_widget_show(scrolled_window);
202         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
203                                         GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
204         gtk_container_add(GTK_CONTAINER(vbox), scrolled_window);
205
206         /* vpaned to separate template settings from templates list */
207         vpaned = gtk_vpaned_new();
208         gtk_widget_show(vpaned);
209         gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled_window),
210                                               vpaned);
211         gtk_viewport_set_shadow_type (GTK_VIEWPORT(
212                         gtk_bin_get_child(GTK_BIN(scrolled_window))), GTK_SHADOW_NONE);
213
214         /* vbox to handle template name and content */
215         vbox1 = gtk_vbox_new(FALSE, 6);
216         gtk_widget_show(vbox1);
217         gtk_container_set_border_width(GTK_CONTAINER(vbox1), 8);
218         gtk_paned_pack1(GTK_PANED(vpaned), vbox1, FALSE, FALSE);
219
220         table = gtk_table_new(5, 2, FALSE);
221         gtk_table_set_row_spacings (GTK_TABLE (table), VSPACING_NARROW_2);
222         gtk_table_set_col_spacings (GTK_TABLE (table), 4);
223         gtk_widget_show(table);
224         gtk_box_pack_start (GTK_BOX (vbox1), table, FALSE, FALSE, 0);
225
226
227         for (i=0; widgets_table[i].label; i++) {
228
229                 GtkWidget *label;
230
231                 label = gtk_label_new( (i != 0) ?
232                         prefs_common_translated_header_name(widgets_table[i].label) :
233                         widgets_table[i].label);
234                 gtk_widget_show(label);
235                 gtk_table_attach(GTK_TABLE(table), label, 0, 1, i, (i + 1),
236                                 (GtkAttachOptions) (GTK_FILL),
237                                 (GtkAttachOptions) 0, 0, 0);
238                 gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
239
240                 *(widgets_table[i].entry) = gtk_entry_new();
241                 gtk_widget_show(*(widgets_table[i].entry));
242                 gtk_table_attach(GTK_TABLE(table), *(widgets_table[i].entry), 1, 2, i, (i + 1),
243                                 (GtkAttachOptions) (GTK_EXPAND|GTK_SHRINK|GTK_FILL),
244                                 (GtkAttachOptions) 0, 0, 0);
245                 CLAWS_SET_TIP(*(widgets_table[i].entry),
246                                 widgets_table[i].tooltips);
247         }
248
249         /* template content */
250         scroll2 = gtk_scrolled_window_new(NULL, NULL);
251         gtk_widget_show(scroll2);
252         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll2),
253                                        GTK_POLICY_AUTOMATIC,
254                                        GTK_POLICY_AUTOMATIC);
255         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll2),
256                                             GTK_SHADOW_IN);
257         gtk_box_pack_start(GTK_BOX(vbox1), scroll2, TRUE, TRUE, 0);
258
259         text_value = gtk_text_view_new();
260         if (prefs_common.textfont) {
261                 PangoFontDescription *font_desc;
262
263                 font_desc = pango_font_description_from_string
264                                                 (prefs_common.textfont);
265                 if (font_desc) {
266                         gtk_widget_modify_font(text_value, font_desc);
267                         pango_font_description_free(font_desc);
268                 }
269         }
270         gtk_widget_show(text_value);
271 #ifndef GENERIC_UMPC
272         gtk_widget_set_size_request(text_value, -1, 120);
273 #else
274         gtk_widget_set_size_request(text_value, -1, 60);
275 #endif
276         gtk_container_add(GTK_CONTAINER(scroll2), text_value);
277         gtk_text_view_set_editable(GTK_TEXT_VIEW(text_value), TRUE);
278         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text_value), GTK_WRAP_WORD);
279
280         /* vbox for buttons and templates list */
281         vbox2 = gtk_vbox_new(FALSE, 6);
282         gtk_widget_show(vbox2);
283         gtk_container_set_border_width(GTK_CONTAINER(vbox2), 8);
284         gtk_paned_pack2(GTK_PANED(vpaned), vbox2, TRUE, FALSE);
285
286         /* register | substitute | delete */
287         hbox2 = gtk_hbox_new(FALSE, 4);
288         gtk_widget_show(hbox2);
289         gtk_box_pack_start(GTK_BOX(vbox2), hbox2, FALSE, FALSE, 0);
290
291         arrow1 = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
292         gtk_widget_show(arrow1);
293         gtk_box_pack_start(GTK_BOX(hbox2), arrow1, FALSE, FALSE, 0);
294         gtk_widget_set_size_request(arrow1, -1, 16);
295
296         hbox3 = gtk_hbox_new(TRUE, 4);
297         gtk_widget_show(hbox3);
298         gtk_box_pack_start(GTK_BOX(hbox2), hbox3, FALSE, FALSE, 0);
299
300         reg_btn = gtk_button_new_from_stock(GTK_STOCK_ADD);
301         gtk_widget_show(reg_btn);
302         gtk_box_pack_start(GTK_BOX(hbox3), reg_btn, FALSE, TRUE, 0);
303         g_signal_connect(G_OBJECT (reg_btn), "clicked",
304                          G_CALLBACK (prefs_template_register_cb), NULL);
305         CLAWS_SET_TIP(reg_btn,
306                         _("Append the new template above to the list"));
307
308         subst_btn = gtkut_get_replace_btn(_("Replace"));
309         gtk_widget_show(subst_btn);
310         gtk_box_pack_start(GTK_BOX(hbox3), subst_btn, FALSE, TRUE, 0);
311         g_signal_connect(G_OBJECT(subst_btn), "clicked",
312                          G_CALLBACK(prefs_template_substitute_cb),
313                          NULL);
314         CLAWS_SET_TIP(subst_btn,
315                         _("Replace the selected template in list with the template above"));
316
317         del_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
318         gtk_widget_show(del_btn);
319         gtk_box_pack_start(GTK_BOX(hbox3), del_btn, FALSE, TRUE, 0);
320         g_signal_connect(G_OBJECT(del_btn), "clicked",
321                          G_CALLBACK(prefs_template_delete_cb), NULL);
322         CLAWS_SET_TIP(del_btn,
323                         _("Delete the selected template from the list"));
324
325         clear_btn = gtk_button_new_from_stock (GTK_STOCK_CLEAR);
326         gtk_widget_show (clear_btn);
327         gtk_box_pack_start (GTK_BOX (hbox3), clear_btn, FALSE, TRUE, 0);
328         g_signal_connect(G_OBJECT (clear_btn), "clicked",
329                         G_CALLBACK(prefs_template_clear_cb), NULL);
330         CLAWS_SET_TIP(clear_btn,
331                         _("Clear all the input fields in the dialog"));
332
333         desc_btn = gtk_button_new_from_stock(GTK_STOCK_INFO);
334         gtk_widget_show(desc_btn);
335         gtk_box_pack_end(GTK_BOX(hbox2), desc_btn, FALSE, FALSE, 0);
336         g_signal_connect(G_OBJECT(desc_btn), "clicked",
337                          G_CALLBACK(quote_fmt_quote_description), window);
338         CLAWS_SET_TIP(desc_btn,
339                         _("Show information on configuring templates"));
340
341         /* templates list */
342         hbox4 = gtk_hbox_new(FALSE, 8);
343         gtk_widget_show(hbox4);
344         gtk_box_pack_start(GTK_BOX(vbox2), hbox4, TRUE, TRUE, 0);
345
346         scroll1 = gtk_scrolled_window_new(NULL, NULL);
347         gtk_widget_show(scroll1);
348         gtk_box_pack_start(GTK_BOX(hbox4), scroll1, TRUE, TRUE, 0);
349         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll1),
350                                        GTK_POLICY_AUTOMATIC,
351                                        GTK_POLICY_AUTOMATIC);
352                                        
353         vbox3 = gtk_vbox_new(FALSE, 8);
354         gtk_widget_show(vbox3);
355         gtk_box_pack_start(GTK_BOX(hbox4), vbox3, FALSE, FALSE, 0);
356
357         top_btn = gtk_button_new_from_stock(GTK_STOCK_GOTO_TOP);
358         gtk_widget_show(top_btn);
359         gtk_box_pack_start(GTK_BOX(vbox3), top_btn, FALSE, FALSE, 0);
360         g_signal_connect(G_OBJECT(top_btn), "clicked",
361                          G_CALLBACK(prefs_template_top_cb), NULL);
362         CLAWS_SET_TIP(top_btn,
363                         _("Move the selected template to the top"));
364
365         PACK_VSPACER(vbox3, spc_vbox, VSPACING_NARROW_2);
366
367         up_btn = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
368         gtk_widget_show(up_btn);
369         gtk_box_pack_start (GTK_BOX(vbox3), up_btn, FALSE, FALSE, 0);
370         g_signal_connect(G_OBJECT(up_btn), "clicked",
371                          G_CALLBACK(prefs_template_up_cb), NULL);
372         CLAWS_SET_TIP(up_btn,
373                         _("Move the selected template up"));
374
375         down_btn = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
376         gtk_widget_show (down_btn);
377         gtk_box_pack_start(GTK_BOX (vbox3), down_btn, FALSE, FALSE, 0);
378         g_signal_connect(G_OBJECT (down_btn), "clicked",
379                          G_CALLBACK(prefs_template_down_cb), NULL);
380         CLAWS_SET_TIP(down_btn,
381                         _("Move the selected template down"));
382
383         PACK_VSPACER(vbox3, spc_vbox, VSPACING_NARROW_2);
384
385         bottom_btn = gtk_button_new_from_stock(GTK_STOCK_GOTO_BOTTOM);
386         gtk_widget_show(bottom_btn);
387         gtk_box_pack_start(GTK_BOX(vbox3), bottom_btn, FALSE, FALSE, 0);
388         g_signal_connect(G_OBJECT(bottom_btn), "clicked",
389                          G_CALLBACK(prefs_template_bottom_cb), NULL);
390         CLAWS_SET_TIP(bottom_btn,
391                         _("Move the selected template to the bottom"));
392
393         list_view = prefs_template_list_view_create();
394         gtk_widget_show(list_view);
395         gtk_widget_set_size_request(scroll1, -1, 140);
396         gtk_container_add(GTK_CONTAINER(scroll1), list_view);
397
398         /* help | cancel | ok */
399         gtkut_stock_button_set_create_with_help(&confirm_area, &help_btn,
400                         &cancel_btn, GTK_STOCK_CANCEL,
401                         &ok_btn, GTK_STOCK_OK,
402                         NULL, NULL);
403         gtk_widget_show(confirm_area);
404         gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
405         gtk_widget_grab_default(ok_btn);
406
407         gtk_window_set_title(GTK_WINDOW(window), _("Template configuration"));
408
409         g_signal_connect(G_OBJECT(window), "delete_event",
410                          G_CALLBACK(prefs_template_deleted_cb), NULL);
411         g_signal_connect(G_OBJECT(window), "size_allocate",
412                          G_CALLBACK(prefs_template_size_allocate_cb), NULL);
413         g_signal_connect(G_OBJECT(window), "key_press_event",
414                          G_CALLBACK(prefs_template_key_pressed_cb), NULL);
415         MANAGE_WINDOW_SIGNALS_CONNECT(window);
416         g_signal_connect(G_OBJECT(ok_btn), "clicked",
417                          G_CALLBACK(prefs_template_ok_cb), NULL);
418         g_signal_connect(G_OBJECT(cancel_btn), "clicked",
419                          G_CALLBACK(prefs_template_cancel_cb), NULL);
420         g_signal_connect(G_OBJECT(help_btn), "clicked",
421                          G_CALLBACK(manual_open_with_anchor_cb),
422                          MANUAL_ANCHOR_TEMPLATES);
423
424         if (!geometry.min_height) {
425                 geometry.min_width = 500;
426                 geometry.min_height = 560;
427         }
428
429         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
430                                       GDK_HINT_MIN_SIZE);
431         gtk_widget_set_size_request(window, prefs_common.templateswin_width,
432                                     prefs_common.templateswin_height);
433
434         templates.window = window;
435         templates.ok_btn = ok_btn;
436         templates.list_view = list_view;
437         templates.text_value = text_value;
438 }
439
440 static void prefs_template_reset_dialog(void)
441 {
442         GtkTextBuffer *buffer;
443
444         gtk_entry_set_text(GTK_ENTRY(templates.entry_name), "");
445         gtk_entry_set_text(GTK_ENTRY(templates.entry_from), "");
446         gtk_entry_set_text(GTK_ENTRY(templates.entry_to), "");
447         gtk_entry_set_text(GTK_ENTRY(templates.entry_cc), "");
448         gtk_entry_set_text(GTK_ENTRY(templates.entry_bcc), "");                 
449         gtk_entry_set_text(GTK_ENTRY(templates.entry_subject), "");
450         
451         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
452         gtk_text_buffer_set_text(buffer, "", -1);
453 }
454
455 static void prefs_template_clear_list(void)
456 {
457         GtkListStore *store;
458         
459         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
460                                 (templates.list_view)));
461         gtk_list_store_clear(store);
462
463         prefs_template_list_view_insert_template(templates.list_view,
464                                                  -1, _("(New)"),
465                                                  NULL);
466 }
467
468 static void prefs_template_window_setup(void)
469 {
470         GSList *tmpl_list;
471         GSList *cur;
472         Template *tmpl;
473         int i;
474
475         manage_window_set_transient(GTK_WINDOW(templates.window));
476         gtk_widget_grab_focus(templates.ok_btn);
477
478         prefs_template_clear_list();
479         
480         tmpl_list = template_read_config();
481
482         address_completion_start(templates.window);
483
484         for (i=0; widgets_table[i].label; i++) {
485                 if (widgets_table[i].compl)
486                         address_completion_register_entry(
487                                 GTK_ENTRY(*(widgets_table[i].entry)), TRUE);
488         }
489
490         for (cur = tmpl_list; cur != NULL; cur = cur->next) {
491                 tmpl = (Template *)cur->data;
492                 prefs_template_list_view_insert_template(templates.list_view,
493                                                          -1, tmpl->name, 
494                                                          tmpl);
495         }
496
497         prefs_template_reset_dialog();
498
499         g_slist_free(tmpl_list);
500 }
501
502 static gint prefs_template_deleted_cb(GtkWidget *widget, GdkEventAny *event,
503                                       gpointer data)
504 {
505         prefs_template_cancel_cb(NULL, NULL);
506         return TRUE;
507 }
508
509 static gboolean prefs_template_key_pressed_cb(GtkWidget *widget,
510                                               GdkEventKey *event, gpointer data)
511 {
512         if (event && event->keyval == GDK_KEY_Escape)
513                 prefs_template_cancel_cb(NULL, NULL);
514         else {
515                 GtkWidget *focused = gtkut_get_focused_child(
516                                         GTK_CONTAINER(widget));
517                 if (focused && GTK_IS_EDITABLE(focused)) {
518                         modified = TRUE;
519                 }
520         }
521         return FALSE;
522 }
523
524 static gboolean prefs_template_search_func_cb (GtkTreeModel *model, gint column, const gchar *key, 
525                                                 GtkTreeIter *iter, gpointer search_data) 
526 {
527         gchar *store_string;
528         gint key_len;
529         gboolean retval;
530         GtkTreePath *path;
531
532         gtk_tree_model_get (model, iter, column, &store_string, -1);
533
534         if (!store_string || !key) return FALSE;
535
536         key_len = strlen (key);
537         retval = (strncmp (key, store_string, key_len) != 0);
538
539         g_free(store_string);
540         debug_print("selecting row\n");
541         path = gtk_tree_model_get_path(model, iter);
542         prefs_template_select_row(GTK_TREE_VIEW(templates.list_view), path);
543         gtk_tree_path_free(path);
544
545         return retval;
546 }
547 static void prefs_template_address_completion_end(void)
548 {
549         gint i;
550
551         for (i=0; widgets_table[i].label; i++) {
552                 if (widgets_table[i].compl)
553                         address_completion_unregister_entry(
554                                 GTK_ENTRY(*(widgets_table[i].entry)));
555         }
556         address_completion_end(templates.window);
557 }
558
559 static void prefs_template_ok_cb(gpointer action, gpointer data)
560 {
561         GSList *tmpl_list;
562         GtkListStore *store;
563
564         if (modified && alertpanel(_("Entry not saved"),
565                                  _("The entry was not saved. Close anyway?"),
566                                  GTK_STOCK_CLOSE, _("+_Continue editing"), 
567                                  NULL) != G_ALERTDEFAULT) {
568                 return;
569         } 
570
571         prefs_template_address_completion_end();
572
573         modified = FALSE;
574         modified_list = FALSE;
575         tmpl_list = prefs_template_get_list();
576         template_set_config(tmpl_list);
577         compose_reflect_prefs_all();
578         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
579                                 (templates.list_view)));
580         gtk_list_store_clear(store);
581         gtk_widget_destroy(templates.window);
582         inc_unlock();
583 }
584
585 static void prefs_template_cancel_cb(gpointer action, gpointer data)
586 {
587         GtkListStore *store;
588
589         if (modified && alertpanel(_("Entry not saved"),
590                                  _("The entry was not saved. Close anyway?"),
591                                  GTK_STOCK_CLOSE, _("+_Continue editing"),
592                                  NULL) != G_ALERTDEFAULT) {
593                 return;
594         } else if (modified_list && alertpanel(_("Templates list not saved"),
595                                  _("The templates list has been modified. Close anyway?"),
596                                  GTK_STOCK_CLOSE, _("+_Continue editing"), 
597                                  NULL) != G_ALERTDEFAULT) {
598                 return;
599         }
600
601         prefs_template_address_completion_end();
602
603         modified = FALSE;
604         modified_list = FALSE;
605         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
606                                 (templates.list_view)));
607         gtk_list_store_clear(store);
608         gtk_widget_destroy(templates.window);
609         inc_unlock();
610 }
611
612 /*!
613  *\brief        Request list for storage. New list is owned
614  *              by template.c...
615  */
616 static GSList *prefs_template_get_list(void)
617 {
618         GSList *tmpl_list = NULL;
619         Template *tmpl;
620         GtkTreeModel *model;
621         GtkTreeIter iter;
622
623         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
624         if (!gtk_tree_model_get_iter_first(model, &iter))
625                 return NULL;
626
627         do {
628                 gtk_tree_model_get(model, &iter,
629                                    TEMPL_DATA, &tmpl,
630                                    -1);
631                 
632                 if (tmpl) {
633                         Template *ntmpl;
634                         
635                         ntmpl = g_new(Template, 1);
636                         ntmpl->load_filename = NULL;
637                         ntmpl->name    = tmpl->name && *(tmpl->name) 
638                                          ? g_strdup(tmpl->name) 
639                                          : NULL;
640                         ntmpl->subject = tmpl->subject && *(tmpl->subject) 
641                                          ? g_strdup(tmpl->subject) 
642                                          : NULL;
643                         ntmpl->from    = tmpl->from && *(tmpl->from)
644                                          ? g_strdup(tmpl->from)
645                                          : NULL;
646                         ntmpl->to      = tmpl->to && *(tmpl->to)
647                                          ? g_strdup(tmpl->to)
648                                          : NULL;
649                         ntmpl->cc      = tmpl->cc && *(tmpl->cc)
650                                          ? g_strdup(tmpl->cc)
651                                          : NULL;
652                         ntmpl->bcc     = tmpl->bcc && *(tmpl->bcc)
653                                          ? g_strdup(tmpl->bcc)
654                                          : NULL;        
655                         ntmpl->value   = tmpl->value && *(tmpl->value)
656                                          ? g_strdup(tmpl->value)
657                                          : NULL;
658                         tmpl_list = g_slist_append(tmpl_list, ntmpl);
659                 }                       
660         
661         } while (gtk_tree_model_iter_next(model, &iter)); 
662
663         return tmpl_list;
664 }
665
666 gboolean prefs_template_string_is_valid(gchar *string, gint *line, gboolean escaped_string, gboolean email)
667 {
668         gboolean result = TRUE;
669         if (string && *string != '\0') {
670                 gchar *tmp = NULL;
671                 gchar *parsed_buf;
672                 MsgInfo dummyinfo;
673                 PrefsAccount *account = account_get_default();
674
675                 if (escaped_string) {
676                         tmp = malloc(strlen(string)+1);
677                         pref_get_unescaped_pref(tmp, string);
678                 } else {
679                         tmp = g_strdup(string);
680                 }
681                 memset(&dummyinfo, 0, sizeof(MsgInfo));
682                 /* init dummy fields, so we can test the result of the parse */
683                 dummyinfo.date="Sat, 30 May 2009 01:23:45 +0200";
684                 dummyinfo.fromname="John Doe";
685                 dummyinfo.from="John Doe <john@example.com>";
686                 dummyinfo.to="John Doe <john@example.com>";
687                 dummyinfo.cc="John Doe <john@example.com>";
688                 dummyinfo.msgid="<1234john@example.com>";
689                 dummyinfo.inreplyto="<1234john@example.com>";
690                 dummyinfo.newsgroups="alt.test";
691                 dummyinfo.subject="subject";
692                 
693                 
694 #ifdef USE_ENCHANT
695                 quote_fmt_init(&dummyinfo, NULL, NULL, TRUE, account, FALSE, NULL);
696 #else
697                 quote_fmt_init(&dummyinfo, NULL, NULL, TRUE, account, FALSE);
698 #endif
699                 quote_fmt_scan_string(tmp);
700                 quote_fmt_parse();
701                 g_free(tmp);
702                 parsed_buf = quote_fmt_get_buffer();
703                 if (!parsed_buf) {
704                         if (line)
705                                 *line = quote_fmt_get_line();
706                         return FALSE;
707                 }
708                 if (email) {
709                         const gchar *start = strrchr(parsed_buf, '<');
710                         const gchar *end = strrchr(parsed_buf, '>');
711                         const gchar *at = strrchr(parsed_buf, '@');
712                         const gchar *space = strrchr(parsed_buf, ' ');
713                         if (!at)
714                                 result = FALSE;
715                         if (at && space && (!start || !end || end < start || start < space))
716                                 result = FALSE;
717                 }
718                 quote_fmt_reset_vartable();
719         }
720         return result;
721 }
722
723 static gboolean prefs_template_list_view_set_row(gint row)
724 /* return TRUE if the row could be modified */
725 {
726         Template *tmpl;
727         gchar *name;
728         gchar *subject;
729         gchar *from;
730         gchar *to;
731         gchar *cc;
732         gchar *bcc;     
733         gchar *value;
734         GtkTextBuffer *buffer;
735         GtkTextIter start, end;
736         GtkTreeModel *model;
737         gint line;
738
739         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
740
741         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
742         gtk_text_buffer_get_start_iter(buffer, &start);
743         gtk_text_buffer_get_iter_at_offset(buffer, &end, -1);
744         value = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
745
746         if (value && *value == '\0') {
747                         g_free(value);
748                 value = NULL;
749                 }
750         if (!prefs_template_string_is_valid(value, &line, TRUE, FALSE)) {
751                 alertpanel_error(_("The body of the template has an error at line %d."), line);
752                 g_free(value);
753                 return FALSE;
754         }
755
756         name = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_name),
757                                       0, -1);
758         if (*name == '\0') {
759                 alertpanel_error(_("The template's name is not set."));
760                 g_free(value);
761                 return FALSE;
762         }
763         from = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_from),
764                                     0, -1);
765         to = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_to),
766                                     0, -1);
767         cc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_cc),
768                                     0, -1);
769         bcc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_bcc),
770                                     0, -1);
771         subject = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_subject),
772                                          0, -1);
773
774         if (from && *from == '\0') {
775                 g_free(from);
776                 from = NULL;
777         }
778         if (to && *to == '\0') {
779                 g_free(to);
780                 to = NULL;
781         }
782         if (cc && *cc == '\0') {
783                 g_free(cc);
784                 cc = NULL;
785         }
786         if (bcc && *bcc == '\0') {
787                 g_free(bcc);
788                 bcc = NULL;
789         }
790         if (subject && *subject == '\0') {
791                 g_free(subject);
792                 subject = NULL;
793         }
794
795         if (!prefs_template_string_is_valid(from, NULL, TRUE, TRUE)) {
796                 alertpanel_error(_("The \"From\" field of the template contains an invalid email address."));
797                 g_free(from);
798                 g_free(value);
799                 return FALSE;
800         }
801         if (!prefs_template_string_is_valid(to, NULL, TRUE, TRUE)) {
802                 alertpanel_error(_("The \"To\" field of the template contains an invalid email address."));
803                 g_free(to);
804                 g_free(value);
805                 return FALSE;
806         }
807         if (!prefs_template_string_is_valid(cc, NULL, TRUE, TRUE)) {
808                 alertpanel_error(_("The \"Cc\" field of the template contains an invalid email address."));     
809                 g_free(cc);
810                 g_free(value);
811                 return FALSE;
812         }
813         if (!prefs_template_string_is_valid(bcc, NULL, TRUE, TRUE)) {
814                 alertpanel_error(_("The \"Bcc\" field of the template contains an invalid email address."));    
815                 g_free(bcc);
816                 g_free(value);
817                 return FALSE;
818         }
819         if (!prefs_template_string_is_valid(subject, NULL, TRUE, FALSE)) {
820                 alertpanel_error(_("The \"Subject\" field of the template is invalid."));       
821                 g_free(subject);
822                 g_free(value);
823                 return FALSE;
824         }
825         
826         tmpl = g_new(Template, 1);
827         tmpl->load_filename = NULL;
828         tmpl->name = name;
829         tmpl->subject = subject;
830         tmpl->from = from;
831         tmpl->to = to;
832         tmpl->cc = cc;
833         tmpl->bcc = bcc;        
834         tmpl->value = value;
835
836         prefs_template_list_view_insert_template(templates.list_view,
837                                                  row, tmpl->name, tmpl);
838
839         return TRUE;
840 }
841
842 static void prefs_template_register_cb(gpointer action, gpointer data)
843 {
844         modified = !prefs_template_list_view_set_row(-1);
845         modified_list = TRUE;
846 }
847
848 static void prefs_template_substitute_cb(gpointer action, gpointer data)
849 {
850         Template *tmpl;
851         gint row;
852         GtkTreeIter iter;
853         GtkTreeModel *model;
854
855         row = gtkut_list_view_get_selected_row(templates.list_view);
856         if (row <= 0)
857                 return;
858
859         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
860         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
861                 return;
862
863         gtk_tree_model_get(model, &iter, TEMPL_DATA, &tmpl, -1);
864         if (!tmpl)
865                 return;
866
867         modified = !prefs_template_list_view_set_row(row);
868         modified_list = TRUE;
869 }
870
871 static void prefs_template_delete_cb(gpointer action, gpointer data)
872 {
873         Template *tmpl;
874         gint row;
875         GtkTreeIter iter;
876         GtkTreeModel *model;
877
878         row = gtkut_list_view_get_selected_row(templates.list_view);
879         if (row <= 0)
880                 return;
881
882         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
883         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
884                 return;
885
886         gtk_tree_model_get(model, &iter, TEMPL_DATA, &tmpl, -1);
887         if (!tmpl)
888                 return;
889
890         if (alertpanel(_("Delete template"),
891                        _("Do you really want to delete this template?"),
892                        GTK_STOCK_CANCEL, GTK_STOCK_DELETE,
893                        NULL) != G_ALERTALTERNATE)
894                 return;
895
896         gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
897         prefs_template_reset_dialog();
898         modified_list = TRUE;   
899 }
900
901 static void prefs_template_delete_all_cb(gpointer action, gpointer data)
902 {
903         GtkListStore *list_store;
904
905         if (alertpanel(_("Delete all templates"),
906                           _("Do you really want to delete all the templates?"),
907                           GTK_STOCK_CANCEL, "+"GTK_STOCK_DELETE, NULL) == G_ALERTDEFAULT)
908            return;
909
910         list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view)));
911         prefs_template_clear_list();
912         modified = FALSE;
913
914         prefs_template_reset_dialog();
915         modified_list = TRUE;
916 }
917
918 static void prefs_template_duplicate_cb(gpointer action, gpointer data)
919 {
920         Template *tmpl;
921         gint row;
922         GtkTreeIter iter;
923         GtkTreeModel *model;
924         
925         row = gtkut_list_view_get_selected_row(templates.list_view);
926         if (row <= 0)
927                 return;
928
929         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
930         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
931                 return;
932
933         gtk_tree_model_get(model, &iter, TEMPL_DATA, &tmpl, -1);
934         if (!tmpl)
935                 return;
936
937         modified_list = !prefs_template_list_view_set_row(-row-2);
938 }
939
940 static void prefs_template_clear_cb(gpointer action, gpointer data)
941 {
942    prefs_template_reset_dialog();
943 }
944
945 static void prefs_template_top_cb(gpointer action, gpointer data)
946 {
947         gint row;
948         GtkTreeIter top, sel;
949         GtkTreeModel *model;
950
951         row = gtkut_list_view_get_selected_row(templates.list_view);
952         if (row <= 1) 
953                 return;
954
955         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));            
956         
957         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, 0)
958         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
959                 return;
960
961         gtk_list_store_move_after(GTK_LIST_STORE(model), &sel, &top);
962         gtkut_list_view_select_row(templates.list_view, 1);
963         modified_list = TRUE;
964 }
965
966 static void prefs_template_up_cb(gpointer action, gpointer data)
967 {
968         gint row;
969         GtkTreeIter top, sel;
970         GtkTreeModel *model;
971
972         row = gtkut_list_view_get_selected_row(templates.list_view);
973         if (row <= 1) 
974                 return;
975                 
976         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));    
977
978         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row - 1)
979         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
980                 return;
981
982         gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
983         gtkut_list_view_select_row(templates.list_view, row - 1);
984         modified_list = TRUE;
985 }
986
987 static void prefs_template_down_cb(gpointer action, gpointer data)
988 {
989         gint row, n_rows;
990         GtkTreeIter top, sel;
991         GtkTreeModel *model;
992
993         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));    
994         n_rows = gtk_tree_model_iter_n_children(model, NULL);
995         row = gtkut_list_view_get_selected_row(templates.list_view);
996         if (row < 1 || row >= n_rows - 1)
997                 return;
998
999         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
1000         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row + 1))
1001                 return;
1002                         
1003         gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
1004         gtkut_list_view_select_row(templates.list_view, row + 1);
1005         modified_list = TRUE;
1006 }
1007
1008 static void prefs_template_bottom_cb(gpointer action, gpointer data)
1009 {
1010         gint row, n_rows;
1011         GtkTreeIter top, sel;
1012         GtkTreeModel *model;
1013
1014         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));    
1015         n_rows = gtk_tree_model_iter_n_children(model, NULL);
1016         row = gtkut_list_view_get_selected_row(templates.list_view);
1017         if (row < 1 || row >= n_rows - 1)
1018                 return;
1019
1020         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
1021         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, n_rows - 1))
1022                 return;
1023
1024         gtk_list_store_move_after(GTK_LIST_STORE(model), &top, &sel);           
1025         gtkut_list_view_select_row(templates.list_view, n_rows - 1);
1026         modified_list = TRUE;
1027 }
1028
1029 static GtkListStore* prefs_template_create_data_store(void)
1030 {
1031         return gtk_list_store_new(N_TEMPL_COLUMNS,
1032                                   G_TYPE_STRING,        
1033                                   G_TYPE_POINTER,
1034                                   G_TYPE_AUTO_POINTER,
1035                                   -1);
1036 }
1037
1038 static void prefs_template_list_view_insert_template(GtkWidget *list_view,
1039                                                      gint row,
1040                                                      const gchar *template,
1041                                                      Template *data)
1042 {
1043         GtkTreeIter iter;
1044         GtkTreeIter sibling;
1045         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
1046                                         (GTK_TREE_VIEW(list_view)));
1047
1048 /*      row -1 to add a new rule to store,
1049         row >=0 to change an existing row
1050         row <-1 insert a new row after (-row-2)
1051 */
1052         if (row >= 0 ) {
1053                 /* modify the existing */
1054                 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
1055                                                    &iter, NULL, row))
1056                         row = -1;
1057         } else if (row < -1 ) {
1058                 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
1059                                                    &sibling, NULL, -row-2))
1060                         row = -1;               
1061         }
1062
1063         if (row == -1 ) {
1064                 /* append new */
1065                 gtk_list_store_append(list_store, &iter);
1066                 gtk_list_store_set(list_store, &iter,
1067                                    TEMPL_TEXT, template,
1068                                    TEMPL_DATA, data,
1069                                    -1);
1070         } else if (row < -1) {
1071                 /* duplicate */
1072                 gtk_list_store_insert_after(list_store, &iter, &sibling);
1073                 gtk_list_store_set(list_store, &iter,
1074                                    TEMPL_TEXT, template,
1075                                    TEMPL_DATA, data,
1076                                    -1);
1077         } else {
1078                 /* change existing */
1079                 GAuto *auto_data =
1080                                         g_auto_pointer_new_with_free(data, (GFreeFunc) template_free);  
1081
1082                 /* if replacing data in an existing row, the auto pointer takes care
1083                  * of destroying the Template data */
1084                 gtk_list_store_set(list_store, &iter,
1085                                    TEMPL_TEXT, template,
1086                                    TEMPL_DATA, data,
1087                                    TEMPL_AUTO_DATA, auto_data,
1088                                    -1);
1089
1090                 g_auto_pointer_free(auto_data);
1091         }
1092 }
1093
1094 static GtkActionGroup *prefs_template_popup_action = NULL;
1095 static GtkWidget *prefs_template_popup_menu = NULL;
1096
1097 static GtkActionEntry prefs_template_popup_entries[] =
1098 {
1099         {"PrefsTemplatePopup",                  NULL, "PrefsTemplatePopup" },
1100         {"PrefsTemplatePopup/Delete",           NULL, N_("_Delete"), NULL, NULL, G_CALLBACK(prefs_template_delete_cb) },
1101         {"PrefsTemplatePopup/DeleteAll",        NULL, N_("Delete _all"), NULL, NULL, G_CALLBACK(prefs_template_delete_all_cb) },
1102         {"PrefsTemplatePopup/Duplicate",        NULL, N_("D_uplicate"), NULL, NULL, G_CALLBACK(prefs_template_duplicate_cb) },
1103 };
1104
1105 static gint prefs_template_list_btn_pressed(GtkWidget *widget, GdkEventButton *event,
1106                                    GtkTreeView *list_view)
1107 {
1108    if (event) {
1109            /* left- or right-button click */
1110            if (event->button == 1 || event->button == 3) {
1111                    GtkTreePath *path = NULL;
1112                    if (gtk_tree_view_get_path_at_pos( list_view, event->x, event->y,
1113                                                            &path, NULL, NULL, NULL)) {
1114                            prefs_template_select_row(list_view, path);
1115                    }
1116                    if (path)
1117                            gtk_tree_path_free(path);
1118            }
1119
1120            /* right-button click */
1121            if (event->button == 3) {
1122                    GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1123                    GtkTreeIter iter;
1124                    gboolean non_empty;
1125                    gint row;
1126
1127                    if (!prefs_template_popup_menu) {
1128                                 prefs_template_popup_action = cm_menu_create_action_group("PrefsTemplatePopup", prefs_template_popup_entries,
1129                                         G_N_ELEMENTS(prefs_template_popup_entries), (gpointer)list_view);
1130                                 MENUITEM_ADDUI("/Menus", "PrefsTemplatePopup", "PrefsTemplatePopup", GTK_UI_MANAGER_MENU)
1131                                 MENUITEM_ADDUI("/Menus/PrefsTemplatePopup", "Delete", "PrefsTemplatePopup/Delete", GTK_UI_MANAGER_MENUITEM)
1132                                 MENUITEM_ADDUI("/Menus/PrefsTemplatePopup", "DeleteAll", "PrefsTemplatePopup/DeleteAll", GTK_UI_MANAGER_MENUITEM)
1133                                 MENUITEM_ADDUI("/Menus/PrefsTemplatePopup", "Duplicate", "PrefsTemplatePopup/Duplicate", GTK_UI_MANAGER_MENUITEM)
1134                                 prefs_template_popup_menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
1135                                         gtk_ui_manager_get_widget(gtkut_ui_manager(), "/Menus/PrefsTemplatePopup")) );
1136                    }
1137
1138                    /* grey out some popup menu items if there is no selected row */
1139                    row = gtkut_list_view_get_selected_row(GTK_WIDGET(list_view));
1140                         cm_menu_set_sensitive("PrefsTemplatePopup/Delete", (row > 0));
1141                         cm_menu_set_sensitive("PrefsTemplatePopup/Duplicate", (row > 0));
1142
1143                    /* grey out seom popup menu items if there is no row
1144                           (not counting the (New) one at row 0) */
1145                    non_empty = gtk_tree_model_get_iter_first(model, &iter);
1146                    if (non_empty)
1147                            non_empty = gtk_tree_model_iter_next(model, &iter);
1148                         cm_menu_set_sensitive("PrefsTemplatePopup/DeleteAll", non_empty);
1149
1150                    gtk_menu_popup(GTK_MENU(prefs_template_popup_menu), 
1151                                           NULL, NULL, NULL, NULL, 
1152                                           event->button, event->time);
1153            }
1154    }
1155    return FALSE;
1156 }
1157
1158 static gboolean prefs_template_list_popup_menu(GtkWidget *widget, gpointer data)
1159 {
1160    GtkTreeView *list_view = (GtkTreeView *)data;
1161    GdkEventButton event;
1162    
1163    event.button = 3;
1164    event.time = gtk_get_current_event_time();
1165    
1166    prefs_template_list_btn_pressed(NULL, &event, list_view);
1167
1168    return TRUE;
1169 }
1170
1171 static GtkWidget *prefs_template_list_view_create(void)
1172 {
1173         GtkTreeView *list_view;
1174         GtkTreeSelection *selector;
1175         GtkTreeModel *model;
1176
1177         model = GTK_TREE_MODEL(prefs_template_create_data_store());
1178         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
1179         g_object_unref(model);  
1180
1181 #ifndef MAEMO
1182         g_signal_connect(G_OBJECT(list_view), "popup-menu",
1183                          G_CALLBACK(prefs_template_list_popup_menu), list_view);
1184 #else
1185         gtk_widget_tap_and_hold_setup(GTK_WIDGET(list_view), NULL, NULL,
1186                         GTK_TAP_AND_HOLD_NONE | GTK_TAP_AND_HOLD_NO_INTERNALS);
1187         g_signal_connect(G_OBJECT(list_view), "tap-and-hold",
1188                          G_CALLBACK(prefs_template_list_popup_menu), list_view);
1189 #endif
1190         g_signal_connect(G_OBJECT(list_view), "button-press-event",
1191                         G_CALLBACK(prefs_template_list_btn_pressed), list_view);
1192         
1193         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
1194         gtk_tree_view_set_reorderable(list_view, TRUE);
1195
1196         selector = gtk_tree_view_get_selection(list_view);
1197         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1198
1199         /* create the columns */
1200         prefs_template_create_list_view_columns(GTK_WIDGET(list_view));
1201
1202         return GTK_WIDGET(list_view);
1203 }
1204
1205 static void prefs_template_create_list_view_columns(GtkWidget *list_view)
1206 {
1207         GtkTreeViewColumn *column;
1208         GtkCellRenderer *renderer;
1209
1210         renderer = gtk_cell_renderer_text_new();
1211         column = gtk_tree_view_column_new_with_attributes
1212                         (_("Current templates"),
1213                          renderer,
1214                          "text", TEMPL_TEXT,
1215                          NULL);
1216         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
1217         gtk_tree_view_set_search_equal_func(GTK_TREE_VIEW(list_view), prefs_template_search_func_cb , NULL, NULL);
1218 }
1219
1220  /*!
1221  *\brief        Triggered when a row has to be selected
1222  */
1223 static void prefs_template_select_row(GtkTreeView *list_view, GtkTreePath *path)
1224 {
1225         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1226         GtkTreeSelection *selection;
1227         Template *tmpl;
1228         Template tmpl_def;
1229         GtkTextBuffer *buffer;
1230         GtkTextIter iter;
1231         GtkTreeIter titer;
1232
1233         if (!model || !path || !gtk_tree_model_get_iter(model, &titer, path))
1234                 return;
1235
1236         /* select row */
1237         selection = gtk_tree_view_get_selection(list_view);
1238         gtk_tree_selection_select_path(selection, path);
1239
1240         tmpl_def.name = _("Template");
1241         tmpl_def.subject = "";
1242         tmpl_def.from = "";
1243         tmpl_def.to = "";
1244         tmpl_def.cc = "";
1245         tmpl_def.bcc = "";      
1246         tmpl_def.value = "";
1247
1248         gtk_tree_model_get(model, &titer, TEMPL_DATA, &tmpl, -1);
1249         if (!tmpl)
1250                 tmpl = &tmpl_def;
1251
1252         gtk_entry_set_text(GTK_ENTRY(templates.entry_name), tmpl->name);
1253         gtk_entry_set_text(GTK_ENTRY(templates.entry_from),
1254                            tmpl->from ? tmpl->from : "");
1255         gtk_entry_set_text(GTK_ENTRY(templates.entry_to),
1256                            tmpl->to ? tmpl->to : "");
1257         gtk_entry_set_text(GTK_ENTRY(templates.entry_cc),
1258                            tmpl->cc ? tmpl->cc : "");
1259         gtk_entry_set_text(GTK_ENTRY(templates.entry_bcc),
1260                            tmpl->bcc ? tmpl->bcc : "");                 
1261         gtk_entry_set_text(GTK_ENTRY(templates.entry_subject),
1262                            tmpl->subject ? tmpl->subject : "");
1263         
1264         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
1265         gtk_text_buffer_set_text(buffer, "", -1);
1266         gtk_text_buffer_get_start_iter(buffer, &iter);
1267         gtk_text_buffer_insert(buffer, &iter, tmpl->value, -1);
1268 }