sync with 0.9.10claws57 HEAD
[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 gboolean 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_TOPLEVEL);
138         gtk_window_set_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_view_new();
190         gtk_widget_show(text_value);
191         gtk_widget_set_size_request(text_value, -1, 120);
192         gtk_container_add(GTK_CONTAINER(scroll2), text_value);
193         gtk_text_view_set_editable(GTK_TEXT_VIEW(text_value), TRUE);
194         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text_value), GTK_WRAP_WORD);
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_size_request(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         g_signal_connect(G_OBJECT (reg_btn), "clicked",
220                          G_CALLBACK (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         g_signal_connect(G_OBJECT(subst_btn), "clicked",
226                          G_CALLBACK(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         g_signal_connect(G_OBJECT(del_btn), "clicked",
233                          G_CALLBACK(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         g_signal_connect(G_OBJECT(desc_btn), "clicked",
239                          G_CALLBACK(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_size_request(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         g_signal_connect(G_OBJECT (clist_tmpls), "select_row",
260                          G_CALLBACK (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         g_signal_connect(G_OBJECT(window), "delete_event",
272                          G_CALLBACK(prefs_template_deleted_cb), NULL);
273         g_signal_connect(G_OBJECT(window), "key_press_event",
274                          G_CALLBACK(prefs_template_key_pressed_cb), NULL);
275         MANAGE_WINDOW_SIGNALS_CONNECT(window);
276         g_signal_connect(G_OBJECT(ok_btn), "clicked",
277                          G_CALLBACK(prefs_template_ok_cb), NULL);
278         g_signal_connect(G_OBJECT(cancel_btn), "clicked",
279                          G_CALLBACK(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 gboolean 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         return FALSE;
354 }
355
356 static void prefs_template_ok_cb(void)
357 {
358         GSList *tmpl_list;
359
360         tmpl_list = prefs_template_get_list();
361         template_set_config(tmpl_list);
362         compose_reflect_prefs_all();
363         gtk_clist_clear(GTK_CLIST(templates.clist_tmpls));
364         gtk_widget_hide(templates.window);
365         inc_unlock();
366 }
367
368 static void prefs_template_cancel_cb(void)
369 {
370         prefs_template_clear();
371         gtk_widget_hide(templates.window);
372         inc_unlock();
373 }
374
375 static void prefs_template_select_cb(GtkCList *clist, gint row, gint column,
376                                      GdkEvent *event)
377 {
378         Template *tmpl;
379         Template tmpl_def;
380         GtkTextBuffer *buffer;
381         GtkTextIter iter;
382
383         tmpl_def.name = _("Template");
384         tmpl_def.subject = "";
385         tmpl_def.to = "";
386         tmpl_def.cc = "";
387         tmpl_def.bcc = "";      
388         tmpl_def.value = "";
389
390         if (!(tmpl = gtk_clist_get_row_data(clist, row)))
391                 tmpl = &tmpl_def;
392
393         gtk_entry_set_text(GTK_ENTRY(templates.entry_name), tmpl->name);
394         gtk_entry_set_text(GTK_ENTRY(templates.entry_to),
395                            tmpl->to ? tmpl->to : "");
396         gtk_entry_set_text(GTK_ENTRY(templates.entry_cc),
397                            tmpl->cc ? tmpl->cc : "");
398         gtk_entry_set_text(GTK_ENTRY(templates.entry_bcc),
399                            tmpl->bcc ? tmpl->bcc : "");                 
400         gtk_entry_set_text(GTK_ENTRY(templates.entry_subject),
401                            tmpl->subject ? tmpl->subject : "");
402         
403         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
404         gtk_text_buffer_set_text(buffer, "\0", -1);
405         gtk_text_buffer_get_start_iter(buffer, &iter);
406         gtk_text_buffer_insert(buffer, &iter, tmpl->value, -1);
407 }
408
409 static GSList *prefs_template_get_list(void)
410 {
411         gint row = 1;
412         GSList *tmpl_list = NULL;
413         Template *tmpl;
414
415         while ((tmpl = gtk_clist_get_row_data
416                 (GTK_CLIST(templates.clist_tmpls), row)) != NULL) {
417                 tmpl_list = g_slist_append(tmpl_list, tmpl);
418                 row++;
419         }
420
421         return tmpl_list;
422 }
423
424 static gint prefs_template_clist_set_row(gint row)
425 {
426         GtkCList *clist = GTK_CLIST(templates.clist_tmpls);
427         Template *tmpl;
428         Template *tmp_tmpl;
429         gchar *name;
430         gchar *subject;
431         gchar *to;
432         gchar *cc;
433         gchar *bcc;     
434         gchar *value;
435         gchar *title[1];
436         GtkTextBuffer *buffer;
437         GtkTextIter start, end;
438
439         g_return_val_if_fail(row != 0, -1);
440
441         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
442         gtk_text_buffer_get_start_iter(buffer, &start);
443         gtk_text_buffer_get_iter_at_offset(buffer, &end, -1);
444         value = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
445
446         if (value && *value != '\0') {
447                 gchar *parsed_buf;
448                 MsgInfo dummyinfo;
449
450                 memset(&dummyinfo, 0, sizeof(MsgInfo));
451                 quote_fmt_init(&dummyinfo, NULL, NULL);
452                 quote_fmt_scan_string(value);
453                 quote_fmt_parse();
454                 parsed_buf = quote_fmt_get_buffer();
455                 if (!parsed_buf) {
456                         alertpanel_error(_("Template format error."));
457                         g_free(value);
458                         return -1;
459                 }
460         }
461
462         name = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_name),
463                                       0, -1);
464         subject = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_subject),
465                                          0, -1);
466         to = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_to),
467                                     0, -1);
468         cc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_cc),
469                                     0, -1);
470         bcc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_bcc),
471                                     0, -1);
472
473         if (subject && *subject == '\0') {
474                 g_free(subject);
475                 subject = NULL;
476         }
477         if (to && *to == '\0') {
478                 g_free(to);
479                 to = NULL;
480         }
481         if (cc && *cc == '\0') {
482                 g_free(cc);
483                 cc = NULL;
484         }
485         if (bcc && *bcc == '\0') {
486                 g_free(bcc);
487                 bcc = NULL;
488         }
489         
490         tmpl = g_new(Template, 1);
491         tmpl->name = name;
492         tmpl->subject = subject;
493         tmpl->to = to;
494         tmpl->cc = cc;
495         tmpl->bcc = bcc;        
496         tmpl->value = value;
497
498         title[0] = name;
499
500         if (row < 0) {
501                 row = gtk_clist_append(clist, title);
502         } else {
503                 gtk_clist_set_text(clist, row, 0, name);
504                 tmp_tmpl = gtk_clist_get_row_data(clist, row);
505                 if (tmp_tmpl)
506                         template_free(tmp_tmpl);
507         }
508
509         gtk_clist_set_row_data(clist, row, tmpl);
510         return row;
511 }
512
513 static void prefs_template_register_cb(void)
514 {
515         prefs_template_clist_set_row(-1);
516 }
517
518 static void prefs_template_substitute_cb(void)
519 {
520         GtkCList *clist = GTK_CLIST(templates.clist_tmpls);
521         Template *tmpl;
522         gint row;
523
524         if (!clist->selection) return;
525
526         row = GPOINTER_TO_INT(clist->selection->data);
527         if (row == 0) return;
528
529         tmpl = gtk_clist_get_row_data(clist, row);
530         if (!tmpl) return;
531
532         prefs_template_clist_set_row(row);
533 }
534
535 static void prefs_template_delete_cb(void)
536 {
537         GtkCList *clist = GTK_CLIST(templates.clist_tmpls);
538         Template *tmpl;
539         gint row;
540
541         if (!clist->selection) return;
542         row = GPOINTER_TO_INT(clist->selection->data);
543         if (row == 0) return;
544
545         if (alertpanel(_("Delete template"),
546                        _("Do you really want to delete this template?"),
547                        _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
548                 return;
549
550         tmpl = gtk_clist_get_row_data(clist, row);
551         template_free(tmpl);
552         gtk_clist_remove(clist, row);
553 }