X-Git-Url: http://pileus.org/git/?p=grits;a=blobdiff_plain;f=src%2Fgis-prefs.c;h=d3d3783c0af6795949b7f43bc5976f16d8bf0d9f;hp=d7d7d6d7e96a4dbe11e5aea7f1f83ab370292e8d;hb=cd2b3c70807c914687f9051b34a8fe029de300ee;hpb=becee285e152746e64b6d3984e2a7229f664062d diff --git a/src/gis-prefs.c b/src/gis-prefs.c index d7d7d6d..d3d3783 100644 --- a/src/gis-prefs.c +++ b/src/gis-prefs.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 Andy Spencer + * Copyright (C) 2009-2010 Andy Spencer * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -33,68 +33,71 @@ static guint signals[NUM_SIGNALS]; GisPrefs *gis_prefs_new(const gchar *config, const gchar *defaults) { g_debug("GisPrefs: new - %s, %s", config, defaults); - GisPrefs *self = g_object_new(GIS_TYPE_PREFS, NULL); + GisPrefs *prefs = g_object_new(GIS_TYPE_PREFS, NULL); if (config) - self->key_path = g_strdup(config); + prefs->key_path = g_strdup(config); else - self->key_path = g_build_filename(g_get_user_config_dir(), + prefs->key_path = g_build_filename(g_get_user_config_dir(), PACKAGE, "config.ini", NULL); GError *error = NULL; - g_key_file_load_from_file(self->key_file, self->key_path, + g_key_file_load_from_file(prefs->key_file, prefs->key_path, G_KEY_FILE_KEEP_COMMENTS, &error); if (error && defaults) { - g_debug("GisPrefs: new - Trying %s defaults", defaults); + g_debug("GisPrefs: new - Trying defaults"); g_clear_error(&error); - g_key_file_load_from_file(self->key_file, defaults, + g_key_file_load_from_file(prefs->key_file, defaults, G_KEY_FILE_KEEP_COMMENTS, &error); } if (error) { g_debug("GisPrefs: new - Trying GIS defaults"); g_clear_error(&error); gchar *tmp = g_build_filename(PKGDATADIR, "defaults.ini", NULL); - g_key_file_load_from_file(self->key_file, tmp, + g_key_file_load_from_file(prefs->key_file, tmp, G_KEY_FILE_KEEP_COMMENTS, &error); g_free(tmp); } if (error) { - g_warning("GisPrefs: new - Unable to load key file `%s': %s", - self->key_path, error->message); + g_debug("GisPrefs: new - Unable to load key file `%s': %s", + prefs->key_path, error->message); } - return self; + g_debug("GisPrefs: new - using %s", prefs->key_path); + return prefs; } #define make_pref_type(name, c_type, g_type) \ -c_type gis_prefs_get_##name##_v(GisPrefs *self, \ - const gchar *group, const gchar *key) \ +c_type gis_prefs_get_##name##_v(GisPrefs *prefs, \ + const gchar *group, const gchar *key, GError **_error) \ { \ GError *error = NULL; \ - c_type value = g_key_file_get_##name(self->key_file, group, key, &error); \ + c_type value = g_key_file_get_##name(prefs->key_file, group, key, &error); \ if (error && error->code != G_KEY_FILE_ERROR_GROUP_NOT_FOUND) \ g_warning("GisPrefs: get_value_##name - error getting key %s: %s\n", \ key, error->message); \ + if (error && _error) \ + *_error = error; \ return value; \ } \ -c_type gis_prefs_get_##name(GisPrefs *self, const gchar *key) \ +c_type gis_prefs_get_##name(GisPrefs *prefs, const gchar *key, GError **error) \ { \ gchar **keys = g_strsplit(key, "/", 2); \ - c_type value = gis_prefs_get_##name##_v(self, keys[0], keys[1]); \ + c_type value = gis_prefs_get_##name##_v(prefs, keys[0], keys[1], error); \ g_strfreev(keys); \ return value; \ } \ \ -void gis_prefs_set_##name##_v(GisPrefs *self, \ +void gis_prefs_set_##name##_v(GisPrefs *prefs, \ const gchar *group, const gchar *key, const c_type value) \ { \ - g_key_file_set_##name(self->key_file, group, key, value); \ + g_key_file_set_##name(prefs->key_file, group, key, value); \ gchar *all = g_strconcat(group, "/", key, NULL); \ - g_signal_emit(self, signals[SIG_PREF_CHANGED], 0, \ + g_signal_emit(prefs, signals[SIG_PREF_CHANGED], 0, \ all, g_type, &value); \ g_free(all); \ } \ -void gis_prefs_set_##name(GisPrefs *self, const gchar *key, const c_type value) \ +void gis_prefs_set_##name(GisPrefs *prefs, const gchar *key, const c_type value) \ { \ gchar **keys = g_strsplit(key, "/", 2); \ - gis_prefs_set_##name##_v(self, keys[0], keys[1], value); \ + gis_prefs_set_##name##_v(prefs, keys[0], keys[1], value); \ g_strfreev(keys); \ } \ @@ -108,48 +111,34 @@ make_pref_type(double, gdouble, G_TYPE_DOUBLE) * GObject code * ****************/ G_DEFINE_TYPE(GisPrefs, gis_prefs, G_TYPE_OBJECT); -static void gis_prefs_init(GisPrefs *self) +static void gis_prefs_init(GisPrefs *prefs) { g_debug("GisPrefs: init"); - self->key_file = g_key_file_new(); + prefs->key_file = g_key_file_new(); } -static GObject *gis_prefs_constructor(GType gtype, guint n_properties, - GObjectConstructParam *properties) -{ - g_debug("GisPrefs: constructor"); - GObjectClass *parent_class = G_OBJECT_CLASS(gis_prefs_parent_class); - return parent_class->constructor(gtype, n_properties, properties); -} -static void gis_prefs_dispose(GObject *_self) +static void gis_prefs_dispose(GObject *_prefs) { g_debug("GisPrefs: dispose"); - GisPrefs *self = GIS_PREFS(_self); - if (self->key_file) { + GisPrefs *prefs = GIS_PREFS(_prefs); + if (prefs->key_file) { gsize length; - gchar *dir = g_path_get_dirname(self->key_path); + gchar *dir = g_path_get_dirname(prefs->key_path); g_mkdir_with_parents(dir, 0755); - gchar *data = g_key_file_to_data(self->key_file, &length, NULL); - g_file_set_contents(self->key_path, data, length, NULL); - g_key_file_free(self->key_file); - g_free(self->key_path); + gchar *data = g_key_file_to_data(prefs->key_file, &length, NULL); + g_file_set_contents(prefs->key_path, data, length, NULL); + g_key_file_free(prefs->key_file); + g_free(prefs->key_path); g_free(dir); g_free(data); - self->key_file = NULL; + prefs->key_file = NULL; } - G_OBJECT_CLASS(gis_prefs_parent_class)->dispose(_self); -} -static void gis_prefs_finalize(GObject *_self) -{ - g_debug("GisPrefs: finalize"); - G_OBJECT_CLASS(gis_prefs_parent_class)->finalize(_self); + G_OBJECT_CLASS(gis_prefs_parent_class)->dispose(_prefs); } static void gis_prefs_class_init(GisPrefsClass *klass) { g_debug("GisPrefs: class_init"); GObjectClass *gobject_class = G_OBJECT_CLASS(klass); - gobject_class->constructor = gis_prefs_constructor; - gobject_class->dispose = gis_prefs_dispose; - gobject_class->finalize = gis_prefs_finalize; + gobject_class->dispose = gis_prefs_dispose; signals[SIG_PREF_CHANGED] = g_signal_new( "pref-changed", G_TYPE_FROM_CLASS(gobject_class), @@ -159,7 +148,7 @@ static void gis_prefs_class_init(GisPrefsClass *klass) NULL, gis_cclosure_marshal_VOID__STRING_UINT_POINTER, G_TYPE_NONE, - 1, + 3, G_TYPE_STRING, G_TYPE_UINT, G_TYPE_POINTER);