[glib/wip/plist] basic nextstep settings backend
- From: William Hua <williamhua src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib/wip/plist] basic nextstep settings backend
- Date: Tue, 27 Dec 2011 06:29:28 +0000 (UTC)
commit 7df3c9852d87800aeeb97a22893c1240ef0c8c02
Author: William Hua <william attente ca>
Date: Mon Dec 26 10:04:38 2011 -0500
basic nextstep settings backend
gio/Makefile.am | 12 +
gio/giomodule.c | 7 +
gio/gnextstepsettingsbackend.c | 528 ++++++++++++++++++++++++++++++++++++++++
gio/gnextstepsettingsbackend.h | 34 +++
gio/gsettingsbackend.c | 2 +-
gio/gsettingsbackend.h | 4 +
6 files changed, 586 insertions(+), 1 deletions(-)
---
diff --git a/gio/Makefile.am b/gio/Makefile.am
index ef5869e..ed444f4 100644
--- a/gio/Makefile.am
+++ b/gio/Makefile.am
@@ -124,6 +124,14 @@ settings_sources += \
gregistrysettingsbackend.c
endif
+if OS_CARBON
+AM_CPPFLAGS += -DG_OS_CARBON
+
+settings_sources += \
+ gnextstepsettingsbackend.h \
+ gnextstepsettingsbackend.c
+endif
+
application_headers = \
gapplication.h \
gapplicationcommandline.h \
@@ -447,6 +455,10 @@ libgio_2_0_la_LIBADD = \
libgio_2_0_la_CPPFLAGS = $(ZLIB_CFLAGS) $(AM_CPPFLAGS)
+if OS_CARBON
+libgio_2_0_la_CFLAGS = -xobjective-c
+endif
+
if PLATFORM_WIN32
no_undefined = -no-undefined
endif
diff --git a/gio/giomodule.c b/gio/giomodule.c
index efd8388..9469fa6 100644
--- a/gio/giomodule.c
+++ b/gio/giomodule.c
@@ -46,6 +46,10 @@
#include "gdesktopappinfo.h"
#endif
+#ifdef G_OS_CARBON
+#include "gnextstepsettingsbackend.h"
+#endif
+
/**
* SECTION:giomodule
* @short_description: Loadable GIO Modules
@@ -916,6 +920,9 @@ _g_io_modules_ensure_loaded (void)
g_win32_directory_monitor_get_type ();
g_registry_backend_get_type ();
#endif
+#ifdef G_OS_CARBON
+ g_nextstep_settings_backend_get_type ();
+#endif
#ifdef G_OS_UNIX
_g_unix_volume_monitor_get_type ();
#endif
diff --git a/gio/gnextstepsettingsbackend.c b/gio/gnextstepsettingsbackend.c
new file mode 100644
index 0000000..a746c14
--- /dev/null
+++ b/gio/gnextstepsettingsbackend.c
@@ -0,0 +1,528 @@
+#include "gnextstepsettingsbackend.h"
+#include "gsimplepermission.h"
+#include "giomodule.h"
+
+
+
+#import <Foundation/Foundation.h>
+
+
+
+typedef GSettingsBackendClass GNextstepSettingsBackendClass;
+
+
+
+struct _GNextstepSettingsBackend
+{
+ GSettingsBackend parent_instance;
+
+ /*< private >*/
+ NSUserDefaults *user_defaults;
+};
+
+
+
+G_DEFINE_TYPE_WITH_CODE (GNextstepSettingsBackend,
+ g_nextstep_settings_backend,
+ G_TYPE_SETTINGS_BACKEND,
+ g_io_extension_point_implement (G_SETTINGS_BACKEND_EXTENSION_POINT_NAME,
+ g_define_type_id, "nextstep", 90));
+
+
+
+static void g_nextstep_settings_backend_finalize (GObject *backend);
+
+static GVariant * g_nextstep_settings_backend_read (GSettingsBackend *backend,
+ const gchar *key,
+ const GVariantType *expected_type,
+ gboolean default_value);
+
+static gboolean g_nextstep_settings_backend_get_writable (GSettingsBackend *backend,
+ const gchar *key);
+
+static gboolean g_nextstep_settings_backend_write (GSettingsBackend *backend,
+ const gchar *key,
+ GVariant *value,
+ gpointer origin_tag);
+
+static gboolean g_nextstep_settings_backend_write_tree (GSettingsBackend *backend,
+ GTree *tree,
+ gpointer origin_tag);
+
+static void g_nextstep_settings_backend_reset (GSettingsBackend *backend,
+ const gchar *key,
+ gpointer origin_tag);
+
+static void g_nextstep_settings_backend_subscribe (GSettingsBackend *backend,
+ const gchar *name);
+
+static void g_nextstep_settings_backend_unsubscribe (GSettingsBackend *backend,
+ const gchar *name);
+
+static void g_nextstep_settings_backend_sync (GSettingsBackend *backend);
+
+static GPermission * g_nextstep_settings_backend_get_permission (GSettingsBackend *backend,
+ const gchar *path);
+
+static gboolean g_nextstep_settings_backend_write_pair (gpointer name,
+ gpointer value,
+ gpointer data);
+
+static GVariant * g_nextstep_settings_backend_get_g_variant (id object,
+ const GVariantType *type);
+
+static id g_nextstep_settings_backend_get_ns_object (GVariant *variant);
+
+
+
+GSettingsBackend *
+g_nextstep_settings_backend_new (void)
+{
+ return g_object_new (G_TYPE_NEXTSTEP_SETTINGS_BACKEND, NULL);
+}
+
+
+
+static void
+g_nextstep_settings_backend_class_init (GNextstepSettingsBackendClass *class)
+{
+ G_OBJECT_CLASS (class)->finalize = g_nextstep_settings_backend_finalize;
+ class->read = g_nextstep_settings_backend_read;
+ class->get_writable = g_nextstep_settings_backend_get_writable;
+ class->write = g_nextstep_settings_backend_write;
+ class->write_tree = g_nextstep_settings_backend_write_tree;
+ class->reset = g_nextstep_settings_backend_reset;
+ class->subscribe = g_nextstep_settings_backend_subscribe;
+ class->unsubscribe = g_nextstep_settings_backend_unsubscribe;
+ class->sync = g_nextstep_settings_backend_sync;
+ class->get_permission = g_nextstep_settings_backend_get_permission;
+}
+
+
+
+static void
+g_nextstep_settings_backend_init (GNextstepSettingsBackend *self)
+{
+ NSAutoreleasePool *pool;
+
+ pool = [[NSAutoreleasePool alloc] init];
+
+ self->user_defaults = [[NSUserDefaults standardUserDefaults] retain];
+
+ [pool drain];
+}
+
+
+
+static void
+g_nextstep_settings_backend_finalize (GObject *self)
+{
+ NSAutoreleasePool *pool;
+
+ pool = [[NSAutoreleasePool alloc] init];
+
+ [G_NEXTSTEP_SETTINGS_BACKEND (self)->user_defaults release];
+
+ [pool drain];
+}
+
+
+
+static GVariant *
+g_nextstep_settings_backend_read (GSettingsBackend *backend,
+ const gchar *key,
+ const GVariantType *expected_type,
+ gboolean default_value)
+{
+ NSAutoreleasePool *pool;
+ NSUserDefaults *user_defaults;
+ NSString *name;
+ id value;
+ GVariant *variant;
+
+ pool = [[NSAutoreleasePool alloc] init];
+ user_defaults = G_NEXTSTEP_SETTINGS_BACKEND (backend)->user_defaults;
+ name = [NSString stringWithUTF8String:key];
+
+ if (default_value)
+ {
+ id old_value;
+
+ old_value = [[[user_defaults objectForKey:name] retain] autorelease];
+ [user_defaults removeObjectForKey:name];
+ value = [[[user_defaults objectForKey:name] retain] autorelease];
+
+ if (old_value != value)
+ [user_defaults setObject:old_value forKey:name];
+ }
+ else
+ value = [user_defaults objectForKey:name];
+
+ variant = g_nextstep_settings_backend_get_g_variant (value, expected_type);
+
+ [pool drain];
+
+ return variant;
+}
+
+
+
+static gboolean
+g_nextstep_settings_backend_get_writable (GSettingsBackend *backend,
+ const gchar *key)
+{
+ return TRUE;
+}
+
+
+
+static gboolean
+g_nextstep_settings_backend_write (GSettingsBackend *backend,
+ const gchar *key,
+ GVariant *value,
+ gpointer origin_tag)
+{
+ NSAutoreleasePool *pool;
+
+ pool = [[NSAutoreleasePool alloc] init];
+
+ g_nextstep_settings_backend_write_pair ((gpointer) key, value, backend);
+ g_settings_backend_changed (backend, key, origin_tag);
+
+ [pool drain];
+
+ return TRUE;
+}
+
+
+
+static gboolean
+g_nextstep_settings_backend_write_tree (GSettingsBackend *backend,
+ GTree *tree,
+ gpointer origin_tag)
+{
+ NSAutoreleasePool *pool;
+
+ pool = [[NSAutoreleasePool alloc] init];
+
+ g_tree_foreach (tree, g_nextstep_settings_backend_write_pair, backend);
+ g_settings_backend_changed_tree (backend, tree, origin_tag);
+
+ [pool drain];
+
+ return TRUE;
+}
+
+
+
+static void
+g_nextstep_settings_backend_reset (GSettingsBackend *backend,
+ const gchar *key,
+ gpointer origin_tag)
+{
+ NSAutoreleasePool *pool;
+ NSUserDefaults *user_defaults;
+ NSString *name;
+ id value;
+ id default_value;
+
+ pool = [[NSAutoreleasePool alloc] init];
+ user_defaults = G_NEXTSTEP_SETTINGS_BACKEND (backend)->user_defaults;
+ name = [NSString stringWithUTF8String:key];
+ value = [user_defaults objectForKey:name];
+
+ [user_defaults removeObjectForKey:name];
+
+ default_value = [user_defaults objectForKey:name];
+
+ if (default_value != value && ![default_value isEqual:value])
+ g_settings_backend_changed (backend, key, origin_tag);
+
+ [pool drain];
+}
+
+
+
+static void
+g_nextstep_settings_backend_subscribe (GSettingsBackend *backend,
+ const gchar *name)
+{
+}
+
+
+
+static void
+g_nextstep_settings_backend_unsubscribe (GSettingsBackend *backend,
+ const gchar *name)
+{
+}
+
+
+
+static void
+g_nextstep_settings_backend_sync (GSettingsBackend *backend)
+{
+ NSAutoreleasePool *pool;
+
+ pool = [[NSAutoreleasePool alloc] init];
+
+ [G_NEXTSTEP_SETTINGS_BACKEND (backend)->user_defaults synchronize];
+
+ [pool drain];
+}
+
+
+
+static GPermission *
+g_nextstep_settings_backend_get_permission (GSettingsBackend *backend,
+ const gchar *path)
+{
+ return g_simple_permission_new (TRUE);
+}
+
+
+
+static gboolean
+g_nextstep_settings_backend_write_pair (gpointer name,
+ gpointer value,
+ gpointer data)
+{
+ GNextstepSettingsBackend *backend;
+ NSString *key;
+ id object;
+
+ backend = G_NEXTSTEP_SETTINGS_BACKEND (data);
+ key = [NSString stringWithUTF8String:name];
+ object = g_nextstep_settings_backend_get_ns_object (value);
+
+ [backend->user_defaults setObject:object forKey:key];
+
+ return FALSE;
+}
+
+
+
+static GVariant *
+g_nextstep_settings_backend_get_g_variant (id object,
+ const GVariantType *type)
+{
+ if ([object isKindOfClass:[NSData class]])
+ return g_variant_parse (type, [[[[NSString alloc] initWithData:object encoding:NSUTF8StringEncoding] autorelease] UTF8String], NULL, NULL, NULL);
+ else if ([object isKindOfClass:[NSNumber class]])
+ {
+ if (g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN))
+ return g_variant_new_boolean ([object boolValue]);
+ else if (g_variant_type_equal (type, G_VARIANT_TYPE_BYTE))
+ return g_variant_new_byte ([object unsignedCharValue]);
+ else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT16))
+ return g_variant_new_int16 ([object shortValue]);
+ else if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT16))
+ return g_variant_new_uint16 ([object unsignedShortValue]);
+ else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT32))
+ return g_variant_new_int32 ([object longValue]);
+ else if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT32))
+ return g_variant_new_uint32 ([object unsignedLongValue]);
+ else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT64))
+ return g_variant_new_int64 ([object longLongValue]);
+ else if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT64))
+ return g_variant_new_uint64 ([object unsignedLongLongValue]);
+ else if (g_variant_type_equal (type, G_VARIANT_TYPE_HANDLE))
+ return g_variant_new_handle ([object longValue]);
+ else if (g_variant_type_equal (type, G_VARIANT_TYPE_DOUBLE))
+ return g_variant_new_double ([object doubleValue]);
+ else
+ return NULL;
+ }
+ else if ([object isKindOfClass:[NSString class]])
+ {
+ const char *string;
+
+ string = [object UTF8String];
+
+ if (g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
+ return g_variant_new_string (string);
+ else if (g_variant_type_equal (type, G_VARIANT_TYPE_OBJECT_PATH))
+ return g_variant_is_object_path (string) ?
+ g_variant_new_object_path (string) : NULL;
+ else if (g_variant_type_equal (type, G_VARIANT_TYPE_SIGNATURE))
+ return g_variant_is_signature (string) ?
+ g_variant_new_signature (string) : NULL;
+ else
+ return NULL;
+ }
+ else if ([object isKindOfClass:[NSDictionary class]])
+ {
+ if (g_variant_type_is_subtype_of (type, G_VARIANT_TYPE_DICTIONARY))
+ {
+ const GVariantType *element_type;
+ const GVariantType *key_type;
+ const GVariantType *value_type;
+
+ element_type = g_variant_type_element (type);
+ key_type = g_variant_type_first (element_type);
+ value_type = g_variant_type_next (key_type);
+
+ if (g_variant_type_equal (key_type, G_VARIANT_TYPE_STRING) ||
+ g_variant_type_equal (key_type, G_VARIANT_TYPE_OBJECT_PATH) ||
+ g_variant_type_equal (key_type, G_VARIANT_TYPE_SIGNATURE))
+ {
+ GVariantBuilder builder;
+ NSString *key;
+
+ g_variant_builder_init (&builder, type);
+
+ for (key in object)
+ {
+ id value;
+ GVariant *name;
+ GVariant *variant;
+ GVariant *entry;
+
+ if ((g_variant_type_equal (key_type, G_VARIANT_TYPE_OBJECT_PATH) &&
+ !g_variant_is_object_path ([key UTF8String])) ||
+ (g_variant_type_equal (key_type, G_VARIANT_TYPE_SIGNATURE) &&
+ !g_variant_is_signature ([key UTF8String])))
+ {
+ g_variant_builder_clear (&builder);
+
+ return NULL;
+ }
+
+ value = [object objectForKey:key];
+ variant = g_nextstep_settings_backend_get_g_variant (value, value_type);
+
+ if (variant == NULL)
+ {
+ g_variant_builder_clear (&builder);
+
+ return NULL;
+ }
+
+ if (g_variant_type_equal (key_type, G_VARIANT_TYPE_STRING))
+ name = g_variant_new_string ([key UTF8String]);
+ else if (g_variant_type_equal (key_type, G_VARIANT_TYPE_OBJECT_PATH))
+ name = g_variant_new_object_path ([key UTF8String]);
+ else if (g_variant_type_equal (key_type, G_VARIANT_TYPE_SIGNATURE))
+ name = g_variant_new_signature ([key UTF8String]);
+
+ entry = g_variant_new_dict_entry (name, variant);
+ g_variant_builder_add_value (&builder, entry);
+ }
+
+ return g_variant_builder_end (&builder);
+ }
+ else
+ return NULL;
+ }
+ else
+ return NULL;
+ }
+ else if ([object isKindOfClass:[NSArray class]])
+ {
+ if (g_variant_type_is_subtype_of (type, G_VARIANT_TYPE_ARRAY))
+ {
+ const GVariantType *value_type;
+ GVariantBuilder builder;
+ id value;
+
+ value_type = g_variant_type_element (type);
+ g_variant_builder_init (&builder, type);
+
+ for (value in object)
+ {
+ GVariant *variant = g_nextstep_settings_backend_get_g_variant (value, value_type);
+
+ if (variant == NULL)
+ {
+ g_variant_builder_clear (&builder);
+
+ return NULL;
+ }
+
+ g_variant_builder_add_value (&builder, variant);
+ }
+
+ return g_variant_builder_end (&builder);
+ }
+ else
+ return NULL;
+ }
+ else
+ return NULL;
+}
+
+
+
+static id
+g_nextstep_settings_backend_get_ns_object (GVariant *variant)
+{
+ if (variant == NULL)
+ return nil;
+ else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_BOOLEAN))
+ return [NSNumber numberWithBool:g_variant_get_boolean (variant)];
+ else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_BYTE))
+ return [NSNumber numberWithUnsignedChar:g_variant_get_byte (variant)];
+ else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_INT16))
+ return [NSNumber numberWithShort:g_variant_get_int16 (variant)];
+ else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_UINT16))
+ return [NSNumber numberWithUnsignedShort:g_variant_get_uint16 (variant)];
+ else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_INT32))
+ return [NSNumber numberWithLong:g_variant_get_int32 (variant)];
+ else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_UINT32))
+ return [NSNumber numberWithUnsignedLong:g_variant_get_uint32 (variant)];
+ else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_INT64))
+ return [NSNumber numberWithLongLong:g_variant_get_int64 (variant)];
+ else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_UINT64))
+ return [NSNumber numberWithUnsignedLongLong:g_variant_get_uint64 (variant)];
+ else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_HANDLE))
+ return [NSNumber numberWithLong:g_variant_get_handle (variant)];
+ else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_DOUBLE))
+ return [NSNumber numberWithDouble:g_variant_get_double (variant)];
+ else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_STRING))
+ return [NSString stringWithUTF8String:g_variant_get_string (variant, NULL)];
+ else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_OBJECT_PATH))
+ return [NSString stringWithUTF8String:g_variant_get_string (variant, NULL)];
+ else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_SIGNATURE))
+ return [NSString stringWithUTF8String:g_variant_get_string (variant, NULL)];
+ else if (g_variant_is_of_type (variant, G_VARIANT_TYPE ("a{s*}")))
+ {
+ NSMutableDictionary *dictionary;
+ GVariantIter iter;
+ GVariant *name;
+ GVariant *value;
+
+ dictionary = [NSMutableDictionary dictionaryWithCapacity:g_variant_n_children (variant)];
+
+ g_variant_iter_init (&iter, variant);
+
+ while (g_variant_iter_loop (&iter, "{s*}", &name, &value))
+ {
+ NSString *key;
+ id object;
+
+ key = [NSString stringWithUTF8String:g_variant_get_string (name, NULL)];
+ object = g_nextstep_settings_backend_get_ns_object (value);
+
+ [dictionary setObject:object forKey:key];
+ }
+
+ return dictionary;
+ }
+ else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_ARRAY))
+ {
+ NSMutableArray *array;
+ GVariantIter iter;
+ GVariant *value;
+
+ array = [NSMutableArray arrayWithCapacity:g_variant_n_children (variant)];
+
+ g_variant_iter_init (&iter, variant);
+
+ while ((value = g_variant_iter_next_value (&iter)) != NULL)
+ [array addObject:g_nextstep_settings_backend_get_ns_object (value)];
+
+ return array;
+ }
+ else
+ return [[NSString stringWithUTF8String:g_variant_print (variant, TRUE)] dataUsingEncoding:NSUTF8StringEncoding];
+}
diff --git a/gio/gnextstepsettingsbackend.h b/gio/gnextstepsettingsbackend.h
new file mode 100644
index 0000000..e4e3dbe
--- /dev/null
+++ b/gio/gnextstepsettingsbackend.h
@@ -0,0 +1,34 @@
+#ifndef __G_NEXTSTEP_SETTINGS_BACKEND_H__
+#define __G_NEXTSTEP_SETTINGS_BACKEND_H__
+
+
+
+#include <gio/gsettingsbackend.h>
+
+
+
+G_BEGIN_DECLS
+
+
+
+#define G_TYPE_NEXTSTEP_SETTINGS_BACKEND (g_nextstep_settings_backend_get_type ())
+#define G_NEXTSTEP_SETTINGS_BACKEND(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), G_TYPE_NEXTSTEP_SETTINGS_BACKEND, GNextstepSettingsBackend))
+#define G_IS_NEXTSTEP_SETTINGS_BACKEND(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), G_TYPE_NEXTSTEP_SETTINGS_BACKEND))
+
+
+
+typedef struct _GNextstepSettingsBackend GNextstepSettingsBackend;
+
+
+
+GType g_nextstep_settings_backend_get_type (void);
+
+GSettingsBackend * g_nextstep_settings_backend_new (void);
+
+
+
+G_END_DECLS
+
+
+
+#endif /* __G_NEXTSTEP_SETTINGS_BACKEND_H__ */
diff --git a/gio/gsettingsbackend.c b/gio/gsettingsbackend.c
index 6075d7f..e81c4ef 100644
--- a/gio/gsettingsbackend.c
+++ b/gio/gsettingsbackend.c
@@ -777,7 +777,7 @@ g_settings_backend_write (GSettingsBackend *backend,
}
/*< private >
- * g_settings_backend_write_keys:
+ * g_settings_backend_write_tree:
* @backend: a #GSettingsBackend implementation
* @values: a #GTree containing key-value pairs to write
* @origin_tag: the origin tag
diff --git a/gio/gsettingsbackend.h b/gio/gsettingsbackend.h
index e674187..2434394 100644
--- a/gio/gsettingsbackend.h
+++ b/gio/gsettingsbackend.h
@@ -139,6 +139,10 @@ GSettingsBackend * g_null_settings_backend_new (void);
GSettingsBackend * g_memory_settings_backend_new (void);
+#ifdef G_OS_CARBON
+GSettingsBackend * g_nextstep_settings_backend_new (void);
+#endif
+
G_END_DECLS
#endif /* __G_SETTINGS_BACKEND_H__ */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]