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 read_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_static_config(GPtrArray *config, const char *var, const char **value)
103 while ((idx = get_config(config, idx + 1, "static", &val)) != -1) {
104 if (g_str_has_prefix(val, var)) {
106 *value = val + strlen(var);
116 save_config(const char *block, const char *name, GPtrArray *config)
122 otype = dbus_g_type_get_struct("GValueArray",
123 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INVALID);
124 otype = dbus_g_type_get_collection("GPtrArray", otype);
125 if (!dbus_g_proxy_call(dbus, "SetConfig", &error,
126 G_TYPE_STRING, block,
132 g_critical("SetConfig: %s", error->message);
133 g_clear_error(&error);
140 load_config(const char *block, const char *name, GPtrArray *array)
144 return read_config(block, name);
148 set_option(GPtrArray *array, bool sopt, const char *var, const char *val)
156 i = get_static_config(array, var, NULL);
158 i = get_config(array, 0, var, NULL);
161 va = g_ptr_array_remove_index(array, i);
162 g_value_array_free(va);
166 n = g_strconcat(var, val, NULL);
170 va = g_value_array_new(2);
171 memset(&nv, 0, sizeof(v));
172 g_value_init(&nv, G_TYPE_STRING);
173 g_value_set_static_string(&nv, sopt ? "static" : var);
174 va = g_value_array_append(va, &nv);
175 g_value_set_static_string(&nv, sopt ? n : val);
176 va = g_value_array_append(va, &nv);
177 g_ptr_array_add(array, va);
178 } else if (val != NULL) {
179 va = g_ptr_array_index(array, i);
180 v = g_value_array_get_nth(va, 1);
182 g_value_init(v, G_TYPE_STRING);
183 g_value_set_static_string(v, sopt ? n : val);