b8e8f8c058aca11d93a9ca3d64b109b12aaf1324
[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 "compose.h"
39 #include "addr_compl.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
154         label1 = gtk_label_new(_("Template name"));
155         gtk_widget_show(label1);
156         gtk_box_pack_start(GTK_BOX(hbox1), label1, FALSE, FALSE, 0);
157
158         entry_name = gtk_entry_new();
159         gtk_widget_show(entry_name);
160         gtk_box_pack_start(GTK_BOX(hbox1), entry_name, TRUE, TRUE, 0);
161
162         /* table for headers */
163         table = gtk_table_new(2, 2, FALSE);
164         gtk_widget_show(table);
165         gtk_box_pack_start(GTK_BOX(vbox1), table, FALSE, FALSE, 0);
166         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
167         gtk_table_set_col_spacings(GTK_TABLE(table), 4);
168
169         ADD_ENTRY(entry_to, _("To:"), 0);
170         address_completion_register_entry(GTK_ENTRY(entry_to));
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(quote_fmt_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         address_completion_start(window);
279
280         templates.window = window;
281         templates.ok_btn = ok_btn;
282         templates.clist_tmpls = clist_tmpls;
283         templates.entry_name = entry_name;
284         templates.entry_subject = entry_subject;
285         templates.entry_to = entry_to;
286         templates.text_value = text_value;
287 }
288
289 static void prefs_template_window_setup(void)
290 {
291         GtkCList *clist = GTK_CLIST(templates.clist_tmpls);
292         GSList *tmpl_list;
293         GSList *cur;
294         gchar *title[1];
295         gint row;
296         Template *tmpl;
297
298         manage_window_set_transient(GTK_WINDOW(templates.window));
299         gtk_widget_grab_focus(templates.ok_btn);
300
301         gtk_clist_freeze(clist);
302         gtk_clist_clear(clist);
303
304         title[0] = _("(New)");
305         row = gtk_clist_append(clist, title);
306         gtk_clist_set_row_data(clist, row, NULL);
307
308         tmpl_list = template_read_config();
309
310         for (cur = tmpl_list; cur != NULL; cur = cur->next) {
311                 tmpl = (Template *)cur->data;
312                 title[0] = tmpl->name;
313                 row = gtk_clist_append(clist, title);
314                 gtk_clist_set_row_data(clist, row, tmpl);
315         }
316
317         g_slist_free(tmpl_list);
318
319         gtk_clist_thaw(clist);
320 }
321
322 static void prefs_template_clear(void)
323 {
324         Template *tmpl;
325         gint row = 1;
326
327         while ((tmpl = gtk_clist_get_row_data
328                 (GTK_CLIST(templates.clist_tmpls), row)) != NULL) {
329                 template_free(tmpl);
330                 row++;
331         }
332
333         gtk_clist_clear(GTK_CLIST(templates.clist_tmpls));
334 }
335
336 static gint prefs_template_deleted_cb(GtkWidget *widget, GdkEventAny *event,
337                                       gpointer data)
338 {
339         prefs_template_cancel_cb();
340         return TRUE;
341 }
342
343 static void prefs_template_key_pressed_cb(GtkWidget *widget,
344                                           GdkEventKey *event, gpointer data)
345 {
346         if (event && event->keyval == GDK_Escape)
347                 prefs_template_cancel_cb();
348 }
349
350 static void prefs_template_ok_cb(void)
351 {
352         GSList *tmpl_list;
353
354         tmpl_list = prefs_template_get_list();
355         template_set_config(tmpl_list);
356         compose_reflect_prefs_all();
357         gtk_clist_clear(GTK_CLIST(templates.clist_tmpls));
358         gtk_widget_hide(templates.window);
359         inc_unlock();
360 }
361
362 static void prefs_template_cancel_cb(void)
363 {
364         prefs_template_clear();
365         gtk_widget_hide(templates.window);
366         inc_unlock();
367 }
368
369 static void prefs_template_select_cb(GtkCList *clist, gint row, gint column,
370                                      GdkEvent *event)
371 {
372         Template *tmpl;
373         Template tmpl_def;
374
375         tmpl_def.name = _("Template");
376         tmpl_def.subject = "";
377         tmpl_def.to = "";
378         tmpl_def.value = "";
379
380         if (!(tmpl = gtk_clist_get_row_data(clist, row)))
381                 tmpl = &tmpl_def;
382
383         gtk_entry_set_text(GTK_ENTRY(templates.entry_name), tmpl->name);
384         gtk_entry_set_text(GTK_ENTRY(templates.entry_to),
385                            tmpl->to ? tmpl->to : "");
386         gtk_entry_set_text(GTK_ENTRY(templates.entry_subject),
387                            tmpl->subject ? tmpl->subject : "");
388         
389         gtk_text_freeze(GTK_TEXT(templates.text_value));
390         gtk_text_set_point(GTK_TEXT(templates.text_value), 0);
391         gtk_text_forward_delete
392                 (GTK_TEXT(templates.text_value), 
393                  gtk_text_get_length(GTK_TEXT(templates.text_value)));
394         gtk_text_insert(GTK_TEXT(templates.text_value), NULL, NULL, NULL,
395                         tmpl->value, -1);
396         gtk_text_thaw(GTK_TEXT(templates.text_value));
397 }
398
399 static GSList *prefs_template_get_list(void)
400 {
401         gint row = 1;
402         GSList *tmpl_list = NULL;
403         Template *tmpl;
404
405         while ((tmpl = gtk_clist_get_row_data
406                 (GTK_CLIST(templates.clist_tmpls), row)) != NULL) {
407                 tmpl_list = g_slist_append(tmpl_list, tmpl);
408                 row++;
409         }
410
411         return tmpl_list;
412 }
413
414 static gint prefs_template_clist_set_row(gint row)
415 {
416         GtkCList *clist = GTK_CLIST(templates.clist_tmpls);
417         Template *tmpl;
418         Template *tmp_tmpl;
419         gchar *name;
420         gchar *subject;
421         gchar *to;
422         gchar *value;
423         gchar *title[1];
424
425         g_return_val_if_fail(row != 0, -1);
426
427         value = gtk_editable_get_chars(GTK_EDITABLE(templates.text_value),
428                                        0, -1);
429
430         if (value && *value != '\0') {
431                 gchar *parsed_buf;
432                 MsgInfo dummyinfo;
433
434                 memset(&dummyinfo, 0, sizeof(MsgInfo));
435                 quote_fmt_init(&dummyinfo, NULL, NULL);
436                 quote_fmt_scan_string(value);
437                 quote_fmt_parse();
438                 parsed_buf = quote_fmt_get_buffer();
439                 if (!parsed_buf) {
440                         alertpanel_error(_("Template format error."));
441                         g_free(value);
442                         return -1;
443                 }
444         }
445
446         name = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_name),
447                                       0, -1);
448         subject = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_subject),
449                                          0, -1);
450         to = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_to),
451                                     0, -1);
452
453         if (subject && *subject == '\0') {
454                 g_free(subject);
455                 subject = NULL;
456         }
457         if (to && *to == '\0') {
458                 g_free(to);
459                 to = NULL;
460         }
461
462         tmpl = g_new(Template, 1);
463         tmpl->name = name;
464         tmpl->subject = subject;
465         tmpl->to = to;
466         tmpl->value = value;
467
468         title[0] = name;
469
470         if (row < 0) {
471                 row = gtk_clist_append(clist, title);
472         } else {
473                 gtk_clist_set_text(clist, row, 0, name);
474                 tmp_tmpl = gtk_clist_get_row_data(clist, row);
475                 if (tmp_tmpl)
476                         template_free(tmp_tmpl);
477         }
478
479         gtk_clist_set_row_data(clist, row, tmpl);
480         return row;
481 }
482
483 static void prefs_template_register_cb(void)
484 {
485         prefs_template_clist_set_row(-1);
486 }
487
488 static void prefs_template_substitute_cb(void)
489 {
490         GtkCList *clist = GTK_CLIST(templates.clist_tmpls);
491         Template *tmpl;
492         gint row;
493
494         if (!clist->selection) return;
495
496         row = GPOINTER_TO_INT(clist->selection->data);
497         if (row == 0) return;
498
499         tmpl = gtk_clist_get_row_data(clist, row);
500         if (!tmpl) return;
501
502         prefs_template_clist_set_row(row);
503 }
504
505 static void prefs_template_delete_cb(void)
506 {
507         GtkCList *clist = GTK_CLIST(templates.clist_tmpls);
508         Template *tmpl;
509         gint row;
510
511         if (!clist->selection) return;
512         row = GPOINTER_TO_INT(clist->selection->data);
513         if (row == 0) return;
514
515         if (alertpanel(_("Delete template"),
516                        _("Do you really want to delete this template?"),
517                        _("Yes"), _("No"), NULL) == G_ALERTALTERNATE)
518                 return;
519
520         tmpl = gtk_clist_get_row_data(clist, row);
521         template_free(tmpl);
522         gtk_clist_remove(clist, row);
523 }