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