Revert "GIOScheduler: Avoid constant iteration over pending job list"
[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 "gsimplepermission.h"
25 #include "gsettingsbackendinternal.h"
26 #include "giomodule.h"
27
28
29 #define G_TYPE_MEMORY_SETTINGS_BACKEND  (g_memory_settings_backend_get_type())
30 #define G_MEMORY_SETTINGS_BACKEND(inst) (G_TYPE_CHECK_INSTANCE_CAST ((inst), \
31                                          G_TYPE_MEMORY_SETTINGS_BACKEND,     \
32                                          GMemorySettingsBackend))
33
34 typedef GSettingsBackendClass GMemorySettingsBackendClass;
35 typedef struct
36 {
37   GSettingsBackend parent_instance;
38   GHashTable *table;
39 } GMemorySettingsBackend;
40
41 G_DEFINE_TYPE_WITH_CODE (GMemorySettingsBackend,
42                          g_memory_settings_backend,
43                          G_TYPE_SETTINGS_BACKEND,
44                          g_io_extension_point_implement (G_SETTINGS_BACKEND_EXTENSION_POINT_NAME,
45                                                          g_define_type_id, "memory", 10))
46
47 static GVariant *
48 g_memory_settings_backend_read (GSettingsBackend   *backend,
49                                 const gchar        *key,
50                                 const GVariantType *expected_type,
51                                 gboolean            default_value)
52 {
53   GMemorySettingsBackend *memory = G_MEMORY_SETTINGS_BACKEND (backend);
54   GVariant *value;
55
56   if (default_value)
57     return NULL;
58
59   value = g_hash_table_lookup (memory->table, key);
60
61   if (value != NULL)
62     g_variant_ref (value);
63
64   return value;
65 }
66
67 static gboolean
68 g_memory_settings_backend_write (GSettingsBackend *backend,
69                                  const gchar      *key,
70                                  GVariant         *value,
71                                  gpointer          origin_tag)
72 {
73   GMemorySettingsBackend *memory = G_MEMORY_SETTINGS_BACKEND (backend);
74   GVariant *old_value;
75
76   old_value = g_hash_table_lookup (memory->table, key);
77   g_variant_ref_sink (value);
78
79   if (old_value == NULL || !g_variant_equal (value, old_value))
80     {
81       g_hash_table_insert (memory->table, g_strdup (key), value);
82       g_settings_backend_changed (backend, key, origin_tag);
83     }
84   else
85     g_variant_unref (value);
86
87   return TRUE;
88 }
89
90 static gboolean
91 g_memory_settings_backend_write_one (gpointer key,
92                                      gpointer value,
93                                      gpointer data)
94 {
95   GMemorySettingsBackend *memory = data;
96
97   if (value != NULL)
98     g_hash_table_insert (memory->table, g_strdup (key), g_variant_ref (value));
99   else
100     g_hash_table_remove (memory->table, key);
101
102   return FALSE;
103 }
104
105 static gboolean
106 g_memory_settings_backend_write_tree (GSettingsBackend *backend,
107                                       GTree            *tree,
108                                       gpointer          origin_tag)
109 {
110   g_tree_foreach (tree, g_memory_settings_backend_write_one, backend);
111   g_settings_backend_changed_tree (backend, tree, origin_tag);
112
113   return TRUE;
114 }
115
116 static void
117 g_memory_settings_backend_reset (GSettingsBackend *backend,
118                                  const gchar      *key,
119                                  gpointer          origin_tag)
120 {
121   GMemorySettingsBackend *memory = G_MEMORY_SETTINGS_BACKEND (backend);
122
123   if (g_hash_table_lookup (memory->table, key))
124     {
125       g_hash_table_remove (memory->table, key);
126       g_settings_backend_changed (backend, key, origin_tag);
127     }
128 }
129
130 static gboolean
131 g_memory_settings_backend_get_writable (GSettingsBackend *backend,
132                                         const gchar      *name)
133 {
134   return TRUE;
135 }
136
137 static GPermission *
138 g_memory_settings_backend_get_permission (GSettingsBackend *backend,
139                                           const gchar      *path)
140 {
141   return g_simple_permission_new (TRUE);
142 }
143
144 static void
145 g_memory_settings_backend_finalize (GObject *object)
146 {
147   GMemorySettingsBackend *memory = G_MEMORY_SETTINGS_BACKEND (object);
148
149   g_hash_table_unref (memory->table);
150
151   G_OBJECT_CLASS (g_memory_settings_backend_parent_class)
152     ->finalize (object);
153 }
154
155 static void
156 g_memory_settings_backend_init (GMemorySettingsBackend *memory)
157 {
158   memory->table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
159                                          (GDestroyNotify) g_variant_unref);
160 }
161
162 static void
163 g_memory_settings_backend_class_init (GMemorySettingsBackendClass *class)
164 {
165   GSettingsBackendClass *backend_class = G_SETTINGS_BACKEND_CLASS (class);
166   GObjectClass *object_class = G_OBJECT_CLASS (class);
167
168   backend_class->read = g_memory_settings_backend_read;
169   backend_class->write = g_memory_settings_backend_write;
170   backend_class->write_tree = g_memory_settings_backend_write_tree;
171   backend_class->reset = g_memory_settings_backend_reset;
172   backend_class->get_writable = g_memory_settings_backend_get_writable;
173   backend_class->get_permission = g_memory_settings_backend_get_permission;
174   object_class->finalize = g_memory_settings_backend_finalize;
175 }
176
177 /**
178  * g_memory_settings_backend_new:
179  *
180  * Creates a memory-backed #GSettingsBackend.
181  *
182  * This backend allows changes to settings, but does not write them
183  * to any backing storage, so the next time you run your application,
184  * the memory backend will start out with the default values again.
185  *
186  * Returns: (transfer full): a newly created #GSettingsBackend
187  *
188  * Since: 2.28
189  */
190 GSettingsBackend *
191 g_memory_settings_backend_new (void)
192 {
193   return g_object_new (G_TYPE_MEMORY_SETTINGS_BACKEND, NULL);
194 }