6250aae1e619da2e05cc264f3da4545a522c698c
[claws.git] / src / prefs_template.c
1 /*
2  * Sylpheed templates subsystem 
3  * Copyright (C) 2001 Alexander Barinov
4  * Copyright (C) 2001 Hiroyuki Yamamoto
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #include "defs.h"
22
23 #include <glib.h>
24 #include <gtk/gtk.h>
25 #include <gdk/gdkkeysyms.h>
26 #include <string.h>
27 #include <dirent.h>
28 #include <sys/stat.h>
29
30 #include "intl.h"
31 #include "template.h"
32 #include "main.h"
33 #include "inc.h"
34 #include "utils.h"
35 #include "gtkutils.h"
36 #include "alertpanel.h"
37 #include "manage_window.h"
38 #include "prefs_common.h"
39 #include "compose.h"
40 #include "quote_fmt.h"
41
42 static struct Templates {
43         GtkWidget *window;
44         GtkWidget *ok_btn;
45         GtkWidget *clist_tmpls;
46         GtkWidget *entry_name;
47         GtkWidget *entry_subject;
48         GtkWidget *entry_to;
49         GtkWidget *text_value;
50 } templates;
51
52 /* widget creating functions */
53 static void prefs_template_window_create        (void);
54 static void prefs_template_window_setup         (void);
55 static void prefs_template_clear                (void);
56
57 static GSList *prefs_template_get_list          (void);
58
59 /* callbacks */
60 static gint prefs_template_deleted_cb           (GtkWidget      *widget,
61                                                  GdkEventAny    *event,
62                                                  gpointer        data);
63 static void prefs_template_key_pressed_cb       (GtkWidget      *widget,
64                                                  GdkEventKey    *event,
65                                                  gpointer        data);
66 static void prefs_template_cancel_cb            (void);
67 static void prefs_template_ok_cb                (void);
68 static void prefs_template_select_cb            (GtkCList       *clist,
69                                                  gint            row,
70                                                  gint            column,
71                                                  GdkEvent       *event);
72 static void prefs_template_register_cb          (void);
73 static void prefs_template_substitute_cb        (void);
74 static void prefs_template_delete_cb            (void);
75
76 /* Called from mainwindow.c */
77 void prefs_template_open(void)
78 {
79         inc_lock();
80
81         if (!templates.window)
82                 prefs_template_window_create();
83
84         prefs_template_window_setup();
85         gtk_widget_show(templates.window);
86 }
87
88 #define ADD_ENTRY(entry, str, row) \
89 { \
90         label1 = gtk_label_new(str); \
91         gtk_widget_show(label1); \
92         gtk_table_attach(GTK_TABLE(table), label1, 0, 1, row, (row + 1), \
93                          GTK_FILL, 0, 0, 0); \
94         gtk_misc_set_alignment(GTK_MISC(label1), 1, 0.5); \
95  \
96         entry = gtk_entry_new(); \
97         gtk_widget_show(entry); \
98         gtk_table_attach(GTK_TABLE(table), entry, 1, 2, row, (row + 1), \
99                          GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0); \
100 }
101
102 static void prefs_template_window_create(void)
103 {
104         /* window structure ;) */
105         GtkWidget *window;
106         GtkWidget   *vpaned;
107         GtkWidget     *vbox1;
108         GtkWidget       *hbox1;
109         GtkWidget         *label1;
110         GtkWidget         *entry_name;
111         GtkWidget       *table;
112         GtkWidget         *entry_to;
113         GtkWidget         *entry_subject;
114         GtkWidget       *scroll2;
115         GtkWidget         *text_value;
116         GtkWidget     *vbox2;
117         GtkWidget       *hbox2;
118         GtkWidget         *arrow1;
119         GtkWidget         *hbox3;
120         GtkWidget           *reg_btn;
121         GtkWidget           *subst_btn;
122         GtkWidget           *del_btn;
123         GtkWidget         *desc_btn;
124         GtkWidget       *scroll1;
125         GtkWidget         *clist_tmpls;
126         GtkWidget       *confirm_area;
127         GtkWidget         *ok_btn;
128         GtkWidget         *cancel_btn;
129
130         gchar *title[1];
131
132         /* main window */
133         window = gtk_window_new(GTK_WINDOW_DIALOG);
134         gtk_window_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
135         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
136         gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, FALSE);
137         gtk_window_set_default_size(GTK_WINDOW(window), 400, -1);
138
139         /* vpaned to separate template settings from templates list */
140         vpaned = gtk_vpaned_new();
141         gtk_widget_show(vpaned);
142         gtk_container_add(GTK_CONTAINER(window), vpaned);
143
144         /* vbox to handle template name and content */
145         vbox1 = gtk_vbox_new(FALSE, 6);
146         gtk_widget_show(vbox1);
147         gtk_container_set_border_width(GTK_CONTAINER(vbox1), 8);
148         gtk_paned_pack1(GTK_PANED(vpaned), vbox1, FALSE, FALSE);
149
150         hbox1 = gtk_hbox_new(FALSE, 8);
151         gtk_widget_show(hbox1);
152         gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, FALSE, 0);
153         gtk_container_set_border_width(GTK_CONTAINER(hbox1), 2);
154
155         label1 = gtk_label_new(_("Template name"));
156         gtk_widget_show(label1);
157         gtk_box_pack_start(GTK_BOX(hbox1), label1, FALSE, FALSE, 0);
158
159         entry_name = gtk_entry_new();
160         gtk_widget_show(entry_name);
161         gtk_box_pack_start(GTK_BOX(hbox1), entry_name, TRUE, TRUE, 0);
162
163         /* table for headers */
164         table = gtk_table_new(2, 2, FALSE);
165         gtk_widget_show(table);
166         gtk_box_pack_start(GTK_BOX(vbox1), table, FALSE, FALSE, 0);
167         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
168         gtk_table_set_col_spacings(GTK_TABLE(table), 4);
169
170         ADD_ENTRY(entry_to, _("To:"), 0);
171         ADD_ENTRY(entry_subject, _("Subject:"), 1);
172
173 #undef ADD_ENTRY
174
175         /* template content */
176         scroll2 = gtk_scrolled_window_new(NULL, NULL);
177         gtk_widget_show(scroll2);
178         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll2),
179                                        GTK_POLICY_NEVER,
180                                        GTK_POLICY_ALWAYS);
181         gtk_box_pack_start(GTK_BOX(vbox1), scroll2, TRUE, TRUE, 0);
182
183         text_value = gtk_text_new(NULL, NULL);
184         gtk_widget_show(text_value);
185         gtk_widget_set_usize(text_value, -1, 120);
186         gtk_container_add(GTK_CONTAINER(scroll2), text_value);
187         gtk_text_set_editable(GTK_TEXT(text_value), TRUE);
188         gtk_text_set_word_wrap(GTK_TEXT(text_value), TRUE);
189
190         /* vbox for buttons and templates list */
191         vbox2 = gtk_vbox_new(FALSE, 6);
192         gtk_widget_show(vbox2);
193         gtk_container_set_border_width(GTK_CONTAINER(vbox2), 8);
194         gtk_paned_pack2(GTK_PANED(vpaned), vbox2, TRUE, FALSE);
195
196         /* register | substitute | delete */
197         hbox2 = gtk_hbox_new(FALSE, 4);
198         gtk_widget_show(hbox2);
199         gtk_box_pack_start(GTK_BOX(vbox2), hbox2, FALSE, FALSE, 0);
200
201         arrow1 = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
202         gtk_widget_show(arrow1);
203         gtk_box_pack_start(GTK_BOX(hbox2), arrow1, FALSE, FALSE, 0);
204         gtk_widget_set_usize(arrow1, -1, 16);
205
206         hbox3 = gtk_hbox_new(TRUE, 4);
207         gtk_widget_show(hbox3);
208         gtk_box_pack_start(GTK_BOX(hbox2), hbox3, FALSE, FALSE, 0);
209
210         reg_btn = gtk_button_new_with_label(_("Register"));
211         gtk_widget_show(reg_btn);
212         gtk_box_pack_start(GTK_BOX(hbox3), reg_btn, FALSE, TRUE, 0);
213         gtk_signal_connect(GTK_OBJECT (reg_btn), "clicked",
214                            GTK_SIGNAL_FUNC (prefs_template_register_cb), NULL);
215
216         subst_btn = gtk_button_new_with_label(_(" Substitute "));
217         gtk_widget_show(subst_btn);
218         gtk_box_pack_start(GTK_BOX(hbox3), subst_btn, FALSE, TRUE, 0);
219         gtk_signal_connect(GTK_OBJECT(subst_btn), "clicked",
220                            GTK_SIGNAL_FUNC(prefs_template_substitute_cb),
221                            NULL);
222
223         del_btn = gtk_button_new_with_label(_("Delete"));
224         gtk_widget_show(del_btn);
225         gtk_box_pack_start(GTK_BOX(hbox3), del_btn, FALSE, TRUE, 0);
226         gtk_signal_connect(GTK_OBJECT(del_btn), "clicked",
227                            GTK_SIGNAL_FUNC(prefs_template_delete_cb), NULL);
228
229         desc_btn = gtk_button_new_with_label(_(" Symbols "));
230         gtk_widget_show(desc_btn);
231         gtk_box_pack_end(GTK_BOX(hbox2), desc_btn, FALSE, FALSE, 0);
232         gtk_signal_connect(GTK_OBJECT(desc_btn), "clicked",
233                            GTK_SIGNAL_FUNC(prefs_quote_description), NULL);
234
235         /* templates list */
236         scroll1 = gtk_scrolled_window_new(NULL, NULL);
237         gtk_widget_show(scroll1);
238         gtk_box_pack_start(GTK_BOX(vbox2), scroll1, TRUE, TRUE, 0);
239         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll1),
240                                        GTK_POLICY_AUTOMATIC,
241                                        GTK_POLICY_AUTOMATIC);
242
243         title[0] = _("Registered templates");
244         clist_tmpls = gtk_clist_new_with_titles(1, title);
245         gtk_widget_show(clist_tmpls);
246         gtk_widget_set_usize(scroll1, -1, 140);
247         gtk_container_add(GTK_CONTAINER(scroll1), clist_tmpls);
248         gtk_clist_set_column_width(GTK_CLIST(clist_tmpls), 0, 80);
249         gtk_clist_set_selection_mode(GTK_CLIST(clist_tmpls),
250                                      GTK_SELECTION_BROWSE);
251         GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(clist_tmpls)->column[0].button,
252                                GTK_CAN_FOCUS);
253         gtk_signal_connect(GTK_OBJECT (clist_tmpls), "select_row",
254                            GTK_SIGNAL_FUNC (prefs_template_select_cb), NULL);
255
256         /* ok | cancel */
257         gtkut_button_set_create(&confirm_area, &ok_btn, _("OK"),
258                                 &cancel_btn, _("Cancel"), NULL, NULL);
259         gtk_widget_show(confirm_area);
260         gtk_box_pack_end(GTK_BOX(vbox2), confirm_area, FALSE, FALSE, 0);
261         gtk_widget_grab_default(ok_btn);
262
263         gtk_window_set_title(GTK_WINDOW(window), _("Templates"));
264
265         gtk_signal_connect(GTK_OBJECT(window), "delete_event",
266                            GTK_SIGNAL_FUNC(prefs_template_deleted_cb), NULL);
267         gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
268                            GTK_SIGNAL_FUNC(prefs_template_key_pressed_cb), NULL);
269         gtk_signal_connect(GTK_OBJECT(window), "focus_in_event",
270                            GTK_SIGNAL_FUNC(manage_window_focus_in), NULL);
271         gtk_signal_connect(GTK_OBJECT(window), "focus_out_event",
272                            GTK_SIGNAL_FUNC(manage_window_focus_out), NULL);
273         gtk_signal_connect(GTK_OBJECT(ok_btn), "clicked",
274                            GTK_SIGNAL_FUNC(prefs_template_ok_cb), NULL);
275         gtk_signal_connect(GTK_OBJECT(cancel_btn), "clicked",
276                             GTK_SIGNAL_FUNC(prefs_template_cancel_cb), NULL);
277
278         templates.window = window;
279         templates.ok_btn = ok_btn;
280         templates.clist_tmpls = clist_tmpls;
281         templates.entry_name = entry_name;
282         templates.entry_subject = entry_subject;
283         templates.entry_to = entry_to;
284         templates.text_value = text_value;
285 }
286
287 static void prefs_template_window_setup(void)
288 {
289         GtkCList *clist = GTK_CLIST(templates.clist_tmpls);
290         GSList *tmpl_list;
291         GSList *cur;
292         gchar *title[1];
293         gint row;
294         Template *tmpl;
295
296         manage_window_set_transient(GTK_WINDOW(templates.window));
297         gtk_widget_grab_focus(templates.ok_btn);
298
299         gtk_clist_freeze(clist);
300         gtk_clist_clear(clist);
301
302         title[0] = _("(New)");
303         row = gtk_clist_append(clist, title);
304         gtk_clist_set_row_data(clist, row, NULL);
305
306         tmpl_list = template_read_config();
307
308         for (cur = tmpl_list; cur != NULL; cur = cur->next) {
309                 tmpl = (Template *)cur->data;
310                 title[0] = tmpl->name;
311                 row = gtk_clist_append(clist, title);
312                 gtk_clist_set_row_data(clist, row, tmpl);
313         }
314
315         g_slist_free(tmpl_list);
316
317         gtk_clist_thaw(clist);
318 }
319
320 static void prefs_template_clear(void)
321 {
322         Template *tmpl;
323         gint row = 1;
324
325         while ((tmpl = gtk_clist_get_row_data
326                 (GTK_CLIST(templates.clist_tmpls), row)) != NULL) {
327                 template_free(tmpl);
328                 row++;
329         }
330
331         gtk_clist_clear(GTK_CLIST(templates.clist_tmpls));
332 }
333
334 static gint prefs_template_deleted_cb(GtkWidget *widget, GdkEventAny *event,
335                                       gpointer data)
336 {
337         prefs_template_cancel_cb();
338         return TRUE;
339 }
340
341 static void prefs_template_key_pressed_cb(GtkWidget *widget,
342                                           GdkEventKey *event, gpointer data)
343 {
344         if (event && event->keyval == GDK_Escape)
345                 prefs_template_cancel_cb();
346 }
347
348 static void prefs_template_ok_cb(void)
349 {
350         GSList *tmpl_list;
351
352         tmpl_list = prefs_template_get_list();
353         template_set_config(tmpl_list);
354         compose_reflect_prefs_all();
355         gtk_clist_clear(GTK_CLIST(templates.clist_tmpls));
356         gtk_widget_hide(templates.window);
357         inc_unlock();
358 }
359
360 static void prefs_template_cancel_cb(void)
361 {
362         prefs_template_clear();
363         gtk_widget_hide(templates.window);
364         inc_unlock();
365 }
366
367 static void prefs_template_select_cb(GtkCList *clist, gint row, gint column,
368                                      GdkEvent *event)
369 {
370         Template *tmpl;
371         Template tmpl_def;
372
373         tmpl_def.name = _("Template");
374         tmpl_def.subject = "";
375         tmpl_def.to = "";
376         tmpl_def.value = "";
377
378         if (!(tmpl = gtk_clist_get_row_data(clist, row)))
379                 tmpl = &tmpl_def;
380
381         gtk_entry_set_text(GTK_ENTRY(templates.entry_name), tmpl->name);
382         gtk_entry_set_text(GTK_ENTRY(templates.entry_to),
383                            tmpl->to ? tmpl->to : "");
384         gtk_entry_set_text(GTK_ENTRY(templates.entry_subject),
385                            tmpl->subject ? tmpl->subject : "");
386         
387         gtk_text_freeze(GTK_TEXT(templates.text_value));
388         gtk_text_set_point(GTK_TEXT(templates.text_value), 0);
389         gtk_text_forward_delete
390                 (GTK_TEXT(templates.text_value), 
391                  gtk_text_get_length(GTK_TEXT(templates.text_value)));
392         gtk_text_insert(GTK_TEXT(templates.text_value), NULL, NULL, NULL,
393                         tmpl->value, -1);
394         gtk_text_thaw(GTK_TEXT(templates.text_value));
395 }
396
397 static GSList *prefs_template_get_list(void)
398 {
399         gint row = 1;
400         GSList *tmpl_list = NULL;
401         Template *tmpl;
402
403         while ((tmpl = gtk_clist_get_row_data
404                 (GTK_CLIST(templates.clist_tmpls), row)) != NULL) {
405                 tmpl_list = g_slist_append(tmpl_list, tmpl);
406                 row++;
407         }
408
409         return tmpl_list;
410 }
411
412 static gint prefs_template_clist_set_row(gint row)
413 {
414         GtkCList *clist = GTK_CLIST(templates.clist_tmpls);
415         Template *tmpl;
416         Template *tmp_tmpl;
417         gchar *name;
418         gchar *subject;
419         gchar *to;
420         gchar *value;
421         gchar *title[1];
422
423         g_return_val_if_fail(row != 0, -1);
424
425         value = gtk_editable_get_chars(GTK_EDITABLE(templates.text_value),
426                                        0, -1);
427
428         if (value && *value != '\0') {
429                 gchar *parsed_buf;
430                 MsgInfo dummyinfo;
431
432                 memset(&dummyinfo, 0, sizeof(MsgInfo));
433                 quote_fmt_init(&dummyinfo, NULL);
434                 quote_fmt_scan_string(value);
435                 quote_fmt_parse();
436                 parsed_buf = quote_fmt_get_buffer();
437                 if (!parsed_buf) {
438                         alertpanel_error(_("Template format error."));
439                         g_free(value);
440                         return -1;
441                 }
442         }
443
444         name = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_name),
445                                       0, -1);
446         subject = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_subject),
447                                       0, -1);
448         to = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_to),
449                                     0, -1);
450
451         tmpl = g_new(Template, 1);
452         tmpl->name = name;
453         tmpl->subject = subject;
454         tmpl->to = to;
455         tmpl->value = value;
456
457         title[0] = name;
458
459         if (row < 0) {
460                 row = gtk_clist_append(clist, title);
461         } else {
462                 gtk_clist_set_text(clist, row, 0, name);
463                 tmp_tmpl = gtk_clist_get_row_data(clist, row);
464                 if (tmp_tmpl)
465                         template_free(tmp_tmpl);
466         }
467
468         gtk_clist_set_row_data(clist, row, tmpl);
469         return row;
470 }
471
472 static void prefs_template_register_cb(void)
473 {
474         prefs_template_clist_set_row(-1);
475 }
476
477 static void prefs_template_substitute_cb(void)
478 {
479         GtkCList *clist = GTK_CLIST(templates.clist_tmpls);
480         Template *tmpl;
481         gint row;
482
483         if (!clist->selection) return;
484
485         row = GPOINTER_TO_INT(clist->selection->data);
486         if (row == 0) return;
487
488         tmpl = gtk_clist_get_row_data(clist, row);
489         if (!tmpl) return;
490
491         prefs_template_clist_set_row(row);
492 }
493
494 static void prefs_template_delete_cb(void)
495 {
496         GtkCList *clist = GTK_CLIST(templates.clist_tmpls);
497         Template *tmpl;
498         gint row;
499
500         if (!clist->selection) return;
501         row = GPOINTER_TO_INT(clist->selection->data);
502         if (row == 0) return;
503
504         if (alertpanel(_("Delete template"),
505                        _("Do you really want to delete this template?"),
506                        _("Yes"), _("No"), NULL) == G_ALERTALTERNATE)
507                 return;
508
509         tmpl = gtk_clist_get_row_data(clist, row);
510         template_free(tmpl);
511         gtk_clist_remove(clist, row);
512 }