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