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