summaryrefslogtreecommitdiff
path: root/engine/dconf-engine-source.c
blob: 019a59ccd07a33040cfbb86ed1519afd74de9f2c (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
/*
 * Copyright © 2010 Codethink Limited
 * Copyright © 2012 Canonical 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"

#include "dconf-engine-source-private.h"

#include <string.h>

void
dconf_engine_source_free (DConfEngineSource *source)
{
  if (source->values)
    gvdb_table_free (source->values);

  if (source->locks)
    gvdb_table_free (source->locks);

  source->vtable->finalize (source);
  g_free (source->bus_name);
  g_free (source->object_path);
  g_free (source->name);
  g_free (source);
}

gboolean
dconf_engine_source_refresh (DConfEngineSource *source)
{
  if (source->vtable->needs_reopen (source))
    {
      gboolean was_open;
      gboolean is_open;

      /* Record if we had a gvdb before or not. */
      was_open = source->values != NULL;

      g_clear_pointer (&source->values, gvdb_table_free);
      g_clear_pointer (&source->locks, gvdb_table_free);

      source->values = source->vtable->reopen (source);
      if (source->values)
        source->locks = gvdb_table_get_table (source->values, ".locks");

      /* Check if we ended up with a gvdb. */
      is_open = source->values != NULL;

      /* Only return TRUE in the case that we either had a database
       * before or ended up with one after.  In the case that we just go
       * from NULL to NULL, return FALSE.
       */
      return was_open || is_open;
    }

  return FALSE;
}

DConfEngineSource *
dconf_engine_source_new (const gchar *description)
{
  const DConfEngineSourceVTable *vtable;
  DConfEngineSource *source;
  const gchar *colon;

  /* Source descriptions are of the form
   *
   *   type:name
   *
   * Where type must currently be one of "user-db" or "system-db".
   *
   * We first find the colon.
   */
  colon = strchr (description, ':');

  /* Ensure that we have a colon and that a database name follows it. */
  if (colon == NULL || colon[1] == '\0')
    return NULL;

  /* Check if the part before the colon is "user-db"... */
  if ((colon == description + 7) && memcmp (description, "user-db", 7) == 0)
    vtable = &dconf_engine_source_user_vtable;

  /* ...or "service-db" */
  else if ((colon == description + 10) && memcmp (description, "service-db", 10) == 0)
    vtable = &dconf_engine_source_service_vtable;

  /* ...or "system-db" */
  else if ((colon == description + 9) && memcmp (description, "system-db", 9) == 0)
    vtable = &dconf_engine_source_system_vtable;

  /* ...or "file-db" */
  else if ((colon == description + 7) && memcmp (description, "file-db", 7) == 0)
    vtable = &dconf_engine_source_file_vtable;

  /* If it's not any of those, we have failed. */
  else
    return NULL;

  /* We have had a successful parse.
   *
   *  - either user-db: or system-db:
   *  - non-NULL and non-empty database name
   *
   * Create the source.
   */
  source = g_malloc0 (vtable->instance_size);
  source->vtable = vtable;
  source->name = g_strdup (colon + 1);
  source->vtable->init (source);

  return source;
}

DConfEngineSource *
dconf_engine_source_new_default (void)
{
  DConfEngineSource *source;

  source = g_malloc0 (dconf_engine_source_user_vtable.instance_size);
  source->vtable = &dconf_engine_source_user_vtable;
  source->name = g_strdup ("user");
  source->vtable->init (source);

  return source;
}