Statistics
| Revision:

root / src / prefs_template.c @ 127

History | View | Annotate | Download (14.8 kB)

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