storage: Switch to settings file
[platform/upstream/connman.git] / src / storage.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27 #include <unistd.h>
28
29 #include "connman.h"
30
31 #define SETTINGS        "settings"
32 #define DEFAULT         "default.profile"
33
34 static GKeyFile *storage_load(const char *pathname)
35 {
36         GKeyFile *keyfile = NULL;
37         GError *error = NULL;
38
39         DBG("Loading %s", pathname);
40
41         keyfile = g_key_file_new();
42
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);
46
47                 g_key_file_free(keyfile);
48                 keyfile = NULL;
49         }
50
51         return keyfile;
52 }
53
54 static void storage_save(GKeyFile *keyfile, char *pathname)
55 {
56         gchar *data = NULL;
57         gsize length = 0;
58         GError *error = NULL;
59
60         data = g_key_file_to_data(keyfile, &length, NULL);
61
62         if (!g_file_set_contents(pathname, data, length, &error)) {
63                 DBG("Failed to store information: %s", error->message);
64                 g_free(error);
65         }
66
67         g_free(data);
68 }
69
70 static void storage_delete(const char *pathname)
71 {
72         DBG("file path %s", pathname);
73
74         if (unlink(pathname) < 0)
75                 connman_error("Failed to remove %s", pathname);
76 }
77
78 GKeyFile *__connman_storage_load_global()
79 {
80         gchar *pathname;
81         GKeyFile *keyfile = NULL;
82
83         pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
84         if(pathname == NULL)
85                 return NULL;
86
87         keyfile = storage_load(pathname);
88
89         g_free(pathname);
90
91         return keyfile;
92 }
93
94 void __connman_storage_save_global(GKeyFile *keyfile)
95 {
96         gchar *pathname;
97
98         pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
99         if(pathname == NULL)
100                 return;
101
102         storage_save(keyfile, pathname);
103
104         g_free(pathname);
105 }
106
107 void __connman_storage_delete_global()
108 {
109         gchar *pathname;
110
111         pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
112         if(pathname == NULL)
113                 return;
114
115         storage_delete(pathname);
116
117         g_free(pathname);
118 }
119
120 GKeyFile *__connman_storage_load_config(const char *ident)
121 {
122         gchar *pathname;
123         GKeyFile *keyfile = NULL;
124
125         pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
126         if(pathname == NULL)
127                 return NULL;
128
129         keyfile = storage_load(pathname);
130
131         g_free(pathname);
132
133         return keyfile;
134 }
135
136 void __connman_storage_save_config(GKeyFile *keyfile, const char *ident)
137 {
138         gchar *pathname;
139
140         pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
141         if(pathname == NULL)
142                 return;
143
144         storage_save(keyfile, pathname);
145 }
146
147 void __connman_storage_delete_config(const char *ident)
148 {
149         gchar *pathname;
150
151         pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
152         if(pathname == NULL)
153                 return;
154
155         storage_delete(pathname);
156
157         g_free(pathname);
158 }
159
160 /*
161  * This function migrates keys from default.profile to settings file.
162  * This can be removed once the migration is over.
163 */
164 void __connman_storage_migrate()
165 {
166         gchar *pathname;
167         GKeyFile *keyfile_def = NULL;
168         GKeyFile *keyfile = NULL;
169         GError *error = NULL;
170         connman_bool_t val;
171
172         /* If setting file exists, migration has been done. */
173         keyfile = __connman_storage_load_global();
174         if (keyfile) {
175                 g_key_file_free(keyfile);
176                 return;
177         }
178
179         pathname = g_strdup_printf("%s/%s", STORAGEDIR, DEFAULT);
180         if(pathname == NULL)
181                 return;
182
183         /* If default.profile doesn't exists, no need to migrate. */
184         keyfile_def = storage_load(pathname);
185         if (keyfile_def == NULL) {
186                 g_free(pathname);
187                 return;
188         }
189
190         /* Copy global settings from default.profile to settings. */
191         keyfile = g_key_file_new();
192
193         /* offline mode */
194         val = g_key_file_get_boolean(keyfile_def, "global",
195                                         "OfflineMode", &error);
196         if (error != NULL)
197                 g_clear_error(&error);
198         else
199                 g_key_file_set_boolean(keyfile, "global",
200                                         "OfflineMode", val);
201
202         /* wifi */
203         val = g_key_file_get_boolean(keyfile_def, "WiFi",
204                                         "Enable", &error);
205         if (error != NULL)
206                 g_clear_error(&error);
207         else
208                 g_key_file_set_boolean(keyfile, "WiFi",
209                                         "Enable", val);
210
211         /* bluetooth */
212         val = g_key_file_get_boolean(keyfile_def, "Bluetooth",
213                                         "Enable", &error);
214         if (error != NULL)
215                 g_clear_error(&error);
216         else
217                 g_key_file_set_boolean(keyfile, "Bluetooth",
218                                         "Enable", val);
219
220         /* wired */
221         val = g_key_file_get_boolean(keyfile_def, "Wired",
222                                         "Enable", &error);
223         if (error != NULL)
224                 g_clear_error(&error);
225         else
226                 g_key_file_set_boolean(keyfile, "Wired",
227                                         "Enable", val);
228
229         /* 3G */
230         val = g_key_file_get_boolean(keyfile_def, "3G",
231                                         "Enable", &error);
232         if (error != NULL)
233                 g_clear_error(&error);
234         else
235                 g_key_file_set_boolean(keyfile, "3G",
236                                         "Enable", val);
237
238         /* WiMAX */
239         val = g_key_file_get_boolean(keyfile_def, "WiMAX",
240                                         "Enable", &error);
241         if (error != NULL)
242                 g_clear_error(&error);
243         else
244                 g_key_file_set_boolean(keyfile, "WiMAX",
245                                         "Enable", val);
246
247         __connman_storage_save_global(keyfile);
248
249         g_key_file_free(keyfile);
250         g_key_file_free(keyfile_def);
251         g_free(pathname);
252 }