2 * Sylpheed templates subsystem
3 * Copyright (C) 2001 Alexander Barinov
4 * Copyright (C) 2001-2005 Hiroyuki Yamamoto
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>
36 #include "alertpanel.h"
37 #include "manage_window.h"
39 #include "addr_compl.h"
40 #include "quote_fmt.h"
41 #include "prefs_common.h"
46 TEMPL_AUTO_DATA, /*!< auto pointer */
50 static struct Templates {
54 GtkWidget *entry_name;
55 GtkWidget *entry_subject;
59 GtkWidget *text_value;
62 static int modified = FALSE;
64 /* widget creating functions */
65 static void prefs_template_window_create (void);
66 static void prefs_template_window_setup (void);
67 static void prefs_template_clear (void);
69 static GSList *prefs_template_get_list (void);
72 static gint prefs_template_deleted_cb (GtkWidget *widget,
75 static gboolean prefs_template_key_pressed_cb (GtkWidget *widget,
78 static void prefs_template_cancel_cb (void);
79 static void prefs_template_ok_cb (void);
80 static void prefs_template_register_cb (void);
81 static void prefs_template_substitute_cb (void);
82 static void prefs_template_delete_cb (void);
84 static GtkListStore* prefs_template_create_data_store (void);
85 static void prefs_template_list_view_insert_template (GtkWidget *list_view,
86 GtkTreeIter *row_iter,
87 const gchar *template,
90 static GtkWidget *prefs_template_list_view_create (void);
91 static void prefs_template_create_list_view_columns (GtkWidget *list_view);
92 static gboolean prefs_template_selected (GtkTreeSelection *selector,
95 gboolean currently_selected,
98 /* Called from mainwindow.c */
99 void prefs_template_open(void)
103 if (!templates.window)
104 prefs_template_window_create();
106 prefs_template_window_setup();
107 gtk_widget_show(templates.window);
111 *\brief Save Gtk object size to prefs dataset
113 static void prefs_template_size_allocate_cb(GtkWidget *widget,
114 GtkAllocation *allocation)
116 g_return_if_fail(allocation != NULL);
118 prefs_common.templateswin_width = allocation->width;
119 prefs_common.templateswin_height = allocation->height;
122 #define ADD_ENTRY(entry, str, row) \
124 label1 = gtk_label_new(str); \
125 gtk_widget_show(label1); \
126 gtk_table_attach(GTK_TABLE(table), label1, 0, 1, row, (row + 1), \
127 GTK_FILL, 0, 0, 0); \
128 gtk_misc_set_alignment(GTK_MISC(label1), 1, 0.5); \
130 entry = gtk_entry_new(); \
131 gtk_widget_show(entry); \
132 gtk_table_attach(GTK_TABLE(table), entry, 1, 2, row, (row + 1), \
133 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0); \
136 static void prefs_template_window_create(void)
138 /* window structure ;) */
144 GtkWidget *entry_name;
148 GtkWidget *entry_bcc;
149 GtkWidget *entry_subject;
151 GtkWidget *text_value;
157 GtkWidget *subst_btn;
161 GtkWidget *list_view;
162 GtkWidget *confirm_area;
164 GtkWidget *cancel_btn;
165 static GdkGeometry geometry;
167 debug_print("Creating templates configuration window...\n");
170 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
171 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
172 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
173 gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
175 /* vpaned to separate template settings from templates list */
176 vpaned = gtk_vpaned_new();
177 gtk_widget_show(vpaned);
178 gtk_container_add(GTK_CONTAINER(window), vpaned);
180 /* vbox to handle template name and content */
181 vbox1 = gtk_vbox_new(FALSE, 6);
182 gtk_widget_show(vbox1);
183 gtk_container_set_border_width(GTK_CONTAINER(vbox1), 8);
184 gtk_paned_pack1(GTK_PANED(vpaned), vbox1, FALSE, FALSE);
186 hbox1 = gtk_hbox_new(FALSE, 8);
187 gtk_widget_show(hbox1);
188 gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, FALSE, 0);
190 label1 = gtk_label_new(_("Template name"));
191 gtk_widget_show(label1);
192 gtk_box_pack_start(GTK_BOX(hbox1), label1, FALSE, FALSE, 0);
194 entry_name = gtk_entry_new();
195 gtk_widget_show(entry_name);
196 gtk_box_pack_start(GTK_BOX(hbox1), entry_name, TRUE, TRUE, 0);
198 /* table for headers */
199 table = gtk_table_new(2, 2, FALSE);
200 gtk_widget_show(table);
201 gtk_box_pack_start(GTK_BOX(vbox1), table, FALSE, FALSE, 0);
202 gtk_table_set_row_spacings(GTK_TABLE(table), 4);
203 gtk_table_set_col_spacings(GTK_TABLE(table), 4);
205 ADD_ENTRY(entry_to, _("To:"), 0);
206 address_completion_register_entry(GTK_ENTRY(entry_to));
207 ADD_ENTRY(entry_cc, _("Cc:"), 1)
208 ADD_ENTRY(entry_bcc, _("Bcc:"), 2)
209 ADD_ENTRY(entry_subject, _("Subject:"), 3);
213 /* template content */
214 scroll2 = gtk_scrolled_window_new(NULL, NULL);
215 gtk_widget_show(scroll2);
216 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll2),
217 GTK_POLICY_AUTOMATIC,
218 GTK_POLICY_AUTOMATIC);
219 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll2),
221 gtk_box_pack_start(GTK_BOX(vbox1), scroll2, TRUE, TRUE, 0);
223 text_value = gtk_text_view_new();
224 gtk_widget_show(text_value);
225 gtk_widget_set_size_request(text_value, -1, 120);
226 gtk_container_add(GTK_CONTAINER(scroll2), text_value);
227 gtk_text_view_set_editable(GTK_TEXT_VIEW(text_value), TRUE);
228 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text_value), GTK_WRAP_WORD);
230 /* vbox for buttons and templates list */
231 vbox2 = gtk_vbox_new(FALSE, 6);
232 gtk_widget_show(vbox2);
233 gtk_container_set_border_width(GTK_CONTAINER(vbox2), 8);
234 gtk_paned_pack2(GTK_PANED(vpaned), vbox2, TRUE, FALSE);
236 /* register | substitute | delete */
237 hbox2 = gtk_hbox_new(FALSE, 4);
238 gtk_widget_show(hbox2);
239 gtk_box_pack_start(GTK_BOX(vbox2), hbox2, FALSE, FALSE, 0);
241 arrow1 = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
242 gtk_widget_show(arrow1);
243 gtk_box_pack_start(GTK_BOX(hbox2), arrow1, FALSE, FALSE, 0);
244 gtk_widget_set_size_request(arrow1, -1, 16);
246 hbox3 = gtk_hbox_new(TRUE, 4);
247 gtk_widget_show(hbox3);
248 gtk_box_pack_start(GTK_BOX(hbox2), hbox3, FALSE, FALSE, 0);
250 reg_btn = gtk_button_new_from_stock(GTK_STOCK_ADD);
251 gtk_widget_show(reg_btn);
252 gtk_box_pack_start(GTK_BOX(hbox3), reg_btn, FALSE, TRUE, 0);
253 g_signal_connect(G_OBJECT (reg_btn), "clicked",
254 G_CALLBACK (prefs_template_register_cb), NULL);
256 subst_btn = gtk_button_new_with_label(_(" Replace "));
257 gtk_widget_show(subst_btn);
258 gtk_box_pack_start(GTK_BOX(hbox3), subst_btn, FALSE, TRUE, 0);
259 g_signal_connect(G_OBJECT(subst_btn), "clicked",
260 G_CALLBACK(prefs_template_substitute_cb),
263 del_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
264 gtk_widget_show(del_btn);
265 gtk_box_pack_start(GTK_BOX(hbox3), del_btn, FALSE, TRUE, 0);
266 g_signal_connect(G_OBJECT(del_btn), "clicked",
267 G_CALLBACK(prefs_template_delete_cb), NULL);
269 desc_btn = gtk_button_new_with_label(_(" Symbols "));
270 gtk_widget_show(desc_btn);
271 gtk_box_pack_end(GTK_BOX(hbox2), desc_btn, FALSE, FALSE, 0);
272 g_signal_connect(G_OBJECT(desc_btn), "clicked",
273 G_CALLBACK(quote_fmt_quote_description), NULL);
276 scroll1 = gtk_scrolled_window_new(NULL, NULL);
277 gtk_widget_show(scroll1);
278 gtk_box_pack_start(GTK_BOX(vbox2), scroll1, TRUE, TRUE, 0);
279 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll1),
280 GTK_POLICY_AUTOMATIC,
281 GTK_POLICY_AUTOMATIC);
283 list_view = prefs_template_list_view_create();
284 gtk_widget_show(list_view);
285 gtk_widget_set_size_request(scroll1, -1, 140);
286 gtk_container_add(GTK_CONTAINER(scroll1), list_view);
289 gtkut_stock_button_set_create(&confirm_area, &ok_btn, GTK_STOCK_OK,
290 &cancel_btn, GTK_STOCK_CANCEL, NULL, NULL);
291 gtk_widget_show(confirm_area);
292 gtk_box_pack_end(GTK_BOX(vbox2), confirm_area, FALSE, FALSE, 0);
293 gtk_widget_grab_default(ok_btn);
295 gtk_window_set_title(GTK_WINDOW(window), _("Template configuration"));
297 g_signal_connect(G_OBJECT(window), "delete_event",
298 G_CALLBACK(prefs_template_deleted_cb), NULL);
299 g_signal_connect(G_OBJECT(window), "size_allocate",
300 G_CALLBACK(prefs_template_size_allocate_cb), NULL);
301 g_signal_connect(G_OBJECT(window), "key_press_event",
302 G_CALLBACK(prefs_template_key_pressed_cb), NULL);
303 MANAGE_WINDOW_SIGNALS_CONNECT(window);
304 g_signal_connect(G_OBJECT(ok_btn), "clicked",
305 G_CALLBACK(prefs_template_ok_cb), NULL);
306 g_signal_connect(G_OBJECT(cancel_btn), "clicked",
307 G_CALLBACK(prefs_template_cancel_cb), NULL);
309 address_completion_start(window);
311 if (!geometry.min_height) {
312 geometry.min_width = 440;
313 geometry.min_height = 500;
316 gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
318 gtk_widget_set_size_request(window, prefs_common.templateswin_width,
319 prefs_common.templateswin_height);
321 templates.window = window;
322 templates.ok_btn = ok_btn;
323 templates.list_view = list_view;
324 templates.entry_name = entry_name;
325 templates.entry_subject = entry_subject;
326 templates.entry_to = entry_to;
327 templates.entry_cc = entry_cc;
328 templates.entry_bcc = entry_bcc;
329 templates.text_value = text_value;
332 static void prefs_template_window_setup(void)
339 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
340 (templates.list_view)));
342 manage_window_set_transient(GTK_WINDOW(templates.window));
343 gtk_widget_grab_focus(templates.ok_btn);
345 gtk_list_store_clear(store);
347 prefs_template_list_view_insert_template(templates.list_view,
351 tmpl_list = template_read_config();
353 for (cur = tmpl_list; cur != NULL; cur = cur->next) {
354 tmpl = (Template *)cur->data;
355 prefs_template_list_view_insert_template(templates.list_view,
360 g_slist_free(tmpl_list);
363 static void prefs_template_clear(void)
367 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
368 (templates.list_view)));
369 gtk_list_store_clear(store);
372 static gint prefs_template_deleted_cb(GtkWidget *widget, GdkEventAny *event,
375 prefs_template_cancel_cb();
379 static gboolean prefs_template_key_pressed_cb(GtkWidget *widget,
380 GdkEventKey *event, gpointer data)
382 if (event && event->keyval == GDK_Escape)
383 prefs_template_cancel_cb();
385 GtkWidget *focused = gtkut_get_focused_child(
386 GTK_CONTAINER(widget));
387 if (focused && GTK_IS_EDITABLE(focused)) {
394 static void prefs_template_ok_cb(void)
398 if (modified && alertpanel(_("Entry not saved"),
399 _("The entry was not saved. Close anyway?"),
400 GTK_STOCK_YES, GTK_STOCK_NO, NULL) != G_ALERTDEFAULT) {
404 tmpl_list = prefs_template_get_list();
405 template_set_config(tmpl_list);
406 compose_reflect_prefs_all();
407 prefs_template_clear();
408 gtk_widget_hide(templates.window);
412 static void prefs_template_cancel_cb(void)
414 if (modified && alertpanel(_("Entry not saved"),
415 _("The entry was not saved. Close anyway?"),
416 GTK_STOCK_YES, GTK_STOCK_NO, NULL) != G_ALERTDEFAULT) {
420 prefs_template_clear();
421 gtk_widget_hide(templates.window);
426 *\brief Request list for storage. New list is owned
429 static GSList *prefs_template_get_list(void)
431 GSList *tmpl_list = NULL;
436 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
437 if (!gtk_tree_model_get_iter_first(model, &iter))
441 gtk_tree_model_get(model, &iter,
448 ntmpl = g_new(Template, 1);
449 ntmpl->name = tmpl->name && *(tmpl->name)
450 ? g_strdup(tmpl->name)
452 ntmpl->subject = tmpl->subject && *(tmpl->subject)
453 ? g_strdup(tmpl->subject)
455 ntmpl->to = tmpl->to && *(tmpl->to)
458 ntmpl->cc = tmpl->cc && *(tmpl->cc)
461 ntmpl->bcc = tmpl->bcc && *(tmpl->bcc)
462 ? g_strdup(tmpl->bcc)
464 ntmpl->value = tmpl->value && *(tmpl->value)
465 ? g_strdup(tmpl->value)
467 tmpl_list = g_slist_append(tmpl_list, ntmpl);
470 } while (gtk_tree_model_iter_next(model, &iter));
475 static gboolean prefs_template_list_view_set_row(GtkTreeIter *row)
476 /* return TRUE if the row could be modified */
485 GtkTextBuffer *buffer;
486 GtkTextIter start, end;
489 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
491 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
492 gtk_text_buffer_get_start_iter(buffer, &start);
493 gtk_text_buffer_get_iter_at_offset(buffer, &end, -1);
494 value = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
496 if (value && *value != '\0') {
500 memset(&dummyinfo, 0, sizeof(MsgInfo));
501 quote_fmt_init(&dummyinfo, NULL, NULL);
502 quote_fmt_scan_string(value);
504 parsed_buf = quote_fmt_get_buffer();
506 alertpanel_error(_("Template format error."));
512 name = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_name),
514 if (name[0] == '\0') {
515 alertpanel_error(_("Template name is not set."));
518 subject = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_subject),
520 to = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_to),
522 cc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_cc),
524 bcc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_bcc),
527 if (subject && *subject == '\0') {
531 if (to && *to == '\0') {
535 if (cc && *cc == '\0') {
539 if (bcc && *bcc == '\0') {
544 tmpl = g_new(Template, 1);
546 tmpl->subject = subject;
552 prefs_template_list_view_insert_template(templates.list_view,
553 row, tmpl->name, tmpl, TRUE);
557 static void prefs_template_register_cb(void)
559 modified = !prefs_template_list_view_set_row(NULL);
562 static void prefs_template_substitute_cb(void)
566 GtkTreeSelection *selection;
569 selection = gtk_tree_view_get_selection
570 (GTK_TREE_VIEW(templates.list_view));
572 if (!gtk_tree_selection_get_selected(selection, &model, &row))
575 gtk_tree_model_get(model, &row,
581 modified = !prefs_template_list_view_set_row(&row);
584 static void prefs_template_delete_cb(void)
588 GtkTreeSelection *selection;
591 selection = gtk_tree_view_get_selection
592 (GTK_TREE_VIEW(templates.list_view));
594 if (!gtk_tree_selection_get_selected(selection, &model, &row))
597 gtk_tree_model_get(model, &row,
604 if (alertpanel(_("Delete template"),
605 _("Do you really want to delete this template?"),
606 GTK_STOCK_YES, GTK_STOCK_NO, NULL) != G_ALERTDEFAULT)
609 gtk_list_store_remove(GTK_LIST_STORE(model), &row);
612 static GtkListStore* prefs_template_create_data_store(void)
614 return gtk_list_store_new(N_TEMPL_COLUMNS,
621 static gboolean prefs_template_list_view_find_insertion_point(GtkTreeModel* model,
622 const gchar* template,
628 if ((gtk_tree_model_get_iter_first(model, &i))) {
629 for ( ; gtk_tree_model_iter_next(model, &i); ) {
630 gtk_tree_model_get(model, &i, TEMPL_TEXT, &text, -1);
631 if (strcmp(text, template) >= 0) {
639 /* cannot add before (need to append) */
643 static void prefs_template_list_view_insert_template(GtkWidget *list_view,
644 GtkTreeIter *row_iter,
645 const gchar *template,
650 GtkTreeModel *model = gtk_tree_view_get_model
651 (GTK_TREE_VIEW(list_view));
652 GtkListStore *list_store = GTK_LIST_STORE(model);
655 if (row_iter == NULL) {
657 GtkTreeIter insertion_point;
660 if (prefs_template_list_view_find_insertion_point(model,
661 template, &insertion_point)) {
662 gtk_list_store_insert_before(list_store, &iter,
665 gtk_list_store_append(list_store, &iter);
669 gtk_list_store_append(list_store, &iter);
672 /* modify the existing */
676 GtkTreeIter insertion_point;
679 /* force re-sorting if template's name changed */
680 gtk_tree_model_get(model, row_iter, TEMPL_TEXT, &text, -1);
681 if (strcmp(text, template) != 0) {
683 /* move the modified template */
684 if (prefs_template_list_view_find_insertion_point(model,
685 template, &insertion_point)) {
686 gtk_list_store_move_before(list_store, row_iter,
689 /* move to the end */
690 gtk_list_store_move_before(list_store, row_iter,
698 auto_data = g_auto_pointer_new_with_free(data, (GFreeFunc) template_free);
700 /* if replacing data in an existing row, the auto pointer takes care
701 * of destroying the Template data */
702 gtk_list_store_set(list_store, &iter,
703 TEMPL_TEXT, template,
705 TEMPL_AUTO_DATA, auto_data,
708 g_auto_pointer_free(auto_data);
711 static GtkWidget *prefs_template_list_view_create(void)
713 GtkTreeView *list_view;
714 GtkTreeSelection *selector;
717 model = GTK_TREE_MODEL(prefs_template_create_data_store());
718 list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
719 g_object_unref(model);
721 gtk_tree_view_set_rules_hint(list_view, prefs_common.enable_rules_hint);
723 selector = gtk_tree_view_get_selection(list_view);
724 gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
725 gtk_tree_selection_set_select_function(selector, prefs_template_selected,
728 /* create the columns */
729 prefs_template_create_list_view_columns(GTK_WIDGET(list_view));
731 return GTK_WIDGET(list_view);
734 static void prefs_template_create_list_view_columns(GtkWidget *list_view)
736 GtkTreeViewColumn *column;
737 GtkCellRenderer *renderer;
739 renderer = gtk_cell_renderer_text_new();
740 column = gtk_tree_view_column_new_with_attributes
741 (_("Current templates"),
745 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
748 static gboolean prefs_template_selected(GtkTreeSelection *selector,
751 gboolean currently_selected,
756 GtkTextBuffer *buffer;
760 if (currently_selected)
763 if (!gtk_tree_model_get_iter(model, &titer, path))
766 tmpl_def.name = _("Template");
767 tmpl_def.subject = "";
773 gtk_tree_model_get(model, &titer,
780 gtk_entry_set_text(GTK_ENTRY(templates.entry_name), tmpl->name);
781 gtk_entry_set_text(GTK_ENTRY(templates.entry_to),
782 tmpl->to ? tmpl->to : "");
783 gtk_entry_set_text(GTK_ENTRY(templates.entry_cc),
784 tmpl->cc ? tmpl->cc : "");
785 gtk_entry_set_text(GTK_ENTRY(templates.entry_bcc),
786 tmpl->bcc ? tmpl->bcc : "");
787 gtk_entry_set_text(GTK_ENTRY(templates.entry_subject),
788 tmpl->subject ? tmpl->subject : "");
790 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
791 gtk_text_buffer_set_text(buffer, "", -1);
792 gtk_text_buffer_get_start_iter(buffer, &iter);
793 gtk_text_buffer_insert(buffer, &iter, tmpl->value, -1);