summaryrefslogtreecommitdiff
path: root/tests/dconf-mock-gvdb.c
blob: 4f58de1e4026bbd55e057b0bb39a01b3c0586fbe (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
#include "../gvdb/gvdb-reader.h"
#include "dconf-mock.h"

/* The global dconf_mock_gvdb_tables hashtable is modified all the time
 * so we need to hold the lock while we access it.
 *
 * The hashtables contained within it are never modified, however.  They
 * can be safely accessed without a lock.
 */

static GHashTable *dconf_mock_gvdb_tables;
static GMutex      dconf_mock_gvdb_lock;

typedef struct
{
  GVariant  *value;
  GvdbTable *table;
} DConfMockGvdbItem;

struct _GvdbTable
{
  GHashTable *table;
  gboolean    is_valid;
  gboolean    top_level;
  gint        ref_count;
};

static void
dconf_mock_gvdb_item_free (gpointer data)
{
  DConfMockGvdbItem *item = data;

  if (item->value)
    g_variant_unref (item->value);

  if (item->table)
    gvdb_table_free (item->table);

  g_slice_free (DConfMockGvdbItem, item);
}

static void
dconf_mock_gvdb_init (void)
{
  if (dconf_mock_gvdb_tables == NULL)
    dconf_mock_gvdb_tables = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) gvdb_table_free);
}

GvdbTable *
dconf_mock_gvdb_table_new (void)
{
  GvdbTable *table;

  table = g_slice_new (GvdbTable);
  table->table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, dconf_mock_gvdb_item_free);
  table->ref_count = 1;
  table->is_valid = TRUE;

  return table;
}

void
dconf_mock_gvdb_table_insert (GvdbTable   *table,
                              const gchar *name,
                              GVariant    *value,
                              GvdbTable   *subtable)
{
  DConfMockGvdbItem *item;

  g_assert (value == NULL || subtable == NULL);

  if (subtable)
    subtable->top_level = FALSE;

  item = g_slice_new (DConfMockGvdbItem);
  item->value = value ? g_variant_ref_sink (value) : NULL;
  item->table = subtable;

  g_hash_table_insert (table->table, g_strdup (name), item);
}

void
dconf_mock_gvdb_install (const gchar *filename,
                         GvdbTable   *table)
{
  g_mutex_lock (&dconf_mock_gvdb_lock);
  dconf_mock_gvdb_init ();

  if (table)
    {
      table->top_level = TRUE;
      g_hash_table_insert (dconf_mock_gvdb_tables, g_strdup (filename), table);
    }
  else
    g_hash_table_remove (dconf_mock_gvdb_tables, filename);

  g_mutex_unlock (&dconf_mock_gvdb_lock);
}

void
gvdb_table_free (GvdbTable *table)
{
  if (g_atomic_int_dec_and_test (&table->ref_count))
    {
      g_hash_table_unref (table->table);
      g_slice_free (GvdbTable, table);
    }
}

GvdbTable *
dconf_mock_gvdb_table_ref (GvdbTable *table)
{
  g_atomic_int_inc (&table->ref_count);

  return table;
}

GvdbTable *
gvdb_table_get_table (GvdbTable   *table,
                      const gchar *key)
{
  DConfMockGvdbItem *item;
  GvdbTable *subtable;

  item = g_hash_table_lookup (table->table, key);

  if (item && item->table)
    subtable = dconf_mock_gvdb_table_ref (item->table);
  else
    subtable = NULL;

  return subtable;
}

gboolean
gvdb_table_has_value (GvdbTable   *table,
                      const gchar *key)
{
  DConfMockGvdbItem *item;

  item = g_hash_table_lookup (table->table, key);

  return item && item->value;
}

GVariant *
gvdb_table_get_value (GvdbTable   *table,
                      const gchar *key)
{
  DConfMockGvdbItem *item;

  item = g_hash_table_lookup (table->table, key);

  return (item && item->value) ? g_variant_ref (item->value) : NULL;
}

gchar **
gvdb_table_list (GvdbTable   *table,
                 const gchar *key)
{
  const gchar * const result[] = { "value", NULL };

  g_assert_cmpstr (key, ==, "/");

  if (!gvdb_table_has_value (table, "/value"))
    return NULL;

  return g_strdupv ((gchar **) result);
}

gchar **
gvdb_table_get_names (GvdbTable *table,
                      gint      *length)
{
  if (length)
    *length = 0;

  return g_new0 (gchar *, 0 + 1);
}

GvdbTable *
gvdb_table_new (const gchar  *filename,
                gboolean      trusted,
                GError      **error)
{
  GvdbTable *table;

  g_mutex_lock (&dconf_mock_gvdb_lock);
  dconf_mock_gvdb_init ();
  table = g_hash_table_lookup (dconf_mock_gvdb_tables, filename);
  if (table)
    dconf_mock_gvdb_table_ref (table);
  g_mutex_unlock (&dconf_mock_gvdb_lock);

  if (table == NULL)
    g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT, "this gvdb does not exist");

  return table;
}

gboolean
gvdb_table_is_valid (GvdbTable *table)
{
  return table->is_valid;
}

void
dconf_mock_gvdb_table_invalidate (GvdbTable *table)
{
  table->is_valid = FALSE;
}