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 (gpointer action, gpointer data);
103 static void prefs_template_ok_cb (gpointer action, gpointer data);
104 static void prefs_template_register_cb (gpointer action, gpointer data);
105 static void prefs_template_substitute_cb (gpointer action, gpointer data);
106 static void prefs_template_delete_cb (gpointer action, gpointer data);
107 static void prefs_template_delete_all_cb (gpointer action, gpointer data);
108 static void prefs_template_clear_cb (gpointer action, gpointer data);
109 static void prefs_template_duplicate_cb (gpointer action, gpointer data);
110 static void prefs_template_top_cb (gpointer action, gpointer data);
111 static void prefs_template_up_cb (gpointer action, gpointer data);
112 static void prefs_template_down_cb (gpointer action, gpointer data);
113 static void prefs_template_bottom_cb (gpointer action, gpointer data);
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;
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);
223 for (i=0; widgets_table[i].label; i++) {
227 label = gtk_label_new(widgets_table[i].label);
228 gtk_widget_show(label);
229 gtk_table_attach(GTK_TABLE(table), label, 0, 1, i, (i + 1),
230 (GtkAttachOptions) (GTK_FILL),
231 (GtkAttachOptions) 0, 0, 0);
232 gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
234 *(widgets_table[i].entry) = gtk_entry_new();
235 gtk_widget_show(*(widgets_table[i].entry));
236 gtk_table_attach(GTK_TABLE(table), *(widgets_table[i].entry), 1, 2, i, (i + 1),
237 (GtkAttachOptions) (GTK_EXPAND|GTK_SHRINK|GTK_FILL),
238 (GtkAttachOptions) 0, 0, 0);
239 CLAWS_SET_TIP(*(widgets_table[i].entry),
240 widgets_table[i].tooltips);
243 /* template content */
244 scroll2 = gtk_scrolled_window_new(NULL, NULL);
245 gtk_widget_show(scroll2);
246 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll2),
247 GTK_POLICY_AUTOMATIC,
248 GTK_POLICY_AUTOMATIC);
249 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll2),
251 gtk_box_pack_start(GTK_BOX(vbox1), scroll2, TRUE, TRUE, 0);
253 text_value = gtk_text_view_new();
254 if (prefs_common.textfont) {
255 PangoFontDescription *font_desc;
257 font_desc = pango_font_description_from_string
258 (prefs_common.textfont);
260 gtk_widget_modify_font(text_value, font_desc);
261 pango_font_description_free(font_desc);
264 gtk_widget_show(text_value);
266 gtk_widget_set_size_request(text_value, -1, 120);
268 gtk_widget_set_size_request(text_value, -1, 60);
270 gtk_container_add(GTK_CONTAINER(scroll2), text_value);
271 gtk_text_view_set_editable(GTK_TEXT_VIEW(text_value), TRUE);
272 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text_value), GTK_WRAP_WORD);
274 /* vbox for buttons and templates list */
275 vbox2 = gtk_vbox_new(FALSE, 6);
276 gtk_widget_show(vbox2);
277 gtk_container_set_border_width(GTK_CONTAINER(vbox2), 8);
278 gtk_paned_pack2(GTK_PANED(vpaned), vbox2, TRUE, FALSE);
280 /* register | substitute | delete */
281 hbox2 = gtk_hbox_new(FALSE, 4);
282 gtk_widget_show(hbox2);
283 gtk_box_pack_start(GTK_BOX(vbox2), hbox2, FALSE, FALSE, 0);
285 arrow1 = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
286 gtk_widget_show(arrow1);
287 gtk_box_pack_start(GTK_BOX(hbox2), arrow1, FALSE, FALSE, 0);
288 gtk_widget_set_size_request(arrow1, -1, 16);
290 hbox3 = gtk_hbox_new(TRUE, 4);
291 gtk_widget_show(hbox3);
292 gtk_box_pack_start(GTK_BOX(hbox2), hbox3, FALSE, FALSE, 0);
294 reg_btn = gtk_button_new_from_stock(GTK_STOCK_ADD);
295 gtk_widget_show(reg_btn);
296 gtk_box_pack_start(GTK_BOX(hbox3), reg_btn, FALSE, TRUE, 0);
297 g_signal_connect(G_OBJECT (reg_btn), "clicked",
298 G_CALLBACK (prefs_template_register_cb), NULL);
299 CLAWS_SET_TIP(reg_btn,
300 _("Append the new template above to the list"));
302 subst_btn = gtkut_get_replace_btn(_("Replace"));
303 gtk_widget_show(subst_btn);
304 gtk_box_pack_start(GTK_BOX(hbox3), subst_btn, FALSE, TRUE, 0);
305 g_signal_connect(G_OBJECT(subst_btn), "clicked",
306 G_CALLBACK(prefs_template_substitute_cb),
308 CLAWS_SET_TIP(subst_btn,
309 _("Replace the selected template in list with the template above"));
311 del_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
312 gtk_widget_show(del_btn);
313 gtk_box_pack_start(GTK_BOX(hbox3), del_btn, FALSE, TRUE, 0);
314 g_signal_connect(G_OBJECT(del_btn), "clicked",
315 G_CALLBACK(prefs_template_delete_cb), NULL);
316 CLAWS_SET_TIP(del_btn,
317 _("Delete the selected template from the list"));
319 clear_btn = gtk_button_new_from_stock (GTK_STOCK_CLEAR);
320 gtk_widget_show (clear_btn);
321 gtk_box_pack_start (GTK_BOX (hbox3), clear_btn, FALSE, TRUE, 0);
322 g_signal_connect(G_OBJECT (clear_btn), "clicked",
323 G_CALLBACK(prefs_template_clear_cb), NULL);
324 CLAWS_SET_TIP(clear_btn,
325 _("Clear all the input fields in the dialog"));
327 #if GTK_CHECK_VERSION(2, 8, 0)
328 desc_btn = gtk_button_new_from_stock(GTK_STOCK_INFO);
330 desc_btn = gtk_button_new_with_label(_(" Symbols... "));
332 gtk_widget_show(desc_btn);
333 gtk_box_pack_end(GTK_BOX(hbox2), desc_btn, FALSE, FALSE, 0);
334 g_signal_connect(G_OBJECT(desc_btn), "clicked",
335 G_CALLBACK(quote_fmt_quote_description), window);
336 CLAWS_SET_TIP(desc_btn,
337 _("Show information on configuring templates"));
340 hbox4 = gtk_hbox_new(FALSE, 8);
341 gtk_widget_show(hbox4);
342 gtk_box_pack_start(GTK_BOX(vbox2), hbox4, TRUE, TRUE, 0);
344 scroll1 = gtk_scrolled_window_new(NULL, NULL);
345 gtk_widget_show(scroll1);
346 gtk_box_pack_start(GTK_BOX(hbox4), scroll1, TRUE, TRUE, 0);
347 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll1),
348 GTK_POLICY_AUTOMATIC,
349 GTK_POLICY_AUTOMATIC);
351 vbox3 = gtk_vbox_new(FALSE, 8);
352 gtk_widget_show(vbox3);
353 gtk_box_pack_start(GTK_BOX(hbox4), vbox3, FALSE, FALSE, 0);
355 top_btn = gtk_button_new_from_stock(GTK_STOCK_GOTO_TOP);
356 gtk_widget_show(top_btn);
357 gtk_box_pack_start(GTK_BOX(vbox3), top_btn, FALSE, FALSE, 0);
358 g_signal_connect(G_OBJECT(top_btn), "clicked",
359 G_CALLBACK(prefs_template_top_cb), NULL);
360 CLAWS_SET_TIP(top_btn,
361 _("Move the selected template to the top"));
363 PACK_VSPACER(vbox3, spc_vbox, VSPACING_NARROW_2);
365 up_btn = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
366 gtk_widget_show(up_btn);
367 gtk_box_pack_start (GTK_BOX(vbox3), up_btn, FALSE, FALSE, 0);
368 g_signal_connect(G_OBJECT(up_btn), "clicked",
369 G_CALLBACK(prefs_template_up_cb), NULL);
370 CLAWS_SET_TIP(up_btn,
371 _("Move the selected template up"));
373 down_btn = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
374 gtk_widget_show (down_btn);
375 gtk_box_pack_start(GTK_BOX (vbox3), down_btn, FALSE, FALSE, 0);
376 g_signal_connect(G_OBJECT (down_btn), "clicked",
377 G_CALLBACK(prefs_template_down_cb), NULL);
378 CLAWS_SET_TIP(down_btn,
379 _("Move the selected template down"));
381 PACK_VSPACER(vbox3, spc_vbox, VSPACING_NARROW_2);
383 bottom_btn = gtk_button_new_from_stock(GTK_STOCK_GOTO_BOTTOM);
384 gtk_widget_show(bottom_btn);
385 gtk_box_pack_start(GTK_BOX(vbox3), bottom_btn, FALSE, FALSE, 0);
386 g_signal_connect(G_OBJECT(bottom_btn), "clicked",
387 G_CALLBACK(prefs_template_bottom_cb), NULL);
388 CLAWS_SET_TIP(bottom_btn,
389 _("Move the selected template to the bottom"));
391 list_view = prefs_template_list_view_create();
392 gtk_widget_show(list_view);
393 gtk_widget_set_size_request(scroll1, -1, 140);
394 gtk_container_add(GTK_CONTAINER(scroll1), list_view);
396 /* help | cancel | ok */
397 gtkut_stock_button_set_create_with_help(&confirm_area, &help_btn,
398 &cancel_btn, GTK_STOCK_CANCEL,
399 &ok_btn, GTK_STOCK_OK,
401 gtk_widget_show(confirm_area);
402 gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
403 gtk_widget_grab_default(ok_btn);
405 gtk_window_set_title(GTK_WINDOW(window), _("Template configuration"));
407 g_signal_connect(G_OBJECT(window), "delete_event",
408 G_CALLBACK(prefs_template_deleted_cb), NULL);
409 g_signal_connect(G_OBJECT(window), "size_allocate",
410 G_CALLBACK(prefs_template_size_allocate_cb), NULL);
411 g_signal_connect(G_OBJECT(window), "key_press_event",
412 G_CALLBACK(prefs_template_key_pressed_cb), NULL);
413 MANAGE_WINDOW_SIGNALS_CONNECT(window);
414 g_signal_connect(G_OBJECT(ok_btn), "clicked",
415 G_CALLBACK(prefs_template_ok_cb), NULL);
416 g_signal_connect(G_OBJECT(cancel_btn), "clicked",
417 G_CALLBACK(prefs_template_cancel_cb), NULL);
418 g_signal_connect(G_OBJECT(help_btn), "clicked",
419 G_CALLBACK(manual_open_with_anchor_cb),
420 MANUAL_ANCHOR_TEMPLATES);
422 if (!geometry.min_height) {
423 geometry.min_width = 500;
424 geometry.min_height = 540;
427 gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
429 gtk_widget_set_size_request(window, prefs_common.templateswin_width,
430 prefs_common.templateswin_height);
432 templates.window = window;
433 templates.ok_btn = ok_btn;
434 templates.list_view = list_view;
435 templates.text_value = text_value;
438 static void prefs_template_reset_dialog(void)
440 GtkTextBuffer *buffer;
442 gtk_entry_set_text(GTK_ENTRY(templates.entry_name), "");
443 gtk_entry_set_text(GTK_ENTRY(templates.entry_from), "");
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)
473 manage_window_set_transient(GTK_WINDOW(templates.window));
474 gtk_widget_grab_focus(templates.ok_btn);
476 prefs_template_clear_list();
478 tmpl_list = template_read_config();
480 address_completion_start(templates.window);
482 for (i=0; widgets_table[i].label; i++) {
483 if (widgets_table[i].compl)
484 address_completion_register_entry(
485 GTK_ENTRY(*(widgets_table[i].entry)), TRUE);
488 for (cur = tmpl_list; cur != NULL; cur = cur->next) {
489 tmpl = (Template *)cur->data;
490 prefs_template_list_view_insert_template(templates.list_view,
495 prefs_template_reset_dialog();
497 g_slist_free(tmpl_list);
500 static gint prefs_template_deleted_cb(GtkWidget *widget, GdkEventAny *event,
503 prefs_template_cancel_cb(NULL, NULL);
507 static gboolean prefs_template_key_pressed_cb(GtkWidget *widget,
508 GdkEventKey *event, gpointer data)
510 if (event && event->keyval == GDK_Escape)
511 prefs_template_cancel_cb(NULL, NULL);
513 GtkWidget *focused = gtkut_get_focused_child(
514 GTK_CONTAINER(widget));
515 if (focused && GTK_IS_EDITABLE(focused)) {
522 static void prefs_template_address_completion_end(void)
526 for (i=0; widgets_table[i].label; i++) {
527 if (widgets_table[i].compl)
528 address_completion_unregister_entry(
529 GTK_ENTRY(*(widgets_table[i].entry)));
531 address_completion_end(templates.window);
534 static void prefs_template_ok_cb(gpointer action, gpointer data)
539 if (modified && alertpanel(_("Entry not saved"),
540 _("The entry was not saved. Close anyway?"),
541 GTK_STOCK_CLOSE, _("+_Continue editing"),
542 NULL) != G_ALERTDEFAULT) {
546 prefs_template_address_completion_end();
549 modified_list = FALSE;
550 tmpl_list = prefs_template_get_list();
551 template_set_config(tmpl_list);
552 compose_reflect_prefs_all();
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);
560 static void prefs_template_cancel_cb(gpointer action, gpointer data)
564 if (modified && alertpanel(_("Entry not saved"),
565 _("The entry was not saved. Close anyway?"),
566 GTK_STOCK_CLOSE, _("+_Continue editing"),
567 NULL) != G_ALERTDEFAULT) {
569 } else if (modified_list && alertpanel(_("Templates list not saved"),
570 _("The templates list has been modified. Close anyway?"),
571 GTK_STOCK_CLOSE, _("+_Continue editing"),
572 NULL) != G_ALERTDEFAULT) {
576 prefs_template_address_completion_end();
579 modified_list = FALSE;
580 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
581 (templates.list_view)));
582 gtk_list_store_clear(store);
583 gtk_widget_hide(templates.window);
588 *\brief Request list for storage. New list is owned
591 static GSList *prefs_template_get_list(void)
593 GSList *tmpl_list = NULL;
598 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
599 if (!gtk_tree_model_get_iter_first(model, &iter))
603 gtk_tree_model_get(model, &iter,
610 ntmpl = g_new(Template, 1);
611 ntmpl->load_filename = NULL;
612 ntmpl->name = tmpl->name && *(tmpl->name)
613 ? g_strdup(tmpl->name)
615 ntmpl->subject = tmpl->subject && *(tmpl->subject)
616 ? g_strdup(tmpl->subject)
618 ntmpl->from = tmpl->from && *(tmpl->from)
619 ? g_strdup(tmpl->from)
621 ntmpl->to = tmpl->to && *(tmpl->to)
624 ntmpl->cc = tmpl->cc && *(tmpl->cc)
627 ntmpl->bcc = tmpl->bcc && *(tmpl->bcc)
628 ? g_strdup(tmpl->bcc)
630 ntmpl->value = tmpl->value && *(tmpl->value)
631 ? g_strdup(tmpl->value)
633 tmpl_list = g_slist_append(tmpl_list, ntmpl);
636 } while (gtk_tree_model_iter_next(model, &iter));
641 gboolean prefs_template_string_is_valid(gchar *string, gint *line, gboolean escaped_string, gboolean email)
643 gboolean result = TRUE;
644 if (string && *string != '\0') {
648 PrefsAccount *account = account_get_default();
650 if (escaped_string) {
651 tmp = malloc(strlen(string)+1);
652 pref_get_unescaped_pref(tmp, string);
654 tmp = g_strdup(string);
656 memset(&dummyinfo, 0, sizeof(MsgInfo));
658 quote_fmt_init(&dummyinfo, NULL, NULL, TRUE, account, FALSE, NULL);
660 quote_fmt_init(&dummyinfo, NULL, NULL, TRUE, account, FALSE);
662 quote_fmt_scan_string(tmp);
665 parsed_buf = quote_fmt_get_buffer();
668 *line = quote_fmt_get_line();
672 const gchar *start = strrchr(parsed_buf, '<');
673 const gchar *end = strrchr(parsed_buf, '>');
674 const gchar *at = strrchr(parsed_buf, '@');
675 const gchar *space = strrchr(parsed_buf, ' ');
678 if (at && space && (!start || !end || end < start || start < space))
681 quote_fmt_reset_vartable();
686 static gboolean prefs_template_list_view_set_row(gint row)
687 /* return TRUE if the row could be modified */
697 GtkTextBuffer *buffer;
698 GtkTextIter start, end;
702 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
704 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
705 gtk_text_buffer_get_start_iter(buffer, &start);
706 gtk_text_buffer_get_iter_at_offset(buffer, &end, -1);
707 value = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
709 if (value && *value == '\0') {
713 if (!prefs_template_string_is_valid(value, &line, TRUE, FALSE)) {
714 alertpanel_error(_("Template body format error at line %d."), line);
719 name = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_name),
722 alertpanel_error(_("Template name is not set."));
726 from = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_from),
728 to = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_to),
730 cc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_cc),
732 bcc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_bcc),
734 subject = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_subject),
737 if (from && *from == '\0') {
741 if (to && *to == '\0') {
745 if (cc && *cc == '\0') {
749 if (bcc && *bcc == '\0') {
753 if (subject && *subject == '\0') {
758 if (!prefs_template_string_is_valid(from, NULL, TRUE, TRUE)) {
759 alertpanel_error(_("Template From format error."));
764 if (!prefs_template_string_is_valid(to, NULL, TRUE, TRUE)) {
765 alertpanel_error(_("Template To format error."));
770 if (!prefs_template_string_is_valid(cc, NULL, TRUE, TRUE)) {
771 alertpanel_error(_("Template Cc format error."));
776 if (!prefs_template_string_is_valid(bcc, NULL, TRUE, TRUE)) {
777 alertpanel_error(_("Template Bcc format error."));
782 if (!prefs_template_string_is_valid(subject, NULL, TRUE, FALSE)) {
783 alertpanel_error(_("Template subject format error."));
789 tmpl = g_new(Template, 1);
790 tmpl->load_filename = NULL;
792 tmpl->subject = subject;
799 prefs_template_list_view_insert_template(templates.list_view,
800 row, tmpl->name, tmpl);
805 static void prefs_template_register_cb(gpointer action, gpointer data)
807 modified = !prefs_template_list_view_set_row(-1);
808 modified_list = TRUE;
811 static void prefs_template_substitute_cb(gpointer action, gpointer data)
818 row = gtkut_list_view_get_selected_row(templates.list_view);
822 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
823 if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
826 gtk_tree_model_get(model, &iter, TEMPL_DATA, &tmpl, -1);
830 modified = !prefs_template_list_view_set_row(row);
831 modified_list = TRUE;
834 static void prefs_template_delete_cb(gpointer action, gpointer data)
841 row = gtkut_list_view_get_selected_row(templates.list_view);
845 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
846 if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
849 gtk_tree_model_get(model, &iter, TEMPL_DATA, &tmpl, -1);
853 if (alertpanel(_("Delete template"),
854 _("Do you really want to delete this template?"),
855 GTK_STOCK_CANCEL, GTK_STOCK_DELETE,
856 NULL) != G_ALERTALTERNATE)
859 gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
860 modified_list = TRUE;
863 static void prefs_template_delete_all_cb(gpointer action, gpointer data)
865 GtkListStore *list_store;
867 if (alertpanel(_("Delete all templates"),
868 _("Do you really want to delete all the templates?"),
869 GTK_STOCK_CANCEL, "+"GTK_STOCK_DELETE, NULL) == G_ALERTDEFAULT)
872 list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view)));
873 prefs_template_clear_list();
876 prefs_template_reset_dialog();
877 modified_list = TRUE;
880 static void prefs_template_duplicate_cb(gpointer action, gpointer data)
887 row = gtkut_list_view_get_selected_row(templates.list_view);
891 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
892 if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
895 gtk_tree_model_get(model, &iter, TEMPL_DATA, &tmpl, -1);
899 modified_list = !prefs_template_list_view_set_row(-row-2);
902 static void prefs_template_clear_cb(gpointer action, gpointer data)
904 prefs_template_reset_dialog();
907 static void prefs_template_top_cb(gpointer action, gpointer data)
910 GtkTreeIter top, sel;
913 row = gtkut_list_view_get_selected_row(templates.list_view);
917 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
919 if (!gtk_tree_model_iter_nth_child(model, &top, NULL, 0)
920 || !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
923 gtk_list_store_move_after(GTK_LIST_STORE(model), &sel, &top);
924 gtkut_list_view_select_row(templates.list_view, 1);
925 modified_list = TRUE;
928 static void prefs_template_up_cb(gpointer action, gpointer data)
931 GtkTreeIter top, sel;
934 row = gtkut_list_view_get_selected_row(templates.list_view);
938 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
940 if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row - 1)
941 || !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
944 gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
945 gtkut_list_view_select_row(templates.list_view, row - 1);
946 modified_list = TRUE;
949 static void prefs_template_down_cb(gpointer action, gpointer data)
952 GtkTreeIter top, sel;
955 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
956 n_rows = gtk_tree_model_iter_n_children(model, NULL);
957 row = gtkut_list_view_get_selected_row(templates.list_view);
958 if (row < 1 || row >= n_rows - 1)
961 if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
962 || !gtk_tree_model_iter_nth_child(model, &sel, NULL, row + 1))
965 gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
966 gtkut_list_view_select_row(templates.list_view, row + 1);
967 modified_list = TRUE;
970 static void prefs_template_bottom_cb(gpointer action, gpointer data)
973 GtkTreeIter top, sel;
976 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
977 n_rows = gtk_tree_model_iter_n_children(model, NULL);
978 row = gtkut_list_view_get_selected_row(templates.list_view);
979 if (row < 1 || row >= n_rows - 1)
982 if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
983 || !gtk_tree_model_iter_nth_child(model, &sel, NULL, n_rows - 1))
986 gtk_list_store_move_after(GTK_LIST_STORE(model), &top, &sel);
987 gtkut_list_view_select_row(templates.list_view, n_rows - 1);
988 modified_list = TRUE;
991 static GtkListStore* prefs_template_create_data_store(void)
993 return gtk_list_store_new(N_TEMPL_COLUMNS,
1000 static void prefs_template_list_view_insert_template(GtkWidget *list_view,
1002 const gchar *template,
1006 GtkTreeIter sibling;
1007 GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
1008 (GTK_TREE_VIEW(list_view)));
1010 /* row -1 to add a new rule to store,
1011 row >=0 to change an existing row
1012 row <-1 insert a new row after (-row-2)
1015 /* modify the existing */
1016 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
1019 } else if (row < -1 ) {
1020 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
1021 &sibling, NULL, -row-2))
1027 gtk_list_store_append(list_store, &iter);
1028 gtk_list_store_set(list_store, &iter,
1029 TEMPL_TEXT, template,
1032 } else if (row < -1) {
1034 gtk_list_store_insert_after(list_store, &iter, &sibling);
1035 gtk_list_store_set(list_store, &iter,
1036 TEMPL_TEXT, template,
1040 /* change existing */
1042 g_auto_pointer_new_with_free(data, (GFreeFunc) template_free);
1044 /* if replacing data in an existing row, the auto pointer takes care
1045 * of destroying the Template data */
1046 gtk_list_store_set(list_store, &iter,
1047 TEMPL_TEXT, template,
1049 TEMPL_AUTO_DATA, auto_data,
1052 g_auto_pointer_free(auto_data);
1056 static GtkActionGroup *prefs_template_popup_action = NULL;
1057 static GtkWidget *prefs_template_popup_menu = NULL;
1059 static GtkActionEntry prefs_template_popup_entries[] =
1061 {"PrefsTemplatePopup", NULL, "PrefsTemplatePopup" },
1062 {"PrefsTemplatePopup/Delete", NULL, N_("_Delete"), NULL, NULL, G_CALLBACK(prefs_template_delete_cb) },
1063 {"PrefsTemplatePopup/DeleteAll", NULL, N_("Delete _all"), NULL, NULL, G_CALLBACK(prefs_template_delete_all_cb) },
1064 {"PrefsTemplatePopup/Duplicate", NULL, N_("D_uplicate"), NULL, NULL, G_CALLBACK(prefs_template_duplicate_cb) },
1067 static gint prefs_template_list_btn_pressed(GtkWidget *widget, GdkEventButton *event,
1068 GtkTreeView *list_view)
1071 /* left- or right-button click */
1072 if (event->button == 1 || event->button == 3) {
1073 GtkTreePath *path = NULL;
1074 if (gtk_tree_view_get_path_at_pos( list_view, event->x, event->y,
1075 &path, NULL, NULL, NULL)) {
1076 prefs_template_select_row(list_view, path);
1079 gtk_tree_path_free(path);
1082 /* right-button click */
1083 if (event->button == 3) {
1084 GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1089 if (!prefs_template_popup_menu) {
1090 prefs_template_popup_action = cm_menu_create_action_group("PrefsTemplatePopup", prefs_template_popup_entries,
1091 G_N_ELEMENTS(prefs_template_popup_entries), (gpointer)list_view);
1092 MENUITEM_ADDUI("/Menus", "PrefsTemplatePopup", "PrefsTemplatePopup", GTK_UI_MANAGER_MENU)
1093 MENUITEM_ADDUI("/Menus/PrefsTemplatePopup", "Delete", "PrefsTemplatePopup/Delete", GTK_UI_MANAGER_MENUITEM)
1094 MENUITEM_ADDUI("/Menus/PrefsTemplatePopup", "DeleteAll", "PrefsTemplatePopup/DeleteAll", GTK_UI_MANAGER_MENUITEM)
1095 MENUITEM_ADDUI("/Menus/PrefsTemplatePopup", "Duplicate", "PrefsTemplatePopup/Duplicate", GTK_UI_MANAGER_MENUITEM)
1096 prefs_template_popup_menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
1097 gtk_ui_manager_get_widget(gtkut_ui_manager(), "/Menus/PrefsTemplatePopup")) );
1100 /* grey out some popup menu items if there is no selected row */
1101 row = gtkut_list_view_get_selected_row(GTK_WIDGET(list_view));
1102 cm_menu_set_sensitive("PrefsTemplatePopup/Delete", (row > 0));
1103 cm_menu_set_sensitive("PrefsTemplatePopup/Duplicate", (row > 0));
1105 /* grey out seom popup menu items if there is no row
1106 (not counting the (New) one at row 0) */
1107 non_empty = gtk_tree_model_get_iter_first(model, &iter);
1109 non_empty = gtk_tree_model_iter_next(model, &iter);
1110 cm_menu_set_sensitive("PrefsTemplatePopup/DeleteAll", non_empty);
1112 gtk_menu_popup(GTK_MENU(prefs_template_popup_menu),
1113 NULL, NULL, NULL, NULL,
1114 event->button, event->time);
1120 static gboolean prefs_template_list_popup_menu(GtkWidget *widget, gpointer data)
1122 GtkTreeView *list_view = (GtkTreeView *)data;
1123 GdkEventButton event;
1126 event.time = gtk_get_current_event_time();
1128 prefs_template_list_btn_pressed(NULL, &event, list_view);
1133 static GtkWidget *prefs_template_list_view_create(void)
1135 GtkTreeView *list_view;
1136 GtkTreeSelection *selector;
1137 GtkTreeModel *model;
1139 model = GTK_TREE_MODEL(prefs_template_create_data_store());
1140 list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
1141 g_object_unref(model);
1144 g_signal_connect(G_OBJECT(list_view), "popup-menu",
1145 G_CALLBACK(prefs_template_list_popup_menu), list_view);
1147 gtk_widget_tap_and_hold_setup(GTK_WIDGET(list_view), NULL, NULL,
1148 GTK_TAP_AND_HOLD_NONE | GTK_TAP_AND_HOLD_NO_INTERNALS);
1149 g_signal_connect(G_OBJECT(list_view), "tap-and-hold",
1150 G_CALLBACK(prefs_template_list_popup_menu), list_view);
1152 g_signal_connect(G_OBJECT(list_view), "button-press-event",
1153 G_CALLBACK(prefs_template_list_btn_pressed), list_view);
1155 gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
1156 gtk_tree_view_set_reorderable(list_view, TRUE);
1158 selector = gtk_tree_view_get_selection(list_view);
1159 gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1161 /* create the columns */
1162 prefs_template_create_list_view_columns(GTK_WIDGET(list_view));
1164 return GTK_WIDGET(list_view);
1167 static void prefs_template_create_list_view_columns(GtkWidget *list_view)
1169 GtkTreeViewColumn *column;
1170 GtkCellRenderer *renderer;
1172 renderer = gtk_cell_renderer_text_new();
1173 column = gtk_tree_view_column_new_with_attributes
1174 (_("Current templates"),
1178 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1182 *\brief Triggered when a row has to be selected
1184 static void prefs_template_select_row(GtkTreeView *list_view, GtkTreePath *path)
1186 GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1187 GtkTreeSelection *selection;
1190 GtkTextBuffer *buffer;
1194 if (!model || !path || !gtk_tree_model_get_iter(model, &titer, path))
1198 selection = gtk_tree_view_get_selection(list_view);
1199 gtk_tree_selection_select_path(selection, path);
1201 tmpl_def.name = _("Template");
1202 tmpl_def.subject = "";
1207 tmpl_def.value = "";
1209 gtk_tree_model_get(model, &titer, TEMPL_DATA, &tmpl, -1);
1213 gtk_entry_set_text(GTK_ENTRY(templates.entry_name), tmpl->name);
1214 gtk_entry_set_text(GTK_ENTRY(templates.entry_from),
1215 tmpl->from ? tmpl->from : "");
1216 gtk_entry_set_text(GTK_ENTRY(templates.entry_to),
1217 tmpl->to ? tmpl->to : "");
1218 gtk_entry_set_text(GTK_ENTRY(templates.entry_cc),
1219 tmpl->cc ? tmpl->cc : "");
1220 gtk_entry_set_text(GTK_ENTRY(templates.entry_bcc),
1221 tmpl->bcc ? tmpl->bcc : "");
1222 gtk_entry_set_text(GTK_ENTRY(templates.entry_subject),
1223 tmpl->subject ? tmpl->subject : "");
1225 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
1226 gtk_text_buffer_set_text(buffer, "", -1);
1227 gtk_text_buffer_get_start_iter(buffer, &iter);
1228 gtk_text_buffer_insert(buffer, &iter, tmpl->value, -1);