dc4d7138d1f21a0bddd1fd1157178bd41071869b
[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 <glib/gi18n.h>
28 #include <gtk/gtk.h>
29 #include <gdk/gdkkeysyms.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <errno.h>
34
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         debug_print("Creating custom header setting window...\n");
158
159         window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
160         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
161         gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
162         gtk_window_set_modal (GTK_WINDOW (window), TRUE);
163         gtk_window_set_resizable(GTK_WINDOW (window), TRUE);
164
165         vbox = gtk_vbox_new (FALSE, 6);
166         gtk_widget_show (vbox);
167         gtk_container_add (GTK_CONTAINER (window), vbox);
168
169         gtkut_stock_button_set_create(&confirm_area, &ok_btn, GTK_STOCK_OK,
170                                       &cancel_btn, GTK_STOCK_CANCEL,
171                                       NULL, NULL);
172         gtk_widget_show (confirm_area);
173         gtk_box_pack_end (GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
174         gtk_widget_grab_default (ok_btn);
175
176         gtk_window_set_title (GTK_WINDOW(window), _("Custom header configuration"));
177         MANAGE_WINDOW_SIGNALS_CONNECT (window);
178         g_signal_connect (G_OBJECT(window), "delete_event",
179                           G_CALLBACK(prefs_custom_header_deleted),
180                           NULL);
181         g_signal_connect (G_OBJECT(window), "key_press_event",
182                           G_CALLBACK(prefs_custom_header_key_pressed),
183                           NULL);
184         g_signal_connect (G_OBJECT(ok_btn), "clicked",
185                           G_CALLBACK(prefs_custom_header_ok), NULL);
186         g_signal_connect (G_OBJECT(cancel_btn), "clicked",
187                           G_CALLBACK(prefs_custom_header_cancel), NULL);
188
189         vbox1 = gtk_vbox_new (FALSE, VSPACING);
190         gtk_widget_show (vbox1);
191         gtk_box_pack_start (GTK_BOX (vbox), vbox1, TRUE, TRUE, 0);
192         gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
193
194         table1 = gtk_table_new (2, 2, FALSE);
195         gtk_widget_show (table1);
196         gtk_box_pack_start (GTK_BOX (vbox1), table1,
197                             FALSE, FALSE, 0);
198         gtk_table_set_row_spacings (GTK_TABLE (table1), 8);
199         gtk_table_set_col_spacings (GTK_TABLE (table1), 8);
200
201         hdr_label = gtk_label_new (_("Header"));
202         gtk_widget_show (hdr_label);
203         gtk_table_attach (GTK_TABLE (table1), hdr_label, 0, 1, 0, 1,
204                           GTK_EXPAND | GTK_SHRINK | GTK_FILL,
205                           0, 0, 0);
206         gtk_misc_set_alignment (GTK_MISC (hdr_label), 0, 0.5);
207         
208         hdr_combo = gtk_combo_new ();
209         gtk_widget_show (hdr_combo);
210         gtk_table_attach (GTK_TABLE (table1), hdr_combo, 0, 1, 1, 2,
211                           GTK_EXPAND | GTK_SHRINK | GTK_FILL,
212                           0, 0, 0);
213         gtk_widget_set_size_request (hdr_combo, 150, -1);
214         gtkut_combo_set_items (GTK_COMBO (hdr_combo),
215                                "User-Agent", "X-Face", "X-Operating-System",
216                                NULL);
217
218         val_label = gtk_label_new (_("Value"));
219         gtk_widget_show (val_label);
220         gtk_table_attach (GTK_TABLE (table1), val_label, 1, 2, 0, 1,
221                           GTK_EXPAND | GTK_SHRINK | GTK_FILL,
222                           0, 0, 0);
223         gtk_misc_set_alignment (GTK_MISC (val_label), 0, 0.5);
224         
225         val_entry = gtk_entry_new ();
226         gtk_widget_show (val_entry);
227         gtk_table_attach (GTK_TABLE (table1), val_entry, 1, 2, 1, 2,
228                           GTK_EXPAND | GTK_SHRINK | GTK_FILL,
229                           0, 0, 0);
230         gtk_widget_set_size_request (val_entry, 200, -1);
231
232         /* add / delete */
233
234         reg_hbox = gtk_hbox_new (FALSE, 4);
235         gtk_widget_show (reg_hbox);
236         gtk_box_pack_start (GTK_BOX (vbox1), reg_hbox, FALSE, FALSE, 0);
237
238         arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_OUT);
239         gtk_widget_show (arrow);
240         gtk_box_pack_start (GTK_BOX (reg_hbox), arrow, FALSE, FALSE, 0);
241         gtk_widget_set_size_request (arrow, -1, 16);
242
243         btn_hbox = gtk_hbox_new (TRUE, 4);
244         gtk_widget_show (btn_hbox);
245         gtk_box_pack_start (GTK_BOX (reg_hbox), btn_hbox, FALSE, FALSE, 0);
246
247         add_btn = gtk_button_new_from_stock (GTK_STOCK_ADD);
248         gtk_widget_show (add_btn);
249         gtk_box_pack_start (GTK_BOX (btn_hbox), add_btn, FALSE, TRUE, 0);
250         g_signal_connect (G_OBJECT (add_btn), "clicked",
251                           G_CALLBACK (prefs_custom_header_add_cb),
252                           NULL);
253
254         del_btn = gtk_button_new_from_stock (GTK_STOCK_DELETE);
255         gtk_widget_show (del_btn);
256         gtk_box_pack_start (GTK_BOX (btn_hbox), del_btn, FALSE, TRUE, 0);
257         g_signal_connect (G_OBJECT (del_btn), "clicked",
258                           G_CALLBACK (prefs_custom_header_delete_cb),
259                           NULL);
260
261
262         ch_hbox = gtk_hbox_new (FALSE, 8);
263         gtk_widget_show (ch_hbox);
264         gtk_box_pack_start (GTK_BOX (vbox1), ch_hbox, TRUE, TRUE, 0);
265
266         ch_scrolledwin = gtk_scrolled_window_new (NULL, NULL);
267         gtk_widget_set_size_request (ch_scrolledwin, -1, 200);
268         gtk_widget_show (ch_scrolledwin);
269         gtk_box_pack_start (GTK_BOX (ch_hbox), ch_scrolledwin, TRUE, TRUE, 0);
270         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (ch_scrolledwin),
271                                         GTK_POLICY_AUTOMATIC,
272                                         GTK_POLICY_AUTOMATIC);
273
274         list_view = prefs_custom_header_list_view_create();
275         gtk_widget_show (list_view);
276         gtk_container_add (GTK_CONTAINER (ch_scrolledwin), list_view);
277
278         btn_vbox = gtk_vbox_new (FALSE, 8);
279         gtk_widget_show (btn_vbox);
280         gtk_box_pack_start (GTK_BOX (ch_hbox), btn_vbox, FALSE, FALSE, 0);
281
282         up_btn = gtk_button_new_from_stock (GTK_STOCK_GO_UP);
283         gtk_widget_show (up_btn);
284         gtk_box_pack_start (GTK_BOX (btn_vbox), up_btn, FALSE, FALSE, 0);
285         g_signal_connect (G_OBJECT (up_btn), "clicked",
286                           G_CALLBACK (prefs_custom_header_up), NULL);
287
288         down_btn = gtk_button_new_from_stock (GTK_STOCK_GO_DOWN);
289         gtk_widget_show (down_btn);
290         gtk_box_pack_start (GTK_BOX (btn_vbox), down_btn, FALSE, FALSE, 0);
291         g_signal_connect (G_OBJECT (down_btn), "clicked",
292                           G_CALLBACK (prefs_custom_header_down), NULL);
293
294         gtk_widget_show_all(window);
295
296         customhdr.window     = window;
297         customhdr.ok_btn     = ok_btn;
298         customhdr.cancel_btn = cancel_btn;
299
300         customhdr.hdr_combo  = hdr_combo;
301         customhdr.hdr_entry  = GTK_COMBO (hdr_combo)->entry;
302         customhdr.val_entry  = val_entry;
303
304         customhdr.list_view   = list_view;
305 }
306
307 void prefs_custom_header_read_config(PrefsAccount *ac)
308 {
309         gchar *rcpath;
310         FILE *fp;
311         gchar buf[PREFSBUFSIZE];
312         CustomHeader *ch;
313
314         debug_print("Reading custom header configuration...\n");
315
316         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
317                              CUSTOM_HEADER_RC, NULL);
318         if ((fp = g_fopen(rcpath, "rb")) == NULL) {
319                 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
320                 g_free(rcpath);
321                 ac->customhdr_list = NULL;
322                 return;
323         }
324         g_free(rcpath);
325
326         /* remove all previous headers list */
327         while (ac->customhdr_list != NULL) {
328                 ch = (CustomHeader *)ac->customhdr_list->data;
329                 custom_header_free(ch);
330                 ac->customhdr_list = g_slist_remove(ac->customhdr_list, ch);
331         }
332
333         while (fgets(buf, sizeof(buf), fp) != NULL) {
334                 ch = custom_header_read_str(buf);
335                 if (ch) {
336                         if (ch->account_id == ac->account_id) {
337                                 ac->customhdr_list =
338                                         g_slist_append(ac->customhdr_list, ch);
339                         } else
340                                 custom_header_free(ch);
341                 }
342         }
343
344         fclose(fp);
345 }
346
347 void prefs_custom_header_write_config(PrefsAccount *ac)
348 {
349         gchar *rcpath;
350         PrefFile *pfile;
351         GSList *cur;
352         gchar buf[PREFSBUFSIZE];
353         FILE * fp;
354         CustomHeader *ch;
355
356         GSList *all_hdrs = NULL;
357
358         debug_print("Writing custom header configuration...\n");
359
360         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
361                              CUSTOM_HEADER_RC, NULL);
362
363         if ((fp = g_fopen(rcpath, "rb")) == NULL) {
364                 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
365         } else {
366                 all_hdrs = NULL;
367
368                 while (fgets(buf, sizeof(buf), fp) != NULL) {
369                         ch = custom_header_read_str(buf);
370                         if (ch) {
371                                 if (ch->account_id != ac->account_id)
372                                         all_hdrs =
373                                                 g_slist_append(all_hdrs, ch);
374                                 else
375                                         custom_header_free(ch);
376                         }
377                 }
378
379                 fclose(fp);
380         }
381
382         if ((pfile = prefs_write_open(rcpath)) == NULL) {
383                 g_warning("failed to write configuration to file\n");
384                 g_free(rcpath);
385                 return;
386         }
387
388         for (cur = all_hdrs; cur != NULL; cur = cur->next) {
389                 CustomHeader *hdr = (CustomHeader *)cur->data;
390                 gchar *chstr;
391
392                 chstr = custom_header_get_str(hdr);
393                 if (fputs(chstr, pfile->fp) == EOF ||
394                     fputc('\n', pfile->fp) == EOF) {
395                         FILE_OP_ERROR(rcpath, "fputs || fputc");
396                         prefs_file_close_revert(pfile);
397                         g_free(rcpath);
398                         g_free(chstr);
399                         return;
400                 }
401                 g_free(chstr);
402         }
403
404         for (cur = ac->customhdr_list; cur != NULL; cur = cur->next) {
405                 CustomHeader *hdr = (CustomHeader *)cur->data;
406                 gchar *chstr;
407
408                 chstr = custom_header_get_str(hdr);
409                 if (fputs(chstr, pfile->fp) == EOF ||
410                     fputc('\n', pfile->fp) == EOF) {
411                         FILE_OP_ERROR(rcpath, "fputs || fputc");
412                         prefs_file_close_revert(pfile);
413                         g_free(rcpath);
414                         g_free(chstr);
415                         return;
416                 }
417                 g_free(chstr);
418         }
419
420         g_free(rcpath);
421
422         while (all_hdrs != NULL) {
423                 ch = (CustomHeader *)all_hdrs->data;
424                 custom_header_free(ch);
425                 all_hdrs = g_slist_remove(all_hdrs, ch);
426         }
427
428         if (prefs_file_close(pfile) < 0) {
429                 g_warning("failed to write configuration to file\n");
430                 return;
431         }
432 }
433
434 static void prefs_custom_header_set_dialog(PrefsAccount *ac)
435 {
436         GtkListStore *store;
437         GSList *cur;
438         
439         store = GTK_LIST_STORE(gtk_tree_view_get_model
440                                 (GTK_TREE_VIEW(customhdr.list_view)));
441         gtk_list_store_clear(store);
442
443         for (cur = ac->customhdr_list; cur != NULL; cur = cur->next) {
444                 CustomHeader *ch = (CustomHeader *)cur->data;
445                 gchar *ch_str;
446
447                 ch_str = g_strdup_printf("%s: %s", ch->name,
448                                          ch->value ? ch->value : "");
449
450                 prefs_custom_header_list_view_insert_header
451                         (customhdr.list_view, NULL, ch_str, ch);                                                 
452
453                 g_free(ch_str);
454         }
455 }
456
457 static void prefs_custom_header_set_list(PrefsAccount *ac)
458 {
459         CustomHeader *ch;
460         GtkTreeIter iter;
461         GtkListStore *store;
462
463         g_slist_free(ac->customhdr_list);
464         ac->customhdr_list = NULL;
465
466         store = GTK_LIST_STORE(gtk_tree_view_get_model
467                                 (GTK_TREE_VIEW(customhdr.list_view)));
468
469         if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter)) {
470                 do {
471                         gtk_tree_model_get(GTK_TREE_MODEL(store), &iter,
472                                            CUSTHDR_DATA, &ch,
473                                            -1);
474                         ac->customhdr_list = g_slist_append(ac->customhdr_list, ch);
475                 } while (gtk_tree_model_iter_next(GTK_TREE_MODEL(store),
476                                                   &iter));
477         }
478 }
479
480 static void prefs_custom_header_list_view_set_row(PrefsAccount *ac)
481 {
482         CustomHeader *ch;
483         const gchar *entry_text;
484         gchar *ch_str;
485         GtkListStore *store;
486
487         store = GTK_LIST_STORE(gtk_tree_view_get_model
488                                 (GTK_TREE_VIEW(customhdr.list_view)));
489
490         entry_text = gtk_entry_get_text(GTK_ENTRY(customhdr.hdr_entry));
491         if (entry_text[0] == '\0') {
492                 alertpanel_error(_("Header name is not set."));
493                 return;
494         }
495         if (!custom_header_is_allowed(entry_text)) {
496                 alertpanel_error(_("This Header name is not allowed as a custom header."));
497                 return;
498         }
499
500         ch = g_new0(CustomHeader, 1);
501
502         ch->account_id = ac->account_id;
503
504         ch->name = g_strdup(entry_text);
505         unfold_line(ch->name);
506         g_strstrip(ch->name);
507         gtk_entry_set_text(GTK_ENTRY(customhdr.hdr_entry), ch->name);
508
509         entry_text = gtk_entry_get_text(GTK_ENTRY(customhdr.val_entry));
510         if (entry_text[0] != '\0') {
511                 ch->value = g_strdup(entry_text);
512                 unfold_line(ch->value);
513                 g_strstrip(ch->value);
514                 gtk_entry_set_text(GTK_ENTRY(customhdr.val_entry), ch->value);
515         }
516
517         ch_str = g_strdup_printf("%s: %s", ch->name,
518                                  ch->value ? ch->value : "");
519         
520         prefs_custom_header_list_view_insert_header
521                 (customhdr.list_view, NULL, ch_str, ch);
522         
523         g_free(ch_str);
524
525         prefs_custom_header_set_list(cur_ac);
526
527 }
528
529 static void prefs_custom_header_add_cb(void)
530 {
531         prefs_custom_header_list_view_set_row(cur_ac);
532 }
533
534 static void prefs_custom_header_delete_cb(void)
535 {
536         GtkTreeIter sel;
537         GtkTreeModel *model;
538         CustomHeader *ch;
539
540         if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection
541                                 (GTK_TREE_VIEW(customhdr.list_view)),
542                                 &model, &sel))
543                 return; 
544
545         if (alertpanel(_("Delete header"),
546                        _("Do you really want to delete this header?"),
547                        GTK_STOCK_YES, GTK_STOCK_NO, NULL) != G_ALERTDEFAULT)
548                 return;
549
550         gtk_tree_model_get(model, &sel,
551                            CUSTHDR_DATA, &ch,
552                            -1);
553         gtk_list_store_remove(GTK_LIST_STORE(model), &sel);
554
555         cur_ac->customhdr_list = g_slist_remove(cur_ac->customhdr_list, ch);
556         
557         custom_header_free(ch);
558 }
559
560 static void prefs_custom_header_up(void)
561 {
562         GtkTreePath *prev, *sel;
563         GtkTreeIter isel;
564         GtkListStore *store;
565         GtkTreeIter iprev;
566         
567         if (!gtk_tree_selection_get_selected
568                 (gtk_tree_view_get_selection
569                         (GTK_TREE_VIEW(customhdr.list_view)),
570                  (GtkTreeModel **) &store,
571                  &isel))
572                 return;
573
574         sel = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &isel);
575         if (!sel)
576                 return;
577         
578         /* no move if we're at row 0... */
579         prev = gtk_tree_path_copy(sel);
580         if (!gtk_tree_path_prev(prev)) {
581                 gtk_tree_path_free(prev);
582                 gtk_tree_path_free(sel);
583                 return;
584         }
585
586         gtk_tree_model_get_iter(GTK_TREE_MODEL(store),
587                                 &iprev, prev);
588         gtk_tree_path_free(sel);
589         gtk_tree_path_free(prev);
590
591         gtk_list_store_swap(store, &iprev, &isel);
592         prefs_custom_header_set_list(cur_ac);
593 }
594
595 static void prefs_custom_header_down(void)
596 {
597         GtkListStore *store;
598         GtkTreeIter next, sel;
599         
600         if (!gtk_tree_selection_get_selected
601                 (gtk_tree_view_get_selection
602                         (GTK_TREE_VIEW(customhdr.list_view)),
603                  (GtkTreeModel **) &store,
604                  &sel))
605                 return;
606
607         next = sel;
608         if (!gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &next)) 
609                 return;
610
611         gtk_list_store_swap(store, &next, &sel);
612         prefs_custom_header_set_list(cur_ac);
613 }
614
615 static gboolean prefs_custom_header_key_pressed(GtkWidget *widget,
616                                                 GdkEventKey *event,
617                                                 gpointer data)
618 {
619         if (event && event->keyval == GDK_Escape)
620                 prefs_custom_header_cancel();
621         return FALSE;
622 }
623
624 static void prefs_custom_header_ok(void)
625 {
626         prefs_custom_header_write_config(cur_ac);
627         gtk_widget_hide(customhdr.window);
628 }
629
630 static void prefs_custom_header_cancel(void)
631 {
632         prefs_custom_header_read_config(cur_ac); 
633         gtk_widget_hide(customhdr.window);
634 }
635
636 static gint prefs_custom_header_deleted(GtkWidget *widget, GdkEventAny *event,
637                                         gpointer data)
638 {
639         prefs_custom_header_cancel();
640         return TRUE;
641 }
642
643 static GtkListStore* prefs_custom_header_create_data_store(void)
644 {
645         return gtk_list_store_new(N_CUSTHDR_COLUMNS,
646                                   G_TYPE_STRING,        
647                                   G_TYPE_POINTER,
648                                   -1);
649 }
650
651 static void prefs_custom_header_list_view_insert_header(GtkWidget *list_view,
652                                                         GtkTreeIter *row_iter,
653                                                         gchar *header,
654                                                         gpointer data)
655 {
656         GtkTreeIter iter;
657         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
658                                         (GTK_TREE_VIEW(list_view)));
659
660         if (row_iter == NULL) {
661                 /* append new */
662                 gtk_list_store_append(list_store, &iter);
663                 gtk_list_store_set(list_store, &iter,
664                                    CUSTHDR_STRING, header,
665                                    CUSTHDR_DATA,   data,
666                                    -1);
667         } else {
668                 /* change existing */
669                 CustomHeader *old_data;
670
671                 gtk_tree_model_get(GTK_TREE_MODEL(list_store), row_iter,
672                                    CUSTHDR_DATA, &old_data,
673                                    -1);
674
675                 custom_header_free(old_data);
676                 
677                 gtk_list_store_set(list_store, row_iter,
678                                    CUSTHDR_STRING, header,
679                                    CUSTHDR_DATA, data,
680                                    -1);
681         }
682 }
683
684 static GtkWidget *prefs_custom_header_list_view_create(void)
685 {
686         GtkTreeView *list_view;
687         GtkTreeSelection *selector;
688         GtkTreeModel *model;
689
690         model = GTK_TREE_MODEL(prefs_custom_header_create_data_store());
691         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
692         g_object_unref(model);  
693         
694         gtk_tree_view_set_rules_hint(list_view, prefs_common.enable_rules_hint);
695         
696         selector = gtk_tree_view_get_selection(list_view);
697         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
698         gtk_tree_selection_set_select_function(selector, prefs_custom_header_selected,
699                                                NULL, NULL);
700
701         /* create the columns */
702         prefs_custom_header_create_list_view_columns(GTK_WIDGET(list_view));
703
704         return GTK_WIDGET(list_view);
705 }
706
707 static void prefs_custom_header_create_list_view_columns(GtkWidget *list_view)
708 {
709         GtkTreeViewColumn *column;
710         GtkCellRenderer *renderer;
711
712         renderer = gtk_cell_renderer_text_new();
713         column = gtk_tree_view_column_new_with_attributes
714                 (_("Current custom headers"),
715                  renderer,
716                  "text", CUSTHDR_STRING,
717                  NULL);
718         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
719 }
720
721 #define ENTRY_SET_TEXT(entry, str) \
722         gtk_entry_set_text(GTK_ENTRY(entry), str ? str : "")
723
724 static gboolean prefs_custom_header_selected(GtkTreeSelection *selector,
725                                              GtkTreeModel *model, 
726                                              GtkTreePath *path,
727                                              gboolean currently_selected,
728                                              gpointer data)
729 {
730         GtkTreeIter iter;
731         CustomHeader *ch;
732         CustomHeader default_ch = { 0, "", NULL };
733
734         if (currently_selected)
735                 return TRUE;
736
737         if (!gtk_tree_model_get_iter(model, &iter, path))
738                 return TRUE;
739
740         gtk_tree_model_get(model, &iter, 
741                            CUSTHDR_DATA, &ch,
742                            -1);
743         
744         if (!ch) ch = &default_ch;
745
746         ENTRY_SET_TEXT(customhdr.hdr_entry, ch->name);
747         ENTRY_SET_TEXT(customhdr.val_entry, ch->value);
748                            
749         return TRUE;
750 }
751
752 #undef ENTRY_SET_TEXT