869068c60fd548f8430caa4bde244c98cf144fdb
[claws.git] / src / prefs_template.c
1 /*
2  * Sylpheed templates subsystem 
3  * Copyright (C) 2001 Alexander Barinov
4  * Copyright (C) 2001-2006 Hiroyuki Yamamoto and the Sylpheed-Claws 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 2 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, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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 "inc.h"
34 #include "utils.h"
35 #include "gtkutils.h"
36 #include "alertpanel.h"
37 #include "manage_window.h"
38 #include "compose.h"
39 #include "addr_compl.h"
40 #include "quote_fmt.h"
41 #include "prefs_common.h"
42
43 enum {
44         TEMPL_TEXT,
45         TEMPL_DATA,
46         TEMPL_AUTO_DATA,        /*!< auto pointer */
47         N_TEMPL_COLUMNS
48 };
49
50 static struct Templates {
51         GtkWidget *window;
52         GtkWidget *ok_btn;
53         GtkWidget *list_view;
54         GtkWidget *entry_name;
55         GtkWidget *entry_subject;
56         GtkWidget *entry_to;
57         GtkWidget *entry_cc;    
58         GtkWidget *entry_bcc;
59         GtkWidget *text_value;
60 } templates;
61
62 static int modified = FALSE;
63
64 /* widget creating functions */
65 static void prefs_template_window_create        (void);
66 static void prefs_template_window_setup         (void);
67 static void prefs_template_clear                (void);
68
69 static GSList *prefs_template_get_list          (void);
70
71 /* callbacks */
72 static gint prefs_template_deleted_cb           (GtkWidget      *widget,
73                                                  GdkEventAny    *event,
74                                                  gpointer        data);
75 static gboolean prefs_template_key_pressed_cb   (GtkWidget      *widget,
76                                                  GdkEventKey    *event,
77                                                  gpointer        data);
78 static void prefs_template_cancel_cb            (void);
79 static void prefs_template_ok_cb                (void);
80 static void prefs_template_register_cb          (void);
81 static void prefs_template_substitute_cb        (void);
82 static void prefs_template_delete_cb            (void);
83
84 static GtkListStore* prefs_template_create_data_store   (void);
85 static void prefs_template_list_view_insert_template    (GtkWidget *list_view,
86                                                          GtkTreeIter *row_iter,
87                                                          const gchar *template,
88                                                          Template *data,
89                                                          gboolean sorted);
90 static GtkWidget *prefs_template_list_view_create       (void);
91 static void prefs_template_create_list_view_columns     (GtkWidget *list_view);
92 static gboolean prefs_template_selected                 (GtkTreeSelection *selector,
93                                                          GtkTreeModel *model, 
94                                                          GtkTreePath *path,
95                                                          gboolean currently_selected,
96                                                          gpointer data);
97
98 /* Called from mainwindow.c */
99 void prefs_template_open(void)
100 {
101         inc_lock();
102
103         if (!templates.window)
104                 prefs_template_window_create();
105
106         prefs_template_window_setup();
107         gtk_widget_show(templates.window);
108 }
109
110 /*!
111  *\brief        Save Gtk object size to prefs dataset
112  */
113 static void prefs_template_size_allocate_cb(GtkWidget *widget,
114                                          GtkAllocation *allocation)
115 {
116         g_return_if_fail(allocation != NULL);
117
118         prefs_common.templateswin_width = allocation->width;
119         prefs_common.templateswin_height = allocation->height;
120 }
121
122 #define ADD_ENTRY(entry, str, row) \
123 { \
124         label1 = gtk_label_new(str); \
125         gtk_widget_show(label1); \
126         gtk_table_attach(GTK_TABLE(table), label1, 0, 1, row, (row + 1), \
127                          GTK_FILL, 0, 0, 0); \
128         gtk_misc_set_alignment(GTK_MISC(label1), 1, 0.5); \
129  \
130         entry = gtk_entry_new(); \
131         gtk_widget_show(entry); \
132         gtk_table_attach(GTK_TABLE(table), entry, 1, 2, row, (row + 1), \
133                          GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0); \
134 }
135
136 static void prefs_template_window_create(void)
137 {
138         /* window structure ;) */
139         GtkWidget *window;
140         GtkWidget   *vpaned;
141         GtkWidget     *vbox1;
142         GtkWidget       *hbox1;
143         GtkWidget         *label1;
144         GtkWidget         *entry_name;
145         GtkWidget       *table;
146         GtkWidget         *entry_to;
147         GtkWidget         *entry_cc;
148         GtkWidget         *entry_bcc;           
149         GtkWidget         *entry_subject;
150         GtkWidget       *scroll2;
151         GtkWidget         *text_value;
152         GtkWidget     *vbox2;
153         GtkWidget       *hbox2;
154         GtkWidget         *arrow1;
155         GtkWidget         *hbox3;
156         GtkWidget           *reg_btn;
157         GtkWidget           *subst_btn;
158         GtkWidget           *del_btn;
159         GtkWidget         *desc_btn;
160         GtkWidget       *scroll1;
161         GtkWidget         *list_view;
162         GtkWidget       *confirm_area;
163         GtkWidget         *ok_btn;
164         GtkWidget         *cancel_btn;
165         static GdkGeometry geometry;
166
167         debug_print("Creating templates configuration window...\n");
168
169         /* main window */
170         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
171         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
172         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
173         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
174
175         /* vpaned to separate template settings from templates list */
176         vpaned = gtk_vpaned_new();
177         gtk_widget_show(vpaned);
178         gtk_container_add(GTK_CONTAINER(window), vpaned);
179
180         /* vbox to handle template name and content */
181         vbox1 = gtk_vbox_new(FALSE, 6);
182         gtk_widget_show(vbox1);
183         gtk_container_set_border_width(GTK_CONTAINER(vbox1), 8);
184         gtk_paned_pack1(GTK_PANED(vpaned), vbox1, FALSE, FALSE);
185
186         hbox1 = gtk_hbox_new(FALSE, 8);
187         gtk_widget_show(hbox1);
188         gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, FALSE, 0);
189
190         label1 = gtk_label_new(_("Template name"));
191         gtk_widget_show(label1);
192         gtk_box_pack_start(GTK_BOX(hbox1), label1, FALSE, FALSE, 0);
193
194         entry_name = gtk_entry_new();
195         gtk_widget_show(entry_name);
196         gtk_box_pack_start(GTK_BOX(hbox1), entry_name, TRUE, TRUE, 0);
197
198         /* table for headers */
199         table = gtk_table_new(2, 2, FALSE);
200         gtk_widget_show(table);
201         gtk_box_pack_start(GTK_BOX(vbox1), table, FALSE, FALSE, 0);
202         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
203         gtk_table_set_col_spacings(GTK_TABLE(table), 4);
204
205         ADD_ENTRY(entry_to, _("To:"), 0);
206         address_completion_register_entry(GTK_ENTRY(entry_to));
207         ADD_ENTRY(entry_cc, _("Cc:"), 1)
208         address_completion_register_entry(GTK_ENTRY(entry_cc));
209         ADD_ENTRY(entry_bcc, _("Bcc:"), 2)      
210         address_completion_register_entry(GTK_ENTRY(entry_bcc));
211         ADD_ENTRY(entry_subject, _("Subject:"), 3);
212
213 #undef ADD_ENTRY
214
215         /* template content */
216         scroll2 = gtk_scrolled_window_new(NULL, NULL);
217         gtk_widget_show(scroll2);
218         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll2),
219                                        GTK_POLICY_AUTOMATIC,
220                                        GTK_POLICY_AUTOMATIC);
221         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll2),
222                                             GTK_SHADOW_IN);
223         gtk_box_pack_start(GTK_BOX(vbox1), scroll2, TRUE, TRUE, 0);
224
225         text_value = gtk_text_view_new();
226         gtk_widget_show(text_value);
227         gtk_widget_set_size_request(text_value, -1, 120);
228         gtk_container_add(GTK_CONTAINER(scroll2), text_value);
229         gtk_text_view_set_editable(GTK_TEXT_VIEW(text_value), TRUE);
230         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text_value), GTK_WRAP_WORD);
231
232         /* vbox for buttons and templates list */
233         vbox2 = gtk_vbox_new(FALSE, 6);
234         gtk_widget_show(vbox2);
235         gtk_container_set_border_width(GTK_CONTAINER(vbox2), 8);
236         gtk_paned_pack2(GTK_PANED(vpaned), vbox2, TRUE, FALSE);
237
238         /* register | substitute | delete */
239         hbox2 = gtk_hbox_new(FALSE, 4);
240         gtk_widget_show(hbox2);
241         gtk_box_pack_start(GTK_BOX(vbox2), hbox2, FALSE, FALSE, 0);
242
243         arrow1 = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
244         gtk_widget_show(arrow1);
245         gtk_box_pack_start(GTK_BOX(hbox2), arrow1, FALSE, FALSE, 0);
246         gtk_widget_set_size_request(arrow1, -1, 16);
247
248         hbox3 = gtk_hbox_new(TRUE, 4);
249         gtk_widget_show(hbox3);
250         gtk_box_pack_start(GTK_BOX(hbox2), hbox3, FALSE, FALSE, 0);
251
252         reg_btn = gtk_button_new_from_stock(GTK_STOCK_ADD);
253         gtk_widget_show(reg_btn);
254         gtk_box_pack_start(GTK_BOX(hbox3), reg_btn, FALSE, TRUE, 0);
255         g_signal_connect(G_OBJECT (reg_btn), "clicked",
256                          G_CALLBACK (prefs_template_register_cb), NULL);
257
258         subst_btn = gtkut_get_replace_btn(_("Replace"));
259         gtk_widget_show(subst_btn);
260         gtk_box_pack_start(GTK_BOX(hbox3), subst_btn, FALSE, TRUE, 0);
261         g_signal_connect(G_OBJECT(subst_btn), "clicked",
262                          G_CALLBACK(prefs_template_substitute_cb),
263                          NULL);
264
265         del_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
266         gtk_widget_show(del_btn);
267         gtk_box_pack_start(GTK_BOX(hbox3), del_btn, FALSE, TRUE, 0);
268         g_signal_connect(G_OBJECT(del_btn), "clicked",
269                          G_CALLBACK(prefs_template_delete_cb), NULL);
270
271 #if GTK_CHECK_VERSION(2, 8, 0)
272         desc_btn = gtk_button_new_from_stock(GTK_STOCK_INFO);
273 #else
274         desc_btn = gtk_button_new_with_label(_(" Symbols... "));
275 #endif
276         gtk_widget_show(desc_btn);
277         gtk_box_pack_end(GTK_BOX(hbox2), desc_btn, FALSE, FALSE, 0);
278         g_signal_connect(G_OBJECT(desc_btn), "clicked",
279                          G_CALLBACK(quote_fmt_quote_description), NULL);
280
281         /* templates list */
282         scroll1 = gtk_scrolled_window_new(NULL, NULL);
283         gtk_widget_show(scroll1);
284         gtk_box_pack_start(GTK_BOX(vbox2), scroll1, TRUE, TRUE, 0);
285         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll1),
286                                        GTK_POLICY_AUTOMATIC,
287                                        GTK_POLICY_AUTOMATIC);
288                                        
289         list_view = prefs_template_list_view_create();
290         gtk_widget_show(list_view);
291         gtk_widget_set_size_request(scroll1, -1, 140);
292         gtk_container_add(GTK_CONTAINER(scroll1), list_view);
293
294         /* ok | cancel */
295         gtkut_stock_button_set_create(&confirm_area, &cancel_btn, GTK_STOCK_CANCEL,
296                                       &ok_btn, GTK_STOCK_OK, NULL, NULL);
297         gtk_widget_show(confirm_area);
298         gtk_box_pack_end(GTK_BOX(vbox2), confirm_area, FALSE, FALSE, 0);
299         gtk_widget_grab_default(ok_btn);
300
301         gtk_window_set_title(GTK_WINDOW(window), _("Template configuration"));
302
303         g_signal_connect(G_OBJECT(window), "delete_event",
304                          G_CALLBACK(prefs_template_deleted_cb), NULL);
305         g_signal_connect(G_OBJECT(window), "size_allocate",
306                          G_CALLBACK(prefs_template_size_allocate_cb), NULL);
307         g_signal_connect(G_OBJECT(window), "key_press_event",
308                          G_CALLBACK(prefs_template_key_pressed_cb), NULL);
309         MANAGE_WINDOW_SIGNALS_CONNECT(window);
310         g_signal_connect(G_OBJECT(ok_btn), "clicked",
311                          G_CALLBACK(prefs_template_ok_cb), NULL);
312         g_signal_connect(G_OBJECT(cancel_btn), "clicked",
313                          G_CALLBACK(prefs_template_cancel_cb), NULL);
314
315         address_completion_start(window);
316
317         if (!geometry.min_height) {
318                 geometry.min_width = 440;
319                 geometry.min_height = 500;
320         }
321
322         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
323                                       GDK_HINT_MIN_SIZE);
324         gtk_widget_set_size_request(window, prefs_common.templateswin_width,
325                                     prefs_common.templateswin_height);
326
327         templates.window = window;
328         templates.ok_btn = ok_btn;
329         templates.list_view = list_view;
330         templates.entry_name = entry_name;
331         templates.entry_subject = entry_subject;
332         templates.entry_to = entry_to;
333         templates.entry_cc = entry_cc;
334         templates.entry_bcc = entry_bcc;        
335         templates.text_value = text_value;
336 }
337
338 static void prefs_template_window_setup(void)
339 {
340         GSList *tmpl_list;
341         GSList *cur;
342         Template *tmpl;
343         GtkListStore *store;
344
345         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
346                                 (templates.list_view)));
347
348         manage_window_set_transient(GTK_WINDOW(templates.window));
349         gtk_widget_grab_focus(templates.ok_btn);
350
351         gtk_list_store_clear(store);
352
353         prefs_template_list_view_insert_template(templates.list_view,
354                                                  NULL, _("(New)"),
355                                                  NULL, FALSE);
356         
357         tmpl_list = template_read_config();
358
359         for (cur = tmpl_list; cur != NULL; cur = cur->next) {
360                 tmpl = (Template *)cur->data;
361                 prefs_template_list_view_insert_template(templates.list_view,
362                                                          NULL, tmpl->name, 
363                                                          tmpl, FALSE);
364         }
365
366         g_slist_free(tmpl_list);
367 }
368
369 static void prefs_template_clear(void)
370 {
371         GtkListStore *store;
372
373         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
374                                 (templates.list_view)));
375         gtk_list_store_clear(store);
376 }
377
378 static gint prefs_template_deleted_cb(GtkWidget *widget, GdkEventAny *event,
379                                       gpointer data)
380 {
381         prefs_template_cancel_cb();
382         return TRUE;
383 }
384
385 static gboolean prefs_template_key_pressed_cb(GtkWidget *widget,
386                                               GdkEventKey *event, gpointer data)
387 {
388         if (event && event->keyval == GDK_Escape)
389                 prefs_template_cancel_cb();
390         else {
391                 GtkWidget *focused = gtkut_get_focused_child(
392                                         GTK_CONTAINER(widget));
393                 if (focused && GTK_IS_EDITABLE(focused)) {
394                         modified = TRUE;
395                 }
396         }
397         return FALSE;
398 }
399
400 static void prefs_template_ok_cb(void)
401 {
402         GSList *tmpl_list;
403
404         if (modified && alertpanel(_("Entry not saved"),
405                                  _("The entry was not saved. Close anyway?"),
406                                  GTK_STOCK_CLOSE, _("_Continue editing"), 
407                                  NULL) != G_ALERTDEFAULT) {
408                 return;
409         }
410         modified = FALSE;
411         tmpl_list = prefs_template_get_list();
412         template_set_config(tmpl_list);
413         compose_reflect_prefs_all();
414         prefs_template_clear();
415         gtk_widget_hide(templates.window);
416         inc_unlock();
417 }
418
419 static void prefs_template_cancel_cb(void)
420 {
421         if (modified && alertpanel(_("Entry not saved"),
422                                  _("The entry was not saved. Close anyway?"),
423                                  GTK_STOCK_CLOSE, _("_Continue editing"),
424                                  NULL) != G_ALERTDEFAULT) {
425                 return;
426         }
427         modified = FALSE;
428         prefs_template_clear();
429         gtk_widget_hide(templates.window);
430         inc_unlock();
431 }
432
433 /*!
434  *\brief        Request list for storage. New list is owned
435  *              by template.c...
436  */
437 static GSList *prefs_template_get_list(void)
438 {
439         GSList *tmpl_list = NULL;
440         Template *tmpl;
441         GtkTreeModel *model;
442         GtkTreeIter iter;
443
444         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
445         if (!gtk_tree_model_get_iter_first(model, &iter))
446                 return NULL;
447
448         do {
449                 gtk_tree_model_get(model, &iter,
450                                    TEMPL_DATA, &tmpl,
451                                    -1);
452                 
453                 if (tmpl) {
454                         Template *ntmpl;
455                         
456                         ntmpl = g_new(Template, 1);
457                         ntmpl->name    = tmpl->name && *(tmpl->name) 
458                                          ? g_strdup(tmpl->name) 
459                                          : NULL;
460                         ntmpl->subject = tmpl->subject && *(tmpl->subject) 
461                                          ? g_strdup(tmpl->subject) 
462                                          : NULL;
463                         ntmpl->to      = tmpl->to && *(tmpl->to)
464                                          ? g_strdup(tmpl->to)
465                                          : NULL;
466                         ntmpl->cc      = tmpl->cc && *(tmpl->cc)
467                                          ? g_strdup(tmpl->cc)
468                                          : NULL;
469                         ntmpl->bcc     = tmpl->bcc && *(tmpl->bcc)
470                                          ? g_strdup(tmpl->bcc)
471                                          : NULL;        
472                         ntmpl->value   = tmpl->value && *(tmpl->value)
473                                          ? g_strdup(tmpl->value)
474                                          : NULL;
475                         tmpl_list = g_slist_append(tmpl_list, ntmpl);
476                 }                       
477         
478         } while (gtk_tree_model_iter_next(model, &iter)); 
479
480         return tmpl_list;
481 }
482
483 static gboolean prefs_template_list_view_set_row(GtkTreeIter *row)
484 /* return TRUE if the row could be modified */
485 {
486         Template *tmpl;
487         gchar *name;
488         gchar *subject;
489         gchar *to;
490         gchar *cc;
491         gchar *bcc;     
492         gchar *value;
493         GtkTextBuffer *buffer;
494         GtkTextIter start, end;
495         GtkTreeModel *model;
496
497         model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
498
499         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
500         gtk_text_buffer_get_start_iter(buffer, &start);
501         gtk_text_buffer_get_iter_at_offset(buffer, &end, -1);
502         value = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
503
504         if (value && *value != '\0') {
505                 gchar *parsed_buf;
506                 MsgInfo dummyinfo;
507
508                 memset(&dummyinfo, 0, sizeof(MsgInfo));
509                 quote_fmt_init(&dummyinfo, NULL, NULL, TRUE);
510                 quote_fmt_scan_string(value);
511                 quote_fmt_parse();
512                 parsed_buf = quote_fmt_get_buffer();
513                 if (!parsed_buf) {
514                         alertpanel_error(_("Template format error."));
515                         g_free(value);
516                         return FALSE;
517                 }
518         }
519
520         name = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_name),
521                                       0, -1);
522         if (name[0] == '\0') {
523                 alertpanel_error(_("Template name is not set."));
524                 return FALSE;
525         }
526         subject = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_subject),
527                                          0, -1);
528         to = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_to),
529                                     0, -1);
530         cc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_cc),
531                                     0, -1);
532         bcc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_bcc),
533                                     0, -1);
534
535         if (subject && *subject == '\0') {
536                 g_free(subject);
537                 subject = NULL;
538         }
539         if (to && *to == '\0') {
540                 g_free(to);
541                 to = NULL;
542         }
543         if (cc && *cc == '\0') {
544                 g_free(cc);
545                 cc = NULL;
546         }
547         if (bcc && *bcc == '\0') {
548                 g_free(bcc);
549                 bcc = NULL;
550         }
551         
552         tmpl = g_new(Template, 1);
553         tmpl->name = name;
554         tmpl->subject = subject;
555         tmpl->to = to;
556         tmpl->cc = cc;
557         tmpl->bcc = bcc;        
558         tmpl->value = value;
559
560         prefs_template_list_view_insert_template(templates.list_view,
561                                                  row, tmpl->name, tmpl, TRUE);
562         return TRUE;
563 }
564
565 static void prefs_template_register_cb(void)
566 {
567         modified = !prefs_template_list_view_set_row(NULL);
568 }
569
570 static void prefs_template_substitute_cb(void)
571 {
572         Template *tmpl;
573         GtkTreeIter row;
574         GtkTreeSelection *selection;
575         GtkTreeModel *model;
576
577         selection = gtk_tree_view_get_selection
578                         (GTK_TREE_VIEW(templates.list_view));
579         
580         if (!gtk_tree_selection_get_selected(selection, &model, &row))
581                 return;
582
583         gtk_tree_model_get(model, &row, 
584                            TEMPL_DATA, &tmpl,
585                            -1);
586
587         if (!tmpl) return;
588
589         modified = !prefs_template_list_view_set_row(&row);
590 }
591
592 static void prefs_template_delete_cb(void)
593 {
594         Template *tmpl;
595         GtkTreeIter row;
596         GtkTreeSelection *selection;
597         GtkTreeModel *model;
598
599         selection = gtk_tree_view_get_selection
600                         (GTK_TREE_VIEW(templates.list_view));
601         
602         if (!gtk_tree_selection_get_selected(selection, &model, &row))
603                 return;
604
605         gtk_tree_model_get(model, &row, 
606                            TEMPL_DATA, &tmpl,
607                            -1);
608
609         if (!tmpl) 
610                 return;
611
612         if (alertpanel(_("Delete template"),
613                        _("Do you really want to delete this template?"),
614                        GTK_STOCK_CANCEL, GTK_STOCK_DELETE,
615                        NULL) != G_ALERTALTERNATE)
616                 return;
617
618         gtk_list_store_remove(GTK_LIST_STORE(model), &row);             
619 }
620
621 static GtkListStore* prefs_template_create_data_store(void)
622 {
623         return gtk_list_store_new(N_TEMPL_COLUMNS,
624                                   G_TYPE_STRING,        
625                                   G_TYPE_POINTER,
626                                   G_TYPE_AUTO_POINTER,
627                                   -1);
628 }
629
630 static gboolean prefs_template_list_view_find_insertion_point(GtkTreeModel* model,
631                                                         const gchar* template,
632                                                         GtkTreeIter* iter)
633 {
634         GtkTreeIter i;
635         gchar *text = NULL;
636
637         if ((gtk_tree_model_get_iter_first(model, &i))) {
638                 for ( ; gtk_tree_model_iter_next(model, &i); ) {
639                         gtk_tree_model_get(model, &i, TEMPL_TEXT, &text, -1);
640                         if (strcmp(text, template) >= 0) {
641                                 g_free(text);
642                                 *iter = i;
643                                 return TRUE;
644                         }
645                         g_free(text);
646                 }
647         }
648         /* cannot add before (need to append) */
649         return FALSE;
650 }
651
652 static void prefs_template_list_view_insert_template(GtkWidget *list_view,
653                                                      GtkTreeIter *row_iter,
654                                                      const gchar *template,
655                                                      Template *data,
656                                                          gboolean sorted)
657 {
658         GtkTreeIter iter;
659         GtkTreeModel *model = gtk_tree_view_get_model
660                                         (GTK_TREE_VIEW(list_view));
661         GtkListStore *list_store = GTK_LIST_STORE(model);
662         GAuto *auto_data;                                       
663
664         if (row_iter == NULL) {
665                 if (sorted) {
666                         GtkTreeIter insertion_point;
667
668                         /* insert sorted */
669                         if (prefs_template_list_view_find_insertion_point(model,
670                                         template, &insertion_point)) {
671                                 gtk_list_store_insert_before(list_store, &iter,
672                                                 &insertion_point);
673                         } else {
674                                 gtk_list_store_append(list_store, &iter);
675                         }
676                 } else {
677                         /* append new */
678                         gtk_list_store_append(list_store, &iter);
679                 }
680         } else {
681                 /* modify the existing */
682                 iter = *row_iter;
683
684                 if (sorted) {
685                         GtkTreeIter insertion_point;
686                         gchar *text = NULL;
687
688                         /* force re-sorting if template's name changed */
689                         gtk_tree_model_get(model, row_iter, TEMPL_TEXT, &text, -1);
690                         if (strcmp(text, template) != 0) {
691
692                                 /* move the modified template */
693                                 if (prefs_template_list_view_find_insertion_point(model,
694                                                 template, &insertion_point)) {
695                                         gtk_list_store_move_before(list_store, row_iter,
696                                                         &insertion_point);
697                                 } else {
698                                         /* move to the end */
699                                         gtk_list_store_move_before(list_store, row_iter,
700                                                         NULL);
701                                 }
702                         }
703                         g_free(text);
704                 }
705         }
706                 
707         auto_data = g_auto_pointer_new_with_free(data, (GFreeFunc) template_free);  
708
709         /* if replacing data in an existing row, the auto pointer takes care
710          * of destroying the Template data */
711         gtk_list_store_set(list_store, &iter,
712                            TEMPL_TEXT, template,
713                            TEMPL_DATA, data,
714                            TEMPL_AUTO_DATA, auto_data,
715                            -1);
716
717         g_auto_pointer_free(auto_data);                    
718 }
719
720 static GtkWidget *prefs_template_list_view_create(void)
721 {
722         GtkTreeView *list_view;
723         GtkTreeSelection *selector;
724         GtkTreeModel *model;
725
726         model = GTK_TREE_MODEL(prefs_template_create_data_store());
727         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
728         g_object_unref(model);  
729         
730         gtk_tree_view_set_rules_hint(list_view, prefs_common.enable_rules_hint);
731         
732         selector = gtk_tree_view_get_selection(list_view);
733         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
734         gtk_tree_selection_set_select_function(selector, prefs_template_selected,
735                                                NULL, NULL);
736
737         /* create the columns */
738         prefs_template_create_list_view_columns(GTK_WIDGET(list_view));
739
740         return GTK_WIDGET(list_view);
741 }
742
743 static void prefs_template_create_list_view_columns(GtkWidget *list_view)
744 {
745         GtkTreeViewColumn *column;
746         GtkCellRenderer *renderer;
747
748         renderer = gtk_cell_renderer_text_new();
749         column = gtk_tree_view_column_new_with_attributes
750                         (_("Current templates"),
751                          renderer,
752                          "text", TEMPL_TEXT,
753                          NULL);
754         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
755 }
756
757 static gboolean prefs_template_selected(GtkTreeSelection *selector,
758                                         GtkTreeModel *model, 
759                                         GtkTreePath *path,
760                                         gboolean currently_selected,
761                                         gpointer data)
762 {
763         Template *tmpl;
764         Template tmpl_def;
765         GtkTextBuffer *buffer;
766         GtkTextIter iter;
767         GtkTreeIter titer;
768
769         if (currently_selected)
770                 return TRUE;
771
772         if (!gtk_tree_model_get_iter(model, &titer, path))
773                 return TRUE;
774
775         tmpl_def.name = _("Template");
776         tmpl_def.subject = "";
777         tmpl_def.to = "";
778         tmpl_def.cc = "";
779         tmpl_def.bcc = "";      
780         tmpl_def.value = "";
781
782         gtk_tree_model_get(model, &titer,
783                            TEMPL_DATA, &tmpl,
784                            -1);
785
786         if (!tmpl) 
787                 tmpl = &tmpl_def;
788
789         gtk_entry_set_text(GTK_ENTRY(templates.entry_name), tmpl->name);
790         gtk_entry_set_text(GTK_ENTRY(templates.entry_to),
791                            tmpl->to ? tmpl->to : "");
792         gtk_entry_set_text(GTK_ENTRY(templates.entry_cc),
793                            tmpl->cc ? tmpl->cc : "");
794         gtk_entry_set_text(GTK_ENTRY(templates.entry_bcc),
795                            tmpl->bcc ? tmpl->bcc : "");                 
796         gtk_entry_set_text(GTK_ENTRY(templates.entry_subject),
797                            tmpl->subject ? tmpl->subject : "");
798         
799         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
800         gtk_text_buffer_set_text(buffer, "", -1);
801         gtk_text_buffer_get_start_iter(buffer, &iter);
802         gtk_text_buffer_insert(buffer, &iter, tmpl->value, -1);
803
804         return TRUE;
805 }