Fix CID 1596595: Resource leak
[claws.git] / src / prefs_display_header.c
blob142269bfe556ec91add693711a794b135f9c32cd
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2024 the Claws Mail team and Hiroyuki Yamamoto
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.
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.
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/>.
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #include "claws-features.h"
22 #endif
24 #include "defs.h"
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>
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 #include "file-utils.h"
45 enum {
46 PREFS_HDR_HEADER,
47 PREFS_HDR_DATA,
48 N_PREFS_HDR_COLUMNS
51 static struct DisplayHeader {
52 GtkWidget *window;
54 GtkWidget *ok_btn;
55 GtkWidget *cancel_btn;
57 GtkWidget *hdr_combo;
58 GtkWidget *key_check;
59 GtkWidget *headers_list_view;
60 GtkWidget *hidden_headers_list_view;
62 GtkWidget *other_headers;
63 } dispheader;
65 /* widget creating functions */
66 static void prefs_display_header_create (void);
68 static void prefs_display_header_set_dialog (void);
69 static void prefs_display_header_set_list (void);
70 static void prefs_display_header_list_view_set_row (gboolean hidden);
72 /* callback functions */
73 static void prefs_display_header_register_cb (GtkButton *btn,
74 gpointer hidden_data);
75 static void prefs_display_header_delete_cb (GtkButton *btn,
76 gpointer list_view_data);
77 static void prefs_display_header_up (void);
78 static void prefs_display_header_down (void);
80 static gboolean prefs_display_header_key_pressed (GtkWidget *widget,
81 GdkEventKey *event,
82 gpointer data);
83 static void prefs_display_header_ok (void);
84 static void prefs_display_header_cancel (void);
85 static gint prefs_display_header_deleted (GtkWidget *widget,
86 GdkEventAny *event,
87 gpointer data);
90 static GtkListStore *prefs_display_header_create_store (void);
91 static void prefs_display_header_insert_header (GtkListStore *store,
92 gchar *name,
93 DisplayHeaderProp *dp);
94 static GtkWidget *prefs_display_header_list_view_create (const gchar *name);
95 static void prefs_filtering_create_list_view_columns (GtkWidget *list_view,
96 const gchar *name);
97 static void headers_list_model_rows_reordered (GtkTreeModel *model,
98 GtkTreePath *path,
99 GtkTreeIter *iter,
100 gpointer arg,
101 GtkTreeView *list_view);
103 static void drag_end (GtkTreeView *list_view,
104 GdkDragContext *context,
105 gpointer data);
107 #ifndef GENERIC_UMPC
108 static gchar *defaults[] =
110 "From",
111 "To",
112 "Cc",
113 "Bcc",
114 "Subject",
115 "Date",
116 "Reply-To",
117 "Sender",
118 "User-Agent",
119 "X-Mailer",
120 "Newsgroups",
121 "Followup-To",
122 "Organization",
123 "X-Newsreader",
124 "-Received",
125 "-Message-ID",
126 "-In-Reply-To",
127 "-References",
128 "-MIME-Version",
129 "-Content-Type",
130 "-Content-Transfer-Encoding",
131 "-X-UIDL",
132 "-Precedence",
133 "-Status",
134 "-Priority",
135 "-X-Face"
137 #else
138 static gchar *defaults[] =
140 "From",
141 "To",
142 "Cc",
143 "Bcc",
144 "Subject",
145 "Date",
146 "Newsgroups",
147 "Followup-To",
148 "-Reply-To",
149 "-Sender",
150 "-User-Agent",
151 "-X-Mailer",
152 "-Organization",
153 "-X-Newsreader",
154 "-Received",
155 "-Message-ID",
156 "-In-Reply-To",
157 "-References",
158 "-MIME-Version",
159 "-Content-Type",
160 "-Content-Transfer-Encoding",
161 "-X-UIDL",
162 "-Precedence",
163 "-Status",
164 "-Priority",
165 "-X-Face"
167 #endif
168 static void prefs_display_header_set_default(void)
170 gint i;
171 DisplayHeaderProp *dp;
173 for(i = 0; i < sizeof(defaults) / sizeof(defaults[0]); i++) {
174 dp = display_header_prop_read_str(defaults[i]);
175 prefs_common.disphdr_list =
176 g_slist_append(prefs_common.disphdr_list, dp);
180 void prefs_display_header_open(void)
182 if (prefs_rc_is_readonly(DISPLAY_HEADER_RC))
183 return;
185 if (!dispheader.window) {
186 prefs_display_header_create();
189 manage_window_set_transient(GTK_WINDOW(dispheader.window));
190 gtk_widget_grab_focus(dispheader.ok_btn);
192 prefs_display_header_set_dialog();
194 gtk_widget_show(dispheader.window);
195 gtk_window_set_modal(GTK_WINDOW(dispheader.window), TRUE);
198 static void prefs_display_header_create(void)
200 GtkWidget *window;
201 GtkWidget *vbox;
202 GtkWidget *btn_hbox;
203 GtkWidget *ok_btn;
204 GtkWidget *cancel_btn;
205 GtkWidget *confirm_area;
207 GtkWidget *vbox1;
209 GtkWidget *hbox1;
210 GtkWidget *hdr_label;
211 GtkWidget *hdr_combo;
213 GtkWidget *btn_vbox;
214 GtkWidget *reg_btn;
215 GtkWidget *del_btn;
216 GtkWidget *up_btn;
217 GtkWidget *down_btn;
219 GtkWidget *list_view_hbox;
220 GtkWidget *list_view_hbox1;
221 GtkWidget *list_view_hbox2;
222 GtkWidget *list_view_scrolledwin;
223 GtkWidget *headers_list_view;
224 GtkWidget *hidden_headers_list_view;
226 GtkWidget *checkbtn_other_headers;
227 gint i;
229 debug_print("Creating display header setting window...\n");
231 window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "prefs_display_header");
232 gtk_container_set_border_width (GTK_CONTAINER (window), 8);
233 gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
234 gtk_window_set_resizable(GTK_WINDOW (window), TRUE);
235 gtk_window_set_type_hint(GTK_WINDOW(window), GDK_WINDOW_TYPE_HINT_DIALOG);
237 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 6);
238 gtk_widget_show (vbox);
239 gtk_container_add (GTK_CONTAINER (window), vbox);
241 btn_hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
242 gtk_widget_show (btn_hbox);
243 gtk_box_pack_end (GTK_BOX (vbox), btn_hbox, FALSE, FALSE, 0);
245 gtkut_stock_button_set_create(&confirm_area, &cancel_btn, NULL, _("_Cancel"),
246 &ok_btn, NULL, _("_OK"), NULL, NULL, NULL);
247 gtk_widget_show (confirm_area);
248 gtk_box_pack_end (GTK_BOX(btn_hbox), confirm_area, FALSE, FALSE, 0);
249 gtk_widget_grab_default (ok_btn);
251 gtk_window_set_title (GTK_WINDOW(window),
252 _("Displayed header configuration"));
253 MANAGE_WINDOW_SIGNALS_CONNECT(window);
254 g_signal_connect (G_OBJECT(window), "delete_event",
255 G_CALLBACK(prefs_display_header_deleted),
256 NULL);
257 g_signal_connect (G_OBJECT(window), "key_press_event",
258 G_CALLBACK(prefs_display_header_key_pressed),
259 NULL);
260 g_signal_connect (G_OBJECT(ok_btn), "clicked",
261 G_CALLBACK(prefs_display_header_ok),
262 NULL);
263 g_signal_connect (G_OBJECT(cancel_btn), "clicked",
264 G_CALLBACK(prefs_display_header_cancel),
265 NULL);
267 vbox1 = gtk_box_new(GTK_ORIENTATION_VERTICAL, VSPACING);
268 gtk_widget_show (vbox1);
269 gtk_box_pack_start (GTK_BOX (vbox), vbox1, TRUE, TRUE, 0);
270 gtk_container_set_border_width (GTK_CONTAINER (vbox1), 2);
272 hbox1 = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
273 gtk_widget_show (hbox1);
274 gtk_box_pack_start (GTK_BOX (vbox1), hbox1, FALSE, TRUE, 0);
276 hdr_label = gtk_label_new (_("Header name"));
277 gtk_widget_show (hdr_label);
278 gtk_box_pack_start (GTK_BOX (hbox1), hdr_label, FALSE, FALSE, 0);
280 hdr_combo = gtk_combo_box_text_new_with_entry();
281 for(i=0; i < 9 ; i++)
282 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT (hdr_combo),
283 (*defaults[i] == '-') ? defaults[i]+1 : defaults[i]);
284 gtk_combo_box_set_active(GTK_COMBO_BOX(hdr_combo), 0);
285 gtk_widget_show (hdr_combo);
286 gtk_box_pack_start (GTK_BOX (hbox1), hdr_combo, TRUE, TRUE, 0);
287 gtk_widget_set_size_request (hdr_combo, 150, -1);
289 list_view_hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
290 gtk_widget_show (list_view_hbox);
291 gtk_box_pack_start (GTK_BOX (vbox1), list_view_hbox, TRUE, TRUE, 0);
293 /* hidden headers list */
295 list_view_hbox2 = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
296 gtk_widget_show (list_view_hbox2);
297 gtk_box_pack_start (GTK_BOX (list_view_hbox), list_view_hbox2, TRUE, TRUE, 0);
299 list_view_scrolledwin = gtk_scrolled_window_new (NULL, NULL);
300 gtk_widget_set_size_request (list_view_scrolledwin, 200, 210);
301 gtk_widget_show (list_view_scrolledwin);
302 gtk_box_pack_start (GTK_BOX (list_view_hbox2), list_view_scrolledwin,
303 TRUE, TRUE, 0);
304 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (list_view_scrolledwin),
305 GTK_POLICY_AUTOMATIC,
306 GTK_POLICY_AUTOMATIC);
307 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(list_view_scrolledwin),
308 GTK_SHADOW_IN);
310 hidden_headers_list_view = prefs_display_header_list_view_create
311 (_("Hidden headers"));
312 gtk_widget_show (hidden_headers_list_view);
313 gtk_container_add (GTK_CONTAINER (list_view_scrolledwin),
314 hidden_headers_list_view);
316 btn_vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 8);
317 gtk_widget_show (btn_vbox);
318 gtk_box_pack_start (GTK_BOX (list_view_hbox2), btn_vbox, FALSE, FALSE, 0);
320 reg_btn = gtkut_stock_button("list-add", _("_Add"));
321 gtk_widget_show (reg_btn);
322 gtk_box_pack_start (GTK_BOX (btn_vbox), reg_btn, FALSE, TRUE, 0);
323 g_signal_connect (G_OBJECT (reg_btn), "clicked",
324 G_CALLBACK
325 (prefs_display_header_register_cb),
326 GINT_TO_POINTER(TRUE));
327 del_btn = gtkut_stock_button("edit-delete", _("_Delete"));
328 gtk_widget_show (del_btn);
329 gtk_box_pack_start (GTK_BOX (btn_vbox), del_btn, FALSE, TRUE, 0);
330 g_signal_connect (G_OBJECT (del_btn), "clicked",
331 G_CALLBACK (prefs_display_header_delete_cb),
332 hidden_headers_list_view);
334 /* display headers list */
336 list_view_hbox1 = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
337 gtk_widget_show (list_view_hbox1);
338 gtk_box_pack_start (GTK_BOX (list_view_hbox), list_view_hbox1, TRUE, TRUE, 0);
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_hbox1), 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);
351 headers_list_view = prefs_display_header_list_view_create
352 (_("Displayed Headers"));
353 gtk_widget_show (headers_list_view);
354 gtk_container_add(GTK_CONTAINER(list_view_scrolledwin), headers_list_view);
355 gtk_tree_view_set_reorderable(GTK_TREE_VIEW(headers_list_view), TRUE);
357 g_signal_connect(G_OBJECT(headers_list_view), "drag_end",
358 G_CALLBACK(drag_end),
359 headers_list_view);
361 /* connect rows change for this list view's model */
362 g_signal_connect(G_OBJECT(gtk_tree_view_get_model(GTK_TREE_VIEW(headers_list_view))),
363 "rows-reordered",
364 G_CALLBACK(headers_list_model_rows_reordered),
365 headers_list_view);
367 btn_vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 8);
368 gtk_widget_show (btn_vbox);
369 gtk_box_pack_start (GTK_BOX (list_view_hbox1), btn_vbox, FALSE, FALSE, 0);
371 reg_btn = gtkut_stock_button("list-add", _("_Add"));
372 gtk_widget_show (reg_btn);
373 gtk_box_pack_start (GTK_BOX (btn_vbox), reg_btn, FALSE, TRUE, 0);
374 g_signal_connect (G_OBJECT (reg_btn), "clicked",
375 G_CALLBACK (prefs_display_header_register_cb),
376 GINT_TO_POINTER(FALSE));
377 del_btn = gtkut_stock_button("list-remove", _("_Remove"));
378 gtk_widget_show (del_btn);
379 gtk_box_pack_start (GTK_BOX (btn_vbox), del_btn, FALSE, TRUE, 0);
380 g_signal_connect (G_OBJECT (del_btn), "clicked",
381 G_CALLBACK (prefs_display_header_delete_cb),
382 headers_list_view);
384 up_btn = gtkut_stock_button("go-up", _("_Up"));
385 gtk_widget_show (up_btn);
386 gtk_box_pack_start (GTK_BOX (btn_vbox), up_btn, FALSE, FALSE, 0);
387 g_signal_connect (G_OBJECT (up_btn), "clicked",
388 G_CALLBACK (prefs_display_header_up), NULL);
390 down_btn = gtkut_stock_button("go-down", _("_Down"));
391 gtk_widget_show (down_btn);
392 gtk_box_pack_start (GTK_BOX (btn_vbox), down_btn, FALSE, FALSE, 0);
393 g_signal_connect (G_OBJECT (down_btn), "clicked",
394 G_CALLBACK (prefs_display_header_down), NULL);
396 PACK_CHECK_BUTTON (vbox, checkbtn_other_headers,
397 _("Show all unspecified headers"));
398 SET_TOGGLE_SENSITIVITY (checkbtn_other_headers, list_view_hbox2);
400 gtk_widget_show_all(window);
402 dispheader.window = window;
403 dispheader.ok_btn = ok_btn;
404 dispheader.cancel_btn = cancel_btn;
406 dispheader.hdr_combo = hdr_combo;
408 dispheader.headers_list_view = headers_list_view;
409 dispheader.hidden_headers_list_view = hidden_headers_list_view;
411 dispheader.other_headers = checkbtn_other_headers;
414 void prefs_display_header_read_config(void)
416 gchar *rcpath;
417 FILE *fp;
418 gchar buf[PREFSBUFSIZE];
419 DisplayHeaderProp *dp;
421 debug_print("Reading configuration for displaying headers...\n");
423 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
424 DISPLAY_HEADER_RC, NULL);
425 if ((fp = claws_fopen(rcpath, "rb")) == NULL) {
426 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "claws_fopen");
427 g_free(rcpath);
428 prefs_common.disphdr_list = NULL;
429 prefs_display_header_set_default();
430 return;
432 g_free(rcpath);
434 /* remove all previous headers list */
435 while (prefs_common.disphdr_list != NULL) {
436 dp = (DisplayHeaderProp *)prefs_common.disphdr_list->data;
437 prefs_common.disphdr_list =
438 g_slist_remove(prefs_common.disphdr_list, dp);
439 display_header_prop_free(dp);
442 while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
443 g_strdelimit(buf, "\r\n", '\0');
444 dp = display_header_prop_read_str(buf);
445 if (dp)
446 prefs_common.disphdr_list =
447 g_slist_append(prefs_common.disphdr_list, dp);
450 claws_fclose(fp);
453 static void prefs_display_header_write_config(void)
455 gchar *rcpath;
456 PrefFile *pfile;
457 GSList *cur;
459 debug_print("Writing configuration for displaying headers...\n");
461 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
462 DISPLAY_HEADER_RC, NULL);
464 if ((pfile = prefs_write_open(rcpath)) == NULL) {
465 g_warning("failed to write configuration to file");
466 g_free(rcpath);
467 return;
470 for (cur = prefs_common.disphdr_list; cur != NULL;
471 cur = cur->next) {
472 DisplayHeaderProp *dp = (DisplayHeaderProp *)cur->data;
473 gchar *dpstr;
475 dpstr = display_header_prop_get_str(dp);
476 if (claws_fputs(dpstr, pfile->fp) == EOF ||
477 claws_fputc('\n', pfile->fp) == EOF) {
478 FILE_OP_ERROR(rcpath, "claws_fputs || claws_fputc");
479 prefs_file_close_revert(pfile);
480 g_free(rcpath);
481 g_free(dpstr);
482 return;
484 g_free(dpstr);
487 g_free(rcpath);
489 if (prefs_file_close(pfile) < 0) {
490 g_warning("failed to write configuration to file");
491 return;
495 static void prefs_display_header_set_dialog(void)
497 GtkTreeView *list_view = GTK_TREE_VIEW(dispheader.headers_list_view);
498 GtkTreeView *hidden_list_view = GTK_TREE_VIEW(dispheader.hidden_headers_list_view);
499 GSList *cur;
500 GtkTreeModel *model_list, *model_hidden;
502 model_list = gtk_tree_view_get_model(list_view);
503 model_hidden = gtk_tree_view_get_model(hidden_list_view);
505 gtk_list_store_clear(GTK_LIST_STORE(model_list));
506 gtk_list_store_clear(GTK_LIST_STORE(model_hidden));
508 for (cur = prefs_common.disphdr_list; cur != NULL;
509 cur = cur->next) {
510 DisplayHeaderProp *dp = (DisplayHeaderProp *)cur->data;
512 if (dp->hidden)
513 prefs_display_header_insert_header(GTK_LIST_STORE
514 (model_hidden), dp->name, dp);
515 else
516 prefs_display_header_insert_header(GTK_LIST_STORE
517 (model_list), dp->name, dp);
520 gtk_toggle_button_set_active
521 (GTK_TOGGLE_BUTTON(dispheader.other_headers),
522 prefs_common.show_other_header);
525 static void prefs_display_header_set_list(void)
527 gint row = 0;
528 DisplayHeaderProp *dp;
529 GtkTreeModel *model;
530 GtkTreeIter iter;
532 g_slist_free(prefs_common.disphdr_list);
533 prefs_common.disphdr_list = NULL;
535 model = gtk_tree_view_get_model(GTK_TREE_VIEW(dispheader.headers_list_view));
536 while (gtk_tree_model_iter_nth_child(model, &iter, NULL, row)) {
537 gtk_tree_model_get(model, &iter, PREFS_HDR_DATA, &dp, -1);
538 if (dp)
539 prefs_common.disphdr_list =
540 g_slist_append(prefs_common.disphdr_list, dp);
541 row++;
544 model = gtk_tree_view_get_model
545 (GTK_TREE_VIEW(dispheader.hidden_headers_list_view));
546 row = 0;
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++;
556 static gint prefs_display_header_find_header(GtkTreeView *list_view,
557 const gchar *header)
559 gint row = 0;
560 DisplayHeaderProp *dp;
561 GtkTreeModel *model;
562 GtkTreeIter iter;
564 model = gtk_tree_view_get_model(list_view);
565 while (gtk_tree_model_iter_nth_child(model, &iter, NULL, row)) {
566 gtk_tree_model_get(model, &iter, PREFS_HDR_DATA, &dp, -1);
567 if (dp && g_ascii_strcasecmp(dp->name, header) == 0)
568 return row;
569 row++;
572 return -1;
575 static void prefs_display_header_list_view_set_row(gboolean hidden)
577 GtkTreeView *list_view;
578 DisplayHeaderProp *dp;
579 gchar *entry_text;
580 GtkTreeModel *model;
582 entry_text = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(dispheader.hdr_combo));
583 if (!entry_text)
584 entry_text = gtk_editable_get_chars(
585 GTK_EDITABLE(gtk_bin_get_child(GTK_BIN(dispheader.hdr_combo))),0,-1);
586 if (!entry_text || entry_text[0] == '\0') {
587 alertpanel_error(_("Header name is not set."));
588 return;
591 if (hidden)
592 list_view = GTK_TREE_VIEW(dispheader.hidden_headers_list_view);
593 else
594 list_view = GTK_TREE_VIEW(dispheader.headers_list_view);
596 if (prefs_display_header_find_header(list_view, entry_text) != -1) {
597 alertpanel_error(_("This header is already in the list."));
598 return;
601 dp = g_new0(DisplayHeaderProp, 1);
603 dp->name = g_strdup(entry_text);
604 dp->hidden = hidden;
606 model = gtk_tree_view_get_model(list_view);
607 prefs_display_header_insert_header(GTK_LIST_STORE(model),
608 dp->name, dp);
610 prefs_display_header_set_list();
612 g_free(entry_text);
615 static void prefs_display_header_register_cb(GtkButton *btn,
616 gpointer hidden_data)
618 prefs_display_header_list_view_set_row(GPOINTER_TO_INT(hidden_data));
621 static void prefs_display_header_delete_cb(GtkButton *btn, gpointer list_view_data)
623 GtkTreeView *list_view = GTK_TREE_VIEW(list_view_data);
624 DisplayHeaderProp *dp;
625 GtkListStore *store = GTK_LIST_STORE(gtk_tree_view_get_model(list_view));
626 GtkTreeSelection *selection = gtk_tree_view_get_selection(list_view);
627 GtkTreeIter iter;
629 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
630 return;
632 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, PREFS_HDR_DATA, &dp, -1);
633 if (!dp)
634 return;
636 prefs_common.disphdr_list =
637 g_slist_remove(prefs_common.disphdr_list, dp);
638 display_header_prop_free(dp);
639 gtk_list_store_remove(store, &iter);
642 static void prefs_display_header_up(void)
644 GtkTreePath *prev, *sel, *try;
645 GtkTreeIter isel;
646 GtkListStore *store = NULL;
647 GtkTreeModel *model = NULL;
648 GtkTreeIter iprev;
650 if (!gtk_tree_selection_get_selected
651 (gtk_tree_view_get_selection
652 (GTK_TREE_VIEW(dispheader.headers_list_view)),
653 &model,
654 &isel))
655 return;
656 store = (GtkListStore *)model;
657 sel = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &isel);
658 if (!sel)
659 return;
661 /* no move if we're at row 0... */
662 try = gtk_tree_path_copy(sel);
663 if (!gtk_tree_path_prev(try)) {
664 gtk_tree_path_free(try);
665 gtk_tree_path_free(sel);
666 return;
669 prev = try;
670 gtk_tree_model_get_iter(GTK_TREE_MODEL(store),
671 &iprev, prev);
672 gtk_list_store_swap(store, &iprev, &isel);
674 gtk_tree_path_free(sel);
675 gtk_tree_path_free(prev);
678 static void prefs_display_header_down(void)
680 GtkListStore *store = NULL;
681 GtkTreeModel *model = NULL;
682 GtkTreeIter next, sel;
683 GtkTreePath *try;
685 if (!gtk_tree_selection_get_selected
686 (gtk_tree_view_get_selection
687 (GTK_TREE_VIEW(dispheader.headers_list_view)),
688 &model,
689 &sel))
690 return;
691 store = (GtkListStore *)model;
692 try = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &sel);
693 if (!try)
694 return;
696 next = sel;
697 if (gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &next))
698 gtk_list_store_swap(store, &next, &sel);
700 gtk_tree_path_free(try);
703 static gboolean prefs_display_header_key_pressed(GtkWidget *widget,
704 GdkEventKey *event,
705 gpointer data)
707 if (event && event->keyval == GDK_KEY_Escape)
708 prefs_display_header_cancel();
709 return FALSE;
712 static void prefs_display_header_ok(void)
714 prefs_common.show_other_header =
715 gtk_toggle_button_get_active
716 (GTK_TOGGLE_BUTTON(dispheader.other_headers));
717 prefs_display_header_write_config();
718 gtk_widget_hide(dispheader.window);
719 gtk_window_set_modal(GTK_WINDOW(dispheader.window), FALSE);
722 static void prefs_display_header_cancel(void)
724 prefs_display_header_read_config();
725 gtk_widget_hide(dispheader.window);
726 gtk_window_set_modal(GTK_WINDOW(dispheader.window), FALSE);
729 static gint prefs_display_header_deleted(GtkWidget *widget, GdkEventAny *event,
730 gpointer data)
732 prefs_display_header_cancel();
733 return TRUE;
736 static GtkListStore *prefs_display_header_create_store(void)
738 return gtk_list_store_new(N_PREFS_HDR_COLUMNS,
739 G_TYPE_STRING,
740 G_TYPE_POINTER,
741 -1);
744 static void prefs_display_header_insert_header(GtkListStore *store,
745 gchar *name,
746 DisplayHeaderProp *dp)
748 GtkTreeIter iter;
750 /* add new */
751 gtk_list_store_append(store, &iter);
752 gtk_list_store_set(store, &iter,
753 PREFS_HDR_HEADER,
754 name,
755 PREFS_HDR_DATA, dp,
756 -1);
759 static GtkWidget *prefs_display_header_list_view_create(const gchar *name)
761 GtkWidget *list_view;
762 GtkTreeSelection *selector;
763 GtkTreeModel *model;
765 model = GTK_TREE_MODEL(prefs_display_header_create_store());
766 list_view = gtk_tree_view_new_with_model(model);
767 g_object_unref(G_OBJECT(model));
769 selector = gtk_tree_view_get_selection(GTK_TREE_VIEW(list_view));
770 gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
772 prefs_filtering_create_list_view_columns(GTK_WIDGET(list_view), name);
774 return list_view;
777 static void prefs_filtering_create_list_view_columns(GtkWidget *list_view,
778 const gchar *name)
780 GtkTreeViewColumn *column;
781 GtkCellRenderer *renderer;
783 renderer = gtk_cell_renderer_text_new();
784 column = gtk_tree_view_column_new_with_attributes
785 (name, renderer, "text", PREFS_HDR_HEADER, NULL);
786 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
790 *\brief Called as a result of a gtk_list_store_swap()
792 static void headers_list_model_rows_reordered(GtkTreeModel *model,
793 GtkTreePath *path,
794 GtkTreeIter *iter,
795 gpointer arg,
796 GtkTreeView *list_view)
798 prefs_display_header_set_list();
802 *\brief Called as a result of a drag & drop
804 static void drag_end(GtkTreeView *list_view,
805 GdkDragContext *context,
806 gpointer data)
808 prefs_display_header_set_list();