2 * Copyright © 2010 Codethink Limited
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.
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.
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/>.
17 * Author: Ryan Lortie <desrt@desrt.ca>
22 #include "gsimplepermission.h"
23 #include "gsettingsbackendinternal.h"
24 #include "giomodule.h"
27 #define G_TYPE_MEMORY_SETTINGS_BACKEND (g_memory_settings_backend_get_type())
28 #define G_MEMORY_SETTINGS_BACKEND(inst) (G_TYPE_CHECK_INSTANCE_CAST ((inst), \
29 G_TYPE_MEMORY_SETTINGS_BACKEND, \
30 GMemorySettingsBackend))
32 typedef GSettingsBackendClass GMemorySettingsBackendClass
;
35 GSettingsBackend parent_instance
;
37 } GMemorySettingsBackend
;
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))
46 g_memory_settings_backend_read (GSettingsBackend
*backend
,
48 const GVariantType
*expected_type
,
49 gboolean default_value
)
51 GMemorySettingsBackend
*memory
= G_MEMORY_SETTINGS_BACKEND (backend
);
57 value
= g_hash_table_lookup (memory
->table
, key
);
60 g_variant_ref (value
);
66 g_memory_settings_backend_write (GSettingsBackend
*backend
,
71 GMemorySettingsBackend
*memory
= G_MEMORY_SETTINGS_BACKEND (backend
);
74 old_value
= g_hash_table_lookup (memory
->table
, key
);
75 g_variant_ref_sink (value
);
77 if (old_value
== NULL
|| !g_variant_equal (value
, old_value
))
79 g_hash_table_insert (memory
->table
, g_strdup (key
), value
);
80 g_settings_backend_changed (backend
, key
, origin_tag
);
83 g_variant_unref (value
);
89 g_memory_settings_backend_write_one (gpointer key
,
93 GMemorySettingsBackend
*memory
= data
;
96 g_hash_table_insert (memory
->table
, g_strdup (key
), g_variant_ref (value
));
98 g_hash_table_remove (memory
->table
, key
);
104 g_memory_settings_backend_write_tree (GSettingsBackend
*backend
,
108 g_tree_foreach (tree
, g_memory_settings_backend_write_one
, backend
);
109 g_settings_backend_changed_tree (backend
, tree
, origin_tag
);
115 g_memory_settings_backend_reset (GSettingsBackend
*backend
,
119 GMemorySettingsBackend
*memory
= G_MEMORY_SETTINGS_BACKEND (backend
);
121 if (g_hash_table_lookup (memory
->table
, key
))
123 g_hash_table_remove (memory
->table
, key
);
124 g_settings_backend_changed (backend
, key
, origin_tag
);
129 g_memory_settings_backend_get_writable (GSettingsBackend
*backend
,
136 g_memory_settings_backend_get_permission (GSettingsBackend
*backend
,
139 return g_simple_permission_new (TRUE
);
143 g_memory_settings_backend_finalize (GObject
*object
)
145 GMemorySettingsBackend
*memory
= G_MEMORY_SETTINGS_BACKEND (object
);
147 g_hash_table_unref (memory
->table
);
149 G_OBJECT_CLASS (g_memory_settings_backend_parent_class
)
154 g_memory_settings_backend_init (GMemorySettingsBackend
*memory
)
156 memory
->table
= g_hash_table_new_full (g_str_hash
, g_str_equal
, g_free
,
157 (GDestroyNotify
) g_variant_unref
);
161 g_memory_settings_backend_class_init (GMemorySettingsBackendClass
*class)
163 GSettingsBackendClass
*backend_class
= G_SETTINGS_BACKEND_CLASS (class);
164 GObjectClass
*object_class
= G_OBJECT_CLASS (class);
166 backend_class
->read
= g_memory_settings_backend_read
;
167 backend_class
->write
= g_memory_settings_backend_write
;
168 backend_class
->write_tree
= g_memory_settings_backend_write_tree
;
169 backend_class
->reset
= g_memory_settings_backend_reset
;
170 backend_class
->get_writable
= g_memory_settings_backend_get_writable
;
171 backend_class
->get_permission
= g_memory_settings_backend_get_permission
;
172 object_class
->finalize
= g_memory_settings_backend_finalize
;
176 * g_memory_settings_backend_new:
178 * Creates a memory-backed #GSettingsBackend.
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.
184 * Returns: (transfer full): a newly created #GSettingsBackend
189 g_memory_settings_backend_new (void)
191 return g_object_new (G_TYPE_MEMORY_SETTINGS_BACKEND
, NULL
);