2005-01-29 [paul] 1.0.0cvs23.2
[claws.git] / src / prefs_customheader.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2005 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <gtk/gtk.h>
28 #include <gdk/gdkkeysyms.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <errno.h>
33
34 #include "intl.h"
35 #include "main.h"
36 #include "prefs_gtk.h"
37 #include "prefs_customheader.h"
38 #include "prefs_common.h"
39 #include "prefs_account.h"
40 #include "mainwindow.h"
41 #include "foldersel.h"
42 #include "manage_window.h"
43 #include "customheader.h"
44 #include "folder.h"
45 #include "utils.h"
46 #include "gtkutils.h"
47 #include "alertpanel.h"
48
49 enum {
50         CUSTHDR_STRING,         /*!< display string managed by list store */
51         CUSTHDR_DATA,           /*!< string managed by us */
52         N_CUSTHDR_COLUMNS
53 };
54
55 static struct CustomHdr {
56         GtkWidget *window;
57
58         GtkWidget *ok_btn;
59         GtkWidget *cancel_btn;
60
61         GtkWidget *hdr_combo;
62         GtkWidget *hdr_entry;
63         GtkWidget *val_entry;
64         GtkWidget *list_view;
65 } customhdr;
66
67 /* widget creating functions */
68 static void prefs_custom_header_create  (void);
69
70 static void prefs_custom_header_set_dialog              (PrefsAccount *ac);
71 static void prefs_custom_header_set_list                (PrefsAccount *ac);
72 static void prefs_custom_header_list_view_set_row       (PrefsAccount *ac);
73
74 /* callback functions */
75 static void prefs_custom_header_add_cb          (void);
76 static void prefs_custom_header_delete_cb       (void);
77 static void prefs_custom_header_up              (void);
78 static void prefs_custom_header_down            (void);
79
80 static gboolean prefs_custom_header_key_pressed (GtkWidget      *widget,
81                                                  GdkEventKey    *event,
82                                                  gpointer        data);
83 static void prefs_custom_header_ok              (void);
84 static void prefs_custom_header_cancel          (void);
85 static gint prefs_custom_header_deleted         (GtkWidget      *widget,
86                                                  GdkEventAny    *event,
87                                                  gpointer        data);
88
89 static GtkListStore* prefs_custom_header_create_data_store      (void);
90
91 static void prefs_custom_header_list_view_insert_header (GtkWidget *list_view,
92                                                          GtkTreeIter *row_iter,
93                                                          gchar *header,
94                                                          gpointer data);
95
96 static GtkWidget *prefs_custom_header_list_view_create (void);
97
98 static void prefs_custom_header_create_list_view_columns        (GtkWidget *list_view);
99
100 static gboolean prefs_custom_header_selected    (GtkTreeSelection *selector,
101                                                  GtkTreeModel *model, 
102                                                  GtkTreePath *path,
103                                                  gboolean currently_selected,
104                                                  gpointer data);
105
106
107 static PrefsAccount *cur_ac = NULL;
108
109 void prefs_custom_header_open(PrefsAccount *ac)
110 {
111         if (!customhdr.window) {
112                 prefs_custom_header_create();
113         }
114
115         manage_window_set_transient(GTK_WINDOW(customhdr.window));
116         gtk_widget_grab_focus(customhdr.ok_btn);
117
118         prefs_custom_header_set_dialog(ac);
119
120         cur_ac = ac;
121
122         gtk_widget_show(customhdr.window);
123 }
124
125 static void prefs_custom_header_create(void)
126 {
127         GtkWidget *window;
128         GtkWidget *vbox;
129
130         GtkWidget *ok_btn;
131         GtkWidget *cancel_btn;
132
133         GtkWidget *confirm_area;
134
135         GtkWidget *vbox1;
136
137         GtkWidget *table1;
138         GtkWidget *hdr_label;
139         GtkWidget *hdr_combo;
140         GtkWidget *val_label;
141         GtkWidget *val_entry;
142
143         GtkWidget *reg_hbox;
144         GtkWidget *btn_hbox;
145         GtkWidget *arrow;
146         GtkWidget *add_btn;
147         GtkWidget *del_btn;
148
149         GtkWidget *ch_hbox;
150         GtkWidget *ch_scrolledwin;
151         GtkWidget *list_view;
152
153         GtkWidget *btn_vbox;
154         GtkWidget *up_btn;
155         GtkWidget *down_btn;
156
157         gchar *title[1];
158
159         debug_print("Creating custom header setting window...\n");
160
161         window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
162         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
163         gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
164         gtk_window_set_modal (GTK_WINDOW (window), TRUE);
165         gtk_window_set_resizable(GTK_WINDOW (window), TRUE);
166
167         vbox = gtk_vbox_new (FALSE, 6);
168         gtk_widget_show (vbox);
169         gtk_container_add (GTK_CONTAINER (window), vbox);
170
171         gtkut_stock_button_set_create(&confirm_area, &ok_btn, GTK_STOCK_OK,
172                                       &cancel_btn, GTK_STOCK_CANCEL,
173                                       NULL, NULL);
174         gtk_widget_show (confirm_area);
175         gtk_box_pack_end (GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
176         gtk_widget_grab_default (ok_btn);
177
178         gtk_window_set_title (GTK_WINDOW(window), _("Custom header configuration"));
179         MANAGE_WINDOW_SIGNALS_CONNECT (window);
180         g_signal_connect (G_OBJECT(window), "delete_event",
181                           G_CALLBACK(prefs_custom_header_deleted),
182                           NULL);
183         g_signal_connect (G_OBJECT(window), "key_press_event",
184                           G_CALLBACK(prefs_custom_header_key_pressed),
185                           NULL);
186         g_signal_connect (G_OBJECT(ok_btn), "clicked",
187                           G_CALLBACK(prefs_custom_header_ok), NULL);
188         g_signal_connect (G_OBJECT(cancel_btn), "clicked",
189                           G_CALLBACK(prefs_custom_header_cancel), NULL);
190
191         vbox1 = gtk_vbox_new (FALSE, VSPACING);
192         gtk_widget_show (vbox1);
193         gtk_box_pack_start (GTK_BOX (vbox), vbox1, TRUE, TRUE, 0);
194         gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
195
196         table1 = gtk_table_new (2, 2, FALSE);
197         gtk_widget_show (table1);
198         gtk_box_pack_start (GTK_BOX (vbox1), table1,
199                             FALSE, FALSE, 0);
200         gtk_table_set_row_spacings (GTK_TABLE (table1), 8);
201         gtk_table_set_col_spacings (GTK_TABLE (table1), 8);
202
203         hdr_label = gtk_label_new (_("Header"));
204         gtk_widget_show (hdr_label);
205         gtk_table_attach (GTK_TABLE (table1), hdr_label, 0, 1, 0, 1,
206                           GTK_EXPAND | GTK_SHRINK | GTK_FILL,
207                           0, 0, 0);
208         gtk_misc_set_alignment (GTK_MISC (hdr_label), 0, 0.5);
209         
210         hdr_combo = gtk_combo_new ();
211         gtk_widget_show (hdr_combo);
212         gtk_table_attach (GTK_TABLE (table1), hdr_combo, 0, 1, 1, 2,
213                           GTK_EXPAND | GTK_SHRINK | GTK_FILL,
214                           0, 0, 0);
215         gtk_widget_set_size_request (hdr_combo, 150, -1);
216         gtkut_combo_set_items (GTK_COMBO (hdr_combo),
217                                "User-Agent", "X-Face", "X-Operating-System",
218                                NULL);
219
220         val_label = gtk_label_new (_("Value"));
221         gtk_widget_show (val_label);
222         gtk_table_attach (GTK_TABLE (table1), val_label, 1, 2, 0, 1,
223                           GTK_EXPAND | GTK_SHRINK | GTK_FILL,
224                           0, 0, 0);
225         gtk_misc_set_alignment (GTK_MISC (val_label), 0, 0.5);
226         
227         val_entry = gtk_entry_new ();
228         gtk_widget_show (val_entry);
229         gtk_table_attach (GTK_TABLE (table1), val_entry, 1, 2, 1, 2,
230                           GTK_EXPAND | GTK_SHRINK | GTK_FILL,
231                           0, 0, 0);
232         gtk_widget_set_size_request (val_entry, 200, -1);
233
234         /* add / delete */
235
236         reg_hbox = gtk_hbox_new (FALSE, 4);
237         gtk_widget_show (reg_hbox);
238         gtk_box_pack_start (GTK_BOX (vbox1), reg_hbox, FALSE, FALSE, 0);
239
240         arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_OUT);
241         gtk_widget_show (arrow);
242         gtk_box_pack_start (GTK_BOX (reg_hbox), arrow, FALSE, FALSE, 0);
243         gtk_widget_set_size_request (arrow, -1, 16);
244
245         btn_hbox = gtk_hbox_new (TRUE, 4);
246         gtk_widget_show (btn_hbox);
247         gtk_box_pack_start (GTK_BOX (reg_hbox), btn_hbox, FALSE, FALSE, 0);
248
249         add_btn = gtk_button_new_from_stock (GTK_STOCK_ADD);
250         gtk_widget_show (add_btn);
251         gtk_box_pack_start (GTK_BOX (btn_hbox), add_btn, FALSE, TRUE, 0);
252         g_signal_connect (G_OBJECT (add_btn), "clicked",
253                           G_CALLBACK (prefs_custom_header_add_cb),
254                           NULL);
255
256         del_btn = gtk_button_new_from_stock (GTK_STOCK_REMOVE);
257         gtk_widget_show (del_btn);
258         gtk_box_pack_start (GTK_BOX (btn_hbox), del_btn, FALSE, TRUE, 0);
259         g_signal_connect (G_OBJECT (del_btn), "clicked",
260                           G_CALLBACK (prefs_custom_header_delete_cb),
261                           NULL);
262
263
264         ch_hbox = gtk_hbox_new (FALSE, 8);
265         gtk_widget_show (ch_hbox);
266         gtk_box_pack_start (GTK_BOX (vbox1), ch_hbox, TRUE, TRUE, 0);
267
268         ch_scrolledwin = gtk_scrolled_window_new (NULL, NULL);
269         gtk_widget_set_size_request (ch_scrolledwin, -1, 200);
270         gtk_widget_show (ch_scrolledwin);
271         gtk_box_pack_start (GTK_BOX (ch_hbox), ch_scrolledwin, TRUE, TRUE, 0);
272         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (ch_scrolledwin),
273                                         GTK_POLICY_AUTOMATIC,
274                                         GTK_POLICY_AUTOMATIC);
275
276         list_view = prefs_custom_header_list_view_create();
277         gtk_widget_show (list_view);
278         gtk_container_add (GTK_CONTAINER (ch_scrolledwin), list_view);
279
280         btn_vbox = gtk_vbox_new (FALSE, 8);
281         gtk_widget_show (btn_vbox);
282         gtk_box_pack_start (GTK_BOX (ch_hbox), btn_vbox, FALSE, FALSE, 0);
283
284         up_btn = gtk_button_new_from_stock (GTK_STOCK_GO_UP);
285         gtk_widget_show (up_btn);
286         gtk_box_pack_start (GTK_BOX (btn_vbox), up_btn, FALSE, FALSE, 0);
287         g_signal_connect (G_OBJECT (up_btn), "clicked",
288                           G_CALLBACK (prefs_custom_header_up), NULL);
289
290         down_btn = gtk_button_new_from_stock (GTK_STOCK_GO_DOWN);
291         gtk_widget_show (down_btn);
292         gtk_box_pack_start (GTK_BOX (btn_vbox), down_btn, FALSE, FALSE, 0);
293         g_signal_connect (G_OBJECT (down_btn), "clicked",
294                           G_CALLBACK (prefs_custom_header_down), NULL);
295
296         gtk_widget_show_all(window);
297
298         customhdr.window     = window;
299         customhdr.ok_btn     = ok_btn;
300         customhdr.cancel_btn = cancel_btn;
301
302         customhdr.hdr_combo  = hdr_combo;
303         customhdr.hdr_entry  = GTK_COMBO (hdr_combo)->entry;
304         customhdr.val_entry  = val_entry;
305
306         customhdr.list_view   = list_view;
307 }
308
309 void prefs_custom_header_read_config(PrefsAccount *ac)
310 {
311         gchar *rcpath;
312         FILE *fp;
313         gchar buf[PREFSBUFSIZE];
314         CustomHeader *ch;
315
316         debug_print("Reading custom header configuration...\n");
317
318         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
319                              CUSTOM_HEADER_RC, NULL);
320         if ((fp = fopen(rcpath, "rb")) == NULL) {
321                 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
322                 g_free(rcpath);
323                 ac->customhdr_list = NULL;
324                 return;
325         }
326         g_free(rcpath);
327
328         /* remove all previous headers list */
329         while (ac->customhdr_list != NULL) {
330                 ch = (CustomHeader *)ac->customhdr_list->data;
331                 custom_header_free(ch);
332                 ac->customhdr_list = g_slist_remove(ac->customhdr_list, ch);
333         }
334
335         while (fgets(buf, sizeof(buf), fp) != NULL) {
336                 ch = custom_header_read_str(buf);
337                 if (ch) {
338                         if (ch->account_id == ac->account_id) {
339                                 ac->customhdr_list =
340                                         g_slist_append(ac->customhdr_list, ch);
341                         } else
342                                 custom_header_free(ch);
343                 }
344         }
345
346         fclose(fp);
347 }
348
349 void prefs_custom_header_write_config(PrefsAccount *ac)
350 {
351         gchar *rcpath;
352         PrefFile *pfile;
353         GSList *cur;
354         gchar buf[PREFSBUFSIZE];
355         FILE * fp;
356         CustomHeader *ch;
357
358         GSList *all_hdrs = NULL;
359
360         debug_print("Writing custom header configuration...\n");
361
362         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
363                              CUSTOM_HEADER_RC, NULL);
364
365         if ((fp = fopen(rcpath, "rb")) == NULL) {
366                 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
367         } else {
368                 all_hdrs = NULL;
369
370                 while (fgets(buf, sizeof(buf), fp) != NULL) {
371                         ch = custom_header_read_str(buf);
372                         if (ch) {
373                                 if (ch->account_id != ac->account_id)
374                                         all_hdrs =
375                                                 g_slist_append(all_hdrs, ch);
376                                 else
377                                         custom_header_free(ch);
378                         }
379                 }
380
381                 fclose(fp);
382         }
383
384         if ((pfile = prefs_write_open(rcpath)) == NULL) {
385                 g_warning("failed to write configuration to file\n");
386                 g_free(rcpath);
387                 return;
388         }
389
390         for (cur = all_hdrs; cur != NULL; cur = cur->next) {
391                 CustomHeader *hdr = (CustomHeader *)cur->data;
392                 gchar *chstr;
393
394                 chstr = custom_header_get_str(hdr);
395                 if (fputs(chstr, pfile->fp) == EOF ||
396                     fputc('\n', pfile->fp) == EOF) {
397                         FILE_OP_ERROR(rcpath, "fputs || fputc");
398                         prefs_file_close_revert(pfile);
399                         g_free(rcpath);
400                         g_free(chstr);
401                         return;
402                 }
403                 g_free(chstr);
404         }
405
406         for (cur = ac->customhdr_list; cur != NULL; cur = cur->next) {
407                 CustomHeader *hdr = (CustomHeader *)cur->data;
408                 gchar *chstr;
409
410                 chstr = custom_header_get_str(hdr);
411                 if (fputs(chstr, pfile->fp) == EOF ||
412                     fputc('\n', pfile->fp) == EOF) {
413                         FILE_OP_ERROR(rcpath, "fputs || fputc");
414                         prefs_file_close_revert(pfile);
415                         g_free(rcpath);
416                         g_free(chstr);
417                         return;
418                 }
419                 g_free(chstr);
420         }
421
422         g_free(rcpath);
423
424         while (all_hdrs != NULL) {
425                 ch = (CustomHeader *)all_hdrs->data;
426                 custom_header_free(ch);
427                 all_hdrs = g_slist_remove(all_hdrs, ch);
428         }
429
430         if (prefs_file_close(pfile) < 0) {
431                 g_warning("failed to write configuration to file\n");
432                 return;
433         }
434 }
435
436 static void prefs_custom_header_set_dialog(PrefsAccount *ac)
437 {
438         GtkListStore *store;
439         GSList *cur;
440         
441         store = GTK_LIST_STORE(gtk_tree_view_get_model
442                                 (GTK_TREE_VIEW(customhdr.list_view)));
443         gtk_list_store_clear(store);
444
445         for (cur = ac->customhdr_list; cur != NULL; cur = cur->next) {
446                 CustomHeader *ch = (CustomHeader *)cur->data;
447                 gchar *ch_str;
448
449                 ch_str = g_strdup_printf("%s: %s", ch->name,
450                                          ch->value ? ch->value : "");
451
452                 prefs_custom_header_list_view_insert_header
453                         (customhdr.list_view, NULL, ch_str, ch);                                                 
454
455                 g_free(ch_str);
456         }
457 }
458
459 static void prefs_custom_header_set_list(PrefsAccount *ac)
460 {
461         CustomHeader *ch;
462         GtkTreeIter iter;
463         GtkListStore *store;
464
465         g_slist_free(ac->customhdr_list);
466         ac->customhdr_list = NULL;
467
468         store = GTK_LIST_STORE(gtk_tree_view_get_model
469                                 (GTK_TREE_VIEW(customhdr.list_view)));
470
471         if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter)) {
472                 do {
473                         gtk_tree_model_get(GTK_TREE_MODEL(store), &iter,
474                                            CUSTHDR_DATA, &ch,
475                                            -1);
476                         ac->customhdr_list = g_slist_append(ac->customhdr_list, ch);
477                 } while (gtk_tree_model_iter_next(GTK_TREE_MODEL(store),
478                                                   &iter));
479         }
480 }
481
482 static void prefs_custom_header_list_view_set_row(PrefsAccount *ac)
483 {
484         CustomHeader *ch;
485         const gchar *entry_text;
486         gchar *ch_str;
487         GtkListStore *store;
488
489         store = GTK_LIST_STORE(gtk_tree_view_get_model
490                                 (GTK_TREE_VIEW(customhdr.list_view)));
491
492         entry_text = gtk_entry_get_text(GTK_ENTRY(customhdr.hdr_entry));
493         if (entry_text[0] == '\0') {
494                 alertpanel_error(_("Header name is not set."));
495                 return;
496         }
497         if (!custom_header_is_allowed(entry_text)) {
498                 alertpanel_error(_("This Header name is not allowed as a custom header."));
499                 return;
500         }
501
502         ch = g_new0(CustomHeader, 1);
503
504         ch->account_id = ac->account_id;
505
506         ch->name = g_strdup(entry_text);
507         unfold_line(ch->name);
508         g_strstrip(ch->name);
509         gtk_entry_set_text(GTK_ENTRY(customhdr.hdr_entry), ch->name);
510
511         entry_text = gtk_entry_get_text(GTK_ENTRY(customhdr.val_entry));
512         if (entry_text[0] != '\0') {
513                 ch->value = g_strdup(entry_text);
514                 unfold_line(ch->value);
515                 g_strstrip(ch->value);
516                 gtk_entry_set_text(GTK_ENTRY(customhdr.val_entry), ch->value);
517         }
518
519         ch_str = g_strdup_printf("%s: %s", ch->name,
520                                  ch->value ? ch->value : "");
521         
522         prefs_custom_header_list_view_insert_header
523                 (customhdr.list_view, NULL, ch_str, ch);
524         
525         g_free(ch_str);
526
527         prefs_custom_header_set_list(cur_ac);
528
529 }
530
531 static void prefs_custom_header_add_cb(void)
532 {
533         prefs_custom_header_list_view_set_row(cur_ac);
534 }
535
536 static void prefs_custom_header_delete_cb(void)
537 {
538         GtkTreeIter sel;
539         GtkTreeModel *model;
540         CustomHeader *ch;
541
542         if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection
543                                 (GTK_TREE_VIEW(customhdr.list_view)),
544                                 &model, &sel))
545                 return; 
546
547         if (alertpanel(_("Delete header"),
548                        _("Do you really want to delete this header?"),
549                        GTK_STOCK_YES, GTK_STOCK_NO, NULL) != G_ALERTDEFAULT)
550                 return;
551
552         gtk_tree_model_get(model, &sel,
553                            CUSTHDR_DATA, &ch,
554                            -1);
555         gtk_list_store_remove(GTK_LIST_STORE(model), &sel);
556
557         cur_ac->customhdr_list = g_slist_remove(cur_ac->customhdr_list, ch);
558         
559         custom_header_free(ch);
560 }
561
562 static void prefs_custom_header_up(void)
563 {
564         GtkTreePath *prev, *sel;
565         GtkTreeIter isel;
566         GtkListStore *store;
567         GtkTreeIter iprev;
568         
569         if (!gtk_tree_selection_get_selected
570                 (gtk_tree_view_get_selection
571                         (GTK_TREE_VIEW(customhdr.list_view)),
572                  (GtkTreeModel **) &store,      
573                  &isel))
574                 return;
575
576         sel = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &isel);
577         if (!sel)
578                 return;
579         
580         /* no move if we're at row 0... */
581         prev = gtk_tree_path_copy(sel);
582         if (!gtk_tree_path_prev(prev)) {
583                 gtk_tree_path_free(prev);
584                 gtk_tree_path_free(sel);
585                 return;
586         }
587
588         gtk_tree_model_get_iter(GTK_TREE_MODEL(store),
589                                 &iprev, prev);
590         gtk_tree_path_free(sel);
591         gtk_tree_path_free(prev);
592
593         gtk_list_store_swap(store, &iprev, &isel);
594         prefs_custom_header_set_list(cur_ac);
595 }
596
597 static void prefs_custom_header_down(void)
598 {
599         GtkListStore *store;
600         GtkTreeIter next, sel;
601         
602         if (!gtk_tree_selection_get_selected
603                 (gtk_tree_view_get_selection
604                         (GTK_TREE_VIEW(customhdr.list_view)),
605                  (GtkTreeModel **) &store,
606                  &sel))
607                 return;
608
609         next = sel;
610         if (!gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &next)) 
611                 return;
612
613         gtk_list_store_swap(store, &next, &sel);
614         prefs_custom_header_set_list(cur_ac);
615 }
616
617 static gboolean prefs_custom_header_key_pressed(GtkWidget *widget,
618                                                 GdkEventKey *event,
619                                                 gpointer data)
620 {
621         if (event && event->keyval == GDK_Escape)
622                 prefs_custom_header_cancel();
623         return FALSE;
624 }
625
626 static void prefs_custom_header_ok(void)
627 {
628         prefs_custom_header_write_config(cur_ac);
629         gtk_widget_hide(customhdr.window);
630 }
631
632 static void prefs_custom_header_cancel(void)
633 {
634         prefs_custom_header_read_config(cur_ac); 
635         gtk_widget_hide(customhdr.window);
636 }
637
638 static gint prefs_custom_header_deleted(GtkWidget *widget, GdkEventAny *event,
639                                         gpointer data)
640 {
641         prefs_custom_header_cancel();
642         return TRUE;
643 }
644
645 static GtkListStore* prefs_custom_header_create_data_store(void)
646 {
647         return gtk_list_store_new(N_CUSTHDR_COLUMNS,
648                                   G_TYPE_STRING,        
649                                   G_TYPE_POINTER,
650                                   -1);
651 }
652
653 static void prefs_custom_header_list_view_insert_header(GtkWidget *list_view,
654                                                         GtkTreeIter *row_iter,
655                                                         gchar *header,
656                                                         gpointer data)
657 {
658         GtkTreeIter iter;
659         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
660                                         (GTK_TREE_VIEW(list_view)));
661
662         if (row_iter == NULL) {
663                 /* append new */
664                 gtk_list_store_append(list_store, &iter);
665                 gtk_list_store_set(list_store, &iter,
666                                    CUSTHDR_STRING, header,
667                                    CUSTHDR_DATA,   data,
668                                    -1);
669         } else {
670                 /* change existing */
671                 CustomHeader *old_data;
672
673                 gtk_tree_model_get(GTK_TREE_MODEL(list_store), row_iter,
674                                    CUSTHDR_DATA, &old_data,
675                                    -1);
676
677                 custom_header_free(old_data);
678                 
679                 gtk_list_store_set(list_store, row_iter,
680                                    CUSTHDR_STRING, header,
681                                    CUSTHDR_DATA, data,
682                                    -1);
683         }
684 }
685
686 static GtkWidget *prefs_custom_header_list_view_create(void)
687 {
688         GtkTreeView *list_view;
689         GtkTreeSelection *selector;
690         GtkTreeModel *model;
691
692         model = GTK_TREE_MODEL(prefs_custom_header_create_data_store());
693         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
694         g_object_unref(model);  
695         
696         gtk_tree_view_set_rules_hint(list_view, TRUE);
697         
698         selector = gtk_tree_view_get_selection(list_view);
699         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
700         gtk_tree_selection_set_select_function(selector, prefs_custom_header_selected,
701                                                NULL, NULL);
702
703         /* create the columns */
704         prefs_custom_header_create_list_view_columns(GTK_WIDGET(list_view));
705
706         return GTK_WIDGET(list_view);
707 }
708
709 static void prefs_custom_header_create_list_view_columns(GtkWidget *list_view)
710 {
711         GtkTreeViewColumn *column;
712         GtkCellRenderer *renderer;
713
714         renderer = gtk_cell_renderer_text_new();
715         column = gtk_tree_view_column_new_with_attributes
716                 (_("Current custom headers"),
717                  renderer,
718                  "text", CUSTHDR_STRING,
719                  NULL);
720         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
721 }
722
723 #define ENTRY_SET_TEXT(entry, str) \
724         gtk_entry_set_text(GTK_ENTRY(entry), str ? str : "")
725
726 static gboolean prefs_custom_header_selected(GtkTreeSelection *selector,
727                                              GtkTreeModel *model, 
728                                              GtkTreePath *path,
729                                              gboolean currently_selected,
730                                              gpointer data)
731 {
732         GtkTreeIter iter;
733         CustomHeader *ch;
734         CustomHeader default_ch = { 0, "", NULL };
735
736         if (currently_selected)
737                 return TRUE;
738
739         if (!gtk_tree_model_get_iter(model, &iter, path))
740                 return TRUE;
741
742         gtk_tree_model_get(model, &iter, 
743                            CUSTHDR_DATA, &ch,
744                            -1);
745         
746         if (!ch) ch = &default_ch;
747
748         ENTRY_SET_TEXT(customhdr.hdr_entry, ch->name);
749         ENTRY_SET_TEXT(customhdr.val_entry, ch->value);
750                            
751         return TRUE;
752 }
753
754 #undef ENTRY_SET_TEXT