summaryrefslogtreecommitdiff
path: root/service/dconf-rebuilder.c
blob: 3f170d6e7887907a896bb5d5b67ae229e2d32a3d (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
/*
 * 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, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Ryan Lortie <desrt@desrt.ca>
 */

#include "dconf-rebuilder.h"

#include <string.h>

#include "../gvdb/gvdb-reader.h"
#include "../gvdb/gvdb-builder.h"
#include "../common/dconf-paths.h"

static GvdbItem *
dconf_rebuilder_get_parent (GHashTable  *table,
                            const gchar *key)
{
  GvdbItem *grandparent, *parent;
  gchar *parent_name;
  gint len;

  if (g_str_equal (key, "/"))
    return NULL;

  len = strlen (key);
  if (key[len - 1] == '/')
    len--;

  while (key[len - 1] != '/')
    len--;

  parent_name = g_strndup (key, len);
  parent = g_hash_table_lookup (table, parent_name);

  if (parent == NULL)
    {
      parent = gvdb_hash_table_insert (table, parent_name);

      grandparent = dconf_rebuilder_get_parent (table, parent_name);

      if (grandparent != NULL)
        gvdb_item_set_parent (parent, grandparent);
    }

  return parent;
}

gboolean
dconf_rebuilder_rebuild (const gchar          *filename,
                         const gchar          *prefix,
                         const gchar * const  *keys,
                         GVariant * const     *values,
                         int                   n_items,
                         GError              **error)
{
  GHashTable *table;
  gboolean success;
  GHashTable *new;
  GvdbTable *old;
  gint i;

  table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_variant_unref);

  /* read in the old values */
  if ((old = gvdb_table_new (filename, FALSE, NULL)))
    {
      gchar **names;
      gint n_names;
      gint i;

      names = gvdb_table_get_names (old, &n_names);
      for (i = 0; i < n_names; i++)
        {
          if (dconf_is_key (names[i], NULL))
            {
              GVariant *value;

              value = gvdb_table_get_value (old, names[i]);

              if (value != NULL)
                {
                  g_hash_table_insert (table, names[i], value);
                  names[i] = NULL;
                }
            }

          g_free (names[i]);
        }

      gvdb_table_unref (old);
      g_free (names);
    }

  /* apply the requested changes */
  for (i = 0; i < n_items; i++)
    {
      gchar *path = g_strconcat (prefix, keys[i], NULL);

      /* Check if we are performing a path reset */
      if (g_str_has_suffix (path, "/"))
        {
          GHashTableIter iter;
          gpointer key;

          g_assert (values[i] == NULL);

          /* A path reset is really a request to delete all keys that
           * has a name starting with the reset path.
           */
          g_hash_table_iter_init (&iter, table);
          while (g_hash_table_iter_next (&iter, &key, NULL))
            if (g_str_has_prefix (key, path))
              g_hash_table_iter_remove (&iter);
        }

      if (values[i] != NULL)
        g_hash_table_insert (table, g_strdup (path), g_variant_ref (values[i]));
      else
        g_hash_table_remove (table, path);

      g_free (path);
    }

  /* convert back to GVDB format */
  {
    GHashTableIter iter;
    gpointer key, value;

    new = gvdb_hash_table_new (NULL, NULL);

    g_hash_table_iter_init (&iter, table);
    while (g_hash_table_iter_next (&iter, &key, &value))
      {
        GvdbItem *item;

        g_assert (g_hash_table_lookup (new, key) == NULL);
        item = gvdb_hash_table_insert (new, key);
        gvdb_item_set_parent (item, dconf_rebuilder_get_parent (new, key));
        gvdb_item_set_value (item, value);
      }
  }

  /* write the new file */
  success = gvdb_table_write_contents (new, filename, FALSE, error);

  /* clean up */
  g_hash_table_unref (table);
  g_hash_table_unref (new);

  return success;
}