2008-09-12 [colin] 3.5.0cvs103
[claws.git] / src / prefs_template.c
1 /*
2  * Claws Mail templates subsystem 
3  * Copyright (C) 2001 Alexander Barinov
4  * Copyright (C) 2001-2007 Hiroyuki Yamamoto and the Claws Mail team
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  * 
19  */
20
21 #include "defs.h"
22
23 #include <glib.h>
24 #include <glib/gi18n.h>
25 #include <gtk/gtk.h>
26 #include <gdk/gdkkeysyms.h>
27 #include <string.h>
28 #include <dirent.h>
29 #include <sys/stat.h>
30
31 #include "template.h"
32 #include "main.h"
33 #include "prefs_gtk.h"
34 #include "inc.h"
35 #include "utils.h"
36 #include "gtkutils.h"
37 #include "alertpanel.h"
38 #include "manage_window.h"
39 #include "compose.h"
40 #include "addr_compl.h"
41 #include "quote_fmt.h"
42 #include "prefs_common.h"
43 #include "manual.h"
44 #include "gtkutils.h"
45 #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         g_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 #ifdef USE_ENCHANT
685                 quote_fmt_init(&dummyinfo, NULL, NULL, TRUE, account, FALSE, NULL);
686 #else
687                 quote_fmt_init(&dummyinfo, NULL, NULL, TRUE, account, FALSE);
688 #endif
689                 quote_fmt_scan_string(tmp);
690                 quote_fmt_parse();
691                 g_free(tmp);
692                 parsed_buf = quote_fmt_get_buffer();
693                 if (!parsed_buf) {
694                         if (line)
695                                 *line = quote_fmt_get_line();
696                         return FALSE;
697                 }
698                 if (email) {
699                         const gchar *start = strrchr(parsed_buf, '<');
700                         const gchar *end = strrchr(parsed_buf, '>');
701                         const gchar *at = strrchr(parsed_buf, '@');
702                         const gchar *space = strrchr(parsed_buf, ' ');
703                         if (!at)
704                                 result = FALSE;
705                         if (at && space && (!start || !end || end < start || start < space))
706                                 result = FALSE;
707                 }
708                 quote_fmt_reset_vartable();
709         }
710         return result;
711 }
712
713 static gboolean prefs_template_list_view_set_row(gint row)
714 /* return TRUE if the row could be modified */
715 {
716         Template *tmpl;
717         gchar *name;
718         gchar *subject;
719         gchar *from;
720         gchar *to;
721         gchar *cc;
722         gchar *bcc;     
723         gchar *value;
724         GtkTextBuffer *buffer;
725         GtkTextIter start, end;
726         GtkTreeModel *model;
727         gint line;
728
729         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
730
731         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
732         gtk_text_buffer_get_start_iter(buffer, &start);
733         gtk_text_buffer_get_iter_at_offset(buffer, &end, -1);
734         value = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
735
736         if (value && *value == '\0') {
737                         g_free(value);
738                 value = NULL;
739                 }
740         if (!prefs_template_string_is_valid(value, &line, TRUE, FALSE)) {
741                 alertpanel_error(_("Template body format error at line %d."), line);
742                 g_free(value);
743                 return FALSE;
744         }
745
746         name = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_name),
747                                       0, -1);
748         if (*name == '\0') {
749                 alertpanel_error(_("Template name is not set."));
750                 g_free(value);
751                 return FALSE;
752         }
753         from = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_from),
754                                     0, -1);
755         to = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_to),
756                                     0, -1);
757         cc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_cc),
758                                     0, -1);
759         bcc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_bcc),
760                                     0, -1);
761         subject = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_subject),
762                                          0, -1);
763
764         if (from && *from == '\0') {
765                 g_free(from);
766                 from = NULL;
767         }
768         if (to && *to == '\0') {
769                 g_free(to);
770                 to = NULL;
771         }
772         if (cc && *cc == '\0') {
773                 g_free(cc);
774                 cc = NULL;
775         }
776         if (bcc && *bcc == '\0') {
777                 g_free(bcc);
778                 bcc = NULL;
779         }
780         if (subject && *subject == '\0') {
781                 g_free(subject);
782                 subject = NULL;
783         }
784
785         if (!prefs_template_string_is_valid(from, NULL, TRUE, TRUE)) {
786                 alertpanel_error(_("Template From format error."));
787                 g_free(from);
788                 g_free(value);
789                 return FALSE;
790         }
791         if (!prefs_template_string_is_valid(to, NULL, TRUE, TRUE)) {
792                 alertpanel_error(_("Template To format error."));
793                 g_free(to);
794                 g_free(value);
795                 return FALSE;
796         }
797         if (!prefs_template_string_is_valid(cc, NULL, TRUE, TRUE)) {
798                 alertpanel_error(_("Template Cc format error."));       
799                 g_free(cc);
800                 g_free(value);
801                 return FALSE;
802         }
803         if (!prefs_template_string_is_valid(bcc, NULL, TRUE, TRUE)) {
804                 alertpanel_error(_("Template Bcc format error."));      
805                 g_free(bcc);
806                 g_free(value);
807                 return FALSE;
808         }
809         if (!prefs_template_string_is_valid(subject, NULL, TRUE, FALSE)) {
810                 alertpanel_error(_("Template subject format error."));  
811                 g_free(subject);
812                 g_free(value);
813                 return FALSE;
814         }
815         
816         tmpl = g_new(Template, 1);
817         tmpl->load_filename = NULL;
818         tmpl->name = name;
819         tmpl->subject = subject;
820         tmpl->from = from;
821         tmpl->to = to;
822         tmpl->cc = cc;
823         tmpl->bcc = bcc;        
824         tmpl->value = value;
825
826         prefs_template_list_view_insert_template(templates.list_view,
827                                                  row, tmpl->name, tmpl);
828
829         return TRUE;
830 }
831
832 static void prefs_template_register_cb(gpointer action, gpointer data)
833 {
834         modified = !prefs_template_list_view_set_row(-1);
835         modified_list = TRUE;
836 }
837
838 static void prefs_template_substitute_cb(gpointer action, gpointer data)
839 {
840         Template *tmpl;
841         gint row;
842         GtkTreeIter iter;
843         GtkTreeModel *model;
844
845         row = gtkut_list_view_get_selected_row(templates.list_view);
846         if (row <= 0)
847                 return;
848
849         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
850         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
851                 return;
852
853         gtk_tree_model_get(model, &iter, TEMPL_DATA, &tmpl, -1);
854         if (!tmpl)
855                 return;
856
857         modified = !prefs_template_list_view_set_row(row);
858         modified_list = TRUE;
859 }
860
861 static void prefs_template_delete_cb(gpointer action, gpointer data)
862 {
863         Template *tmpl;
864         gint row;
865         GtkTreeIter iter;
866         GtkTreeModel *model;
867
868         row = gtkut_list_view_get_selected_row(templates.list_view);
869         if (row <= 0)
870                 return;
871
872         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
873         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
874                 return;
875
876         gtk_tree_model_get(model, &iter, TEMPL_DATA, &tmpl, -1);
877         if (!tmpl)
878                 return;
879
880         if (alertpanel(_("Delete template"),
881                        _("Do you really want to delete this template?"),
882                        GTK_STOCK_CANCEL, GTK_STOCK_DELETE,
883                        NULL) != G_ALERTALTERNATE)
884                 return;
885
886         gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
887         modified_list = TRUE;   
888 }
889
890 static void prefs_template_delete_all_cb(gpointer action, gpointer data)
891 {
892         GtkListStore *list_store;
893
894         if (alertpanel(_("Delete all templates"),
895                           _("Do you really want to delete all the templates?"),
896                           GTK_STOCK_CANCEL, "+"GTK_STOCK_DELETE, NULL) == G_ALERTDEFAULT)
897            return;
898
899         list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view)));
900         prefs_template_clear_list();
901         modified = FALSE;
902
903         prefs_template_reset_dialog();
904         modified_list = TRUE;
905 }
906
907 static void prefs_template_duplicate_cb(gpointer action, gpointer data)
908 {
909         Template *tmpl;
910         gint row;
911         GtkTreeIter iter;
912         GtkTreeModel *model;
913         
914         row = gtkut_list_view_get_selected_row(templates.list_view);
915         if (row <= 0)
916                 return;
917
918         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
919         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
920                 return;
921
922         gtk_tree_model_get(model, &iter, TEMPL_DATA, &tmpl, -1);
923         if (!tmpl)
924                 return;
925
926         modified_list = !prefs_template_list_view_set_row(-row-2);
927 }
928
929 static void prefs_template_clear_cb(gpointer action, gpointer data)
930 {
931    prefs_template_reset_dialog();
932 }
933
934 static void prefs_template_top_cb(gpointer action, gpointer data)
935 {
936         gint row;
937         GtkTreeIter top, sel;
938         GtkTreeModel *model;
939
940         row = gtkut_list_view_get_selected_row(templates.list_view);
941         if (row <= 1) 
942                 return;
943
944         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));            
945         
946         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, 0)
947         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
948                 return;
949
950         gtk_list_store_move_after(GTK_LIST_STORE(model), &sel, &top);
951         gtkut_list_view_select_row(templates.list_view, 1);
952         modified_list = TRUE;
953 }
954
955 static void prefs_template_up_cb(gpointer action, gpointer data)
956 {
957         gint row;
958         GtkTreeIter top, sel;
959         GtkTreeModel *model;
960
961         row = gtkut_list_view_get_selected_row(templates.list_view);
962         if (row <= 1) 
963                 return;
964                 
965         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));    
966
967         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row - 1)
968         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
969                 return;
970
971         gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
972         gtkut_list_view_select_row(templates.list_view, row - 1);
973         modified_list = TRUE;
974 }
975
976 static void prefs_template_down_cb(gpointer action, gpointer data)
977 {
978         gint row, n_rows;
979         GtkTreeIter top, sel;
980         GtkTreeModel *model;
981
982         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));    
983         n_rows = gtk_tree_model_iter_n_children(model, NULL);
984         row = gtkut_list_view_get_selected_row(templates.list_view);
985         if (row < 1 || row >= n_rows - 1)
986                 return;
987
988         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
989         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, row + 1))
990                 return;
991                         
992         gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
993         gtkut_list_view_select_row(templates.list_view, row + 1);
994         modified_list = TRUE;
995 }
996
997 static void prefs_template_bottom_cb(gpointer action, gpointer data)
998 {
999         gint row, n_rows;
1000         GtkTreeIter top, sel;
1001         GtkTreeModel *model;
1002
1003         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));    
1004         n_rows = gtk_tree_model_iter_n_children(model, NULL);
1005         row = gtkut_list_view_get_selected_row(templates.list_view);
1006         if (row < 1 || row >= n_rows - 1)
1007                 return;
1008
1009         if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
1010         ||  !gtk_tree_model_iter_nth_child(model, &sel, NULL, n_rows - 1))
1011                 return;
1012
1013         gtk_list_store_move_after(GTK_LIST_STORE(model), &top, &sel);           
1014         gtkut_list_view_select_row(templates.list_view, n_rows - 1);
1015         modified_list = TRUE;
1016 }
1017
1018 static GtkListStore* prefs_template_create_data_store(void)
1019 {
1020         return gtk_list_store_new(N_TEMPL_COLUMNS,
1021                                   G_TYPE_STRING,        
1022                                   G_TYPE_POINTER,
1023                                   G_TYPE_AUTO_POINTER,
1024                                   -1);
1025 }
1026
1027 static void prefs_template_list_view_insert_template(GtkWidget *list_view,
1028                                                      gint row,
1029                                                      const gchar *template,
1030                                                      Template *data)
1031 {
1032         GtkTreeIter iter;
1033         GtkTreeIter sibling;
1034         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
1035                                         (GTK_TREE_VIEW(list_view)));
1036
1037 /*      row -1 to add a new rule to store,
1038         row >=0 to change an existing row
1039         row <-1 insert a new row after (-row-2)
1040 */
1041         if (row >= 0 ) {
1042                 /* modify the existing */
1043                 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
1044                                                    &iter, NULL, row))
1045                         row = -1;
1046         } else if (row < -1 ) {
1047                 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
1048                                                    &sibling, NULL, -row-2))
1049                         row = -1;               
1050         }
1051
1052         if (row == -1 ) {
1053                 /* append new */
1054                 gtk_list_store_append(list_store, &iter);
1055                 gtk_list_store_set(list_store, &iter,
1056                                    TEMPL_TEXT, template,
1057                                    TEMPL_DATA, data,
1058                                    -1);
1059         } else if (row < -1) {
1060                 /* duplicate */
1061                 gtk_list_store_insert_after(list_store, &iter, &sibling);
1062                 gtk_list_store_set(list_store, &iter,
1063                                    TEMPL_TEXT, template,
1064                                    TEMPL_DATA, data,
1065                                    -1);
1066         } else {
1067                 /* change existing */
1068                 GAuto *auto_data =
1069                                         g_auto_pointer_new_with_free(data, (GFreeFunc) template_free);  
1070
1071                 /* if replacing data in an existing row, the auto pointer takes care
1072                  * of destroying the Template data */
1073                 gtk_list_store_set(list_store, &iter,
1074                                    TEMPL_TEXT, template,
1075                                    TEMPL_DATA, data,
1076                                    TEMPL_AUTO_DATA, auto_data,
1077                                    -1);
1078
1079                 g_auto_pointer_free(auto_data);
1080         }
1081 }
1082
1083 static GtkActionGroup *prefs_template_popup_action = NULL;
1084 static GtkWidget *prefs_template_popup_menu = NULL;
1085
1086 static GtkActionEntry prefs_template_popup_entries[] =
1087 {
1088         {"PrefsTemplatePopup",                  NULL, "PrefsTemplatePopup" },
1089         {"PrefsTemplatePopup/Delete",           NULL, N_("_Delete"), NULL, NULL, G_CALLBACK(prefs_template_delete_cb) },
1090         {"PrefsTemplatePopup/DeleteAll",        NULL, N_("Delete _all"), NULL, NULL, G_CALLBACK(prefs_template_delete_all_cb) },
1091         {"PrefsTemplatePopup/Duplicate",        NULL, N_("D_uplicate"), NULL, NULL, G_CALLBACK(prefs_template_duplicate_cb) },
1092 };
1093
1094 static gint prefs_template_list_btn_pressed(GtkWidget *widget, GdkEventButton *event,
1095                                    GtkTreeView *list_view)
1096 {
1097    if (event) {
1098            /* left- or right-button click */
1099            if (event->button == 1 || event->button == 3) {
1100                    GtkTreePath *path = NULL;
1101                    if (gtk_tree_view_get_path_at_pos( list_view, event->x, event->y,
1102                                                            &path, NULL, NULL, NULL)) {
1103                            prefs_template_select_row(list_view, path);
1104                    }
1105                    if (path)
1106                            gtk_tree_path_free(path);
1107            }
1108
1109            /* right-button click */
1110            if (event->button == 3) {
1111                    GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1112                    GtkTreeIter iter;
1113                    gboolean non_empty;
1114                    gint row;
1115
1116                    if (!prefs_template_popup_menu) {
1117                                 prefs_template_popup_action = cm_menu_create_action_group("PrefsTemplatePopup", prefs_template_popup_entries,
1118                                         G_N_ELEMENTS(prefs_template_popup_entries), (gpointer)list_view);
1119                                 MENUITEM_ADDUI("/Menus", "PrefsTemplatePopup", "PrefsTemplatePopup", GTK_UI_MANAGER_MENU)
1120                                 MENUITEM_ADDUI("/Menus/PrefsTemplatePopup", "Delete", "PrefsTemplatePopup/Delete", GTK_UI_MANAGER_MENUITEM)
1121                                 MENUITEM_ADDUI("/Menus/PrefsTemplatePopup", "DeleteAll", "PrefsTemplatePopup/DeleteAll", GTK_UI_MANAGER_MENUITEM)
1122                                 MENUITEM_ADDUI("/Menus/PrefsTemplatePopup", "Duplicate", "PrefsTemplatePopup/Duplicate", GTK_UI_MANAGER_MENUITEM)
1123                                 prefs_template_popup_menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
1124                                         gtk_ui_manager_get_widget(gtkut_ui_manager(), "/Menus/PrefsTemplatePopup")) );
1125                    }
1126
1127                    /* grey out some popup menu items if there is no selected row */
1128                    row = gtkut_list_view_get_selected_row(GTK_WIDGET(list_view));
1129                         cm_menu_set_sensitive("PrefsTemplatePopup/Delete", (row > 0));
1130                         cm_menu_set_sensitive("PrefsTemplatePopup/Duplicate", (row > 0));
1131
1132                    /* grey out seom popup menu items if there is no row
1133                           (not counting the (New) one at row 0) */
1134                    non_empty = gtk_tree_model_get_iter_first(model, &iter);
1135                    if (non_empty)
1136                            non_empty = gtk_tree_model_iter_next(model, &iter);
1137                         cm_menu_set_sensitive("PrefsTemplatePopup/DeleteAll", non_empty);
1138
1139                    gtk_menu_popup(GTK_MENU(prefs_template_popup_menu), 
1140                                           NULL, NULL, NULL, NULL, 
1141                                           event->button, event->time);
1142            }
1143    }
1144    return FALSE;
1145 }
1146
1147 static gboolean prefs_template_list_popup_menu(GtkWidget *widget, gpointer data)
1148 {
1149    GtkTreeView *list_view = (GtkTreeView *)data;
1150    GdkEventButton event;
1151    
1152    event.button = 3;
1153    event.time = gtk_get_current_event_time();
1154    
1155    prefs_template_list_btn_pressed(NULL, &event, list_view);
1156
1157    return TRUE;
1158 }
1159
1160 static GtkWidget *prefs_template_list_view_create(void)
1161 {
1162         GtkTreeView *list_view;
1163         GtkTreeSelection *selector;
1164         GtkTreeModel *model;
1165
1166         model = GTK_TREE_MODEL(prefs_template_create_data_store());
1167         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
1168         g_object_unref(model);  
1169
1170 #ifndef MAEMO
1171         g_signal_connect(G_OBJECT(list_view), "popup-menu",
1172                          G_CALLBACK(prefs_template_list_popup_menu), list_view);
1173 #else
1174         gtk_widget_tap_and_hold_setup(GTK_WIDGET(list_view), NULL, NULL,
1175                         GTK_TAP_AND_HOLD_NONE | GTK_TAP_AND_HOLD_NO_INTERNALS);
1176         g_signal_connect(G_OBJECT(list_view), "tap-and-hold",
1177                          G_CALLBACK(prefs_template_list_popup_menu), list_view);
1178 #endif
1179         g_signal_connect(G_OBJECT(list_view), "button-press-event",
1180                         G_CALLBACK(prefs_template_list_btn_pressed), list_view);
1181         
1182         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
1183         gtk_tree_view_set_reorderable(list_view, TRUE);
1184
1185         selector = gtk_tree_view_get_selection(list_view);
1186         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1187
1188         /* create the columns */
1189         prefs_template_create_list_view_columns(GTK_WIDGET(list_view));
1190
1191         return GTK_WIDGET(list_view);
1192 }
1193
1194 static void prefs_template_create_list_view_columns(GtkWidget *list_view)
1195 {
1196         GtkTreeViewColumn *column;
1197         GtkCellRenderer *renderer;
1198
1199         renderer = gtk_cell_renderer_text_new();
1200         column = gtk_tree_view_column_new_with_attributes
1201                         (_("Current templates"),
1202                          renderer,
1203                          "text", TEMPL_TEXT,
1204                          NULL);
1205         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
1206         gtk_tree_view_set_search_equal_func(GTK_TREE_VIEW(list_view), prefs_template_search_func_cb , NULL, NULL);
1207 }
1208
1209  /*!
1210  *\brief        Triggered when a row has to be selected
1211  */
1212 static void prefs_template_select_row(GtkTreeView *list_view, GtkTreePath *path)
1213 {
1214         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1215         GtkTreeSelection *selection;
1216         Template *tmpl;
1217         Template tmpl_def;
1218         GtkTextBuffer *buffer;
1219         GtkTextIter iter;
1220         GtkTreeIter titer;
1221
1222         if (!model || !path || !gtk_tree_model_get_iter(model, &titer, path))
1223                 return;
1224
1225         /* select row */
1226         selection = gtk_tree_view_get_selection(list_view);
1227         gtk_tree_selection_select_path(selection, path);
1228
1229         tmpl_def.name = _("Template");
1230         tmpl_def.subject = "";
1231         tmpl_def.from = "";
1232         tmpl_def.to = "";
1233         tmpl_def.cc = "";
1234         tmpl_def.bcc = "";      
1235         tmpl_def.value = "";
1236
1237         gtk_tree_model_get(model, &titer, TEMPL_DATA, &tmpl, -1);
1238         if (!tmpl)
1239                 tmpl = &tmpl_def;
1240
1241         gtk_entry_set_text(GTK_ENTRY(templates.entry_name), tmpl->name);
1242         gtk_entry_set_text(GTK_ENTRY(templates.entry_from),
1243                            tmpl->from ? tmpl->from : "");
1244         gtk_entry_set_text(GTK_ENTRY(templates.entry_to),
1245                            tmpl->to ? tmpl->to : "");
1246         gtk_entry_set_text(GTK_ENTRY(templates.entry_cc),
1247                            tmpl->cc ? tmpl->cc : "");
1248         gtk_entry_set_text(GTK_ENTRY(templates.entry_bcc),
1249                            tmpl->bcc ? tmpl->bcc : "");                 
1250         gtk_entry_set_text(GTK_ENTRY(templates.entry_subject),
1251                            tmpl->subject ? tmpl->subject : "");
1252         
1253         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
1254         gtk_text_buffer_set_text(buffer, "", -1);
1255         gtk_text_buffer_get_start_iter(buffer, &iter);
1256         gtk_text_buffer_insert(buffer, &iter, tmpl->value, -1);
1257 }