image viewer: don't make the 'load image' button larger than necessary
[claws.git] / src / prefs_template.c
blob1b5abf59236697dfef1ef1ba92e73b54d6543dd9
1 /*
2 * Claws Mail templates subsystem
3 * Copyright (C) 2001 Alexander Barinov
4 * Copyright (C) 2001-2022 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 <dirent.h>
29 #include <sys/stat.h>
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 "menu.h"
45 #include "account.h"
47 enum {
48 TEMPL_TEXT,
49 TEMPL_DATA,
50 TEMPL_AUTO_DATA, /*!< auto pointer */
51 N_TEMPL_COLUMNS
54 static struct Templates {
55 GtkWidget *window;
56 GtkWidget *ok_btn;
57 GtkWidget *list_view;
58 GtkWidget *entry_name;
59 GtkWidget *entry_subject;
60 GtkWidget *entry_from;
61 GtkWidget *entry_to;
62 GtkWidget *entry_cc;
63 GtkWidget *entry_bcc;
64 GtkWidget *entry_replyto;
65 GtkWidget *text_value;
66 } templates;
68 static int modified = FALSE;
69 static int modified_list = FALSE;
71 static struct
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 {"Reply-To", &templates.entry_replyto, TRUE, NULL},
86 {"Subject", &templates.entry_subject, FALSE, NULL},
87 {NULL, NULL, FALSE, NULL}
91 /* widget creating functions */
92 static void prefs_template_window_create (void);
93 static void prefs_template_window_setup (void);
95 static GSList *prefs_template_get_list (void);
97 /* callbacks */
98 static gint prefs_template_deleted_cb (GtkWidget *widget,
99 GdkEventAny *event,
100 gpointer data);
101 static gboolean prefs_template_key_pressed_cb (GtkWidget *widget,
102 GdkEventKey *event,
103 gpointer data);
104 static gboolean prefs_template_search_func_cb (GtkTreeModel *model, gint column,
105 const gchar *key, GtkTreeIter *iter,
106 gpointer search_data);
108 static void prefs_template_cancel_cb (gpointer action, gpointer data);
109 static void prefs_template_ok_cb (gpointer action, gpointer data);
110 static void prefs_template_register_cb (gpointer action, gpointer data);
111 static void prefs_template_substitute_cb (gpointer action, gpointer data);
112 static void prefs_template_delete_cb (gpointer action, gpointer data);
113 static void prefs_template_delete_all_cb (gpointer action, gpointer data);
114 static void prefs_template_clear_cb (gpointer action, gpointer data);
115 static void prefs_template_duplicate_cb (gpointer action, gpointer data);
116 static void prefs_template_top_cb (gpointer action, gpointer data);
117 static void prefs_template_up_cb (gpointer action, gpointer data);
118 static void prefs_template_down_cb (gpointer action, gpointer data);
119 static void prefs_template_bottom_cb (gpointer action, gpointer data);
121 static GtkListStore* prefs_template_create_data_store (void);
122 static void prefs_template_list_view_insert_template (GtkWidget *list_view,
123 gint row,
124 const gchar *template,
125 Template *data);
126 static GtkWidget *prefs_template_list_view_create (void);
127 static void prefs_template_create_list_view_columns (GtkWidget *list_view);
128 static void prefs_template_select_row(GtkTreeView *list_view, GtkTreePath *path);
130 /* Called from mainwindow.c */
131 void prefs_template_open(void)
133 inc_lock();
135 prefs_template_window_create();
137 prefs_template_window_setup();
138 gtk_widget_show(templates.window);
139 gtk_window_set_modal(GTK_WINDOW(templates.window), TRUE);
143 *\brief Save Gtk object size to prefs dataset
145 static void prefs_template_size_allocate_cb(GtkWidget *widget,
146 GtkAllocation *allocation)
148 cm_return_if_fail(allocation != NULL);
150 gtk_window_get_size(GTK_WINDOW(widget),
151 &prefs_common.templateswin_width, &prefs_common.templateswin_height);
154 static void prefs_template_window_create(void)
156 /* window structure ;) */
157 GtkWidget *window;
158 GtkWidget *vbox;
159 GtkWidget *scrolled_window;
160 GtkWidget *vpaned;
161 GtkWidget *vbox1;
162 GtkWidget *table; /* including : entry_[name|from|to|cc|bcc|replyto|subject] */
163 GtkWidget *scroll2;
164 GtkWidget *text_value;
165 GtkWidget *vbox2;
166 GtkWidget *hbox2;
167 GtkWidget *arrow1;
168 GtkWidget *hbox3;
169 GtkWidget *reg_btn;
170 GtkWidget *subst_btn;
171 GtkWidget *del_btn;
172 GtkWidget *clear_btn;
173 GtkWidget *desc_btn;
174 GtkWidget *hbox4;
175 GtkWidget *scroll1;
176 GtkWidget *list_view;
177 GtkWidget *vbox3;
178 GtkWidget *spc_vbox;
179 GtkWidget *top_btn;
180 GtkWidget *up_btn;
181 GtkWidget *down_btn;
182 GtkWidget *bottom_btn;
183 GtkWidget *confirm_area;
184 GtkWidget *help_btn;
185 GtkWidget *cancel_btn;
186 GtkWidget *ok_btn;
187 static GdkGeometry geometry;
188 gint i;
190 debug_print("Creating templates configuration window...\n");
192 /* main window */
193 window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "prefs_template");
194 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
195 gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
196 gtk_window_set_type_hint(GTK_WINDOW(window), GDK_WINDOW_TYPE_HINT_DIALOG);
198 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 8);
199 gtk_widget_show(vbox);
200 gtk_container_add(GTK_CONTAINER(window), vbox);
201 gtk_container_set_border_width(GTK_CONTAINER(vbox), 4);
203 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
204 gtk_widget_show(scrolled_window);
205 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
206 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
207 gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 0);
209 /* vpaned to separate template settings from templates list */
210 vpaned = gtk_paned_new(GTK_ORIENTATION_VERTICAL);
211 gtk_widget_show(vpaned);
212 gtk_container_add(GTK_CONTAINER(scrolled_window), vpaned);
213 gtk_viewport_set_shadow_type (GTK_VIEWPORT(
214 gtk_bin_get_child(GTK_BIN(scrolled_window))), GTK_SHADOW_NONE);
216 /* vbox to handle template name and content */
217 vbox1 = gtk_box_new(GTK_ORIENTATION_VERTICAL, 6);
218 gtk_widget_show(vbox1);
219 gtk_container_set_border_width(GTK_CONTAINER(vbox1), 8);
220 gtk_paned_pack1(GTK_PANED(vpaned), vbox1, FALSE, FALSE);
222 table = gtk_grid_new();
223 gtk_widget_show(table);
224 gtk_grid_set_row_spacing(GTK_GRID(table), VSPACING_NARROW_2);
225 gtk_grid_set_column_spacing(GTK_GRID(table), 4);
227 gtk_box_pack_start (GTK_BOX (vbox1), table, FALSE, FALSE, 0);
229 for (i=0; widgets_table[i].label; i++) {
231 GtkWidget *label;
233 label = gtk_label_new( (i != 0) ?
234 prefs_common_translated_header_name(widgets_table[i].label) :
235 _(widgets_table[i].label));
236 gtk_widget_show(label);
237 gtk_label_set_xalign(GTK_LABEL(label), 1.0);
238 gtk_grid_attach(GTK_GRID(table), label, 0, i, 1, 1);
240 *(widgets_table[i].entry) = gtk_entry_new();
241 gtk_widget_show(*(widgets_table[i].entry));
242 gtk_grid_attach(GTK_GRID(table), *(widgets_table[i].entry), 1, i, 1, 1);
243 gtk_widget_set_hexpand(*(widgets_table[i].entry), TRUE);
244 gtk_widget_set_halign(*(widgets_table[i].entry), GTK_ALIGN_FILL);
245 CLAWS_SET_TIP(*(widgets_table[i].entry),
246 _(widgets_table[i].tooltips));
249 /* template content */
250 scroll2 = gtk_scrolled_window_new(NULL, NULL);
251 gtk_widget_show(scroll2);
252 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll2),
253 GTK_POLICY_AUTOMATIC,
254 GTK_POLICY_AUTOMATIC);
255 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll2),
256 GTK_SHADOW_IN);
257 gtk_box_pack_start(GTK_BOX(vbox1), scroll2, TRUE, TRUE, 0);
259 text_value = gtk_text_view_new();
260 if (prefs_common.textfont) {
261 PangoFontDescription *font_desc;
263 font_desc = pango_font_description_from_string
264 (prefs_common.textfont);
265 if (font_desc) {
266 gtk_widget_override_font(text_value, font_desc);
267 pango_font_description_free(font_desc);
270 gtk_widget_show(text_value);
271 #ifndef GENERIC_UMPC
272 gtk_scrolled_window_set_min_content_height(GTK_SCROLLED_WINDOW(scroll2), 120);
273 #else
274 gtk_scrolled_window_set_min_content_height(GTK_SCROLLED_WINDOW(scroll2), 60);
275 #endif
276 gtk_container_add(GTK_CONTAINER(scroll2), text_value);
277 gtk_text_view_set_editable(GTK_TEXT_VIEW(text_value), TRUE);
278 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text_value), GTK_WRAP_WORD);
280 /* vbox for buttons and templates list */
281 vbox2 = gtk_box_new(GTK_ORIENTATION_VERTICAL, 6);
282 gtk_widget_show(vbox2);
283 gtk_container_set_border_width(GTK_CONTAINER(vbox2), 8);
284 gtk_paned_pack2(GTK_PANED(vpaned), vbox2, TRUE, FALSE);
286 /* register | substitute | delete */
287 hbox2 = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 4);
288 gtk_widget_show(hbox2);
289 gtk_box_pack_start(GTK_BOX(vbox2), hbox2, FALSE, FALSE, 0);
291 arrow1 = gtk_image_new_from_icon_name("pan-down-symbolic", GTK_ICON_SIZE_MENU);
292 gtk_widget_show(arrow1);
293 gtk_box_pack_start(GTK_BOX(hbox2), arrow1, FALSE, FALSE, 0);
295 hbox3 = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 4);
296 gtk_widget_show(hbox3);
297 gtk_box_pack_start(GTK_BOX(hbox2), hbox3, FALSE, FALSE, 0);
299 reg_btn = gtkut_stock_button("list-add", _("_Add"));
300 gtk_widget_show(reg_btn);
301 gtk_box_pack_start(GTK_BOX(hbox3), reg_btn, FALSE, TRUE, 0);
302 g_signal_connect(G_OBJECT (reg_btn), "clicked",
303 G_CALLBACK (prefs_template_register_cb), NULL);
304 CLAWS_SET_TIP(reg_btn,
305 _("Append the new template above to the list"));
307 subst_btn = gtkut_stock_button("edit-redo", _("_Replace"));
308 gtk_widget_show(subst_btn);
309 gtk_box_pack_start(GTK_BOX(hbox3), subst_btn, FALSE, TRUE, 0);
310 g_signal_connect(G_OBJECT(subst_btn), "clicked",
311 G_CALLBACK(prefs_template_substitute_cb),
312 NULL);
313 CLAWS_SET_TIP(subst_btn,
314 _("Replace the selected template in list with the template above"));
316 del_btn = gtkut_stock_button("edit-delete", _("D_elete"));
317 gtk_widget_show(del_btn);
318 gtk_box_pack_start(GTK_BOX(hbox3), del_btn, FALSE, TRUE, 0);
319 g_signal_connect(G_OBJECT(del_btn), "clicked",
320 G_CALLBACK(prefs_template_delete_cb), NULL);
321 CLAWS_SET_TIP(del_btn,
322 _("Delete the selected template from the list"));
324 clear_btn = gtkut_stock_button("edit-clear", _("C_lear"));
325 gtk_widget_show (clear_btn);
326 gtk_box_pack_start (GTK_BOX (hbox3), clear_btn, FALSE, TRUE, 0);
327 g_signal_connect(G_OBJECT (clear_btn), "clicked",
328 G_CALLBACK(prefs_template_clear_cb), NULL);
329 CLAWS_SET_TIP(clear_btn,
330 _("Clear all the input fields in the dialog"));
332 desc_btn = gtkut_stock_button("dialog-information", _("_Information"));
333 gtk_widget_show(desc_btn);
334 gtk_box_pack_end(GTK_BOX(hbox2), desc_btn, FALSE, FALSE, 0);
335 g_signal_connect(G_OBJECT(desc_btn), "clicked",
336 G_CALLBACK(quote_fmt_quote_description), window);
337 CLAWS_SET_TIP(desc_btn,
338 _("Show information on configuring templates"));
340 /* templates list */
341 hbox4 = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
342 gtk_widget_show(hbox4);
343 gtk_box_pack_start(GTK_BOX(vbox2), hbox4, TRUE, TRUE, 0);
345 scroll1 = gtk_scrolled_window_new(NULL, NULL);
346 gtk_widget_show(scroll1);
347 gtk_box_pack_start(GTK_BOX(hbox4), scroll1, TRUE, TRUE, 0);
348 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll1),
349 GTK_POLICY_AUTOMATIC,
350 GTK_POLICY_AUTOMATIC);
352 vbox3 = gtk_box_new(GTK_ORIENTATION_VERTICAL, 8);
353 gtk_widget_show(vbox3);
354 gtk_box_pack_start(GTK_BOX(hbox4), vbox3, FALSE, FALSE, 0);
356 top_btn = gtkut_stock_button("go-top", _("_Top"));
357 gtk_widget_show(top_btn);
358 gtk_box_pack_start(GTK_BOX(vbox3), top_btn, FALSE, FALSE, 0);
359 g_signal_connect(G_OBJECT(top_btn), "clicked",
360 G_CALLBACK(prefs_template_top_cb), NULL);
361 CLAWS_SET_TIP(top_btn,
362 _("Move the selected template to the top"));
364 PACK_SPACER(vbox3, spc_vbox, VSPACING_NARROW_2);
366 up_btn = gtkut_stock_button("go-up", _("_Up"));
367 gtk_widget_show(up_btn);
368 gtk_box_pack_start (GTK_BOX(vbox3), up_btn, FALSE, FALSE, 0);
369 g_signal_connect(G_OBJECT(up_btn), "clicked",
370 G_CALLBACK(prefs_template_up_cb), NULL);
371 CLAWS_SET_TIP(up_btn,
372 _("Move the selected template up"));
374 down_btn = gtkut_stock_button("go-down", _("_Down"));
375 gtk_widget_show (down_btn);
376 gtk_box_pack_start(GTK_BOX (vbox3), down_btn, FALSE, FALSE, 0);
377 g_signal_connect(G_OBJECT (down_btn), "clicked",
378 G_CALLBACK(prefs_template_down_cb), NULL);
379 CLAWS_SET_TIP(down_btn,
380 _("Move the selected template down"));
382 PACK_SPACER(vbox3, spc_vbox, VSPACING_NARROW_2);
384 bottom_btn = gtkut_stock_button("go-bottom", _("_Bottom"));
385 gtk_widget_show(bottom_btn);
386 gtk_box_pack_start(GTK_BOX(vbox3), bottom_btn, FALSE, FALSE, 0);
387 g_signal_connect(G_OBJECT(bottom_btn), "clicked",
388 G_CALLBACK(prefs_template_bottom_cb), NULL);
389 CLAWS_SET_TIP(bottom_btn,
390 _("Move the selected template to the bottom"));
392 list_view = prefs_template_list_view_create();
393 gtk_widget_show(list_view);
394 gtk_widget_set_size_request(scroll1, -1, 140);
395 gtk_container_add(GTK_CONTAINER(scroll1), list_view);
397 /* help | cancel | ok */
398 gtkut_stock_button_set_create_with_help(&confirm_area, &help_btn,
399 &cancel_btn, NULL, _("_Cancel"),
400 &ok_btn, NULL, _("_OK"),
401 NULL, NULL, NULL);
402 gtk_widget_show(confirm_area);
403 gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
404 gtk_widget_grab_default(ok_btn);
406 gtk_window_set_title(GTK_WINDOW(window), _("Template configuration"));
408 g_signal_connect(G_OBJECT(window), "delete_event",
409 G_CALLBACK(prefs_template_deleted_cb), NULL);
410 g_signal_connect(G_OBJECT(window), "size_allocate",
411 G_CALLBACK(prefs_template_size_allocate_cb), NULL);
412 g_signal_connect(G_OBJECT(window), "key_press_event",
413 G_CALLBACK(prefs_template_key_pressed_cb), NULL);
414 MANAGE_WINDOW_SIGNALS_CONNECT(window);
415 g_signal_connect(G_OBJECT(ok_btn), "clicked",
416 G_CALLBACK(prefs_template_ok_cb), NULL);
417 g_signal_connect(G_OBJECT(cancel_btn), "clicked",
418 G_CALLBACK(prefs_template_cancel_cb), NULL);
419 g_signal_connect(G_OBJECT(help_btn), "clicked",
420 G_CALLBACK(manual_open_with_anchor_cb),
421 MANUAL_ANCHOR_TEMPLATES);
423 if (!geometry.min_height) {
424 geometry.min_width = 500;
425 geometry.min_height = 540;
428 gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
429 GDK_HINT_MIN_SIZE);
430 gtk_window_set_default_size(GTK_WINDOW(window),
431 prefs_common.templateswin_width,
432 prefs_common.templateswin_height);
434 templates.window = window;
435 templates.ok_btn = ok_btn;
436 templates.list_view = list_view;
437 templates.text_value = text_value;
440 static void prefs_template_reset_dialog(void)
442 GtkTextBuffer *buffer;
444 gtk_entry_set_text(GTK_ENTRY(templates.entry_name), "");
445 gtk_entry_set_text(GTK_ENTRY(templates.entry_from), "");
446 gtk_entry_set_text(GTK_ENTRY(templates.entry_to), "");
447 gtk_entry_set_text(GTK_ENTRY(templates.entry_cc), "");
448 gtk_entry_set_text(GTK_ENTRY(templates.entry_bcc), "");
449 gtk_entry_set_text(GTK_ENTRY(templates.entry_replyto), "");
450 gtk_entry_set_text(GTK_ENTRY(templates.entry_subject), "");
452 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
453 gtk_text_buffer_set_text(buffer, "", -1);
456 static void prefs_template_clear_list(void)
458 GtkListStore *store;
460 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
461 (templates.list_view)));
462 gtk_list_store_clear(store);
464 prefs_template_list_view_insert_template(templates.list_view,
465 -1, _("(New)"),
466 NULL);
469 static void prefs_template_window_setup(void)
471 GSList *tmpl_list;
472 GSList *cur;
473 Template *tmpl;
474 int i;
476 manage_window_set_transient(GTK_WINDOW(templates.window));
477 gtk_widget_grab_focus(templates.ok_btn);
479 prefs_template_clear_list();
481 tmpl_list = template_read_config();
483 address_completion_start(templates.window);
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);
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);
498 prefs_template_reset_dialog();
500 g_slist_free(tmpl_list);
503 static gint prefs_template_deleted_cb(GtkWidget *widget, GdkEventAny *event,
504 gpointer data)
506 prefs_template_cancel_cb(NULL, NULL);
507 return TRUE;
510 static gboolean prefs_template_key_pressed_cb(GtkWidget *widget,
511 GdkEventKey *event, gpointer data)
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;
522 return FALSE;
525 static gboolean prefs_template_search_func_cb (GtkTreeModel *model, gint column, const gchar *key,
526 GtkTreeIter *iter, gpointer search_data)
528 gchar *store_string;
529 gint key_len;
530 gboolean retval;
531 GtkTreePath *path;
533 gtk_tree_model_get (model, iter, column, &store_string, -1);
535 if (!store_string || !key) return FALSE;
537 key_len = strlen (key);
538 retval = (strncmp (key, store_string, key_len) != 0);
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);
546 return retval;
548 static void prefs_template_address_completion_end(void)
550 gint i;
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)));
557 address_completion_end(templates.window);
560 static void prefs_template_ok_cb(gpointer action, gpointer data)
562 GSList *tmpl_list;
563 GtkListStore *store;
565 if (modified && alertpanel(_("Entry not saved"),
566 _("The entry was not saved. Close anyway?"),
567 "window-close", _("_Close"), NULL, _("_Continue editing"),
568 NULL, NULL, ALERTFOCUS_SECOND) != G_ALERTDEFAULT) {
569 return;
572 prefs_template_address_completion_end();
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();
586 static void prefs_template_cancel_cb(gpointer action, gpointer data)
588 GtkListStore *store;
590 if (modified && alertpanel(_("Entry not saved"),
591 _("The entry was not saved. Close anyway?"),
592 "window-close", _("_Close"), NULL, _("_Continue editing"),
593 NULL, NULL, ALERTFOCUS_SECOND) != G_ALERTDEFAULT) {
594 return;
595 } else if (modified_list && alertpanel(_("Templates list not saved"),
596 _("The templates list has been modified. Close anyway?"),
597 "window-close", _("_Close"), NULL, _("_Continue editing"),
598 NULL, NULL, ALERTFOCUS_SECOND) != G_ALERTDEFAULT) {
599 return;
602 prefs_template_address_completion_end();
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();
614 *\brief Request list for storage. New list is owned
615 * by template.c...
617 static GSList *prefs_template_get_list(void)
619 GSList *tmpl_list = NULL;
620 Template *tmpl;
621 GtkTreeModel *model;
622 GtkTreeIter iter;
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;
628 do {
629 gtk_tree_model_get(model, &iter,
630 TEMPL_DATA, &tmpl,
631 -1);
633 if (tmpl) {
634 Template *ntmpl;
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->replyto = tmpl->replyto && *(tmpl->replyto)
657 ? g_strdup(tmpl->replyto)
658 : NULL;
659 ntmpl->value = tmpl->value && *(tmpl->value)
660 ? g_strdup(tmpl->value)
661 : NULL;
662 tmpl_list = g_slist_append(tmpl_list, ntmpl);
665 } while (gtk_tree_model_iter_next(model, &iter));
667 return tmpl_list;
670 gboolean prefs_template_string_is_valid(gchar *string, gint *line, gboolean escaped_string, gboolean email)
672 gboolean result = TRUE;
673 if (string && *string != '\0') {
674 gchar *tmp = NULL;
675 gchar *parsed_buf;
676 MsgInfo dummyinfo;
677 PrefsAccount *account = account_get_default();
679 if (escaped_string) {
680 tmp = malloc(strlen(string)+1);
681 pref_get_unescaped_pref(tmp, string);
682 } else {
683 tmp = g_strdup(string);
685 memset(&dummyinfo, 0, sizeof(MsgInfo));
686 /* init dummy fields, so we can test the result of the parse */
687 dummyinfo.date="Sat, 30 May 2009 01:23:45 +0200";
688 dummyinfo.fromname="John Doe";
689 dummyinfo.from="John Doe <john@example.com>";
690 dummyinfo.to="John Doe <john@example.com>";
691 dummyinfo.cc="John Doe <john@example.com>";
692 dummyinfo.msgid="<1234john@example.com>";
693 dummyinfo.inreplyto="<1234john@example.com>";
694 dummyinfo.newsgroups="alt.test";
695 dummyinfo.subject="subject";
698 #ifdef USE_ENCHANT
699 quote_fmt_init(&dummyinfo, NULL, NULL, TRUE, account, FALSE, NULL);
700 #else
701 quote_fmt_init(&dummyinfo, NULL, NULL, TRUE, account, FALSE);
702 #endif
703 quote_fmt_scan_string(tmp);
704 quote_fmt_parse();
705 g_free(tmp);
706 parsed_buf = quote_fmt_get_buffer();
707 if (!parsed_buf) {
708 if (line)
709 *line = quote_fmt_get_line();
710 quote_fmtlex_destroy();
711 return FALSE;
713 quote_fmt_reset_vartable();
714 quote_fmtlex_destroy();
716 return result;
719 static gboolean prefs_template_list_view_set_row(gint row)
720 /* return TRUE if the row could be modified */
722 Template *tmpl;
723 gchar *name;
724 gchar *subject;
725 gchar *from;
726 gchar *to;
727 gchar *cc;
728 gchar *bcc;
729 gchar *replyto;
730 gchar *value;
731 GtkTextBuffer *buffer;
732 GtkTextIter start, end;
733 gint line;
735 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
736 gtk_text_buffer_get_start_iter(buffer, &start);
737 gtk_text_buffer_get_iter_at_offset(buffer, &end, -1);
738 value = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
740 if (value && *value == '\0') {
741 g_free(value);
742 value = NULL;
744 if (!prefs_template_string_is_valid(value, &line, TRUE, FALSE)) {
745 alertpanel_error(_("The body of the template has an error at line %d."), line);
746 g_free(value);
747 return FALSE;
750 name = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_name),
751 0, -1);
752 if (*name == '\0') {
753 alertpanel_error(_("The template's name is not set."));
754 g_free(value);
755 return FALSE;
757 from = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_from),
758 0, -1);
759 to = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_to),
760 0, -1);
761 cc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_cc),
762 0, -1);
763 bcc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_bcc),
764 0, -1);
765 replyto = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_replyto),
766 0, -1);
767 subject = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_subject),
768 0, -1);
770 if (from && *from == '\0') {
771 g_free(from);
772 from = NULL;
774 if (to && *to == '\0') {
775 g_free(to);
776 to = NULL;
778 if (cc && *cc == '\0') {
779 g_free(cc);
780 cc = NULL;
782 if (bcc && *bcc == '\0') {
783 g_free(bcc);
784 bcc = NULL;
786 if (replyto && *replyto == '\0') {
787 g_free(replyto);
788 replyto = NULL;
790 if (subject && *subject == '\0') {
791 g_free(subject);
792 subject = NULL;
795 if (!prefs_template_string_is_valid(from, NULL, TRUE, TRUE)) {
796 alertpanel_error(_("The \"From\" field of the template contains an invalid email address."));
797 g_free(from);
798 g_free(value);
799 return FALSE;
801 if (!prefs_template_string_is_valid(to, NULL, TRUE, TRUE)) {
802 alertpanel_error(_("The \"To\" field of the template contains an invalid email address."));
803 g_free(to);
804 g_free(value);
805 return FALSE;
807 if (!prefs_template_string_is_valid(cc, NULL, TRUE, TRUE)) {
808 alertpanel_error(_("The \"Cc\" field of the template contains an invalid email address."));
809 g_free(cc);
810 g_free(value);
811 return FALSE;
813 if (!prefs_template_string_is_valid(bcc, NULL, TRUE, TRUE)) {
814 alertpanel_error(_("The \"Bcc\" field of the template contains an invalid email address."));
815 g_free(bcc);
816 g_free(value);
817 return FALSE;
819 if (!prefs_template_string_is_valid(replyto, NULL, TRUE, TRUE)) {
820 alertpanel_error(_("The \"Reply-To\" field of the template contains an invalid email address."));
821 g_free(replyto);
822 g_free(value);
823 return FALSE;
825 if (!prefs_template_string_is_valid(subject, NULL, TRUE, FALSE)) {
826 alertpanel_error(_("The \"Subject\" field of the template is invalid."));
827 g_free(subject);
828 g_free(value);
829 return FALSE;
832 tmpl = g_new(Template, 1);
833 tmpl->load_filename = NULL;
834 tmpl->name = name;
835 tmpl->subject = subject;
836 tmpl->from = from;
837 tmpl->to = to;
838 tmpl->cc = cc;
839 tmpl->bcc = bcc;
840 tmpl->replyto = replyto;
841 tmpl->value = value;
843 prefs_template_list_view_insert_template(templates.list_view,
844 row, tmpl->name, tmpl);
846 return TRUE;
849 static void prefs_template_register_cb(gpointer action, gpointer data)
851 modified = !prefs_template_list_view_set_row(-1);
852 modified_list = TRUE;
855 static void prefs_template_substitute_cb(gpointer action, gpointer data)
857 Template *tmpl;
858 gint row;
859 GtkTreeIter iter;
860 GtkTreeModel *model;
862 row = gtkut_list_view_get_selected_row(templates.list_view);
863 if (row <= 0)
864 return;
866 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
867 if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
868 return;
870 gtk_tree_model_get(model, &iter, TEMPL_DATA, &tmpl, -1);
871 if (!tmpl)
872 return;
874 modified = !prefs_template_list_view_set_row(row);
875 modified_list = TRUE;
878 static void prefs_template_delete_cb(gpointer action, gpointer data)
880 Template *tmpl;
881 gint row;
882 GtkTreeIter iter;
883 GtkTreeModel *model;
885 row = gtkut_list_view_get_selected_row(templates.list_view);
886 if (row <= 0)
887 return;
889 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
890 if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
891 return;
893 gtk_tree_model_get(model, &iter, TEMPL_DATA, &tmpl, -1);
894 if (!tmpl)
895 return;
897 if (alertpanel(_("Delete template"),
898 _("Do you really want to delete this template?"),
899 NULL, _("_Cancel"), "edit-delete", _("D_elete"), NULL, NULL,
900 ALERTFOCUS_FIRST) != G_ALERTALTERNATE)
901 return;
903 gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
904 prefs_template_reset_dialog();
905 modified_list = TRUE;
908 static void prefs_template_delete_all_cb(gpointer action, gpointer data)
910 if (alertpanel(_("Delete all templates"),
911 _("Do you really want to delete all the templates?"),
912 NULL, _("_Cancel"), "edit-delete", _("D_elete"), NULL, NULL,
913 ALERTFOCUS_SECOND) == G_ALERTDEFAULT)
914 return;
916 prefs_template_clear_list();
917 modified = FALSE;
919 prefs_template_reset_dialog();
920 modified_list = TRUE;
923 static void prefs_template_duplicate_cb(gpointer action, gpointer data)
925 Template *tmpl;
926 gint row;
927 GtkTreeIter iter;
928 GtkTreeModel *model;
930 row = gtkut_list_view_get_selected_row(templates.list_view);
931 if (row <= 0)
932 return;
934 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
935 if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
936 return;
938 gtk_tree_model_get(model, &iter, TEMPL_DATA, &tmpl, -1);
939 if (!tmpl)
940 return;
942 modified_list = !prefs_template_list_view_set_row(-row-2);
945 static void prefs_template_clear_cb(gpointer action, gpointer data)
947 prefs_template_reset_dialog();
950 static void prefs_template_top_cb(gpointer action, gpointer data)
952 gint row;
953 GtkTreeIter top, sel;
954 GtkTreeModel *model;
956 row = gtkut_list_view_get_selected_row(templates.list_view);
957 if (row <= 1)
958 return;
960 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
962 if (!gtk_tree_model_iter_nth_child(model, &top, NULL, 0)
963 || !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
964 return;
966 gtk_list_store_move_after(GTK_LIST_STORE(model), &sel, &top);
967 gtkut_list_view_select_row(templates.list_view, 1);
968 modified_list = TRUE;
971 static void prefs_template_up_cb(gpointer action, gpointer data)
973 gint row;
974 GtkTreeIter top, sel;
975 GtkTreeModel *model;
977 row = gtkut_list_view_get_selected_row(templates.list_view);
978 if (row <= 1)
979 return;
981 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
983 if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row - 1)
984 || !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
985 return;
987 gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
988 gtkut_list_view_select_row(templates.list_view, row - 1);
989 modified_list = TRUE;
992 static void prefs_template_down_cb(gpointer action, gpointer data)
994 gint row, n_rows;
995 GtkTreeIter top, sel;
996 GtkTreeModel *model;
998 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
999 n_rows = gtk_tree_model_iter_n_children(model, NULL);
1000 row = gtkut_list_view_get_selected_row(templates.list_view);
1001 if (row < 1 || row >= n_rows - 1)
1002 return;
1004 if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
1005 || !gtk_tree_model_iter_nth_child(model, &sel, NULL, row + 1))
1006 return;
1008 gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
1009 gtkut_list_view_select_row(templates.list_view, row + 1);
1010 modified_list = TRUE;
1013 static void prefs_template_bottom_cb(gpointer action, gpointer data)
1015 gint row, n_rows;
1016 GtkTreeIter top, sel;
1017 GtkTreeModel *model;
1019 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
1020 n_rows = gtk_tree_model_iter_n_children(model, NULL);
1021 row = gtkut_list_view_get_selected_row(templates.list_view);
1022 if (row < 1 || row >= n_rows - 1)
1023 return;
1025 if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
1026 || !gtk_tree_model_iter_nth_child(model, &sel, NULL, n_rows - 1))
1027 return;
1029 gtk_list_store_move_after(GTK_LIST_STORE(model), &top, &sel);
1030 gtkut_list_view_select_row(templates.list_view, n_rows - 1);
1031 modified_list = TRUE;
1034 static GtkListStore* prefs_template_create_data_store(void)
1036 return gtk_list_store_new(N_TEMPL_COLUMNS,
1037 G_TYPE_STRING,
1038 G_TYPE_POINTER,
1039 G_TYPE_AUTO_POINTER,
1040 -1);
1043 static void prefs_template_list_view_insert_template(GtkWidget *list_view,
1044 gint row,
1045 const gchar *template,
1046 Template *data)
1048 GtkTreeIter iter;
1049 GtkTreeIter sibling;
1050 GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
1051 (GTK_TREE_VIEW(list_view)));
1053 /* row -1 to add a new rule to store,
1054 row >=0 to change an existing row
1055 row <-1 insert a new row after (-row-2)
1057 if (row >= 0 ) {
1058 /* modify the existing */
1059 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
1060 &iter, NULL, row))
1061 row = -1;
1062 } else if (row < -1 ) {
1063 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
1064 &sibling, NULL, -row-2))
1065 row = -1;
1068 if (row == -1 ) {
1069 /* append new */
1070 gtk_list_store_append(list_store, &iter);
1071 gtk_list_store_set(list_store, &iter,
1072 TEMPL_TEXT, template,
1073 TEMPL_DATA, data,
1074 -1);
1075 } else if (row < -1) {
1076 /* duplicate */
1077 gtk_list_store_insert_after(list_store, &iter, &sibling);
1078 gtk_list_store_set(list_store, &iter,
1079 TEMPL_TEXT, template,
1080 TEMPL_DATA, data,
1081 -1);
1082 } else {
1083 /* change existing */
1084 GAuto *auto_data =
1085 g_auto_pointer_new_with_free(data, (GFreeFunc) template_free);
1087 /* if replacing data in an existing row, the auto pointer takes care
1088 * of destroying the Template data */
1089 gtk_list_store_set(list_store, &iter,
1090 TEMPL_TEXT, template,
1091 TEMPL_DATA, data,
1092 TEMPL_AUTO_DATA, auto_data,
1093 -1);
1095 g_auto_pointer_free(auto_data);
1099 static GtkActionGroup *prefs_template_popup_action = NULL;
1100 static GtkWidget *prefs_template_popup_menu = NULL;
1102 static GtkActionEntry prefs_template_popup_entries[] =
1104 {"PrefsTemplatePopup", NULL, "PrefsTemplatePopup", NULL, NULL, NULL },
1105 {"PrefsTemplatePopup/Delete", NULL, N_("_Delete"), NULL, NULL, G_CALLBACK(prefs_template_delete_cb) },
1106 {"PrefsTemplatePopup/DeleteAll", NULL, N_("Delete _all"), NULL, NULL, G_CALLBACK(prefs_template_delete_all_cb) },
1107 {"PrefsTemplatePopup/Duplicate", NULL, N_("D_uplicate"), NULL, NULL, G_CALLBACK(prefs_template_duplicate_cb) },
1110 static void prefs_template_row_selected(GtkTreeSelection *selection,
1111 GtkTreeView *list_view)
1113 GtkTreePath *path;
1114 GtkTreeIter iter;
1115 GtkTreeModel *model;
1117 if (!gtk_tree_selection_get_selected(selection, &model, &iter))
1118 return;
1120 path = gtk_tree_model_get_path(model, &iter);
1121 prefs_template_select_row(list_view, path);
1122 gtk_tree_path_free(path);
1125 static gint prefs_template_list_btn_pressed(GtkWidget *widget, GdkEventButton *event,
1126 GtkTreeView *list_view)
1128 if (event) {
1129 /* left- or right-button click */
1130 if (event->button == 1 || event->button == 3) {
1131 GtkTreePath *path = NULL;
1132 if (gtk_tree_view_get_path_at_pos( list_view, event->x, event->y,
1133 &path, NULL, NULL, NULL)) {
1134 prefs_template_select_row(list_view, path);
1136 if (path)
1137 gtk_tree_path_free(path);
1140 /* right-button click */
1141 if (event->button == 3) {
1142 GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1143 GtkTreeIter iter;
1144 gboolean non_empty;
1145 gint row;
1147 if (!prefs_template_popup_menu) {
1148 prefs_template_popup_action = cm_menu_create_action_group("PrefsTemplatePopup", prefs_template_popup_entries,
1149 G_N_ELEMENTS(prefs_template_popup_entries), (gpointer)list_view);
1150 MENUITEM_ADDUI("/Menus", "PrefsTemplatePopup", "PrefsTemplatePopup", GTK_UI_MANAGER_MENU)
1151 MENUITEM_ADDUI("/Menus/PrefsTemplatePopup", "Delete", "PrefsTemplatePopup/Delete", GTK_UI_MANAGER_MENUITEM)
1152 MENUITEM_ADDUI("/Menus/PrefsTemplatePopup", "DeleteAll", "PrefsTemplatePopup/DeleteAll", GTK_UI_MANAGER_MENUITEM)
1153 MENUITEM_ADDUI("/Menus/PrefsTemplatePopup", "Duplicate", "PrefsTemplatePopup/Duplicate", GTK_UI_MANAGER_MENUITEM)
1154 prefs_template_popup_menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
1155 gtk_ui_manager_get_widget(gtkut_ui_manager(), "/Menus/PrefsTemplatePopup")) );
1158 /* grey out some popup menu items if there is no selected row */
1159 row = gtkut_list_view_get_selected_row(GTK_WIDGET(list_view));
1160 cm_menu_set_sensitive("PrefsTemplatePopup/Delete", (row > 0));
1161 cm_menu_set_sensitive("PrefsTemplatePopup/Duplicate", (row > 0));
1163 /* grey out seom popup menu items if there is no row
1164 (not counting the (New) one at row 0) */
1165 non_empty = gtk_tree_model_get_iter_first(model, &iter);
1166 if (non_empty)
1167 non_empty = gtk_tree_model_iter_next(model, &iter);
1168 cm_menu_set_sensitive("PrefsTemplatePopup/DeleteAll", non_empty);
1170 gtk_menu_popup_at_pointer(GTK_MENU(prefs_template_popup_menu), NULL);
1173 return FALSE;
1176 static gboolean prefs_template_list_popup_menu(GtkWidget *widget, gpointer data)
1178 GtkTreeView *list_view = (GtkTreeView *)data;
1179 GdkEventButton event;
1181 event.button = 3;
1182 event.time = gtk_get_current_event_time();
1184 prefs_template_list_btn_pressed(NULL, &event, list_view);
1186 return TRUE;
1189 static GtkWidget *prefs_template_list_view_create(void)
1191 GtkTreeView *list_view;
1192 GtkTreeSelection *selector;
1193 GtkTreeModel *model;
1195 model = GTK_TREE_MODEL(prefs_template_create_data_store());
1196 list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
1197 g_object_unref(model);
1199 g_signal_connect(G_OBJECT(list_view), "popup-menu",
1200 G_CALLBACK(prefs_template_list_popup_menu), list_view);
1201 g_signal_connect(G_OBJECT(list_view), "button-press-event",
1202 G_CALLBACK(prefs_template_list_btn_pressed), list_view);
1204 gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
1205 gtk_tree_view_set_reorderable(list_view, TRUE);
1207 selector = gtk_tree_view_get_selection(list_view);
1208 gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1209 g_signal_connect(G_OBJECT(selector), "changed",
1210 G_CALLBACK(prefs_template_row_selected), list_view);
1212 /* create the columns */
1213 prefs_template_create_list_view_columns(GTK_WIDGET(list_view));
1215 return GTK_WIDGET(list_view);
1218 static void prefs_template_create_list_view_columns(GtkWidget *list_view)
1220 GtkTreeViewColumn *column;
1221 GtkCellRenderer *renderer;
1223 renderer = gtk_cell_renderer_text_new();
1224 column = gtk_tree_view_column_new_with_attributes
1225 (_("Current templates"),
1226 renderer,
1227 "text", TEMPL_TEXT,
1228 NULL);
1229 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1230 gtk_tree_view_set_search_equal_func(GTK_TREE_VIEW(list_view), prefs_template_search_func_cb , NULL, NULL);
1234 *\brief Triggered when a row has to be selected
1236 static void prefs_template_select_row(GtkTreeView *list_view, GtkTreePath *path)
1238 GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1239 GtkTreeSelection *selection;
1240 Template *tmpl;
1241 Template tmpl_def;
1242 GtkTextBuffer *buffer;
1243 GtkTextIter iter;
1244 GtkTreeIter titer;
1246 if (!model || !path || !gtk_tree_model_get_iter(model, &titer, path))
1247 return;
1249 /* select row */
1250 selection = gtk_tree_view_get_selection(list_view);
1251 gtk_tree_selection_select_path(selection, path);
1253 tmpl_def.name = _("Template");
1254 tmpl_def.subject = "";
1255 tmpl_def.from = "";
1256 tmpl_def.to = "";
1257 tmpl_def.cc = "";
1258 tmpl_def.bcc = "";
1259 tmpl_def.replyto = "";
1260 tmpl_def.value = "";
1262 gtk_tree_model_get(model, &titer, TEMPL_DATA, &tmpl, -1);
1263 if (!tmpl)
1264 tmpl = &tmpl_def;
1266 gtk_entry_set_text(GTK_ENTRY(templates.entry_name), tmpl->name);
1267 gtk_entry_set_text(GTK_ENTRY(templates.entry_from),
1268 tmpl->from ? tmpl->from : "");
1269 gtk_entry_set_text(GTK_ENTRY(templates.entry_to),
1270 tmpl->to ? tmpl->to : "");
1271 gtk_entry_set_text(GTK_ENTRY(templates.entry_cc),
1272 tmpl->cc ? tmpl->cc : "");
1273 gtk_entry_set_text(GTK_ENTRY(templates.entry_bcc),
1274 tmpl->bcc ? tmpl->bcc : "");
1275 gtk_entry_set_text(GTK_ENTRY(templates.entry_replyto),
1276 tmpl->replyto ? tmpl->replyto : "");
1277 gtk_entry_set_text(GTK_ENTRY(templates.entry_subject),
1278 tmpl->subject ? tmpl->subject : "");
1280 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
1281 gtk_text_buffer_set_text(buffer, "", -1);
1282 gtk_text_buffer_get_start_iter(buffer, &iter);
1283 gtk_text_buffer_insert(buffer, &iter, tmpl->value, -1);