/src/glib/gio/gmemorysettingsbackend.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright © 2010 Codethink Limited  | 
3  |  |  *  | 
4  |  |  * SPDX-License-Identifier: LGPL-2.1-or-later  | 
5  |  |  *  | 
6  |  |  * This library is free software; you can redistribute it and/or  | 
7  |  |  * modify it under the terms of the GNU Lesser General Public  | 
8  |  |  * License as published by the Free Software Foundation; either  | 
9  |  |  * version 2.1 of the License, or (at your option) any later version.  | 
10  |  |  *  | 
11  |  |  * This library is distributed in the hope that it will be useful,  | 
12  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of  | 
13  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  | 
14  |  |  * Lesser General Public License for more details.  | 
15  |  |  *  | 
16  |  |  * You should have received a copy of the GNU Lesser General Public  | 
17  |  |  * License along with this library; if not, see <http://www.gnu.org/licenses/>.  | 
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-priv.h"  | 
27  |  |  | 
28  |  |  | 
29  | 0  | #define G_TYPE_MEMORY_SETTINGS_BACKEND  (g_memory_settings_backend_get_type())  | 
30  | 0  | #define G_MEMORY_SETTINGS_BACKEND(inst) (G_TYPE_CHECK_INSTANCE_CAST ((inst), \  | 
31  | 0  |                                          G_TYPE_MEMORY_SETTINGS_BACKEND,     \  | 
32  | 0  |                                          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_modules_ensure_extension_points_registered ();  | 
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  | 0  | { | 
54  | 0  |   GMemorySettingsBackend *memory = G_MEMORY_SETTINGS_BACKEND (backend);  | 
55  | 0  |   GVariant *value;  | 
56  |  | 
  | 
57  | 0  |   if (default_value)  | 
58  | 0  |     return NULL;  | 
59  |  |  | 
60  | 0  |   value = g_hash_table_lookup (memory->table, key);  | 
61  |  | 
  | 
62  | 0  |   if (value != NULL)  | 
63  | 0  |     g_variant_ref (value);  | 
64  |  | 
  | 
65  | 0  |   return value;  | 
66  | 0  | }  | 
67  |  |  | 
68  |  | static gboolean  | 
69  |  | g_memory_settings_backend_write (GSettingsBackend *backend,  | 
70  |  |                                  const gchar      *key,  | 
71  |  |                                  GVariant         *value,  | 
72  |  |                                  gpointer          origin_tag)  | 
73  | 0  | { | 
74  | 0  |   GMemorySettingsBackend *memory = G_MEMORY_SETTINGS_BACKEND (backend);  | 
75  | 0  |   GVariant *old_value;  | 
76  |  | 
  | 
77  | 0  |   old_value = g_hash_table_lookup (memory->table, key);  | 
78  | 0  |   g_variant_ref_sink (value);  | 
79  |  | 
  | 
80  | 0  |   if (old_value == NULL || !g_variant_equal (value, old_value))  | 
81  | 0  |     { | 
82  | 0  |       g_hash_table_insert (memory->table, g_strdup (key), value);  | 
83  | 0  |       g_settings_backend_changed (backend, key, origin_tag);  | 
84  | 0  |     }  | 
85  | 0  |   else  | 
86  | 0  |     g_variant_unref (value);  | 
87  |  | 
  | 
88  | 0  |   return TRUE;  | 
89  | 0  | }  | 
90  |  |  | 
91  |  | static gboolean  | 
92  |  | g_memory_settings_backend_write_one (gpointer key,  | 
93  |  |                                      gpointer value,  | 
94  |  |                                      gpointer data)  | 
95  | 0  | { | 
96  | 0  |   GMemorySettingsBackend *memory = data;  | 
97  |  | 
  | 
98  | 0  |   if (value != NULL)  | 
99  | 0  |     g_hash_table_insert (memory->table, g_strdup (key), g_variant_ref (value));  | 
100  | 0  |   else  | 
101  | 0  |     g_hash_table_remove (memory->table, key);  | 
102  |  | 
  | 
103  | 0  |   return FALSE;  | 
104  | 0  | }  | 
105  |  |  | 
106  |  | static gboolean  | 
107  |  | g_memory_settings_backend_write_tree (GSettingsBackend *backend,  | 
108  |  |                                       GTree            *tree,  | 
109  |  |                                       gpointer          origin_tag)  | 
110  | 0  | { | 
111  | 0  |   g_tree_foreach (tree, g_memory_settings_backend_write_one, backend);  | 
112  | 0  |   g_settings_backend_changed_tree (backend, tree, origin_tag);  | 
113  |  | 
  | 
114  | 0  |   return TRUE;  | 
115  | 0  | }  | 
116  |  |  | 
117  |  | static void  | 
118  |  | g_memory_settings_backend_reset (GSettingsBackend *backend,  | 
119  |  |                                  const gchar      *key,  | 
120  |  |                                  gpointer          origin_tag)  | 
121  | 0  | { | 
122  | 0  |   GMemorySettingsBackend *memory = G_MEMORY_SETTINGS_BACKEND (backend);  | 
123  |  | 
  | 
124  | 0  |   if (g_hash_table_lookup (memory->table, key))  | 
125  | 0  |     { | 
126  | 0  |       g_hash_table_remove (memory->table, key);  | 
127  | 0  |       g_settings_backend_changed (backend, key, origin_tag);  | 
128  | 0  |     }  | 
129  | 0  | }  | 
130  |  |  | 
131  |  | static gboolean  | 
132  |  | g_memory_settings_backend_get_writable (GSettingsBackend *backend,  | 
133  |  |                                         const gchar      *name)  | 
134  | 0  | { | 
135  | 0  |   return TRUE;  | 
136  | 0  | }  | 
137  |  |  | 
138  |  | static GPermission *  | 
139  |  | g_memory_settings_backend_get_permission (GSettingsBackend *backend,  | 
140  |  |                                           const gchar      *path)  | 
141  | 0  | { | 
142  | 0  |   return g_simple_permission_new (TRUE);  | 
143  | 0  | }  | 
144  |  |  | 
145  |  | static void  | 
146  |  | g_memory_settings_backend_finalize (GObject *object)  | 
147  | 0  | { | 
148  | 0  |   GMemorySettingsBackend *memory = G_MEMORY_SETTINGS_BACKEND (object);  | 
149  |  | 
  | 
150  | 0  |   g_hash_table_unref (memory->table);  | 
151  |  | 
  | 
152  | 0  |   G_OBJECT_CLASS (g_memory_settings_backend_parent_class)  | 
153  | 0  |     ->finalize (object);  | 
154  | 0  | }  | 
155  |  |  | 
156  |  | static void  | 
157  |  | g_memory_settings_backend_init (GMemorySettingsBackend *memory)  | 
158  | 0  | { | 
159  | 0  |   memory->table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,  | 
160  | 0  |                                          (GDestroyNotify) g_variant_unref);  | 
161  | 0  | }  | 
162  |  |  | 
163  |  | static void  | 
164  |  | g_memory_settings_backend_class_init (GMemorySettingsBackendClass *class)  | 
165  | 0  | { | 
166  | 0  |   GSettingsBackendClass *backend_class = G_SETTINGS_BACKEND_CLASS (class);  | 
167  | 0  |   GObjectClass *object_class = G_OBJECT_CLASS (class);  | 
168  |  | 
  | 
169  | 0  |   backend_class->read = g_memory_settings_backend_read;  | 
170  | 0  |   backend_class->write = g_memory_settings_backend_write;  | 
171  | 0  |   backend_class->write_tree = g_memory_settings_backend_write_tree;  | 
172  | 0  |   backend_class->reset = g_memory_settings_backend_reset;  | 
173  | 0  |   backend_class->get_writable = g_memory_settings_backend_get_writable;  | 
174  | 0  |   backend_class->get_permission = g_memory_settings_backend_get_permission;  | 
175  | 0  |   object_class->finalize = g_memory_settings_backend_finalize;  | 
176  | 0  | }  | 
177  |  |  | 
178  |  | /**  | 
179  |  |  * g_memory_settings_backend_new:  | 
180  |  |  *  | 
181  |  |  * Creates a memory-backed #GSettingsBackend.  | 
182  |  |  *  | 
183  |  |  * This backend allows changes to settings, but does not write them  | 
184  |  |  * to any backing storage, so the next time you run your application,  | 
185  |  |  * the memory backend will start out with the default values again.  | 
186  |  |  *  | 
187  |  |  * Returns: (transfer full): a newly created #GSettingsBackend  | 
188  |  |  *  | 
189  |  |  * Since: 2.28  | 
190  |  |  */  | 
191  |  | GSettingsBackend *  | 
192  |  | g_memory_settings_backend_new (void)  | 
193  | 0  | { | 
194  | 0  |   return g_object_new (G_TYPE_MEMORY_SETTINGS_BACKEND, NULL);  | 
195  | 0  | }  |