2 * Sylpheed templates subsystem
3 * Copyright (C) 2001 Alexander Barinov
4 * Copyright (C) 2001-2006 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 2 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, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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"
48 TEMPL_AUTO_DATA, /*!< auto pointer */
52 static struct Templates {
56 GtkWidget *entry_name;
57 GtkWidget *entry_subject;
61 GtkWidget *text_value;
64 static int modified = FALSE;
66 /* widget creating functions */
67 static void prefs_template_window_create (void);
68 static void prefs_template_window_setup (void);
69 static void prefs_template_clear (void);
71 static GSList *prefs_template_get_list (void);
74 static gint prefs_template_deleted_cb (GtkWidget *widget,
77 static gboolean prefs_template_key_pressed_cb (GtkWidget *widget,
80 static void prefs_template_cancel_cb (void);
81 static void prefs_template_ok_cb (void);
82 static void prefs_template_register_cb (void);
83 static void prefs_template_substitute_cb (void);
84 static void prefs_template_delete_cb (void);
86 static GtkListStore* prefs_template_create_data_store (void);
87 static void prefs_template_list_view_insert_template (GtkWidget *list_view,
88 GtkTreeIter *row_iter,
89 const gchar *template,
92 static GtkWidget *prefs_template_list_view_create (void);
93 static void prefs_template_create_list_view_columns (GtkWidget *list_view);
94 static gboolean prefs_template_selected (GtkTreeSelection *selector,
97 gboolean currently_selected,
100 /* Called from mainwindow.c */
101 void prefs_template_open(void)
105 if (!templates.window)
106 prefs_template_window_create();
108 prefs_template_window_setup();
109 gtk_widget_show(templates.window);
113 *\brief Save Gtk object size to prefs dataset
115 static void prefs_template_size_allocate_cb(GtkWidget *widget,
116 GtkAllocation *allocation)
118 g_return_if_fail(allocation != NULL);
120 prefs_common.templateswin_width = allocation->width;
121 prefs_common.templateswin_height = allocation->height;
124 static void prefs_template_window_create(void)
126 /* window structure ;) */
130 GtkWidget *table; /* including : entry_[name|to|cc|bcc|subject] */
132 GtkWidget *text_value;
138 GtkWidget *subst_btn;
142 GtkWidget *list_view;
143 GtkWidget *confirm_area;
145 GtkWidget *cancel_btn;
147 static GdkGeometry geometry;
149 GtkTooltips *tooltips;
151 debug_print("Creating templates configuration window...\n");
154 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
155 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
156 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
157 gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
159 /* vpaned to separate template settings from templates list */
160 vpaned = gtk_vpaned_new();
161 gtk_widget_show(vpaned);
162 gtk_container_add(GTK_CONTAINER(window), vpaned);
164 /* vbox to handle template name and content */
165 vbox1 = gtk_vbox_new(FALSE, 6);
166 gtk_widget_show(vbox1);
167 gtk_container_set_border_width(GTK_CONTAINER(vbox1), 8);
168 gtk_paned_pack1(GTK_PANED(vpaned), vbox1, FALSE, FALSE);
170 table = gtk_table_new(5, 2, FALSE);
171 gtk_table_set_row_spacings (GTK_TABLE (table), VSPACING_NARROW_2);
172 gtk_table_set_col_spacings (GTK_TABLE (table), 4);
173 gtk_widget_show(table);
174 gtk_box_pack_start (GTK_BOX (vbox1), table, FALSE, FALSE, 0);
176 tooltips = gtk_tooltips_new();
185 {_("Name"), &templates.entry_name, FALSE,
186 _("This name is used as the Menu item")},
187 {_("To"), &templates.entry_to, TRUE, NULL},
188 {_("Cc"), &templates.entry_cc, TRUE, NULL},
189 {_("Bcc"), &templates.entry_bcc, TRUE, NULL},
190 {_("Subject"), &templates.entry_subject, FALSE, NULL},
191 {NULL, NULL, FALSE, NULL}
194 for (i=0; tab[i].label; i++) {
198 label = gtk_label_new(tab[i].label);
199 gtk_widget_show(label);
200 gtk_table_attach(GTK_TABLE(table), label, 0, 1, i, (i + 1),
201 (GtkAttachOptions) (GTK_FILL),
202 (GtkAttachOptions) 0, 0, 0);
203 gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
205 *(tab[i].entry) = gtk_entry_new();
206 gtk_widget_show(*(tab[i].entry));
207 gtk_table_attach(GTK_TABLE(table), *(tab[i].entry), 1, 2, i, (i + 1),
208 (GtkAttachOptions) (GTK_EXPAND|GTK_SHRINK|GTK_FILL),
209 (GtkAttachOptions) 0, 0, 0);
210 gtk_tooltips_set_tip(tooltips, *(tab[i].entry), tab[i].tooltips, NULL);
213 address_completion_register_entry(
214 GTK_ENTRY(*(tab[i].entry)), TRUE);
217 /* template content */
218 scroll2 = gtk_scrolled_window_new(NULL, NULL);
219 gtk_widget_show(scroll2);
220 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll2),
221 GTK_POLICY_AUTOMATIC,
222 GTK_POLICY_AUTOMATIC);
223 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll2),
225 gtk_box_pack_start(GTK_BOX(vbox1), scroll2, TRUE, TRUE, 0);
227 text_value = gtk_text_view_new();
228 gtk_widget_show(text_value);
229 gtk_widget_set_size_request(text_value, -1, 120);
230 gtk_container_add(GTK_CONTAINER(scroll2), text_value);
231 gtk_text_view_set_editable(GTK_TEXT_VIEW(text_value), TRUE);
232 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text_value), GTK_WRAP_WORD);
234 /* vbox for buttons and templates list */
235 vbox2 = gtk_vbox_new(FALSE, 6);
236 gtk_widget_show(vbox2);
237 gtk_container_set_border_width(GTK_CONTAINER(vbox2), 8);
238 gtk_paned_pack2(GTK_PANED(vpaned), vbox2, TRUE, FALSE);
240 /* register | substitute | delete */
241 hbox2 = gtk_hbox_new(FALSE, 4);
242 gtk_widget_show(hbox2);
243 gtk_box_pack_start(GTK_BOX(vbox2), hbox2, FALSE, FALSE, 0);
245 arrow1 = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
246 gtk_widget_show(arrow1);
247 gtk_box_pack_start(GTK_BOX(hbox2), arrow1, FALSE, FALSE, 0);
248 gtk_widget_set_size_request(arrow1, -1, 16);
250 hbox3 = gtk_hbox_new(TRUE, 4);
251 gtk_widget_show(hbox3);
252 gtk_box_pack_start(GTK_BOX(hbox2), hbox3, FALSE, FALSE, 0);
254 reg_btn = gtk_button_new_from_stock(GTK_STOCK_ADD);
255 gtk_widget_show(reg_btn);
256 gtk_box_pack_start(GTK_BOX(hbox3), reg_btn, FALSE, TRUE, 0);
257 g_signal_connect(G_OBJECT (reg_btn), "clicked",
258 G_CALLBACK (prefs_template_register_cb), NULL);
260 subst_btn = gtkut_get_replace_btn(_("Replace"));
261 gtk_widget_show(subst_btn);
262 gtk_box_pack_start(GTK_BOX(hbox3), subst_btn, FALSE, TRUE, 0);
263 g_signal_connect(G_OBJECT(subst_btn), "clicked",
264 G_CALLBACK(prefs_template_substitute_cb),
267 del_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
268 gtk_widget_show(del_btn);
269 gtk_box_pack_start(GTK_BOX(hbox3), del_btn, FALSE, TRUE, 0);
270 g_signal_connect(G_OBJECT(del_btn), "clicked",
271 G_CALLBACK(prefs_template_delete_cb), NULL);
273 #if GTK_CHECK_VERSION(2, 8, 0)
274 desc_btn = gtk_button_new_from_stock(GTK_STOCK_INFO);
276 desc_btn = gtk_button_new_with_label(_(" Symbols... "));
278 gtk_widget_show(desc_btn);
279 gtk_box_pack_end(GTK_BOX(hbox2), desc_btn, FALSE, FALSE, 0);
280 g_signal_connect(G_OBJECT(desc_btn), "clicked",
281 G_CALLBACK(quote_fmt_quote_description), NULL);
284 scroll1 = gtk_scrolled_window_new(NULL, NULL);
285 gtk_widget_show(scroll1);
286 gtk_box_pack_start(GTK_BOX(vbox2), scroll1, TRUE, TRUE, 0);
287 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll1),
288 GTK_POLICY_AUTOMATIC,
289 GTK_POLICY_AUTOMATIC);
291 list_view = prefs_template_list_view_create();
292 gtk_widget_show(list_view);
293 gtk_widget_set_size_request(scroll1, -1, 140);
294 gtk_container_add(GTK_CONTAINER(scroll1), list_view);
296 /* help | cancel | ok */
297 gtkut_stock_button_set_create_with_help(&confirm_area, &help_btn,
298 &cancel_btn, GTK_STOCK_CANCEL,
299 &ok_btn, GTK_STOCK_OK,
301 gtk_widget_show(confirm_area);
302 gtk_box_pack_end(GTK_BOX(vbox2), confirm_area, FALSE, FALSE, 0);
303 gtk_widget_grab_default(ok_btn);
305 gtk_window_set_title(GTK_WINDOW(window), _("Template configuration"));
307 g_signal_connect(G_OBJECT(window), "delete_event",
308 G_CALLBACK(prefs_template_deleted_cb), NULL);
309 g_signal_connect(G_OBJECT(window), "size_allocate",
310 G_CALLBACK(prefs_template_size_allocate_cb), NULL);
311 g_signal_connect(G_OBJECT(window), "key_press_event",
312 G_CALLBACK(prefs_template_key_pressed_cb), NULL);
313 MANAGE_WINDOW_SIGNALS_CONNECT(window);
314 g_signal_connect(G_OBJECT(ok_btn), "clicked",
315 G_CALLBACK(prefs_template_ok_cb), NULL);
316 g_signal_connect(G_OBJECT(cancel_btn), "clicked",
317 G_CALLBACK(prefs_template_cancel_cb), NULL);
318 g_signal_connect(G_OBJECT(help_btn), "clicked",
319 G_CALLBACK(manual_open_with_anchor_cb),
320 MANUAL_ANCHOR_TEMPLATES);
322 address_completion_start(window);
324 if (!geometry.min_height) {
325 geometry.min_width = 440;
326 geometry.min_height = 500;
329 gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
331 gtk_widget_set_size_request(window, prefs_common.templateswin_width,
332 prefs_common.templateswin_height);
334 templates.window = window;
335 templates.ok_btn = ok_btn;
336 templates.list_view = list_view;
337 templates.text_value = text_value;
340 static void prefs_template_window_setup(void)
346 GtkTextBuffer *buffer;
348 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
349 (templates.list_view)));
351 manage_window_set_transient(GTK_WINDOW(templates.window));
352 gtk_widget_grab_focus(templates.ok_btn);
354 gtk_list_store_clear(store);
356 prefs_template_list_view_insert_template(templates.list_view,
360 tmpl_list = template_read_config();
362 for (cur = tmpl_list; cur != NULL; cur = cur->next) {
363 tmpl = (Template *)cur->data;
364 prefs_template_list_view_insert_template(templates.list_view,
369 gtk_entry_set_text(GTK_ENTRY(templates.entry_name), "");
370 gtk_entry_set_text(GTK_ENTRY(templates.entry_to), "");
371 gtk_entry_set_text(GTK_ENTRY(templates.entry_cc), "");
372 gtk_entry_set_text(GTK_ENTRY(templates.entry_bcc), "");
373 gtk_entry_set_text(GTK_ENTRY(templates.entry_subject), "");
375 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
376 gtk_text_buffer_set_text(buffer, "", -1);
378 g_slist_free(tmpl_list);
381 static void prefs_template_clear(void)
385 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
386 (templates.list_view)));
387 gtk_list_store_clear(store);
390 static gint prefs_template_deleted_cb(GtkWidget *widget, GdkEventAny *event,
393 prefs_template_cancel_cb();
397 static gboolean prefs_template_key_pressed_cb(GtkWidget *widget,
398 GdkEventKey *event, gpointer data)
400 if (event && event->keyval == GDK_Escape)
401 prefs_template_cancel_cb();
403 GtkWidget *focused = gtkut_get_focused_child(
404 GTK_CONTAINER(widget));
405 if (focused && GTK_IS_EDITABLE(focused)) {
412 static void prefs_template_ok_cb(void)
416 if (modified && alertpanel(_("Entry not saved"),
417 _("The entry was not saved. Close anyway?"),
418 GTK_STOCK_CLOSE, _("+_Continue editing"),
419 NULL) != G_ALERTDEFAULT) {
423 tmpl_list = prefs_template_get_list();
424 template_set_config(tmpl_list);
425 compose_reflect_prefs_all();
426 prefs_template_clear();
427 gtk_widget_hide(templates.window);
431 static void prefs_template_cancel_cb(void)
433 if (modified && alertpanel(_("Entry not saved"),
434 _("The entry was not saved. Close anyway?"),
435 GTK_STOCK_CLOSE, _("+_Continue editing"),
436 NULL) != G_ALERTDEFAULT) {
440 prefs_template_clear();
441 gtk_widget_hide(templates.window);
446 *\brief Request list for storage. New list is owned
449 static GSList *prefs_template_get_list(void)
451 GSList *tmpl_list = NULL;
456 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
457 if (!gtk_tree_model_get_iter_first(model, &iter))
461 gtk_tree_model_get(model, &iter,
468 ntmpl = g_new(Template, 1);
469 ntmpl->name = tmpl->name && *(tmpl->name)
470 ? g_strdup(tmpl->name)
472 ntmpl->subject = tmpl->subject && *(tmpl->subject)
473 ? g_strdup(tmpl->subject)
475 ntmpl->to = tmpl->to && *(tmpl->to)
478 ntmpl->cc = tmpl->cc && *(tmpl->cc)
481 ntmpl->bcc = tmpl->bcc && *(tmpl->bcc)
482 ? g_strdup(tmpl->bcc)
484 ntmpl->value = tmpl->value && *(tmpl->value)
485 ? g_strdup(tmpl->value)
487 tmpl_list = g_slist_append(tmpl_list, ntmpl);
490 } while (gtk_tree_model_iter_next(model, &iter));
495 gboolean prefs_template_string_is_valid(gchar *string)
497 if (string && *string != '\0') {
501 memset(&dummyinfo, 0, sizeof(MsgInfo));
502 quote_fmt_init(&dummyinfo, NULL, NULL, TRUE, NULL);
503 quote_fmt_scan_string(string);
505 parsed_buf = quote_fmt_get_buffer();
508 quote_fmt_reset_vartable();
513 static gboolean prefs_template_list_view_set_row(GtkTreeIter *row)
514 /* return TRUE if the row could be modified */
523 GtkTextBuffer *buffer;
524 GtkTextIter start, end;
527 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
529 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
530 gtk_text_buffer_get_start_iter(buffer, &start);
531 gtk_text_buffer_get_iter_at_offset(buffer, &end, -1);
532 value = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
534 if (value && *value == '\0') {
538 if (!prefs_template_string_is_valid(value)) {
539 alertpanel_error(_("Template body format error."));
544 name = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_name),
547 alertpanel_error(_("Template name is not set."));
550 to = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_to),
552 cc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_cc),
554 bcc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_bcc),
556 subject = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_subject),
559 if (to && *to == '\0') {
563 if (cc && *cc == '\0') {
567 if (bcc && *bcc == '\0') {
571 if (subject && *subject == '\0') {
576 if (!prefs_template_string_is_valid(to)) {
577 alertpanel_error(_("Template To format error."));
581 if (!prefs_template_string_is_valid(cc)) {
582 alertpanel_error(_("Template Cc format error."));
586 if (!prefs_template_string_is_valid(bcc)) {
587 alertpanel_error(_("Template Bcc format error."));
591 if (!prefs_template_string_is_valid(subject)) {
592 alertpanel_error(_("Template subject format error."));
597 tmpl = g_new(Template, 1);
599 tmpl->subject = subject;
605 prefs_template_list_view_insert_template(templates.list_view,
606 row, tmpl->name, tmpl, TRUE);
610 static void prefs_template_register_cb(void)
612 modified = !prefs_template_list_view_set_row(NULL);
615 static void prefs_template_substitute_cb(void)
619 GtkTreeSelection *selection;
622 selection = gtk_tree_view_get_selection
623 (GTK_TREE_VIEW(templates.list_view));
625 if (!gtk_tree_selection_get_selected(selection, &model, &row))
628 gtk_tree_model_get(model, &row,
634 modified = !prefs_template_list_view_set_row(&row);
637 static void prefs_template_delete_cb(void)
641 GtkTreeSelection *selection;
644 selection = gtk_tree_view_get_selection
645 (GTK_TREE_VIEW(templates.list_view));
647 if (!gtk_tree_selection_get_selected(selection, &model, &row))
650 gtk_tree_model_get(model, &row,
657 if (alertpanel(_("Delete template"),
658 _("Do you really want to delete this template?"),
659 GTK_STOCK_CANCEL, GTK_STOCK_DELETE,
660 NULL) != G_ALERTALTERNATE)
663 gtk_list_store_remove(GTK_LIST_STORE(model), &row);
666 static GtkListStore* prefs_template_create_data_store(void)
668 return gtk_list_store_new(N_TEMPL_COLUMNS,
675 static gboolean prefs_template_list_view_find_insertion_point(GtkTreeModel* model,
676 const gchar* template,
682 if ((gtk_tree_model_get_iter_first(model, &i))) {
683 for ( ; gtk_tree_model_iter_next(model, &i); ) {
684 gtk_tree_model_get(model, &i, TEMPL_TEXT, &text, -1);
685 if (strcmp(text, template) >= 0) {
693 /* cannot add before (need to append) */
697 static void prefs_template_list_view_insert_template(GtkWidget *list_view,
698 GtkTreeIter *row_iter,
699 const gchar *template,
704 GtkTreeModel *model = gtk_tree_view_get_model
705 (GTK_TREE_VIEW(list_view));
706 GtkListStore *list_store = GTK_LIST_STORE(model);
709 if (row_iter == NULL) {
711 GtkTreeIter insertion_point;
714 if (prefs_template_list_view_find_insertion_point(model,
715 template, &insertion_point)) {
716 gtk_list_store_insert_before(list_store, &iter,
719 gtk_list_store_append(list_store, &iter);
723 gtk_list_store_append(list_store, &iter);
726 /* modify the existing */
730 GtkTreeIter insertion_point;
733 /* force re-sorting if template's name changed */
734 gtk_tree_model_get(model, row_iter, TEMPL_TEXT, &text, -1);
735 if (strcmp(text, template) != 0) {
737 /* move the modified template */
738 if (prefs_template_list_view_find_insertion_point(model,
739 template, &insertion_point)) {
740 gtk_list_store_move_before(list_store, row_iter,
743 /* move to the end */
744 gtk_list_store_move_before(list_store, row_iter,
752 auto_data = g_auto_pointer_new_with_free(data, (GFreeFunc) template_free);
754 /* if replacing data in an existing row, the auto pointer takes care
755 * of destroying the Template data */
756 gtk_list_store_set(list_store, &iter,
757 TEMPL_TEXT, template,
759 TEMPL_AUTO_DATA, auto_data,
762 g_auto_pointer_free(auto_data);
765 static GtkWidget *prefs_template_list_view_create(void)
767 GtkTreeView *list_view;
768 GtkTreeSelection *selector;
771 model = GTK_TREE_MODEL(prefs_template_create_data_store());
772 list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
773 g_object_unref(model);
775 gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
777 selector = gtk_tree_view_get_selection(list_view);
778 gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
779 gtk_tree_selection_set_select_function(selector, prefs_template_selected,
782 /* create the columns */
783 prefs_template_create_list_view_columns(GTK_WIDGET(list_view));
785 return GTK_WIDGET(list_view);
788 static void prefs_template_create_list_view_columns(GtkWidget *list_view)
790 GtkTreeViewColumn *column;
791 GtkCellRenderer *renderer;
793 renderer = gtk_cell_renderer_text_new();
794 column = gtk_tree_view_column_new_with_attributes
795 (_("Current templates"),
799 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
802 static gboolean prefs_template_selected(GtkTreeSelection *selector,
805 gboolean currently_selected,
810 GtkTextBuffer *buffer;
814 if (currently_selected)
817 if (!gtk_tree_model_get_iter(model, &titer, path))
820 tmpl_def.name = _("Template");
821 tmpl_def.subject = "";
827 gtk_tree_model_get(model, &titer,
834 gtk_entry_set_text(GTK_ENTRY(templates.entry_name), tmpl->name);
835 gtk_entry_set_text(GTK_ENTRY(templates.entry_to),
836 tmpl->to ? tmpl->to : "");
837 gtk_entry_set_text(GTK_ENTRY(templates.entry_cc),
838 tmpl->cc ? tmpl->cc : "");
839 gtk_entry_set_text(GTK_ENTRY(templates.entry_bcc),
840 tmpl->bcc ? tmpl->bcc : "");
841 gtk_entry_set_text(GTK_ENTRY(templates.entry_subject),
842 tmpl->subject ? tmpl->subject : "");
844 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
845 gtk_text_buffer_set_text(buffer, "", -1);
846 gtk_text_buffer_get_start_iter(buffer, &iter);
847 gtk_text_buffer_insert(buffer, &iter, tmpl->value, -1);