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