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