Statistics
| Revision:

root / src / prefs_template.c @ 3069

History | View | Annotate | Download (19.2 kB)

1 1 hiro
/*
2 1 hiro
 * Sylpheed templates subsystem
3 1 hiro
 * Copyright (C) 2001 Alexander Barinov
4 3014 hiro
 * Copyright (C) 2001-2012 Hiroyuki Yamamoto
5 1 hiro
 *
6 1 hiro
 * This program is free software; you can redistribute it and/or modify
7 1 hiro
 * it under the terms of the GNU General Public License as published by
8 1 hiro
 * the Free Software Foundation; either version 2 of the License, or
9 1 hiro
 * (at your option) any later version.
10 1 hiro
 *
11 1 hiro
 * This program is distributed in the hope that it will be useful,
12 1 hiro
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 1 hiro
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 1 hiro
 * GNU General Public License for more details.
15 1 hiro
 *
16 1 hiro
 * You should have received a copy of the GNU General Public License
17 1 hiro
 * along with this program; if not, write to the Free Software
18 1 hiro
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 1 hiro
 */
20 1 hiro
21 1 hiro
#include "defs.h"
22 1 hiro
23 1 hiro
#include <glib.h>
24 92 hiro
#include <glib/gi18n.h>
25 1 hiro
#include <gtk/gtk.h>
26 1 hiro
#include <gdk/gdkkeysyms.h>
27 1 hiro
#include <string.h>
28 1 hiro
#include <dirent.h>
29 1 hiro
#include <sys/stat.h>
30 1 hiro
31 1 hiro
#include "template.h"
32 1 hiro
#include "main.h"
33 1 hiro
#include "inc.h"
34 1 hiro
#include "utils.h"
35 1 hiro
#include "gtkutils.h"
36 1 hiro
#include "alertpanel.h"
37 1 hiro
#include "manage_window.h"
38 1 hiro
#include "prefs_common.h"
39 533 hiro
#include "prefs_common_dialog.h"
40 1 hiro
#include "compose.h"
41 703 hiro
#include "mainwindow.h"
42 1 hiro
#include "addr_compl.h"
43 1 hiro
#include "quote_fmt.h"
44 3014 hiro
#include "plugin.h"
45 1 hiro
46 1 hiro
static struct Templates {
47 1 hiro
        GtkWidget *window;
48 1 hiro
        GtkWidget *clist_tmpls;
49 1 hiro
        GtkWidget *entry_name;
50 1 hiro
        GtkWidget *entry_to;
51 1 hiro
        GtkWidget *entry_cc;
52 1423 hiro
        GtkWidget *entry_bcc;
53 1423 hiro
        GtkWidget *entry_replyto;
54 1 hiro
        GtkWidget *entry_subject;
55 1 hiro
        GtkWidget *text_value;
56 405 hiro
        GtkWidget *confirm_area;
57 405 hiro
        GtkWidget *ok_btn;
58 405 hiro
        GtkWidget *cancel_btn;
59 2629 hiro
60 2629 hiro
        gboolean entry_modified;
61 2629 hiro
        gboolean list_modified;
62 1 hiro
} templates;
63 1 hiro
64 1 hiro
/* widget creating functions */
65 1 hiro
static void prefs_template_window_create        (void);
66 1 hiro
static void prefs_template_window_setup                (void);
67 1 hiro
static void prefs_template_clear                (void);
68 1 hiro
69 1 hiro
static GSList *prefs_template_get_list                (void);
70 1 hiro
71 1 hiro
/* callbacks */
72 1 hiro
static gint prefs_template_deleted_cb                (GtkWidget        *widget,
73 1 hiro
                                                 GdkEventAny        *event,
74 1 hiro
                                                 gpointer         data);
75 1 hiro
static gboolean prefs_template_key_pressed_cb        (GtkWidget        *widget,
76 1 hiro
                                                 GdkEventKey        *event,
77 1 hiro
                                                 gpointer         data);
78 2629 hiro
static void prefs_template_changed_cb                (GtkEditable        *editable,
79 2629 hiro
                                                 gpointer         data);
80 1 hiro
static void prefs_template_cancel_cb                (void);
81 1 hiro
static void prefs_template_ok_cb                (void);
82 1 hiro
static void prefs_template_select_cb                (GtkCList        *clist,
83 1 hiro
                                                 gint                 row,
84 1 hiro
                                                 gint                 column,
85 1 hiro
                                                 GdkEvent        *event);
86 1 hiro
static void prefs_template_register_cb                (void);
87 1 hiro
static void prefs_template_substitute_cb        (void);
88 1 hiro
static void prefs_template_delete_cb                (void);
89 2630 hiro
static void prefs_template_up_cb                (void);
90 2630 hiro
static void prefs_template_down_cb                (void);
91 1 hiro
92 1 hiro
/* Called from mainwindow.c */
93 1 hiro
void prefs_template_open(void)
94 1 hiro
{
95 1 hiro
        inc_lock();
96 1 hiro
97 1 hiro
        if (!templates.window)
98 1 hiro
                prefs_template_window_create();
99 1 hiro
100 1 hiro
        prefs_template_window_setup();
101 1 hiro
        gtk_widget_show(templates.window);
102 3014 hiro
103 3014 hiro
        syl_plugin_signal_emit("prefs-template-open", templates.window);
104 1 hiro
}
105 1 hiro
106 1 hiro
#define ADD_ENTRY(entry, str, row) \
107 1 hiro
{ \
108 1 hiro
        label1 = gtk_label_new(str); \
109 1 hiro
        gtk_widget_show(label1); \
110 1 hiro
        gtk_table_attach(GTK_TABLE(table), label1, 0, 1, row, (row + 1), \
111 1 hiro
                         GTK_FILL, 0, 0, 0); \
112 1 hiro
        gtk_misc_set_alignment(GTK_MISC(label1), 1, 0.5); \
113 1 hiro
 \
114 1 hiro
        entry = gtk_entry_new(); \
115 1 hiro
        gtk_widget_show(entry); \
116 1 hiro
        gtk_table_attach(GTK_TABLE(table), entry, 1, 2, row, (row + 1), \
117 1 hiro
                         GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0); \
118 2629 hiro
        g_signal_connect(G_OBJECT(entry), "changed", \
119 2629 hiro
                         G_CALLBACK(prefs_template_changed_cb), NULL); \
120 1 hiro
}
121 1 hiro
122 1 hiro
static void prefs_template_window_create(void)
123 1 hiro
{
124 1 hiro
        /* window structure ;) */
125 1 hiro
        GtkWidget *window;
126 1 hiro
        GtkWidget   *vpaned;
127 1 hiro
        GtkWidget     *vbox1;
128 1 hiro
        GtkWidget       *hbox1;
129 1 hiro
        GtkWidget         *label1;
130 1 hiro
        GtkWidget         *entry_name;
131 1 hiro
        GtkWidget       *table;
132 1 hiro
        GtkWidget         *entry_to;
133 1 hiro
        GtkWidget         *entry_cc;
134 1423 hiro
        GtkWidget         *entry_bcc;
135 1423 hiro
        GtkWidget         *entry_replyto;
136 1 hiro
        GtkWidget         *entry_subject;
137 1 hiro
        GtkWidget       *scroll2;
138 1 hiro
        GtkWidget         *text_value;
139 1 hiro
        GtkWidget     *vbox2;
140 1 hiro
        GtkWidget       *hbox2;
141 1 hiro
        GtkWidget         *arrow1;
142 1 hiro
        GtkWidget         *hbox3;
143 1 hiro
        GtkWidget           *reg_btn;
144 1 hiro
        GtkWidget           *subst_btn;
145 1 hiro
        GtkWidget           *del_btn;
146 1 hiro
        GtkWidget         *desc_btn;
147 2630 hiro
        GtkWidget       *hbox4;
148 2630 hiro
        GtkWidget         *scroll1;
149 2630 hiro
        GtkWidget           *clist_tmpls;
150 2630 hiro
        GtkWidget         *vbox3;
151 2630 hiro
        GtkWidget           *vbox4;
152 2630 hiro
        GtkWidget             *up_btn;
153 2630 hiro
        GtkWidget             *down_btn;
154 1 hiro
        GtkWidget       *confirm_area;
155 1 hiro
        GtkWidget         *ok_btn;
156 1 hiro
        GtkWidget         *cancel_btn;
157 1 hiro
158 2629 hiro
        GtkTextBuffer *buffer;
159 2629 hiro
160 1 hiro
        gchar *title[1];
161 1 hiro
162 1 hiro
        /* main window */
163 1 hiro
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
164 1 hiro
        gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
165 1 hiro
        gtk_window_set_modal(GTK_WINDOW(window), TRUE);
166 1 hiro
        gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, FALSE);
167 1 hiro
168 1 hiro
        /* vpaned to separate template settings from templates list */
169 1 hiro
        vpaned = gtk_vpaned_new();
170 1 hiro
        gtk_widget_show(vpaned);
171 1 hiro
        gtk_container_add(GTK_CONTAINER(window), vpaned);
172 1 hiro
173 1 hiro
        /* vbox to handle template name and content */
174 1 hiro
        vbox1 = gtk_vbox_new(FALSE, 6);
175 1 hiro
        gtk_widget_show(vbox1);
176 1 hiro
        gtk_container_set_border_width(GTK_CONTAINER(vbox1), 8);
177 1 hiro
        gtk_paned_pack1(GTK_PANED(vpaned), vbox1, FALSE, FALSE);
178 1 hiro
179 1 hiro
        hbox1 = gtk_hbox_new(FALSE, 8);
180 1 hiro
        gtk_widget_show(hbox1);
181 1 hiro
        gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, FALSE, 0);
182 1 hiro
183 1 hiro
        label1 = gtk_label_new(_("Template name"));
184 1 hiro
        gtk_widget_show(label1);
185 1 hiro
        gtk_box_pack_start(GTK_BOX(hbox1), label1, FALSE, FALSE, 0);
186 1 hiro
187 1 hiro
        entry_name = gtk_entry_new();
188 1 hiro
        gtk_widget_show(entry_name);
189 1 hiro
        gtk_box_pack_start(GTK_BOX(hbox1), entry_name, TRUE, TRUE, 0);
190 2629 hiro
        g_signal_connect(G_OBJECT(entry_name), "changed",
191 2629 hiro
                         G_CALLBACK(prefs_template_changed_cb), NULL);
192 1 hiro
193 1 hiro
        /* table for headers */
194 1423 hiro
        table = gtk_table_new(5, 2, FALSE);
195 1 hiro
        gtk_widget_show(table);
196 1 hiro
        gtk_box_pack_start(GTK_BOX(vbox1), table, FALSE, FALSE, 0);
197 1 hiro
        gtk_table_set_row_spacings(GTK_TABLE(table), 4);
198 1 hiro
        gtk_table_set_col_spacings(GTK_TABLE(table), 4);
199 1 hiro
200 1 hiro
        ADD_ENTRY(entry_to, _("To:"), 0);
201 1 hiro
        address_completion_register_entry(GTK_ENTRY(entry_to));
202 1 hiro
        ADD_ENTRY(entry_cc, _("Cc:"), 1);
203 1 hiro
        address_completion_register_entry(GTK_ENTRY(entry_cc));
204 1423 hiro
        ADD_ENTRY(entry_bcc, _("Bcc:"), 2);
205 1423 hiro
        address_completion_register_entry(GTK_ENTRY(entry_bcc));
206 1423 hiro
        ADD_ENTRY(entry_replyto, _("Reply-To:"), 3);
207 1423 hiro
        address_completion_register_entry(GTK_ENTRY(entry_replyto));
208 1423 hiro
        ADD_ENTRY(entry_subject, _("Subject:"), 4);
209 1 hiro
210 1 hiro
#undef ADD_ENTRY
211 1 hiro
212 1 hiro
        /* template content */
213 1 hiro
        scroll2 = gtk_scrolled_window_new(NULL, NULL);
214 1 hiro
        gtk_widget_show(scroll2);
215 1 hiro
        gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll2),
216 1423 hiro
                                       GTK_POLICY_NEVER,
217 405 hiro
                                       GTK_POLICY_AUTOMATIC);
218 405 hiro
        gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll2),
219 405 hiro
                                            GTK_SHADOW_IN);
220 1 hiro
        gtk_box_pack_start(GTK_BOX(vbox1), scroll2, TRUE, TRUE, 0);
221 1 hiro
222 1 hiro
        text_value = gtk_text_view_new();
223 1 hiro
        gtk_widget_show(text_value);
224 1423 hiro
        gtk_widget_set_size_request(text_value, 360, 120);
225 1 hiro
        gtk_container_add(GTK_CONTAINER(scroll2), text_value);
226 1 hiro
        gtk_text_view_set_editable(GTK_TEXT_VIEW(text_value), TRUE);
227 2629 hiro
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_value));
228 2629 hiro
        g_signal_connect(G_OBJECT(buffer), "changed",
229 2629 hiro
                         G_CALLBACK(prefs_template_changed_cb), NULL);
230 1 hiro
231 1 hiro
        /* vbox for buttons and templates list */
232 1 hiro
        vbox2 = gtk_vbox_new(FALSE, 6);
233 1 hiro
        gtk_widget_show(vbox2);
234 1 hiro
        gtk_container_set_border_width(GTK_CONTAINER(vbox2), 8);
235 1 hiro
        gtk_paned_pack2(GTK_PANED(vpaned), vbox2, TRUE, FALSE);
236 1 hiro
237 1 hiro
        /* register | substitute | delete */
238 1 hiro
        hbox2 = gtk_hbox_new(FALSE, 4);
239 1 hiro
        gtk_widget_show(hbox2);
240 1 hiro
        gtk_box_pack_start(GTK_BOX(vbox2), hbox2, FALSE, FALSE, 0);
241 1 hiro
242 1 hiro
        arrow1 = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
243 1 hiro
        gtk_widget_show(arrow1);
244 1 hiro
        gtk_box_pack_start(GTK_BOX(hbox2), arrow1, FALSE, FALSE, 0);
245 1 hiro
        gtk_widget_set_size_request(arrow1, -1, 16);
246 1 hiro
247 1 hiro
        hbox3 = gtk_hbox_new(TRUE, 4);
248 1 hiro
        gtk_widget_show(hbox3);
249 1 hiro
        gtk_box_pack_start(GTK_BOX(hbox2), hbox3, FALSE, FALSE, 0);
250 1 hiro
251 1 hiro
        reg_btn = gtk_button_new_with_label(_("Register"));
252 1 hiro
        gtk_widget_show(reg_btn);
253 1 hiro
        gtk_box_pack_start(GTK_BOX(hbox3), reg_btn, FALSE, TRUE, 0);
254 1 hiro
        g_signal_connect(G_OBJECT (reg_btn), "clicked",
255 1 hiro
                         G_CALLBACK (prefs_template_register_cb), NULL);
256 1 hiro
257 1 hiro
        subst_btn = gtk_button_new_with_label(_(" Substitute "));
258 1 hiro
        gtk_widget_show(subst_btn);
259 1 hiro
        gtk_box_pack_start(GTK_BOX(hbox3), subst_btn, FALSE, TRUE, 0);
260 1 hiro
        g_signal_connect(G_OBJECT(subst_btn), "clicked",
261 1 hiro
                         G_CALLBACK(prefs_template_substitute_cb), NULL);
262 1 hiro
263 1 hiro
        del_btn = gtk_button_new_with_label(_("Delete"));
264 1 hiro
        gtk_widget_show(del_btn);
265 1 hiro
        gtk_box_pack_start(GTK_BOX(hbox3), del_btn, FALSE, TRUE, 0);
266 1 hiro
        g_signal_connect(G_OBJECT(del_btn), "clicked",
267 1 hiro
                         G_CALLBACK(prefs_template_delete_cb), NULL);
268 1 hiro
269 1 hiro
        desc_btn = gtk_button_new_with_label(_(" Symbols "));
270 1 hiro
        gtk_widget_show(desc_btn);
271 1 hiro
        gtk_box_pack_end(GTK_BOX(hbox2), desc_btn, FALSE, FALSE, 0);
272 1 hiro
        g_signal_connect(G_OBJECT(desc_btn), "clicked",
273 1 hiro
                         G_CALLBACK(prefs_quote_description), NULL);
274 1 hiro
275 1 hiro
        /* templates list */
276 2630 hiro
        hbox4 = gtk_hbox_new(FALSE, 8);
277 2630 hiro
        gtk_widget_show(hbox4);
278 2630 hiro
        gtk_box_pack_start(GTK_BOX(vbox2), hbox4, TRUE, TRUE, 0);
279 2630 hiro
280 1 hiro
        scroll1 = gtk_scrolled_window_new(NULL, NULL);
281 1 hiro
        gtk_widget_show(scroll1);
282 2630 hiro
        gtk_box_pack_start(GTK_BOX(hbox4), scroll1, TRUE, TRUE, 0);
283 1 hiro
        gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll1),
284 1 hiro
                                       GTK_POLICY_AUTOMATIC,
285 1 hiro
                                       GTK_POLICY_AUTOMATIC);
286 1 hiro
287 1 hiro
        title[0] = _("Registered templates");
288 1 hiro
        clist_tmpls = gtk_clist_new_with_titles(1, title);
289 1 hiro
        gtk_widget_show(clist_tmpls);
290 1 hiro
        gtk_widget_set_size_request(scroll1, -1, 140);
291 1 hiro
        gtk_container_add(GTK_CONTAINER(scroll1), clist_tmpls);
292 1 hiro
        gtk_clist_set_column_width(GTK_CLIST(clist_tmpls), 0, 80);
293 1 hiro
        gtk_clist_set_selection_mode(GTK_CLIST(clist_tmpls),
294 1 hiro
                                     GTK_SELECTION_BROWSE);
295 1577 hiro
        gtkut_clist_set_redraw(GTK_CLIST(clist_tmpls));
296 1 hiro
        GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(clist_tmpls)->column[0].button,
297 1 hiro
                               GTK_CAN_FOCUS);
298 1 hiro
        g_signal_connect(G_OBJECT (clist_tmpls), "select_row",
299 1 hiro
                         G_CALLBACK (prefs_template_select_cb), NULL);
300 1 hiro
301 2630 hiro
        vbox3 = gtk_vbox_new(TRUE, 0);
302 2630 hiro
        gtk_widget_show(vbox3);
303 2630 hiro
        gtk_box_pack_start(GTK_BOX(hbox4), vbox3, FALSE, FALSE, 0);
304 2630 hiro
305 2630 hiro
        vbox4 = gtk_vbox_new(TRUE, 8);
306 2630 hiro
        gtk_widget_show(vbox4);
307 2630 hiro
        gtk_box_pack_start(GTK_BOX(vbox3), vbox4, TRUE, FALSE, 0);
308 2630 hiro
309 2630 hiro
        up_btn = gtk_button_new_with_label(_("Up"));
310 2630 hiro
        gtk_widget_show(up_btn);
311 2630 hiro
        gtk_box_pack_start(GTK_BOX(vbox4), up_btn, FALSE, FALSE, 0);
312 2630 hiro
        g_signal_connect(G_OBJECT (up_btn), "clicked",
313 2630 hiro
                         G_CALLBACK (prefs_template_up_cb), NULL);
314 2630 hiro
315 2630 hiro
        down_btn = gtk_button_new_with_label(_("Down"));
316 2630 hiro
        gtk_widget_show(down_btn);
317 2630 hiro
        gtk_box_pack_start(GTK_BOX(vbox4), down_btn, FALSE, FALSE, 0);
318 2630 hiro
        g_signal_connect(G_OBJECT (down_btn), "clicked",
319 2630 hiro
                         G_CALLBACK (prefs_template_down_cb), NULL);
320 2630 hiro
321 1 hiro
        /* ok | cancel */
322 30 hiro
        gtkut_stock_button_set_create(&confirm_area, &ok_btn, GTK_STOCK_OK,
323 405 hiro
                                      &cancel_btn, GTK_STOCK_CANCEL,
324 405 hiro
                                      NULL, NULL);
325 1 hiro
        gtk_widget_show(confirm_area);
326 1 hiro
        gtk_box_pack_end(GTK_BOX(vbox2), confirm_area, FALSE, FALSE, 0);
327 1 hiro
        gtk_widget_grab_default(ok_btn);
328 1 hiro
329 1 hiro
        gtk_window_set_title(GTK_WINDOW(window), _("Templates"));
330 1 hiro
331 1 hiro
        g_signal_connect(G_OBJECT(window), "delete_event",
332 1 hiro
                         G_CALLBACK(prefs_template_deleted_cb), NULL);
333 1 hiro
        g_signal_connect(G_OBJECT(window), "key_press_event",
334 1 hiro
                         G_CALLBACK(prefs_template_key_pressed_cb), NULL);
335 1 hiro
        MANAGE_WINDOW_SIGNALS_CONNECT(window);
336 1 hiro
        g_signal_connect(G_OBJECT(ok_btn), "clicked",
337 1 hiro
                         G_CALLBACK(prefs_template_ok_cb), NULL);
338 1 hiro
        g_signal_connect(G_OBJECT(cancel_btn), "clicked",
339 1 hiro
                         G_CALLBACK(prefs_template_cancel_cb), NULL);
340 1 hiro
341 1 hiro
        address_completion_start(window);
342 1 hiro
343 1 hiro
        templates.window = window;
344 1 hiro
        templates.clist_tmpls = clist_tmpls;
345 1 hiro
        templates.entry_name = entry_name;
346 1 hiro
        templates.entry_to = entry_to;
347 1 hiro
        templates.entry_cc = entry_cc;
348 1423 hiro
        templates.entry_bcc = entry_bcc;
349 1423 hiro
        templates.entry_replyto = entry_replyto;
350 1 hiro
        templates.entry_subject = entry_subject;
351 1 hiro
        templates.text_value = text_value;
352 405 hiro
        templates.confirm_area = confirm_area;
353 405 hiro
        templates.ok_btn = ok_btn;
354 405 hiro
        templates.cancel_btn = cancel_btn;
355 2629 hiro
        templates.entry_modified = FALSE;
356 2629 hiro
        templates.list_modified = FALSE;
357 1 hiro
}
358 1 hiro
359 1 hiro
static void prefs_template_window_setup(void)
360 1 hiro
{
361 1 hiro
        GtkCList *clist = GTK_CLIST(templates.clist_tmpls);
362 1 hiro
        GSList *tmpl_list;
363 1 hiro
        GSList *cur;
364 1 hiro
        gchar *title[1];
365 1 hiro
        gint row;
366 1 hiro
        Template *tmpl;
367 1 hiro
368 405 hiro
        gtkut_box_set_reverse_order(GTK_BOX(templates.confirm_area),
369 405 hiro
                                    !prefs_common.comply_gnome_hig);
370 1 hiro
        manage_window_set_transient(GTK_WINDOW(templates.window));
371 1 hiro
        gtk_widget_grab_focus(templates.ok_btn);
372 1 hiro
373 1 hiro
        gtk_clist_freeze(clist);
374 1 hiro
        gtk_clist_clear(clist);
375 1 hiro
376 1 hiro
        title[0] = _("(New)");
377 1 hiro
        row = gtk_clist_append(clist, title);
378 1 hiro
        gtk_clist_set_row_data(clist, row, NULL);
379 1 hiro
380 1 hiro
        tmpl_list = template_read_config();
381 1 hiro
382 1 hiro
        for (cur = tmpl_list; cur != NULL; cur = cur->next) {
383 1 hiro
                tmpl = (Template *)cur->data;
384 1 hiro
                title[0] = tmpl->name;
385 1 hiro
                row = gtk_clist_append(clist, title);
386 1 hiro
                gtk_clist_set_row_data(clist, row, tmpl);
387 1 hiro
        }
388 1 hiro
389 1 hiro
        g_slist_free(tmpl_list);
390 1 hiro
391 1 hiro
        gtk_clist_thaw(clist);
392 2629 hiro
393 2629 hiro
        templates.entry_modified = FALSE;
394 2629 hiro
        templates.list_modified = FALSE;
395 1 hiro
}
396 1 hiro
397 1 hiro
static void prefs_template_clear(void)
398 1 hiro
{
399 1 hiro
        Template *tmpl;
400 1 hiro
        gint row = 1;
401 1 hiro
402 1 hiro
        while ((tmpl = gtk_clist_get_row_data
403 1 hiro
                (GTK_CLIST(templates.clist_tmpls), row)) != NULL) {
404 1 hiro
                template_free(tmpl);
405 1 hiro
                row++;
406 1 hiro
        }
407 1 hiro
408 1 hiro
        gtk_clist_clear(GTK_CLIST(templates.clist_tmpls));
409 1 hiro
}
410 1 hiro
411 1 hiro
static gint prefs_template_deleted_cb(GtkWidget *widget, GdkEventAny *event,
412 1 hiro
                                      gpointer data)
413 1 hiro
{
414 1 hiro
        prefs_template_cancel_cb();
415 1 hiro
        return TRUE;
416 1 hiro
}
417 1 hiro
418 1 hiro
static gboolean prefs_template_key_pressed_cb(GtkWidget *widget,
419 1 hiro
                                              GdkEventKey *event, gpointer data)
420 1 hiro
{
421 1 hiro
        if (event && event->keyval == GDK_Escape)
422 1 hiro
                prefs_template_cancel_cb();
423 1 hiro
        return FALSE;
424 1 hiro
}
425 1 hiro
426 2629 hiro
static void prefs_template_changed_cb(GtkEditable *editable, gpointer data)
427 2629 hiro
{
428 2629 hiro
        templates.entry_modified = TRUE;
429 2629 hiro
}
430 2629 hiro
431 1 hiro
static void prefs_template_ok_cb(void)
432 1 hiro
{
433 1 hiro
        GSList *tmpl_list;
434 1 hiro
435 2631 hiro
        if (templates.entry_modified) {
436 2631 hiro
                if (alertpanel(_("Template is modified"),
437 2631 hiro
                               _("Current modification is not applied. Finish without saving it?"),
438 2631 hiro
                               GTK_STOCK_YES, GTK_STOCK_NO, NULL)
439 2631 hiro
                    != G_ALERTDEFAULT)
440 2631 hiro
                        return;
441 2631 hiro
        }
442 2631 hiro
443 1 hiro
        tmpl_list = prefs_template_get_list();
444 1 hiro
        template_set_config(tmpl_list);
445 1 hiro
        compose_reflect_prefs_all();
446 1 hiro
        gtk_clist_clear(GTK_CLIST(templates.clist_tmpls));
447 1 hiro
        gtk_widget_hide(templates.window);
448 703 hiro
        main_window_popup(main_window_get());
449 1 hiro
        inc_unlock();
450 1 hiro
}
451 1 hiro
452 1 hiro
static void prefs_template_cancel_cb(void)
453 1 hiro
{
454 2629 hiro
        if (templates.entry_modified || templates.list_modified) {
455 2629 hiro
                if (alertpanel(_("Templates are modified"),
456 2629 hiro
                               _("Really discard modification to templates?"),
457 2629 hiro
                               GTK_STOCK_YES, GTK_STOCK_NO, NULL)
458 2629 hiro
                    != G_ALERTDEFAULT)
459 2629 hiro
                        return;
460 2629 hiro
        }
461 2629 hiro
462 1 hiro
        prefs_template_clear();
463 1 hiro
        gtk_widget_hide(templates.window);
464 703 hiro
        main_window_popup(main_window_get());
465 1 hiro
        inc_unlock();
466 1 hiro
}
467 1 hiro
468 1 hiro
static void prefs_template_select_cb(GtkCList *clist, gint row, gint column,
469 1 hiro
                                     GdkEvent *event)
470 1 hiro
{
471 1 hiro
        Template *tmpl;
472 1 hiro
        Template tmpl_def;
473 1 hiro
        GtkTextBuffer *buffer;
474 1 hiro
        GtkTextIter iter;
475 1 hiro
476 1 hiro
        tmpl_def.name = _("Template");
477 1 hiro
        tmpl_def.subject = "";
478 1 hiro
        tmpl_def.to = "";
479 1 hiro
        tmpl_def.cc = "";
480 1423 hiro
        tmpl_def.bcc = "";
481 1423 hiro
        tmpl_def.replyto = "";
482 1 hiro
        tmpl_def.value = "";
483 1 hiro
484 1 hiro
        if (!(tmpl = gtk_clist_get_row_data(clist, row)))
485 1 hiro
                tmpl = &tmpl_def;
486 1 hiro
487 1 hiro
        gtk_entry_set_text(GTK_ENTRY(templates.entry_name), tmpl->name);
488 1 hiro
        gtk_entry_set_text(GTK_ENTRY(templates.entry_to),
489 1 hiro
                           tmpl->to ? tmpl->to : "");
490 1 hiro
        gtk_entry_set_text(GTK_ENTRY(templates.entry_cc),
491 1 hiro
                           tmpl->cc ? tmpl->cc : "");
492 1423 hiro
        gtk_entry_set_text(GTK_ENTRY(templates.entry_bcc),
493 1423 hiro
                           tmpl->bcc ? tmpl->bcc : "");
494 1423 hiro
        gtk_entry_set_text(GTK_ENTRY(templates.entry_replyto),
495 1423 hiro
                           tmpl->replyto ? tmpl->replyto : "");
496 1 hiro
        gtk_entry_set_text(GTK_ENTRY(templates.entry_subject),
497 1 hiro
                           tmpl->subject ? tmpl->subject : "");
498 1 hiro
499 1 hiro
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
500 1 hiro
        gtk_text_buffer_set_text(buffer, "", 0);
501 1 hiro
        gtk_text_buffer_get_start_iter(buffer, &iter);
502 1 hiro
        gtk_text_buffer_insert(buffer, &iter, tmpl->value, -1);
503 2629 hiro
504 2629 hiro
        templates.entry_modified = FALSE;
505 1 hiro
}
506 1 hiro
507 1 hiro
static GSList *prefs_template_get_list(void)
508 1 hiro
{
509 1 hiro
        gint row = 1;
510 1 hiro
        GSList *tmpl_list = NULL;
511 1 hiro
        Template *tmpl;
512 1 hiro
513 1 hiro
        while ((tmpl = gtk_clist_get_row_data
514 1 hiro
                (GTK_CLIST(templates.clist_tmpls), row)) != NULL) {
515 1 hiro
                tmpl_list = g_slist_append(tmpl_list, tmpl);
516 1 hiro
                row++;
517 1 hiro
        }
518 1 hiro
519 1 hiro
        return tmpl_list;
520 1 hiro
}
521 1 hiro
522 1 hiro
static gint prefs_template_clist_set_row(gint row)
523 1 hiro
{
524 1 hiro
        GtkCList *clist = GTK_CLIST(templates.clist_tmpls);
525 1 hiro
        Template *tmpl;
526 1 hiro
        Template *tmp_tmpl;
527 1 hiro
        GtkTextBuffer *buffer;
528 1 hiro
        GtkTextIter start, end;
529 1 hiro
        gchar *name;
530 1 hiro
        gchar *to;
531 1 hiro
        gchar *cc;
532 1423 hiro
        gchar *bcc;
533 1423 hiro
        gchar *replyto;
534 1 hiro
        gchar *subject;
535 1 hiro
        gchar *value;
536 1 hiro
        gchar *title[1];
537 1 hiro
538 1 hiro
        g_return_val_if_fail(row != 0, -1);
539 1 hiro
540 1 hiro
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(templates.text_value));
541 1 hiro
        gtk_text_buffer_get_start_iter(buffer, &start);
542 1423 hiro
        gtk_text_buffer_get_end_iter(buffer, &end);
543 1 hiro
        value = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
544 1 hiro
545 1 hiro
        if (value && *value != '\0') {
546 1 hiro
                gchar *parsed_buf;
547 1 hiro
                MsgInfo dummyinfo;
548 1 hiro
549 1 hiro
                memset(&dummyinfo, 0, sizeof(MsgInfo));
550 1 hiro
                quote_fmt_init(&dummyinfo, NULL, NULL);
551 1 hiro
                quote_fmt_scan_string(value);
552 1 hiro
                quote_fmt_parse();
553 1 hiro
                parsed_buf = quote_fmt_get_buffer();
554 1 hiro
                if (!parsed_buf) {
555 1 hiro
                        alertpanel_error(_("Template format error."));
556 1 hiro
                        g_free(value);
557 1 hiro
                        return -1;
558 1 hiro
                }
559 1 hiro
        }
560 1 hiro
561 1 hiro
        name = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_name),
562 1 hiro
                                      0, -1);
563 1423 hiro
        to = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_to), 0, -1);
564 1423 hiro
        cc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_cc), 0, -1);
565 1423 hiro
        bcc = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_bcc), 0, -1);
566 1423 hiro
        replyto = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_replyto),
567 1423 hiro
                                         0, -1);
568 1 hiro
        subject = gtk_editable_get_chars(GTK_EDITABLE(templates.entry_subject),
569 1 hiro
                                         0, -1);
570 1 hiro
571 1423 hiro
#define NULLIFY_IF_EMPTY(val)                \
572 1423 hiro
        if (val && *val == '\0') {        \
573 1423 hiro
                g_free(val);                \
574 1423 hiro
                val = NULL;                \
575 1 hiro
        }
576 1 hiro
577 1423 hiro
        NULLIFY_IF_EMPTY(to);
578 1423 hiro
        NULLIFY_IF_EMPTY(cc);
579 1423 hiro
        NULLIFY_IF_EMPTY(bcc);
580 1423 hiro
        NULLIFY_IF_EMPTY(replyto);
581 1423 hiro
        NULLIFY_IF_EMPTY(subject);
582 1423 hiro
583 1423 hiro
#undef NULLIFY_IF_EMPTY
584 1423 hiro
585 1 hiro
        tmpl = g_new(Template, 1);
586 1 hiro
        tmpl->name = name;
587 1 hiro
        tmpl->to = to;
588 1 hiro
        tmpl->cc = cc;
589 1423 hiro
        tmpl->bcc = bcc;
590 1423 hiro
        tmpl->replyto = replyto;
591 1 hiro
        tmpl->subject = subject;
592 1 hiro
        tmpl->value = value;
593 1 hiro
594 1 hiro
        title[0] = name;
595 1 hiro
596 1 hiro
        if (row < 0) {
597 1 hiro
                row = gtk_clist_append(clist, title);
598 1 hiro
        } else {
599 1 hiro
                gtk_clist_set_text(clist, row, 0, name);
600 1 hiro
                tmp_tmpl = gtk_clist_get_row_data(clist, row);
601 1 hiro
                if (tmp_tmpl)
602 1 hiro
                        template_free(tmp_tmpl);
603 1 hiro
        }
604 1 hiro
605 1 hiro
        gtk_clist_set_row_data(clist, row, tmpl);
606 2631 hiro
        templates.entry_modified = FALSE;
607 2629 hiro
        templates.list_modified = TRUE;
608 2629 hiro
609 1 hiro
        return row;
610 1 hiro
}
611 1 hiro
612 1 hiro
static void prefs_template_register_cb(void)
613 1 hiro
{
614 1 hiro
        prefs_template_clist_set_row(-1);
615 1 hiro
}
616 1 hiro
617 1 hiro
static void prefs_template_substitute_cb(void)
618 1 hiro
{
619 1 hiro
        GtkCList *clist = GTK_CLIST(templates.clist_tmpls);
620 1 hiro
        Template *tmpl;
621 1 hiro
        gint row;
622 1 hiro
623 1 hiro
        if (!clist->selection) return;
624 1 hiro
625 1 hiro
        row = GPOINTER_TO_INT(clist->selection->data);
626 1 hiro
        if (row == 0) return;
627 1 hiro
628 1 hiro
        tmpl = gtk_clist_get_row_data(clist, row);
629 1 hiro
        if (!tmpl) return;
630 1 hiro
631 1 hiro
        prefs_template_clist_set_row(row);
632 1 hiro
}
633 1 hiro
634 1 hiro
static void prefs_template_delete_cb(void)
635 1 hiro
{
636 1 hiro
        GtkCList *clist = GTK_CLIST(templates.clist_tmpls);
637 1 hiro
        Template *tmpl;
638 1 hiro
        gint row;
639 1 hiro
640 1 hiro
        if (!clist->selection) return;
641 1 hiro
        row = GPOINTER_TO_INT(clist->selection->data);
642 1 hiro
        if (row == 0) return;
643 1 hiro
644 1 hiro
        if (alertpanel(_("Delete template"),
645 1 hiro
                       _("Do you really want to delete this template?"),
646 47 hiro
                       GTK_STOCK_YES, GTK_STOCK_NO, NULL) != G_ALERTDEFAULT)
647 1 hiro
                return;
648 1 hiro
649 1 hiro
        tmpl = gtk_clist_get_row_data(clist, row);
650 1 hiro
        template_free(tmpl);
651 1 hiro
        gtk_clist_remove(clist, row);
652 2629 hiro
        templates.list_modified = TRUE;
653 1 hiro
}
654 2630 hiro
655 2630 hiro
static void prefs_template_up_cb(void)
656 2630 hiro
{
657 2630 hiro
        GtkCList *clist = GTK_CLIST(templates.clist_tmpls);
658 2630 hiro
        gint row;
659 2630 hiro
660 2630 hiro
        if (!clist->selection) return;
661 2630 hiro
        row = GPOINTER_TO_INT(clist->selection->data);
662 2630 hiro
        if (row > 1) {
663 2630 hiro
                gtk_clist_row_move(clist, row, row - 1);
664 2630 hiro
                templates.list_modified = TRUE;
665 2630 hiro
        }
666 2630 hiro
}
667 2630 hiro
668 2630 hiro
static void prefs_template_down_cb(void)
669 2630 hiro
{
670 2630 hiro
        GtkCList *clist = GTK_CLIST(templates.clist_tmpls);
671 2630 hiro
        gint row;
672 2630 hiro
673 2630 hiro
        if (!clist->selection) return;
674 2630 hiro
        row = GPOINTER_TO_INT(clist->selection->data);
675 2630 hiro
        if (row > 0 && row < clist->rows - 1) {
676 2630 hiro
                gtk_clist_row_move(clist, row, row + 1);
677 2630 hiro
                templates.list_modified = TRUE;
678 2630 hiro
        }
679 2630 hiro
}