summaryrefslogtreecommitdiff
path: root/gsettings/dconfsettingsbackend.c
blob: 6c8179b58587e757d7153be5c3d8dc5f6b52f57f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
 * Copyright © 2010 Codethink Limited
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the licence, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Ryan Lortie <desrt@desrt.ca>
 */

#include "config.h"

#define G_SETTINGS_ENABLE_BACKEND
#include <gio/gsettingsbackend.h>
#include "../engine/dconf-engine.h"
#include <gio/gio.h>

#include <string.h>

typedef GSettingsBackendClass DConfSettingsBackendClass;

typedef struct
{
  GSettingsBackend backend;
  DConfEngine     *engine;
} DConfSettingsBackend;

static GType dconf_settings_backend_get_type (void);
G_DEFINE_TYPE (DConfSettingsBackend, dconf_settings_backend, G_TYPE_SETTINGS_BACKEND)

static GVariant *
dconf_settings_backend_read (GSettingsBackend   *backend,
                             const gchar        *key,
                             const GVariantType *expected_type,
                             gboolean            default_value)
{
  DConfSettingsBackend *dcsb = (DConfSettingsBackend *) backend;

  return dconf_engine_read (dcsb->engine,
                            default_value ? DCONF_READ_DEFAULT_VALUE : 0,
                            NULL, key);
}

static GVariant *
dconf_settings_backend_read_user_value (GSettingsBackend   *backend,
                                        const gchar        *key,
                                        const GVariantType *expected_type)
{
  DConfSettingsBackend *dcsb = (DConfSettingsBackend *) backend;

  return dconf_engine_read (dcsb->engine, DCONF_READ_USER_VALUE, NULL, key);
}

static gboolean
dconf_settings_backend_write (GSettingsBackend *backend,
                              const gchar      *key,
                              GVariant         *value,
                              gpointer          origin_tag)
{
  DConfSettingsBackend *dcsb = (DConfSettingsBackend *) backend;
  DConfChangeset *change;
  gboolean success;

  change = dconf_changeset_new ();
  dconf_changeset_set (change, key, value);

  success = dconf_engine_change_fast (dcsb->engine, change, origin_tag, NULL);
  dconf_changeset_unref (change);

  return success;
}

static gboolean
dconf_settings_backend_add_to_changeset (gpointer key,
                                         gpointer value,
                                         gpointer data)
{
  dconf_changeset_set (data, key, value);

  return FALSE;
}

static gboolean
dconf_settings_backend_write_tree (GSettingsBackend *backend,
                                   GTree            *tree,
                                   gpointer          origin_tag)
{
  DConfSettingsBackend *dcsb = (DConfSettingsBackend *) backend;
  DConfChangeset *change;
  gboolean success;

  if (g_tree_nnodes (tree) == 0)
    return TRUE;

  change = dconf_changeset_new ();
  g_tree_foreach (tree, dconf_settings_backend_add_to_changeset, change);
  success = dconf_engine_change_fast (dcsb->engine, change, origin_tag, NULL);
  dconf_changeset_unref (change);

  return success;
}

static void
dconf_settings_backend_reset (GSettingsBackend *backend,
                              const gchar      *key,
                              gpointer          origin_tag)
{
  dconf_settings_backend_write (backend, key, NULL, origin_tag);
}

static gboolean
dconf_settings_backend_get_writable (GSettingsBackend *backend,
                                     const gchar      *name)
{
  DConfSettingsBackend *dcsb = (DConfSettingsBackend *) backend;

  return dconf_engine_is_writable (dcsb->engine, name);
}

static void
dconf_settings_backend_subscribe (GSettingsBackend *backend,
                                  const gchar      *name)
{
  DConfSettingsBackend *dcsb = (DConfSettingsBackend *) backend;

  dconf_engine_watch_fast (dcsb->engine, name);
}

static void
dconf_settings_backend_unsubscribe (GSettingsBackend *backend,
                                    const gchar      *name)
{
  DConfSettingsBackend *dcsb = (DConfSettingsBackend *) backend;

  dconf_engine_unwatch_fast (dcsb->engine, name);
}

static void
dconf_settings_backend_sync (GSettingsBackend *backend)
{
  DConfSettingsBackend *dcsb = (DConfSettingsBackend *) backend;

  dconf_engine_sync (dcsb->engine);
}

static void
dconf_settings_backend_free_weak_ref (gpointer data)
{
  GWeakRef *weak_ref = data;

  g_weak_ref_clear (weak_ref);
  g_slice_free (GWeakRef, weak_ref);
}

static void
dconf_settings_backend_init (DConfSettingsBackend *dcsb)
{
  GWeakRef *weak_ref;

  weak_ref = g_slice_new (GWeakRef);
  g_weak_ref_init (weak_ref, dcsb);
  dcsb->engine = dconf_engine_new (NULL, weak_ref, dconf_settings_backend_free_weak_ref);
}

static void
dconf_settings_backend_finalize (GObject *object)
{
  DConfSettingsBackend *dcsb = (DConfSettingsBackend *) object;

  dconf_engine_unref (dcsb->engine);

  G_OBJECT_CLASS (dconf_settings_backend_parent_class)
    ->finalize (object);
}

static void
dconf_settings_backend_class_init (GSettingsBackendClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);

  object_class->finalize = dconf_settings_backend_finalize;

  class->read = dconf_settings_backend_read;
  class->read_user_value = dconf_settings_backend_read_user_value;
  class->write = dconf_settings_backend_write;
  class->write_tree = dconf_settings_backend_write_tree;
  class->reset = dconf_settings_backend_reset;
  class->get_writable = dconf_settings_backend_get_writable;
  class->subscribe = dconf_settings_backend_subscribe;
  class->unsubscribe = dconf_settings_backend_unsubscribe;
  class->sync = dconf_settings_backend_sync;
}

void
g_io_module_load (GIOModule *module)
{
  g_type_module_use (G_TYPE_MODULE (module));
  g_io_extension_point_implement (G_SETTINGS_BACKEND_EXTENSION_POINT_NAME,
                                  dconf_settings_backend_get_type (),
                                  "dconf", 100);
}

void
g_io_module_unload (GIOModule *module)
{
  g_assert_not_reached ();
}

gchar **
g_io_module_query (void)
{
  return g_strsplit (G_SETTINGS_BACKEND_EXTENSION_POINT_NAME, "!", 0);
}

void
dconf_engine_change_notify (DConfEngine         *engine,
                            const gchar         *prefix,
                            const gchar * const *changes,
                            const gchar         *tag,
                            gboolean             is_writability,
                            gpointer             origin_tag,
                            gpointer             user_data)
{
  GWeakRef *weak_ref = user_data;
  DConfSettingsBackend *dcsb;
  g_debug ("change_notify: %s", prefix);

  dcsb = g_weak_ref_get (weak_ref);

  if (dcsb == NULL)
    return;

  if (changes[0] == NULL)
    return;

  if (is_writability)
    {
      /* We know that the engine does it this way... */
      g_assert (changes[0][0] == '\0' && changes[1] == NULL);

      if (g_str_has_suffix (prefix, "/"))
        g_settings_backend_path_writable_changed (G_SETTINGS_BACKEND (dcsb), prefix);
      else
        g_settings_backend_writable_changed (G_SETTINGS_BACKEND (dcsb), prefix);
    }

  /* We send the normal change notification even in the event that this
   * was a writability notification because adding/removing a lock could
   * impact the value that gets read.
   */
  if (changes[1] == NULL)
    {
      if (g_str_has_suffix (prefix, "/"))
        g_settings_backend_path_changed (G_SETTINGS_BACKEND (dcsb), prefix, origin_tag);
      else
        g_settings_backend_changed (G_SETTINGS_BACKEND (dcsb), prefix, origin_tag);
    }
  else
    g_settings_backend_keys_changed (G_SETTINGS_BACKEND (dcsb), prefix, changes, origin_tag);
}