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 "gsimplepermission.h"
26 #include "gsettingsbackend.h"
27 #include "giomodule.h"
28
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                                 gboolean            default_value)
53 {
54   GMemorySettingsBackend *memory = G_MEMORY_SETTINGS_BACKEND (backend);
55   GVariant *value;
56
57   if (default_value)
58     return NULL;
59
60   value = g_hash_table_lookup (memory->table, key);
61
62   if (value != NULL)
63     g_variant_ref (value);
64
65   return value;
66 }
67
68 static gboolean
69 g_memory_settings_backend_write (GSettingsBackend *backend,
70                                  const gchar      *key,
71                                  GVariant         *value,
72                                  gpointer          origin_tag)
73 {
74   GMemorySettingsBackend *memory = G_MEMORY_SETTINGS_BACKEND (backend);
75   GVariant *old_value;
76
77   old_value = g_hash_table_lookup (memory->table, key);
78   g_variant_ref_sink (value);
79
80   if (old_value == NULL || !g_variant_equal (value, old_value))
81     {
82       g_hash_table_insert (memory->table, g_strdup (key), value);
83       g_settings_backend_changed (backend, key, origin_tag);
84     }
85   else
86     g_variant_unref (value);
87
88   return TRUE;
89 }
90
91 static gboolean
92 g_memory_settings_backend_write_one (gpointer key,
93                                      gpointer value,
94                                      gpointer data)
95 {
96   GMemorySettingsBackend *memory = data;
97
98   if (value != NULL)
99     g_hash_table_insert (memory->table, g_strdup (key), g_variant_ref (value));
100   else
101     g_hash_table_remove (memory->table, key);
102
103   return FALSE;
104 }
105
106 static gboolean
107 g_memory_settings_backend_write_tree (GSettingsBackend *backend,
108                                       GTree            *tree,
109                                       gpointer          origin_tag)
110 {
111   g_tree_foreach (tree, g_memory_settings_backend_write_one, backend);
112   g_settings_backend_changed_tree (backend, tree, origin_tag);
113
114   return TRUE;
115 }
116
117 static void
118 g_memory_settings_backend_reset (GSettingsBackend *backend,
119                                  const gchar      *key,
120                                  gpointer          origin_tag)
121 {
122   GMemorySettingsBackend *memory = G_MEMORY_SETTINGS_BACKEND (backend);
123
124   if (g_hash_table_lookup (memory->table, key))
125     {
126       g_hash_table_remove (memory->table, key);
127       g_settings_backend_changed (backend, key, origin_tag);
128     }
129 }
130
131 static gboolean
132 g_memory_settings_backend_get_writable (GSettingsBackend *backend,
133                                         const gchar      *name)
134 {
135   return TRUE;
136 }
137
138 static GPermission *
139 g_memory_settings_backend_get_permission (GSettingsBackend *backend,
140                                           const gchar      *path)
141 {
142   return g_simple_permission_new (TRUE);
143 }
144
145 static void
146 g_memory_settings_backend_finalize (GObject *object)
147 {
148   GMemorySettingsBackend *memory = G_MEMORY_SETTINGS_BACKEND (object);
149
150   g_hash_table_unref (memory->table);
151
152   G_OBJECT_CLASS (g_memory_settings_backend_parent_class)
153     ->finalize (object);
154 }
155
156 static void
157 g_memory_settings_backend_init (GMemorySettingsBackend *memory)
158 {
159   memory->table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
160                                          (GDestroyNotify) g_variant_unref);
161 }
162
163 static void
164 g_memory_settings_backend_class_init (GMemorySettingsBackendClass *class)
165 {
166   GSettingsBackendClass *backend_class = G_SETTINGS_BACKEND_CLASS (class);
167   GObjectClass *object_class = G_OBJECT_CLASS (class);
168
169   backend_class->read = g_memory_settings_backend_read;
170   backend_class->write = g_memory_settings_backend_write;
171   backend_class->write_tree = g_memory_settings_backend_write_tree;
172   backend_class->reset = g_memory_settings_backend_reset;
173   backend_class->get_writable = g_memory_settings_backend_get_writable;
174   backend_class->get_permission = g_memory_settings_backend_get_permission;
175   object_class->finalize = g_memory_settings_backend_finalize;
176 }