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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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"
45 TEMPL_AUTO_DATA, /*!< auto pointer */
49 static struct Templates {
53 GtkWidget *entry_name;
54 GtkWidget *entry_subject;
58 GtkWidget *text_value;
61 /* widget creating functions */
62 static void prefs_template_window_create (void);
63 static void prefs_template_window_setup (void);
64 static void prefs_template_clear (void);
66 static GSList *prefs_template_get_list (void);
69 static gint prefs_template_deleted_cb (GtkWidget *widget,
72 static gboolean prefs_template_key_pressed_cb (GtkWidget *widget,
75 static void prefs_template_cancel_cb (void);
76 static void prefs_template_ok_cb (void);
77 static void prefs_template_register_cb (void);
78 static void prefs_template_substitute_cb (void);
79 static void prefs_template_delete_cb (void);
81 static GtkListStore* prefs_template_create_data_store (void);
82 static void prefs_template_list_view_insert_template (GtkWidget *list_view,
83 GtkTreeIter *row_iter,
84 const gchar *template,
86 static GtkWidget *prefs_template_list_view_create (void);
87 static void prefs_template_create_list_view_columns (GtkWidget *list_view);
88 static gboolean prefs_template_selected (GtkTreeSelection *selector,
91 gboolean currently_selected,
94 /* Called from mainwindow.c */
95 void prefs_template_open(void)
99 if (!templates.window)
100 prefs_template_window_create();
102 prefs_template_window_setup();
103 gtk_widget_show(templates.window);
106 #define ADD_ENTRY(entry, str, row) \
108 label1 = gtk_label_new(str); \
109 gtk_widget_show(label1); \
110 gtk_table_attach(GTK_TABLE(table), label1, 0, 1, row, (row + 1), \
111 GTK_FILL, 0, 0, 0); \
112 gtk_misc_set_alignment(GTK_MISC(label1), 1, 0.5); \
114 entry = gtk_entry_new(); \
115 gtk_widget_show(entry); \
116 gtk_table_attach(GTK_TABLE(table), entry, 1, 2, row, (row + 1), \
117 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0); \
120 static void prefs_template_window_create(void)
122 /* window structure ;) */
128 GtkWidget *entry_name;
132 GtkWidget *entry_bcc;
133 GtkWidget *entry_subject;
135 GtkWidget *text_value;
141 GtkWidget *subst_btn;
145 GtkWidget *list_view;
146 GtkWidget *confirm_area;
148 GtkWidget *cancel_btn;
151 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
152 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
153 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
154 gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
155 gtk_window_set_default_size(GTK_WINDOW(window), 400, -1);
157 /* vpaned to separate template settings from templates list */
158 vpaned = gtk_vpaned_new();
159 gtk_widget_show(vpaned);
160 gtk_container_add(GTK_CONTAINER(window), vpaned);
162 /* vbox to handle template name and content */
163 vbox1 = gtk_vbox_new(FALSE, 6);
164 gtk_widget_show(vbox1);
165 gtk_container_set_border_width(GTK_CONTAINER(vbox1), 8);
166 gtk_paned_pack1(GTK_PANED(vpaned), vbox1, FALSE, FALSE);
168 hbox1 = gtk_hbox_new(FALSE, 8);
169 gtk_widget_show(hbox1);
170 gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, FALSE, 0);
172 label1 = gtk_label_new(_("Template name"));
173 gtk_widget_show(label1);
174 gtk_box_pack_start(GTK_BOX(hbox1), label1, FALSE, FALSE, 0);
176 entry_name = gtk_entry_new();
177 gtk_widget_show(entry_name);
178 gtk_box_pack_start(GTK_BOX(hbox1), entry_name, TRUE, TRUE, 0);
180 /* table for headers */
181 table = gtk_table_new(2, 2, FALSE);
182 gtk_widget_show(table);
183 gtk_box_pack_start(GTK_BOX(vbox1), table, FALSE, FALSE, 0);
184 gtk_table_set_row_spacings(GTK_TABLE(table), 4);
185 gtk_table_set_col_spacings(GTK_TABLE(table), 4);
187 ADD_ENTRY(entry_to, _("To:"), 0);
188 address_completion_register_entry(GTK_ENTRY(entry_to));
189 ADD_ENTRY(entry_cc, _("Cc:"), 1)
190 ADD_ENTRY(entry_bcc, _("Bcc:"), 2)
191 ADD_ENTRY(entry_subject, _("Subject:"), 3);
195 /* template content */
196 scroll2 = gtk_scrolled_window_new(NULL, NULL);
197 gtk_widget_show(scroll2);
198 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll2),
200 GTK_POLICY_AUTOMATIC);
201 gtk_box_pack_start(GTK_BOX(vbox1), scroll2, TRUE, TRUE, 0);
203 text_value = gtk_text_view_new();
204 gtk_widget_show(text_value);
205 gtk_widget_set_size_request(text_value, -1, 120);
206 gtk_container_add(GTK_CONTAINER(scroll2), text_value);
207 gtk_text_view_set_editable(GTK_TEXT_VIEW(text_value), TRUE);
208 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text_value), GTK_WRAP_WORD);
210 /* vbox for buttons and templates list */
211 vbox2 = gtk_vbox_new(FALSE, 6);
212 gtk_widget_show(vbox2);
213 gtk_container_set_border_width(GTK_CONTAINER(vbox2), 8);
214 gtk_paned_pack2(GTK_PANED(vpaned), vbox2, TRUE, FALSE);
216 /* register | substitute | delete */
217 hbox2 = gtk_hbox_new(FALSE, 4);
218 gtk_widget_show(hbox2);
219 gtk_box_pack_start(GTK_BOX(vbox2), hbox2, FALSE, FALSE, 0);
221 arrow1 = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
222 gtk_widget_show(arrow1);
223 gtk_box_pack_start(GTK_BOX(hbox2), arrow1, FALSE, FALSE, 0);
224 gtk_widget_set_size_request(arrow1, -1, 16);
226 hbox3 = gtk_hbox_new(TRUE, 4);
227 gtk_widget_show(hbox3);
228 gtk_box_pack_start(GTK_BOX(hbox2), hbox3, FALSE, FALSE, 0);
230 reg_btn = gtk_button_new_from_stock(GTK_STOCK_ADD);
231 gtk_widget_show(reg_btn);
232 gtk_box_pack_start(GTK_BOX(hbox3), reg_btn, FALSE, TRUE, 0);
233 g_signal_connect(G_OBJECT (reg_btn), "clicked",
234 G_CALLBACK (prefs_template_register_cb), NULL);
236 subst_btn = gtk_button_new_with_label(_(" Replace "));
237 gtk_widget_show(subst_btn);
238 gtk_box_pack_start(GTK_BOX(hbox3), subst_btn, FALSE, TRUE, 0);
239 g_signal_connect(G_OBJECT(subst_btn), "clicked",
240 G_CALLBACK(prefs_template_substitute_cb),
243 del_btn = gtk_button_new_from_stock(GTK_STOCK_REMOVE);
244 gtk_widget_show(del_btn);
245 gtk_box_pack_start(GTK_BOX(hbox3), del_btn, FALSE, TRUE, 0);
246 g_signal_connect(G_OBJECT(del_btn), "clicked",
247 G_CALLBACK(prefs_template_delete_cb), NULL);
249 desc_btn = gtk_button_new_with_label(_(" Symbols "));
250 gtk_widget_show(desc_btn);
251 gtk_box_pack_end(GTK_BOX(hbox2), desc_btn, FALSE, FALSE, 0);
252 g_signal_connect(G_OBJECT(desc_btn), "clicked",
253 G_CALLBACK(quote_fmt_quote_description), NULL);
256 scroll1 = gtk_scrolled_window_new(NULL, NULL);
257 gtk_widget_show(scroll1);
258 gtk_box_pack_start(GTK_BOX(vbox2), scroll1, TRUE, TRUE, 0);
259 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll1),
260 GTK_POLICY_AUTOMATIC,
261 GTK_POLICY_AUTOMATIC);
263 list_view = prefs_template_list_view_create();
264 gtk_widget_show(list_view);
265 gtk_widget_set_size_request(scroll1, -1, 140);
266 gtk_container_add(GTK_CONTAINER(scroll1), list_view);
269 gtkut_stock_button_set_create(&confirm_area, &ok_btn, GTK_STOCK_OK,
270 &cancel_btn, GTK_STOCK_CANCEL, NULL, NULL);
271 gtk_widget_show(confirm_area);
272 gtk_box_pack_end(GTK_BOX(vbox2), confirm_area, FALSE, FALSE, 0);
273 gtk_widget_grab_default(ok_btn);
275 gtk_window_set_title(GTK_WINDOW(window), _("Template configuration"));
277 g_signal_connect(G_OBJECT(window), "delete_event",
278 G_CALLBACK(prefs_template_deleted_cb), NULL);
279 g_signal_connect(G_OBJECT(window), "key_press_event",
280 G_CALLBACK(prefs_template_key_pressed_cb), NULL);
281 MANAGE_WINDOW_SIGNALS_CONNECT(window);
282 g_signal_connect(G_OBJECT(ok_btn), "clicked",
283 G_CALLBACK(prefs_template_ok_cb), NULL);
284 g_signal_connect(G_OBJECT(cancel_btn), "clicked",
285 G_CALLBACK(prefs_template_cancel_cb), NULL);
287 address_completion_start(window);
289 templates.window = window;
290 templates.ok_btn = ok_btn;
291 templates.list_view = list_view;
292 templates.entry_name = entry_name;
293 templates.entry_subject = entry_subject;
294 templates.entry_to = entry_to;
295 templates.entry_cc = entry_cc;
296 templates.entry_bcc = entry_bcc;
297 templates.text_value = text_value;
300 static void prefs_template_window_setup(void)
309 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
310 (templates.list_view)));
312 manage_window_set_transient(GTK_WINDOW(templates.window));
313 gtk_widget_grab_focus(templates.ok_btn);
315 gtk_list_store_clear(store);
317 prefs_template_list_view_insert_template(templates.list_view,
321 tmpl_list = template_read_config();
323 for (cur = tmpl_list; cur != NULL; cur = cur->next) {
324 tmpl = (Template *)cur->data;
325 prefs_template_list_view_insert_template(templates.list_view,
330 g_slist_free(tmpl_list);
333 static void prefs_template_clear(void)
337 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW
338 (templates.list_view)));
339 gtk_list_store_clear(store);
342 static gint prefs_template_deleted_cb(GtkWidget *widget, GdkEventAny *event,
345 prefs_template_cancel_cb();
349 static gboolean prefs_template_key_pressed_cb(GtkWidget *widget,
350 GdkEventKey *event, gpointer data)
352 if (event && event->keyval == GDK_Escape)
353 prefs_template_cancel_cb();
357 static void prefs_template_ok_cb(void)
361 tmpl_list = prefs_template_get_list();
362 template_set_config(tmpl_list);
363 compose_reflect_prefs_all();
364 prefs_template_clear();
365 gtk_widget_hide(templates.window);
369 static void prefs_template_cancel_cb(void)
371 prefs_template_clear();
372 gtk_widget_hide(templates.window);
377 *\brief Request list for storage. New list is owned
380 static GSList *prefs_template_get_list(void)
382 GSList *tmpl_list = NULL;
387 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
388 if (!gtk_tree_model_get_iter_first(model, &iter))
392 gtk_tree_model_get(model, &iter,
399 ntmpl = g_new(Template, 1);
400 ntmpl->name = tmpl->name && *(tmpl->name)
401 ? g_strdup(tmpl->name)
403 ntmpl->subject = tmpl->subject && *(tmpl->subject)
404 ? g_strdup(tmpl->subject)
406 ntmpl->to = tmpl->to && *(tmpl->to)
409 ntmpl->cc = tmpl->cc && *(tmpl->cc)
412 ntmpl->bcc = tmpl->bcc && *(tmpl->bcc)
413 ? g_strdup(tmpl->bcc)
415 ntmpl->value = tmpl->value && *(tmpl->value)
416 ? g_strdup(tmpl->value)
418 tmpl_list = g_slist_append(tmpl_list, ntmpl);
421 } while (gtk_tree_model_iter_next(model, &iter));
426 static void prefs_template_list_view_set_row(GtkTreeIter *row)
436 GtkTextBuffer *buffer;
437 GtkTextIter start, end;
440 model = gtk_tree_view_get_model(GTK_TREE_VIEW(templates.list_view));
442 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
443 gtk_text_buffer_get_start_iter(buffer, &start);
444 gtk_text_buffer_get_iter_at_offset(buffer, &end, -1);
445 value = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
447 if (value && *value != '\0') {
451 memset(&dummyinfo, 0, sizeof(MsgInfo));
452 quote_fmt_init(&dummyinfo, NULL, NULL);
453 quote_fmt_scan_string(value);
455 parsed_buf = quote_fmt_get_buffer();
457 alertpanel_error(_("Template format error."));
463 name = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_name),
465 subject = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_subject),
467 to = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_to),
469 cc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_cc),
471 bcc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_bcc),
474 if (subject && *subject == '\0') {
478 if (to && *to == '\0') {
482 if (cc && *cc == '\0') {
486 if (bcc && *bcc == '\0') {
491 tmpl = g_new(Template, 1);
493 tmpl->subject = subject;
499 prefs_template_list_view_insert_template(templates.list_view,
500 row, tmpl->name, tmpl);
503 static void prefs_template_register_cb(void)
505 prefs_template_list_view_set_row(NULL);
508 static void prefs_template_substitute_cb(void)
512 GtkTreeSelection *selection;
515 selection = gtk_tree_view_get_selection
516 (GTK_TREE_VIEW(templates.list_view));
518 if (!gtk_tree_selection_get_selected(selection, &model, &row))
521 gtk_tree_model_get(model, &row,
527 prefs_template_list_view_set_row(&row);
530 static void prefs_template_delete_cb(void)
534 GtkTreeSelection *selection;
537 selection = gtk_tree_view_get_selection
538 (GTK_TREE_VIEW(templates.list_view));
540 if (!gtk_tree_selection_get_selected(selection, &model, &row))
543 gtk_tree_model_get(model, &row,
550 if (alertpanel(_("Delete template"),
551 _("Do you really want to delete this template?"),
552 GTK_STOCK_YES, GTK_STOCK_NO, NULL) != G_ALERTDEFAULT)
555 gtk_list_store_remove(GTK_LIST_STORE(model), &row);
558 static GtkListStore* prefs_template_create_data_store(void)
560 return gtk_list_store_new(N_TEMPL_COLUMNS,
567 static void prefs_template_list_view_insert_template(GtkWidget *list_view,
568 GtkTreeIter *row_iter,
569 const gchar *template,
573 GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
574 (GTK_TREE_VIEW(list_view)));
577 if (row_iter == NULL) {
579 gtk_list_store_append(list_store, &iter);
584 auto_data = g_auto_pointer_new_with_free(data, (GFreeFunc) template_free);
586 /* if replacing data in an existing row, the auto pointer takes care
587 * of destroying the Template data */
588 gtk_list_store_set(list_store, &iter,
589 TEMPL_TEXT, template,
591 TEMPL_AUTO_DATA, auto_data,
594 g_auto_pointer_free(auto_data);
597 static GtkWidget *prefs_template_list_view_create(void)
599 GtkTreeView *list_view;
600 GtkTreeSelection *selector;
603 model = GTK_TREE_MODEL(prefs_template_create_data_store());
604 list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
605 g_object_unref(model);
607 gtk_tree_view_set_rules_hint(list_view, TRUE);
609 selector = gtk_tree_view_get_selection(list_view);
610 gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
611 gtk_tree_selection_set_select_function(selector, prefs_template_selected,
614 /* create the columns */
615 prefs_template_create_list_view_columns(GTK_WIDGET(list_view));
617 return GTK_WIDGET(list_view);
620 static void prefs_template_create_list_view_columns(GtkWidget *list_view)
622 GtkTreeViewColumn *column;
623 GtkCellRenderer *renderer;
625 renderer = gtk_cell_renderer_text_new();
626 column = gtk_tree_view_column_new_with_attributes
627 (_("Current templates"),
631 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
634 static gboolean prefs_template_selected(GtkTreeSelection *selector,
637 gboolean currently_selected,
642 GtkTextBuffer *buffer;
646 if (currently_selected)
649 if (!gtk_tree_model_get_iter(model, &titer, path))
652 tmpl_def.name = _("Template");
653 tmpl_def.subject = "";
659 gtk_tree_model_get(model, &titer,
666 gtk_entry_set_text(GTK_ENTRY(templates.entry_name), tmpl->name);
667 gtk_entry_set_text(GTK_ENTRY(templates.entry_to),
668 tmpl->to ? tmpl->to : "");
669 gtk_entry_set_text(GTK_ENTRY(templates.entry_cc),
670 tmpl->cc ? tmpl->cc : "");
671 gtk_entry_set_text(GTK_ENTRY(templates.entry_bcc),
672 tmpl->bcc ? tmpl->bcc : "");
673 gtk_entry_set_text(GTK_ENTRY(templates.entry_subject),
674 tmpl->subject ? tmpl->subject : "");
676 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
677 gtk_text_buffer_set_text(buffer, "", -1);
678 gtk_text_buffer_get_start_iter(buffer, &iter);
679 gtk_text_buffer_insert(buffer, &iter, tmpl->value, -1);