2 * Claws Mail templates subsystem
3 * Copyright (C) 2001 Alexander Barinov
4 * Copyright (C) 2001-2009 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 gboolean prefs_template_search_func_cb (GtkTreeModel *model, gint column,
103 const gchar *key, GtkTreeIter *iter,
104 gpointer search_data);
106 static void prefs_template_cancel_cb (gpointer action, gpointer data);
107 static void prefs_template_ok_cb (gpointer action, gpointer data);
108 static void prefs_template_register_cb (gpointer action, gpointer data);
109 static void prefs_template_substitute_cb (gpointer action, gpointer data);
110 static void prefs_template_delete_cb (gpointer action, gpointer data);
111 static void prefs_template_delete_all_cb (gpointer action, gpointer data);
112 static void prefs_template_clear_cb (gpointer action, gpointer data);
113 static void prefs_template_duplicate_cb (gpointer action, gpointer data);
114 static void prefs_template_top_cb (gpointer action, gpointer data);
115 static void prefs_template_up_cb (gpointer action, gpointer data);
116 static void prefs_template_down_cb (gpointer action, gpointer data);
117 static void prefs_template_bottom_cb (gpointer action, gpointer data);
119 static GtkListStore* prefs_template_create_data_store (void);
120 static void prefs_template_list_view_insert_template (GtkWidget *list_view,
122 const gchar *template,
124 static GtkWidget *prefs_template_list_view_create (void);
125 static void prefs_template_create_list_view_columns (GtkWidget *list_view);
126 static void prefs_template_select_row(GtkTreeView *list_view, GtkTreePath *path);
128 /* Called from mainwindow.c */
129 void prefs_template_open(void)
133 if (!templates.window)
134 prefs_template_window_create();
136 prefs_template_window_setup();
137 gtk_widget_show(templates.window);
141 *\brief Save Gtk object size to prefs dataset
143 static void prefs_template_size_allocate_cb(GtkWidget *widget,
144 GtkAllocation *allocation)
146 cm_return_if_fail(allocation != NULL);
148 prefs_common.templateswin_width = allocation->width;
149 prefs_common.templateswin_height = allocation->height;
152 static void prefs_template_window_create(void)
154 /* window structure ;) */
157 GtkWidget *scrolled_window;
160 GtkWidget *table; /* including : entry_[name|from|to|cc|bcc|subject] */
162 GtkWidget *text_value;
168 GtkWidget *subst_btn;
170 GtkWidget *clear_btn;
174 GtkWidget *list_view;
180 GtkWidget *bottom_btn;
181 GtkWidget *confirm_area;
183 GtkWidget *cancel_btn;
185 static GdkGeometry geometry;
189 debug_print("Creating templates configuration window...\n");
192 window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "prefs_template");
193 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
194 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
195 gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
197 vbox = gtk_vbox_new(FALSE, 8);
198 gtk_widget_show(vbox);
199 gtk_container_add(GTK_CONTAINER(window), vbox);
200 gtk_container_set_border_width(GTK_CONTAINER(vbox), 4);
202 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
203 gtk_widget_show(scrolled_window);
204 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
205 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
206 gtk_container_add(GTK_CONTAINER(vbox), scrolled_window);
208 /* vpaned to separate template settings from templates list */
209 vpaned = gtk_vpaned_new();
210 gtk_widget_show(vpaned);
211 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled_window),
214 /* vbox to handle template name and content */
215 vbox1 = gtk_vbox_new(FALSE, 6);
216 gtk_widget_show(vbox1);
217 gtk_container_set_border_width(GTK_CONTAINER(vbox1), 8);
218 gtk_paned_pack1(GTK_PANED(vpaned), vbox1, FALSE, FALSE);
220 table = gtk_table_new(5, 2, FALSE);
221 gtk_table_set_row_spacings (GTK_TABLE (table), VSPACING_NARROW_2);
222 gtk_table_set_col_spacings (GTK_TABLE (table), 4);
223 gtk_widget_show(table);
224 gtk_box_pack_start (GTK_BOX (vbox1), table, FALSE, FALSE, 0);
227 for (i=0; widgets_table[i].label; i++) {
231 label = gtk_label_new(widgets_table[i].label);
232 gtk_widget_show(label);
233 gtk_table_attach(GTK_TABLE(table), label, 0, 1, i, (i + 1),
234 (GtkAttachOptions) (GTK_FILL),
235 (GtkAttachOptions) 0, 0, 0);
236 gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
238 *(widgets_table[i].entry) = gtk_entry_new();
239 gtk_widget_show(*(widgets_table[i].entry));
240 gtk_table_attach(GTK_TABLE(table), *(widgets_table[i].entry), 1, 2, i, (i + 1),
241 (GtkAttachOptions) (GTK_EXPAND|GTK_SHRINK|GTK_FILL),
242 (GtkAttachOptions) 0, 0, 0);
243 CLAWS_SET_TIP(*(widgets_table[i].entry),
244 widgets_table[i].tooltips);
247 /* template content */
248 scroll2 = gtk_scrolled_window_new(NULL, NULL);
249 gtk_widget_show(scroll2);
250 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll2),
251 GTK_POLICY_AUTOMATIC,
252 GTK_POLICY_AUTOMATIC);
253 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll2),
255 gtk_box_pack_start(GTK_BOX(vbox1), scroll2, TRUE, TRUE, 0);
257 text_value = gtk_text_view_new();
258 if (prefs_common.textfont) {
259 PangoFontDescription *font_desc;
261 font_desc = pango_font_description_from_string
262 (prefs_common.textfont);
264 gtk_widget_modify_font(text_value, font_desc);
265 pango_font_description_free(font_desc);
268 gtk_widget_show(text_value);
270 gtk_widget_set_size_request(text_value, -1, 120);
272 gtk_widget_set_size_request(text_value, -1, 60);
274 gtk_container_add(GTK_CONTAINER(scroll2), text_value);
275 gtk_text_view_set_editable(GTK_TEXT_VIEW(text_value), TRUE);
276 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text_value), GTK_WRAP_WORD);
278 /* vbox for buttons and templates list */
279 vbox2 = gtk_vbox_new(FALSE, 6);
280 gtk_widget_show(vbox2);
281 gtk_container_set_border_width(GTK_CONTAINER(vbox2), 8);
282 gtk_paned_pack2(GTK_PANED(vpaned), vbox2, TRUE, FALSE);
284 /* register | substitute | delete */
285 hbox2 = gtk_hbox_new(FALSE, 4);
286 gtk_widget_show(hbox2);
287 gtk_box_pack_start(GTK_BOX(vbox2), hbox2, FALSE, FALSE, 0);
289 arrow1 = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
290 gtk_widget_show(arrow1);
291 gtk_box_pack_start(GTK_BOX(hbox2), arrow1, FALSE, FALSE, 0);
292 gtk_widget_set_size_request(arrow1, -1, 16);
294 hbox3 = gtk_hbox_new(TRUE, 4);
295 gtk_widget_show(hbox3);
296 gtk_box_pack_start(GTK_BOX(hbox2), hbox3, FALSE, FALSE, 0);
298 reg_btn = gtk_button_new_from_stock(GTK_STOCK_ADD);
299 gtk_widget_show(reg_btn);
300 gtk_box_pack_start(GTK_BOX(hbox3), reg_btn, FALSE, TRUE, 0);
301 g_signal_connect(G_OBJECT (reg_btn), "clicked",
302 G_CALLBACK (prefs_template_register_cb), NULL);
303 CLAWS_SET_TIP(reg_btn,
304 _("Append the new template above to the list"));
306 subst_btn = gtkut_get_replace_btn(_("Replace"));
307 gtk_widget_show(subst_btn);
308 gtk_box_pack_start(GTK_BOX(hbox3), subst_btn, FALSE, TRUE, 0);
309 g_signal_connect(G_OBJECT(subst_btn), "clicked",
310 G_CALLBACK(prefs_template_substitute_cb),
312 CLAWS_SET_TIP(subst_btn,
313 _("Replace the selected template in list with the template above"));
315 del_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
316 gtk_widget_show(del_btn);
317 gtk_box_pack_start(GTK_BOX(hbox3), del_btn, FALSE, TRUE, 0);
318 g_signal_connect(G_OBJECT(del_btn), "clicked",
319 G_CALLBACK(prefs_template_delete_cb), NULL);
320 CLAWS_SET_TIP(del_btn,
321 _("Delete the selected template from the list"));
323 clear_btn = gtk_button_new_from_stock (GTK_STOCK_CLEAR);
324 gtk_widget_show (clear_btn);
325 gtk_box_pack_start (GTK_BOX (hbox3), clear_btn, FALSE, TRUE, 0);
326 g_signal_connect(G_OBJECT (clear_btn), "clicked",
327 G_CALLBACK(prefs_template_clear_cb), NULL);
328 CLAWS_SET_TIP(clear_btn,
329 _("Clear all the input fields in the dialog"));
331 #if GTK_CHECK_VERSION(2, 8, 0)
332 desc_btn = gtk_button_new_from_stock(GTK_STOCK_INFO);
334 desc_btn = gtk_button_new_with_label(_(" Symbols... "));
336 gtk_widget_show(desc_btn);
337 gtk_box_pack_end(GTK_BOX(hbox2), desc_btn, FALSE, FALSE, 0);
338 g_signal_connect(G_OBJECT(desc_btn), "clicked",
339 G_CALLBACK(quote_fmt_quote_description), window);
340 CLAWS_SET_TIP(desc_btn,
341 _("Show information on configuring templates"));
344 hbox4 = gtk_hbox_new(FALSE, 8);
345 gtk_widget_show(hbox4);
346 gtk_box_pack_start(GTK_BOX(vbox2), hbox4, TRUE, TRUE, 0);
348 scroll1 = gtk_scrolled_window_new(NULL, NULL);
349 gtk_widget_show(scroll1);
350 gtk_box_pack_start(GTK_BOX(hbox4), scroll1, TRUE, TRUE, 0);
351 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll1),
352 GTK_POLICY_AUTOMATIC,
353 GTK_POLICY_AUTOMATIC);
355 vbox3 = gtk_vbox_new(FALSE, 8);
356 gtk_widget_show(vbox3);
357 gtk_box_pack_start(GTK_BOX(hbox4), vbox3, FALSE, FALSE, 0);
359 top_btn = gtk_button_new_from_stock(GTK_STOCK_GOTO_TOP);
360 gtk_widget_show(top_btn);
361 gtk_box_pack_start(GTK_BOX(vbox3), top_btn, FALSE, FALSE, 0);
362 g_signal_connect(G_OBJECT(top_btn), "clicked",
363 G_CALLBACK(prefs_template_top_cb), NULL);
364 CLAWS_SET_TIP(top_btn,
365 _("Move the selected template to the top"));
367 PACK_VSPACER(vbox3, spc_vbox, VSPACING_NARROW_2);
369 up_btn = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
370 gtk_widget_show(up_btn);
371 gtk_box_pack_start (GTK_BOX(vbox3), up_btn, FALSE, FALSE, 0);
372 g_signal_connect(G_OBJECT(up_btn), "clicked",
373 G_CALLBACK(prefs_template_up_cb), NULL);
374 CLAWS_SET_TIP(up_btn,
375 _("Move the selected template up"));
377 down_btn = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
378 gtk_widget_show (down_btn);
379 gtk_box_pack_start(GTK_BOX (vbox3), down_btn, FALSE, FALSE, 0);
380 g_signal_connect(G_OBJECT (down_btn), "clicked",
381 G_CALLBACK(prefs_template_down_cb), NULL);
382 CLAWS_SET_TIP(down_btn,
383 _("Move the selected template down"));
385 PACK_VSPACER(vbox3, spc_vbox, VSPACING_NARROW_2);
387 bottom_btn = gtk_button_new_from_stock(GTK_STOCK_GOTO_BOTTOM);
388 gtk_widget_show(bottom_btn);
389 gtk_box_pack_start(GTK_BOX(vbox3), bottom_btn, FALSE, FALSE, 0);
390 g_signal_connect(G_OBJECT(bottom_btn), "clicked",
391 G_CALLBACK(prefs_template_bottom_cb), NULL);
392 CLAWS_SET_TIP(bottom_btn,
393 _("Move the selected template to the bottom"));
395 list_view = prefs_template_list_view_create();
396 gtk_widget_show(list_view);
397 gtk_widget_set_size_request(scroll1, -1, 140);
398 gtk_container_add(GTK_CONTAINER(scroll1), list_view);
400 /* help | cancel | ok */
401 gtkut_stock_button_set_create_with_help(&confirm_area, &help_btn,
402 &cancel_btn, GTK_STOCK_CANCEL,
403 &ok_btn, GTK_STOCK_OK,
405 gtk_widget_show(confirm_area);
406 gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
407 gtk_widget_grab_default(ok_btn);
409 gtk_window_set_title(GTK_WINDOW(window), _("Template configuration"));
411 g_signal_connect(G_OBJECT(window), "delete_event",
412 G_CALLBACK(prefs_template_deleted_cb), NULL);
413 g_signal_connect(G_OBJECT(window), "size_allocate",
414 G_CALLBACK(prefs_template_size_allocate_cb), NULL);
415 g_signal_connect(G_OBJECT(window), "key_press_event",
416 G_CALLBACK(prefs_template_key_pressed_cb), NULL);
417 MANAGE_WINDOW_SIGNALS_CONNECT(window);
418 g_signal_connect(G_OBJECT(ok_btn), "clicked",
419 G_CALLBACK(prefs_template_ok_cb), NULL);
420 g_signal_connect(G_OBJECT(cancel_btn), "clicked",
421 G_CALLBACK(prefs_template_cancel_cb), NULL);
422 g_signal_connect(G_OBJECT(help_btn), "clicked",
423 G_CALLBACK(manual_open_with_anchor_cb),
424 MANUAL_ANCHOR_TEMPLATES);
426 if (!geometry.min_height) {
427 geometry.min_width = 500;
428 geometry.min_height = 540;
431 gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
433 gtk_widget_set_size_request(window, prefs_common.templateswin_width,
434 prefs_common.templateswin_height);
436 templates.window = window;
437 templates.ok_btn = ok_btn;
438 templates.list_view = list_view;
439 templates.text_value = text_value;
442 static void prefs_template_reset_dialog(void)
444 GtkTextBuffer *buffer;
446 gtk_entry_set_text(GTK_ENTRY(templates.entry_name), "");
447 gtk_entry_set_text(GTK_ENTRY(templates.entry_from), "");
448 gtk_entry_set_text(GTK_ENTRY(templates.entry_to), "");
449 gtk_entry_set_text(GTK_ENTRY(templates.entry_cc), "");
450 gtk_entry_set_text(GTK_ENTRY(templates.entry_bcc), "");
451 gtk_entry_set_text(GTK_ENTRY(templates.entry_subject), "");
453 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
454 gtk_text_buffer_set_text(buffer, "", -1);
457 static void prefs_template_clear_list(void)
461 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
462 (templates.list_view)));
463 gtk_list_store_clear(store);
465 prefs_template_list_view_insert_template(templates.list_view,
470 static void prefs_template_window_setup(void)
477 manage_window_set_transient(GTK_WINDOW(templates.window));
478 gtk_widget_grab_focus(templates.ok_btn);
480 prefs_template_clear_list();
482 tmpl_list = template_read_config();
484 address_completion_start(templates.window);
486 for (i=0; widgets_table[i].label; i++) {
487 if (widgets_table[i].compl)
488 address_completion_register_entry(
489 GTK_ENTRY(*(widgets_table[i].entry)), TRUE);
492 for (cur = tmpl_list; cur != NULL; cur = cur->next) {
493 tmpl = (Template *)cur->data;
494 prefs_template_list_view_insert_template(templates.list_view,
499 prefs_template_reset_dialog();
501 g_slist_free(tmpl_list);
504 static gint prefs_template_deleted_cb(GtkWidget *widget, GdkEventAny *event,
507 prefs_template_cancel_cb(NULL, NULL);
511 static gboolean prefs_template_key_pressed_cb(GtkWidget *widget,
512 GdkEventKey *event, gpointer data)
514 if (event && event->keyval == GDK_Escape)
515 prefs_template_cancel_cb(NULL, NULL);
517 GtkWidget *focused = gtkut_get_focused_child(
518 GTK_CONTAINER(widget));
519 if (focused && GTK_IS_EDITABLE(focused)) {
526 static gboolean prefs_template_search_func_cb (GtkTreeModel *model, gint column, const gchar *key,
527 GtkTreeIter *iter, gpointer search_data)
534 gtk_tree_model_get (model, iter, column, &store_string, -1);
536 if (!store_string || !key) return FALSE;
538 key_len = strlen (key);
539 retval = (strncmp (key, store_string, key_len) != 0);
541 g_free(store_string);
542 debug_print("selecting row\n");
543 path = gtk_tree_model_get_path(model, iter);
544 prefs_template_select_row(GTK_TREE_VIEW(templates.list_view), path);
545 gtk_tree_path_free(path);
549 static void prefs_template_address_completion_end(void)
553 for (i=0; widgets_table[i].label; i++) {
554 if (widgets_table[i].compl)
555 address_completion_unregister_entry(
556 GTK_ENTRY(*(widgets_table[i].entry)));
558 address_completion_end(templates.window);
561 static void prefs_template_ok_cb(gpointer action, gpointer data)
566 if (modified && alertpanel(_("Entry not saved"),
567 _("The entry was not saved. Close anyway?"),
568 GTK_STOCK_CLOSE, _("+_Continue editing"),
569 NULL) != G_ALERTDEFAULT) {
573 prefs_template_address_completion_end();
576 modified_list = FALSE;
577 tmpl_list = prefs_template_get_list();
578 template_set_config(tmpl_list);
579 compose_reflect_prefs_all();
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);
587 static void prefs_template_cancel_cb(gpointer action, gpointer data)
591 if (modified && alertpanel(_("Entry not saved"),
592 _("The entry was not saved. Close anyway?"),
593 GTK_STOCK_CLOSE, _("+_Continue editing"),
594 NULL) != G_ALERTDEFAULT) {
596 } else if (modified_list && alertpanel(_("Templates list not saved"),
597 _("The templates list has been modified. Close anyway?"),
598 GTK_STOCK_CLOSE, _("+_Continue editing"),
599 NULL) != G_ALERTDEFAULT) {
603 prefs_template_address_completion_end();
606 modified_list = FALSE;
607 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
608 (templates.list_view)));
609 gtk_list_store_clear(store);
610 gtk_widget_hide(templates.window);
615 *\brief Request list for storage. New list is owned
618 static GSList *prefs_template_get_list(void)
620 GSList *tmpl_list = NULL;
625 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
626 if (!gtk_tree_model_get_iter_first(model, &iter))
630 gtk_tree_model_get(model, &iter,
637 ntmpl = g_new(Template, 1);
638 ntmpl->load_filename = NULL;
639 ntmpl->name = tmpl->name && *(tmpl->name)
640 ? g_strdup(tmpl->name)
642 ntmpl->subject = tmpl->subject && *(tmpl->subject)
643 ? g_strdup(tmpl->subject)
645 ntmpl->from = tmpl->from && *(tmpl->from)
646 ? g_strdup(tmpl->from)
648 ntmpl->to = tmpl->to && *(tmpl->to)
651 ntmpl->cc = tmpl->cc && *(tmpl->cc)
654 ntmpl->bcc = tmpl->bcc && *(tmpl->bcc)
655 ? g_strdup(tmpl->bcc)
657 ntmpl->value = tmpl->value && *(tmpl->value)
658 ? g_strdup(tmpl->value)
660 tmpl_list = g_slist_append(tmpl_list, ntmpl);
663 } while (gtk_tree_model_iter_next(model, &iter));
668 gboolean prefs_template_string_is_valid(gchar *string, gint *line, gboolean escaped_string, gboolean email)
670 gboolean result = TRUE;
671 if (string && *string != '\0') {
675 PrefsAccount *account = account_get_default();
677 if (escaped_string) {
678 tmp = malloc(strlen(string)+1);
679 pref_get_unescaped_pref(tmp, string);
681 tmp = g_strdup(string);
683 memset(&dummyinfo, 0, sizeof(MsgInfo));
684 /* init dummy fields, so we can test the result of the parse */
685 dummyinfo.date="Sat, 30 May 2009 01:23:45 +0200";
686 dummyinfo.fromname="John Doe";
687 dummyinfo.from="John Doe <john@example.com>";
688 dummyinfo.to="John Doe <john@example.com>";
689 dummyinfo.cc="John Doe <john@example.com>";
690 dummyinfo.msgid="<1234john@example.com>";
691 dummyinfo.inreplyto="<1234john@example.com>";
692 dummyinfo.newsgroups="alt.test";
693 dummyinfo.subject="subject";
697 quote_fmt_init(&dummyinfo, NULL, NULL, TRUE, account, FALSE, NULL);
699 quote_fmt_init(&dummyinfo, NULL, NULL, TRUE, account, FALSE);
701 quote_fmt_scan_string(tmp);
704 parsed_buf = quote_fmt_get_buffer();
707 *line = quote_fmt_get_line();
711 const gchar *start = strrchr(parsed_buf, '<');
712 const gchar *end = strrchr(parsed_buf, '>');
713 const gchar *at = strrchr(parsed_buf, '@');
714 const gchar *space = strrchr(parsed_buf, ' ');
717 if (at && space && (!start || !end || end < start || start < space))
720 quote_fmt_reset_vartable();
725 static gboolean prefs_template_list_view_set_row(gint row)
726 /* return TRUE if the row could be modified */
736 GtkTextBuffer *buffer;
737 GtkTextIter start, end;
741 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
743 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
744 gtk_text_buffer_get_start_iter(buffer, &start);
745 gtk_text_buffer_get_iter_at_offset(buffer, &end, -1);
746 value = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
748 if (value && *value == '\0') {
752 if (!prefs_template_string_is_valid(value, &line, TRUE, FALSE)) {
753 alertpanel_error(_("The body of the template has an error at line %d."), line);
758 name = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_name),
761 alertpanel_error(_("The template's name is not set."));
765 from = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_from),
767 to = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_to),
769 cc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_cc),
771 bcc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_bcc),
773 subject = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_subject),
776 if (from && *from == '\0') {
780 if (to && *to == '\0') {
784 if (cc && *cc == '\0') {
788 if (bcc && *bcc == '\0') {
792 if (subject && *subject == '\0') {
797 if (!prefs_template_string_is_valid(from, NULL, TRUE, TRUE)) {
798 alertpanel_error(_("The \"From\" field of the template contains an invalid email address."));
803 if (!prefs_template_string_is_valid(to, NULL, TRUE, TRUE)) {
804 alertpanel_error(_("The \"To\" field of the template contains an invalid email address."));
809 if (!prefs_template_string_is_valid(cc, NULL, TRUE, TRUE)) {
810 alertpanel_error(_("The \"Cc\" field of the template contains an invalid email address."));
815 if (!prefs_template_string_is_valid(bcc, NULL, TRUE, TRUE)) {
816 alertpanel_error(_("The \"Bcc\" field of the template contains an invalid email address."));
821 if (!prefs_template_string_is_valid(subject, NULL, TRUE, FALSE)) {
822 alertpanel_error(_("The \"Subject\" field of the template is invalid."));
828 tmpl = g_new(Template, 1);
829 tmpl->load_filename = NULL;
831 tmpl->subject = subject;
838 prefs_template_list_view_insert_template(templates.list_view,
839 row, tmpl->name, tmpl);
844 static void prefs_template_register_cb(gpointer action, gpointer data)
846 modified = !prefs_template_list_view_set_row(-1);
847 modified_list = TRUE;
850 static void prefs_template_substitute_cb(gpointer action, gpointer data)
857 row = gtkut_list_view_get_selected_row(templates.list_view);
861 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
862 if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
865 gtk_tree_model_get(model, &iter, TEMPL_DATA, &tmpl, -1);
869 modified = !prefs_template_list_view_set_row(row);
870 modified_list = TRUE;
873 static void prefs_template_delete_cb(gpointer action, gpointer data)
880 row = gtkut_list_view_get_selected_row(templates.list_view);
884 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
885 if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
888 gtk_tree_model_get(model, &iter, TEMPL_DATA, &tmpl, -1);
892 if (alertpanel(_("Delete template"),
893 _("Do you really want to delete this template?"),
894 GTK_STOCK_CANCEL, GTK_STOCK_DELETE,
895 NULL) != G_ALERTALTERNATE)
898 gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
899 modified_list = TRUE;
902 static void prefs_template_delete_all_cb(gpointer action, gpointer data)
904 GtkListStore *list_store;
906 if (alertpanel(_("Delete all templates"),
907 _("Do you really want to delete all the templates?"),
908 GTK_STOCK_CANCEL, "+"GTK_STOCK_DELETE, NULL) == G_ALERTDEFAULT)
911 list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view)));
912 prefs_template_clear_list();
915 prefs_template_reset_dialog();
916 modified_list = TRUE;
919 static void prefs_template_duplicate_cb(gpointer action, gpointer data)
926 row = gtkut_list_view_get_selected_row(templates.list_view);
930 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
931 if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
934 gtk_tree_model_get(model, &iter, TEMPL_DATA, &tmpl, -1);
938 modified_list = !prefs_template_list_view_set_row(-row-2);
941 static void prefs_template_clear_cb(gpointer action, gpointer data)
943 prefs_template_reset_dialog();
946 static void prefs_template_top_cb(gpointer action, gpointer data)
949 GtkTreeIter top, sel;
952 row = gtkut_list_view_get_selected_row(templates.list_view);
956 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
958 if (!gtk_tree_model_iter_nth_child(model, &top, NULL, 0)
959 || !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
962 gtk_list_store_move_after(GTK_LIST_STORE(model), &sel, &top);
963 gtkut_list_view_select_row(templates.list_view, 1);
964 modified_list = TRUE;
967 static void prefs_template_up_cb(gpointer action, gpointer data)
970 GtkTreeIter top, sel;
973 row = gtkut_list_view_get_selected_row(templates.list_view);
977 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
979 if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row - 1)
980 || !gtk_tree_model_iter_nth_child(model, &sel, NULL, row))
983 gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
984 gtkut_list_view_select_row(templates.list_view, row - 1);
985 modified_list = TRUE;
988 static void prefs_template_down_cb(gpointer action, gpointer data)
991 GtkTreeIter top, sel;
994 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
995 n_rows = gtk_tree_model_iter_n_children(model, NULL);
996 row = gtkut_list_view_get_selected_row(templates.list_view);
997 if (row < 1 || row >= n_rows - 1)
1000 if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
1001 || !gtk_tree_model_iter_nth_child(model, &sel, NULL, row + 1))
1004 gtk_list_store_swap(GTK_LIST_STORE(model), &top, &sel);
1005 gtkut_list_view_select_row(templates.list_view, row + 1);
1006 modified_list = TRUE;
1009 static void prefs_template_bottom_cb(gpointer action, gpointer data)
1012 GtkTreeIter top, sel;
1013 GtkTreeModel *model;
1015 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
1016 n_rows = gtk_tree_model_iter_n_children(model, NULL);
1017 row = gtkut_list_view_get_selected_row(templates.list_view);
1018 if (row < 1 || row >= n_rows - 1)
1021 if (!gtk_tree_model_iter_nth_child(model, &top, NULL, row)
1022 || !gtk_tree_model_iter_nth_child(model, &sel, NULL, n_rows - 1))
1025 gtk_list_store_move_after(GTK_LIST_STORE(model), &top, &sel);
1026 gtkut_list_view_select_row(templates.list_view, n_rows - 1);
1027 modified_list = TRUE;
1030 static GtkListStore* prefs_template_create_data_store(void)
1032 return gtk_list_store_new(N_TEMPL_COLUMNS,
1035 G_TYPE_AUTO_POINTER,
1039 static void prefs_template_list_view_insert_template(GtkWidget *list_view,
1041 const gchar *template,
1045 GtkTreeIter sibling;
1046 GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
1047 (GTK_TREE_VIEW(list_view)));
1049 /* row -1 to add a new rule to store,
1050 row >=0 to change an existing row
1051 row <-1 insert a new row after (-row-2)
1054 /* modify the existing */
1055 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
1058 } else if (row < -1 ) {
1059 if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store),
1060 &sibling, NULL, -row-2))
1066 gtk_list_store_append(list_store, &iter);
1067 gtk_list_store_set(list_store, &iter,
1068 TEMPL_TEXT, template,
1071 } else if (row < -1) {
1073 gtk_list_store_insert_after(list_store, &iter, &sibling);
1074 gtk_list_store_set(list_store, &iter,
1075 TEMPL_TEXT, template,
1079 /* change existing */
1081 g_auto_pointer_new_with_free(data, (GFreeFunc) template_free);
1083 /* if replacing data in an existing row, the auto pointer takes care
1084 * of destroying the Template data */
1085 gtk_list_store_set(list_store, &iter,
1086 TEMPL_TEXT, template,
1088 TEMPL_AUTO_DATA, auto_data,
1091 g_auto_pointer_free(auto_data);
1095 static GtkActionGroup *prefs_template_popup_action = NULL;
1096 static GtkWidget *prefs_template_popup_menu = NULL;
1098 static GtkActionEntry prefs_template_popup_entries[] =
1100 {"PrefsTemplatePopup", NULL, "PrefsTemplatePopup" },
1101 {"PrefsTemplatePopup/Delete", NULL, N_("_Delete"), NULL, NULL, G_CALLBACK(prefs_template_delete_cb) },
1102 {"PrefsTemplatePopup/DeleteAll", NULL, N_("Delete _all"), NULL, NULL, G_CALLBACK(prefs_template_delete_all_cb) },
1103 {"PrefsTemplatePopup/Duplicate", NULL, N_("D_uplicate"), NULL, NULL, G_CALLBACK(prefs_template_duplicate_cb) },
1106 static gint prefs_template_list_btn_pressed(GtkWidget *widget, GdkEventButton *event,
1107 GtkTreeView *list_view)
1110 /* left- or right-button click */
1111 if (event->button == 1 || event->button == 3) {
1112 GtkTreePath *path = NULL;
1113 if (gtk_tree_view_get_path_at_pos( list_view, event->x, event->y,
1114 &path, NULL, NULL, NULL)) {
1115 prefs_template_select_row(list_view, path);
1118 gtk_tree_path_free(path);
1121 /* right-button click */
1122 if (event->button == 3) {
1123 GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1128 if (!prefs_template_popup_menu) {
1129 prefs_template_popup_action = cm_menu_create_action_group("PrefsTemplatePopup", prefs_template_popup_entries,
1130 G_N_ELEMENTS(prefs_template_popup_entries), (gpointer)list_view);
1131 MENUITEM_ADDUI("/Menus", "PrefsTemplatePopup", "PrefsTemplatePopup", GTK_UI_MANAGER_MENU)
1132 MENUITEM_ADDUI("/Menus/PrefsTemplatePopup", "Delete", "PrefsTemplatePopup/Delete", GTK_UI_MANAGER_MENUITEM)
1133 MENUITEM_ADDUI("/Menus/PrefsTemplatePopup", "DeleteAll", "PrefsTemplatePopup/DeleteAll", GTK_UI_MANAGER_MENUITEM)
1134 MENUITEM_ADDUI("/Menus/PrefsTemplatePopup", "Duplicate", "PrefsTemplatePopup/Duplicate", GTK_UI_MANAGER_MENUITEM)
1135 prefs_template_popup_menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
1136 gtk_ui_manager_get_widget(gtkut_ui_manager(), "/Menus/PrefsTemplatePopup")) );
1139 /* grey out some popup menu items if there is no selected row */
1140 row = gtkut_list_view_get_selected_row(GTK_WIDGET(list_view));
1141 cm_menu_set_sensitive("PrefsTemplatePopup/Delete", (row > 0));
1142 cm_menu_set_sensitive("PrefsTemplatePopup/Duplicate", (row > 0));
1144 /* grey out seom popup menu items if there is no row
1145 (not counting the (New) one at row 0) */
1146 non_empty = gtk_tree_model_get_iter_first(model, &iter);
1148 non_empty = gtk_tree_model_iter_next(model, &iter);
1149 cm_menu_set_sensitive("PrefsTemplatePopup/DeleteAll", non_empty);
1151 gtk_menu_popup(GTK_MENU(prefs_template_popup_menu),
1152 NULL, NULL, NULL, NULL,
1153 event->button, event->time);
1159 static gboolean prefs_template_list_popup_menu(GtkWidget *widget, gpointer data)
1161 GtkTreeView *list_view = (GtkTreeView *)data;
1162 GdkEventButton event;
1165 event.time = gtk_get_current_event_time();
1167 prefs_template_list_btn_pressed(NULL, &event, list_view);
1172 static GtkWidget *prefs_template_list_view_create(void)
1174 GtkTreeView *list_view;
1175 GtkTreeSelection *selector;
1176 GtkTreeModel *model;
1178 model = GTK_TREE_MODEL(prefs_template_create_data_store());
1179 list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
1180 g_object_unref(model);
1183 g_signal_connect(G_OBJECT(list_view), "popup-menu",
1184 G_CALLBACK(prefs_template_list_popup_menu), list_view);
1186 gtk_widget_tap_and_hold_setup(GTK_WIDGET(list_view), NULL, NULL,
1187 GTK_TAP_AND_HOLD_NONE | GTK_TAP_AND_HOLD_NO_INTERNALS);
1188 g_signal_connect(G_OBJECT(list_view), "tap-and-hold",
1189 G_CALLBACK(prefs_template_list_popup_menu), list_view);
1191 g_signal_connect(G_OBJECT(list_view), "button-press-event",
1192 G_CALLBACK(prefs_template_list_btn_pressed), list_view);
1194 gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
1195 gtk_tree_view_set_reorderable(list_view, TRUE);
1197 selector = gtk_tree_view_get_selection(list_view);
1198 gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1200 /* create the columns */
1201 prefs_template_create_list_view_columns(GTK_WIDGET(list_view));
1203 return GTK_WIDGET(list_view);
1206 static void prefs_template_create_list_view_columns(GtkWidget *list_view)
1208 GtkTreeViewColumn *column;
1209 GtkCellRenderer *renderer;
1211 renderer = gtk_cell_renderer_text_new();
1212 column = gtk_tree_view_column_new_with_attributes
1213 (_("Current templates"),
1217 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1218 gtk_tree_view_set_search_equal_func(GTK_TREE_VIEW(list_view), prefs_template_search_func_cb , NULL, NULL);
1222 *\brief Triggered when a row has to be selected
1224 static void prefs_template_select_row(GtkTreeView *list_view, GtkTreePath *path)
1226 GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1227 GtkTreeSelection *selection;
1230 GtkTextBuffer *buffer;
1234 if (!model || !path || !gtk_tree_model_get_iter(model, &titer, path))
1238 selection = gtk_tree_view_get_selection(list_view);
1239 gtk_tree_selection_select_path(selection, path);
1241 tmpl_def.name = _("Template");
1242 tmpl_def.subject = "";
1247 tmpl_def.value = "";
1249 gtk_tree_model_get(model, &titer, TEMPL_DATA, &tmpl, -1);
1253 gtk_entry_set_text(GTK_ENTRY(templates.entry_name), tmpl->name);
1254 gtk_entry_set_text(GTK_ENTRY(templates.entry_from),
1255 tmpl->from ? tmpl->from : "");
1256 gtk_entry_set_text(GTK_ENTRY(templates.entry_to),
1257 tmpl->to ? tmpl->to : "");
1258 gtk_entry_set_text(GTK_ENTRY(templates.entry_cc),
1259 tmpl->cc ? tmpl->cc : "");
1260 gtk_entry_set_text(GTK_ENTRY(templates.entry_bcc),
1261 tmpl->bcc ? tmpl->bcc : "");
1262 gtk_entry_set_text(GTK_ENTRY(templates.entry_subject),
1263 tmpl->subject ? tmpl->subject : "");
1265 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
1266 gtk_text_buffer_set_text(buffer, "", -1);
1267 gtk_text_buffer_get_start_iter(buffer, &iter);
1268 gtk_text_buffer_insert(buffer, &iter, tmpl->value, -1);