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