Fix CID 1596595: Resource leak
[claws.git] / src / prefs_template.c
blob57604ff9a45de908c1512ef870dbf1e3b96a1511
1 /*
2 * Claws Mail templates subsystem
3 * Copyright (C) 2001 Alexander Barinov
4 * Copyright (C) 2001-2024 The Claws Mail team
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.
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.
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/>.
20 #include "config.h"
21 #include "defs.h"
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 <sys/stat.h>
30 #include "template.h"
31 #include "main.h"
32 #include "prefs_gtk.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 #include "manual.h"
43 #include "menu.h"
44 #include "account.h"
46 enum {
47 TEMPL_TEXT,
48 TEMPL_DATA,
49 TEMPL_AUTO_DATA, /*!< auto pointer */
50 N_TEMPL_COLUMNS
53 static struct Templates {
54 GtkWidget *window;
55 GtkWidget *ok_btn;
56 GtkWidget *list_view;
57 GtkWidget *entry_name;
58 GtkWidget *entry_subject;
59 GtkWidget *entry_from;
60 GtkWidget *entry_to;
61 GtkWidget *entry_cc;
62 GtkWidget *entry_bcc;
63 GtkWidget *entry_replyto;
64 GtkWidget *text_value;
65 } templates;
67 static int modified = FALSE;
68 static int modified_list = FALSE;
70 static struct
72 gchar *label;
73 GtkWidget **entry;
74 gboolean compl;
75 gchar *tooltips;
76 } widgets_table[] = {
77 {N_("Name"), &templates.entry_name, FALSE,
78 N_("This name is used as the Menu item")},
79 {"From", &templates.entry_from, TRUE,
80 N_("Override composing account's From header. This doesn't change the composing account.")},
81 {"To", &templates.entry_to, TRUE, NULL},
82 {"Cc", &templates.entry_cc, TRUE, NULL},
83 {"Bcc", &templates.entry_bcc, TRUE, NULL},
84 {"Reply-To", &templates.entry_replyto, TRUE, NULL},
85 {"Subject", &templates.entry_subject, FALSE, NULL},
86 {NULL, NULL, FALSE, NULL}
90 /* widget creating functions */
91 static void prefs_template_window_create (void);
92 static void prefs_template_window_setup (void);
94 static GSList *prefs_template_get_list (void);
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);
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);
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);
129 /* Called from mainwindow.c */
130 void prefs_template_open(void)
132 inc_lock();
134 prefs_template_window_create();
136 prefs_template_window_setup();
137 gtk_widget_show(templates.window);
138 gtk_window_set_modal(GTK_WINDOW(templates.window), TRUE);
142 *\brief Save Gtk object size to prefs dataset
144 static void prefs_template_size_allocate_cb(GtkWidget *widget,
145 GtkAllocation *allocation)
147 cm_return_if_fail(allocation != NULL);
149 gtk_window_get_size(GTK_WINDOW(widget),
150 &prefs_common.templateswin_width, &prefs_common.templateswin_height);
153 static void prefs_template_window_create(void)
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|replyto|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;
189 debug_print("Creating templates configuration window...\n");
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 gtk_window_set_type_hint(GTK_WINDOW(window), GDK_WINDOW_TYPE_HINT_DIALOG);
197 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 8);
198 gtk_widget_show(vbox);
199 gtk_container_add(GTK_CONTAINER(window), vbox);
200 gtk_container_set_border_width(GTK_CONTAINER(vbox), 4);
202 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
203 gtk_widget_show(scrolled_window);
204 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
205 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
206 gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 0);
208 /* vpaned to separate template settings from templates list */
209 vpaned = gtk_paned_new(GTK_ORIENTATION_VERTICAL);
210 gtk_widget_show(vpaned);
211 gtk_container_add(GTK_CONTAINER(scrolled_window), vpaned);
212 gtk_viewport_set_shadow_type (GTK_VIEWPORT(
213 gtk_bin_get_child(GTK_BIN(scrolled_window))), GTK_SHADOW_NONE);
215 /* vbox to handle template name and content */
216 vbox1 = gtk_box_new(GTK_ORIENTATION_VERTICAL, 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);
221 table = gtk_grid_new();
222 gtk_widget_show(table);
223 gtk_grid_set_row_spacing(GTK_GRID(table), VSPACING_NARROW_2);
224 gtk_grid_set_column_spacing(GTK_GRID(table), 4);
226 gtk_box_pack_start (GTK_BOX (vbox1), table, FALSE, FALSE, 0);
228 for (i=0; widgets_table[i].label; i++) {
230 GtkWidget *label;
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_label_set_xalign(GTK_LABEL(label), 1.0);
237 gtk_grid_attach(GTK_GRID(table), label, 0, i, 1, 1);
239 *(widgets_table[i].entry) = gtk_entry_new();
240 gtk_widget_show(*(widgets_table[i].entry));
241 gtk_grid_attach(GTK_GRID(table), *(widgets_table[i].entry), 1, i, 1, 1);
242 gtk_widget_set_hexpand(*(widgets_table[i].entry), TRUE);
243 gtk_widget_set_halign(*(widgets_table[i].entry), GTK_ALIGN_FILL);
244 CLAWS_SET_TIP(*(widgets_table[i].entry),
245 _(widgets_table[i].tooltips));
248 /* template content */
249 scroll2 = gtk_scrolled_window_new(NULL, NULL);
250 gtk_widget_show(scroll2);
251 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll2),
252 GTK_POLICY_AUTOMATIC,
253 GTK_POLICY_AUTOMATIC);
254 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll2),
255 GTK_SHADOW_IN);
256 gtk_box_pack_start(GTK_BOX(vbox1), scroll2, TRUE, TRUE, 0);
258 text_value = gtk_text_view_new();
259 if (prefs_common.textfont) {
260 PangoFontDescription *font_desc;
262 font_desc = pango_font_description_from_string
263 (prefs_common.textfont);
264 if (font_desc) {
265 gtk_widget_override_font(text_value, font_desc);
266 pango_font_description_free(font_desc);
269 gtk_widget_show(text_value);
270 #ifndef GENERIC_UMPC
271 gtk_scrolled_window_set_min_content_height(GTK_SCROLLED_WINDOW(scroll2), 120);
272 #else
273 gtk_scrolled_window_set_min_content_height(GTK_SCROLLED_WINDOW(scroll2), 60);
274 #endif
275 gtk_container_add(GTK_CONTAINER(scroll2), text_value);
276 gtk_text_view_set_editable(GTK_TEXT_VIEW(text_value), TRUE);
277 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text_value), GTK_WRAP_WORD);
279 /* vbox for buttons and templates list */
280 vbox2 = gtk_box_new(GTK_ORIENTATION_VERTICAL, 6);
281 gtk_widget_show(vbox2);
282 gtk_container_set_border_width(GTK_CONTAINER(vbox2), 8);
283 gtk_paned_pack2(GTK_PANED(vpaned), vbox2, TRUE, FALSE);
285 /* register | substitute | delete */
286 hbox2 = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 4);
287 gtk_widget_show(hbox2);
288 gtk_box_pack_start(GTK_BOX(vbox2), hbox2, FALSE, FALSE, 0);
290 arrow1 = gtk_image_new_from_icon_name("pan-down-symbolic", GTK_ICON_SIZE_MENU);
291 gtk_widget_show(arrow1);
292 gtk_box_pack_start(GTK_BOX(hbox2), arrow1, FALSE, FALSE, 0);
294 hbox3 = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 4);
295 gtk_widget_show(hbox3);
296 gtk_box_pack_start(GTK_BOX(hbox2), hbox3, FALSE, FALSE, 0);
298 reg_btn = gtkut_stock_button("list-add", _("_Add"));
299 gtk_widget_show(reg_btn);
300 gtk_box_pack_start(GTK_BOX(hbox3), reg_btn, FALSE, TRUE, 0);
301 g_signal_connect(G_OBJECT (reg_btn), "clicked",
302 G_CALLBACK (prefs_template_register_cb), NULL);
303 CLAWS_SET_TIP(reg_btn,
304 _("Append the new template above to the list"));
306 subst_btn = gtkut_stock_button("edit-redo", _("_Replace"));
307 gtk_widget_show(subst_btn);
308 gtk_box_pack_start(GTK_BOX(hbox3), subst_btn, FALSE, TRUE, 0);
309 g_signal_connect(G_OBJECT(subst_btn), "clicked",
310 G_CALLBACK(prefs_template_substitute_cb),
311 NULL);
312 CLAWS_SET_TIP(subst_btn,
313 _("Replace the selected template in list with the template above"));
315 del_btn = gtkut_stock_button("edit-delete", _("D_elete"));
316 gtk_widget_show(del_btn);
317 gtk_box_pack_start(GTK_BOX(hbox3), del_btn, FALSE, TRUE, 0);
318 g_signal_connect(G_OBJECT(del_btn), "clicked",
319 G_CALLBACK(prefs_template_delete_cb), NULL);
320 CLAWS_SET_TIP(del_btn,
321 _("Delete the selected template from the list"));
323 clear_btn = gtkut_stock_button("edit-clear", _("C_lear"));
324 gtk_widget_show (clear_btn);
325 gtk_box_pack_start (GTK_BOX (hbox3), clear_btn, FALSE, TRUE, 0);
326 g_signal_connect(G_OBJECT (clear_btn), "clicked",
327 G_CALLBACK(prefs_template_clear_cb), NULL);
328 CLAWS_SET_TIP(clear_btn,
329 _("Clear all the input fields in the dialog"));
331 desc_btn = gtkut_stock_button("dialog-information", _("_Information"));
332 gtk_widget_show(desc_btn);
333 gtk_box_pack_end(GTK_BOX(hbox2), desc_btn, FALSE, FALSE, 0);
334 g_signal_connect(G_OBJECT(desc_btn), "clicked",
335 G_CALLBACK(quote_fmt_quote_description), window);
336 CLAWS_SET_TIP(desc_btn,
337 _("Show information on configuring templates"));
339 /* templates list */
340 hbox4 = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
341 gtk_widget_show(hbox4);
342 gtk_box_pack_start(GTK_BOX(vbox2), hbox4, TRUE, TRUE, 0);
344 scroll1 = gtk_scrolled_window_new(NULL, NULL);
345 gtk_widget_show(scroll1);
346 gtk_box_pack_start(GTK_BOX(hbox4), scroll1, TRUE, TRUE, 0);
347 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll1),
348 GTK_POLICY_AUTOMATIC,
349 GTK_POLICY_AUTOMATIC);
351 vbox3 = gtk_box_new(GTK_ORIENTATION_VERTICAL, 8);
352 gtk_widget_show(vbox3);
353 gtk_box_pack_start(GTK_BOX(hbox4), vbox3, FALSE, FALSE, 0);
355 top_btn = gtkut_stock_button("go-top", _("_Top"));
356 gtk_widget_show(top_btn);
357 gtk_box_pack_start(GTK_BOX(vbox3), top_btn, FALSE, FALSE, 0);
358 g_signal_connect(G_OBJECT(top_btn), "clicked",
359 G_CALLBACK(prefs_template_top_cb), NULL);
360 CLAWS_SET_TIP(top_btn,
361 _("Move the selected template to the top"));
363 PACK_SPACER(vbox3, spc_vbox, VSPACING_NARROW_2);
365 up_btn = gtkut_stock_button("go-up", _("_Up"));
366 gtk_widget_show(up_btn);
367 gtk_box_pack_start (GTK_BOX(vbox3), up_btn, FALSE, FALSE, 0);
368 g_signal_connect(G_OBJECT(up_btn), "clicked",
369 G_CALLBACK(prefs_template_up_cb), NULL);
370 CLAWS_SET_TIP(up_btn,
371 _("Move the selected template up"));
373 down_btn = gtkut_stock_button("go-down", _("_Down"));
374 gtk_widget_show (down_btn);
375 gtk_box_pack_start(GTK_BOX (vbox3), down_btn, FALSE, FALSE, 0);
376 g_signal_connect(G_OBJECT (down_btn), "clicked",
377 G_CALLBACK(prefs_template_down_cb), NULL);
378 CLAWS_SET_TIP(down_btn,
379 _("Move the selected template down"));
381 PACK_SPACER(vbox3, spc_vbox, VSPACING_NARROW_2);
383 bottom_btn = gtkut_stock_button("go-bottom", _("_Bottom"));
384 gtk_widget_show(bottom_btn);
385 gtk_box_pack_start(GTK_BOX(vbox3), bottom_btn, FALSE, FALSE, 0);
386 g_signal_connect(G_OBJECT(bottom_btn), "clicked",
387 G_CALLBACK(prefs_template_bottom_cb), NULL);
388 CLAWS_SET_TIP(bottom_btn,
389 _("Move the selected template to the bottom"));
391 list_view = prefs_template_list_view_create();
392 gtk_widget_show(list_view);
393 gtk_widget_set_size_request(scroll1, -1, 140);
394 gtk_container_add(GTK_CONTAINER(scroll1), list_view);
396 /* help | cancel | ok */
397 gtkut_stock_button_set_create_with_help(&confirm_area, &help_btn,
398 &cancel_btn, NULL, _("_Cancel"),
399 &ok_btn, NULL, _("_OK"),
400 NULL, NULL, NULL);
401 gtk_widget_show(confirm_area);
402 gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
403 gtk_widget_grab_default(ok_btn);
405 gtk_window_set_title(GTK_WINDOW(window), _("Template configuration"));
407 g_signal_connect(G_OBJECT(window), "delete_event",
408 G_CALLBACK(prefs_template_deleted_cb), NULL);
409 g_signal_connect(G_OBJECT(window), "size_allocate",
410 G_CALLBACK(prefs_template_size_allocate_cb), NULL);
411 g_signal_connect(G_OBJECT(window), "key_press_event",
412 G_CALLBACK(prefs_template_key_pressed_cb), NULL);
413 MANAGE_WINDOW_SIGNALS_CONNECT(window);
414 g_signal_connect(G_OBJECT(ok_btn), "clicked",
415 G_CALLBACK(prefs_template_ok_cb), NULL);
416 g_signal_connect(G_OBJECT(cancel_btn), "clicked",
417 G_CALLBACK(prefs_template_cancel_cb), NULL);
418 g_signal_connect(G_OBJECT(help_btn), "clicked",
419 G_CALLBACK(manual_open_with_anchor_cb),
420 MANUAL_ANCHOR_TEMPLATES);
422 if (!geometry.min_height) {
423 geometry.min_width = 500;
424 geometry.min_height = 540;
427 gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
428 GDK_HINT_MIN_SIZE);
429 gtk_window_set_default_size(GTK_WINDOW(window),
430 prefs_common.templateswin_width,
431 prefs_common.templateswin_height);
433 templates.window = window;
434 templates.ok_btn = ok_btn;
435 templates.list_view = list_view;
436 templates.text_value = text_value;
439 static void prefs_template_reset_dialog(void)
441 GtkTextBuffer *buffer;
443 gtk_entry_set_text(GTK_ENTRY(templates.entry_name), "");
444 gtk_entry_set_text(GTK_ENTRY(templates.entry_from), "");
445 gtk_entry_set_text(GTK_ENTRY(templates.entry_to), "");
446 gtk_entry_set_text(GTK_ENTRY(templates.entry_cc), "");
447 gtk_entry_set_text(GTK_ENTRY(templates.entry_bcc), "");
448 gtk_entry_set_text(GTK_ENTRY(templates.entry_replyto), "");
449 gtk_entry_set_text(GTK_ENTRY(templates.entry_subject), "");
451 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
452 gtk_text_buffer_set_text(buffer, "", -1);
455 static void prefs_template_clear_list(void)
457 GtkListStore *store;
459 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
460 (templates.list_view)));
461 gtk_list_store_clear(store);
463 prefs_template_list_view_insert_template(templates.list_view,
464 -1, _("(New)"),
465 NULL);
468 static void prefs_template_window_setup(void)
470 GSList *tmpl_list;
471 GSList *cur;
472 Template *tmpl;
473 int i;
475 manage_window_set_transient(GTK_WINDOW(templates.window));
476 gtk_widget_grab_focus(templates.ok_btn);
478 prefs_template_clear_list();
480 tmpl_list = template_read_config();
482 address_completion_start(templates.window);
484 for (i=0; widgets_table[i].label; i++) {
485 if (widgets_table[i].compl)
486 address_completion_register_entry(
487 GTK_ENTRY(*(widgets_table[i].entry)), TRUE);
490 for (cur = tmpl_list; cur != NULL; cur = cur->next) {
491 tmpl = (Template *)cur->data;
492 prefs_template_list_view_insert_template(templates.list_view,
493 -1, tmpl->name,
494 tmpl);
497 prefs_template_reset_dialog();
499 g_slist_free(tmpl_list);
502 static gint prefs_template_deleted_cb(GtkWidget *widget, GdkEventAny *event,
503 gpointer data)
505 prefs_template_cancel_cb(NULL, NULL);
506 return TRUE;
509 static gboolean prefs_template_key_pressed_cb(GtkWidget *widget,
510 GdkEventKey *event, gpointer data)
512 if (event && event->keyval == GDK_KEY_Escape)
513 prefs_template_cancel_cb(NULL, NULL);
514 else {
515 GtkWidget *focused = gtkut_get_focused_child(
516 GTK_CONTAINER(widget));
517 if (focused && GTK_IS_EDITABLE(focused)) {
518 modified = TRUE;
521 return FALSE;
524 static gboolean prefs_template_search_func_cb (GtkTreeModel *model, gint column, const gchar *key,
525 GtkTreeIter *iter, gpointer search_data)
527 gchar *store_string;
528 gint key_len;
529 gboolean retval;
530 GtkTreePath *path;
532 gtk_tree_model_get (model, iter, column, &store_string, -1);
534 if (!store_string || !key) return FALSE;
536 key_len = strlen (key);
537 retval = (strncmp (key, store_string, key_len) != 0);
539 g_free(store_string);
540 debug_print("selecting row\n");
541 path = gtk_tree_model_get_path(model, iter);
542 prefs_template_select_row(GTK_TREE_VIEW(templates.list_view), path);
543 gtk_tree_path_free(path);
545 return retval;
547 static void prefs_template_address_completion_end(void)
549 gint i;
551 for (i=0; widgets_table[i].label; i++) {
552 if (widgets_table[i].compl)
553 address_completion_unregister_entry(
554 GTK_ENTRY(*(widgets_table[i].entry)));
556 address_completion_end(templates.window);
559 static void prefs_template_ok_cb(gpointer action, gpointer data)
561 GSList *tmpl_list;
562 GtkListStore *store;
564 if (modified && alertpanel(_("Entry not saved"),
565 _("The entry was not saved. Close anyway?"),
566 "window-close", _("_Close"), NULL, _("_Continue editing"),
567 NULL, NULL, ALERTFOCUS_SECOND) != G_ALERTDEFAULT) {
568 return;
571 prefs_template_address_completion_end();
573 modified = FALSE;
574 modified_list = FALSE;
575 tmpl_list = prefs_template_get_list();
576 template_set_config(tmpl_list);
577 compose_reflect_prefs_all();
578 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
579 (templates.list_view)));
580 gtk_list_store_clear(store);
581 gtk_widget_destroy(templates.window);
582 inc_unlock();
585 static void prefs_template_cancel_cb(gpointer action, gpointer data)
587 GtkListStore *store;
589 if (modified && alertpanel(_("Entry not saved"),
590 _("The entry was not saved. Close anyway?"),
591 "window-close", _("_Close"), NULL, _("_Continue editing"),
592 NULL, NULL, ALERTFOCUS_SECOND) != G_ALERTDEFAULT) {
593 return;
594 } else if (modified_list && alertpanel(_("Templates list not saved"),
595 _("The templates list has been modified. Close anyway?"),
596 "window-close", _("_Close"), NULL, _("_Continue editing"),
597 NULL, NULL, ALERTFOCUS_SECOND) != G_ALERTDEFAULT) {
598 return;
601 prefs_template_address_completion_end();
603 modified = FALSE;
604 modified_list = FALSE;
605 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
606 (templates.list_view)));
607 gtk_list_store_clear(store);
608 gtk_widget_destroy(templates.window);
609 inc_unlock();
613 *\brief Request list for storage. New list is owned
614 * by template.c...
616 static GSList *prefs_template_get_list(void)
618 GSList *tmpl_list = NULL;
619 Template *tmpl;
620 GtkTreeModel *model;
621 GtkTreeIter iter;
623 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
624 if (!gtk_tree_model_get_iter_first(model, &iter))
625 return NULL;
627 do {
628 gtk_tree_model_get(model, &iter,
629 TEMPL_DATA, &tmpl,
630 -1);
632 if (tmpl) {
633 Template *ntmpl;
635 ntmpl = g_new(Template, 1);
636 ntmpl->load_filename = NULL;
637 ntmpl->name = tmpl->name && *(tmpl->name)
638 ? g_strdup(tmpl->name)
639 : NULL;
640 ntmpl->subject = tmpl->subject && *(tmpl->subject)
641 ? g_strdup(tmpl->subject)
642 : NULL;
643 ntmpl->from = tmpl->from && *(tmpl->from)
644 ? g_strdup(tmpl->from)
645 : NULL;
646 ntmpl->to = tmpl->to && *(tmpl->to)
647 ? g_strdup(tmpl->to)
648 : NULL;
649 ntmpl->cc = tmpl->cc && *(tmpl->cc)
650 ? g_strdup(tmpl->cc)
651 : NULL;
652 ntmpl->bcc = tmpl->bcc && *(tmpl->bcc)
653 ? g_strdup(tmpl->bcc)
654 : NULL;
655 ntmpl->replyto = tmpl->replyto && *(tmpl->replyto)
656 ? g_strdup(tmpl->replyto)
657 : NULL;
658 ntmpl->value = tmpl->value && *(tmpl->value)
659 ? g_strdup(tmpl->value)
660 : NULL;
661 tmpl_list = g_slist_append(tmpl_list, ntmpl);
664 } while (gtk_tree_model_iter_next(model, &iter));
666 return tmpl_list;
669 gboolean prefs_template_string_is_valid(gchar *string, gint *line, gboolean escaped_string, gboolean email)
671 gboolean result = TRUE;
672 if (string && *string != '\0') {
673 gchar *tmp = NULL;
674 gchar *parsed_buf;
675 MsgInfo dummyinfo;
676 PrefsAccount *account = account_get_default();
678 if (escaped_string) {
679 tmp = malloc(strlen(string)+1);
680 pref_get_unescaped_pref(tmp, string);
681 } else {
682 tmp = g_strdup(string);
684 memset(&dummyinfo, 0, sizeof(MsgInfo));
685 /* init dummy fields, so we can test the result of the parse */
686 dummyinfo.date="Sat, 30 May 2009 01:23:45 +0200";
687 dummyinfo.fromname="John Doe";
688 dummyinfo.from="John Doe <john@example.com>";
689 dummyinfo.to="John Doe <john@example.com>";
690 dummyinfo.cc="John Doe <john@example.com>";
691 dummyinfo.msgid="<1234john@example.com>";
692 dummyinfo.inreplyto="<1234john@example.com>";
693 dummyinfo.newsgroups="alt.test";
694 dummyinfo.subject="subject";
697 #ifdef USE_ENCHANT
698 quote_fmt_init(&dummyinfo, NULL, NULL, TRUE, account, FALSE, NULL);
699 #else
700 quote_fmt_init(&dummyinfo, NULL, NULL, TRUE, account, FALSE);
701 #endif
702 quote_fmt_scan_string(tmp);
703 quote_fmt_parse();
704 g_free(tmp);
705 parsed_buf = quote_fmt_get_buffer();
706 if (!parsed_buf) {
707 if (line)
708 *line = quote_fmt_get_line();
709 quote_fmtlex_destroy();
710 return FALSE;
712 quote_fmt_reset_vartable();
713 quote_fmtlex_destroy();
715 return result;
718 static gboolean prefs_template_list_view_set_row(gint row)
719 /* return TRUE if the row could be modified */
721 Template *tmpl;
722 gchar *name;
723 gchar *subject;
724 gchar *from;
725 gchar *to;
726 gchar *cc;
727 gchar *bcc;
728 gchar *replyto;
729 gchar *value;
730 GtkTextBuffer *buffer;
731 GtkTextIter start, end;
732 gint line;
734 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
735 gtk_text_buffer_get_start_iter(buffer, &start);
736 gtk_text_buffer_get_iter_at_offset(buffer, &end, -1);
737 value = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
739 if (value && *value == '\0') {
740 g_free(value);
741 value = NULL;
743 if (!prefs_template_string_is_valid(value, &line, TRUE, FALSE)) {
744 alertpanel_error(_("The body of the template has an error at line %d."), line);
745 g_free(value);
746 return FALSE;
749 name = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_name),
750 0, -1);
751 if (*name == '\0') {
752 alertpanel_error(_("The template's name is not set."));
753 g_free(value);
754 return FALSE;
756 from = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_from),
757 0, -1);
758 to = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_to),
759 0, -1);
760 cc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_cc),
761 0, -1);
762 bcc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_bcc),
763 0, -1);
764 replyto = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_replyto),
765 0, -1);
766 subject = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_subject),
767 0, -1);
769 if (from && *from == '\0') {
770 g_free(from);
771 from = NULL;
773 if (to && *to == '\0') {
774 g_free(to);
775 to = NULL;
777 if (cc && *cc == '\0') {
778 g_free(cc);
779 cc = NULL;
781 if (bcc && *bcc == '\0') {
782 g_free(bcc);
783 bcc = NULL;
785 if (replyto && *replyto == '\0') {
786 g_free(replyto);
787 replyto = NULL;
789 if (subject && *subject == '\0') {
790 g_free(subject);
791 subject = NULL;
794 if (!prefs_template_string_is_valid(from, NULL, TRUE, TRUE)) {
795 alertpanel_error(_("The \"From\" field of the template contains an invalid email address."));
796 g_free(from);
797 g_free(value);
798 return FALSE;
800 if (!prefs_template_string_is_valid(to, NULL, TRUE, TRUE)) {
801 alertpanel_error(_("The \"To\" field of the template contains an invalid email address."));
802 g_free(to);
803 g_free(value);
804 return FALSE;
806 if (!prefs_template_string_is_valid(cc, NULL, TRUE, TRUE)) {
807 alertpanel_error(_("The \"Cc\" field of the template contains an invalid email address."));
808 g_free(cc);
809 g_free(value);
810 return FALSE;
812 if (!prefs_template_string_is_valid(bcc, NULL, TRUE, TRUE)) {
813 alertpanel_error(_("The \"Bcc\" field of the template contains an invalid email address."));
814 g_free(bcc);
815 g_free(value);
816 return FALSE;
818 if (!prefs_template_string_is_valid(replyto, NULL, TRUE, TRUE)) {
819 alertpanel_error(_("The \"Reply-To\" field of the template contains an invalid email address."));
820 g_free(replyto);
821 g_free(value);
822 return FALSE;
824 if (!prefs_template_string_is_valid(subject, NULL, TRUE, FALSE)) {
825 alertpanel_error(_("The \"Subject\" field of the template is invalid."));
826 g_free(subject);
827 g_free(value);
828 return FALSE;
831 tmpl = g_new(Template, 1);
832 tmpl->load_filename = NULL;
833 tmpl->name = name;
834 tmpl->subject = subject;
835 tmpl->from = from;
836 tmpl->to = to;
837 tmpl->cc = cc;
838 tmpl->bcc = bcc;
839 tmpl->replyto = replyto;
840 tmpl->value = value;
842 prefs_template_list_view_insert_template(templates.list_view,
843 row, tmpl->name, tmpl);
845 return TRUE;
848 static void prefs_template_register_cb(gpointer action, gpointer data)
850 modified = !prefs_template_list_view_set_row(-1);
851 modified_list = TRUE;
854 static void prefs_template_substitute_cb(gpointer action, gpointer data)
856 Template *tmpl;
857 gint row;
858 GtkTreeIter iter;
859 GtkTreeModel *model;
861 row = gtkut_list_view_get_selected_row(templates.list_view);
862 if (row <= 0)
863 return;
865 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
866 if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
867 return;
869 gtk_tree_model_get(model, &iter, TEMPL_DATA, &tmpl, -1);
870 if (!tmpl)
871 return;
873 modified = !prefs_template_list_view_set_row(row);
874 modified_list = TRUE;
877 static void prefs_template_delete_cb(gpointer action, gpointer data)
879 Template *tmpl;
880 gint row;
881 GtkTreeIter iter;
882 GtkTreeModel *model;
884 row = gtkut_list_view_get_selected_row(templates.list_view);
885 if (row <= 0)
886 return;
888 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
889 if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
890 return;
892 gtk_tree_model_get(model, &iter, TEMPL_DATA, &tmpl, -1);
893 if (!tmpl)
894 return;
896 if (alertpanel(_("Delete template"),
897 _("Do you really want to delete this template?"),
898 NULL, _("_Cancel"), "edit-delete", _("D_elete"), NULL, NULL,
899 ALERTFOCUS_FIRST) != G_ALERTALTERNATE)
900 return;
902 gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
903 prefs_template_reset_dialog();
904 modified_list = TRUE;
907 static void prefs_template_delete_all_cb(gpointer action, gpointer data)
909 if (alertpanel(_("Delete all templates"),
910 _("Do you really want to delete all the templates?"),
911 NULL, _("_Cancel"), "edit-delete", _("D_elete"), NULL, NULL,
912 ALERTFOCUS_SECOND) == G_ALERTDEFAULT)
913 return;
915 prefs_template_clear_list();
916 modified = FALSE;
918 prefs_template_reset_dialog();
919 modified_list = TRUE;
922 static void prefs_template_duplicate_cb(gpointer action, gpointer data)
924 Template *tmpl;
925 gint row;
926 GtkTreeIter iter;
927 GtkTreeModel *model;
929 row = gtkut_list_view_get_selected_row(templates.list_view);
930 if (row <= 0)
931 return;
933 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
934 if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
935 return;
937 gtk_tree_model_get(model, &iter, TEMPL_DATA, &tmpl, -1);
938 if (!tmpl)
939 return;
941 modified_list = !prefs_template_list_view_set_row(-row-2);
944 static void prefs_template_clear_cb(gpointer action, gpointer data)
946 prefs_template_reset_dialog();
949 static void prefs_template_top_cb(gpointer action, gpointer data)
951 gint row;
952 GtkTreeIter top, sel;
953 GtkTreeModel *model;
955 row = gtkut_list_view_get_selected_row(templates.list_view);
956 if (row <= 1)
957 return;
959 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
961 if (!gtk_tree_model_iter_nth_child(model, &top, NULL, 0)
962 || !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
963 return;
965 gtk_list_store_move_after(GTK_LIST_STORE(model), &sel, &top);
966 gtkut_list_view_select_row(templates.list_view, 1);
967 modified_list = TRUE;
970 static void prefs_template_up_cb(gpointer action, gpointer data)
972 gint row;
973 GtkTreeIter top, sel;
974 GtkTreeModel *model;
976 row = gtkut_list_view_get_selected_row(templates.list_view);
977 if (row <= 1)
978 return;
980 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
982 if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row - 1)
983 || !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
984 return;
986 gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
987 gtkut_list_view_select_row(templates.list_view, row - 1);
988 modified_list = TRUE;
991 static void prefs_template_down_cb(gpointer action, gpointer data)
993 gint row, n_rows;
994 GtkTreeIter top, sel;
995 GtkTreeModel *model;
997 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
998 n_rows = gtk_tree_model_iter_n_children(model, NULL);
999 row = gtkut_list_view_get_selected_row(templates.list_view);
1000 if (row < 1 || row >= n_rows - 1)
1001 return;
1003 if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
1004 || !gtk_tree_model_iter_nth_child(model, &sel, NULL, row + 1))
1005 return;
1007 gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
1008 gtkut_list_view_select_row(templates.list_view, row + 1);
1009 modified_list = TRUE;
1012 static void prefs_template_bottom_cb(gpointer action, gpointer data)
1014 gint row, n_rows;
1015 GtkTreeIter top, sel;
1016 GtkTreeModel *model;
1018 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
1019 n_rows = gtk_tree_model_iter_n_children(model, NULL);
1020 row = gtkut_list_view_get_selected_row(templates.list_view);
1021 if (row < 1 || row >= n_rows - 1)
1022 return;
1024 if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
1025 || !gtk_tree_model_iter_nth_child(model, &sel, NULL, n_rows - 1))
1026 return;
1028 gtk_list_store_move_after(GTK_LIST_STORE(model), &top, &sel);
1029 gtkut_list_view_select_row(templates.list_view, n_rows - 1);
1030 modified_list = TRUE;
1033 static GtkListStore* prefs_template_create_data_store(void)
1035 return gtk_list_store_new(N_TEMPL_COLUMNS,
1036 G_TYPE_STRING,
1037 G_TYPE_POINTER,
1038 G_TYPE_AUTO_POINTER,
1039 -1);
1042 static void prefs_template_list_view_insert_template(GtkWidget *list_view,
1043 gint row,
1044 const gchar *template,
1045 Template *data)
1047 GtkTreeIter iter;
1048 GtkTreeIter sibling;
1049 GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
1050 (GTK_TREE_VIEW(list_view)));
1052 /* row -1 to add a new rule to store,
1053 row >=0 to change an existing row
1054 row <-1 insert a new row after (-row-2)
1056 if (row >= 0 ) {
1057 /* modify the existing */
1058 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
1059 &iter, NULL, row))
1060 row = -1;
1061 } else if (row < -1 ) {
1062 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
1063 &sibling, NULL, -row-2))
1064 row = -1;
1067 if (row == -1 ) {
1068 /* append new */
1069 gtk_list_store_append(list_store, &iter);
1070 gtk_list_store_set(list_store, &iter,
1071 TEMPL_TEXT, template,
1072 TEMPL_DATA, data,
1073 -1);
1074 } else if (row < -1) {
1075 /* duplicate */
1076 gtk_list_store_insert_after(list_store, &iter, &sibling);
1077 gtk_list_store_set(list_store, &iter,
1078 TEMPL_TEXT, template,
1079 TEMPL_DATA, data,
1080 -1);
1081 } else {
1082 /* change existing */
1083 GAuto *auto_data =
1084 g_auto_pointer_new_with_free(data, (GFreeFunc) template_free);
1086 /* if replacing data in an existing row, the auto pointer takes care
1087 * of destroying the Template data */
1088 gtk_list_store_set(list_store, &iter,
1089 TEMPL_TEXT, template,
1090 TEMPL_DATA, data,
1091 TEMPL_AUTO_DATA, auto_data,
1092 -1);
1094 g_auto_pointer_free(auto_data);
1098 static GtkActionGroup *prefs_template_popup_action = NULL;
1099 static GtkWidget *prefs_template_popup_menu = NULL;
1101 static GtkActionEntry prefs_template_popup_entries[] =
1103 {"PrefsTemplatePopup", NULL, "PrefsTemplatePopup", NULL, NULL, NULL },
1104 {"PrefsTemplatePopup/Delete", NULL, N_("_Delete"), NULL, NULL, G_CALLBACK(prefs_template_delete_cb) },
1105 {"PrefsTemplatePopup/DeleteAll", NULL, N_("Delete _all"), NULL, NULL, G_CALLBACK(prefs_template_delete_all_cb) },
1106 {"PrefsTemplatePopup/Duplicate", NULL, N_("D_uplicate"), NULL, NULL, G_CALLBACK(prefs_template_duplicate_cb) },
1109 static void prefs_template_row_selected(GtkTreeSelection *selection,
1110 GtkTreeView *list_view)
1112 GtkTreePath *path;
1113 GtkTreeIter iter;
1114 GtkTreeModel *model;
1116 if (!gtk_tree_selection_get_selected(selection, &model, &iter))
1117 return;
1119 path = gtk_tree_model_get_path(model, &iter);
1120 prefs_template_select_row(list_view, path);
1121 gtk_tree_path_free(path);
1124 static gint prefs_template_list_btn_pressed(GtkWidget *widget, GdkEventButton *event,
1125 GtkTreeView *list_view)
1127 if (event) {
1128 /* left- or right-button click */
1129 if (event->button == 1 || event->button == 3) {
1130 GtkTreePath *path = NULL;
1131 if (gtk_tree_view_get_path_at_pos( list_view, event->x, event->y,
1132 &path, NULL, NULL, NULL)) {
1133 prefs_template_select_row(list_view, path);
1135 if (path)
1136 gtk_tree_path_free(path);
1139 /* right-button click */
1140 if (event->button == 3) {
1141 GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1142 GtkTreeIter iter;
1143 gboolean non_empty;
1144 gint row;
1146 if (!prefs_template_popup_menu) {
1147 prefs_template_popup_action = cm_menu_create_action_group("PrefsTemplatePopup", prefs_template_popup_entries,
1148 G_N_ELEMENTS(prefs_template_popup_entries), (gpointer)list_view);
1149 MENUITEM_ADDUI("/Menus", "PrefsTemplatePopup", "PrefsTemplatePopup", GTK_UI_MANAGER_MENU)
1150 MENUITEM_ADDUI("/Menus/PrefsTemplatePopup", "Delete", "PrefsTemplatePopup/Delete", GTK_UI_MANAGER_MENUITEM)
1151 MENUITEM_ADDUI("/Menus/PrefsTemplatePopup", "DeleteAll", "PrefsTemplatePopup/DeleteAll", GTK_UI_MANAGER_MENUITEM)
1152 MENUITEM_ADDUI("/Menus/PrefsTemplatePopup", "Duplicate", "PrefsTemplatePopup/Duplicate", GTK_UI_MANAGER_MENUITEM)
1153 prefs_template_popup_menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
1154 gtk_ui_manager_get_widget(gtkut_ui_manager(), "/Menus/PrefsTemplatePopup")) );
1157 /* grey out some popup menu items if there is no selected row */
1158 row = gtkut_list_view_get_selected_row(GTK_WIDGET(list_view));
1159 cm_menu_set_sensitive("PrefsTemplatePopup/Delete", (row > 0));
1160 cm_menu_set_sensitive("PrefsTemplatePopup/Duplicate", (row > 0));
1162 /* grey out seom popup menu items if there is no row
1163 (not counting the (New) one at row 0) */
1164 non_empty = gtk_tree_model_get_iter_first(model, &iter);
1165 if (non_empty)
1166 non_empty = gtk_tree_model_iter_next(model, &iter);
1167 cm_menu_set_sensitive("PrefsTemplatePopup/DeleteAll", non_empty);
1169 gtk_menu_popup_at_pointer(GTK_MENU(prefs_template_popup_menu), NULL);
1172 return FALSE;
1175 static gboolean prefs_template_list_popup_menu(GtkWidget *widget, gpointer data)
1177 GtkTreeView *list_view = (GtkTreeView *)data;
1178 GdkEventButton event;
1180 event.button = 3;
1181 event.time = gtk_get_current_event_time();
1183 prefs_template_list_btn_pressed(NULL, &event, list_view);
1185 return TRUE;
1188 static GtkWidget *prefs_template_list_view_create(void)
1190 GtkTreeView *list_view;
1191 GtkTreeSelection *selector;
1192 GtkTreeModel *model;
1194 model = GTK_TREE_MODEL(prefs_template_create_data_store());
1195 list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
1196 g_object_unref(model);
1198 g_signal_connect(G_OBJECT(list_view), "popup-menu",
1199 G_CALLBACK(prefs_template_list_popup_menu), list_view);
1200 g_signal_connect(G_OBJECT(list_view), "button-press-event",
1201 G_CALLBACK(prefs_template_list_btn_pressed), list_view);
1203 gtk_tree_view_set_reorderable(list_view, TRUE);
1205 selector = gtk_tree_view_get_selection(list_view);
1206 gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1207 g_signal_connect(G_OBJECT(selector), "changed",
1208 G_CALLBACK(prefs_template_row_selected), list_view);
1210 /* create the columns */
1211 prefs_template_create_list_view_columns(GTK_WIDGET(list_view));
1213 return GTK_WIDGET(list_view);
1216 static void prefs_template_create_list_view_columns(GtkWidget *list_view)
1218 GtkTreeViewColumn *column;
1219 GtkCellRenderer *renderer;
1221 renderer = gtk_cell_renderer_text_new();
1222 column = gtk_tree_view_column_new_with_attributes
1223 (_("Current templates"),
1224 renderer,
1225 "text", TEMPL_TEXT,
1226 NULL);
1227 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1228 gtk_tree_view_set_search_equal_func(GTK_TREE_VIEW(list_view), prefs_template_search_func_cb , NULL, NULL);
1232 *\brief Triggered when a row has to be selected
1234 static void prefs_template_select_row(GtkTreeView *list_view, GtkTreePath *path)
1236 GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1237 GtkTreeSelection *selection;
1238 Template *tmpl;
1239 Template tmpl_def;
1240 GtkTextBuffer *buffer;
1241 GtkTextIter iter;
1242 GtkTreeIter titer;
1244 if (!model || !path || !gtk_tree_model_get_iter(model, &titer, path))
1245 return;
1247 /* select row */
1248 selection = gtk_tree_view_get_selection(list_view);
1249 gtk_tree_selection_select_path(selection, path);
1251 tmpl_def.name = _("Template");
1252 tmpl_def.subject = "";
1253 tmpl_def.from = "";
1254 tmpl_def.to = "";
1255 tmpl_def.cc = "";
1256 tmpl_def.bcc = "";
1257 tmpl_def.replyto = "";
1258 tmpl_def.value = "";
1260 gtk_tree_model_get(model, &titer, TEMPL_DATA, &tmpl, -1);
1261 if (!tmpl)
1262 tmpl = &tmpl_def;
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_replyto),
1274 tmpl->replyto ? tmpl->replyto : "");
1275 gtk_entry_set_text(GTK_ENTRY(templates.entry_subject),
1276 tmpl->subject ? tmpl->subject : "");
1278 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
1279 gtk_text_buffer_set_text(buffer, "", -1);
1280 gtk_text_buffer_get_start_iter(buffer, &iter);
1281 gtk_text_buffer_insert(buffer, &iter, tmpl->value, -1);