2 * Copyright © 2011 William Hua
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the licence, or (at your option) any later version.
9 * This library 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 GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * Author: William Hua <william@attente.ca>
22 #include "gsettingsbackendinternal.h"
23 #include "gsimplepermission.h"
24 #include "giomodule.h"
26 #import <Foundation/Foundation.h>
28 #define G_NEXTSTEP_SETTINGS_BACKEND(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), g_nextstep_settings_backend_get_type (), GNextstepSettingsBackend))
30 typedef struct _GNextstepSettingsBackend GNextstepSettingsBackend;
31 typedef GSettingsBackendClass GNextstepSettingsBackendClass;
33 struct _GNextstepSettingsBackend
35 GSettingsBackend parent_instance;
38 NSUserDefaults *user_defaults;
42 G_DEFINE_TYPE_WITH_CODE (GNextstepSettingsBackend,
43 g_nextstep_settings_backend,
44 G_TYPE_SETTINGS_BACKEND,
45 g_io_extension_point_implement (G_SETTINGS_BACKEND_EXTENSION_POINT_NAME,
46 g_define_type_id, "nextstep", 90));
48 static void g_nextstep_settings_backend_finalize (GObject *backend);
50 static GVariant * g_nextstep_settings_backend_read (GSettingsBackend *backend,
52 const GVariantType *expected_type,
53 gboolean default_value);
55 static gboolean g_nextstep_settings_backend_get_writable (GSettingsBackend *backend,
58 static gboolean g_nextstep_settings_backend_write (GSettingsBackend *backend,
63 static gboolean g_nextstep_settings_backend_write_tree (GSettingsBackend *backend,
67 static void g_nextstep_settings_backend_reset (GSettingsBackend *backend,
71 static void g_nextstep_settings_backend_subscribe (GSettingsBackend *backend,
74 static void g_nextstep_settings_backend_unsubscribe (GSettingsBackend *backend,
77 static void g_nextstep_settings_backend_sync (GSettingsBackend *backend);
79 static GPermission * g_nextstep_settings_backend_get_permission (GSettingsBackend *backend,
82 static gboolean g_nextstep_settings_backend_write_pair (gpointer name,
86 static GVariant * g_nextstep_settings_backend_get_g_variant (id object,
87 const GVariantType *type);
89 static id g_nextstep_settings_backend_get_ns_object (GVariant *variant);
92 g_nextstep_settings_backend_class_init (GNextstepSettingsBackendClass *class)
94 G_OBJECT_CLASS (class)->finalize = g_nextstep_settings_backend_finalize;
95 class->read = g_nextstep_settings_backend_read;
96 class->get_writable = g_nextstep_settings_backend_get_writable;
97 class->write = g_nextstep_settings_backend_write;
98 class->write_tree = g_nextstep_settings_backend_write_tree;
99 class->reset = g_nextstep_settings_backend_reset;
100 class->subscribe = g_nextstep_settings_backend_subscribe;
101 class->unsubscribe = g_nextstep_settings_backend_unsubscribe;
102 class->sync = g_nextstep_settings_backend_sync;
103 class->get_permission = g_nextstep_settings_backend_get_permission;
107 g_nextstep_settings_backend_init (GNextstepSettingsBackend *self)
109 NSAutoreleasePool *pool;
111 pool = [[NSAutoreleasePool alloc] init];
113 self->user_defaults = [[NSUserDefaults standardUserDefaults] retain];
115 g_mutex_init (&self->mutex);
121 g_nextstep_settings_backend_finalize (GObject *self)
123 GNextstepSettingsBackend *backend = G_NEXTSTEP_SETTINGS_BACKEND (self);
124 NSAutoreleasePool *pool;
126 pool = [[NSAutoreleasePool alloc] init];
128 g_mutex_clear (&backend->mutex);
130 [backend->user_defaults release];
134 G_OBJECT_CLASS (g_nextstep_settings_backend_parent_class)->finalize (self);
138 g_nextstep_settings_backend_read (GSettingsBackend *backend,
140 const GVariantType *expected_type,
141 gboolean default_value)
143 GNextstepSettingsBackend *self = G_NEXTSTEP_SETTINGS_BACKEND (backend);
144 NSAutoreleasePool *pool;
152 pool = [[NSAutoreleasePool alloc] init];
153 name = [NSString stringWithUTF8String:key];
155 g_mutex_lock (&self->mutex);
156 value = [self->user_defaults objectForKey:name];
157 g_mutex_unlock (&self->mutex);
159 variant = g_nextstep_settings_backend_get_g_variant (value, expected_type);
167 g_nextstep_settings_backend_get_writable (GSettingsBackend *backend,
174 g_nextstep_settings_backend_write (GSettingsBackend *backend,
179 GNextstepSettingsBackend *self = G_NEXTSTEP_SETTINGS_BACKEND (backend);
180 NSAutoreleasePool *pool;
182 pool = [[NSAutoreleasePool alloc] init];
184 g_mutex_lock (&self->mutex);
185 g_nextstep_settings_backend_write_pair ((gpointer) key, value, self);
186 g_mutex_unlock (&self->mutex);
188 g_settings_backend_changed (backend, key, origin_tag);
196 g_nextstep_settings_backend_write_tree (GSettingsBackend *backend,
200 GNextstepSettingsBackend *self = G_NEXTSTEP_SETTINGS_BACKEND (backend);
201 NSAutoreleasePool *pool;
203 pool = [[NSAutoreleasePool alloc] init];
205 g_mutex_lock (&self->mutex);
206 g_tree_foreach (tree, g_nextstep_settings_backend_write_pair, self);
207 g_mutex_unlock (&self->mutex);
208 g_settings_backend_changed_tree (backend, tree, origin_tag);
216 g_nextstep_settings_backend_reset (GSettingsBackend *backend,
220 GNextstepSettingsBackend *self = G_NEXTSTEP_SETTINGS_BACKEND (backend);
221 NSAutoreleasePool *pool;
224 pool = [[NSAutoreleasePool alloc] init];
225 name = [NSString stringWithUTF8String:key];
227 g_mutex_lock (&self->mutex);
228 [self->user_defaults removeObjectForKey:name];
229 g_mutex_unlock (&self->mutex);
231 g_settings_backend_changed (backend, key, origin_tag);
237 g_nextstep_settings_backend_subscribe (GSettingsBackend *backend,
243 g_nextstep_settings_backend_unsubscribe (GSettingsBackend *backend,
249 g_nextstep_settings_backend_sync (GSettingsBackend *backend)
251 GNextstepSettingsBackend *self = G_NEXTSTEP_SETTINGS_BACKEND (backend);
252 NSAutoreleasePool *pool;
254 pool = [[NSAutoreleasePool alloc] init];
256 g_mutex_lock (&self->mutex);
257 [self->user_defaults synchronize];
258 g_mutex_unlock (&self->mutex);
264 g_nextstep_settings_backend_get_permission (GSettingsBackend *backend,
267 return g_simple_permission_new (TRUE);
271 g_nextstep_settings_backend_write_pair (gpointer name,
275 GNextstepSettingsBackend *backend = G_NEXTSTEP_SETTINGS_BACKEND (data);
279 key = [NSString stringWithUTF8String:name];
280 object = g_nextstep_settings_backend_get_ns_object (value);
282 [backend->user_defaults setObject:object forKey:key];
288 g_nextstep_settings_backend_get_g_variant (id object,
289 const GVariantType *type)
291 if ([object isKindOfClass:[NSData class]])
292 return g_variant_parse (type, [[[[NSString alloc] initWithData:object encoding:NSUTF8StringEncoding] autorelease] UTF8String], NULL, NULL, NULL);
293 else if ([object isKindOfClass:[NSNumber class]])
295 if (g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN))
296 return g_variant_new_boolean ([object boolValue]);
297 else if (g_variant_type_equal (type, G_VARIANT_TYPE_BYTE))
298 return g_variant_new_byte ([object unsignedCharValue]);
299 else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT16))
300 return g_variant_new_int16 ([object shortValue]);
301 else if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT16))
302 return g_variant_new_uint16 ([object unsignedShortValue]);
303 else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT32))
304 return g_variant_new_int32 ([object longValue]);
305 else if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT32))
306 return g_variant_new_uint32 ([object unsignedLongValue]);
307 else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT64))
308 return g_variant_new_int64 ([object longLongValue]);
309 else if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT64))
310 return g_variant_new_uint64 ([object unsignedLongLongValue]);
311 else if (g_variant_type_equal (type, G_VARIANT_TYPE_HANDLE))
312 return g_variant_new_handle ([object longValue]);
313 else if (g_variant_type_equal (type, G_VARIANT_TYPE_DOUBLE))
314 return g_variant_new_double ([object doubleValue]);
316 else if ([object isKindOfClass:[NSString class]])
320 string = [object UTF8String];
322 if (g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
323 return g_variant_new_string (string);
324 else if (g_variant_type_equal (type, G_VARIANT_TYPE_OBJECT_PATH))
325 return g_variant_is_object_path (string) ?
326 g_variant_new_object_path (string) : NULL;
327 else if (g_variant_type_equal (type, G_VARIANT_TYPE_SIGNATURE))
328 return g_variant_is_signature (string) ?
329 g_variant_new_signature (string) : NULL;
331 else if ([object isKindOfClass:[NSDictionary class]])
333 if (g_variant_type_is_subtype_of (type, G_VARIANT_TYPE ("a{s*}")))
335 const GVariantType *value_type;
336 GVariantBuilder builder;
339 value_type = g_variant_type_value (g_variant_type_element (type));
341 g_variant_builder_init (&builder, type);
343 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1050
346 NSEnumerator *enumerator = [object objectEnumerator];
347 while((key = [enumerator nextObject]))
355 name = g_variant_new_string ([key UTF8String]);
356 value = [object objectForKey:key];
357 variant = g_nextstep_settings_backend_get_g_variant (value, value_type);
361 g_variant_builder_clear (&builder);
366 entry = g_variant_new_dict_entry (name, variant);
367 g_variant_builder_add_value (&builder, entry);
370 return g_variant_builder_end (&builder);
373 else if ([object isKindOfClass:[NSArray class]])
375 if (g_variant_type_is_subtype_of (type, G_VARIANT_TYPE_ARRAY))
377 const GVariantType *value_type;
378 GVariantBuilder builder;
381 value_type = g_variant_type_element (type);
382 g_variant_builder_init (&builder, type);
384 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1050
387 NSEnumerator *enumerator = [object objectEnumerator];
388 while((value = [enumerator nextObject]))
391 GVariant *variant = g_nextstep_settings_backend_get_g_variant (value, value_type);
395 g_variant_builder_clear (&builder);
400 g_variant_builder_add_value (&builder, variant);
403 return g_variant_builder_end (&builder);
411 g_nextstep_settings_backend_get_ns_object (GVariant *variant)
415 else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_BOOLEAN))
416 return [NSNumber numberWithBool:g_variant_get_boolean (variant)];
417 else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_BYTE))
418 return [NSNumber numberWithUnsignedChar:g_variant_get_byte (variant)];
419 else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_INT16))
420 return [NSNumber numberWithShort:g_variant_get_int16 (variant)];
421 else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_UINT16))
422 return [NSNumber numberWithUnsignedShort:g_variant_get_uint16 (variant)];
423 else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_INT32))
424 return [NSNumber numberWithLong:g_variant_get_int32 (variant)];
425 else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_UINT32))
426 return [NSNumber numberWithUnsignedLong:g_variant_get_uint32 (variant)];
427 else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_INT64))
428 return [NSNumber numberWithLongLong:g_variant_get_int64 (variant)];
429 else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_UINT64))
430 return [NSNumber numberWithUnsignedLongLong:g_variant_get_uint64 (variant)];
431 else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_HANDLE))
432 return [NSNumber numberWithLong:g_variant_get_handle (variant)];
433 else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_DOUBLE))
434 return [NSNumber numberWithDouble:g_variant_get_double (variant)];
435 else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_STRING))
436 return [NSString stringWithUTF8String:g_variant_get_string (variant, NULL)];
437 else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_OBJECT_PATH))
438 return [NSString stringWithUTF8String:g_variant_get_string (variant, NULL)];
439 else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_SIGNATURE))
440 return [NSString stringWithUTF8String:g_variant_get_string (variant, NULL)];
441 else if (g_variant_is_of_type (variant, G_VARIANT_TYPE ("a{s*}")))
443 NSMutableDictionary *dictionary;
448 dictionary = [NSMutableDictionary dictionaryWithCapacity:g_variant_iter_init (&iter, variant)];
450 while (g_variant_iter_loop (&iter, "{s*}", &name, &value))
455 key = [NSString stringWithUTF8String:g_variant_get_string (name, NULL)];
456 object = g_nextstep_settings_backend_get_ns_object (value);
458 [dictionary setObject:object forKey:key];
463 else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_ARRAY))
465 NSMutableArray *array;
469 array = [NSMutableArray arrayWithCapacity:g_variant_iter_init (&iter, variant)];
471 while ((value = g_variant_iter_next_value (&iter)) != NULL)
472 [array addObject:g_nextstep_settings_backend_get_ns_object (value)];
477 return [[NSString stringWithUTF8String:g_variant_print (variant, TRUE)] dataUsingEncoding:NSUTF8StringEncoding];