2005-09-14 [paul] 1.9.14cvs26
[claws.git] / src / prefs_display_header.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 "prefs_gtk.h"
36 #include "prefs_display_header.h"
37 #include "prefs_common.h"
38 #include "manage_window.h"
39 #include "alertpanel.h"
40 #include "displayheader.h"
41 #include "utils.h"
42 #include "gtkutils.h"
43
44 enum {
45         PREFS_HDR_HEADER,
46         PREFS_HDR_DATA,
47         N_PREFS_HDR_COLUMNS
48 };
49
50 static struct DisplayHeader {
51         GtkWidget *window;
52
53         GtkWidget *ok_btn;
54         GtkWidget *cancel_btn;
55
56         GtkWidget *hdr_combo;
57         GtkWidget *hdr_entry;
58         GtkWidget *key_check;
59         GtkWidget *headers_list_view;
60         GtkWidget *hidden_headers_list_view;
61
62         GtkWidget *other_headers;
63 } dispheader;
64
65 /* widget creating functions */
66 static void prefs_display_header_create (void);
67
68 static void prefs_display_header_set_dialog     (void);
69 static void prefs_display_header_set_list       (void);
70 static void prefs_display_header_list_view_set_row      (gboolean hidden);
71
72 /* callback functions */
73 static void prefs_display_header_register_cb    (GtkButton      *btn,
74                                                  gpointer        hidden_data);
75 static void prefs_display_header_delete_cb      (GtkButton      *btn,
76                                                  gpointer        list_view_data);
77 static void prefs_display_header_up             (void);
78 static void prefs_display_header_down           (void);
79
80 static gboolean prefs_display_header_key_pressed        (GtkWidget      *widget,
81                                                          GdkEventKey    *event,
82                                                          gpointer        data);
83 static void prefs_display_header_ok             (void);
84 static void prefs_display_header_cancel         (void);
85 static gint prefs_display_header_deleted        (GtkWidget      *widget,
86                                                  GdkEventAny    *event,
87                                                  gpointer        data);
88
89
90 static GtkListStore *prefs_display_header_create_store  (void);
91 static void prefs_display_header_insert_header          (GtkListStore *store,
92                                                          gchar *name,
93                                                          DisplayHeaderProp *dp);
94 static GtkWidget *prefs_display_header_list_view_create (const gchar *name);
95 static void prefs_filtering_create_list_view_columns    (GtkWidget *list_view, 
96                                                          const gchar *name);
97 static void headers_list_model_row_changed              (GtkTreeModel *model, 
98                                                          GtkTreePath *path, 
99                                                          GtkTreeIter *iter, 
100                                                          GtkTreeView *list_view);
101 static void headers_list_model_rows_reordered           (GtkTreeModel *model,
102                                                          GtkTreePath  *path, 
103                                                          GtkTreeIter  *iter,
104                                                          gpointer      arg,
105                                                          GtkTreeView  *list_view);
106                                                          
107 static void drag_begin  (GtkTreeView *list_view,
108                          GdkDragContext *context,
109                          gpointer data);
110
111 static void drag_end    (GtkTreeView *list_view,
112                          GdkDragContext *context,
113                          gpointer data);
114
115 static gchar *defaults[] =
116 {
117         "From",
118         "To",
119         "Cc",
120         "Reply-To",
121         "Newsgroups",
122         "Followup-To",
123         "Subject",
124         "Date",
125         "Sender",
126         "Organization",
127         "X-Mailer",
128         "X-Newsreader",
129         "User-Agent",
130         "-Received",
131         "-Message-ID",
132         "-In-Reply-To",
133         "-References",
134         "-Mime-Version",
135         "-Content-Type",
136         "-Content-Transfer-Encoding",
137         "-X-UIDL",
138         "-Precedence",
139         "-Status",
140         "-Priority",
141         "-X-Face"
142 };
143
144 static void prefs_display_header_set_default(void)
145 {
146         gint i;
147         DisplayHeaderProp *dp;
148
149         for(i = 0; i < sizeof(defaults) / sizeof(defaults[0]); i++) {
150                 dp = display_header_prop_read_str(defaults[i]);
151                 prefs_common.disphdr_list =
152                         g_slist_append(prefs_common.disphdr_list, dp);
153         }
154 }
155
156 void prefs_display_header_open(void)
157 {
158         if (prefs_rc_is_readonly(DISPLAY_HEADER_RC))
159                 return;
160
161         if (!dispheader.window) {
162                 prefs_display_header_create();
163         }
164
165         manage_window_set_transient(GTK_WINDOW(dispheader.window));
166         gtk_widget_grab_focus(dispheader.ok_btn);
167
168         prefs_display_header_set_dialog();
169
170         gtk_widget_show(dispheader.window);
171 }
172
173 static void prefs_display_header_create(void)
174 {
175         GtkWidget *window;
176         GtkWidget *vbox;
177         GtkWidget *btn_hbox;
178         GtkWidget *ok_btn;
179         GtkWidget *cancel_btn;
180         GtkWidget *confirm_area;
181
182         GtkWidget *vbox1;
183
184         GtkWidget *hbox1;
185         GtkWidget *hdr_label;
186         GtkWidget *hdr_combo;
187
188         GtkWidget *btn_vbox;
189         GtkWidget *reg_btn;
190         GtkWidget *del_btn;
191         GtkWidget *up_btn;
192         GtkWidget *down_btn;
193
194         GtkWidget *list_view_hbox;
195         GtkWidget *list_view_hbox1;
196         GtkWidget *list_view_hbox2;
197         GtkWidget *list_view_scrolledwin;
198         GtkWidget *headers_list_view;
199         GtkWidget *hidden_headers_list_view;
200
201         GtkWidget *checkbtn_other_headers;
202
203         debug_print("Creating display header setting window...\n");
204
205         window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
206         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
207         gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
208         gtk_window_set_modal (GTK_WINDOW (window), TRUE);
209         gtk_window_set_resizable(GTK_WINDOW (window), TRUE);
210
211         vbox = gtk_vbox_new (FALSE, 6);
212         gtk_widget_show (vbox);
213         gtk_container_add (GTK_CONTAINER (window), vbox);
214
215         btn_hbox = gtk_hbox_new (FALSE, 8);
216         gtk_widget_show (btn_hbox);
217         gtk_box_pack_end (GTK_BOX (vbox), btn_hbox, FALSE, FALSE, 0);
218
219         gtkut_stock_button_set_create(&confirm_area, &ok_btn, GTK_STOCK_OK,
220                                       &cancel_btn, GTK_STOCK_CANCEL,
221                                       NULL, NULL);
222         gtk_widget_show (confirm_area);
223         gtk_box_pack_end (GTK_BOX(btn_hbox), confirm_area, FALSE, FALSE, 0);
224         gtk_widget_grab_default (ok_btn);
225
226         gtk_window_set_title (GTK_WINDOW(window),
227                               _("Displayed header configuration"));
228         MANAGE_WINDOW_SIGNALS_CONNECT(window);
229         g_signal_connect (G_OBJECT(window), "delete_event",
230                           G_CALLBACK(prefs_display_header_deleted),
231                           NULL);
232         g_signal_connect (G_OBJECT(window), "key_press_event",
233                           G_CALLBACK(prefs_display_header_key_pressed),
234                           NULL);
235         g_signal_connect (G_OBJECT(ok_btn), "clicked",
236                           G_CALLBACK(prefs_display_header_ok),
237                           NULL);
238         g_signal_connect (G_OBJECT(cancel_btn), "clicked",
239                           G_CALLBACK(prefs_display_header_cancel),
240                           NULL);
241
242         vbox1 = gtk_vbox_new (FALSE, VSPACING);
243         gtk_widget_show (vbox1);
244         gtk_box_pack_start (GTK_BOX (vbox), vbox1, TRUE, TRUE, 0);
245         gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
246
247         hbox1 = gtk_hbox_new (FALSE, 8);
248         gtk_widget_show (hbox1);
249         gtk_box_pack_start (GTK_BOX (vbox1), hbox1, FALSE, TRUE, 0);
250
251         hdr_label = gtk_label_new (_("Header name"));
252         gtk_widget_show (hdr_label);
253         gtk_box_pack_start (GTK_BOX (hbox1), hdr_label, FALSE, FALSE, 0);
254
255         hdr_combo = gtk_combo_new ();
256         gtk_widget_show (hdr_combo);
257         gtk_box_pack_start (GTK_BOX (hbox1), hdr_combo, TRUE, TRUE, 0);
258         gtk_widget_set_size_request (hdr_combo, 150, -1);
259         gtkut_combo_set_items (GTK_COMBO (hdr_combo),
260                                "From", "To", "Cc", "Subject", "Date",
261                                "Reply-To", "Sender", "User-Agent", "X-Mailer",
262                                NULL);
263
264         list_view_hbox = gtk_hbox_new (FALSE, 10);
265         gtk_widget_show (list_view_hbox);
266         gtk_box_pack_start (GTK_BOX (vbox1), list_view_hbox, TRUE, TRUE, 0);
267
268         /* display headers list */
269
270         list_view_hbox1 = gtk_hbox_new (FALSE, 8);
271         gtk_widget_show (list_view_hbox1);
272         gtk_box_pack_start (GTK_BOX (list_view_hbox), list_view_hbox1, TRUE, TRUE, 0);
273
274         list_view_scrolledwin = gtk_scrolled_window_new (NULL, NULL);
275         gtk_widget_set_size_request (list_view_scrolledwin, 200, 210);
276         gtk_widget_show (list_view_scrolledwin);
277         gtk_box_pack_start (GTK_BOX (list_view_hbox1), list_view_scrolledwin,
278                             TRUE, TRUE, 0);
279         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (list_view_scrolledwin),
280                                         GTK_POLICY_AUTOMATIC,
281                                         GTK_POLICY_AUTOMATIC);
282
283         headers_list_view = prefs_display_header_list_view_create
284                                 (_("Displayed Headers"));
285         gtk_widget_show (headers_list_view);
286         gtk_container_add(GTK_CONTAINER(list_view_scrolledwin), headers_list_view);
287         gtk_tree_view_set_reorderable(GTK_TREE_VIEW(headers_list_view), TRUE);
288
289         g_signal_connect(G_OBJECT(headers_list_view), "drag_begin",                      
290                          G_CALLBACK(drag_begin),
291                          headers_list_view);
292                          
293         g_signal_connect(G_OBJECT(headers_list_view), "drag_end",                        
294                          G_CALLBACK(drag_end),
295                          headers_list_view);
296         
297         /* connect rows change for this list view's model */
298         g_signal_connect(G_OBJECT(gtk_tree_view_get_model(GTK_TREE_VIEW(headers_list_view))),
299                          "rows-reordered", 
300                          G_CALLBACK(headers_list_model_rows_reordered),
301                          headers_list_view);
302
303         btn_vbox = gtk_vbox_new (FALSE, 8);
304         gtk_widget_show (btn_vbox);
305         gtk_box_pack_start (GTK_BOX (list_view_hbox1), btn_vbox, FALSE, FALSE, 0);
306
307         reg_btn = gtk_button_new_from_stock (GTK_STOCK_ADD);
308         gtk_widget_show (reg_btn);
309         gtk_box_pack_start (GTK_BOX (btn_vbox), reg_btn, FALSE, TRUE, 0);
310         g_signal_connect (G_OBJECT (reg_btn), "clicked",
311                           G_CALLBACK (prefs_display_header_register_cb),
312                             GINT_TO_POINTER(FALSE));
313         del_btn = gtk_button_new_from_stock (GTK_STOCK_REMOVE);
314         gtk_widget_show (del_btn);
315         gtk_box_pack_start (GTK_BOX (btn_vbox), del_btn, FALSE, TRUE, 0);
316         g_signal_connect (G_OBJECT (del_btn), "clicked",
317                           G_CALLBACK (prefs_display_header_delete_cb),
318                           headers_list_view);
319
320         up_btn = gtk_button_new_from_stock (GTK_STOCK_GO_UP);
321         gtk_widget_show (up_btn);
322         gtk_box_pack_start (GTK_BOX (btn_vbox), up_btn, FALSE, FALSE, 0);
323         g_signal_connect (G_OBJECT (up_btn), "clicked",
324                           G_CALLBACK (prefs_display_header_up), NULL);
325
326         down_btn = gtk_button_new_from_stock (GTK_STOCK_GO_DOWN);
327         gtk_widget_show (down_btn);
328         gtk_box_pack_start (GTK_BOX (btn_vbox), down_btn, FALSE, FALSE, 0);
329         g_signal_connect (G_OBJECT (down_btn), "clicked",
330                           G_CALLBACK (prefs_display_header_down), NULL);
331
332         /* hidden headers list */
333
334         list_view_hbox2 = gtk_hbox_new (FALSE, 8);
335         gtk_widget_show (list_view_hbox2);
336         gtk_box_pack_start (GTK_BOX (list_view_hbox), list_view_hbox2, TRUE, TRUE, 0);
337
338         list_view_scrolledwin = gtk_scrolled_window_new (NULL, NULL);
339         gtk_widget_set_size_request (list_view_scrolledwin, 200, 210);
340         gtk_widget_show (list_view_scrolledwin);
341         gtk_box_pack_start (GTK_BOX (list_view_hbox2), list_view_scrolledwin,
342                             TRUE, TRUE, 0);
343         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (list_view_scrolledwin),
344                                         GTK_POLICY_AUTOMATIC,
345                                         GTK_POLICY_AUTOMATIC);
346
347         hidden_headers_list_view = prefs_display_header_list_view_create
348                                         (_("Hidden headers"));
349         gtk_widget_show (hidden_headers_list_view);
350         gtk_container_add (GTK_CONTAINER (list_view_scrolledwin),
351                            hidden_headers_list_view);
352
353         btn_vbox = gtk_vbox_new (FALSE, 8);
354         gtk_widget_show (btn_vbox);
355         gtk_box_pack_start (GTK_BOX (list_view_hbox2), btn_vbox, FALSE, FALSE, 0);
356
357         reg_btn = gtk_button_new_from_stock (GTK_STOCK_ADD);
358         gtk_widget_show (reg_btn);
359         gtk_box_pack_start (GTK_BOX (btn_vbox), reg_btn, FALSE, TRUE, 0);
360         g_signal_connect (G_OBJECT (reg_btn), "clicked",
361                             G_CALLBACK
362                             (prefs_display_header_register_cb),
363                             GINT_TO_POINTER(TRUE));
364         del_btn = gtk_button_new_from_stock (GTK_STOCK_DELETE);
365         gtk_widget_show (del_btn);
366         gtk_box_pack_start (GTK_BOX (btn_vbox), del_btn, FALSE, TRUE, 0);
367         g_signal_connect (G_OBJECT       (del_btn), "clicked",
368                             G_CALLBACK (prefs_display_header_delete_cb),
369                             hidden_headers_list_view);
370
371         PACK_CHECK_BUTTON (btn_hbox, checkbtn_other_headers,
372                            _("Show all unspecified headers"));
373         SET_TOGGLE_SENSITIVITY (checkbtn_other_headers, list_view_hbox2);
374
375         gtk_widget_show_all(window);
376
377         dispheader.window        = window;
378         dispheader.ok_btn        = ok_btn;
379         dispheader.cancel_btn    = cancel_btn;
380
381         dispheader.hdr_combo     = hdr_combo;
382         dispheader.hdr_entry     = GTK_COMBO (hdr_combo)->entry;
383
384         dispheader.headers_list_view        = headers_list_view;
385         dispheader.hidden_headers_list_view = hidden_headers_list_view;
386
387         dispheader.other_headers = checkbtn_other_headers;
388 }
389
390 void prefs_display_header_read_config(void)
391 {
392         gchar *rcpath;
393         FILE *fp;
394         gchar buf[PREFSBUFSIZE];
395         DisplayHeaderProp *dp;
396
397         debug_print("Reading configuration for displaying headers...\n");
398
399         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
400                              DISPLAY_HEADER_RC, NULL);
401         if ((fp = g_fopen(rcpath, "rb")) == NULL) {
402                 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
403                 g_free(rcpath);
404                 prefs_common.disphdr_list = NULL;
405                 prefs_display_header_set_default();
406                 return;
407         }
408         g_free(rcpath);
409
410         /* remove all previous headers list */
411         while (prefs_common.disphdr_list != NULL) {
412                 dp = (DisplayHeaderProp *)prefs_common.disphdr_list->data;
413                 display_header_prop_free(dp);
414                 prefs_common.disphdr_list =
415                         g_slist_remove(prefs_common.disphdr_list, dp);
416         }
417
418         while (fgets(buf, sizeof(buf), fp) != NULL) {
419                 g_strdelimit(buf, "\r\n", '\0');
420                 dp = display_header_prop_read_str(buf);
421                 if (dp)
422                         prefs_common.disphdr_list =
423                                 g_slist_append(prefs_common.disphdr_list, dp);
424         }
425
426         fclose(fp);
427 }
428
429 void prefs_display_header_write_config(void)
430 {
431         gchar *rcpath;
432         PrefFile *pfile;
433         GSList *cur;
434
435         debug_print("Writing configuration for displaying headers...\n");
436
437         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
438                              DISPLAY_HEADER_RC, NULL);
439
440         if ((pfile = prefs_write_open(rcpath)) == NULL) {
441                 g_warning("failed to write configuration to file\n");
442                 g_free(rcpath);
443                 return;
444         }
445
446         for (cur = prefs_common.disphdr_list; cur != NULL;
447              cur = cur->next) {
448                 DisplayHeaderProp *dp = (DisplayHeaderProp *)cur->data;
449                 gchar *dpstr;
450
451                 dpstr = display_header_prop_get_str(dp);
452                 if (fputs(dpstr, pfile->fp) == EOF ||
453                     fputc('\n', pfile->fp) == EOF) {
454                         FILE_OP_ERROR(rcpath, "fputs || fputc");
455                         prefs_file_close_revert(pfile);
456                         g_free(rcpath);
457                         g_free(dpstr);
458                         return;
459                 }
460                 g_free(dpstr);
461         }
462
463         g_free(rcpath);
464
465         if (prefs_file_close(pfile) < 0) {
466                 g_warning("failed to write configuration to file\n");
467                 return;
468         }
469 }
470
471 static void prefs_display_header_set_dialog(void)
472 {
473         GtkTreeView *list_view = GTK_TREE_VIEW(dispheader.headers_list_view);
474         GtkTreeView *hidden_list_view = GTK_TREE_VIEW(dispheader.hidden_headers_list_view);
475         GSList *cur;
476         GtkTreeModel *model_list, *model_hidden;
477
478         model_list = gtk_tree_view_get_model(list_view);
479         model_hidden = gtk_tree_view_get_model(hidden_list_view);
480
481         gtk_list_store_clear(GTK_LIST_STORE(model_list));
482         gtk_list_store_clear(GTK_LIST_STORE(model_hidden));
483
484         for (cur = prefs_common.disphdr_list; cur != NULL;
485              cur = cur->next) {
486                 DisplayHeaderProp *dp = (DisplayHeaderProp *)cur->data;
487
488                 if (dp->hidden)
489                         prefs_display_header_insert_header(GTK_LIST_STORE
490                                                 (model_hidden), dp->name, dp);  
491                 else
492                         prefs_display_header_insert_header(GTK_LIST_STORE
493                                                 (model_list), dp->name, dp);
494         }
495
496         gtk_toggle_button_set_active
497                 (GTK_TOGGLE_BUTTON(dispheader.other_headers),
498                  prefs_common.show_other_header);
499 }
500
501 static void prefs_display_header_set_list(void)
502 {
503         gint row = 0;
504         DisplayHeaderProp *dp;
505         GtkTreeModel *model;
506         GtkTreeIter iter;
507
508         g_slist_free(prefs_common.disphdr_list);
509         prefs_common.disphdr_list = NULL;
510
511         model = gtk_tree_view_get_model(GTK_TREE_VIEW(dispheader.headers_list_view));
512         while (gtk_tree_model_iter_nth_child(model, &iter, NULL, row)) {
513                 gtk_tree_model_get(model, &iter, PREFS_HDR_DATA, &dp, -1);
514                 if (dp)
515                         prefs_common.disphdr_list =
516                                 g_slist_append(prefs_common.disphdr_list, dp);
517                 row++;                          
518         }
519
520         model = gtk_tree_view_get_model
521                         (GTK_TREE_VIEW(dispheader.hidden_headers_list_view));
522         row = 0;
523         while (gtk_tree_model_iter_nth_child(model, &iter, NULL, row)) {
524                 gtk_tree_model_get(model, &iter, PREFS_HDR_DATA, &dp, -1);
525                 if (dp) 
526                         prefs_common.disphdr_list =
527                                 g_slist_append(prefs_common.disphdr_list, dp);
528                 row++;
529         }
530 }
531
532 static gint prefs_display_header_find_header(GtkTreeView *list_view,
533                                              const gchar *header)
534 {
535         gint row = 0;
536         DisplayHeaderProp *dp;
537         GtkTreeModel *model;
538         GtkTreeIter iter;
539
540         model = gtk_tree_view_get_model(list_view);
541         while (gtk_tree_model_iter_nth_child(model, &iter, NULL, row)) {
542                 gtk_tree_model_get(model, &iter, PREFS_HDR_DATA, &dp, -1);
543                 if (dp && g_ascii_strcasecmp(dp->name, header) == 0)
544                         return row;
545                 row++;
546         }
547
548         return -1;
549 }
550
551 static void prefs_display_header_list_view_set_row(gboolean hidden)
552 {
553         GtkTreeView *list_view;
554         DisplayHeaderProp *dp;
555         const gchar *entry_text;
556         GtkTreeModel *model;
557
558         entry_text = gtk_entry_get_text(GTK_ENTRY(dispheader.hdr_entry));
559         if (entry_text[0] == '\0') {
560                 alertpanel_error(_("Header name is not set."));
561                 return;
562         }
563
564         if (hidden)
565                 list_view = GTK_TREE_VIEW(dispheader.hidden_headers_list_view);
566         else
567                 list_view = GTK_TREE_VIEW(dispheader.headers_list_view);
568
569         if (prefs_display_header_find_header(list_view, entry_text) != -1) {
570                 alertpanel_error(_("This header is already in the list."));
571                 return;
572         }
573
574         dp = g_new0(DisplayHeaderProp, 1);
575
576         dp->name = g_strdup(entry_text);
577         dp->hidden = hidden;
578
579         model = gtk_tree_view_get_model(list_view);
580         prefs_display_header_insert_header(GTK_LIST_STORE(model),
581                                            dp->name, dp);
582
583         prefs_display_header_set_list();
584 }
585
586 static void prefs_display_header_register_cb(GtkButton *btn,
587                                              gpointer hidden_data)
588 {
589         prefs_display_header_list_view_set_row(GPOINTER_TO_INT(hidden_data));
590 }
591
592 static void prefs_display_header_delete_cb(GtkButton *btn, gpointer list_view_data)
593 {
594         GtkTreeView *list_view = GTK_TREE_VIEW(list_view_data);
595         DisplayHeaderProp *dp;
596         GtkListStore *store = GTK_LIST_STORE(gtk_tree_view_get_model(list_view));
597         GtkTreeSelection *selection = gtk_tree_view_get_selection(list_view);
598         GtkTreeIter iter;
599
600         if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
601                 return;
602
603         gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, PREFS_HDR_DATA, &dp, -1);
604         if (!dp) 
605                 return;
606
607         prefs_common.disphdr_list =
608                 g_slist_remove(prefs_common.disphdr_list, dp);
609         display_header_prop_free(dp);
610         gtk_list_store_remove(store, &iter);
611 }
612
613 static void prefs_display_header_up(void)
614 {
615         GtkTreePath *prev, *sel, *try;
616         GtkTreeIter isel;
617         GtkListStore *store = NULL;
618         GtkTreeIter iprev;
619         
620         if (!gtk_tree_selection_get_selected
621                 (gtk_tree_view_get_selection
622                         (GTK_TREE_VIEW(dispheader.headers_list_view)),
623                  (GtkTreeModel **)(GtkListStore *) store,       
624                  &isel))
625                 return;
626
627         sel = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &isel);
628         if (!sel)
629                 return;
630         
631         /* no move if we're at row 0... */
632         try = gtk_tree_path_copy(sel);
633         if (!gtk_tree_path_prev(try)) {
634                 gtk_tree_path_free(try);
635                 gtk_tree_path_free(sel);
636                 return;
637         }
638
639         prev = try;
640         gtk_tree_model_get_iter(GTK_TREE_MODEL(store),
641                                 &iprev, prev);
642         gtk_list_store_swap(store, &iprev, &isel);
643
644         gtk_tree_path_free(sel);
645         gtk_tree_path_free(prev);
646 }
647
648 static void prefs_display_header_down(void)
649 {
650         GtkListStore *store = NULL;
651         GtkTreeIter next, sel;
652         GtkTreePath *try;
653         
654         if (!gtk_tree_selection_get_selected
655                 (gtk_tree_view_get_selection
656                         (GTK_TREE_VIEW(dispheader.headers_list_view)),
657                  (GtkTreeModel **)(GtkListStore *) store,
658                  &sel))
659                 return;
660
661         try = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &sel);
662         if (!try) 
663                 return;
664         
665         next = sel;
666         if (gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &next))
667                 gtk_list_store_swap(store, &next, &sel);
668                 
669         gtk_tree_path_free(try);
670 }
671
672 static gboolean prefs_display_header_key_pressed(GtkWidget *widget,
673                                              GdkEventKey *event,
674                                              gpointer data)
675 {
676         if (event && event->keyval == GDK_Escape)
677                 prefs_display_header_cancel();
678         return FALSE;
679 }
680
681 static void prefs_display_header_ok(void)
682 {
683         prefs_common.show_other_header =
684                 gtk_toggle_button_get_active
685                         (GTK_TOGGLE_BUTTON(dispheader.other_headers));
686         prefs_display_header_write_config();
687         gtk_widget_hide(dispheader.window);
688 }
689
690 static void prefs_display_header_cancel(void)
691 {
692         prefs_display_header_read_config();
693         gtk_widget_hide(dispheader.window);
694 }
695
696 static gint prefs_display_header_deleted(GtkWidget *widget, GdkEventAny *event,
697                                          gpointer data)
698 {
699         prefs_display_header_cancel();
700         return TRUE;
701 }
702
703 static GtkListStore *prefs_display_header_create_store(void)
704 {
705         return gtk_list_store_new(N_PREFS_HDR_COLUMNS,
706                                   G_TYPE_STRING,
707                                   G_TYPE_POINTER,
708                                   -1);
709 }
710
711 static void prefs_display_header_insert_header(GtkListStore *store,
712                                                gchar *name,
713                                                DisplayHeaderProp *dp)
714 {
715         GtkTreeIter iter;
716
717         /* add new */
718         gtk_list_store_append(store, &iter);
719         gtk_list_store_set(store, &iter,
720                            PREFS_HDR_HEADER, name,
721                            PREFS_HDR_DATA, dp,
722                            -1);
723 }
724
725 static GtkWidget *prefs_display_header_list_view_create(const gchar *name)
726 {
727         GtkWidget *list_view;
728         GtkTreeSelection *selector;
729         GtkTreeModel *model;
730
731         model = GTK_TREE_MODEL(prefs_display_header_create_store());
732         list_view = gtk_tree_view_new_with_model(model);
733         g_object_unref(G_OBJECT(model));
734         
735         gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(list_view),
736                                      prefs_common.enable_rules_hint);
737         
738         selector = gtk_tree_view_get_selection(GTK_TREE_VIEW(list_view));
739         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
740
741         prefs_filtering_create_list_view_columns(GTK_WIDGET(list_view), name);
742
743         return list_view;
744 }
745
746 static void prefs_filtering_create_list_view_columns(GtkWidget *list_view, 
747                                                      const gchar *name)
748 {
749         GtkTreeViewColumn *column;
750         GtkCellRenderer *renderer;
751
752         renderer = gtk_cell_renderer_text_new();
753         column = gtk_tree_view_column_new_with_attributes
754                 (name, renderer, "text", PREFS_HDR_HEADER, NULL);
755         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
756 }
757
758 /*!
759  *\brief        Called as a result of a drag & drop
760  */
761 static void headers_list_model_row_changed(GtkTreeModel *model, 
762                                            GtkTreePath *path, 
763                                            GtkTreeIter *iter, 
764                                            GtkTreeView *list_view)
765 {
766         prefs_display_header_set_list();
767 }
768
769 /*!
770  *\brief        Called as a result of a gtk_list_store_swap()
771  */
772 static void headers_list_model_rows_reordered(GtkTreeModel *model,
773                                               GtkTreePath  *path, 
774                                               GtkTreeIter  *iter,
775                                               gpointer      arg,
776                                               GtkTreeView  *list_view)
777 {
778         prefs_display_header_set_list();
779 }
780
781 static void drag_begin(GtkTreeView *list_view,
782                       GdkDragContext *context,
783                       gpointer data)
784 {
785         /* XXX unfortunately a completed drag & drop does not emit 
786          * a "rows_reordered" signal, but a "row_changed" signal.
787          * So during drag and drop, listen to "row_changed", and
788          * update the account list accordingly */
789
790         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
791         g_signal_connect(G_OBJECT(model), "row_changed",
792                          G_CALLBACK(headers_list_model_row_changed),
793                          list_view);
794 }
795
796 static void drag_end(GtkTreeView *list_view,
797                     GdkDragContext *context,
798                     gpointer data)
799 {
800         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
801         g_signal_handlers_disconnect_by_func(G_OBJECT(model),
802                                              G_CALLBACK(headers_list_model_row_changed),
803                                              list_view);
804 }
805