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