Revert "GIOScheduler: Avoid constant iteration over pending job list"
[platform/upstream/glib.git] / gio / gnextstepsettingsbackend.c
1 /*
2  * Copyright © 2011 William Hua
3  *
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.
8  *
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.
13  *
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.
18  *
19  * Author: William Hua <william@attente.ca>
20  */
21
22 #include "gsettingsbackendinternal.h"
23 #include "gsimplepermission.h"
24 #include "giomodule.h"
25
26 #import <Foundation/Foundation.h>
27
28 #define G_NEXTSTEP_SETTINGS_BACKEND(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), g_nextstep_settings_backend_get_type (), GNextstepSettingsBackend))
29
30 typedef struct _GNextstepSettingsBackend GNextstepSettingsBackend;
31 typedef GSettingsBackendClass            GNextstepSettingsBackendClass;
32
33 struct _GNextstepSettingsBackend
34 {
35   GSettingsBackend  parent_instance;
36
37   /*< private >*/
38   NSUserDefaults   *user_defaults;
39   GMutex            mutex;
40 };
41
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));
47
48 static void          g_nextstep_settings_backend_finalize       (GObject            *backend);
49
50 static GVariant *    g_nextstep_settings_backend_read           (GSettingsBackend   *backend,
51                                                                  const gchar        *key,
52                                                                  const GVariantType *expected_type,
53                                                                  gboolean            default_value);
54
55 static gboolean      g_nextstep_settings_backend_get_writable   (GSettingsBackend   *backend,
56                                                                  const gchar        *key);
57
58 static gboolean      g_nextstep_settings_backend_write          (GSettingsBackend   *backend,
59                                                                  const gchar        *key,
60                                                                  GVariant           *value,
61                                                                  gpointer            origin_tag);
62
63 static gboolean      g_nextstep_settings_backend_write_tree     (GSettingsBackend   *backend,
64                                                                  GTree              *tree,
65                                                                  gpointer            origin_tag);
66
67 static void          g_nextstep_settings_backend_reset          (GSettingsBackend   *backend,
68                                                                  const gchar        *key,
69                                                                  gpointer            origin_tag);
70
71 static void          g_nextstep_settings_backend_subscribe      (GSettingsBackend   *backend,
72                                                                  const gchar        *name);
73
74 static void          g_nextstep_settings_backend_unsubscribe    (GSettingsBackend   *backend,
75                                                                  const gchar        *name);
76
77 static void          g_nextstep_settings_backend_sync           (GSettingsBackend   *backend);
78
79 static GPermission * g_nextstep_settings_backend_get_permission (GSettingsBackend   *backend,
80                                                                  const gchar        *path);
81
82 static gboolean      g_nextstep_settings_backend_write_pair     (gpointer            name,
83                                                                  gpointer            value,
84                                                                  gpointer            data);
85
86 static GVariant *    g_nextstep_settings_backend_get_g_variant  (id                  object,
87                                                                  const GVariantType *type);
88
89 static id            g_nextstep_settings_backend_get_ns_object  (GVariant           *variant);
90
91 static void
92 g_nextstep_settings_backend_class_init (GNextstepSettingsBackendClass *class)
93 {
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;
104 }
105
106 static void
107 g_nextstep_settings_backend_init (GNextstepSettingsBackend *self)
108 {
109   NSAutoreleasePool *pool;
110
111   pool = [[NSAutoreleasePool alloc] init];
112
113   self->user_defaults = [[NSUserDefaults standardUserDefaults] retain];
114
115   g_mutex_init (&self->mutex);
116
117   [pool drain];
118 }
119
120 static void
121 g_nextstep_settings_backend_finalize (GObject *self)
122 {
123   GNextstepSettingsBackend *backend = G_NEXTSTEP_SETTINGS_BACKEND (self);
124   NSAutoreleasePool *pool;
125
126   pool = [[NSAutoreleasePool alloc] init];
127
128   g_mutex_clear (&backend->mutex);
129
130   [backend->user_defaults release];
131
132   [pool drain];
133
134   G_OBJECT_CLASS (g_nextstep_settings_backend_parent_class)->finalize (self);
135 }
136
137 static GVariant *
138 g_nextstep_settings_backend_read (GSettingsBackend   *backend,
139                                   const gchar        *key,
140                                   const GVariantType *expected_type,
141                                   gboolean            default_value)
142 {
143   GNextstepSettingsBackend *self = G_NEXTSTEP_SETTINGS_BACKEND (backend);
144   NSAutoreleasePool *pool;
145   NSString *name;
146   id value;
147   GVariant *variant;
148
149   if (default_value)
150     return NULL;
151
152   pool = [[NSAutoreleasePool alloc] init];
153   name = [NSString stringWithUTF8String:key];
154
155   g_mutex_lock (&self->mutex);
156   value = [self->user_defaults objectForKey:name];
157   g_mutex_unlock (&self->mutex);
158
159   variant = g_nextstep_settings_backend_get_g_variant (value, expected_type);
160
161   [pool drain];
162
163   return variant;
164 }
165
166 static gboolean
167 g_nextstep_settings_backend_get_writable (GSettingsBackend *backend,
168                                           const gchar      *key)
169 {
170   return TRUE;
171 }
172
173 static gboolean
174 g_nextstep_settings_backend_write (GSettingsBackend *backend,
175                                    const gchar      *key,
176                                    GVariant         *value,
177                                    gpointer          origin_tag)
178 {
179   GNextstepSettingsBackend *self = G_NEXTSTEP_SETTINGS_BACKEND (backend);
180   NSAutoreleasePool *pool;
181
182   pool = [[NSAutoreleasePool alloc] init];
183
184   g_mutex_lock (&self->mutex);
185   g_nextstep_settings_backend_write_pair ((gpointer) key, value, self);
186   g_mutex_unlock (&self->mutex);
187
188   g_settings_backend_changed (backend, key, origin_tag);
189
190   [pool drain];
191
192   return TRUE;
193 }
194
195 static gboolean
196 g_nextstep_settings_backend_write_tree (GSettingsBackend *backend,
197                                         GTree            *tree,
198                                         gpointer          origin_tag)
199 {
200   GNextstepSettingsBackend *self = G_NEXTSTEP_SETTINGS_BACKEND (backend);
201   NSAutoreleasePool *pool;
202
203   pool = [[NSAutoreleasePool alloc] init];
204
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);
209
210   [pool drain];
211
212   return TRUE;
213 }
214
215 static void
216 g_nextstep_settings_backend_reset (GSettingsBackend *backend,
217                                    const gchar      *key,
218                                    gpointer          origin_tag)
219 {
220   GNextstepSettingsBackend *self = G_NEXTSTEP_SETTINGS_BACKEND (backend);
221   NSAutoreleasePool *pool;
222   NSString *name;
223
224   pool = [[NSAutoreleasePool alloc] init];
225   name = [NSString stringWithUTF8String:key];
226
227   g_mutex_lock (&self->mutex);
228   [self->user_defaults removeObjectForKey:name];
229   g_mutex_unlock (&self->mutex);
230
231   g_settings_backend_changed (backend, key, origin_tag);
232
233   [pool drain];
234 }
235
236 static void
237 g_nextstep_settings_backend_subscribe (GSettingsBackend *backend,
238                                        const gchar      *name)
239 {
240 }
241
242 static void
243 g_nextstep_settings_backend_unsubscribe (GSettingsBackend *backend,
244                                          const gchar      *name)
245 {
246 }
247
248 static void
249 g_nextstep_settings_backend_sync (GSettingsBackend *backend)
250 {
251   GNextstepSettingsBackend *self = G_NEXTSTEP_SETTINGS_BACKEND (backend);
252   NSAutoreleasePool *pool;
253
254   pool = [[NSAutoreleasePool alloc] init];
255
256   g_mutex_lock (&self->mutex);
257   [self->user_defaults synchronize];
258   g_mutex_unlock (&self->mutex);
259
260   [pool drain];
261 }
262
263 static GPermission *
264 g_nextstep_settings_backend_get_permission (GSettingsBackend *backend,
265                                             const gchar      *path)
266 {
267   return g_simple_permission_new (TRUE);
268 }
269
270 static gboolean
271 g_nextstep_settings_backend_write_pair (gpointer name,
272                                         gpointer value,
273                                         gpointer data)
274 {
275   GNextstepSettingsBackend *backend = G_NEXTSTEP_SETTINGS_BACKEND (data);
276   NSString *key;
277   id object;
278
279   key = [NSString stringWithUTF8String:name];
280   object = g_nextstep_settings_backend_get_ns_object (value);
281
282   [backend->user_defaults setObject:object forKey:key];
283
284   return FALSE;
285 }
286
287 static GVariant *
288 g_nextstep_settings_backend_get_g_variant (id                  object,
289                                            const GVariantType *type)
290 {
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]])
294     {
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]);
315     }
316   else if ([object isKindOfClass:[NSString class]])
317     {
318       const char *string;
319
320       string = [object UTF8String];
321
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;
330     }
331   else if ([object isKindOfClass:[NSDictionary class]])
332     {
333       if (g_variant_type_is_subtype_of (type, G_VARIANT_TYPE ("a{s*}")))
334         {
335           const GVariantType *value_type;
336           GVariantBuilder builder;
337           NSString *key;
338
339           value_type = g_variant_type_value (g_variant_type_element (type));
340
341           g_variant_builder_init (&builder, type);
342
343 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1050
344           for(key in object)
345 #else
346           NSEnumerator *enumerator = [object objectEnumerator];
347           while((key = [enumerator nextObject]))
348 #endif
349             {
350               GVariant *name;
351               id value;
352               GVariant *variant;
353               GVariant *entry;
354
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);
358
359               if (variant == NULL)
360                 {
361                   g_variant_builder_clear (&builder);
362
363                   return NULL;
364                 }
365
366               entry = g_variant_new_dict_entry (name, variant);
367               g_variant_builder_add_value (&builder, entry);
368             }
369
370           return g_variant_builder_end (&builder);
371         }
372     }
373   else if ([object isKindOfClass:[NSArray class]])
374     {
375       if (g_variant_type_is_subtype_of (type, G_VARIANT_TYPE_ARRAY))
376         {
377           const GVariantType *value_type;
378           GVariantBuilder builder;
379           id value;
380
381           value_type = g_variant_type_element (type);
382           g_variant_builder_init (&builder, type);
383
384 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1050
385           for(value in object)
386 #else
387           NSEnumerator *enumerator = [object objectEnumerator];
388           while((value = [enumerator nextObject]))
389 #endif
390             {
391               GVariant *variant = g_nextstep_settings_backend_get_g_variant (value, value_type);
392
393               if (variant == NULL)
394                 {
395                   g_variant_builder_clear (&builder);
396
397                   return NULL;
398                 }
399
400               g_variant_builder_add_value (&builder, variant);
401             }
402
403           return g_variant_builder_end (&builder);
404         }
405     }
406
407   return NULL;
408 }
409
410 static id
411 g_nextstep_settings_backend_get_ns_object (GVariant *variant)
412 {
413   if (variant == NULL)
414     return nil;
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*}")))
442     {
443       NSMutableDictionary *dictionary;
444       GVariantIter iter;
445       GVariant *name;
446       GVariant *value;
447
448       dictionary = [NSMutableDictionary dictionaryWithCapacity:g_variant_iter_init (&iter, variant)];
449
450       while (g_variant_iter_loop (&iter, "{s*}", &name, &value))
451         {
452           NSString *key;
453           id object;
454
455           key = [NSString stringWithUTF8String:g_variant_get_string (name, NULL)];
456           object = g_nextstep_settings_backend_get_ns_object (value);
457
458           [dictionary setObject:object forKey:key];
459         }
460
461       return dictionary;
462     }
463   else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_ARRAY))
464     {
465       NSMutableArray *array;
466       GVariantIter iter;
467       GVariant *value;
468
469       array = [NSMutableArray arrayWithCapacity:g_variant_iter_init (&iter, variant)];
470
471       while ((value = g_variant_iter_next_value (&iter)) != NULL)
472         [array addObject:g_nextstep_settings_backend_get_ns_object (value)];
473
474       return array;
475     }
476   else
477     return [[NSString stringWithUTF8String:g_variant_print (variant, TRUE)] dataUsingEncoding:NSUTF8StringEncoding];
478 }