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