Merge remote branch 'gvdb/master'
[platform/upstream/glib.git] / gio / gmemorysettingsbackend.c
1 /*
2  * Copyright © 2010 Codethink Limited
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: Ryan Lortie <desrt@desrt.ca>
20  */
21
22 #include "config.h"
23
24 #include "gmemorysettingsbackend.h"
25 #include "gsettingsbackend.h"
26 #include "giomodule.h"
27
28 #include "gioalias.h"
29
30 #define G_TYPE_MEMORY_SETTINGS_BACKEND  (g_memory_settings_backend_get_type())
31 #define G_MEMORY_SETTINGS_BACKEND(inst) (G_TYPE_CHECK_INSTANCE_CAST ((inst), \
32                                          G_TYPE_MEMORY_SETTINGS_BACKEND,     \
33                                          GMemorySettingsBackend))
34
35 typedef GSettingsBackendClass GMemorySettingsBackendClass;
36 typedef struct
37 {
38   GSettingsBackend parent_instance;
39   GHashTable *table;
40 } GMemorySettingsBackend;
41
42 G_DEFINE_TYPE_WITH_CODE (GMemorySettingsBackend,
43                          g_memory_settings_backend,
44                          G_TYPE_SETTINGS_BACKEND,
45                          g_io_extension_point_implement (G_SETTINGS_BACKEND_EXTENSION_POINT_NAME,
46                                                          g_define_type_id, "memory", 10))
47
48 static GVariant *
49 g_memory_settings_backend_read (GSettingsBackend   *backend,
50                                 const gchar        *key,
51                                 const GVariantType *expected_type)
52 {
53   GVariant *value;
54
55   GMemorySettingsBackend *memory = G_MEMORY_SETTINGS_BACKEND (backend);
56
57   value = g_hash_table_lookup (memory->table, key);
58
59   if (value != NULL)
60     g_variant_ref (value);
61
62   return value;
63 }
64
65 static gboolean
66 g_memory_settings_backend_write (GSettingsBackend *backend,
67                                  const gchar      *key,
68                                  GVariant         *value,
69                                  gpointer          origin_tag)
70 {
71   GMemorySettingsBackend *memory = G_MEMORY_SETTINGS_BACKEND (backend);
72   GVariant *old_value;
73
74   old_value = g_hash_table_lookup (memory->table, key);
75   g_variant_ref_sink (value);
76
77   if (old_value == NULL || !g_variant_equal (value, old_value))
78     {
79       g_hash_table_insert (memory->table, g_strdup (key), value);
80       g_settings_backend_changed (backend, key, origin_tag);
81     }
82   else
83     g_variant_unref (value);
84
85   return TRUE;
86 }
87
88 static gboolean
89 g_memory_settings_backend_write_one (gpointer key,
90                                      gpointer value,
91                                      gpointer data)
92 {
93   GMemorySettingsBackend *memory = data;
94
95   g_hash_table_insert (memory->table, g_strdup (key), g_variant_ref (value));
96
97   return FALSE;
98 }
99
100 static gboolean
101 g_memory_settings_backend_write_keys (GSettingsBackend *backend,
102                                       GTree            *tree,
103                                       gpointer          origin_tag)
104 {
105   g_tree_foreach (tree, g_memory_settings_backend_write_one, backend);
106   g_settings_backend_changed_tree (backend, tree, origin_tag);
107
108   return TRUE;
109 }
110
111 static gboolean
112 g_memory_settings_backend_get_writable (GSettingsBackend *backend,
113                                         const gchar      *name)
114 {
115   return TRUE;
116 }
117
118 static void
119 g_memory_settings_backend_finalize (GObject *object)
120 {
121   GMemorySettingsBackend *memory = G_MEMORY_SETTINGS_BACKEND (object);
122
123   g_hash_table_unref (memory->table);
124
125   G_OBJECT_CLASS (g_memory_settings_backend_parent_class)
126     ->finalize (object);
127 }
128
129 static void
130 g_memory_settings_backend_init (GMemorySettingsBackend *memory)
131 {
132   memory->table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
133                                          (GDestroyNotify) g_variant_unref);
134 }
135
136 static void
137 g_memory_settings_backend_class_init (GMemorySettingsBackendClass *class)
138 {
139   GSettingsBackendClass *backend_class = G_SETTINGS_BACKEND_CLASS (class);
140   GObjectClass *object_class = G_OBJECT_CLASS (class);
141
142   backend_class->read = g_memory_settings_backend_read;
143   backend_class->write = g_memory_settings_backend_write;
144   backend_class->write_keys = g_memory_settings_backend_write_keys;
145   backend_class->get_writable = g_memory_settings_backend_get_writable;
146   object_class->finalize = g_memory_settings_backend_finalize;
147 }