5 * Copyright (C) 2007-2009 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 static GSList *storage_list = NULL;
30 static gint compare_priority(gconstpointer a, gconstpointer b)
32 const struct connman_storage *storage1 = a;
33 const struct connman_storage *storage2 = b;
35 return storage2->priority - storage1->priority;
39 * connman_storage_register:
40 * @storage: storage module
42 * Register a new storage module
44 * Returns: %0 on success
46 int connman_storage_register(struct connman_storage *storage)
48 DBG("storage %p name %s", storage, storage->name);
50 storage_list = g_slist_insert_sorted(storage_list, storage,
57 * connman_storage_unregister:
58 * @storage: storage module
60 * Remove a previously registered storage module
62 void connman_storage_unregister(struct connman_storage *storage)
64 DBG("storage %p name %s", storage, storage->name);
66 storage_list = g_slist_remove(storage_list, storage);
69 int __connman_storage_load_device(struct connman_device *device)
73 DBG("device %p", device);
75 for (list = storage_list; list; list = list->next) {
76 struct connman_storage *storage = list->data;
78 if (storage->device_load) {
79 if (storage->device_load(device) == 0)
87 int __connman_storage_save_device(struct connman_device *device)
91 DBG("device %p", device);
93 for (list = storage_list; list; list = list->next) {
94 struct connman_storage *storage = list->data;
96 if (storage->device_save) {
97 if (storage->device_save(device) == 0)
105 int __connman_storage_load_network(struct connman_network *network)
109 DBG("network %p", network);
111 for (list = storage_list; list; list = list->next) {
112 struct connman_storage *storage = list->data;
114 if (storage->network_load) {
115 if (storage->network_load(network) == 0)
123 int __connman_storage_save_network(struct connman_network *network)
127 DBG("network %p", network);
129 for (list = storage_list; list; list = list->next) {
130 struct connman_storage *storage = list->data;
132 if (storage->network_save) {
133 if (storage->network_save(network) == 0)
141 int __connman_storage_init(void)
148 void __connman_storage_cleanup(void)
153 static int do_load(GKeyFile *keyfile, struct connman_element *element)
157 DBG("element %p name %s", element, element->name);
159 value = g_key_file_get_string(keyfile, element->path,
162 element->policy = __connman_element_string2policy(value);
164 if (element->type == CONNMAN_ELEMENT_TYPE_NETWORK)
165 element->remember = g_key_file_get_boolean(keyfile,
166 element->path, "Remember", NULL);
168 value = g_key_file_get_string(keyfile, element->path,
169 "WiFi.Security", NULL);
171 connman_element_set_property(element,
172 CONNMAN_PROPERTY_ID_WIFI_SECURITY, &value);
174 value = g_key_file_get_string(keyfile, element->path,
175 "WiFi.Passphrase", NULL);
177 connman_element_set_property(element,
178 CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, &value);
183 int __connman_element_load(struct connman_element *element)
186 gchar *pathname, *data = NULL;
189 DBG("element %p name %s", element, element->name);
191 pathname = g_strdup_printf("%s/elements.conf", STORAGEDIR);
192 if (pathname == NULL)
195 keyfile = g_key_file_new();
197 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
204 if (g_key_file_load_from_data(keyfile, data, length,
212 do_load(keyfile, element);
214 g_key_file_free(keyfile);
219 static void do_update(GKeyFile *keyfile, struct connman_element *element)
225 DBG("element %p name %s", element, element->name);
227 g_key_file_set_string(keyfile, element->path, "Name", element->name);
229 str = __connman_element_policy2string(element->policy);
231 g_key_file_set_string(keyfile, element->path, "Policy", str);
233 //g_key_file_set_boolean(keyfile, element->path, "Enabled",
234 // element->enabled);
236 if (element->type == CONNMAN_ELEMENT_TYPE_NETWORK)
237 g_key_file_set_boolean(keyfile, element->path, "Remember",
240 __connman_element_lock(element);
242 for (list = element->properties; list; list = list->next) {
243 struct connman_property *property = list->data;
245 if (property->flags & CONNMAN_PROPERTY_FLAG_STATIC)
248 if (property->flags & CONNMAN_PROPERTY_FLAG_REFERENCE)
251 if (property->type == DBUS_TYPE_STRING)
252 g_key_file_set_string(keyfile, element->path,
253 property->name, property->value);
256 __connman_element_unlock(element);
258 if (connman_element_get_value(element,
259 CONNMAN_PROPERTY_ID_WIFI_SECURITY, &value) == 0)
260 g_key_file_set_string(keyfile, element->path,
261 "WiFi.Security", value);
263 if (connman_element_get_value(element,
264 CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, &value) == 0)
265 g_key_file_set_string(keyfile, element->path,
266 "WiFi.Passphrase", value);
269 int __connman_element_store(struct connman_element *element)
272 gchar *pathname, *data = NULL;
275 DBG("element %p name %s", element, element->name);
277 if (element->type != CONNMAN_ELEMENT_TYPE_DEVICE &&
278 element->type != CONNMAN_ELEMENT_TYPE_NETWORK)
281 if (element->subtype == CONNMAN_ELEMENT_SUBTYPE_FAKE)
284 pathname = g_strdup_printf("%s/elements.conf", STORAGEDIR);
285 if (pathname == NULL)
288 keyfile = g_key_file_new();
290 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
294 if (g_key_file_load_from_data(keyfile, data, length,
302 do_update(keyfile, element);
304 data = g_key_file_to_data(keyfile, &length, NULL);
306 g_file_set_contents(pathname, data, length, NULL);
311 g_key_file_free(keyfile);