Statistics
| Revision:

root / src / prefs_customheader.c @ 6

History | View | Annotate | Download (16.6 kB)

1 1 hiro
/*
2 1 hiro
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 1 hiro
 * Copyright (C) 1999-2003 Hiroyuki Yamamoto
4 1 hiro
 *
5 1 hiro
 * This program is free software; you can redistribute it and/or modify
6 1 hiro
 * it under the terms of the GNU General Public License as published by
7 1 hiro
 * the Free Software Foundation; either version 2 of the License, or
8 1 hiro
 * (at your option) any later version.
9 1 hiro
 *
10 1 hiro
 * This program is distributed in the hope that it will be useful,
11 1 hiro
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 1 hiro
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 1 hiro
 * GNU General Public License for more details.
14 1 hiro
 *
15 1 hiro
 * You should have received a copy of the GNU General Public License
16 1 hiro
 * along with this program; if not, write to the Free Software
17 1 hiro
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 1 hiro
 */
19 1 hiro
20 1 hiro
#ifdef HAVE_CONFIG_H
21 1 hiro
#  include "config.h"
22 1 hiro
#endif
23 1 hiro
24 1 hiro
#include "defs.h"
25 1 hiro
26 1 hiro
#include <glib.h>
27 1 hiro
#include <gtk/gtk.h>
28 1 hiro
#include <gdk/gdkkeysyms.h>
29 1 hiro
#include <stdio.h>
30 1 hiro
#include <stdlib.h>
31 1 hiro
#include <string.h>
32 1 hiro
#include <errno.h>
33 1 hiro
34 1 hiro
#include "intl.h"
35 1 hiro
#include "main.h"
36 1 hiro
#include "prefs.h"
37 1 hiro
#include "prefs_customheader.h"
38 1 hiro
#include "prefs_common.h"
39 1 hiro
#include "prefs_account.h"
40 1 hiro
#include "mainwindow.h"
41 1 hiro
#include "foldersel.h"
42 1 hiro
#include "manage_window.h"
43 1 hiro
#include "customheader.h"
44 1 hiro
#include "folder.h"
45 1 hiro
#include "utils.h"
46 1 hiro
#include "gtkutils.h"
47 1 hiro
#include "alertpanel.h"
48 1 hiro
49 1 hiro
static struct CustomHdr {
50 1 hiro
        GtkWidget *window;
51 1 hiro
52 1 hiro
        GtkWidget *ok_btn;
53 1 hiro
        GtkWidget *cancel_btn;
54 1 hiro
55 1 hiro
        GtkWidget *hdr_combo;
56 1 hiro
        GtkWidget *hdr_entry;
57 1 hiro
        GtkWidget *val_entry;
58 1 hiro
        GtkWidget *customhdr_clist;
59 1 hiro
} customhdr;
60 1 hiro
61 1 hiro
/* widget creating functions */
62 1 hiro
static void prefs_custom_header_create        (void);
63 1 hiro
64 1 hiro
static void prefs_custom_header_set_dialog        (PrefsAccount *ac);
65 1 hiro
static void prefs_custom_header_set_list        (PrefsAccount *ac);
66 1 hiro
static gint prefs_custom_header_clist_set_row        (PrefsAccount *ac,
67 1 hiro
                                                 gint               row);
68 1 hiro
69 1 hiro
/* callback functions */
70 1 hiro
static void prefs_custom_header_add_cb                (void);
71 1 hiro
static void prefs_custom_header_delete_cb        (void);
72 1 hiro
static void prefs_custom_header_up                (void);
73 1 hiro
static void prefs_custom_header_down                (void);
74 1 hiro
static void prefs_custom_header_select                (GtkCList        *clist,
75 1 hiro
                                                 gint                 row,
76 1 hiro
                                                 gint                 column,
77 1 hiro
                                                 GdkEvent        *event);
78 1 hiro
79 1 hiro
static void prefs_custom_header_row_moved        (GtkCList        *clist,
80 1 hiro
                                                 gint                 source_row,
81 1 hiro
                                                 gint                 dest_row,
82 1 hiro
                                                 gpointer         data);
83 1 hiro
84 1 hiro
static gboolean prefs_custom_header_key_pressed        (GtkWidget        *widget,
85 1 hiro
                                                 GdkEventKey        *event,
86 1 hiro
                                                 gpointer         data);
87 1 hiro
static void prefs_custom_header_ok                (void);
88 1 hiro
static void prefs_custom_header_cancel                (void);
89 1 hiro
static gint prefs_custom_header_deleted                (GtkWidget        *widget,
90 1 hiro
                                                 GdkEventAny        *event,
91 1 hiro
                                                 gpointer         data);
92 1 hiro
93 1 hiro
static PrefsAccount *cur_ac = NULL;
94 1 hiro
95 1 hiro
void prefs_custom_header_open(PrefsAccount *ac)
96 1 hiro
{
97 1 hiro
        if (!customhdr.window) {
98 1 hiro
                prefs_custom_header_create();
99 1 hiro
        }
100 1 hiro
101 1 hiro
        manage_window_set_transient(GTK_WINDOW(customhdr.window));
102 1 hiro
        gtk_widget_grab_focus(customhdr.ok_btn);
103 1 hiro
104 1 hiro
        prefs_custom_header_set_dialog(ac);
105 1 hiro
106 1 hiro
        cur_ac = ac;
107 1 hiro
108 1 hiro
        gtk_widget_show(customhdr.window);
109 1 hiro
}
110 1 hiro
111 1 hiro
static void prefs_custom_header_create(void)
112 1 hiro
{
113 1 hiro
        GtkWidget *window;
114 1 hiro
        GtkWidget *vbox;
115 1 hiro
116 1 hiro
        GtkWidget *ok_btn;
117 1 hiro
        GtkWidget *cancel_btn;
118 1 hiro
119 1 hiro
        GtkWidget *confirm_area;
120 1 hiro
121 1 hiro
        GtkWidget *vbox1;
122 1 hiro
123 1 hiro
        GtkWidget *table1;
124 1 hiro
        GtkWidget *hdr_label;
125 1 hiro
        GtkWidget *hdr_combo;
126 1 hiro
        GtkWidget *val_label;
127 1 hiro
        GtkWidget *val_entry;
128 1 hiro
129 1 hiro
        GtkWidget *reg_hbox;
130 1 hiro
        GtkWidget *btn_hbox;
131 1 hiro
        GtkWidget *arrow;
132 1 hiro
        GtkWidget *add_btn;
133 1 hiro
        GtkWidget *del_btn;
134 1 hiro
135 1 hiro
        GtkWidget *ch_hbox;
136 1 hiro
        GtkWidget *ch_scrolledwin;
137 1 hiro
        GtkWidget *customhdr_clist;
138 1 hiro
139 1 hiro
        GtkWidget *btn_vbox;
140 1 hiro
        GtkWidget *up_btn;
141 1 hiro
        GtkWidget *down_btn;
142 1 hiro
143 1 hiro
        gchar *title[1];
144 1 hiro
145 1 hiro
        debug_print("Creating custom header setting window...\n");
146 1 hiro
147 1 hiro
        window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
148 1 hiro
        gtk_container_set_border_width (GTK_CONTAINER (window), 8);
149 1 hiro
        gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
150 1 hiro
        gtk_window_set_modal (GTK_WINDOW (window), TRUE);
151 1 hiro
        gtk_window_set_policy (GTK_WINDOW (window), FALSE, TRUE, FALSE);
152 1 hiro
153 1 hiro
        vbox = gtk_vbox_new (FALSE, 6);
154 1 hiro
        gtk_widget_show (vbox);
155 1 hiro
        gtk_container_add (GTK_CONTAINER (window), vbox);
156 1 hiro
157 1 hiro
        gtkut_button_set_create(&confirm_area, &ok_btn, _("OK"),
158 1 hiro
                                &cancel_btn, _("Cancel"), NULL, NULL);
159 1 hiro
        gtk_widget_show (confirm_area);
160 1 hiro
        gtk_box_pack_end (GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
161 1 hiro
        gtk_widget_grab_default (ok_btn);
162 1 hiro
163 1 hiro
        gtk_window_set_title (GTK_WINDOW(window), _("Custom header setting"));
164 1 hiro
        MANAGE_WINDOW_SIGNALS_CONNECT (window);
165 1 hiro
        g_signal_connect (G_OBJECT(window), "delete_event",
166 1 hiro
                          G_CALLBACK(prefs_custom_header_deleted),
167 1 hiro
                          NULL);
168 1 hiro
        g_signal_connect (G_OBJECT(window), "key_press_event",
169 1 hiro
                          G_CALLBACK(prefs_custom_header_key_pressed),
170 1 hiro
                          NULL);
171 1 hiro
        g_signal_connect (G_OBJECT(ok_btn), "clicked",
172 1 hiro
                          G_CALLBACK(prefs_custom_header_ok), NULL);
173 1 hiro
        g_signal_connect (G_OBJECT(cancel_btn), "clicked",
174 1 hiro
                          G_CALLBACK(prefs_custom_header_cancel), NULL);
175 1 hiro
176 1 hiro
        vbox1 = gtk_vbox_new (FALSE, VSPACING);
177 1 hiro
        gtk_widget_show (vbox1);
178 1 hiro
        gtk_box_pack_start (GTK_BOX (vbox), vbox1, TRUE, TRUE, 0);
179 1 hiro
        gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
180 1 hiro
181 1 hiro
        table1 = gtk_table_new (2, 2, FALSE);
182 1 hiro
        gtk_widget_show (table1);
183 1 hiro
        gtk_box_pack_start (GTK_BOX (vbox1), table1,
184 1 hiro
                            FALSE, FALSE, 0);
185 1 hiro
        gtk_table_set_row_spacings (GTK_TABLE (table1), 8);
186 1 hiro
        gtk_table_set_col_spacings (GTK_TABLE (table1), 8);
187 1 hiro
188 1 hiro
        hdr_label = gtk_label_new (_("Header"));
189 1 hiro
        gtk_widget_show (hdr_label);
190 1 hiro
        gtk_table_attach (GTK_TABLE (table1), hdr_label, 0, 1, 0, 1,
191 1 hiro
                          GTK_EXPAND | GTK_SHRINK | GTK_FILL,
192 1 hiro
                          0, 0, 0);
193 1 hiro
        gtk_misc_set_alignment (GTK_MISC (hdr_label), 0, 0.5);
194 1 hiro
195 1 hiro
        hdr_combo = gtk_combo_new ();
196 1 hiro
        gtk_widget_show (hdr_combo);
197 1 hiro
        gtk_table_attach (GTK_TABLE (table1), hdr_combo, 0, 1, 1, 2,
198 1 hiro
                          GTK_EXPAND | GTK_SHRINK | GTK_FILL,
199 1 hiro
                          0, 0, 0);
200 1 hiro
        gtk_widget_set_size_request (hdr_combo, 150, -1);
201 1 hiro
        gtkut_combo_set_items (GTK_COMBO (hdr_combo),
202 1 hiro
                               "User-Agent", "X-Face", "X-Operating-System",
203 1 hiro
                               NULL);
204 1 hiro
205 1 hiro
        val_label = gtk_label_new (_("Value"));
206 1 hiro
        gtk_widget_show (val_label);
207 1 hiro
        gtk_table_attach (GTK_TABLE (table1), val_label, 1, 2, 0, 1,
208 1 hiro
                          GTK_EXPAND | GTK_SHRINK | GTK_FILL,
209 1 hiro
                          0, 0, 0);
210 1 hiro
        gtk_misc_set_alignment (GTK_MISC (val_label), 0, 0.5);
211 1 hiro
212 1 hiro
        val_entry = gtk_entry_new ();
213 1 hiro
        gtk_widget_show (val_entry);
214 1 hiro
        gtk_table_attach (GTK_TABLE (table1), val_entry, 1, 2, 1, 2,
215 1 hiro
                          GTK_EXPAND | GTK_SHRINK | GTK_FILL,
216 1 hiro
                          0, 0, 0);
217 1 hiro
        gtk_widget_set_size_request (val_entry, 200, -1);
218 1 hiro
219 1 hiro
        /* add / delete */
220 1 hiro
221 1 hiro
        reg_hbox = gtk_hbox_new (FALSE, 4);
222 1 hiro
        gtk_widget_show (reg_hbox);
223 1 hiro
        gtk_box_pack_start (GTK_BOX (vbox1), reg_hbox, FALSE, FALSE, 0);
224 1 hiro
225 1 hiro
        arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_OUT);
226 1 hiro
        gtk_widget_show (arrow);
227 1 hiro
        gtk_box_pack_start (GTK_BOX (reg_hbox), arrow, FALSE, FALSE, 0);
228 1 hiro
        gtk_widget_set_size_request (arrow, -1, 16);
229 1 hiro
230 1 hiro
        btn_hbox = gtk_hbox_new (TRUE, 4);
231 1 hiro
        gtk_widget_show (btn_hbox);
232 1 hiro
        gtk_box_pack_start (GTK_BOX (reg_hbox), btn_hbox, FALSE, FALSE, 0);
233 1 hiro
234 1 hiro
        add_btn = gtk_button_new_with_label (_("Add"));
235 1 hiro
        gtk_widget_show (add_btn);
236 1 hiro
        gtk_box_pack_start (GTK_BOX (btn_hbox), add_btn, FALSE, TRUE, 0);
237 1 hiro
        g_signal_connect (G_OBJECT (add_btn), "clicked",
238 1 hiro
                          G_CALLBACK (prefs_custom_header_add_cb), NULL);
239 1 hiro
240 1 hiro
        del_btn = gtk_button_new_with_label (_(" Delete "));
241 1 hiro
        gtk_widget_show (del_btn);
242 1 hiro
        gtk_box_pack_start (GTK_BOX (btn_hbox), del_btn, FALSE, TRUE, 0);
243 1 hiro
        g_signal_connect (G_OBJECT (del_btn), "clicked",
244 1 hiro
                          G_CALLBACK (prefs_custom_header_delete_cb), NULL);
245 1 hiro
246 1 hiro
247 1 hiro
        ch_hbox = gtk_hbox_new (FALSE, 8);
248 1 hiro
        gtk_widget_show (ch_hbox);
249 1 hiro
        gtk_box_pack_start (GTK_BOX (vbox1), ch_hbox, TRUE, TRUE, 0);
250 1 hiro
251 1 hiro
        ch_scrolledwin = gtk_scrolled_window_new (NULL, NULL);
252 1 hiro
        gtk_widget_set_size_request (ch_scrolledwin, -1, 200);
253 1 hiro
        gtk_widget_show (ch_scrolledwin);
254 1 hiro
        gtk_box_pack_start (GTK_BOX (ch_hbox), ch_scrolledwin, TRUE, TRUE, 0);
255 1 hiro
        gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (ch_scrolledwin),
256 1 hiro
                                        GTK_POLICY_AUTOMATIC,
257 1 hiro
                                        GTK_POLICY_AUTOMATIC);
258 1 hiro
259 1 hiro
        title[0] = _("Custom headers");
260 1 hiro
        customhdr_clist = gtk_clist_new_with_titles(1, title);
261 1 hiro
        gtk_widget_show (customhdr_clist);
262 1 hiro
        gtk_container_add (GTK_CONTAINER (ch_scrolledwin), customhdr_clist);
263 1 hiro
        gtk_clist_set_column_width (GTK_CLIST (customhdr_clist), 0, 80);
264 1 hiro
        gtk_clist_set_selection_mode (GTK_CLIST (customhdr_clist),
265 1 hiro
                                      GTK_SELECTION_BROWSE);
266 1 hiro
        gtk_clist_set_reorderable (GTK_CLIST (customhdr_clist), TRUE);
267 1 hiro
        gtk_clist_set_use_drag_icons (GTK_CLIST (customhdr_clist), FALSE);
268 1 hiro
        GTK_WIDGET_UNSET_FLAGS (GTK_CLIST (customhdr_clist)->column[0].button,
269 1 hiro
                                GTK_CAN_FOCUS);
270 1 hiro
        g_signal_connect (G_OBJECT (customhdr_clist), "select_row",
271 1 hiro
                          G_CALLBACK (prefs_custom_header_select), NULL);
272 1 hiro
        g_signal_connect_after
273 1 hiro
                (G_OBJECT (customhdr_clist), "row_move",
274 1 hiro
                 G_CALLBACK (prefs_custom_header_row_moved), NULL);
275 1 hiro
276 1 hiro
        btn_vbox = gtk_vbox_new (FALSE, 8);
277 1 hiro
        gtk_widget_show (btn_vbox);
278 1 hiro
        gtk_box_pack_start (GTK_BOX (ch_hbox), btn_vbox, FALSE, FALSE, 0);
279 1 hiro
280 1 hiro
        up_btn = gtk_button_new_with_label (_("Up"));
281 1 hiro
        gtk_widget_show (up_btn);
282 1 hiro
        gtk_box_pack_start (GTK_BOX (btn_vbox), up_btn, FALSE, FALSE, 0);
283 1 hiro
        g_signal_connect (G_OBJECT (up_btn), "clicked",
284 1 hiro
                          G_CALLBACK (prefs_custom_header_up), NULL);
285 1 hiro
286 1 hiro
        down_btn = gtk_button_new_with_label (_("Down"));
287 1 hiro
        gtk_widget_show (down_btn);
288 1 hiro
        gtk_box_pack_start (GTK_BOX (btn_vbox), down_btn, FALSE, FALSE, 0);
289 1 hiro
        g_signal_connect (G_OBJECT (down_btn), "clicked",
290 1 hiro
                          G_CALLBACK (prefs_custom_header_down), NULL);
291 1 hiro
292 1 hiro
        gtk_widget_show_all(window);
293 1 hiro
294 1 hiro
        customhdr.window     = window;
295 1 hiro
        customhdr.ok_btn     = ok_btn;
296 1 hiro
        customhdr.cancel_btn = cancel_btn;
297 1 hiro
298 1 hiro
        customhdr.hdr_combo  = hdr_combo;
299 1 hiro
        customhdr.hdr_entry  = GTK_COMBO (hdr_combo)->entry;
300 1 hiro
        customhdr.val_entry  = val_entry;
301 1 hiro
302 1 hiro
        customhdr.customhdr_clist   = customhdr_clist;
303 1 hiro
}
304 1 hiro
305 1 hiro
void prefs_custom_header_read_config(PrefsAccount *ac)
306 1 hiro
{
307 1 hiro
        gchar *rcpath;
308 1 hiro
        FILE *fp;
309 1 hiro
        gchar buf[PREFSBUFSIZE];
310 1 hiro
        CustomHeader *ch;
311 1 hiro
312 1 hiro
        debug_print("Reading custom header configuration...\n");
313 1 hiro
314 1 hiro
        rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
315 1 hiro
                             CUSTOM_HEADER_RC, NULL);
316 1 hiro
        if ((fp = fopen(rcpath, "rb")) == NULL) {
317 1 hiro
                if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
318 1 hiro
                g_free(rcpath);
319 1 hiro
                ac->customhdr_list = NULL;
320 1 hiro
                return;
321 1 hiro
        }
322 1 hiro
        g_free(rcpath);
323 1 hiro
324 1 hiro
        /* remove all previous headers list */
325 1 hiro
        while (ac->customhdr_list != NULL) {
326 1 hiro
                ch = (CustomHeader *)ac->customhdr_list->data;
327 1 hiro
                custom_header_free(ch);
328 1 hiro
                ac->customhdr_list = g_slist_remove(ac->customhdr_list, ch);
329 1 hiro
        }
330 1 hiro
331 1 hiro
        while (fgets(buf, sizeof(buf), fp) != NULL) {
332 1 hiro
                ch = custom_header_read_str(buf);
333 1 hiro
                if (ch) {
334 1 hiro
                        if (ch->account_id == ac->account_id) {
335 1 hiro
                                ac->customhdr_list =
336 1 hiro
                                        g_slist_append(ac->customhdr_list, ch);
337 1 hiro
                        } else
338 1 hiro
                                custom_header_free(ch);
339 1 hiro
                }
340 1 hiro
        }
341 1 hiro
342 1 hiro
        fclose(fp);
343 1 hiro
}
344 1 hiro
345 1 hiro
void prefs_custom_header_write_config(PrefsAccount *ac)
346 1 hiro
{
347 1 hiro
        gchar *rcpath;
348 1 hiro
        PrefFile *pfile;
349 1 hiro
        GSList *cur;
350 1 hiro
        gchar buf[PREFSBUFSIZE];
351 1 hiro
        FILE * fp;
352 1 hiro
        CustomHeader *ch;
353 1 hiro
354 1 hiro
        GSList *all_hdrs = NULL;
355 1 hiro
356 1 hiro
        debug_print("Writing custom header configuration...\n");
357 1 hiro
358 1 hiro
        rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
359 1 hiro
                             CUSTOM_HEADER_RC, NULL);
360 1 hiro
361 1 hiro
        if ((fp = fopen(rcpath, "rb")) == NULL) {
362 1 hiro
                if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
363 1 hiro
        } else {
364 1 hiro
                all_hdrs = NULL;
365 1 hiro
366 1 hiro
                while (fgets(buf, sizeof(buf), fp) != NULL) {
367 1 hiro
                        ch = custom_header_read_str(buf);
368 1 hiro
                        if (ch) {
369 1 hiro
                                if (ch->account_id != ac->account_id)
370 1 hiro
                                        all_hdrs =
371 1 hiro
                                                g_slist_append(all_hdrs, ch);
372 1 hiro
                                else
373 1 hiro
                                        custom_header_free(ch);
374 1 hiro
                        }
375 1 hiro
                }
376 1 hiro
377 1 hiro
                fclose(fp);
378 1 hiro
        }
379 1 hiro
380 1 hiro
        if ((pfile = prefs_file_open(rcpath)) == NULL) {
381 1 hiro
                g_warning(_("failed to write configuration to file\n"));
382 1 hiro
                g_free(rcpath);
383 1 hiro
                return;
384 1 hiro
        }
385 1 hiro
386 1 hiro
        for (cur = all_hdrs; cur != NULL; cur = cur->next) {
387 1 hiro
                 CustomHeader *hdr = (CustomHeader *)cur->data;
388 1 hiro
                gchar *chstr;
389 1 hiro
390 1 hiro
                chstr = custom_header_get_str(hdr);
391 1 hiro
                if (fputs(chstr, pfile->fp) == EOF ||
392 1 hiro
                    fputc('\n', pfile->fp) == EOF) {
393 1 hiro
                        FILE_OP_ERROR(rcpath, "fputs || fputc");
394 1 hiro
                        prefs_file_close_revert(pfile);
395 1 hiro
                        g_free(rcpath);
396 1 hiro
                        g_free(chstr);
397 1 hiro
                        return;
398 1 hiro
                }
399 1 hiro
                g_free(chstr);
400 1 hiro
        }
401 1 hiro
402 1 hiro
        for (cur = ac->customhdr_list; cur != NULL; cur = cur->next) {
403 1 hiro
                 CustomHeader *hdr = (CustomHeader *)cur->data;
404 1 hiro
                gchar *chstr;
405 1 hiro
406 1 hiro
                chstr = custom_header_get_str(hdr);
407 1 hiro
                if (fputs(chstr, pfile->fp) == EOF ||
408 1 hiro
                    fputc('\n', pfile->fp) == EOF) {
409 1 hiro
                        FILE_OP_ERROR(rcpath, "fputs || fputc");
410 1 hiro
                        prefs_file_close_revert(pfile);
411 1 hiro
                        g_free(rcpath);
412 1 hiro
                        g_free(chstr);
413 1 hiro
                        return;
414 1 hiro
                }
415 1 hiro
                g_free(chstr);
416 1 hiro
        }
417 1 hiro
418 1 hiro
        g_free(rcpath);
419 1 hiro
420 1 hiro
         while (all_hdrs != NULL) {
421 1 hiro
                 ch = (CustomHeader *)all_hdrs->data;
422 1 hiro
                 custom_header_free(ch);
423 1 hiro
                 all_hdrs = g_slist_remove(all_hdrs, ch);
424 1 hiro
         }
425 1 hiro
426 1 hiro
        if (prefs_file_close(pfile) < 0) {
427 1 hiro
                g_warning(_("failed to write configuration to file\n"));
428 1 hiro
                return;
429 1 hiro
        }
430 1 hiro
}
431 1 hiro
432 1 hiro
static void prefs_custom_header_set_dialog(PrefsAccount *ac)
433 1 hiro
{
434 1 hiro
        GtkCList *clist = GTK_CLIST(customhdr.customhdr_clist);
435 1 hiro
        GSList *cur;
436 1 hiro
        gchar *ch_str[1];
437 1 hiro
        gint row;
438 1 hiro
439 1 hiro
        gtk_clist_freeze(clist);
440 1 hiro
        gtk_clist_clear(clist);
441 1 hiro
442 1 hiro
        for (cur = ac->customhdr_list; cur != NULL; cur = cur->next) {
443 1 hiro
                 CustomHeader *ch = (CustomHeader *)cur->data;
444 1 hiro
445 1 hiro
                ch_str[0] = g_strdup_printf("%s: %s", ch->name,
446 1 hiro
                                            ch->value ? ch->value : "");
447 1 hiro
                row = gtk_clist_append(clist, ch_str);
448 1 hiro
                gtk_clist_set_row_data(clist, row, ch);
449 1 hiro
450 1 hiro
                g_free(ch_str[0]);
451 1 hiro
        }
452 1 hiro
453 1 hiro
        gtk_clist_thaw(clist);
454 1 hiro
}
455 1 hiro
456 1 hiro
static void prefs_custom_header_set_list(PrefsAccount *ac)
457 1 hiro
{
458 1 hiro
        gint row = 0;
459 1 hiro
        CustomHeader *ch;
460 1 hiro
461 1 hiro
        g_slist_free(ac->customhdr_list);
462 1 hiro
        ac->customhdr_list = NULL;
463 1 hiro
464 1 hiro
        while ((ch = gtk_clist_get_row_data
465 1 hiro
                (GTK_CLIST(customhdr.customhdr_clist), row)) != NULL) {
466 1 hiro
                ac->customhdr_list = g_slist_append(ac->customhdr_list, ch);
467 1 hiro
                row++;
468 1 hiro
        }
469 1 hiro
}
470 1 hiro
471 1 hiro
static gint prefs_custom_header_clist_set_row(PrefsAccount *ac, gint row)
472 1 hiro
{
473 1 hiro
        GtkCList *clist = GTK_CLIST(customhdr.customhdr_clist);
474 1 hiro
        CustomHeader *ch;
475 1 hiro
        const gchar *entry_text;
476 1 hiro
        gchar *ch_str[1];
477 1 hiro
478 1 hiro
        entry_text = gtk_entry_get_text(GTK_ENTRY(customhdr.hdr_entry));
479 1 hiro
        if (entry_text[0] == '\0') {
480 1 hiro
                alertpanel_error(_("Header name is not set."));
481 1 hiro
                return -1;
482 1 hiro
        }
483 1 hiro
484 1 hiro
        ch = g_new0(CustomHeader, 1);
485 1 hiro
486 1 hiro
        ch->account_id = ac->account_id;
487 1 hiro
488 1 hiro
        ch->name = g_strdup(entry_text);
489 1 hiro
        unfold_line(ch->name);
490 1 hiro
        g_strstrip(ch->name);
491 1 hiro
        gtk_entry_set_text(GTK_ENTRY(customhdr.hdr_entry), ch->name);
492 1 hiro
493 1 hiro
        entry_text = gtk_entry_get_text(GTK_ENTRY(customhdr.val_entry));
494 1 hiro
        if (entry_text[0] != '\0') {
495 1 hiro
                ch->value = g_strdup(entry_text);
496 1 hiro
                unfold_line(ch->value);
497 1 hiro
                g_strstrip(ch->value);
498 1 hiro
                gtk_entry_set_text(GTK_ENTRY(customhdr.val_entry), ch->value);
499 1 hiro
        }
500 1 hiro
501 1 hiro
        ch_str[0] = g_strdup_printf("%s: %s", ch->name,
502 1 hiro
                                    ch->value ? ch->value : "");
503 1 hiro
504 1 hiro
        if (row < 0)
505 1 hiro
                row = gtk_clist_append(clist, ch_str);
506 1 hiro
        else {
507 1 hiro
                CustomHeader *tmp_ch;
508 1 hiro
509 1 hiro
                gtk_clist_set_text(clist, row, 0, ch_str[0]);
510 1 hiro
                tmp_ch = gtk_clist_get_row_data(clist, row);
511 1 hiro
                if (tmp_ch)
512 1 hiro
                        custom_header_free(tmp_ch);
513 1 hiro
        }
514 1 hiro
515 1 hiro
        gtk_clist_set_row_data(clist, row, ch);
516 1 hiro
517 1 hiro
        g_free(ch_str[0]);
518 1 hiro
519 1 hiro
        prefs_custom_header_set_list(cur_ac);
520 1 hiro
521 1 hiro
        return row;
522 1 hiro
}
523 1 hiro
524 1 hiro
static void prefs_custom_header_add_cb(void)
525 1 hiro
{
526 1 hiro
        prefs_custom_header_clist_set_row(cur_ac, -1);
527 1 hiro
}
528 1 hiro
529 1 hiro
static void prefs_custom_header_delete_cb(void)
530 1 hiro
{
531 1 hiro
        GtkCList *clist = GTK_CLIST(customhdr.customhdr_clist);
532 1 hiro
        CustomHeader *ch;
533 1 hiro
        gint row;
534 1 hiro
535 1 hiro
        if (!clist->selection) return;
536 1 hiro
        row = GPOINTER_TO_INT(clist->selection->data);
537 1 hiro
538 1 hiro
        if (alertpanel(_("Delete header"),
539 1 hiro
                       _("Do you really want to delete this header?"),
540 1 hiro
                       _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
541 1 hiro
                return;
542 1 hiro
543 1 hiro
        ch = gtk_clist_get_row_data(clist, row);
544 1 hiro
        custom_header_free(ch);
545 1 hiro
        gtk_clist_remove(clist, row);
546 1 hiro
        cur_ac->customhdr_list = g_slist_remove(cur_ac->customhdr_list, ch);
547 1 hiro
}
548 1 hiro
549 1 hiro
static void prefs_custom_header_up(void)
550 1 hiro
{
551 1 hiro
        GtkCList *clist = GTK_CLIST(customhdr.customhdr_clist);
552 1 hiro
        gint row;
553 1 hiro
554 1 hiro
        if (!clist->selection) return;
555 1 hiro
556 1 hiro
        row = GPOINTER_TO_INT(clist->selection->data);
557 1 hiro
        if (row > 0)
558 1 hiro
                gtk_clist_row_move(clist, row, row - 1);
559 1 hiro
}
560 1 hiro
561 1 hiro
static void prefs_custom_header_down(void)
562 1 hiro
{
563 1 hiro
        GtkCList *clist = GTK_CLIST(customhdr.customhdr_clist);
564 1 hiro
        gint row;
565 1 hiro
566 1 hiro
        if (!clist->selection) return;
567 1 hiro
568 1 hiro
        row = GPOINTER_TO_INT(clist->selection->data);
569 1 hiro
        if (row >= 0 && row < clist->rows - 1)
570 1 hiro
                gtk_clist_row_move(clist, row, row + 1);
571 1 hiro
}
572 1 hiro
573 1 hiro
#define ENTRY_SET_TEXT(entry, str) \
574 1 hiro
        gtk_entry_set_text(GTK_ENTRY(entry), str ? str : "")
575 1 hiro
576 1 hiro
static void prefs_custom_header_select(GtkCList *clist, gint row, gint column,
577 1 hiro
                                       GdkEvent *event)
578 1 hiro
{
579 1 hiro
        CustomHeader *ch;
580 1 hiro
        CustomHeader default_ch = { 0, "", NULL };
581 1 hiro
582 1 hiro
        ch = gtk_clist_get_row_data(clist, row);
583 1 hiro
        if (!ch) ch = &default_ch;
584 1 hiro
585 1 hiro
        ENTRY_SET_TEXT(customhdr.hdr_entry, ch->name);
586 1 hiro
        ENTRY_SET_TEXT(customhdr.val_entry, ch->value);
587 1 hiro
}
588 1 hiro
589 1 hiro
#undef ENTRY_SET_TEXT
590 1 hiro
591 1 hiro
static void prefs_custom_header_row_moved(GtkCList *clist, gint source_row,
592 1 hiro
                                          gint dest_row, gpointer data)
593 1 hiro
{
594 1 hiro
        prefs_custom_header_set_list(cur_ac);
595 1 hiro
}
596 1 hiro
597 1 hiro
static gboolean prefs_custom_header_key_pressed(GtkWidget *widget,
598 1 hiro
                                                GdkEventKey *event,
599 1 hiro
                                                gpointer data)
600 1 hiro
{
601 1 hiro
        if (event && event->keyval == GDK_Escape)
602 1 hiro
                prefs_custom_header_cancel();
603 1 hiro
        return FALSE;
604 1 hiro
}
605 1 hiro
606 1 hiro
static void prefs_custom_header_ok(void)
607 1 hiro
{
608 1 hiro
        prefs_custom_header_write_config(cur_ac);
609 1 hiro
        gtk_widget_hide(customhdr.window);
610 1 hiro
}
611 1 hiro
612 1 hiro
static void prefs_custom_header_cancel(void)
613 1 hiro
{
614 1 hiro
        prefs_custom_header_read_config(cur_ac);
615 1 hiro
        gtk_widget_hide(customhdr.window);
616 1 hiro
}
617 1 hiro
618 1 hiro
static gint prefs_custom_header_deleted(GtkWidget *widget, GdkEventAny *event,
619 1 hiro
                                        gpointer data)
620 1 hiro
{
621 1 hiro
        prefs_custom_header_cancel();
622 1 hiro
        return TRUE;
623 1 hiro
}