aaf0ad0c0a1d6df10053df465b709b6300a47a93
[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 guint changed_timeout = 0;
99
100 static gboolean services_changed(gpointer user_data)
101 {
102         changed_timeout = 0;
103
104         if (default_profile == NULL)
105                 return FALSE;
106
107         connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
108                                 CONNMAN_MANAGER_INTERFACE, "Services",
109                                 DBUS_TYPE_OBJECT_PATH, __connman_service_list,
110                                 NULL);
111
112         return FALSE;
113 }
114
115 void __connman_profile_changed(gboolean delayed)
116 {
117         DBG("");
118
119         if (changed_timeout > 0) {
120                 g_source_remove(changed_timeout);
121                 changed_timeout = 0;
122         }
123
124         if (__connman_connection_update_gateway() == TRUE) {
125                 services_changed(NULL);
126                 return;
127         }
128
129         if (delayed == FALSE) {
130                 services_changed(NULL);
131                 return;
132         }
133
134         changed_timeout = g_timeout_add_seconds(1, services_changed, NULL);
135 }
136
137 static void free_profile(struct connman_profile *profile)
138 {
139         g_free(profile->name);
140         g_free(profile->path);
141         g_free(profile->ident);
142         g_free(profile);
143 }
144
145 static int profile_init(void)
146 {
147         DBG("");
148
149         default_profile = g_try_new0(struct connman_profile, 1);
150         if (default_profile == NULL)
151                 return -ENOMEM;
152
153         default_profile->ident = g_strdup(PROFILE_DEFAULT_IDENT);
154         default_profile->path = g_strdup_printf("/profile/%s",
155                                         PROFILE_DEFAULT_IDENT);
156
157         if (default_profile->ident == NULL || default_profile->path == NULL) {
158                 free_profile(default_profile);
159                 return -ENOMEM;
160         }
161
162         default_profile->name = g_strdup("Default");
163
164         __connman_storage_load_profile(default_profile);
165
166         connman_info("Adding default profile");
167
168         DBG("profile %p path %s", default_profile, default_profile->path);
169
170         return 0;
171 }
172
173 static int profile_load(struct connman_profile *profile)
174 {
175         GKeyFile *keyfile;
176         GError *error = NULL;
177         connman_bool_t offlinemode;
178         char *name;
179
180         DBG("profile %p", profile);
181
182         keyfile = __connman_storage_open_profile(profile->ident);
183         if (keyfile == NULL)
184                 return -EIO;
185
186         name = g_key_file_get_string(keyfile, "global", "Name", NULL);
187         if (name != NULL) {
188                 g_free(profile->name);
189                 profile->name = name;
190         }
191
192         offlinemode = g_key_file_get_boolean(keyfile, "global",
193                                                 "OfflineMode", &error);
194         if (error == NULL)
195                 profile->offlinemode = offlinemode;
196         g_clear_error(&error);
197
198         __connman_storage_close_profile(profile->ident, keyfile, FALSE);
199
200         return 0;
201 }
202
203 static int profile_save(struct connman_profile *profile)
204 {
205         GKeyFile *keyfile;
206
207         DBG("profile %p", profile);
208
209         keyfile = __connman_storage_open_profile(profile->ident);
210         if (keyfile == NULL)
211                 return -EIO;
212
213         if (profile->name != NULL)
214                 g_key_file_set_string(keyfile, "global",
215                                                 "Name", profile->name);
216
217         g_key_file_set_boolean(keyfile, "global",
218                                         "OfflineMode", profile->offlinemode);
219
220         __connman_storage_close_profile(profile->ident, keyfile, TRUE);
221
222         return 0;
223 }
224
225 static struct connman_storage profile_storage = {
226         .name           = "profile",
227         .priority       = CONNMAN_STORAGE_PRIORITY_LOW,
228         .profile_init   = profile_init,
229         .profile_load   = profile_load,
230         .profile_save   = profile_save,
231 };
232
233 int __connman_profile_init(void)
234 {
235         DBG("");
236
237         connection = connman_dbus_get_connection();
238         if (connection == NULL)
239                 return -1;
240
241         if (connman_storage_register(&profile_storage) < 0)
242                 connman_error("Failed to register profile storage");
243
244         return 0;
245 }
246
247 void __connman_profile_cleanup(void)
248 {
249         DBG("");
250
251         if (connection == NULL)
252                 return;
253
254         connman_storage_unregister(&profile_storage);
255
256         dbus_connection_unref(connection);
257 }