1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2009 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
25 #include "psppire-conf.h"
27 static void psppire_conf_init (PsppireConf *conf);
28 static void psppire_conf_class_init (PsppireConfClass *class);
30 static void psppire_conf_finalize (GObject *object);
31 static void psppire_conf_dispose (GObject *object);
33 static GObjectClass *parent_class = NULL;
37 psppire_conf_get_type (void)
39 static GType conf_type = 0;
43 static const GTypeInfo conf_info =
45 sizeof (PsppireConfClass),
47 NULL, /* base_finalize */
48 (GClassInitFunc) psppire_conf_class_init,
49 NULL, /* class_finalize */
50 NULL, /* class_data */
53 (GInstanceInitFunc) psppire_conf_init,
56 conf_type = g_type_register_static (G_TYPE_OBJECT,
66 conf_read (PsppireConf *conf)
68 g_key_file_load_from_file (conf->keyfile,
70 G_KEY_FILE_KEEP_COMMENTS,
75 conf_write (PsppireConf *conf)
79 gchar *kf = g_key_file_to_data (conf->keyfile, &length, NULL);
81 if ( ! g_file_set_contents (conf->filename, kf, length, NULL) )
83 g_warning ("Cannot open %s for writing", conf->filename);
90 psppire_conf_dispose (GObject *object)
95 psppire_conf_finalize (GObject *object)
97 PsppireConf *conf = PSPPIRE_CONF (object);
98 g_mutex_free (conf->mutex);
99 g_key_file_free (conf->keyfile);
100 g_free (conf->filename);
104 static PsppireConf *the_instance = NULL;
107 psppire_conf_construct (GType type,
108 guint n_construct_params,
109 GObjectConstructParam *construct_params)
115 object = G_OBJECT_CLASS (parent_class)->constructor (type,
118 the_instance = PSPPIRE_CONF (object);
121 object = g_object_ref (G_OBJECT (the_instance));
127 psppire_conf_class_init (PsppireConfClass *class)
129 GObjectClass *object_class;
131 parent_class = g_type_class_peek_parent (class);
132 object_class = (GObjectClass*) class;
134 object_class->finalize = psppire_conf_finalize;
135 object_class->dispose = psppire_conf_dispose;
136 object_class->constructor = psppire_conf_construct;
141 psppire_conf_init (PsppireConf *conf)
143 const gchar *dirname = g_get_user_config_dir ();
145 conf->mutex = g_mutex_new ();
147 conf->filename = g_strdup_printf ("%s/%s", dirname, "psppirerc");
149 conf->keyfile = g_key_file_new ();
151 conf->dispose_has_run = FALSE;
156 psppire_conf_new (void)
158 return g_object_new (psppire_conf_get_type (), NULL);
164 psppire_conf_get_int (PsppireConf *conf, const gchar *base,
165 const gchar *name, gint *value)
170 g_mutex_lock (conf->mutex);
172 *value = g_key_file_get_integer (conf->keyfile,
181 g_mutex_unlock (conf->mutex);
187 psppire_conf_get_boolean (PsppireConf *conf, const gchar *base,
188 const gchar *name, gboolean *value)
193 g_mutex_lock (conf->mutex);
196 b = g_key_file_get_boolean (conf->keyfile,
207 g_mutex_unlock (conf->mutex);
214 psppire_conf_set_int (PsppireConf *conf,
215 const gchar *base, const gchar *name,
218 g_mutex_lock (conf->mutex);
219 g_key_file_set_integer (conf->keyfile, base, name, value);
221 g_mutex_unlock (conf->mutex);
225 psppire_conf_set_boolean (PsppireConf *conf,
226 const gchar *base, const gchar *name,
229 g_mutex_lock (conf->mutex);
230 g_key_file_set_boolean (conf->keyfile, base, name, value);
232 g_mutex_unlock (conf->mutex);
236 A convenience function to set the geometry of a
237 window from from a saved config
240 psppire_conf_set_window_geometry (PsppireConf *conf,
248 if (psppire_conf_get_int (conf, base, "height", &height)
250 psppire_conf_get_int (conf, base, "width", &width) )
252 gtk_window_set_default_size (window, width, height);
255 if ( psppire_conf_get_int (conf, base, "x", &x)
257 psppire_conf_get_int (conf, base, "y", &y) )
259 gtk_window_move (window, x, y);
262 if ( psppire_conf_get_boolean (conf, base, "maximize", &maximize))
265 gtk_window_maximize (window);
267 gtk_window_unmaximize (window);
273 A convenience function to save the window geometry.
274 This should typically be called from a window's
275 "configure-event" and "window-state-event" signal handlers
278 psppire_conf_save_window_geometry (PsppireConf *conf,
286 GdkEventConfigure *event = &e->configure;
288 if ( gdk_window_get_state (event->window) &
289 GDK_WINDOW_STATE_MAXIMIZED )
292 psppire_conf_set_int (conf, base, "height", event->height);
293 psppire_conf_set_int (conf, base, "width", event->width);
295 psppire_conf_set_int (conf, base, "x", event->x);
296 psppire_conf_set_int (conf, base, "y", event->y);
299 case GDK_WINDOW_STATE:
301 GdkEventWindowState *event = &e->window_state;
303 psppire_conf_set_boolean (conf, base, "maximize",
304 event->new_window_state &
305 GDK_WINDOW_STATE_MAXIMIZED );