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