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