add get_permission API to GSettingsBackend
[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 #include "gioalias.h"
30
31 #define G_TYPE_MEMORY_SETTINGS_BACKEND  (g_memory_settings_backend_get_type())
32 #define G_MEMORY_SETTINGS_BACKEND(inst) (G_TYPE_CHECK_INSTANCE_CAST ((inst), \
33                                          G_TYPE_MEMORY_SETTINGS_BACKEND,     \
34                                          GMemorySettingsBackend))
35
36 typedef GSettingsBackendClass GMemorySettingsBackendClass;
37 typedef struct
38 {
39   GSettingsBackend parent_instance;
40   GHashTable *table;
41 } GMemorySettingsBackend;
42
43 G_DEFINE_TYPE_WITH_CODE (GMemorySettingsBackend,
44                          g_memory_settings_backend,
45                          G_TYPE_SETTINGS_BACKEND,
46                          g_io_extension_point_implement (G_SETTINGS_BACKEND_EXTENSION_POINT_NAME,
47                                                          g_define_type_id, "memory", 10))
48
49 static GVariant *
50 g_memory_settings_backend_read (GSettingsBackend   *backend,
51                                 const gchar        *key,
52                                 const GVariantType *expected_type,
53                                 gboolean            default_value)
54 {
55   GMemorySettingsBackend *memory = G_MEMORY_SETTINGS_BACKEND (backend);
56   GVariant *value;
57
58   if (default_value)
59     return NULL;
60
61   value = g_hash_table_lookup (memory->table, key);
62
63   if (value != NULL)
64     g_variant_ref (value);
65
66   return value;
67 }
68
69 static gboolean
70 g_memory_settings_backend_write (GSettingsBackend *backend,
71                                  const gchar      *key,
72                                  GVariant         *value,
73                                  gpointer          origin_tag)
74 {
75   GMemorySettingsBackend *memory = G_MEMORY_SETTINGS_BACKEND (backend);
76   GVariant *old_value;
77
78   old_value = g_hash_table_lookup (memory->table, key);
79   g_variant_ref_sink (value);
80
81   if (old_value == NULL || !g_variant_equal (value, old_value))
82     {
83       g_hash_table_insert (memory->table, g_strdup (key), value);
84       g_settings_backend_changed (backend, key, origin_tag);
85     }
86   else
87     g_variant_unref (value);
88
89   return TRUE;
90 }
91
92 static gboolean
93 g_memory_settings_backend_write_one (gpointer key,
94                                      gpointer value,
95                                      gpointer data)
96 {
97   GMemorySettingsBackend *memory = data;
98
99   g_hash_table_insert (memory->table, g_strdup (key), g_variant_ref (value));
100
101   return FALSE;
102 }
103
104 static gboolean
105 g_memory_settings_backend_write_keys (GSettingsBackend *backend,
106                                       GTree            *tree,
107                                       gpointer          origin_tag)
108 {
109   g_tree_foreach (tree, g_memory_settings_backend_write_one, backend);
110   g_settings_backend_changed_tree (backend, tree, origin_tag);
111
112   return TRUE;
113 }
114
115 static gboolean
116 g_memory_settings_backend_get_writable (GSettingsBackend *backend,
117                                         const gchar      *name)
118 {
119   return TRUE;
120 }
121
122 static GPermission *
123 g_memory_settings_backend_get_permission (GSettingsBackend *backend,
124                                           const gchar      *path)
125 {
126   return g_simple_permission_new (TRUE);
127 }
128
129 static void
130 g_memory_settings_backend_finalize (GObject *object)
131 {
132   GMemorySettingsBackend *memory = G_MEMORY_SETTINGS_BACKEND (object);
133
134   g_hash_table_unref (memory->table);
135
136   G_OBJECT_CLASS (g_memory_settings_backend_parent_class)
137     ->finalize (object);
138 }
139
140 static void
141 g_memory_settings_backend_init (GMemorySettingsBackend *memory)
142 {
143   memory->table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
144                                          (GDestroyNotify) g_variant_unref);
145 }
146
147 static void
148 g_memory_settings_backend_class_init (GMemorySettingsBackendClass *class)
149 {
150   GSettingsBackendClass *backend_class = G_SETTINGS_BACKEND_CLASS (class);
151   GObjectClass *object_class = G_OBJECT_CLASS (class);
152
153   backend_class->read = g_memory_settings_backend_read;
154   backend_class->write = g_memory_settings_backend_write;
155   backend_class->write_keys = g_memory_settings_backend_write_keys;
156   backend_class->get_writable = g_memory_settings_backend_get_writable;
157   backend_class->get_permission = g_memory_settings_backend_get_permission;
158   object_class->finalize = g_memory_settings_backend_finalize;
159 }