3f13de79f1f4eb3e65486d3614e678a071898208
[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 *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 static gchar *defaults[] =
115 {
116         "From",
117         "To",
118         "Cc",
119         "Subject",
120         "Date", 
121         "Reply-To",
122         "Sender",
123         "User-Agent",
124         "X-Mailer",     
125         "Newsgroups",
126         "Followup-To",
127         "Organization",
128         "X-Newsreader",
129         "-Received",
130         "-Message-ID",
131         "-In-Reply-To",
132         "-References",
133         "-Mime-Version",
134         "-Content-Type",
135         "-Content-Transfer-Encoding",
136         "-X-UIDL",
137         "-Precedence",
138         "-Status",
139         "-Priority",
140         "-X-Face"
141 };
142
143 static void prefs_display_header_set_default(void)
144 {
145         gint i;
146         DisplayHeaderProp *dp;
147
148         for(i = 0; i < sizeof(defaults) / sizeof(defaults[0]); i++) {
149                 dp = display_header_prop_read_str(defaults[i]);
150                 prefs_common.disphdr_list =
151                         g_slist_append(prefs_common.disphdr_list, dp);
152         }
153 }
154
155 void prefs_display_header_open(void)
156 {
157         if (prefs_rc_is_readonly(DISPLAY_HEADER_RC))
158                 return;
159
160         if (!dispheader.window) {
161                 prefs_display_header_create();
162         }
163
164         manage_window_set_transient(GTK_WINDOW(dispheader.window));
165         gtk_widget_grab_focus(dispheader.ok_btn);
166
167         prefs_display_header_set_dialog();
168
169         gtk_widget_show(dispheader.window);
170 }
171
172 static void prefs_display_header_create(void)
173 {
174         GtkWidget *window;
175         GtkWidget *vbox;
176         GtkWidget *btn_hbox;
177         GtkWidget *ok_btn;
178         GtkWidget *cancel_btn;
179         GtkWidget *confirm_area;
180
181         GtkWidget *vbox1;
182
183         GtkWidget *hbox1;
184         GtkWidget *hdr_label;
185         GtkWidget *hdr_combo;
186
187         GtkWidget *btn_vbox;
188         GtkWidget *reg_btn;
189         GtkWidget *del_btn;
190         GtkWidget *up_btn;
191         GtkWidget *down_btn;
192
193         GtkWidget *list_view_hbox;
194         GtkWidget *list_view_hbox1;
195         GtkWidget *list_view_hbox2;
196         GtkWidget *list_view_scrolledwin;
197         GtkWidget *headers_list_view;
198         GtkWidget *hidden_headers_list_view;
199
200         GtkWidget *checkbtn_other_headers;
201         gint i;
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_box_entry_new_text();
256         for(i=0; i < 9 ; i++)
257                 gtk_combo_box_append_text(GTK_COMBO_BOX (hdr_combo),
258                         prefs_common.trans_hdr ? gettext(defaults[i]) : defaults[i]);
259         gtk_combo_box_set_active(GTK_COMBO_BOX(hdr_combo), 0);
260         gtk_widget_show (hdr_combo);
261         gtk_box_pack_start (GTK_BOX (hbox1), hdr_combo, TRUE, TRUE, 0);
262         gtk_widget_set_size_request (hdr_combo, 150, -1);
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
389         dispheader.headers_list_view        = headers_list_view;
390         dispheader.hidden_headers_list_view = hidden_headers_list_view;
391
392         dispheader.other_headers = checkbtn_other_headers;
393 }
394
395 void prefs_display_header_read_config(void)
396 {
397         gchar *rcpath;
398         FILE *fp;
399         gchar buf[PREFSBUFSIZE];
400         DisplayHeaderProp *dp;
401
402         debug_print("Reading configuration for displaying headers...\n");
403
404         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
405                              DISPLAY_HEADER_RC, NULL);
406         if ((fp = g_fopen(rcpath, "rb")) == NULL) {
407                 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
408                 g_free(rcpath);
409                 prefs_common.disphdr_list = NULL;
410                 prefs_display_header_set_default();
411                 return;
412         }
413         g_free(rcpath);
414
415         /* remove all previous headers list */
416         while (prefs_common.disphdr_list != NULL) {
417                 dp = (DisplayHeaderProp *)prefs_common.disphdr_list->data;
418                 display_header_prop_free(dp);
419                 prefs_common.disphdr_list =
420                         g_slist_remove(prefs_common.disphdr_list, dp);
421         }
422
423         while (fgets(buf, sizeof(buf), fp) != NULL) {
424                 g_strdelimit(buf, "\r\n", '\0');
425                 dp = display_header_prop_read_str(buf);
426                 if (dp)
427                         prefs_common.disphdr_list =
428                                 g_slist_append(prefs_common.disphdr_list, dp);
429         }
430
431         fclose(fp);
432 }
433
434 void prefs_display_header_write_config(void)
435 {
436         gchar *rcpath;
437         PrefFile *pfile;
438         GSList *cur;
439
440         debug_print("Writing configuration for displaying headers...\n");
441
442         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
443                              DISPLAY_HEADER_RC, NULL);
444
445         if ((pfile = prefs_write_open(rcpath)) == NULL) {
446                 g_warning("failed to write configuration to file\n");
447                 g_free(rcpath);
448                 return;
449         }
450
451         for (cur = prefs_common.disphdr_list; cur != NULL;
452              cur = cur->next) {
453                 DisplayHeaderProp *dp = (DisplayHeaderProp *)cur->data;
454                 gchar *dpstr;
455
456                 dpstr = display_header_prop_get_str(dp);
457                 if (fputs(dpstr, pfile->fp) == EOF ||
458                     fputc('\n', pfile->fp) == EOF) {
459                         FILE_OP_ERROR(rcpath, "fputs || fputc");
460                         prefs_file_close_revert(pfile);
461                         g_free(rcpath);
462                         g_free(dpstr);
463                         return;
464                 }
465                 g_free(dpstr);
466         }
467
468         g_free(rcpath);
469
470         if (prefs_file_close(pfile) < 0) {
471                 g_warning("failed to write configuration to file\n");
472                 return;
473         }
474 }
475
476 static void prefs_display_header_set_dialog(void)
477 {
478         GtkTreeView *list_view = GTK_TREE_VIEW(dispheader.headers_list_view);
479         GtkTreeView *hidden_list_view = GTK_TREE_VIEW(dispheader.hidden_headers_list_view);
480         GSList *cur;
481         GtkTreeModel *model_list, *model_hidden;
482
483         model_list = gtk_tree_view_get_model(list_view);
484         model_hidden = gtk_tree_view_get_model(hidden_list_view);
485
486         gtk_list_store_clear(GTK_LIST_STORE(model_list));
487         gtk_list_store_clear(GTK_LIST_STORE(model_hidden));
488
489         for (cur = prefs_common.disphdr_list; cur != NULL;
490              cur = cur->next) {
491                 DisplayHeaderProp *dp = (DisplayHeaderProp *)cur->data;
492
493                 if (dp->hidden)
494                         prefs_display_header_insert_header(GTK_LIST_STORE
495                                                 (model_hidden), dp->name, dp);  
496                 else
497                         prefs_display_header_insert_header(GTK_LIST_STORE
498                                                 (model_list), dp->name, dp);
499         }
500
501         gtk_toggle_button_set_active
502                 (GTK_TOGGLE_BUTTON(dispheader.other_headers),
503                  prefs_common.show_other_header);
504 }
505
506 static void prefs_display_header_set_list(void)
507 {
508         gint row = 0;
509         DisplayHeaderProp *dp;
510         GtkTreeModel *model;
511         GtkTreeIter iter;
512
513         g_slist_free(prefs_common.disphdr_list);
514         prefs_common.disphdr_list = NULL;
515
516         model = gtk_tree_view_get_model(GTK_TREE_VIEW(dispheader.headers_list_view));
517         while (gtk_tree_model_iter_nth_child(model, &iter, NULL, row)) {
518                 gtk_tree_model_get(model, &iter, PREFS_HDR_DATA, &dp, -1);
519                 if (dp)
520                         prefs_common.disphdr_list =
521                                 g_slist_append(prefs_common.disphdr_list, dp);
522                 row++;                          
523         }
524
525         model = gtk_tree_view_get_model
526                         (GTK_TREE_VIEW(dispheader.hidden_headers_list_view));
527         row = 0;
528         while (gtk_tree_model_iter_nth_child(model, &iter, NULL, row)) {
529                 gtk_tree_model_get(model, &iter, PREFS_HDR_DATA, &dp, -1);
530                 if (dp) 
531                         prefs_common.disphdr_list =
532                                 g_slist_append(prefs_common.disphdr_list, dp);
533                 row++;
534         }
535 }
536
537 static gint prefs_display_header_find_header(GtkTreeView *list_view,
538                                              const gchar *header)
539 {
540         gint row = 0;
541         DisplayHeaderProp *dp;
542         GtkTreeModel *model;
543         GtkTreeIter iter;
544
545         model = gtk_tree_view_get_model(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 && g_ascii_strcasecmp(dp->name, header) == 0)
549                         return row;
550                 row++;
551         }
552
553         return -1;
554 }
555
556 static void prefs_display_header_list_view_set_row(gboolean hidden)
557 {
558         GtkTreeView *list_view;
559         DisplayHeaderProp *dp;
560         const gchar *entry_text;
561         GtkTreeModel *model;
562
563         entry_text = gtk_combo_box_get_active_text(GTK_COMBO_BOX(dispheader.hdr_combo));
564         if (entry_text[0] == '\0') {
565                 alertpanel_error(_("Header name is not set."));
566                 return;
567         }
568
569         if (hidden)
570                 list_view = GTK_TREE_VIEW(dispheader.hidden_headers_list_view);
571         else
572                 list_view = GTK_TREE_VIEW(dispheader.headers_list_view);
573
574         if (prefs_display_header_find_header(list_view, entry_text) != -1) {
575                 alertpanel_error(_("This header is already in the list."));
576                 return;
577         }
578
579         dp = g_new0(DisplayHeaderProp, 1);
580
581         dp->name = g_strdup(entry_text);
582         dp->hidden = hidden;
583
584         model = gtk_tree_view_get_model(list_view);
585         prefs_display_header_insert_header(GTK_LIST_STORE(model),
586                                            dp->name, dp);
587
588         prefs_display_header_set_list();
589 }
590
591 static void prefs_display_header_register_cb(GtkButton *btn,
592                                              gpointer hidden_data)
593 {
594         prefs_display_header_list_view_set_row(GPOINTER_TO_INT(hidden_data));
595 }
596
597 static void prefs_display_header_delete_cb(GtkButton *btn, gpointer list_view_data)
598 {
599         GtkTreeView *list_view = GTK_TREE_VIEW(list_view_data);
600         DisplayHeaderProp *dp;
601         GtkListStore *store = GTK_LIST_STORE(gtk_tree_view_get_model(list_view));
602         GtkTreeSelection *selection = gtk_tree_view_get_selection(list_view);
603         GtkTreeIter iter;
604
605         if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
606                 return;
607
608         gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, PREFS_HDR_DATA, &dp, -1);
609         if (!dp) 
610                 return;
611
612         prefs_common.disphdr_list =
613                 g_slist_remove(prefs_common.disphdr_list, dp);
614         display_header_prop_free(dp);
615         gtk_list_store_remove(store, &iter);
616 }
617
618 static void prefs_display_header_up(void)
619 {
620         GtkTreePath *prev, *sel, *try;
621         GtkTreeIter isel;
622         GtkListStore *store = NULL;
623         GtkTreeModel *model = NULL;
624         GtkTreeIter iprev;
625         
626         if (!gtk_tree_selection_get_selected
627                 (gtk_tree_view_get_selection
628                         (GTK_TREE_VIEW(dispheader.headers_list_view)),
629                  &model,        
630                  &isel))
631                 return;
632         store = (GtkListStore *)model;
633         sel = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &isel);
634         if (!sel)
635                 return;
636         
637         /* no move if we're at row 0... */
638         try = gtk_tree_path_copy(sel);
639         if (!gtk_tree_path_prev(try)) {
640                 gtk_tree_path_free(try);
641                 gtk_tree_path_free(sel);
642                 return;
643         }
644
645         prev = try;
646         gtk_tree_model_get_iter(GTK_TREE_MODEL(store),
647                                 &iprev, prev);
648         gtk_list_store_swap(store, &iprev, &isel);
649
650         gtk_tree_path_free(sel);
651         gtk_tree_path_free(prev);
652 }
653
654 static void prefs_display_header_down(void)
655 {
656         GtkListStore *store = NULL;
657         GtkTreeModel *model = NULL;
658         GtkTreeIter next, sel;
659         GtkTreePath *try;
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                  &sel))
666                 return;
667         store = (GtkListStore *)model;
668         try = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &sel);
669         if (!try) 
670                 return;
671         
672         next = sel;
673         if (gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &next))
674                 gtk_list_store_swap(store, &next, &sel);
675                 
676         gtk_tree_path_free(try);
677 }
678
679 static gboolean prefs_display_header_key_pressed(GtkWidget *widget,
680                                              GdkEventKey *event,
681                                              gpointer data)
682 {
683         if (event && event->keyval == GDK_Escape)
684                 prefs_display_header_cancel();
685         return FALSE;
686 }
687
688 static void prefs_display_header_ok(void)
689 {
690         prefs_common.show_other_header =
691                 gtk_toggle_button_get_active
692                         (GTK_TOGGLE_BUTTON(dispheader.other_headers));
693         prefs_display_header_write_config();
694         gtk_widget_hide(dispheader.window);
695 }
696
697 static void prefs_display_header_cancel(void)
698 {
699         prefs_display_header_read_config();
700         gtk_widget_hide(dispheader.window);
701 }
702
703 static gint prefs_display_header_deleted(GtkWidget *widget, GdkEventAny *event,
704                                          gpointer data)
705 {
706         prefs_display_header_cancel();
707         return TRUE;
708 }
709
710 static GtkListStore *prefs_display_header_create_store(void)
711 {
712         return gtk_list_store_new(N_PREFS_HDR_COLUMNS,
713                                   G_TYPE_STRING,
714                                   G_TYPE_POINTER,
715                                   -1);
716 }
717
718 static void prefs_display_header_insert_header(GtkListStore *store,
719                                                gchar *name,
720                                                DisplayHeaderProp *dp)
721 {
722         GtkTreeIter iter;
723
724         /* add new */
725         gtk_list_store_append(store, &iter);
726         gtk_list_store_set(store, &iter,
727                            PREFS_HDR_HEADER,
728                            prefs_common.trans_hdr ? gettext(name) : 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