5 * Copyright (C) 2007-2010 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
31 #define SETTINGS "settings"
32 #define DEFAULT "default.profile"
34 static GKeyFile *storage_load(const char *pathname)
36 GKeyFile *keyfile = NULL;
39 DBG("Loading %s", pathname);
41 keyfile = g_key_file_new();
43 if (!g_key_file_load_from_file(keyfile, pathname, 0, &error)) {
44 DBG("Unable to load %s: %s", pathname, error->message);
45 g_clear_error(&error);
47 g_key_file_free(keyfile);
54 static void storage_save(GKeyFile *keyfile, char *pathname)
60 data = g_key_file_to_data(keyfile, &length, NULL);
62 if (!g_file_set_contents(pathname, data, length, &error)) {
63 DBG("Failed to store information: %s", error->message);
70 static void storage_delete(const char *pathname)
72 DBG("file path %s", pathname);
74 if (unlink(pathname) < 0)
75 connman_error("Failed to remove %s", pathname);
78 GKeyFile *__connman_storage_load_global()
81 GKeyFile *keyfile = NULL;
83 pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
87 keyfile = storage_load(pathname);
94 void __connman_storage_save_global(GKeyFile *keyfile)
98 pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
102 storage_save(keyfile, pathname);
107 void __connman_storage_delete_global()
111 pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
115 storage_delete(pathname);
120 GKeyFile *__connman_storage_load_config(const char *ident)
123 GKeyFile *keyfile = NULL;
125 pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
129 keyfile = storage_load(pathname);
136 void __connman_storage_save_config(GKeyFile *keyfile, const char *ident)
140 pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
144 storage_save(keyfile, pathname);
147 void __connman_storage_delete_config(const char *ident)
151 pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
155 storage_delete(pathname);
161 * This function migrates keys from default.profile to settings file.
162 * This can be removed once the migration is over.
164 void __connman_storage_migrate()
167 GKeyFile *keyfile_def = NULL;
168 GKeyFile *keyfile = NULL;
169 GError *error = NULL;
172 /* If setting file exists, migration has been done. */
173 keyfile = __connman_storage_load_global();
175 g_key_file_free(keyfile);
179 pathname = g_strdup_printf("%s/%s", STORAGEDIR, DEFAULT);
183 /* If default.profile doesn't exists, no need to migrate. */
184 keyfile_def = storage_load(pathname);
185 if (keyfile_def == NULL) {
190 /* Copy global settings from default.profile to settings. */
191 keyfile = g_key_file_new();
194 val = g_key_file_get_boolean(keyfile_def, "global",
195 "OfflineMode", &error);
197 g_clear_error(&error);
199 g_key_file_set_boolean(keyfile, "global",
203 val = g_key_file_get_boolean(keyfile_def, "WiFi",
206 g_clear_error(&error);
208 g_key_file_set_boolean(keyfile, "WiFi",
212 val = g_key_file_get_boolean(keyfile_def, "Bluetooth",
215 g_clear_error(&error);
217 g_key_file_set_boolean(keyfile, "Bluetooth",
221 val = g_key_file_get_boolean(keyfile_def, "Wired",
224 g_clear_error(&error);
226 g_key_file_set_boolean(keyfile, "Wired",
230 val = g_key_file_get_boolean(keyfile_def, "3G",
233 g_clear_error(&error);
235 g_key_file_set_boolean(keyfile, "3G",
239 val = g_key_file_get_boolean(keyfile_def, "WiMAX",
242 g_clear_error(&error);
244 g_key_file_set_boolean(keyfile, "WiMAX",
247 __connman_storage_save_global(keyfile);
249 g_key_file_free(keyfile);
250 g_key_file_free(keyfile_def);