2 * Claws Mail templates subsystem
3 * Copyright (C) 2001 Alexander Barinov
4 * Copyright (C) 2001-2007 Hiroyuki Yamamoto and 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/>.
24 #include <glib/gi18n.h>
26 #include <gdk/gdkkeysyms.h>
33 #include "prefs_gtk.h"
37 #include "alertpanel.h"
38 #include "manage_window.h"
40 #include "addr_compl.h"
41 #include "quote_fmt.h"
42 #include "prefs_common.h"
50 TEMPL_AUTO_DATA, /*!< auto pointer */
54 static struct Templates {
58 GtkWidget *entry_name;
59 GtkWidget *entry_subject;
63 GtkWidget *text_value;
66 static int modified = FALSE;
67 static int modified_list = FALSE;
68 /* widget creating functions */
69 static void prefs_template_window_create (void);
70 static void prefs_template_window_setup (void);
72 static GSList *prefs_template_get_list (void);
75 static gint prefs_template_deleted_cb (GtkWidget *widget,
78 static gboolean prefs_template_key_pressed_cb (GtkWidget *widget,
81 static void prefs_template_cancel_cb (void);
82 static void prefs_template_ok_cb (void);
83 static void prefs_template_register_cb (void);
84 static void prefs_template_substitute_cb (void);
85 static void prefs_template_delete_cb (void);
86 static void prefs_template_delete_all_cb (void);
87 static void prefs_template_clear_cb (void);
88 static void prefs_template_duplicate_cb (void);
89 static void prefs_template_top_cb (void);
90 static void prefs_template_up_cb (void);
91 static void prefs_template_down_cb (void);
92 static void prefs_template_bottom_cb (void);
94 static GtkListStore* prefs_template_create_data_store (void);
95 static void prefs_template_list_view_insert_template (GtkWidget *list_view,
97 const gchar *template,
99 static GtkWidget *prefs_template_list_view_create (void);
100 static void prefs_template_create_list_view_columns (GtkWidget *list_view);
101 static void prefs_template_select_row(GtkTreeView *list_view, GtkTreePath *path);
103 /* Called from mainwindow.c */
104 void prefs_template_open(void)
108 if (!templates.window)
109 prefs_template_window_create();
111 prefs_template_window_setup();
112 gtk_widget_show(templates.window);
116 *\brief Save Gtk object size to prefs dataset
118 static void prefs_template_size_allocate_cb(GtkWidget *widget,
119 GtkAllocation *allocation)
121 g_return_if_fail(allocation != NULL);
123 prefs_common.templateswin_width = allocation->width;
124 prefs_common.templateswin_height = allocation->height;
127 static void prefs_template_window_create(void)
129 /* window structure ;) */
132 GtkWidget *scrolled_window;
135 GtkWidget *table; /* including : entry_[name|to|cc|bcc|subject] */
137 GtkWidget *text_value;
143 GtkWidget *subst_btn;
145 GtkWidget *clear_btn;
149 GtkWidget *list_view;
155 GtkWidget *bottom_btn;
156 GtkWidget *confirm_area;
158 GtkWidget *cancel_btn;
160 static GdkGeometry geometry;
162 GtkTooltips *tooltips;
164 debug_print("Creating templates configuration window...\n");
167 window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "prefs_template");
168 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
169 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
170 gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
172 vbox = gtk_vbox_new(FALSE, 8);
173 gtk_widget_show(vbox);
174 gtk_container_add(GTK_CONTAINER(window), vbox);
175 gtk_container_set_border_width(GTK_CONTAINER(vbox), 4);
177 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
178 gtk_widget_show(scrolled_window);
179 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
180 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
181 gtk_container_add(GTK_CONTAINER(vbox), scrolled_window);
183 /* vpaned to separate template settings from templates list */
184 vpaned = gtk_vpaned_new();
185 gtk_widget_show(vpaned);
186 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled_window),
189 /* vbox to handle template name and content */
190 vbox1 = gtk_vbox_new(FALSE, 6);
191 gtk_widget_show(vbox1);
192 gtk_container_set_border_width(GTK_CONTAINER(vbox1), 8);
193 gtk_paned_pack1(GTK_PANED(vpaned), vbox1, FALSE, FALSE);
195 table = gtk_table_new(5, 2, FALSE);
196 gtk_table_set_row_spacings (GTK_TABLE (table), VSPACING_NARROW_2);
197 gtk_table_set_col_spacings (GTK_TABLE (table), 4);
198 gtk_widget_show(table);
199 gtk_box_pack_start (GTK_BOX (vbox1), table, FALSE, FALSE, 0);
201 tooltips = gtk_tooltips_new();
210 {_("Name"), &templates.entry_name, FALSE,
211 _("This name is used as the Menu item")},
212 {_("To"), &templates.entry_to, TRUE, NULL},
213 {_("Cc"), &templates.entry_cc, TRUE, NULL},
214 {_("Bcc"), &templates.entry_bcc, TRUE, NULL},
215 {_("Subject"), &templates.entry_subject, FALSE, NULL},
216 {NULL, NULL, FALSE, NULL}
219 for (i=0; tab[i].label; i++) {
223 label = gtk_label_new(tab[i].label);
224 gtk_widget_show(label);
225 gtk_table_attach(GTK_TABLE(table), label, 0, 1, i, (i + 1),
226 (GtkAttachOptions) (GTK_FILL),
227 (GtkAttachOptions) 0, 0, 0);
228 gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
230 *(tab[i].entry) = gtk_entry_new();
231 gtk_widget_show(*(tab[i].entry));
232 gtk_table_attach(GTK_TABLE(table), *(tab[i].entry), 1, 2, i, (i + 1),
233 (GtkAttachOptions) (GTK_EXPAND|GTK_SHRINK|GTK_FILL),
234 (GtkAttachOptions) 0, 0, 0);
235 gtk_tooltips_set_tip(tooltips, *(tab[i].entry), tab[i].tooltips, NULL);
238 address_completion_register_entry(
239 GTK_ENTRY(*(tab[i].entry)), TRUE);
242 /* template content */
243 scroll2 = gtk_scrolled_window_new(NULL, NULL);
244 gtk_widget_show(scroll2);
245 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll2),
246 GTK_POLICY_AUTOMATIC,
247 GTK_POLICY_AUTOMATIC);
248 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll2),
250 gtk_box_pack_start(GTK_BOX(vbox1), scroll2, TRUE, TRUE, 0);
252 text_value = gtk_text_view_new();
253 if (prefs_common.textfont) {
254 PangoFontDescription *font_desc;
256 font_desc = pango_font_description_from_string
257 (prefs_common.textfont);
259 gtk_widget_modify_font(text_value, font_desc);
260 pango_font_description_free(font_desc);
263 gtk_widget_show(text_value);
265 gtk_widget_set_size_request(text_value, -1, 120);
267 gtk_widget_set_size_request(text_value, -1, 60);
269 gtk_container_add(GTK_CONTAINER(scroll2), text_value);
270 gtk_text_view_set_editable(GTK_TEXT_VIEW(text_value), TRUE);
271 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text_value), GTK_WRAP_WORD);
273 /* vbox for buttons and templates list */
274 vbox2 = gtk_vbox_new(FALSE, 6);
275 gtk_widget_show(vbox2);
276 gtk_container_set_border_width(GTK_CONTAINER(vbox2), 8);
277 gtk_paned_pack2(GTK_PANED(vpaned), vbox2, TRUE, FALSE);
279 /* register | substitute | delete */
280 hbox2 = gtk_hbox_new(FALSE, 4);
281 gtk_widget_show(hbox2);
282 gtk_box_pack_start(GTK_BOX(vbox2), hbox2, FALSE, FALSE, 0);
284 arrow1 = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
285 gtk_widget_show(arrow1);
286 gtk_box_pack_start(GTK_BOX(hbox2), arrow1, FALSE, FALSE, 0);
287 gtk_widget_set_size_request(arrow1, -1, 16);
289 hbox3 = gtk_hbox_new(TRUE, 4);
290 gtk_widget_show(hbox3);
291 gtk_box_pack_start(GTK_BOX(hbox2), hbox3, FALSE, FALSE, 0);
293 reg_btn = gtk_button_new_from_stock(GTK_STOCK_ADD);
294 gtk_widget_show(reg_btn);
295 gtk_box_pack_start(GTK_BOX(hbox3), reg_btn, FALSE, TRUE, 0);
296 g_signal_connect(G_OBJECT (reg_btn), "clicked",
297 G_CALLBACK (prefs_template_register_cb), NULL);
298 gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), reg_btn,
299 _("Append the new template above to the list"), NULL);
301 subst_btn = gtkut_get_replace_btn(_("Replace"));
302 gtk_widget_show(subst_btn);
303 gtk_box_pack_start(GTK_BOX(hbox3), subst_btn, FALSE, TRUE, 0);
304 g_signal_connect(G_OBJECT(subst_btn), "clicked",
305 G_CALLBACK(prefs_template_substitute_cb),
307 gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), subst_btn,
308 _("Replace the selected template in list with the template above"), NULL);
310 del_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
311 gtk_widget_show(del_btn);
312 gtk_box_pack_start(GTK_BOX(hbox3), del_btn, FALSE, TRUE, 0);
313 g_signal_connect(G_OBJECT(del_btn), "clicked",
314 G_CALLBACK(prefs_template_delete_cb), NULL);
315 gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), del_btn,
316 _("Delete the selected template from the list"), NULL);
318 clear_btn = gtk_button_new_from_stock (GTK_STOCK_CLEAR);
319 gtk_widget_show (clear_btn);
320 gtk_box_pack_start (GTK_BOX (hbox3), clear_btn, FALSE, TRUE, 0);
321 g_signal_connect(G_OBJECT (clear_btn), "clicked",
322 G_CALLBACK(prefs_template_clear_cb), NULL);
323 gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), clear_btn,
324 _("Clear all the input fields in the dialog"), NULL);
326 #if GTK_CHECK_VERSION(2, 8, 0)
327 desc_btn = gtk_button_new_from_stock(GTK_STOCK_INFO);
329 desc_btn = gtk_button_new_with_label(_(" Symbols... "));
331 gtk_widget_show(desc_btn);
332 gtk_box_pack_end(GTK_BOX(hbox2), desc_btn, FALSE, FALSE, 0);
333 g_signal_connect(G_OBJECT(desc_btn), "clicked",
334 G_CALLBACK(quote_fmt_quote_description), window);
335 gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), desc_btn,
336 _("Show information on configuring templates"), NULL);
339 hbox4 = gtk_hbox_new(FALSE, 8);
340 gtk_widget_show(hbox4);
341 gtk_box_pack_start(GTK_BOX(vbox2), hbox4, TRUE, TRUE, 0);
343 scroll1 = gtk_scrolled_window_new(NULL, NULL);
344 gtk_widget_show(scroll1);
345 gtk_box_pack_start(GTK_BOX(hbox4), scroll1, TRUE, TRUE, 0);
346 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll1),
347 GTK_POLICY_AUTOMATIC,
348 GTK_POLICY_AUTOMATIC);
350 vbox3 = gtk_vbox_new(FALSE, 8);
351 gtk_widget_show(vbox3);
352 gtk_box_pack_start(GTK_BOX(hbox4), vbox3, FALSE, FALSE, 0);
354 top_btn = gtk_button_new_from_stock(GTK_STOCK_GOTO_TOP);
355 gtk_widget_show(top_btn);
356 gtk_box_pack_start(GTK_BOX(vbox3), top_btn, FALSE, FALSE, 0);
357 g_signal_connect(G_OBJECT(top_btn), "clicked",
358 G_CALLBACK(prefs_template_top_cb), NULL);
359 gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), top_btn,
360 _("Move the selected template to the top"), NULL);
362 PACK_VSPACER(vbox3, spc_vbox, VSPACING_NARROW_2);
364 up_btn = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
365 gtk_widget_show(up_btn);
366 gtk_box_pack_start (GTK_BOX(vbox3), up_btn, FALSE, FALSE, 0);
367 g_signal_connect(G_OBJECT(up_btn), "clicked",
368 G_CALLBACK(prefs_template_up_cb), NULL);
369 gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), up_btn,
370 _("Move the selected template up"), NULL);
372 down_btn = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
373 gtk_widget_show (down_btn);
374 gtk_box_pack_start(GTK_BOX (vbox3), down_btn, FALSE, FALSE, 0);
375 g_signal_connect(G_OBJECT (down_btn), "clicked",
376 G_CALLBACK(prefs_template_down_cb), NULL);
377 gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), down_btn,
378 _("Move the selected template down"), NULL);
380 PACK_VSPACER(vbox3, spc_vbox, VSPACING_NARROW_2);
382 bottom_btn = gtk_button_new_from_stock(GTK_STOCK_GOTO_BOTTOM);
383 gtk_widget_show(bottom_btn);
384 gtk_box_pack_start(GTK_BOX(vbox3), bottom_btn, FALSE, FALSE, 0);
385 g_signal_connect(G_OBJECT(bottom_btn), "clicked",
386 G_CALLBACK(prefs_template_bottom_cb), NULL);
387 gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), bottom_btn,
388 _("Move the selected template to the bottom"), NULL);
390 list_view = prefs_template_list_view_create();
391 gtk_widget_show(list_view);
392 gtk_widget_set_size_request(scroll1, -1, 140);
393 gtk_container_add(GTK_CONTAINER(scroll1), list_view);
395 /* help | cancel | ok */
396 gtkut_stock_button_set_create_with_help(&confirm_area, &help_btn,
397 &cancel_btn, GTK_STOCK_CANCEL,
398 &ok_btn, GTK_STOCK_OK,
400 gtk_widget_show(confirm_area);
401 gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
402 gtk_widget_grab_default(ok_btn);
404 gtk_window_set_title(GTK_WINDOW(window), _("Template configuration"));
406 g_signal_connect(G_OBJECT(window), "delete_event",
407 G_CALLBACK(prefs_template_deleted_cb), NULL);
408 g_signal_connect(G_OBJECT(window), "size_allocate",
409 G_CALLBACK(prefs_template_size_allocate_cb), NULL);
410 g_signal_connect(G_OBJECT(window), "key_press_event",
411 G_CALLBACK(prefs_template_key_pressed_cb), NULL);
412 MANAGE_WINDOW_SIGNALS_CONNECT(window);
413 g_signal_connect(G_OBJECT(ok_btn), "clicked",
414 G_CALLBACK(prefs_template_ok_cb), NULL);
415 g_signal_connect(G_OBJECT(cancel_btn), "clicked",
416 G_CALLBACK(prefs_template_cancel_cb), NULL);
417 g_signal_connect(G_OBJECT(help_btn), "clicked",
418 G_CALLBACK(manual_open_with_anchor_cb),
419 MANUAL_ANCHOR_TEMPLATES);
421 address_completion_start(window);
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,
430 gtk_widget_set_size_request(window, 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_to), "");
445 gtk_entry_set_text(GTK_ENTRY(templates.entry_cc), "");
446 gtk_entry_set_text(GTK_ENTRY(templates.entry_bcc), "");
447 gtk_entry_set_text(GTK_ENTRY(templates.entry_subject), "");
449 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
450 gtk_text_buffer_set_text(buffer, "", -1);
453 static void prefs_template_clear_list(void)
457 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
458 (templates.list_view)));
459 gtk_list_store_clear(store);
461 prefs_template_list_view_insert_template(templates.list_view,
466 static void prefs_template_window_setup(void)
472 manage_window_set_transient(GTK_WINDOW(templates.window));
473 gtk_widget_grab_focus(templates.ok_btn);
475 prefs_template_clear_list();
477 tmpl_list = template_read_config();
479 for (cur = tmpl_list; cur != NULL; cur = cur->next) {
480 tmpl = (Template *)cur->data;
481 prefs_template_list_view_insert_template(templates.list_view,
486 prefs_template_reset_dialog();
488 g_slist_free(tmpl_list);
491 static gint prefs_template_deleted_cb(GtkWidget *widget, GdkEventAny *event,
494 prefs_template_cancel_cb();
498 static gboolean prefs_template_key_pressed_cb(GtkWidget *widget,
499 GdkEventKey *event, gpointer data)
501 if (event && event->keyval == GDK_Escape)
502 prefs_template_cancel_cb();
504 GtkWidget *focused = gtkut_get_focused_child(
505 GTK_CONTAINER(widget));
506 if (focused && GTK_IS_EDITABLE(focused)) {
513 static void prefs_template_ok_cb(void)
518 if (modified && alertpanel(_("Entry not saved"),
519 _("The entry was not saved. Close anyway?"),
520 GTK_STOCK_CLOSE, _("+_Continue editing"),
521 NULL) != G_ALERTDEFAULT) {
525 modified_list = FALSE;
526 tmpl_list = prefs_template_get_list();
527 template_set_config(tmpl_list);
528 compose_reflect_prefs_all();
529 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
530 (templates.list_view)));
531 gtk_list_store_clear(store);
532 gtk_widget_hide(templates.window);
536 static void prefs_template_cancel_cb(void)
540 if (modified && alertpanel(_("Entry not saved"),
541 _("The entry was not saved. Close anyway?"),
542 GTK_STOCK_CLOSE, _("+_Continue editing"),
543 NULL) != G_ALERTDEFAULT) {
545 } else if (modified_list && alertpanel(_("Templates list not saved"),
546 _("The templates list has been modified. Close anyway?"),
547 GTK_STOCK_CLOSE, _("+_Continue editing"),
548 NULL) != G_ALERTDEFAULT) {
552 modified_list = FALSE;
553 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
554 (templates.list_view)));
555 gtk_list_store_clear(store);
556 gtk_widget_hide(templates.window);
561 *\brief Request list for storage. New list is owned
564 static GSList *prefs_template_get_list(void)
566 GSList *tmpl_list = NULL;
571 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
572 if (!gtk_tree_model_get_iter_first(model, &iter))
576 gtk_tree_model_get(model, &iter,
583 ntmpl = g_new(Template, 1);
584 ntmpl->load_filename = NULL;
585 ntmpl->name = tmpl->name && *(tmpl->name)
586 ? g_strdup(tmpl->name)
588 ntmpl->subject = tmpl->subject && *(tmpl->subject)
589 ? g_strdup(tmpl->subject)
591 ntmpl->to = tmpl->to && *(tmpl->to)
594 ntmpl->cc = tmpl->cc && *(tmpl->cc)
597 ntmpl->bcc = tmpl->bcc && *(tmpl->bcc)
598 ? g_strdup(tmpl->bcc)
600 ntmpl->value = tmpl->value && *(tmpl->value)
601 ? g_strdup(tmpl->value)
603 tmpl_list = g_slist_append(tmpl_list, ntmpl);
606 } while (gtk_tree_model_iter_next(model, &iter));
611 gboolean prefs_template_string_is_valid(gchar *string, gint *line, gboolean escaped_string)
613 if (string && *string != '\0') {
617 memset(&dummyinfo, 0, sizeof(MsgInfo));
619 quote_fmt_init(&dummyinfo, NULL, NULL, TRUE, NULL, escaped_string, NULL);
621 quote_fmt_init(&dummyinfo, NULL, NULL, TRUE, NULL, escaped_string);
623 quote_fmt_scan_string(string);
625 parsed_buf = quote_fmt_get_buffer();
628 *line = quote_fmt_get_line();
631 quote_fmt_reset_vartable();
636 static gboolean prefs_template_list_view_set_row(gint row)
637 /* return TRUE if the row could be modified */
646 GtkTextBuffer *buffer;
647 GtkTextIter start, end;
651 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
653 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
654 gtk_text_buffer_get_start_iter(buffer, &start);
655 gtk_text_buffer_get_iter_at_offset(buffer, &end, -1);
656 value = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
658 if (value && *value == '\0') {
662 if (!prefs_template_string_is_valid(value, &line, FALSE)) {
663 alertpanel_error(_("Template body format error at line %d."), line);
668 name = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_name),
671 alertpanel_error(_("Template name is not set."));
675 to = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_to),
677 cc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_cc),
679 bcc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_bcc),
681 subject = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_subject),
684 if (to && *to == '\0') {
688 if (cc && *cc == '\0') {
692 if (bcc && *bcc == '\0') {
696 if (subject && *subject == '\0') {
701 if (!prefs_template_string_is_valid(to, NULL, FALSE)) {
702 alertpanel_error(_("Template To format error."));
707 if (!prefs_template_string_is_valid(cc, NULL, FALSE)) {
708 alertpanel_error(_("Template Cc format error."));
713 if (!prefs_template_string_is_valid(bcc, NULL, FALSE)) {
714 alertpanel_error(_("Template Bcc format error."));
719 if (!prefs_template_string_is_valid(subject, NULL, FALSE)) {
720 alertpanel_error(_("Template subject format error."));
726 tmpl = g_new(Template, 1);
727 tmpl->load_filename = NULL;
729 tmpl->subject = subject;
735 prefs_template_list_view_insert_template(templates.list_view,
736 row, tmpl->name, tmpl);
741 static void prefs_template_register_cb(void)
743 modified = !prefs_template_list_view_set_row(-1);
744 modified_list = TRUE;
747 static void prefs_template_substitute_cb(void)
754 row = gtkut_list_view_get_selected_row(templates.list_view);
758 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
759 if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
762 gtk_tree_model_get(model, &iter, TEMPL_DATA, &tmpl, -1);
766 modified = !prefs_template_list_view_set_row(row);
767 modified_list = TRUE;
770 static void prefs_template_delete_cb(void)
777 row = gtkut_list_view_get_selected_row(templates.list_view);
781 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
782 if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
785 gtk_tree_model_get(model, &iter, TEMPL_DATA, &tmpl, -1);
789 if (alertpanel(_("Delete template"),
790 _("Do you really want to delete this template?"),
791 GTK_STOCK_CANCEL, GTK_STOCK_DELETE,
792 NULL) != G_ALERTALTERNATE)
795 gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
796 modified_list = TRUE;
799 static void prefs_template_delete_all_cb(void)
801 GtkListStore *list_store;
803 if (alertpanel(_("Delete all templates"),
804 _("Do you really want to delete all the templates?"),
805 GTK_STOCK_CANCEL, "+"GTK_STOCK_DELETE, NULL) == G_ALERTDEFAULT)
808 list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view)));
809 prefs_template_clear_list();
812 prefs_template_reset_dialog();
813 modified_list = TRUE;
816 static void prefs_template_duplicate_cb(void)
823 row = gtkut_list_view_get_selected_row(templates.list_view);
827 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
828 if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
831 gtk_tree_model_get(model, &iter, TEMPL_DATA, &tmpl, -1);
835 modified_list = !prefs_template_list_view_set_row(-row-2);
838 static void prefs_template_clear_cb(void)
840 prefs_template_reset_dialog();
843 static void prefs_template_top_cb(void)
846 GtkTreeIter top, sel;
849 row = gtkut_list_view_get_selected_row(templates.list_view);
853 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
855 if (!gtk_tree_model_iter_nth_child(model, &top, NULL, 0)
856 || !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
859 gtk_list_store_move_after(GTK_LIST_STORE(model), &sel, &top);
860 gtkut_list_view_select_row(templates.list_view, 1);
861 modified_list = TRUE;
864 static void prefs_template_up_cb(void)
867 GtkTreeIter top, sel;
870 row = gtkut_list_view_get_selected_row(templates.list_view);
874 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
876 if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row - 1)
877 || !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
880 gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
881 gtkut_list_view_select_row(templates.list_view, row - 1);
882 modified_list = TRUE;
885 static void prefs_template_down_cb(void)
888 GtkTreeIter top, sel;
891 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
892 n_rows = gtk_tree_model_iter_n_children(model, NULL);
893 row = gtkut_list_view_get_selected_row(templates.list_view);
894 if (row < 1 || row >= n_rows - 1)
897 if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
898 || !gtk_tree_model_iter_nth_child(model, &sel, NULL, row + 1))
901 gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
902 gtkut_list_view_select_row(templates.list_view, row + 1);
903 modified_list = TRUE;
906 static void prefs_template_bottom_cb(void)
909 GtkTreeIter top, sel;
912 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
913 n_rows = gtk_tree_model_iter_n_children(model, NULL);
914 row = gtkut_list_view_get_selected_row(templates.list_view);
915 if (row < 1 || row >= n_rows - 1)
918 if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
919 || !gtk_tree_model_iter_nth_child(model, &sel, NULL, n_rows - 1))
922 gtk_list_store_move_after(GTK_LIST_STORE(model), &top, &sel);
923 gtkut_list_view_select_row(templates.list_view, n_rows - 1);
924 modified_list = TRUE;
927 static GtkListStore* prefs_template_create_data_store(void)
929 return gtk_list_store_new(N_TEMPL_COLUMNS,
936 static void prefs_template_list_view_insert_template(GtkWidget *list_view,
938 const gchar *template,
943 GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
944 (GTK_TREE_VIEW(list_view)));
946 /* row -1 to add a new rule to store,
947 row >=0 to change an existing row
948 row <-1 insert a new row after (-row-2)
951 /* modify the existing */
952 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
955 } else if (row < -1 ) {
956 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
957 &sibling, NULL, -row-2))
963 gtk_list_store_append(list_store, &iter);
964 gtk_list_store_set(list_store, &iter,
965 TEMPL_TEXT, template,
968 } else if (row < -1) {
970 gtk_list_store_insert_after(list_store, &iter, &sibling);
971 gtk_list_store_set(list_store, &iter,
972 TEMPL_TEXT, template,
976 /* change existing */
978 g_auto_pointer_new_with_free(data, (GFreeFunc) template_free);
980 /* if replacing data in an existing row, the auto pointer takes care
981 * of destroying the Template data */
982 gtk_list_store_set(list_store, &iter,
983 TEMPL_TEXT, template,
985 TEMPL_AUTO_DATA, auto_data,
988 g_auto_pointer_free(auto_data);
992 static GtkItemFactory *prefs_template_popup_factory = NULL;
993 static GtkWidget *prefs_template_popup_menu = NULL;
995 static GtkItemFactoryEntry prefs_template_popup_entries[] =
997 {N_("/_Delete"), NULL, prefs_template_delete_cb, 0, NULL, NULL},
998 {N_("/Delete _all"), NULL, prefs_template_delete_all_cb, 0, NULL, NULL},
999 {N_("/D_uplicate"), NULL, prefs_template_duplicate_cb, 0, NULL, NULL},
1002 static gint prefs_template_list_btn_pressed(GtkWidget *widget, GdkEventButton *event,
1003 GtkTreeView *list_view)
1006 /* left- or right-button click */
1007 if (event->button == 1 || event->button == 3) {
1008 GtkTreePath *path = NULL;
1009 if (gtk_tree_view_get_path_at_pos( list_view, event->x, event->y,
1010 &path, NULL, NULL, NULL)) {
1011 prefs_template_select_row(list_view, path);
1014 gtk_tree_path_free(path);
1017 /* right-button click */
1018 if (event->button == 3) {
1019 GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1024 if (!prefs_template_popup_menu) {
1025 gint n_entries = sizeof(prefs_template_popup_entries) /
1026 sizeof(prefs_template_popup_entries[0]);
1027 prefs_template_popup_menu = menu_create_items(prefs_template_popup_entries,
1028 n_entries, "<PrefsTemplatePopupMenu>",
1029 &prefs_template_popup_factory, list_view);
1032 /* grey out some popup menu items if there is no selected row */
1033 row = gtkut_list_view_get_selected_row(GTK_WIDGET(list_view));
1034 menu_set_sensitive(prefs_template_popup_factory, "/Delete", (row > 0));
1035 menu_set_sensitive(prefs_template_popup_factory, "/Duplicate", (row > 0));
1037 /* grey out seom popup menu items if there is no row
1038 (not counting the (New) one at row 0) */
1039 non_empty = gtk_tree_model_get_iter_first(model, &iter);
1041 non_empty = gtk_tree_model_iter_next(model, &iter);
1042 menu_set_sensitive(prefs_template_popup_factory, "/Delete all", non_empty);
1044 gtk_menu_popup(GTK_MENU(prefs_template_popup_menu),
1045 NULL, NULL, NULL, NULL,
1046 event->button, event->time);
1052 static gboolean prefs_template_list_popup_menu(GtkWidget *widget, gpointer data)
1054 GtkTreeView *list_view = (GtkTreeView *)data;
1055 GdkEventButton event;
1058 event.time = gtk_get_current_event_time();
1060 prefs_template_list_btn_pressed(NULL, &event, list_view);
1065 static GtkWidget *prefs_template_list_view_create(void)
1067 GtkTreeView *list_view;
1068 GtkTreeSelection *selector;
1069 GtkTreeModel *model;
1071 model = GTK_TREE_MODEL(prefs_template_create_data_store());
1072 list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
1073 g_object_unref(model);
1076 g_signal_connect(G_OBJECT(list_view), "popup-menu",
1077 G_CALLBACK(prefs_template_list_popup_menu), list_view);
1079 gtk_widget_tap_and_hold_setup(GTK_WIDGET(list_view), NULL, NULL,
1080 GTK_TAP_AND_HOLD_NONE | GTK_TAP_AND_HOLD_NO_INTERNALS);
1081 g_signal_connect(G_OBJECT(list_view), "tap-and-hold",
1082 G_CALLBACK(prefs_template_list_popup_menu), list_view);
1084 g_signal_connect(G_OBJECT(list_view), "button-press-event",
1085 G_CALLBACK(prefs_template_list_btn_pressed), list_view);
1087 gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
1088 gtk_tree_view_set_reorderable(list_view, TRUE);
1090 selector = gtk_tree_view_get_selection(list_view);
1091 gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1093 /* create the columns */
1094 prefs_template_create_list_view_columns(GTK_WIDGET(list_view));
1096 return GTK_WIDGET(list_view);
1099 static void prefs_template_create_list_view_columns(GtkWidget *list_view)
1101 GtkTreeViewColumn *column;
1102 GtkCellRenderer *renderer;
1104 renderer = gtk_cell_renderer_text_new();
1105 column = gtk_tree_view_column_new_with_attributes
1106 (_("Current templates"),
1110 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1114 *\brief Triggered when a row has to be selected
1116 static void prefs_template_select_row(GtkTreeView *list_view, GtkTreePath *path)
1118 GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1119 GtkTreeSelection *selection;
1122 GtkTextBuffer *buffer;
1126 if (!model || !path || !gtk_tree_model_get_iter(model, &titer, path))
1130 selection = gtk_tree_view_get_selection(list_view);
1131 gtk_tree_selection_select_path(selection, path);
1133 tmpl_def.name = _("Template");
1134 tmpl_def.subject = "";
1138 tmpl_def.value = "";
1140 gtk_tree_model_get(model, &titer, TEMPL_DATA, &tmpl, -1);
1144 gtk_entry_set_text(GTK_ENTRY(templates.entry_name), tmpl->name);
1145 gtk_entry_set_text(GTK_ENTRY(templates.entry_to),
1146 tmpl->to ? tmpl->to : "");
1147 gtk_entry_set_text(GTK_ENTRY(templates.entry_cc),
1148 tmpl->cc ? tmpl->cc : "");
1149 gtk_entry_set_text(GTK_ENTRY(templates.entry_bcc),
1150 tmpl->bcc ? tmpl->bcc : "");
1151 gtk_entry_set_text(GTK_ENTRY(templates.entry_subject),
1152 tmpl->subject ? tmpl->subject : "");
1154 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
1155 gtk_text_buffer_set_text(buffer, "", -1);
1156 gtk_text_buffer_get_start_iter(buffer, &iter);
1157 gtk_text_buffer_insert(buffer, &iter, tmpl->value, -1);