3 * Copyright 2009 Roy Marples <roy@marples.name>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include "dhcpcd-config.h"
29 #include "dhcpcd-gtk.h"
32 free_config(GPtrArray **config)
36 if (config == NULL || *config == NULL)
38 for (i = 0; i < (*config)->len; i++)
39 g_value_array_free(g_ptr_array_index(*config, i));
40 g_ptr_array_free(*config, TRUE);
45 load_config(const char *block, const char *name)
52 otype = dbus_g_type_get_struct("GValueArray",
53 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INVALID);
54 otype = dbus_g_type_get_collection("GPtrArray", otype);
55 if (!dbus_g_proxy_call(dbus, "GetConfig", &error,
56 G_TYPE_STRING, block, G_TYPE_STRING, name, G_TYPE_INVALID,
57 otype, &config, G_TYPE_INVALID))
59 g_critical("GetConfig: %s", error->message);
60 g_clear_error(&error);
67 _get_config(GPtrArray *config, int idx, const char *opt, const char **value)
75 for (; (uint)idx < config->len; idx++) {
76 c = g_ptr_array_index(config, idx);
77 val = g_value_array_get_nth(c, 0);
78 str = g_value_get_string(val);
79 if (strcmp(str, opt) != 0)
82 val = g_value_array_get_nth(c, 1);
83 str = g_value_get_string(val);
97 get_config(GPtrArray *config, const char *opt, const char **value)
100 return _get_config(config, 0, opt, value);
104 get_config_static(GPtrArray *config, const char *var, const char **value)
110 while ((idx = _get_config(config, idx + 1, "static", &val)) != -1) {
111 if (g_str_has_prefix(val, var)) {
113 *value = val + strlen(var);
123 set_option(GPtrArray *config, bool sopt, const char *var, const char *val)
131 i = get_config_static(config, var, NULL);
133 i = get_config(config, var, NULL);
136 va = g_ptr_array_remove_index(config, i);
137 g_value_array_free(va);
141 n = g_strconcat(var, val, NULL);
144 memset(&nv, 0, sizeof(v));
145 g_value_init(&nv, G_TYPE_STRING);
147 va = g_value_array_new(2);
148 g_ptr_array_add(config, va);
149 g_value_set_static_string(&nv, sopt ? "static" : var);
150 va = g_value_array_append(va, &nv);
152 va = g_ptr_array_index(config, i);
153 va = g_value_array_remove(va, 1);
155 g_value_set_static_string(&nv, sopt ? n : val);
156 va = g_value_array_append(va, &nv);
162 save_config(const char *block, const char *name, GPtrArray *config)
168 otype = dbus_g_type_get_struct("GValueArray",
169 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INVALID);
170 otype = dbus_g_type_get_collection("GPtrArray", otype);
171 if (!dbus_g_proxy_call(dbus, "SetConfig", &error,
172 G_TYPE_STRING, block,
178 g_critical("SetConfig: %s", error->message);
179 g_clear_error(&error);