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