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;
60 GtkWidget *entry_from;
64 GtkWidget *text_value;
67 static int modified = FALSE;
68 static int modified_list = FALSE;
77 {N_("Name"), &templates.entry_name, FALSE,
78 N_("This name is used as the Menu item")},
79 {N_("From"), &templates.entry_from, TRUE,
80 N_("Override composing account's From header. This doesn't change the composing account.")},
81 {N_("To"), &templates.entry_to, TRUE, NULL},
82 {N_("Cc"), &templates.entry_cc, TRUE, NULL},
83 {N_("Bcc"), &templates.entry_bcc, TRUE, NULL},
84 {N_("Subject"), &templates.entry_subject, FALSE, NULL},
85 {NULL, NULL, FALSE, NULL}
89 /* widget creating functions */
90 static void prefs_template_window_create (void);
91 static void prefs_template_window_setup (void);
93 static GSList *prefs_template_get_list (void);
96 static gint prefs_template_deleted_cb (GtkWidget *widget,
99 static gboolean prefs_template_key_pressed_cb (GtkWidget *widget,
102 static void prefs_template_cancel_cb (void);
103 static void prefs_template_ok_cb (void);
104 static void prefs_template_register_cb (void);
105 static void prefs_template_substitute_cb (void);
106 static void prefs_template_delete_cb (void);
107 static void prefs_template_delete_all_cb (void);
108 static void prefs_template_clear_cb (void);
109 static void prefs_template_duplicate_cb (void);
110 static void prefs_template_top_cb (void);
111 static void prefs_template_up_cb (void);
112 static void prefs_template_down_cb (void);
113 static void prefs_template_bottom_cb (void);
115 static GtkListStore* prefs_template_create_data_store (void);
116 static void prefs_template_list_view_insert_template (GtkWidget *list_view,
118 const gchar *template,
120 static GtkWidget *prefs_template_list_view_create (void);
121 static void prefs_template_create_list_view_columns (GtkWidget *list_view);
122 static void prefs_template_select_row(GtkTreeView *list_view, GtkTreePath *path);
124 /* Called from mainwindow.c */
125 void prefs_template_open(void)
129 if (!templates.window)
130 prefs_template_window_create();
132 prefs_template_window_setup();
133 gtk_widget_show(templates.window);
137 *\brief Save Gtk object size to prefs dataset
139 static void prefs_template_size_allocate_cb(GtkWidget *widget,
140 GtkAllocation *allocation)
142 g_return_if_fail(allocation != NULL);
144 prefs_common.templateswin_width = allocation->width;
145 prefs_common.templateswin_height = allocation->height;
148 static void prefs_template_window_create(void)
150 /* window structure ;) */
153 GtkWidget *scrolled_window;
156 GtkWidget *table; /* including : entry_[name|from|to|cc|bcc|subject] */
158 GtkWidget *text_value;
164 GtkWidget *subst_btn;
166 GtkWidget *clear_btn;
170 GtkWidget *list_view;
176 GtkWidget *bottom_btn;
177 GtkWidget *confirm_area;
179 GtkWidget *cancel_btn;
181 static GdkGeometry geometry;
183 GtkTooltips *tooltips;
185 debug_print("Creating templates configuration window...\n");
188 window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "prefs_template");
189 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
190 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
191 gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
193 vbox = gtk_vbox_new(FALSE, 8);
194 gtk_widget_show(vbox);
195 gtk_container_add(GTK_CONTAINER(window), vbox);
196 gtk_container_set_border_width(GTK_CONTAINER(vbox), 4);
198 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
199 gtk_widget_show(scrolled_window);
200 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
201 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
202 gtk_container_add(GTK_CONTAINER(vbox), scrolled_window);
204 /* vpaned to separate template settings from templates list */
205 vpaned = gtk_vpaned_new();
206 gtk_widget_show(vpaned);
207 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled_window),
210 /* vbox to handle template name and content */
211 vbox1 = gtk_vbox_new(FALSE, 6);
212 gtk_widget_show(vbox1);
213 gtk_container_set_border_width(GTK_CONTAINER(vbox1), 8);
214 gtk_paned_pack1(GTK_PANED(vpaned), vbox1, FALSE, FALSE);
216 table = gtk_table_new(5, 2, FALSE);
217 gtk_table_set_row_spacings (GTK_TABLE (table), VSPACING_NARROW_2);
218 gtk_table_set_col_spacings (GTK_TABLE (table), 4);
219 gtk_widget_show(table);
220 gtk_box_pack_start (GTK_BOX (vbox1), table, FALSE, FALSE, 0);
222 tooltips = gtk_tooltips_new();
224 for (i=0; widgets_table[i].label; i++) {
228 label = gtk_label_new(widgets_table[i].label);
229 gtk_widget_show(label);
230 gtk_table_attach(GTK_TABLE(table), label, 0, 1, i, (i + 1),
231 (GtkAttachOptions) (GTK_FILL),
232 (GtkAttachOptions) 0, 0, 0);
233 gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
235 *(widgets_table[i].entry) = gtk_entry_new();
236 gtk_widget_show(*(widgets_table[i].entry));
237 gtk_table_attach(GTK_TABLE(table), *(widgets_table[i].entry), 1, 2, i, (i + 1),
238 (GtkAttachOptions) (GTK_EXPAND|GTK_SHRINK|GTK_FILL),
239 (GtkAttachOptions) 0, 0, 0);
240 gtk_tooltips_set_tip(tooltips, *(widgets_table[i].entry),
241 widgets_table[i].tooltips, NULL);
243 if (widgets_table[i].compl)
244 address_completion_register_entry(
245 GTK_ENTRY(*(widgets_table[i].entry)), TRUE);
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),
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);
265 gtk_widget_modify_font(text_value, font_desc);
266 pango_font_description_free(font_desc);
269 gtk_widget_show(text_value);
271 gtk_widget_set_size_request(text_value, -1, 120);
273 gtk_widget_set_size_request(text_value, -1, 60);
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_vbox_new(FALSE, 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_hbox_new(FALSE, 4);
287 gtk_widget_show(hbox2);
288 gtk_box_pack_start(GTK_BOX(vbox2), hbox2, FALSE, FALSE, 0);
290 arrow1 = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
291 gtk_widget_show(arrow1);
292 gtk_box_pack_start(GTK_BOX(hbox2), arrow1, FALSE, FALSE, 0);
293 gtk_widget_set_size_request(arrow1, -1, 16);
295 hbox3 = gtk_hbox_new(TRUE, 4);
296 gtk_widget_show(hbox3);
297 gtk_box_pack_start(GTK_BOX(hbox2), hbox3, FALSE, FALSE, 0);
299 reg_btn = gtk_button_new_from_stock(GTK_STOCK_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 gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), reg_btn,
305 _("Append the new template above to the list"), NULL);
307 subst_btn = gtkut_get_replace_btn(_("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),
313 gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), subst_btn,
314 _("Replace the selected template in list with the template above"), NULL);
316 del_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
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 gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), del_btn,
322 _("Delete the selected template from the list"), NULL);
324 clear_btn = gtk_button_new_from_stock (GTK_STOCK_CLEAR);
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 gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), clear_btn,
330 _("Clear all the input fields in the dialog"), NULL);
332 #if GTK_CHECK_VERSION(2, 8, 0)
333 desc_btn = gtk_button_new_from_stock(GTK_STOCK_INFO);
335 desc_btn = gtk_button_new_with_label(_(" Symbols... "));
337 gtk_widget_show(desc_btn);
338 gtk_box_pack_end(GTK_BOX(hbox2), desc_btn, FALSE, FALSE, 0);
339 g_signal_connect(G_OBJECT(desc_btn), "clicked",
340 G_CALLBACK(quote_fmt_quote_description), window);
341 gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), desc_btn,
342 _("Show information on configuring templates"), NULL);
345 hbox4 = gtk_hbox_new(FALSE, 8);
346 gtk_widget_show(hbox4);
347 gtk_box_pack_start(GTK_BOX(vbox2), hbox4, TRUE, TRUE, 0);
349 scroll1 = gtk_scrolled_window_new(NULL, NULL);
350 gtk_widget_show(scroll1);
351 gtk_box_pack_start(GTK_BOX(hbox4), scroll1, TRUE, TRUE, 0);
352 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll1),
353 GTK_POLICY_AUTOMATIC,
354 GTK_POLICY_AUTOMATIC);
356 vbox3 = gtk_vbox_new(FALSE, 8);
357 gtk_widget_show(vbox3);
358 gtk_box_pack_start(GTK_BOX(hbox4), vbox3, FALSE, FALSE, 0);
360 top_btn = gtk_button_new_from_stock(GTK_STOCK_GOTO_TOP);
361 gtk_widget_show(top_btn);
362 gtk_box_pack_start(GTK_BOX(vbox3), top_btn, FALSE, FALSE, 0);
363 g_signal_connect(G_OBJECT(top_btn), "clicked",
364 G_CALLBACK(prefs_template_top_cb), NULL);
365 gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), top_btn,
366 _("Move the selected template to the top"), NULL);
368 PACK_VSPACER(vbox3, spc_vbox, VSPACING_NARROW_2);
370 up_btn = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
371 gtk_widget_show(up_btn);
372 gtk_box_pack_start (GTK_BOX(vbox3), up_btn, FALSE, FALSE, 0);
373 g_signal_connect(G_OBJECT(up_btn), "clicked",
374 G_CALLBACK(prefs_template_up_cb), NULL);
375 gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), up_btn,
376 _("Move the selected template up"), NULL);
378 down_btn = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
379 gtk_widget_show (down_btn);
380 gtk_box_pack_start(GTK_BOX (vbox3), down_btn, FALSE, FALSE, 0);
381 g_signal_connect(G_OBJECT (down_btn), "clicked",
382 G_CALLBACK(prefs_template_down_cb), NULL);
383 gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), down_btn,
384 _("Move the selected template down"), NULL);
386 PACK_VSPACER(vbox3, spc_vbox, VSPACING_NARROW_2);
388 bottom_btn = gtk_button_new_from_stock(GTK_STOCK_GOTO_BOTTOM);
389 gtk_widget_show(bottom_btn);
390 gtk_box_pack_start(GTK_BOX(vbox3), bottom_btn, FALSE, FALSE, 0);
391 g_signal_connect(G_OBJECT(bottom_btn), "clicked",
392 G_CALLBACK(prefs_template_bottom_cb), NULL);
393 gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), bottom_btn,
394 _("Move the selected template to the bottom"), NULL);
396 list_view = prefs_template_list_view_create();
397 gtk_widget_show(list_view);
398 gtk_widget_set_size_request(scroll1, -1, 140);
399 gtk_container_add(GTK_CONTAINER(scroll1), list_view);
401 /* help | cancel | ok */
402 gtkut_stock_button_set_create_with_help(&confirm_area, &help_btn,
403 &cancel_btn, GTK_STOCK_CANCEL,
404 &ok_btn, GTK_STOCK_OK,
406 gtk_widget_show(confirm_area);
407 gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
408 gtk_widget_grab_default(ok_btn);
410 gtk_window_set_title(GTK_WINDOW(window), _("Template configuration"));
412 g_signal_connect(G_OBJECT(window), "delete_event",
413 G_CALLBACK(prefs_template_deleted_cb), NULL);
414 g_signal_connect(G_OBJECT(window), "size_allocate",
415 G_CALLBACK(prefs_template_size_allocate_cb), NULL);
416 g_signal_connect(G_OBJECT(window), "key_press_event",
417 G_CALLBACK(prefs_template_key_pressed_cb), NULL);
418 MANAGE_WINDOW_SIGNALS_CONNECT(window);
419 g_signal_connect(G_OBJECT(ok_btn), "clicked",
420 G_CALLBACK(prefs_template_ok_cb), NULL);
421 g_signal_connect(G_OBJECT(cancel_btn), "clicked",
422 G_CALLBACK(prefs_template_cancel_cb), NULL);
423 g_signal_connect(G_OBJECT(help_btn), "clicked",
424 G_CALLBACK(manual_open_with_anchor_cb),
425 MANUAL_ANCHOR_TEMPLATES);
427 address_completion_start(window);
429 if (!geometry.min_height) {
430 geometry.min_width = 500;
431 geometry.min_height = 540;
434 gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
436 gtk_widget_set_size_request(window, prefs_common.templateswin_width,
437 prefs_common.templateswin_height);
439 templates.window = window;
440 templates.ok_btn = ok_btn;
441 templates.list_view = list_view;
442 templates.text_value = text_value;
445 static void prefs_template_reset_dialog(void)
447 GtkTextBuffer *buffer;
449 gtk_entry_set_text(GTK_ENTRY(templates.entry_name), "");
450 gtk_entry_set_text(GTK_ENTRY(templates.entry_from), "");
451 gtk_entry_set_text(GTK_ENTRY(templates.entry_to), "");
452 gtk_entry_set_text(GTK_ENTRY(templates.entry_cc), "");
453 gtk_entry_set_text(GTK_ENTRY(templates.entry_bcc), "");
454 gtk_entry_set_text(GTK_ENTRY(templates.entry_subject), "");
456 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
457 gtk_text_buffer_set_text(buffer, "", -1);
460 static void prefs_template_clear_list(void)
464 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
465 (templates.list_view)));
466 gtk_list_store_clear(store);
468 prefs_template_list_view_insert_template(templates.list_view,
473 static void prefs_template_window_setup(void)
479 manage_window_set_transient(GTK_WINDOW(templates.window));
480 gtk_widget_grab_focus(templates.ok_btn);
482 prefs_template_clear_list();
484 tmpl_list = template_read_config();
486 for (cur = tmpl_list; cur != NULL; cur = cur->next) {
487 tmpl = (Template *)cur->data;
488 prefs_template_list_view_insert_template(templates.list_view,
493 prefs_template_reset_dialog();
495 g_slist_free(tmpl_list);
498 static gint prefs_template_deleted_cb(GtkWidget *widget, GdkEventAny *event,
501 prefs_template_cancel_cb();
505 static gboolean prefs_template_key_pressed_cb(GtkWidget *widget,
506 GdkEventKey *event, gpointer data)
508 if (event && event->keyval == GDK_Escape)
509 prefs_template_cancel_cb();
511 GtkWidget *focused = gtkut_get_focused_child(
512 GTK_CONTAINER(widget));
513 if (focused && GTK_IS_EDITABLE(focused)) {
520 static void prefs_template_address_completion_end(void)
524 for (i=0; widgets_table[i].label; i++) {
525 if (widgets_table[i].compl)
526 address_completion_unregister_entry(
527 GTK_ENTRY(*(widgets_table[i].entry)));
529 address_completion_end(templates.window);
532 static void prefs_template_ok_cb(void)
537 if (modified && alertpanel(_("Entry not saved"),
538 _("The entry was not saved. Close anyway?"),
539 GTK_STOCK_CLOSE, _("+_Continue editing"),
540 NULL) != G_ALERTDEFAULT) {
544 prefs_template_address_completion_end();
547 modified_list = FALSE;
548 tmpl_list = prefs_template_get_list();
549 template_set_config(tmpl_list);
550 compose_reflect_prefs_all();
551 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
552 (templates.list_view)));
553 gtk_list_store_clear(store);
554 gtk_widget_hide(templates.window);
558 static void prefs_template_cancel_cb(void)
562 if (modified && alertpanel(_("Entry not saved"),
563 _("The entry was not saved. Close anyway?"),
564 GTK_STOCK_CLOSE, _("+_Continue editing"),
565 NULL) != G_ALERTDEFAULT) {
567 } else if (modified_list && alertpanel(_("Templates list not saved"),
568 _("The templates list has been modified. Close anyway?"),
569 GTK_STOCK_CLOSE, _("+_Continue editing"),
570 NULL) != G_ALERTDEFAULT) {
574 prefs_template_address_completion_end();
577 modified_list = FALSE;
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_hide(templates.window);
586 *\brief Request list for storage. New list is owned
589 static GSList *prefs_template_get_list(void)
591 GSList *tmpl_list = NULL;
596 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
597 if (!gtk_tree_model_get_iter_first(model, &iter))
601 gtk_tree_model_get(model, &iter,
608 ntmpl = g_new(Template, 1);
609 ntmpl->load_filename = NULL;
610 ntmpl->name = tmpl->name && *(tmpl->name)
611 ? g_strdup(tmpl->name)
613 ntmpl->subject = tmpl->subject && *(tmpl->subject)
614 ? g_strdup(tmpl->subject)
616 ntmpl->from = tmpl->from && *(tmpl->from)
617 ? g_strdup(tmpl->from)
619 ntmpl->to = tmpl->to && *(tmpl->to)
622 ntmpl->cc = tmpl->cc && *(tmpl->cc)
625 ntmpl->bcc = tmpl->bcc && *(tmpl->bcc)
626 ? g_strdup(tmpl->bcc)
628 ntmpl->value = tmpl->value && *(tmpl->value)
629 ? g_strdup(tmpl->value)
631 tmpl_list = g_slist_append(tmpl_list, ntmpl);
634 } while (gtk_tree_model_iter_next(model, &iter));
639 gboolean prefs_template_string_is_valid(gchar *string, gint *line, gboolean escaped_string, gboolean email)
641 gboolean result = TRUE;
642 if (string && *string != '\0') {
646 PrefsAccount *account = account_get_default();
648 if (escaped_string) {
649 tmp = malloc(strlen(string)+1);
650 pref_get_unescaped_pref(tmp, string);
652 tmp = g_strdup(string);
654 memset(&dummyinfo, 0, sizeof(MsgInfo));
656 quote_fmt_init(&dummyinfo, NULL, NULL, TRUE, account, FALSE, NULL);
658 quote_fmt_init(&dummyinfo, NULL, NULL, TRUE, account, FALSE);
660 quote_fmt_scan_string(tmp);
663 parsed_buf = quote_fmt_get_buffer();
666 *line = quote_fmt_get_line();
670 const gchar *start = strrchr(parsed_buf, '<');
671 const gchar *end = strrchr(parsed_buf, '>');
672 const gchar *at = strrchr(parsed_buf, '@');
673 const gchar *space = strrchr(parsed_buf, ' ');
676 if (at && space && (!start || !end || end < start || start < space))
679 quote_fmt_reset_vartable();
684 static gboolean prefs_template_list_view_set_row(gint row)
685 /* return TRUE if the row could be modified */
695 GtkTextBuffer *buffer;
696 GtkTextIter start, end;
700 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
702 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
703 gtk_text_buffer_get_start_iter(buffer, &start);
704 gtk_text_buffer_get_iter_at_offset(buffer, &end, -1);
705 value = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
707 if (value && *value == '\0') {
711 if (!prefs_template_string_is_valid(value, &line, TRUE, FALSE)) {
712 alertpanel_error(_("Template body format error at line %d."), line);
717 name = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_name),
720 alertpanel_error(_("Template name is not set."));
724 from = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_from),
726 to = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_to),
728 cc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_cc),
730 bcc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_bcc),
732 subject = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_subject),
735 if (from && *from == '\0') {
739 if (to && *to == '\0') {
743 if (cc && *cc == '\0') {
747 if (bcc && *bcc == '\0') {
751 if (subject && *subject == '\0') {
756 if (!prefs_template_string_is_valid(from, NULL, TRUE, TRUE)) {
757 alertpanel_error(_("Template From format error."));
762 if (!prefs_template_string_is_valid(to, NULL, TRUE, TRUE)) {
763 alertpanel_error(_("Template To format error."));
768 if (!prefs_template_string_is_valid(cc, NULL, TRUE, TRUE)) {
769 alertpanel_error(_("Template Cc format error."));
774 if (!prefs_template_string_is_valid(bcc, NULL, TRUE, TRUE)) {
775 alertpanel_error(_("Template Bcc format error."));
780 if (!prefs_template_string_is_valid(subject, NULL, TRUE, FALSE)) {
781 alertpanel_error(_("Template subject format error."));
787 tmpl = g_new(Template, 1);
788 tmpl->load_filename = NULL;
790 tmpl->subject = subject;
797 prefs_template_list_view_insert_template(templates.list_view,
798 row, tmpl->name, tmpl);
803 static void prefs_template_register_cb(void)
805 modified = !prefs_template_list_view_set_row(-1);
806 modified_list = TRUE;
809 static void prefs_template_substitute_cb(void)
816 row = gtkut_list_view_get_selected_row(templates.list_view);
820 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
821 if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
824 gtk_tree_model_get(model, &iter, TEMPL_DATA, &tmpl, -1);
828 modified = !prefs_template_list_view_set_row(row);
829 modified_list = TRUE;
832 static void prefs_template_delete_cb(void)
839 row = gtkut_list_view_get_selected_row(templates.list_view);
843 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
844 if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
847 gtk_tree_model_get(model, &iter, TEMPL_DATA, &tmpl, -1);
851 if (alertpanel(_("Delete template"),
852 _("Do you really want to delete this template?"),
853 GTK_STOCK_CANCEL, GTK_STOCK_DELETE,
854 NULL) != G_ALERTALTERNATE)
857 gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
858 modified_list = TRUE;
861 static void prefs_template_delete_all_cb(void)
863 GtkListStore *list_store;
865 if (alertpanel(_("Delete all templates"),
866 _("Do you really want to delete all the templates?"),
867 GTK_STOCK_CANCEL, "+"GTK_STOCK_DELETE, NULL) == G_ALERTDEFAULT)
870 list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view)));
871 prefs_template_clear_list();
874 prefs_template_reset_dialog();
875 modified_list = TRUE;
878 static void prefs_template_duplicate_cb(void)
885 row = gtkut_list_view_get_selected_row(templates.list_view);
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))
893 gtk_tree_model_get(model, &iter, TEMPL_DATA, &tmpl, -1);
897 modified_list = !prefs_template_list_view_set_row(-row-2);
900 static void prefs_template_clear_cb(void)
902 prefs_template_reset_dialog();
905 static void prefs_template_top_cb(void)
908 GtkTreeIter top, sel;
911 row = gtkut_list_view_get_selected_row(templates.list_view);
915 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
917 if (!gtk_tree_model_iter_nth_child(model, &top, NULL, 0)
918 || !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
921 gtk_list_store_move_after(GTK_LIST_STORE(model), &sel, &top);
922 gtkut_list_view_select_row(templates.list_view, 1);
923 modified_list = TRUE;
926 static void prefs_template_up_cb(void)
929 GtkTreeIter top, sel;
932 row = gtkut_list_view_get_selected_row(templates.list_view);
936 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
938 if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row - 1)
939 || !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
942 gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
943 gtkut_list_view_select_row(templates.list_view, row - 1);
944 modified_list = TRUE;
947 static void prefs_template_down_cb(void)
950 GtkTreeIter top, sel;
953 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
954 n_rows = gtk_tree_model_iter_n_children(model, NULL);
955 row = gtkut_list_view_get_selected_row(templates.list_view);
956 if (row < 1 || row >= n_rows - 1)
959 if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
960 || !gtk_tree_model_iter_nth_child(model, &sel, NULL, row + 1))
963 gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
964 gtkut_list_view_select_row(templates.list_view, row + 1);
965 modified_list = TRUE;
968 static void prefs_template_bottom_cb(void)
971 GtkTreeIter top, sel;
974 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
975 n_rows = gtk_tree_model_iter_n_children(model, NULL);
976 row = gtkut_list_view_get_selected_row(templates.list_view);
977 if (row < 1 || row >= n_rows - 1)
980 if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
981 || !gtk_tree_model_iter_nth_child(model, &sel, NULL, n_rows - 1))
984 gtk_list_store_move_after(GTK_LIST_STORE(model), &top, &sel);
985 gtkut_list_view_select_row(templates.list_view, n_rows - 1);
986 modified_list = TRUE;
989 static GtkListStore* prefs_template_create_data_store(void)
991 return gtk_list_store_new(N_TEMPL_COLUMNS,
998 static void prefs_template_list_view_insert_template(GtkWidget *list_view,
1000 const gchar *template,
1004 GtkTreeIter sibling;
1005 GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
1006 (GTK_TREE_VIEW(list_view)));
1008 /* row -1 to add a new rule to store,
1009 row >=0 to change an existing row
1010 row <-1 insert a new row after (-row-2)
1013 /* modify the existing */
1014 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
1017 } else if (row < -1 ) {
1018 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
1019 &sibling, NULL, -row-2))
1025 gtk_list_store_append(list_store, &iter);
1026 gtk_list_store_set(list_store, &iter,
1027 TEMPL_TEXT, template,
1030 } else if (row < -1) {
1032 gtk_list_store_insert_after(list_store, &iter, &sibling);
1033 gtk_list_store_set(list_store, &iter,
1034 TEMPL_TEXT, template,
1038 /* change existing */
1040 g_auto_pointer_new_with_free(data, (GFreeFunc) template_free);
1042 /* if replacing data in an existing row, the auto pointer takes care
1043 * of destroying the Template data */
1044 gtk_list_store_set(list_store, &iter,
1045 TEMPL_TEXT, template,
1047 TEMPL_AUTO_DATA, auto_data,
1050 g_auto_pointer_free(auto_data);
1054 static GtkItemFactory *prefs_template_popup_factory = NULL;
1055 static GtkWidget *prefs_template_popup_menu = NULL;
1057 static GtkItemFactoryEntry prefs_template_popup_entries[] =
1059 {N_("/_Delete"), NULL, prefs_template_delete_cb, 0, NULL, NULL},
1060 {N_("/Delete _all"), NULL, prefs_template_delete_all_cb, 0, NULL, NULL},
1061 {N_("/D_uplicate"), NULL, prefs_template_duplicate_cb, 0, NULL, NULL},
1064 static gint prefs_template_list_btn_pressed(GtkWidget *widget, GdkEventButton *event,
1065 GtkTreeView *list_view)
1068 /* left- or right-button click */
1069 if (event->button == 1 || event->button == 3) {
1070 GtkTreePath *path = NULL;
1071 if (gtk_tree_view_get_path_at_pos( list_view, event->x, event->y,
1072 &path, NULL, NULL, NULL)) {
1073 prefs_template_select_row(list_view, path);
1076 gtk_tree_path_free(path);
1079 /* right-button click */
1080 if (event->button == 3) {
1081 GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1086 if (!prefs_template_popup_menu) {
1087 gint n_entries = sizeof(prefs_template_popup_entries) /
1088 sizeof(prefs_template_popup_entries[0]);
1089 prefs_template_popup_menu = menu_create_items(prefs_template_popup_entries,
1090 n_entries, "<PrefsTemplatePopupMenu>",
1091 &prefs_template_popup_factory, list_view);
1094 /* grey out some popup menu items if there is no selected row */
1095 row = gtkut_list_view_get_selected_row(GTK_WIDGET(list_view));
1096 menu_set_sensitive(prefs_template_popup_factory, "/Delete", (row > 0));
1097 menu_set_sensitive(prefs_template_popup_factory, "/Duplicate", (row > 0));
1099 /* grey out seom popup menu items if there is no row
1100 (not counting the (New) one at row 0) */
1101 non_empty = gtk_tree_model_get_iter_first(model, &iter);
1103 non_empty = gtk_tree_model_iter_next(model, &iter);
1104 menu_set_sensitive(prefs_template_popup_factory, "/Delete all", non_empty);
1106 gtk_menu_popup(GTK_MENU(prefs_template_popup_menu),
1107 NULL, NULL, NULL, NULL,
1108 event->button, event->time);
1114 static gboolean prefs_template_list_popup_menu(GtkWidget *widget, gpointer data)
1116 GtkTreeView *list_view = (GtkTreeView *)data;
1117 GdkEventButton event;
1120 event.time = gtk_get_current_event_time();
1122 prefs_template_list_btn_pressed(NULL, &event, list_view);
1127 static GtkWidget *prefs_template_list_view_create(void)
1129 GtkTreeView *list_view;
1130 GtkTreeSelection *selector;
1131 GtkTreeModel *model;
1133 model = GTK_TREE_MODEL(prefs_template_create_data_store());
1134 list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
1135 g_object_unref(model);
1138 g_signal_connect(G_OBJECT(list_view), "popup-menu",
1139 G_CALLBACK(prefs_template_list_popup_menu), list_view);
1141 gtk_widget_tap_and_hold_setup(GTK_WIDGET(list_view), NULL, NULL,
1142 GTK_TAP_AND_HOLD_NONE | GTK_TAP_AND_HOLD_NO_INTERNALS);
1143 g_signal_connect(G_OBJECT(list_view), "tap-and-hold",
1144 G_CALLBACK(prefs_template_list_popup_menu), list_view);
1146 g_signal_connect(G_OBJECT(list_view), "button-press-event",
1147 G_CALLBACK(prefs_template_list_btn_pressed), list_view);
1149 gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
1150 gtk_tree_view_set_reorderable(list_view, TRUE);
1152 selector = gtk_tree_view_get_selection(list_view);
1153 gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1155 /* create the columns */
1156 prefs_template_create_list_view_columns(GTK_WIDGET(list_view));
1158 return GTK_WIDGET(list_view);
1161 static void prefs_template_create_list_view_columns(GtkWidget *list_view)
1163 GtkTreeViewColumn *column;
1164 GtkCellRenderer *renderer;
1166 renderer = gtk_cell_renderer_text_new();
1167 column = gtk_tree_view_column_new_with_attributes
1168 (_("Current templates"),
1172 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1176 *\brief Triggered when a row has to be selected
1178 static void prefs_template_select_row(GtkTreeView *list_view, GtkTreePath *path)
1180 GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1181 GtkTreeSelection *selection;
1184 GtkTextBuffer *buffer;
1188 if (!model || !path || !gtk_tree_model_get_iter(model, &titer, path))
1192 selection = gtk_tree_view_get_selection(list_view);
1193 gtk_tree_selection_select_path(selection, path);
1195 tmpl_def.name = _("Template");
1196 tmpl_def.subject = "";
1201 tmpl_def.value = "";
1203 gtk_tree_model_get(model, &titer, TEMPL_DATA, &tmpl, -1);
1207 gtk_entry_set_text(GTK_ENTRY(templates.entry_name), tmpl->name);
1208 gtk_entry_set_text(GTK_ENTRY(templates.entry_from),
1209 tmpl->from ? tmpl->from : "");
1210 gtk_entry_set_text(GTK_ENTRY(templates.entry_to),
1211 tmpl->to ? tmpl->to : "");
1212 gtk_entry_set_text(GTK_ENTRY(templates.entry_cc),
1213 tmpl->cc ? tmpl->cc : "");
1214 gtk_entry_set_text(GTK_ENTRY(templates.entry_bcc),
1215 tmpl->bcc ? tmpl->bcc : "");
1216 gtk_entry_set_text(GTK_ENTRY(templates.entry_subject),
1217 tmpl->subject ? tmpl->subject : "");
1219 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
1220 gtk_text_buffer_set_text(buffer, "", -1);
1221 gtk_text_buffer_get_start_iter(buffer, &iter);
1222 gtk_text_buffer_insert(buffer, &iter, tmpl->value, -1);