GMemoryInputStream: fix skip_async()
[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 "config.h"
23
24 #include "gsettingsbackendinternal.h"
25 #include "gsimplepermission.h"
26 #include "giomodule.h"
27
28 #import <Foundation/Foundation.h>
29
30 GType g_nextstep_settings_backend_get_type (void);
31
32 #define G_NEXTSTEP_SETTINGS_BACKEND(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), g_nextstep_settings_backend_get_type (), GNextstepSettingsBackend))
33
34 typedef struct _GNextstepSettingsBackend GNextstepSettingsBackend;
35 typedef GSettingsBackendClass            GNextstepSettingsBackendClass;
36
37 struct _GNextstepSettingsBackend
38 {
39   GSettingsBackend  parent_instance;
40
41   /*< private >*/
42   NSUserDefaults   *user_defaults;
43   GMutex            mutex;
44 };
45
46 G_DEFINE_TYPE_WITH_CODE (GNextstepSettingsBackend,
47                          g_nextstep_settings_backend,
48                          G_TYPE_SETTINGS_BACKEND,
49                          g_io_extension_point_implement (G_SETTINGS_BACKEND_EXTENSION_POINT_NAME,
50                                                          g_define_type_id, "nextstep", 90));
51
52 static void          g_nextstep_settings_backend_finalize       (GObject            *backend);
53
54 static GVariant *    g_nextstep_settings_backend_read           (GSettingsBackend   *backend,
55                                                                  const gchar        *key,
56                                                                  const GVariantType *expected_type,
57                                                                  gboolean            default_value);
58
59 static gboolean      g_nextstep_settings_backend_get_writable   (GSettingsBackend   *backend,
60                                                                  const gchar        *key);
61
62 static gboolean      g_nextstep_settings_backend_write          (GSettingsBackend   *backend,
63                                                                  const gchar        *key,
64                                                                  GVariant           *value,
65                                                                  gpointer            origin_tag);
66
67 static gboolean      g_nextstep_settings_backend_write_tree     (GSettingsBackend   *backend,
68                                                                  GTree              *tree,
69                                                                  gpointer            origin_tag);
70
71 static void          g_nextstep_settings_backend_reset          (GSettingsBackend   *backend,
72                                                                  const gchar        *key,
73                                                                  gpointer            origin_tag);
74
75 static void          g_nextstep_settings_backend_subscribe      (GSettingsBackend   *backend,
76                                                                  const gchar        *name);
77
78 static void          g_nextstep_settings_backend_unsubscribe    (GSettingsBackend   *backend,
79                                                                  const gchar        *name);
80
81 static void          g_nextstep_settings_backend_sync           (GSettingsBackend   *backend);
82
83 static GPermission * g_nextstep_settings_backend_get_permission (GSettingsBackend   *backend,
84                                                                  const gchar        *path);
85
86 static gboolean      g_nextstep_settings_backend_write_pair     (gpointer            name,
87                                                                  gpointer            value,
88                                                                  gpointer            data);
89
90 static GVariant *    g_nextstep_settings_backend_get_g_variant  (id                  object,
91                                                                  const GVariantType *type);
92
93 static id            g_nextstep_settings_backend_get_ns_object  (GVariant           *variant);
94
95 static void
96 g_nextstep_settings_backend_class_init (GNextstepSettingsBackendClass *class)
97 {
98   G_OBJECT_CLASS (class)->finalize = g_nextstep_settings_backend_finalize;
99   class->read = g_nextstep_settings_backend_read;
100   class->get_writable = g_nextstep_settings_backend_get_writable;
101   class->write = g_nextstep_settings_backend_write;
102   class->write_tree = g_nextstep_settings_backend_write_tree;
103   class->reset = g_nextstep_settings_backend_reset;
104   class->subscribe = g_nextstep_settings_backend_subscribe;
105   class->unsubscribe = g_nextstep_settings_backend_unsubscribe;
106   class->sync = g_nextstep_settings_backend_sync;
107   class->get_permission = g_nextstep_settings_backend_get_permission;
108 }
109
110 static void
111 g_nextstep_settings_backend_init (GNextstepSettingsBackend *self)
112 {
113   NSAutoreleasePool *pool;
114
115   pool = [[NSAutoreleasePool alloc] init];
116
117   self->user_defaults = [[NSUserDefaults standardUserDefaults] retain];
118
119   g_mutex_init (&self->mutex);
120
121   [pool drain];
122 }
123
124 static void
125 g_nextstep_settings_backend_finalize (GObject *self)
126 {
127   GNextstepSettingsBackend *backend = G_NEXTSTEP_SETTINGS_BACKEND (self);
128   NSAutoreleasePool *pool;
129
130   pool = [[NSAutoreleasePool alloc] init];
131
132   g_mutex_clear (&backend->mutex);
133
134   [backend->user_defaults release];
135
136   [pool drain];
137
138   G_OBJECT_CLASS (g_nextstep_settings_backend_parent_class)->finalize (self);
139 }
140
141 static GVariant *
142 g_nextstep_settings_backend_read (GSettingsBackend   *backend,
143                                   const gchar        *key,
144                                   const GVariantType *expected_type,
145                                   gboolean            default_value)
146 {
147   GNextstepSettingsBackend *self = G_NEXTSTEP_SETTINGS_BACKEND (backend);
148   NSAutoreleasePool *pool;
149   NSString *name;
150   id value;
151   GVariant *variant;
152
153   if (default_value)
154     return NULL;
155
156   pool = [[NSAutoreleasePool alloc] init];
157   name = [NSString stringWithUTF8String:key];
158
159   g_mutex_lock (&self->mutex);
160   value = [self->user_defaults objectForKey:name];
161   g_mutex_unlock (&self->mutex);
162
163   variant = g_nextstep_settings_backend_get_g_variant (value, expected_type);
164
165   [pool drain];
166
167   return variant;
168 }
169
170 static gboolean
171 g_nextstep_settings_backend_get_writable (GSettingsBackend *backend,
172                                           const gchar      *key)
173 {
174   return TRUE;
175 }
176
177 static gboolean
178 g_nextstep_settings_backend_write (GSettingsBackend *backend,
179                                    const gchar      *key,
180                                    GVariant         *value,
181                                    gpointer          origin_tag)
182 {
183   GNextstepSettingsBackend *self = G_NEXTSTEP_SETTINGS_BACKEND (backend);
184   NSAutoreleasePool *pool;
185
186   pool = [[NSAutoreleasePool alloc] init];
187
188   g_mutex_lock (&self->mutex);
189   g_nextstep_settings_backend_write_pair ((gpointer) key, value, self);
190   g_mutex_unlock (&self->mutex);
191
192   g_settings_backend_changed (backend, key, origin_tag);
193
194   [pool drain];
195
196   return TRUE;
197 }
198
199 static gboolean
200 g_nextstep_settings_backend_write_tree (GSettingsBackend *backend,
201                                         GTree            *tree,
202                                         gpointer          origin_tag)
203 {
204   GNextstepSettingsBackend *self = G_NEXTSTEP_SETTINGS_BACKEND (backend);
205   NSAutoreleasePool *pool;
206
207   pool = [[NSAutoreleasePool alloc] init];
208
209   g_mutex_lock (&self->mutex);
210   g_tree_foreach (tree, g_nextstep_settings_backend_write_pair, self);
211   g_mutex_unlock (&self->mutex);
212   g_settings_backend_changed_tree (backend, tree, origin_tag);
213
214   [pool drain];
215
216   return TRUE;
217 }
218
219 static void
220 g_nextstep_settings_backend_reset (GSettingsBackend *backend,
221                                    const gchar      *key,
222                                    gpointer          origin_tag)
223 {
224   GNextstepSettingsBackend *self = G_NEXTSTEP_SETTINGS_BACKEND (backend);
225   NSAutoreleasePool *pool;
226   NSString *name;
227
228   pool = [[NSAutoreleasePool alloc] init];
229   name = [NSString stringWithUTF8String:key];
230
231   g_mutex_lock (&self->mutex);
232   [self->user_defaults removeObjectForKey:name];
233   g_mutex_unlock (&self->mutex);
234
235   g_settings_backend_changed (backend, key, origin_tag);
236
237   [pool drain];
238 }
239
240 static void
241 g_nextstep_settings_backend_subscribe (GSettingsBackend *backend,
242                                        const gchar      *name)
243 {
244 }
245
246 static void
247 g_nextstep_settings_backend_unsubscribe (GSettingsBackend *backend,
248                                          const gchar      *name)
249 {
250 }
251
252 static void
253 g_nextstep_settings_backend_sync (GSettingsBackend *backend)
254 {
255   GNextstepSettingsBackend *self = G_NEXTSTEP_SETTINGS_BACKEND (backend);
256   NSAutoreleasePool *pool;
257
258   pool = [[NSAutoreleasePool alloc] init];
259
260   g_mutex_lock (&self->mutex);
261   [self->user_defaults synchronize];
262   g_mutex_unlock (&self->mutex);
263
264   [pool drain];
265 }
266
267 static GPermission *
268 g_nextstep_settings_backend_get_permission (GSettingsBackend *backend,
269                                             const gchar      *path)
270 {
271   return g_simple_permission_new (TRUE);
272 }
273
274 static gboolean
275 g_nextstep_settings_backend_write_pair (gpointer name,
276                                         gpointer value,
277                                         gpointer data)
278 {
279   GNextstepSettingsBackend *backend = G_NEXTSTEP_SETTINGS_BACKEND (data);
280   NSString *key;
281   id object;
282
283   key = [NSString stringWithUTF8String:name];
284   object = g_nextstep_settings_backend_get_ns_object (value);
285
286   [backend->user_defaults setObject:object forKey:key];
287
288   return FALSE;
289 }
290
291 static GVariant *
292 g_nextstep_settings_backend_get_g_variant (id                  object,
293                                            const GVariantType *type)
294 {
295   if ([object isKindOfClass:[NSData class]])
296     return g_variant_parse (type, [[[[NSString alloc] initWithData:object encoding:NSUTF8StringEncoding] autorelease] UTF8String], NULL, NULL, NULL);
297   else if ([object isKindOfClass:[NSNumber class]])
298     {
299       if (g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN))
300         return g_variant_new_boolean ([object boolValue]);
301       else if (g_variant_type_equal (type, G_VARIANT_TYPE_BYTE))
302         return g_variant_new_byte ([object unsignedCharValue]);
303       else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT16))
304         return g_variant_new_int16 ([object shortValue]);
305       else if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT16))
306         return g_variant_new_uint16 ([object unsignedShortValue]);
307       else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT32))
308         return g_variant_new_int32 ([object longValue]);
309       else if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT32))
310         return g_variant_new_uint32 ([object unsignedLongValue]);
311       else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT64))
312         return g_variant_new_int64 ([object longLongValue]);
313       else if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT64))
314         return g_variant_new_uint64 ([object unsignedLongLongValue]);
315       else if (g_variant_type_equal (type, G_VARIANT_TYPE_HANDLE))
316         return g_variant_new_handle ([object longValue]);
317       else if (g_variant_type_equal (type, G_VARIANT_TYPE_DOUBLE))
318         return g_variant_new_double ([object doubleValue]);
319     }
320   else if ([object isKindOfClass:[NSString class]])
321     {
322       const char *string;
323
324       string = [object UTF8String];
325
326       if (g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
327         return g_variant_new_string (string);
328       else if (g_variant_type_equal (type, G_VARIANT_TYPE_OBJECT_PATH))
329         return g_variant_is_object_path (string) ?
330                g_variant_new_object_path (string) : NULL;
331       else if (g_variant_type_equal (type, G_VARIANT_TYPE_SIGNATURE))
332         return g_variant_is_signature (string) ?
333                g_variant_new_signature (string) : NULL;
334     }
335   else if ([object isKindOfClass:[NSDictionary class]])
336     {
337       if (g_variant_type_is_subtype_of (type, G_VARIANT_TYPE ("a{s*}")))
338         {
339           const GVariantType *value_type;
340           GVariantBuilder builder;
341           NSString *key;
342
343           value_type = g_variant_type_value (g_variant_type_element (type));
344
345           g_variant_builder_init (&builder, type);
346
347 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1050
348           for(key in object)
349 #else
350           NSEnumerator *enumerator = [object objectEnumerator];
351           while((key = [enumerator nextObject]))
352 #endif
353             {
354               GVariant *name;
355               id value;
356               GVariant *variant;
357               GVariant *entry;
358
359               name = g_variant_new_string ([key UTF8String]);
360               value = [object objectForKey:key];
361               variant = g_nextstep_settings_backend_get_g_variant (value, value_type);
362
363               if (variant == NULL)
364                 {
365                   g_variant_builder_clear (&builder);
366
367                   return NULL;
368                 }
369
370               entry = g_variant_new_dict_entry (name, variant);
371               g_variant_builder_add_value (&builder, entry);
372             }
373
374           return g_variant_builder_end (&builder);
375         }
376     }
377   else if ([object isKindOfClass:[NSArray class]])
378     {
379       if (g_variant_type_is_subtype_of (type, G_VARIANT_TYPE_ARRAY))
380         {
381           const GVariantType *value_type;
382           GVariantBuilder builder;
383           id value;
384
385           value_type = g_variant_type_element (type);
386           g_variant_builder_init (&builder, type);
387
388 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1050
389           for(value in object)
390 #else
391           NSEnumerator *enumerator = [object objectEnumerator];
392           while((value = [enumerator nextObject]))
393 #endif
394             {
395               GVariant *variant = g_nextstep_settings_backend_get_g_variant (value, value_type);
396
397               if (variant == NULL)
398                 {
399                   g_variant_builder_clear (&builder);
400
401                   return NULL;
402                 }
403
404               g_variant_builder_add_value (&builder, variant);
405             }
406
407           return g_variant_builder_end (&builder);
408         }
409     }
410
411   return NULL;
412 }
413
414 static id
415 g_nextstep_settings_backend_get_ns_object (GVariant *variant)
416 {
417   if (variant == NULL)
418     return nil;
419   else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_BOOLEAN))
420     return [NSNumber numberWithBool:g_variant_get_boolean (variant)];
421   else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_BYTE))
422     return [NSNumber numberWithUnsignedChar:g_variant_get_byte (variant)];
423   else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_INT16))
424     return [NSNumber numberWithShort:g_variant_get_int16 (variant)];
425   else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_UINT16))
426     return [NSNumber numberWithUnsignedShort:g_variant_get_uint16 (variant)];
427   else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_INT32))
428     return [NSNumber numberWithLong:g_variant_get_int32 (variant)];
429   else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_UINT32))
430     return [NSNumber numberWithUnsignedLong:g_variant_get_uint32 (variant)];
431   else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_INT64))
432     return [NSNumber numberWithLongLong:g_variant_get_int64 (variant)];
433   else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_UINT64))
434     return [NSNumber numberWithUnsignedLongLong:g_variant_get_uint64 (variant)];
435   else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_HANDLE))
436     return [NSNumber numberWithLong:g_variant_get_handle (variant)];
437   else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_DOUBLE))
438     return [NSNumber numberWithDouble:g_variant_get_double (variant)];
439   else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_STRING))
440     return [NSString stringWithUTF8String:g_variant_get_string (variant, NULL)];
441   else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_OBJECT_PATH))
442     return [NSString stringWithUTF8String:g_variant_get_string (variant, NULL)];
443   else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_SIGNATURE))
444     return [NSString stringWithUTF8String:g_variant_get_string (variant, NULL)];
445   else if (g_variant_is_of_type (variant, G_VARIANT_TYPE ("a{s*}")))
446     {
447       NSMutableDictionary *dictionary;
448       GVariantIter iter;
449       GVariant *name;
450       GVariant *value;
451
452       dictionary = [NSMutableDictionary dictionaryWithCapacity:g_variant_iter_init (&iter, variant)];
453
454       while (g_variant_iter_loop (&iter, "{s*}", &name, &value))
455         {
456           NSString *key;
457           id object;
458
459           key = [NSString stringWithUTF8String:g_variant_get_string (name, NULL)];
460           object = g_nextstep_settings_backend_get_ns_object (value);
461
462           [dictionary setObject:object forKey:key];
463         }
464
465       return dictionary;
466     }
467   else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_ARRAY))
468     {
469       NSMutableArray *array;
470       GVariantIter iter;
471       GVariant *value;
472
473       array = [NSMutableArray arrayWithCapacity:g_variant_iter_init (&iter, variant)];
474
475       while ((value = g_variant_iter_next_value (&iter)) != NULL)
476         [array addObject:g_nextstep_settings_backend_get_ns_object (value)];
477
478       return array;
479     }
480   else
481     return [[NSString stringWithUTF8String:g_variant_print (variant, TRUE)] dataUsingEncoding:NSUTF8StringEncoding];
482 }