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_key_file_free (conf->keyfile);
99 g_free (conf->filename);
103 static PsppireConf *the_instance = NULL;
106 psppire_conf_construct (GType type,
107 guint n_construct_params,
108 GObjectConstructParam *construct_params)
114 object = G_OBJECT_CLASS (parent_class)->constructor (type,
117 the_instance = PSPPIRE_CONF (object);
120 object = g_object_ref (G_OBJECT (the_instance));
126 psppire_conf_class_init (PsppireConfClass *class)
128 GObjectClass *object_class;
130 parent_class = g_type_class_peek_parent (class);
131 object_class = (GObjectClass*) class;
133 object_class->finalize = psppire_conf_finalize;
134 object_class->dispose = psppire_conf_dispose;
135 object_class->constructor = psppire_conf_construct;
141 psppire_conf_init (PsppireConf *conf)
143 const gchar *dirname = g_get_user_config_dir ();
145 conf->filename = g_strdup_printf ("%s/%s", dirname, "psppirerc");
147 conf->keyfile = g_key_file_new ();
149 conf->dispose_has_run = FALSE;
154 psppire_conf_new (void)
156 return g_object_new (psppire_conf_get_type (), NULL);
162 psppire_conf_get_int (PsppireConf *conf, const gchar *base,
163 const gchar *name, gint *value)
168 *value = g_key_file_get_integer (conf->keyfile,
180 psppire_conf_get_boolean (PsppireConf *conf, const gchar *base,
181 const gchar *name, gboolean *value)
187 b = g_key_file_get_boolean (conf->keyfile,
203 psppire_conf_set_int (PsppireConf *conf,
204 const gchar *base, const gchar *name,
207 g_key_file_set_integer (conf->keyfile, base, name, value);
212 psppire_conf_set_boolean (PsppireConf *conf,
213 const gchar *base, const gchar *name,
216 g_key_file_set_boolean (conf->keyfile, base, name, value);
221 A convenience function to set the geometry of a
222 window from from a saved config
225 psppire_conf_set_window_geometry (PsppireConf *conf,
233 if (psppire_conf_get_int (conf, base, "height", &height)
235 psppire_conf_get_int (conf, base, "width", &width) )
237 gtk_window_set_default_size (window, width, height);
240 if ( psppire_conf_get_int (conf, base, "x", &x)
242 psppire_conf_get_int (conf, base, "y", &y) )
244 gtk_window_move (window, x, y);
247 if ( psppire_conf_get_boolean (conf, base, "maximize", &maximize))
250 gtk_window_maximize (window);
252 gtk_window_unmaximize (window);
258 A convenience function to save the window geometry.
259 This should typically be called from a window's
260 "configure-event" and "window-state-event" signal handlers
263 psppire_conf_save_window_geometry (PsppireConf *conf,
271 GdkEventConfigure *event = &e->configure;
273 if ( gdk_window_get_state (event->window) &
274 GDK_WINDOW_STATE_MAXIMIZED )
277 psppire_conf_set_int (conf, base, "height", event->height);
278 psppire_conf_set_int (conf, base, "width", event->width);
280 psppire_conf_set_int (conf, base, "x", event->x);
281 psppire_conf_set_int (conf, base, "y", event->y);
284 case GDK_WINDOW_STATE:
286 GdkEventWindowState *event = &e->window_state;
288 psppire_conf_set_boolean (conf, base, "maximize",
289 event->new_window_state &
290 GDK_WINDOW_STATE_MAXIMIZED );