sync with sylpheed 0.6.5cvs14
[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 *text_value;
47 } templates;
48
49 /* widget creating functions */
50 static void prefs_template_window_create        (void);
51 static void prefs_template_window_setup         (void);
52 static void prefs_template_clear                (void);
53
54 static GSList *prefs_template_get_list          (void);
55
56 /* callbacks */
57 static gint prefs_template_deleted_cb           (GtkWidget      *widget,
58                                                  GdkEventAny    *event,
59                                                  gpointer        data);
60 static void prefs_template_key_pressed_cb       (GtkWidget      *widget,
61                                                  GdkEventKey    *event,
62                                                  gpointer        data);
63 static void prefs_template_cancel_cb            (void);
64 static void prefs_template_ok_cb                (void);
65 static void prefs_template_select_cb            (GtkCList       *clist,
66                                                  gint            row,
67                                                  gint            column,
68                                                  GdkEvent       *event);
69 static void prefs_template_register_cb          (void);
70 static void prefs_template_substitute_cb        (void);
71 static void prefs_template_delete_cb            (void);
72
73 /* Called from mainwindow.c */
74 void prefs_template_open(void)
75 {
76         inc_lock();
77
78         if (!templates.window)
79                 prefs_template_window_create();
80
81         prefs_template_window_setup();
82         gtk_widget_show(templates.window);
83 }
84
85 static void prefs_template_window_create(void)
86 {
87         /* window structure ;) */
88         GtkWidget *window;
89         GtkWidget   *vpaned;
90         GtkWidget     *vbox1;
91         GtkWidget       *hbox1;
92         GtkWidget         *label1;
93         GtkWidget         *entry_name;
94         GtkWidget         *entry_subject;
95         GtkWidget       *scroll2;
96         GtkWidget         *text_value;
97         GtkWidget     *vbox2;
98         GtkWidget       *hbox2;
99         GtkWidget         *arrow1;
100         GtkWidget         *hbox3;
101         GtkWidget           *reg_btn;
102         GtkWidget           *subst_btn;
103         GtkWidget           *del_btn;
104         GtkWidget         *desc_btn;
105         GtkWidget       *scroll1;
106         GtkWidget         *clist_tmpls;
107         GtkWidget       *confirm_area;
108         GtkWidget         *ok_btn;
109         GtkWidget         *cancel_btn;
110
111         gchar *title[1];
112
113         /* main window */
114         window = gtk_window_new(GTK_WINDOW_DIALOG);
115         gtk_window_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
116         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
117         gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, FALSE);
118         gtk_window_set_default_size(GTK_WINDOW(window), 400, -1);
119
120         /* vpaned to separate template settings from templates list */
121         vpaned = gtk_vpaned_new();
122         gtk_widget_show(vpaned);
123         gtk_container_add(GTK_CONTAINER(window), vpaned);
124
125         /* vbox to handle template name and content */
126         vbox1 = gtk_vbox_new(FALSE, 6);
127         gtk_widget_show(vbox1);
128         gtk_container_set_border_width(GTK_CONTAINER(vbox1), 8);
129         gtk_paned_pack1(GTK_PANED(vpaned), vbox1, FALSE, FALSE);
130
131         /* hbox for a label and template name entry */
132         hbox1 = gtk_hbox_new(FALSE, 8);
133         gtk_widget_show(hbox1);
134         gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, FALSE, 0);
135         gtk_container_set_border_width(GTK_CONTAINER(hbox1), 2);
136
137         /* self-documenting */
138         label1 = gtk_label_new(_("Template name"));
139         gtk_widget_show(label1);
140         gtk_box_pack_start(GTK_BOX(hbox1), label1, FALSE, FALSE, 0);
141
142         /* holds template name */
143         entry_name = gtk_entry_new();
144         gtk_widget_show(entry_name);
145         gtk_box_pack_start(GTK_BOX(hbox1), entry_name, TRUE, TRUE, 0);
146
147         /* hbox for a label and subject entry */
148         hbox1 = gtk_hbox_new(FALSE, 8);
149         gtk_widget_show(hbox1);
150         gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, FALSE, 0);
151         gtk_container_set_border_width(GTK_CONTAINER(hbox1), 2);
152
153         /* self-documenting */
154         label1 = gtk_label_new(_("Subject"));
155         gtk_widget_show(label1);
156         gtk_box_pack_start(GTK_BOX(hbox1), label1, FALSE, FALSE, 0);
157
158         /* holds subject */
159         entry_subject = gtk_entry_new();
160         gtk_widget_show(entry_subject);
161         gtk_box_pack_start(GTK_BOX(hbox1), entry_subject, TRUE, TRUE, 0);
162
163         /* template content */
164         scroll2 = gtk_scrolled_window_new(NULL, NULL);
165         gtk_widget_show(scroll2);
166         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll2),
167                                        GTK_POLICY_NEVER,
168                                        GTK_POLICY_ALWAYS);
169         gtk_box_pack_start(GTK_BOX(vbox1), scroll2, TRUE, TRUE, 0);
170
171         text_value = gtk_text_new(NULL, NULL);
172         gtk_widget_show(text_value);
173         gtk_widget_set_usize(text_value, -1, 120);
174         gtk_container_add(GTK_CONTAINER(scroll2), text_value);
175         gtk_text_set_editable(GTK_TEXT(text_value), TRUE);
176         gtk_text_set_word_wrap(GTK_TEXT(text_value), TRUE);
177
178         /* vbox for buttons and templates list */
179         vbox2 = gtk_vbox_new(FALSE, 6);
180         gtk_widget_show(vbox2);
181         gtk_container_set_border_width(GTK_CONTAINER(vbox2), 8);
182         gtk_paned_pack2(GTK_PANED(vpaned), vbox2, TRUE, FALSE);
183
184         /* register | substitute | delete */
185         hbox2 = gtk_hbox_new(FALSE, 4);
186         gtk_widget_show(hbox2);
187         gtk_box_pack_start(GTK_BOX(vbox2), hbox2, FALSE, FALSE, 0);
188
189         arrow1 = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
190         gtk_widget_show(arrow1);
191         gtk_box_pack_start(GTK_BOX(hbox2), arrow1, FALSE, FALSE, 0);
192         gtk_widget_set_usize(arrow1, -1, 16);
193
194         hbox3 = gtk_hbox_new(TRUE, 4);
195         gtk_widget_show(hbox3);
196         gtk_box_pack_start(GTK_BOX(hbox2), hbox3, FALSE, FALSE, 0);
197
198         reg_btn = gtk_button_new_with_label(_("Register"));
199         gtk_widget_show(reg_btn);
200         gtk_box_pack_start(GTK_BOX(hbox3), reg_btn, FALSE, TRUE, 0);
201         gtk_signal_connect(GTK_OBJECT (reg_btn), "clicked",
202                            GTK_SIGNAL_FUNC (prefs_template_register_cb), NULL);
203
204         subst_btn = gtk_button_new_with_label(_(" Substitute "));
205         gtk_widget_show(subst_btn);
206         gtk_box_pack_start(GTK_BOX(hbox3), subst_btn, FALSE, TRUE, 0);
207         gtk_signal_connect(GTK_OBJECT(subst_btn), "clicked",
208                            GTK_SIGNAL_FUNC(prefs_template_substitute_cb),
209                            NULL);
210
211         del_btn = gtk_button_new_with_label(_("Delete"));
212         gtk_widget_show(del_btn);
213         gtk_box_pack_start(GTK_BOX(hbox3), del_btn, FALSE, TRUE, 0);
214         gtk_signal_connect(GTK_OBJECT(del_btn), "clicked",
215                            GTK_SIGNAL_FUNC(prefs_template_delete_cb), NULL);
216
217         desc_btn = gtk_button_new_with_label(_(" Symbols "));
218         gtk_widget_show(desc_btn);
219         gtk_box_pack_end(GTK_BOX(hbox2), desc_btn, FALSE, FALSE, 0);
220         gtk_signal_connect(GTK_OBJECT(desc_btn), "clicked",
221                            GTK_SIGNAL_FUNC(prefs_quote_description), NULL);
222
223         /* templates list */
224         scroll1 = gtk_scrolled_window_new(NULL, NULL);
225         gtk_widget_show(scroll1);
226         gtk_box_pack_start(GTK_BOX(vbox2), scroll1, TRUE, TRUE, 0);
227         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll1),
228                                        GTK_POLICY_AUTOMATIC,
229                                        GTK_POLICY_AUTOMATIC);
230
231         title[0] = _("Registered templates");
232         clist_tmpls = gtk_clist_new_with_titles(1, title);
233         gtk_widget_show(clist_tmpls);
234         gtk_widget_set_usize(scroll1, -1, 140);
235         gtk_container_add(GTK_CONTAINER(scroll1), clist_tmpls);
236         gtk_clist_set_column_width(GTK_CLIST(clist_tmpls), 0, 80);
237         gtk_clist_set_selection_mode(GTK_CLIST(clist_tmpls),
238                                      GTK_SELECTION_BROWSE);
239         GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(clist_tmpls)->column[0].button,
240                                GTK_CAN_FOCUS);
241         gtk_signal_connect(GTK_OBJECT (clist_tmpls), "select_row",
242                            GTK_SIGNAL_FUNC (prefs_template_select_cb), NULL);
243
244         /* ok | cancel */
245         gtkut_button_set_create(&confirm_area, &ok_btn, _("OK"),
246                                 &cancel_btn, _("Cancel"), NULL, NULL);
247         gtk_widget_show(confirm_area);
248         gtk_box_pack_end(GTK_BOX(vbox2), confirm_area, FALSE, FALSE, 0);
249         gtk_widget_grab_default(ok_btn);
250
251         gtk_window_set_title(GTK_WINDOW(window), _("Templates"));
252
253         gtk_signal_connect(GTK_OBJECT(window), "delete_event",
254                            GTK_SIGNAL_FUNC(prefs_template_deleted_cb), NULL);
255         gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
256                            GTK_SIGNAL_FUNC(prefs_template_key_pressed_cb), NULL);
257         gtk_signal_connect(GTK_OBJECT(window), "focus_in_event",
258                            GTK_SIGNAL_FUNC(manage_window_focus_in), NULL);
259         gtk_signal_connect(GTK_OBJECT(window), "focus_out_event",
260                            GTK_SIGNAL_FUNC(manage_window_focus_out), NULL);
261         gtk_signal_connect(GTK_OBJECT(ok_btn), "clicked",
262                            GTK_SIGNAL_FUNC(prefs_template_ok_cb), NULL);
263         gtk_signal_connect(GTK_OBJECT(cancel_btn), "clicked",
264                             GTK_SIGNAL_FUNC(prefs_template_cancel_cb), NULL);
265
266         templates.window = window;
267         templates.ok_btn = ok_btn;
268         templates.clist_tmpls = clist_tmpls;
269         templates.entry_name = entry_name;
270         templates.entry_subject = entry_subject;
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 *value;
404         gchar *title[1];
405
406         g_return_val_if_fail(row != 0, -1);
407
408         name = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_name),
409                                       0, -1);
410         subject = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_subject),
411                                       0, -1);
412         value = gtk_editable_get_chars(GTK_EDITABLE(templates.text_value),
413                                        0, -1);
414
415         tmpl = g_new(Template, 1);
416         tmpl->name = name;
417         tmpl->subject = subject;
418         tmpl->value = value;
419
420         title[0] = name;
421
422         if (row < 0) {
423                 row = gtk_clist_append(clist, title);
424         } else {
425                 gtk_clist_set_text(clist, row, 0, name);
426                 tmp_tmpl = gtk_clist_get_row_data(clist, row);
427                 if (tmp_tmpl)
428                         template_free(tmp_tmpl);
429         }
430
431         gtk_clist_set_row_data(clist, row, tmpl);
432         return row;
433 }
434
435 static void prefs_template_register_cb(void)
436 {
437         prefs_template_clist_set_row(-1);
438 }
439
440 static void prefs_template_substitute_cb(void)
441 {
442         GtkCList *clist = GTK_CLIST(templates.clist_tmpls);
443         Template *tmpl;
444         gint row;
445
446         if (!clist->selection) return;
447
448         row = GPOINTER_TO_INT(clist->selection->data);
449         if (row == 0) return;
450
451         tmpl = gtk_clist_get_row_data(clist, row);
452         if (!tmpl) return;
453
454         prefs_template_clist_set_row(row);
455 }
456
457 static void prefs_template_delete_cb(void)
458 {
459         GtkCList *clist = GTK_CLIST(templates.clist_tmpls);
460         Template *tmpl;
461         gint row;
462
463         if (!clist->selection) return;
464         row = GPOINTER_TO_INT(clist->selection->data);
465         if (row == 0) return;
466
467         if (alertpanel(_("Delete template"),
468                        _("Do you really want to delete this template?"),
469                        _("Yes"), _("No"), NULL) == G_ALERTALTERNATE)
470                 return;
471
472         tmpl = gtk_clist_get_row_data(clist, row);
473         template_free(tmpl);
474         gtk_clist_remove(clist, row);
475 }