4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 1999, Thomas Leonard, <tal197@ecs.soton.ac.uk>.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* options.c - code for handling user choices */
30 #include "gui_support.h"
34 /* Add OptionsSection structs to this list in your _init() functions */
35 GSList
*options_sections
= NULL
;
37 static GtkWidget
*window
, *sections_vbox
;
38 static FILE *save_file
= NULL
;
39 static GHashTable
*option_hash
= NULL
;
41 /* Static prototypes */
42 static void save_options(GtkWidget
*widget
, gpointer data
);
43 static char *process_option_line(guchar
*line
);
47 GtkWidget
*tl_vbox
, *scrolled_area
;
48 GtkWidget
*border
, *label
;
49 GtkWidget
*actions
, *button
;
50 char *string
, *save_path
;
52 window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
53 gtk_window_set_title(GTK_WINDOW(window
), "ROX-Filer options");
54 gtk_signal_connect(GTK_OBJECT(window
), "delete_event",
55 GTK_SIGNAL_FUNC(hide_dialog_event
), window
);
56 gtk_container_set_border_width(GTK_CONTAINER(window
), 4);
57 gtk_window_set_default_size(GTK_WINDOW(window
), 400, 400);
59 tl_vbox
= gtk_vbox_new(FALSE
, 4);
60 gtk_container_add(GTK_CONTAINER(window
), tl_vbox
);
62 scrolled_area
= gtk_scrolled_window_new(NULL
, NULL
);
63 gtk_container_set_border_width(GTK_CONTAINER(scrolled_area
), 4);
64 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_area
),
65 GTK_POLICY_NEVER
, GTK_POLICY_ALWAYS
);
66 gtk_box_pack_start(GTK_BOX(tl_vbox
), scrolled_area
, TRUE
, TRUE
, 0);
68 border
= gtk_frame_new(NULL
);
69 gtk_frame_set_shadow_type(GTK_FRAME(border
), GTK_SHADOW_NONE
);
70 gtk_container_set_border_width(GTK_CONTAINER(border
), 4);
71 gtk_scrolled_window_add_with_viewport(
72 GTK_SCROLLED_WINDOW(scrolled_area
), border
);
74 sections_vbox
= gtk_vbox_new(FALSE
, 4);
75 gtk_container_add(GTK_CONTAINER(border
), sections_vbox
);
77 save_path
= choices_find_path_save("...");
80 string
= g_strconcat("Choices will be saved as ",
83 label
= gtk_label_new(string
);
87 label
= gtk_label_new("Choices saving is disabled by "
88 "CHOICESPATH variable");
89 gtk_box_pack_start(GTK_BOX(tl_vbox
), label
, FALSE
, TRUE
, 0);
91 actions
= gtk_hbox_new(TRUE
, 16);
92 gtk_box_pack_start(GTK_BOX(tl_vbox
), actions
, FALSE
, TRUE
, 0);
94 button
= gtk_button_new_with_label("Save");
95 gtk_box_pack_start(GTK_BOX(actions
), button
, FALSE
, TRUE
, 0);
97 gtk_widget_set_sensitive(button
, FALSE
);
98 gtk_signal_connect(GTK_OBJECT(button
), "clicked",
99 GTK_SIGNAL_FUNC(save_options
), (gpointer
) TRUE
);
101 button
= gtk_button_new_with_label("OK");
102 gtk_box_pack_start(GTK_BOX(actions
), button
, FALSE
, TRUE
, 0);
103 gtk_signal_connect(GTK_OBJECT(button
), "clicked",
104 GTK_SIGNAL_FUNC(save_options
), (gpointer
) FALSE
);
106 button
= gtk_button_new_with_label("Cancel");
107 gtk_box_pack_start(GTK_BOX(actions
), button
, FALSE
, TRUE
, 0);
108 gtk_signal_connect_object(GTK_OBJECT(button
), "clicked",
109 GTK_SIGNAL_FUNC(gtk_widget_hide
), GTK_OBJECT(window
));
112 void options_load(void)
114 static gboolean need_init
= TRUE
;
120 GSList
*next
= options_sections
;
124 OptionsSection
*section
= (OptionsSection
*) next
->data
;
126 group
= gtk_frame_new(section
->name
);
127 gtk_box_pack_start(GTK_BOX(sections_vbox
), group
,
129 gtk_container_add(GTK_CONTAINER(group
),
137 path
= choices_find_path_load("options");
139 return; /* Nothing to load */
141 parse_file(path
, process_option_line
);
144 void parse_file(char *path
, ParseFunc
*parse_line
)
149 if (load_file(path
, &data
, &length
))
155 while (line
&& *line
)
157 eol
= strchr(line
, '\n');
161 error
= parse_line(line
);
167 message
= g_string_new(NULL
);
168 g_string_sprintf(message
,
169 "Error in options file at "
170 "line %d: %s", line_number
,
172 delayed_error("ROX-Filer", message
->str
);
173 g_string_free(message
, TRUE
);
186 /* Call this on init to register a handler for a key (thing before the = in
188 * The function returns a pointer to an error messages (which will
189 * NOT be free()d), or NULL on success.
191 void option_register(char *key
, OptionFunc
*func
)
194 option_hash
= g_hash_table_new(g_str_hash
, g_str_equal
);
195 g_hash_table_insert(option_hash
, key
, func
);
198 /* Process one line from the options file (\0 term'd).
199 * Returns NULL on success, or a pointer to an error message.
200 * The line is modified.
202 static char *process_option_line(guchar
*line
)
207 g_return_val_if_fail(option_hash
!= NULL
, "No registered functions!");
209 eq
= strchr(line
, '=');
211 return "Missing '='";
214 while (c
> line
&& (*c
== ' ' || *c
== '\t'))
218 while (*c
== ' ' || *c
== '\t')
221 func
= (OptionFunc
*) g_hash_table_lookup(option_hash
, line
);
223 return "Bad key (no such option name)";
228 static void save_options(GtkWidget
*widget
, gpointer data
)
230 gboolean save
= (gboolean
) data
;
231 GSList
*next
= options_sections
;
235 OptionsSection
*section
= (OptionsSection
*) next
->data
;
244 path
= choices_find_path_save("options");
245 g_return_if_fail(path
!= NULL
);
247 save_file
= fopen(path
, "wb");
251 str
= g_strconcat("Unable to open '", path
,
255 report_error("ROX-Filer", str
);
260 next
= options_sections
;
263 OptionsSection
*section
= (OptionsSection
*) next
->data
;
268 if (save_file
&& fclose(save_file
) == EOF
)
270 report_error("ROX-Filer", g_strerror(errno
));
275 gtk_widget_hide(window
);
278 void options_show(FilerWindow
*filer_window
)
280 GSList
*next
= options_sections
;
282 if (GTK_WIDGET_MAPPED(window
))
283 gtk_widget_hide(window
);
287 OptionsSection
*section
= (OptionsSection
*) next
->data
;
292 gtk_widget_show_all(window
);
295 void option_write(char *name
, char *value
)
301 return; /* Error already reported hopefully */
303 string
= g_strconcat(name
, " = ", value
, "\n", NULL
);
304 len
= strlen(string
);
305 if (fwrite(string
, sizeof(char), len
, save_file
) < len
)
307 delayed_error("Saving options", g_strerror(errno
));