b0cb3efa142277eef241555c9a84ce3803013c59
[platform/upstream/connman.git] / src / profile.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 <string.h>
28
29 #include <glib.h>
30 #include <gdbus.h>
31
32 #include "connman.h"
33
34 #define PROFILE_DEFAULT_IDENT  "default"
35
36 struct connman_profile {
37         char *ident;
38         char *path;
39         char *name;
40         connman_bool_t offlinemode;
41 };
42
43 static struct connman_profile *default_profile = NULL;
44
45 static DBusConnection *connection = NULL;
46
47 connman_bool_t __connman_profile_get_offlinemode(void)
48 {
49         if (default_profile == NULL)
50                 return FALSE;
51
52         DBG("offlinemode %d", default_profile->offlinemode);
53
54         return default_profile->offlinemode;
55 }
56
57 int __connman_profile_set_offlinemode(connman_bool_t offlinemode)
58 {
59         DBG("offlinemode %d", offlinemode);
60
61         if (default_profile == NULL)
62                 return -EINVAL;
63
64         if (default_profile->offlinemode == offlinemode)
65                 return -EALREADY;
66
67         default_profile->offlinemode = offlinemode;
68         return 0;
69 }
70
71 int __connman_profile_save_default(void)
72 {
73         DBG("");
74
75         if (default_profile != NULL)
76                 __connman_storage_save_profile(default_profile);
77
78         return 0;
79 }
80
81 const char *__connman_profile_active_ident(void)
82 {
83         DBG("");
84
85         return PROFILE_DEFAULT_IDENT;
86 }
87
88 const char *__connman_profile_active_path(void)
89 {
90         DBG("");
91
92         if (default_profile == NULL)
93                 return NULL;
94
95         return default_profile->path;
96 }
97
98 static void free_profile(struct connman_profile *profile)
99 {
100         g_free(profile->name);
101         g_free(profile->path);
102         g_free(profile->ident);
103         g_free(profile);
104 }
105
106 static int profile_init(void)
107 {
108         DBG("");
109
110         default_profile = g_try_new0(struct connman_profile, 1);
111         if (default_profile == NULL)
112                 return -ENOMEM;
113
114         default_profile->ident = g_strdup(PROFILE_DEFAULT_IDENT);
115         default_profile->path = g_strdup_printf("/profile/%s",
116                                         PROFILE_DEFAULT_IDENT);
117
118         if (default_profile->ident == NULL || default_profile->path == NULL) {
119                 free_profile(default_profile);
120                 return -ENOMEM;
121         }
122
123         default_profile->name = g_strdup("Default");
124
125         __connman_storage_load_profile(default_profile);
126
127         connman_info("Adding default profile");
128
129         DBG("profile %p path %s", default_profile, default_profile->path);
130
131         return 0;
132 }
133
134 static int profile_load(struct connman_profile *profile)
135 {
136         GKeyFile *keyfile;
137         GError *error = NULL;
138         connman_bool_t offlinemode;
139         char *name;
140
141         DBG("profile %p", profile);
142
143         keyfile = __connman_storage_open_profile(profile->ident);
144         if (keyfile == NULL)
145                 return -EIO;
146
147         name = g_key_file_get_string(keyfile, "global", "Name", NULL);
148         if (name != NULL) {
149                 g_free(profile->name);
150                 profile->name = name;
151         }
152
153         offlinemode = g_key_file_get_boolean(keyfile, "global",
154                                                 "OfflineMode", &error);
155         if (error == NULL)
156                 profile->offlinemode = offlinemode;
157         g_clear_error(&error);
158
159         __connman_storage_close_profile(profile->ident, keyfile, FALSE);
160
161         return 0;
162 }
163
164 static int profile_save(struct connman_profile *profile)
165 {
166         GKeyFile *keyfile;
167
168         DBG("profile %p", profile);
169
170         keyfile = __connman_storage_open_profile(profile->ident);
171         if (keyfile == NULL)
172                 return -EIO;
173
174         if (profile->name != NULL)
175                 g_key_file_set_string(keyfile, "global",
176                                                 "Name", profile->name);
177
178         g_key_file_set_boolean(keyfile, "global",
179                                         "OfflineMode", profile->offlinemode);
180
181         __connman_storage_close_profile(profile->ident, keyfile, TRUE);
182
183         return 0;
184 }
185
186 static struct connman_storage profile_storage = {
187         .name           = "profile",
188         .priority       = CONNMAN_STORAGE_PRIORITY_LOW,
189         .profile_init   = profile_init,
190         .profile_load   = profile_load,
191         .profile_save   = profile_save,
192 };
193
194 int __connman_profile_init(void)
195 {
196         DBG("");
197
198         connection = connman_dbus_get_connection();
199         if (connection == NULL)
200                 return -1;
201
202         if (connman_storage_register(&profile_storage) < 0)
203                 connman_error("Failed to register profile storage");
204
205         return 0;
206 }
207
208 void __connman_profile_cleanup(void)
209 {
210         DBG("");
211
212         if (connection == NULL)
213                 return;
214
215         connman_storage_unregister(&profile_storage);
216
217         dbus_connection_unref(connection);
218 }