Statistics
| Revision:

root / src / prefs_template.c @ 1

History | View | Annotate | Download (14.8 KB)

1
/*
2
 * Sylpheed templates subsystem 
3
 * Copyright (C) 2001 Alexander Barinov
4
 * Copyright (C) 2001-2004 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_to;
49
        GtkWidget *entry_cc;
50
        GtkWidget *entry_subject;
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_subject;
117
        GtkWidget       *scroll2;
118
        GtkWidget         *text_value;
119
        GtkWidget     *vbox2;
120
        GtkWidget       *hbox2;
121
        GtkWidget         *arrow1;
122
        GtkWidget         *hbox3;
123
        GtkWidget           *reg_btn;
124
        GtkWidget           *subst_btn;
125
        GtkWidget           *del_btn;
126
        GtkWidget         *desc_btn;
127
        GtkWidget       *scroll1;
128
        GtkWidget         *clist_tmpls;
129
        GtkWidget       *confirm_area;
130
        GtkWidget         *ok_btn;
131
        GtkWidget         *cancel_btn;
132

    
133
        gchar *title[1];
134

    
135
        /* main window */
136
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
137
        gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
138
        gtk_window_set_modal(GTK_WINDOW(window), TRUE);
139
        gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, FALSE);
140
        gtk_window_set_default_size(GTK_WINDOW(window), 400, -1);
141

    
142
        /* vpaned to separate template settings from templates list */
143
        vpaned = gtk_vpaned_new();
144
        gtk_widget_show(vpaned);
145
        gtk_container_add(GTK_CONTAINER(window), vpaned);
146

    
147
        /* vbox to handle template name and content */
148
        vbox1 = gtk_vbox_new(FALSE, 6);
149
        gtk_widget_show(vbox1);
150
        gtk_container_set_border_width(GTK_CONTAINER(vbox1), 8);
151
        gtk_paned_pack1(GTK_PANED(vpaned), vbox1, FALSE, FALSE);
152

    
153
        hbox1 = gtk_hbox_new(FALSE, 8);
154
        gtk_widget_show(hbox1);
155
        gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, FALSE, 0);
156

    
157
        label1 = gtk_label_new(_("Template name"));
158
        gtk_widget_show(label1);
159
        gtk_box_pack_start(GTK_BOX(hbox1), label1, FALSE, FALSE, 0);
160

    
161
        entry_name = gtk_entry_new();
162
        gtk_widget_show(entry_name);
163
        gtk_box_pack_start(GTK_BOX(hbox1), entry_name, TRUE, TRUE, 0);
164

    
165
        /* table for headers */
166
        table = gtk_table_new(3, 2, FALSE);
167
        gtk_widget_show(table);
168
        gtk_box_pack_start(GTK_BOX(vbox1), table, FALSE, FALSE, 0);
169
        gtk_table_set_row_spacings(GTK_TABLE(table), 4);
170
        gtk_table_set_col_spacings(GTK_TABLE(table), 4);
171

    
172
        ADD_ENTRY(entry_to, _("To:"), 0);
173
        address_completion_register_entry(GTK_ENTRY(entry_to));
174
        ADD_ENTRY(entry_cc, _("Cc:"), 1);
175
        address_completion_register_entry(GTK_ENTRY(entry_cc));
176
        ADD_ENTRY(entry_subject, _("Subject:"), 2);
177

    
178
#undef ADD_ENTRY
179

    
180
        /* template content */
181
        scroll2 = gtk_scrolled_window_new(NULL, NULL);
182
        gtk_widget_show(scroll2);
183
        gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll2),
184
                                       GTK_POLICY_NEVER,
185
                                       GTK_POLICY_ALWAYS);
186
        gtk_box_pack_start(GTK_BOX(vbox1), scroll2, TRUE, TRUE, 0);
187

    
188
        text_value = gtk_text_view_new();
189
        gtk_widget_show(text_value);
190
        gtk_widget_set_size_request(text_value, -1, 120);
191
        gtk_container_add(GTK_CONTAINER(scroll2), text_value);
192
        gtk_text_view_set_editable(GTK_TEXT_VIEW(text_value), TRUE);
193
        gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text_value), GTK_WRAP_WORD);
194

    
195
        /* vbox for buttons and templates list */
196
        vbox2 = gtk_vbox_new(FALSE, 6);
197
        gtk_widget_show(vbox2);
198
        gtk_container_set_border_width(GTK_CONTAINER(vbox2), 8);
199
        gtk_paned_pack2(GTK_PANED(vpaned), vbox2, TRUE, FALSE);
200

    
201
        /* register | substitute | delete */
202
        hbox2 = gtk_hbox_new(FALSE, 4);
203
        gtk_widget_show(hbox2);
204
        gtk_box_pack_start(GTK_BOX(vbox2), hbox2, FALSE, FALSE, 0);
205

    
206
        arrow1 = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
207
        gtk_widget_show(arrow1);
208
        gtk_box_pack_start(GTK_BOX(hbox2), arrow1, FALSE, FALSE, 0);
209
        gtk_widget_set_size_request(arrow1, -1, 16);
210

    
211
        hbox3 = gtk_hbox_new(TRUE, 4);
212
        gtk_widget_show(hbox3);
213
        gtk_box_pack_start(GTK_BOX(hbox2), hbox3, FALSE, FALSE, 0);
214

    
215
        reg_btn = gtk_button_new_with_label(_("Register"));
216
        gtk_widget_show(reg_btn);
217
        gtk_box_pack_start(GTK_BOX(hbox3), reg_btn, FALSE, TRUE, 0);
218
        g_signal_connect(G_OBJECT (reg_btn), "clicked",
219
                         G_CALLBACK (prefs_template_register_cb), NULL);
220

    
221
        subst_btn = gtk_button_new_with_label(_(" Substitute "));
222
        gtk_widget_show(subst_btn);
223
        gtk_box_pack_start(GTK_BOX(hbox3), subst_btn, FALSE, TRUE, 0);
224
        g_signal_connect(G_OBJECT(subst_btn), "clicked",
225
                         G_CALLBACK(prefs_template_substitute_cb), NULL);
226

    
227
        del_btn = gtk_button_new_with_label(_("Delete"));
228
        gtk_widget_show(del_btn);
229
        gtk_box_pack_start(GTK_BOX(hbox3), del_btn, FALSE, TRUE, 0);
230
        g_signal_connect(G_OBJECT(del_btn), "clicked",
231
                         G_CALLBACK(prefs_template_delete_cb), NULL);
232

    
233
        desc_btn = gtk_button_new_with_label(_(" Symbols "));
234
        gtk_widget_show(desc_btn);
235
        gtk_box_pack_end(GTK_BOX(hbox2), desc_btn, FALSE, FALSE, 0);
236
        g_signal_connect(G_OBJECT(desc_btn), "clicked",
237
                         G_CALLBACK(prefs_quote_description), NULL);
238

    
239
        /* templates list */
240
        scroll1 = gtk_scrolled_window_new(NULL, NULL);
241
        gtk_widget_show(scroll1);
242
        gtk_box_pack_start(GTK_BOX(vbox2), scroll1, TRUE, TRUE, 0);
243
        gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll1),
244
                                       GTK_POLICY_AUTOMATIC,
245
                                       GTK_POLICY_AUTOMATIC);
246

    
247
        title[0] = _("Registered templates");
248
        clist_tmpls = gtk_clist_new_with_titles(1, title);
249
        gtk_widget_show(clist_tmpls);
250
        gtk_widget_set_size_request(scroll1, -1, 140);
251
        gtk_container_add(GTK_CONTAINER(scroll1), clist_tmpls);
252
        gtk_clist_set_column_width(GTK_CLIST(clist_tmpls), 0, 80);
253
        gtk_clist_set_selection_mode(GTK_CLIST(clist_tmpls),
254
                                     GTK_SELECTION_BROWSE);
255
        GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(clist_tmpls)->column[0].button,
256
                               GTK_CAN_FOCUS);
257
        g_signal_connect(G_OBJECT (clist_tmpls), "select_row",
258
                         G_CALLBACK (prefs_template_select_cb), NULL);
259

    
260
        /* ok | cancel */
261
        gtkut_button_set_create(&confirm_area, &ok_btn, _("OK"),
262
                                &cancel_btn, _("Cancel"), NULL, NULL);
263
        gtk_widget_show(confirm_area);
264
        gtk_box_pack_end(GTK_BOX(vbox2), confirm_area, FALSE, FALSE, 0);
265
        gtk_widget_grab_default(ok_btn);
266

    
267
        gtk_window_set_title(GTK_WINDOW(window), _("Templates"));
268

    
269
        g_signal_connect(G_OBJECT(window), "delete_event",
270
                         G_CALLBACK(prefs_template_deleted_cb), NULL);
271
        g_signal_connect(G_OBJECT(window), "key_press_event",
272
                         G_CALLBACK(prefs_template_key_pressed_cb), NULL);
273
        MANAGE_WINDOW_SIGNALS_CONNECT(window);
274
        g_signal_connect(G_OBJECT(ok_btn), "clicked",
275
                         G_CALLBACK(prefs_template_ok_cb), NULL);
276
        g_signal_connect(G_OBJECT(cancel_btn), "clicked",
277
                         G_CALLBACK(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_to = entry_to;
286
        templates.entry_cc = entry_cc;
287
        templates.entry_subject = entry_subject;
288
        templates.text_value = text_value;
289
}
290

    
291
static void prefs_template_window_setup(void)
292
{
293
        GtkCList *clist = GTK_CLIST(templates.clist_tmpls);
294
        GSList *tmpl_list;
295
        GSList *cur;
296
        gchar *title[1];
297
        gint row;
298
        Template *tmpl;
299

    
300
        manage_window_set_transient(GTK_WINDOW(templates.window));
301
        gtk_widget_grab_focus(templates.ok_btn);
302

    
303
        gtk_clist_freeze(clist);
304
        gtk_clist_clear(clist);
305

    
306
        title[0] = _("(New)");
307
        row = gtk_clist_append(clist, title);
308
        gtk_clist_set_row_data(clist, row, NULL);
309

    
310
        tmpl_list = template_read_config();
311

    
312
        for (cur = tmpl_list; cur != NULL; cur = cur->next) {
313
                tmpl = (Template *)cur->data;
314
                title[0] = tmpl->name;
315
                row = gtk_clist_append(clist, title);
316
                gtk_clist_set_row_data(clist, row, tmpl);
317
        }
318

    
319
        g_slist_free(tmpl_list);
320

    
321
        gtk_clist_thaw(clist);
322
}
323

    
324
static void prefs_template_clear(void)
325
{
326
        Template *tmpl;
327
        gint row = 1;
328

    
329
        while ((tmpl = gtk_clist_get_row_data
330
                (GTK_CLIST(templates.clist_tmpls), row)) != NULL) {
331
                template_free(tmpl);
332
                row++;
333
        }
334

    
335
        gtk_clist_clear(GTK_CLIST(templates.clist_tmpls));
336
}
337

    
338
static gint prefs_template_deleted_cb(GtkWidget *widget, GdkEventAny *event,
339
                                      gpointer data)
340
{
341
        prefs_template_cancel_cb();
342
        return TRUE;
343
}
344

    
345
static gboolean prefs_template_key_pressed_cb(GtkWidget *widget,
346
                                              GdkEventKey *event, gpointer data)
347
{
348
        if (event && event->keyval == GDK_Escape)
349
                prefs_template_cancel_cb();
350
        return FALSE;
351
}
352

    
353
static void prefs_template_ok_cb(void)
354
{
355
        GSList *tmpl_list;
356

    
357
        tmpl_list = prefs_template_get_list();
358
        template_set_config(tmpl_list);
359
        compose_reflect_prefs_all();
360
        gtk_clist_clear(GTK_CLIST(templates.clist_tmpls));
361
        gtk_widget_hide(templates.window);
362
        inc_unlock();
363
}
364

    
365
static void prefs_template_cancel_cb(void)
366
{
367
        prefs_template_clear();
368
        gtk_widget_hide(templates.window);
369
        inc_unlock();
370
}
371

    
372
static void prefs_template_select_cb(GtkCList *clist, gint row, gint column,
373
                                     GdkEvent *event)
374
{
375
        Template *tmpl;
376
        Template tmpl_def;
377
        GtkTextBuffer *buffer;
378
        GtkTextIter iter;
379

    
380
        tmpl_def.name = _("Template");
381
        tmpl_def.subject = "";
382
        tmpl_def.to = "";
383
        tmpl_def.cc = "";
384
        tmpl_def.value = "";
385

    
386
        if (!(tmpl = gtk_clist_get_row_data(clist, row)))
387
                tmpl = &tmpl_def;
388

    
389
        gtk_entry_set_text(GTK_ENTRY(templates.entry_name), tmpl->name);
390
        gtk_entry_set_text(GTK_ENTRY(templates.entry_to),
391
                           tmpl->to ? tmpl->to : "");
392
        gtk_entry_set_text(GTK_ENTRY(templates.entry_cc),
393
                           tmpl->cc ? tmpl->cc : "");
394
        gtk_entry_set_text(GTK_ENTRY(templates.entry_subject),
395
                           tmpl->subject ? tmpl->subject : "");
396

    
397
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
398
        gtk_text_buffer_set_text(buffer, "", 0);
399
        gtk_text_buffer_get_start_iter(buffer, &iter);
400
        gtk_text_buffer_insert(buffer, &iter, tmpl->value, -1);
401
}
402

    
403
static GSList *prefs_template_get_list(void)
404
{
405
        gint row = 1;
406
        GSList *tmpl_list = NULL;
407
        Template *tmpl;
408

    
409
        while ((tmpl = gtk_clist_get_row_data
410
                (GTK_CLIST(templates.clist_tmpls), row)) != NULL) {
411
                tmpl_list = g_slist_append(tmpl_list, tmpl);
412
                row++;
413
        }
414

    
415
        return tmpl_list;
416
}
417

    
418
static gint prefs_template_clist_set_row(gint row)
419
{
420
        GtkCList *clist = GTK_CLIST(templates.clist_tmpls);
421
        Template *tmpl;
422
        Template *tmp_tmpl;
423
        GtkTextBuffer *buffer;
424
        GtkTextIter start, end;
425
        gchar *name;
426
        gchar *to;
427
        gchar *cc;
428
        gchar *subject;
429
        gchar *value;
430
        gchar *title[1];
431

    
432
        g_return_val_if_fail(row != 0, -1);
433

    
434
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
435
        gtk_text_buffer_get_start_iter(buffer, &start);
436
        gtk_text_buffer_get_iter_at_offset(buffer, &end, -1); // end_iter?
437
        value = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
438

    
439
        if (value && *value != '\0') {
440
                gchar *parsed_buf;
441
                MsgInfo dummyinfo;
442

    
443
                memset(&dummyinfo, 0, sizeof(MsgInfo));
444
                quote_fmt_init(&dummyinfo, NULL, NULL);
445
                quote_fmt_scan_string(value);
446
                quote_fmt_parse();
447
                parsed_buf = quote_fmt_get_buffer();
448
                if (!parsed_buf) {
449
                        alertpanel_error(_("Template format error."));
450
                        g_free(value);
451
                        return -1;
452
                }
453
        }
454

    
455
        name = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_name),
456
                                      0, -1);
457
        subject = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_subject),
458
                                         0, -1);
459
        to = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_to), 0, -1);
460
        cc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_cc), 0, -1);
461

    
462
        if (subject && *subject == '\0') {
463
                g_free(subject);
464
                subject = NULL;
465
        }
466
        if (to && *to == '\0') {
467
                g_free(to);
468
                to = NULL;
469
        }
470

    
471
        tmpl = g_new(Template, 1);
472
        tmpl->name = name;
473
        tmpl->to = to;
474
        tmpl->cc = cc;
475
        tmpl->subject = subject;
476
        tmpl->value = value;
477

    
478
        title[0] = name;
479

    
480
        if (row < 0) {
481
                row = gtk_clist_append(clist, title);
482
        } else {
483
                gtk_clist_set_text(clist, row, 0, name);
484
                tmp_tmpl = gtk_clist_get_row_data(clist, row);
485
                if (tmp_tmpl)
486
                        template_free(tmp_tmpl);
487
        }
488

    
489
        gtk_clist_set_row_data(clist, row, tmpl);
490
        return row;
491
}
492

    
493
static void prefs_template_register_cb(void)
494
{
495
        prefs_template_clist_set_row(-1);
496
}
497

    
498
static void prefs_template_substitute_cb(void)
499
{
500
        GtkCList *clist = GTK_CLIST(templates.clist_tmpls);
501
        Template *tmpl;
502
        gint row;
503

    
504
        if (!clist->selection) return;
505

    
506
        row = GPOINTER_TO_INT(clist->selection->data);
507
        if (row == 0) return;
508

    
509
        tmpl = gtk_clist_get_row_data(clist, row);
510
        if (!tmpl) return;
511

    
512
        prefs_template_clist_set_row(row);
513
}
514

    
515
static void prefs_template_delete_cb(void)
516
{
517
        GtkCList *clist = GTK_CLIST(templates.clist_tmpls);
518
        Template *tmpl;
519
        gint row;
520

    
521
        if (!clist->selection) return;
522
        row = GPOINTER_TO_INT(clist->selection->data);
523
        if (row == 0) return;
524

    
525
        if (alertpanel(_("Delete template"),
526
                       _("Do you really want to delete this template?"),
527
                       _("Yes"), _("No"), NULL) == G_ALERTALTERNATE)
528
                return;
529

    
530
        tmpl = gtk_clist_get_row_data(clist, row);
531
        template_free(tmpl);
532
        gtk_clist_remove(clist, row);
533
}