1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2009, 2010 Free Software Foundation
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 This module provides an interface for simple user preference config
29 #include "psppire-conf.h"
31 G_DEFINE_TYPE (PsppireConf, psppire_conf, G_TYPE_OBJECT)
33 static void psppire_conf_finalize (GObject *object);
34 static void psppire_conf_dispose (GObject *object);
36 static GObjectClass *parent_class = NULL;
39 conf_read (PsppireConf *conf)
41 g_key_file_load_from_file (conf->keyfile,
43 G_KEY_FILE_KEEP_COMMENTS,
48 flush_conf (PsppireConf *conf)
52 gchar *kf = g_key_file_to_data (conf->keyfile, &length, NULL);
55 if (! g_file_set_contents (conf->filename, kf, length, &err))
57 g_warning ("Cannot open %s for writing: %s", conf->filename, err->message);
67 conf_write (PsppireConf *conf)
70 conf->idle = g_idle_add_full (G_PRIORITY_LOW,
71 (GSourceFunc) flush_conf, conf, NULL);
76 psppire_conf_dispose (GObject *object)
81 psppire_conf_finalize (GObject *object)
83 PsppireConf *conf = PSPPIRE_CONF (object);
84 g_key_file_free (conf->keyfile);
85 g_free (conf->filename);
89 static PsppireConf *the_instance = NULL;
92 psppire_conf_construct (GType type,
93 guint n_construct_params,
94 GObjectConstructParam *construct_params)
100 object = G_OBJECT_CLASS (parent_class)->constructor (type,
103 the_instance = PSPPIRE_CONF (object);
106 object = g_object_ref (G_OBJECT (the_instance));
112 psppire_conf_class_init (PsppireConfClass *class)
114 GObjectClass *object_class;
116 parent_class = g_type_class_peek_parent (class);
117 object_class = G_OBJECT_CLASS (class);
119 object_class->finalize = psppire_conf_finalize;
120 object_class->dispose = psppire_conf_dispose;
121 object_class->constructor = psppire_conf_construct;
126 psppire_conf_init (PsppireConf *conf)
128 const gchar *dirname;
131 /* Get the name of the directory for user configuration files, then, if it
132 doesn't already exist, create it, since we might be the first program
133 to want to put files there. */
134 dirname = g_get_user_config_dir ();
135 if (stat (dirname, &s) == -1 && errno == ENOENT)
136 mkdir (dirname, 0700);
138 conf->filename = g_strdup_printf ("%s/%s", dirname, "psppirerc");
140 conf->keyfile = g_key_file_new ();
147 psppire_conf_new (void)
149 return g_object_new (psppire_conf_get_type (), NULL);
155 psppire_conf_get_int (PsppireConf *conf, const gchar *base,
156 const gchar *name, gint *value)
161 *value = g_key_file_get_integer (conf->keyfile,
173 psppire_conf_get_boolean (PsppireConf *conf, const gchar *base,
174 const gchar *name, gboolean *value)
180 b = g_key_file_get_boolean (conf->keyfile,
197 psppire_conf_get_string (PsppireConf *conf, const gchar *base,
198 const gchar *name, gchar **value)
204 b = g_key_file_get_string (conf->keyfile,
222 psppire_conf_get_variant (PsppireConf *conf, const gchar *base,
223 const gchar *name, GVariant **v)
229 b = g_key_file_get_string (conf->keyfile,
239 *v = g_variant_parse (NULL, b, NULL, NULL, NULL);
247 psppire_conf_get_enum (PsppireConf *conf, const gchar *base,
256 b = g_key_file_get_string (conf->keyfile,
266 GEnumClass *ec = g_type_class_ref (t);
267 GEnumValue *ev = g_enum_get_value_by_nick (ec, b);
269 g_type_class_unref (ec);
277 psppire_conf_set_int (PsppireConf *conf,
278 const gchar *base, const gchar *name,
281 g_key_file_set_integer (conf->keyfile, base, name, value);
286 psppire_conf_set_boolean (PsppireConf *conf,
287 const gchar *base, const gchar *name,
290 g_key_file_set_boolean (conf->keyfile, base, name, value);
296 psppire_conf_set_string (PsppireConf *conf,
297 const gchar *base, const gchar *name,
300 g_key_file_set_string (conf->keyfile, base, name, value);
305 psppire_conf_set_variant (PsppireConf *conf,
306 const gchar *base, const gchar *name,
309 gchar *v = g_variant_print (value, FALSE);
310 g_key_file_set_string (conf->keyfile, base, name, v);
316 psppire_conf_set_enum (PsppireConf *conf,
317 const gchar *base, const gchar *name,
321 GEnumClass *ec = g_type_class_ref (enum_type);
322 GEnumValue *ev = g_enum_get_value (ec, value);
324 g_key_file_set_string (conf->keyfile, base, name,
327 g_type_class_unref (ec);
335 A convenience function to set the geometry of a
336 window from from a saved config
339 psppire_conf_set_window_geometry (PsppireConf *conf,
347 if (psppire_conf_get_int (conf, base, "height", &height)
349 psppire_conf_get_int (conf, base, "width", &width))
351 gtk_window_set_default_size (window, width, height);
354 if (psppire_conf_get_int (conf, base, "x", &x)
356 psppire_conf_get_int (conf, base, "y", &y))
358 gtk_window_move (window, x, y);
361 if (psppire_conf_get_boolean (conf, base, "maximize", &maximize))
364 gtk_window_maximize (window);
366 gtk_window_unmaximize (window);
372 A convenience function to save the window geometry.
373 This should typically be called from a window's
374 "configure-event" and "window-state-event" signal handlers
377 psppire_conf_save_window_geometry (PsppireConf *conf,
379 GtkWindow *gtk_window)
384 w = gtk_widget_get_window (GTK_WIDGET (gtk_window));
388 maximized = (gdk_window_get_state (w) & GDK_WINDOW_STATE_MAXIMIZED) != 0;
389 psppire_conf_set_boolean (conf, base, "maximize", maximized);
395 gint width = gdk_window_get_width (w);
396 gint height= gdk_window_get_height (w);
398 gdk_window_get_position (w, &x, &y);
400 psppire_conf_set_int (conf, base, "height", height);
401 psppire_conf_set_int (conf, base, "width", width);
402 psppire_conf_set_int (conf, base, "x", x);
403 psppire_conf_set_int (conf, base, "y", y);