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 static void psppire_conf_init (PsppireConf *conf);
32 static void psppire_conf_class_init (PsppireConfClass *class);
34 static void psppire_conf_finalize (GObject *object);
35 static void psppire_conf_dispose (GObject *object);
37 static GObjectClass *parent_class = NULL;
41 psppire_conf_get_type (void)
43 static GType conf_type = 0;
47 static const GTypeInfo conf_info =
49 sizeof (PsppireConfClass),
51 NULL, /* base_finalize */
52 (GClassInitFunc) psppire_conf_class_init,
53 NULL, /* class_finalize */
54 NULL, /* class_data */
57 (GInstanceInitFunc) psppire_conf_init,
60 conf_type = g_type_register_static (G_TYPE_OBJECT,
70 conf_read (PsppireConf *conf)
72 g_key_file_load_from_file (conf->keyfile,
74 G_KEY_FILE_KEEP_COMMENTS,
79 flush_conf (PsppireConf *conf)
83 gchar *kf = g_key_file_to_data (conf->keyfile, &length, NULL);
85 if (! g_file_set_contents (conf->filename, kf, length, NULL))
87 g_warning ("Cannot open %s for writing", conf->filename);
96 conf_write (PsppireConf *conf)
99 conf->idle = g_idle_add_full (G_PRIORITY_LOW,
100 (GSourceFunc) flush_conf, conf, NULL);
105 psppire_conf_dispose (GObject *object)
110 psppire_conf_finalize (GObject *object)
112 PsppireConf *conf = PSPPIRE_CONF (object);
113 g_key_file_free (conf->keyfile);
114 g_free (conf->filename);
118 static PsppireConf *the_instance = NULL;
121 psppire_conf_construct (GType type,
122 guint n_construct_params,
123 GObjectConstructParam *construct_params)
129 object = G_OBJECT_CLASS (parent_class)->constructor (type,
132 the_instance = PSPPIRE_CONF (object);
135 object = g_object_ref (G_OBJECT (the_instance));
141 psppire_conf_class_init (PsppireConfClass *class)
143 GObjectClass *object_class;
145 parent_class = g_type_class_peek_parent (class);
146 object_class = (GObjectClass*) class;
148 object_class->finalize = psppire_conf_finalize;
149 object_class->dispose = psppire_conf_dispose;
150 object_class->constructor = psppire_conf_construct;
155 psppire_conf_init (PsppireConf *conf)
157 const gchar *dirname;
160 /* Get the name of the directory for user configuration files, then, if it
161 doesn't already exist, create it, since we might be the first program
162 to want to put files there. */
163 dirname = g_get_user_config_dir ();
164 if (stat (dirname, &s) == -1 && errno == ENOENT)
165 mkdir (dirname, 0700);
167 conf->filename = g_strdup_printf ("%s/%s", dirname, "psppirerc");
169 conf->keyfile = g_key_file_new ();
176 psppire_conf_new (void)
178 return g_object_new (psppire_conf_get_type (), NULL);
184 psppire_conf_get_int (PsppireConf *conf, const gchar *base,
185 const gchar *name, gint *value)
190 *value = g_key_file_get_integer (conf->keyfile,
202 psppire_conf_get_boolean (PsppireConf *conf, const gchar *base,
203 const gchar *name, gboolean *value)
209 b = g_key_file_get_boolean (conf->keyfile,
226 psppire_conf_get_string (PsppireConf *conf, const gchar *base,
227 const gchar *name, gchar **value)
233 b = g_key_file_get_string (conf->keyfile,
251 psppire_conf_get_variant (PsppireConf *conf, const gchar *base,
252 const gchar *name, GVariant **v)
258 b = g_key_file_get_string (conf->keyfile,
268 *v = g_variant_parse (NULL, b, NULL, NULL, NULL);
276 psppire_conf_get_enum (PsppireConf *conf, const gchar *base,
285 b = g_key_file_get_string (conf->keyfile,
295 GEnumClass *ec = g_type_class_ref (t);
296 GEnumValue *ev = g_enum_get_value_by_nick (ec, b);
298 g_type_class_unref (ec);
306 psppire_conf_set_int (PsppireConf *conf,
307 const gchar *base, const gchar *name,
310 g_key_file_set_integer (conf->keyfile, base, name, value);
315 psppire_conf_set_boolean (PsppireConf *conf,
316 const gchar *base, const gchar *name,
319 g_key_file_set_boolean (conf->keyfile, base, name, value);
325 psppire_conf_set_string (PsppireConf *conf,
326 const gchar *base, const gchar *name,
329 g_key_file_set_string (conf->keyfile, base, name, value);
334 psppire_conf_set_variant (PsppireConf *conf,
335 const gchar *base, const gchar *name,
338 gchar *v = g_variant_print (value, FALSE);
339 g_key_file_set_string (conf->keyfile, base, name, v);
345 psppire_conf_set_enum (PsppireConf *conf,
346 const gchar *base, const gchar *name,
350 GEnumClass *ec = g_type_class_ref (enum_type);
351 GEnumValue *ev = g_enum_get_value (ec, value);
353 g_key_file_set_string (conf->keyfile, base, name,
356 g_type_class_unref (ec);
364 A convenience function to set the geometry of a
365 window from from a saved config
368 psppire_conf_set_window_geometry (PsppireConf *conf,
376 if (psppire_conf_get_int (conf, base, "height", &height)
378 psppire_conf_get_int (conf, base, "width", &width))
380 gtk_window_set_default_size (window, width, height);
383 if (psppire_conf_get_int (conf, base, "x", &x)
385 psppire_conf_get_int (conf, base, "y", &y))
387 gtk_window_move (window, x, y);
390 if (psppire_conf_get_boolean (conf, base, "maximize", &maximize))
393 gtk_window_maximize (window);
395 gtk_window_unmaximize (window);
401 A convenience function to save the window geometry.
402 This should typically be called from a window's
403 "configure-event" and "window-state-event" signal handlers
406 psppire_conf_save_window_geometry (PsppireConf *conf,
408 GtkWindow *gtk_window)
413 w = gtk_widget_get_window (GTK_WIDGET (gtk_window));
417 maximized = (gdk_window_get_state (w) & GDK_WINDOW_STATE_MAXIMIZED) != 0;
418 psppire_conf_set_boolean (conf, base, "maximize", maximized);
424 gint width = gdk_window_get_width (w);
425 gint height= gdk_window_get_height (w);
427 gdk_window_get_position (w, &x, &y);
429 psppire_conf_set_int (conf, base, "height", height);
430 psppire_conf_set_int (conf, base, "width", width);
431 psppire_conf_set_int (conf, base, "x", x);
432 psppire_conf_set_int (conf, base, "y", y);