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