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