a966815cfa78069013d7676e1696e745656fa5c5
[claws.git] / src / prefs_display_header.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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, &cancel_btn, GTK_STOCK_CANCEL,
220                                       &ok_btn, GTK_STOCK_OK,
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         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(list_view_scrolledwin),
283                                             GTK_SHADOW_IN);
284
285         headers_list_view = prefs_display_header_list_view_create
286                                 (_("Displayed Headers"));
287         gtk_widget_show (headers_list_view);
288         gtk_container_add(GTK_CONTAINER(list_view_scrolledwin), headers_list_view);
289         gtk_tree_view_set_reorderable(GTK_TREE_VIEW(headers_list_view), TRUE);
290
291         g_signal_connect(G_OBJECT(headers_list_view), "drag_begin",                      
292                          G_CALLBACK(drag_begin),
293                          headers_list_view);
294                          
295         g_signal_connect(G_OBJECT(headers_list_view), "drag_end",                        
296                          G_CALLBACK(drag_end),
297                          headers_list_view);
298         
299         /* connect rows change for this list view's model */
300         g_signal_connect(G_OBJECT(gtk_tree_view_get_model(GTK_TREE_VIEW(headers_list_view))),
301                          "rows-reordered", 
302                          G_CALLBACK(headers_list_model_rows_reordered),
303                          headers_list_view);
304
305         btn_vbox = gtk_vbox_new (FALSE, 8);
306         gtk_widget_show (btn_vbox);
307         gtk_box_pack_start (GTK_BOX (list_view_hbox1), btn_vbox, FALSE, FALSE, 0);
308
309         reg_btn = gtk_button_new_from_stock (GTK_STOCK_ADD);
310         gtk_widget_show (reg_btn);
311         gtk_box_pack_start (GTK_BOX (btn_vbox), reg_btn, FALSE, TRUE, 0);
312         g_signal_connect (G_OBJECT (reg_btn), "clicked",
313                           G_CALLBACK (prefs_display_header_register_cb),
314                             GINT_TO_POINTER(FALSE));
315         del_btn = gtk_button_new_from_stock (GTK_STOCK_REMOVE);
316         gtk_widget_show (del_btn);
317         gtk_box_pack_start (GTK_BOX (btn_vbox), del_btn, FALSE, TRUE, 0);
318         g_signal_connect (G_OBJECT (del_btn), "clicked",
319                           G_CALLBACK (prefs_display_header_delete_cb),
320                           headers_list_view);
321
322         up_btn = gtk_button_new_from_stock (GTK_STOCK_GO_UP);
323         gtk_widget_show (up_btn);
324         gtk_box_pack_start (GTK_BOX (btn_vbox), up_btn, FALSE, FALSE, 0);
325         g_signal_connect (G_OBJECT (up_btn), "clicked",
326                           G_CALLBACK (prefs_display_header_up), NULL);
327
328         down_btn = gtk_button_new_from_stock (GTK_STOCK_GO_DOWN);
329         gtk_widget_show (down_btn);
330         gtk_box_pack_start (GTK_BOX (btn_vbox), down_btn, FALSE, FALSE, 0);
331         g_signal_connect (G_OBJECT (down_btn), "clicked",
332                           G_CALLBACK (prefs_display_header_down), NULL);
333
334         /* hidden headers list */
335
336         list_view_hbox2 = gtk_hbox_new (FALSE, 8);
337         gtk_widget_show (list_view_hbox2);
338         gtk_box_pack_start (GTK_BOX (list_view_hbox), list_view_hbox2, TRUE, TRUE, 0);
339
340         list_view_scrolledwin = gtk_scrolled_window_new (NULL, NULL);
341         gtk_widget_set_size_request (list_view_scrolledwin, 200, 210);
342         gtk_widget_show (list_view_scrolledwin);
343         gtk_box_pack_start (GTK_BOX (list_view_hbox2), list_view_scrolledwin,
344                             TRUE, TRUE, 0);
345         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (list_view_scrolledwin),
346                                         GTK_POLICY_AUTOMATIC,
347                                         GTK_POLICY_AUTOMATIC);
348         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(list_view_scrolledwin),
349                                             GTK_SHADOW_IN);
350
351         hidden_headers_list_view = prefs_display_header_list_view_create
352                                         (_("Hidden headers"));
353         gtk_widget_show (hidden_headers_list_view);
354         gtk_container_add (GTK_CONTAINER (list_view_scrolledwin),
355                            hidden_headers_list_view);
356
357         btn_vbox = gtk_vbox_new (FALSE, 8);
358         gtk_widget_show (btn_vbox);
359         gtk_box_pack_start (GTK_BOX (list_view_hbox2), btn_vbox, FALSE, FALSE, 0);
360
361         reg_btn = gtk_button_new_from_stock (GTK_STOCK_ADD);
362         gtk_widget_show (reg_btn);
363         gtk_box_pack_start (GTK_BOX (btn_vbox), reg_btn, FALSE, TRUE, 0);
364         g_signal_connect (G_OBJECT (reg_btn), "clicked",
365                             G_CALLBACK
366                             (prefs_display_header_register_cb),
367                             GINT_TO_POINTER(TRUE));
368         del_btn = gtk_button_new_from_stock (GTK_STOCK_DELETE);
369         gtk_widget_show (del_btn);
370         gtk_box_pack_start (GTK_BOX (btn_vbox), del_btn, FALSE, TRUE, 0);
371         g_signal_connect (G_OBJECT       (del_btn), "clicked",
372                             G_CALLBACK (prefs_display_header_delete_cb),
373                             hidden_headers_list_view);
374
375         
376
377         PACK_CHECK_BUTTON (vbox, checkbtn_other_headers,
378                            _("Show all unspecified headers"));
379         SET_TOGGLE_SENSITIVITY (checkbtn_other_headers, list_view_hbox2);
380
381         gtk_widget_show_all(window);
382
383         dispheader.window        = window;
384         dispheader.ok_btn        = ok_btn;
385         dispheader.cancel_btn    = cancel_btn;
386
387         dispheader.hdr_combo     = hdr_combo;
388         dispheader.hdr_entry     = GTK_COMBO (hdr_combo)->entry;
389
390         dispheader.headers_list_view        = headers_list_view;
391         dispheader.hidden_headers_list_view = hidden_headers_list_view;
392
393         dispheader.other_headers = checkbtn_other_headers;
394 }
395
396 void prefs_display_header_read_config(void)
397 {
398         gchar *rcpath;
399         FILE *fp;
400         gchar buf[PREFSBUFSIZE];
401         DisplayHeaderProp *dp;
402
403         debug_print("Reading configuration for displaying headers...\n");
404
405         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
406                              DISPLAY_HEADER_RC, NULL);
407         if ((fp = g_fopen(rcpath, "rb")) == NULL) {
408                 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
409                 g_free(rcpath);
410                 prefs_common.disphdr_list = NULL;
411                 prefs_display_header_set_default();
412                 return;
413         }
414         g_free(rcpath);
415
416         /* remove all previous headers list */
417         while (prefs_common.disphdr_list != NULL) {
418                 dp = (DisplayHeaderProp *)prefs_common.disphdr_list->data;
419                 display_header_prop_free(dp);
420                 prefs_common.disphdr_list =
421                         g_slist_remove(prefs_common.disphdr_list, dp);
422         }
423
424         while (fgets(buf, sizeof(buf), fp) != NULL) {
425                 g_strdelimit(buf, "\r\n", '\0');
426                 dp = display_header_prop_read_str(buf);
427                 if (dp)
428                         prefs_common.disphdr_list =
429                                 g_slist_append(prefs_common.disphdr_list, dp);
430         }
431
432         fclose(fp);
433 }
434
435 void prefs_display_header_write_config(void)
436 {
437         gchar *rcpath;
438         PrefFile *pfile;
439         GSList *cur;
440
441         debug_print("Writing configuration for displaying headers...\n");
442
443         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
444                              DISPLAY_HEADER_RC, NULL);
445
446         if ((pfile = prefs_write_open(rcpath)) == NULL) {
447                 g_warning("failed to write configuration to file\n");
448                 g_free(rcpath);
449                 return;
450         }
451
452         for (cur = prefs_common.disphdr_list; cur != NULL;
453              cur = cur->next) {
454                 DisplayHeaderProp *dp = (DisplayHeaderProp *)cur->data;
455                 gchar *dpstr;
456
457                 dpstr = display_header_prop_get_str(dp);
458                 if (fputs(dpstr, pfile->fp) == EOF ||
459                     fputc('\n', pfile->fp) == EOF) {
460                         FILE_OP_ERROR(rcpath, "fputs || fputc");
461                         prefs_file_close_revert(pfile);
462                         g_free(rcpath);
463                         g_free(dpstr);
464                         return;
465                 }
466                 g_free(dpstr);
467         }
468
469         g_free(rcpath);
470
471         if (prefs_file_close(pfile) < 0) {
472                 g_warning("failed to write configuration to file\n");
473                 return;
474         }
475 }
476
477 static void prefs_display_header_set_dialog(void)
478 {
479         GtkTreeView *list_view = GTK_TREE_VIEW(dispheader.headers_list_view);
480         GtkTreeView *hidden_list_view = GTK_TREE_VIEW(dispheader.hidden_headers_list_view);
481         GSList *cur;
482         GtkTreeModel *model_list, *model_hidden;
483
484         model_list = gtk_tree_view_get_model(list_view);
485         model_hidden = gtk_tree_view_get_model(hidden_list_view);
486
487         gtk_list_store_clear(GTK_LIST_STORE(model_list));
488         gtk_list_store_clear(GTK_LIST_STORE(model_hidden));
489
490         for (cur = prefs_common.disphdr_list; cur != NULL;
491              cur = cur->next) {
492                 DisplayHeaderProp *dp = (DisplayHeaderProp *)cur->data;
493
494                 if (dp->hidden)
495                         prefs_display_header_insert_header(GTK_LIST_STORE
496                                                 (model_hidden), dp->name, dp);  
497                 else
498                         prefs_display_header_insert_header(GTK_LIST_STORE
499                                                 (model_list), dp->name, dp);
500         }
501
502         gtk_toggle_button_set_active
503                 (GTK_TOGGLE_BUTTON(dispheader.other_headers),
504                  prefs_common.show_other_header);
505 }
506
507 static void prefs_display_header_set_list(void)
508 {
509         gint row = 0;
510         DisplayHeaderProp *dp;
511         GtkTreeModel *model;
512         GtkTreeIter iter;
513
514         g_slist_free(prefs_common.disphdr_list);
515         prefs_common.disphdr_list = NULL;
516
517         model = gtk_tree_view_get_model(GTK_TREE_VIEW(dispheader.headers_list_view));
518         while (gtk_tree_model_iter_nth_child(model, &iter, NULL, row)) {
519                 gtk_tree_model_get(model, &iter, PREFS_HDR_DATA, &dp, -1);
520                 if (dp)
521                         prefs_common.disphdr_list =
522                                 g_slist_append(prefs_common.disphdr_list, dp);
523                 row++;                          
524         }
525
526         model = gtk_tree_view_get_model
527                         (GTK_TREE_VIEW(dispheader.hidden_headers_list_view));
528         row = 0;
529         while (gtk_tree_model_iter_nth_child(model, &iter, NULL, row)) {
530                 gtk_tree_model_get(model, &iter, PREFS_HDR_DATA, &dp, -1);
531                 if (dp) 
532                         prefs_common.disphdr_list =
533                                 g_slist_append(prefs_common.disphdr_list, dp);
534                 row++;
535         }
536 }
537
538 static gint prefs_display_header_find_header(GtkTreeView *list_view,
539                                              const gchar *header)
540 {
541         gint row = 0;
542         DisplayHeaderProp *dp;
543         GtkTreeModel *model;
544         GtkTreeIter iter;
545
546         model = gtk_tree_view_get_model(list_view);
547         while (gtk_tree_model_iter_nth_child(model, &iter, NULL, row)) {
548                 gtk_tree_model_get(model, &iter, PREFS_HDR_DATA, &dp, -1);
549                 if (dp && g_ascii_strcasecmp(dp->name, header) == 0)
550                         return row;
551                 row++;
552         }
553
554         return -1;
555 }
556
557 static void prefs_display_header_list_view_set_row(gboolean hidden)
558 {
559         GtkTreeView *list_view;
560         DisplayHeaderProp *dp;
561         const gchar *entry_text;
562         GtkTreeModel *model;
563
564         entry_text = gtk_entry_get_text(GTK_ENTRY(dispheader.hdr_entry));
565         if (entry_text[0] == '\0') {
566                 alertpanel_error(_("Header name is not set."));
567                 return;
568         }
569
570         if (hidden)
571                 list_view = GTK_TREE_VIEW(dispheader.hidden_headers_list_view);
572         else
573                 list_view = GTK_TREE_VIEW(dispheader.headers_list_view);
574
575         if (prefs_display_header_find_header(list_view, entry_text) != -1) {
576                 alertpanel_error(_("This header is already in the list."));
577                 return;
578         }
579
580         dp = g_new0(DisplayHeaderProp, 1);
581
582         dp->name = g_strdup(entry_text);
583         dp->hidden = hidden;
584
585         model = gtk_tree_view_get_model(list_view);
586         prefs_display_header_insert_header(GTK_LIST_STORE(model),
587                                            dp->name, dp);
588
589         prefs_display_header_set_list();
590 }
591
592 static void prefs_display_header_register_cb(GtkButton *btn,
593                                              gpointer hidden_data)
594 {
595         prefs_display_header_list_view_set_row(GPOINTER_TO_INT(hidden_data));
596 }
597
598 static void prefs_display_header_delete_cb(GtkButton *btn, gpointer list_view_data)
599 {
600         GtkTreeView *list_view = GTK_TREE_VIEW(list_view_data);
601         DisplayHeaderProp *dp;
602         GtkListStore *store = GTK_LIST_STORE(gtk_tree_view_get_model(list_view));
603         GtkTreeSelection *selection = gtk_tree_view_get_selection(list_view);
604         GtkTreeIter iter;
605
606         if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
607                 return;
608
609         gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, PREFS_HDR_DATA, &dp, -1);
610         if (!dp) 
611                 return;
612
613         prefs_common.disphdr_list =
614                 g_slist_remove(prefs_common.disphdr_list, dp);
615         display_header_prop_free(dp);
616         gtk_list_store_remove(store, &iter);
617 }
618
619 static void prefs_display_header_up(void)
620 {
621         GtkTreePath *prev, *sel, *try;
622         GtkTreeIter isel;
623         GtkListStore *store = NULL;
624         GtkTreeModel *model = NULL;
625         GtkTreeIter iprev;
626         
627         if (!gtk_tree_selection_get_selected
628                 (gtk_tree_view_get_selection
629                         (GTK_TREE_VIEW(dispheader.headers_list_view)),
630                  &model,        
631                  &isel))
632                 return;
633         store = (GtkListStore *)model;
634         sel = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &isel);
635         if (!sel)
636                 return;
637         
638         /* no move if we're at row 0... */
639         try = gtk_tree_path_copy(sel);
640         if (!gtk_tree_path_prev(try)) {
641                 gtk_tree_path_free(try);
642                 gtk_tree_path_free(sel);
643                 return;
644         }
645
646         prev = try;
647         gtk_tree_model_get_iter(GTK_TREE_MODEL(store),
648                                 &iprev, prev);
649         gtk_list_store_swap(store, &iprev, &isel);
650
651         gtk_tree_path_free(sel);
652         gtk_tree_path_free(prev);
653 }
654
655 static void prefs_display_header_down(void)
656 {
657         GtkListStore *store = NULL;
658         GtkTreeModel *model = NULL;
659         GtkTreeIter next, sel;
660         GtkTreePath *try;
661         
662         if (!gtk_tree_selection_get_selected
663                 (gtk_tree_view_get_selection
664                         (GTK_TREE_VIEW(dispheader.headers_list_view)),
665                  &model,
666                  &sel))
667                 return;
668         store = (GtkListStore *)model;
669         try = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &sel);
670         if (!try) 
671                 return;
672         
673         next = sel;
674         if (gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &next))
675                 gtk_list_store_swap(store, &next, &sel);
676                 
677         gtk_tree_path_free(try);
678 }
679
680 static gboolean prefs_display_header_key_pressed(GtkWidget *widget,
681                                              GdkEventKey *event,
682                                              gpointer data)
683 {
684         if (event && event->keyval == GDK_Escape)
685                 prefs_display_header_cancel();
686         return FALSE;
687 }
688
689 static void prefs_display_header_ok(void)
690 {
691         prefs_common.show_other_header =
692                 gtk_toggle_button_get_active
693                         (GTK_TOGGLE_BUTTON(dispheader.other_headers));
694         prefs_display_header_write_config();
695         gtk_widget_hide(dispheader.window);
696 }
697
698 static void prefs_display_header_cancel(void)
699 {
700         prefs_display_header_read_config();
701         gtk_widget_hide(dispheader.window);
702 }
703
704 static gint prefs_display_header_deleted(GtkWidget *widget, GdkEventAny *event,
705                                          gpointer data)
706 {
707         prefs_display_header_cancel();
708         return TRUE;
709 }
710
711 static GtkListStore *prefs_display_header_create_store(void)
712 {
713         return gtk_list_store_new(N_PREFS_HDR_COLUMNS,
714                                   G_TYPE_STRING,
715                                   G_TYPE_POINTER,
716                                   -1);
717 }
718
719 static void prefs_display_header_insert_header(GtkListStore *store,
720                                                gchar *name,
721                                                DisplayHeaderProp *dp)
722 {
723         GtkTreeIter iter;
724
725         /* add new */
726         gtk_list_store_append(store, &iter);
727         gtk_list_store_set(store, &iter,
728                            PREFS_HDR_HEADER, name,
729                            PREFS_HDR_DATA, dp,
730                            -1);
731 }
732
733 static GtkWidget *prefs_display_header_list_view_create(const gchar *name)
734 {
735         GtkWidget *list_view;
736         GtkTreeSelection *selector;
737         GtkTreeModel *model;
738
739         model = GTK_TREE_MODEL(prefs_display_header_create_store());
740         list_view = gtk_tree_view_new_with_model(model);
741         g_object_unref(G_OBJECT(model));
742         
743         gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(list_view),
744                                      prefs_common.enable_rules_hint);
745         
746         selector = gtk_tree_view_get_selection(GTK_TREE_VIEW(list_view));
747         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
748
749         prefs_filtering_create_list_view_columns(GTK_WIDGET(list_view), name);
750
751         return list_view;
752 }
753
754 static void prefs_filtering_create_list_view_columns(GtkWidget *list_view, 
755                                                      const gchar *name)
756 {
757         GtkTreeViewColumn *column;
758         GtkCellRenderer *renderer;
759
760         renderer = gtk_cell_renderer_text_new();
761         column = gtk_tree_view_column_new_with_attributes
762                 (name, renderer, "text", PREFS_HDR_HEADER, NULL);
763         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
764 }
765
766 /*!
767  *\brief        Called as a result of a drag & drop
768  */
769 static void headers_list_model_row_changed(GtkTreeModel *model, 
770                                            GtkTreePath *path, 
771                                            GtkTreeIter *iter, 
772                                            GtkTreeView *list_view)
773 {
774         prefs_display_header_set_list();
775 }
776
777 /*!
778  *\brief        Called as a result of a gtk_list_store_swap()
779  */
780 static void headers_list_model_rows_reordered(GtkTreeModel *model,
781                                               GtkTreePath  *path, 
782                                               GtkTreeIter  *iter,
783                                               gpointer      arg,
784                                               GtkTreeView  *list_view)
785 {
786         prefs_display_header_set_list();
787 }
788
789 static void drag_begin(GtkTreeView *list_view,
790                       GdkDragContext *context,
791                       gpointer data)
792 {
793         /* XXX unfortunately a completed drag & drop does not emit 
794          * a "rows_reordered" signal, but a "row_changed" signal.
795          * So during drag and drop, listen to "row_changed", and
796          * update the account list accordingly */
797
798         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
799         g_signal_connect(G_OBJECT(model), "row_changed",
800                          G_CALLBACK(headers_list_model_row_changed),
801                          list_view);
802 }
803
804 static void drag_end(GtkTreeView *list_view,
805                     GdkDragContext *context,
806                     gpointer data)
807 {
808         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
809         g_signal_handlers_disconnect_by_func(G_OBJECT(model),
810                                              G_CALLBACK(headers_list_model_row_changed),
811                                              list_view);
812 }
813