hook gvariant vectors up to kdbus
[platform/upstream/glib.git] / gio / gnextstepsettingsbackend.c
index ac4a105..f9b808c 100644 (file)
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  *
  * Author: William Hua <william@attente.ca>
  */
 
-
+#include "config.h"
 
 #include "gsettingsbackendinternal.h"
 #include "gsimplepermission.h"
 #include "giomodule.h"
 
-
-
 #import <Foundation/Foundation.h>
 
-
+GType g_nextstep_settings_backend_get_type (void);
 
 #define G_NEXTSTEP_SETTINGS_BACKEND(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), g_nextstep_settings_backend_get_type (), GNextstepSettingsBackend))
 
-
-
 typedef struct _GNextstepSettingsBackend GNextstepSettingsBackend;
 typedef GSettingsBackendClass            GNextstepSettingsBackendClass;
 
-
-
 struct _GNextstepSettingsBackend
 {
   GSettingsBackend  parent_instance;
 
   /*< private >*/
   NSUserDefaults   *user_defaults;
+  GMutex            mutex;
 };
 
-
-
 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,
@@ -101,25 +90,21 @@ static GVariant *    g_nextstep_settings_backend_get_g_variant  (id
 
 static id            g_nextstep_settings_backend_get_ns_object  (GVariant           *variant);
 
-
-
 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;
+  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)
 {
@@ -129,44 +114,50 @@ g_nextstep_settings_backend_init (GNextstepSettingsBackend *self)
 
   self->user_defaults = [[NSUserDefaults standardUserDefaults] retain];
 
+  g_mutex_init (&self->mutex);
+
   [pool drain];
 }
 
-
-
 static void
 g_nextstep_settings_backend_finalize (GObject *self)
 {
+  GNextstepSettingsBackend *backend = G_NEXTSTEP_SETTINGS_BACKEND (self);
   NSAutoreleasePool *pool;
 
   pool = [[NSAutoreleasePool alloc] init];
 
-  [G_NEXTSTEP_SETTINGS_BACKEND (self)->user_defaults release];
+  g_mutex_clear (&backend->mutex);
+
+  [backend->user_defaults release];
 
   [pool drain];
 
   G_OBJECT_CLASS (g_nextstep_settings_backend_parent_class)->finalize (self);
 }
 
-
-
 static GVariant *
 g_nextstep_settings_backend_read (GSettingsBackend   *backend,
                                   const gchar        *key,
                                   const GVariantType *expected_type,
                                   gboolean            default_value)
 {
+  GNextstepSettingsBackend *self = G_NEXTSTEP_SETTINGS_BACKEND (backend);
   NSAutoreleasePool *pool;
-  NSString          *name;
-  id                 value;
-  GVariant          *variant;
+  NSString *name;
+  id value;
+  GVariant *variant;
 
   if (default_value)
     return NULL;
 
-  pool    = [[NSAutoreleasePool alloc] init];
-  name    = [NSString stringWithUTF8String:key];
-  value   = [G_NEXTSTEP_SETTINGS_BACKEND (backend)->user_defaults objectForKey:name];
+  pool = [[NSAutoreleasePool alloc] init];
+  name = [NSString stringWithUTF8String:key];
+
+  g_mutex_lock (&self->mutex);
+  value = [self->user_defaults objectForKey:name];
+  g_mutex_unlock (&self->mutex);
+
   variant = g_nextstep_settings_backend_get_g_variant (value, expected_type);
 
   [pool drain];
@@ -174,8 +165,6 @@ g_nextstep_settings_backend_read (GSettingsBackend   *backend,
   return variant;
 }
 
-
-
 static gboolean
 g_nextstep_settings_backend_get_writable (GSettingsBackend *backend,
                                           const gchar      *key)
@@ -183,19 +172,21 @@ g_nextstep_settings_backend_get_writable (GSettingsBackend *backend,
   return TRUE;
 }
 
-
-
 static gboolean
 g_nextstep_settings_backend_write (GSettingsBackend *backend,
                                    const gchar      *key,
                                    GVariant         *value,
                                    gpointer          origin_tag)
 {
+  GNextstepSettingsBackend *self = G_NEXTSTEP_SETTINGS_BACKEND (backend);
   NSAutoreleasePool *pool;
 
   pool = [[NSAutoreleasePool alloc] init];
 
-  g_nextstep_settings_backend_write_pair ((gpointer) key, value, backend);
+  g_mutex_lock (&self->mutex);
+  g_nextstep_settings_backend_write_pair ((gpointer) key, value, self);
+  g_mutex_unlock (&self->mutex);
+
   g_settings_backend_changed (backend, key, origin_tag);
 
   [pool drain];
@@ -203,18 +194,19 @@ g_nextstep_settings_backend_write (GSettingsBackend *backend,
   return TRUE;
 }
 
-
-
 static gboolean
 g_nextstep_settings_backend_write_tree (GSettingsBackend *backend,
                                         GTree            *tree,
                                         gpointer          origin_tag)
 {
+  GNextstepSettingsBackend *self = G_NEXTSTEP_SETTINGS_BACKEND (backend);
   NSAutoreleasePool *pool;
 
   pool = [[NSAutoreleasePool alloc] init];
 
-  g_tree_foreach (tree, g_nextstep_settings_backend_write_pair, backend);
+  g_mutex_lock (&self->mutex);
+  g_tree_foreach (tree, g_nextstep_settings_backend_write_pair, self);
+  g_mutex_unlock (&self->mutex);
   g_settings_backend_changed_tree (backend, tree, origin_tag);
 
   [pool drain];
@@ -222,66 +214,54 @@ g_nextstep_settings_backend_write_tree (GSettingsBackend *backend,
   return TRUE;
 }
 
-
-
 static void
 g_nextstep_settings_backend_reset (GSettingsBackend *backend,
                                    const gchar      *key,
                                    gpointer          origin_tag)
 {
+  GNextstepSettingsBackend *self = G_NEXTSTEP_SETTINGS_BACKEND (backend);
   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];
+  NSString *name;
 
-  [user_defaults removeObjectForKey:name];
+  pool = [[NSAutoreleasePool alloc] init];
+  name = [NSString stringWithUTF8String:key];
 
-  default_value = [user_defaults objectForKey:name];
+  g_mutex_lock (&self->mutex);
+  [self->user_defaults removeObjectForKey:name];
+  g_mutex_unlock (&self->mutex);
 
-  if (default_value != value && ![default_value isEqual:value])
-    g_settings_backend_changed (backend, key, origin_tag);
+  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)
 {
+  GNextstepSettingsBackend *self = G_NEXTSTEP_SETTINGS_BACKEND (backend);
   NSAutoreleasePool *pool;
 
   pool = [[NSAutoreleasePool alloc] init];
 
-  [G_NEXTSTEP_SETTINGS_BACKEND (backend)->user_defaults synchronize];
+  g_mutex_lock (&self->mutex);
+  [self->user_defaults synchronize];
+  g_mutex_unlock (&self->mutex);
 
   [pool drain];
 }
 
-
-
 static GPermission *
 g_nextstep_settings_backend_get_permission (GSettingsBackend *backend,
                                             const gchar      *path)
@@ -289,28 +269,23 @@ g_nextstep_settings_backend_get_permission (GSettingsBackend *backend,
   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;
+  GNextstepSettingsBackend *backend = G_NEXTSTEP_SETTINGS_BACKEND (data);
+  NSString *key;
+  id object;
 
-  backend = G_NEXTSTEP_SETTINGS_BACKEND (data);
-  key     = [NSString stringWithUTF8String:name];
-  object  = g_nextstep_settings_backend_get_ns_object (value);
+  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)
@@ -349,10 +324,10 @@ g_nextstep_settings_backend_get_g_variant (id                  object,
       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) ?
+        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) ?
+        return g_variant_is_signature (string) ?
                g_variant_new_signature (string) : NULL;
     }
   else if ([object isKindOfClass:[NSDictionary class]])
@@ -360,22 +335,27 @@ g_nextstep_settings_backend_get_g_variant (id                  object,
       if (g_variant_type_is_subtype_of (type, G_VARIANT_TYPE ("a{s*}")))
         {
           const GVariantType *value_type;
-          GVariantBuilder     builder;
-          NSString           *key;
+          GVariantBuilder builder;
+          NSString *key;
 
           value_type = g_variant_type_value (g_variant_type_element (type));
 
           g_variant_builder_init (&builder, type);
 
-          for (key in object)
+#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1050
+          for(key in object)
+#else
+          NSEnumerator *enumerator = [object objectEnumerator];
+          while((key = [enumerator nextObject]))
+#endif
             {
               GVariant *name;
-              id        value;
+              id value;
               GVariant *variant;
               GVariant *entry;
 
-              name    = g_variant_new_string ([key UTF8String]);
-              value   = [object objectForKey:key];
+              name = g_variant_new_string ([key UTF8String]);
+              value = [object objectForKey:key];
               variant = g_nextstep_settings_backend_get_g_variant (value, value_type);
 
               if (variant == NULL)
@@ -397,13 +377,18 @@ g_nextstep_settings_backend_get_g_variant (id                  object,
       if (g_variant_type_is_subtype_of (type, G_VARIANT_TYPE_ARRAY))
         {
           const GVariantType *value_type;
-          GVariantBuilder     builder;
-          id                  value;
+          GVariantBuilder builder;
+          id value;
 
           value_type = g_variant_type_element (type);
           g_variant_builder_init (&builder, type);
 
-          for (value in object)
+#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1050
+          for(value in object)
+#else
+          NSEnumerator *enumerator = [object objectEnumerator];
+          while((value = [enumerator nextObject]))
+#endif
             {
               GVariant *variant = g_nextstep_settings_backend_get_g_variant (value, value_type);
 
@@ -424,8 +409,6 @@ g_nextstep_settings_backend_get_g_variant (id                  object,
   return NULL;
 }
 
-
-
 static id
 g_nextstep_settings_backend_get_ns_object (GVariant *variant)
 {
@@ -460,18 +443,18 @@ g_nextstep_settings_backend_get_ns_object (GVariant *variant)
   else if (g_variant_is_of_type (variant, G_VARIANT_TYPE ("a{s*}")))
     {
       NSMutableDictionary *dictionary;
-      GVariantIter         iter;
-      GVariant            *name;
-      GVariant            *value;
+      GVariantIter iter;
+      GVariant *name;
+      GVariant *value;
 
       dictionary = [NSMutableDictionary dictionaryWithCapacity:g_variant_iter_init (&iter, variant)];
 
       while (g_variant_iter_loop (&iter, "{s*}", &name, &value))
         {
           NSString *key;
-          id        object;
+          id object;
 
-          key    = [NSString stringWithUTF8String:g_variant_get_string (name, NULL)];
+          key = [NSString stringWithUTF8String:g_variant_get_string (name, NULL)];
           object = g_nextstep_settings_backend_get_ns_object (value);
 
           [dictionary setObject:object forKey:key];
@@ -482,8 +465,8 @@ g_nextstep_settings_backend_get_ns_object (GVariant *variant)
   else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_ARRAY))
     {
       NSMutableArray *array;
-      GVariantIter    iter;
-      GVariant       *value;
+      GVariantIter iter;
+      GVariant *value;
 
       array = [NSMutableArray arrayWithCapacity:g_variant_iter_init (&iter, variant)];