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