technology: Redo offlinemode logic
[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 static void offlinemode_changed(struct connman_profile *profile)
48 {
49         connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
50                                 CONNMAN_MANAGER_INTERFACE, "OfflineMode",
51                                 DBUS_TYPE_BOOLEAN, &profile->offlinemode);
52 }
53
54 connman_bool_t __connman_profile_get_offlinemode(void)
55 {
56         if (default_profile == NULL)
57                 return FALSE;
58
59         DBG("offlinemode %d", default_profile->offlinemode);
60
61         return default_profile->offlinemode;
62 }
63
64 int __connman_profile_set_offlinemode(connman_bool_t offlinemode)
65 {
66         DBG("offlinemode %d", offlinemode);
67
68         if (default_profile == NULL)
69                 return -EINVAL;
70
71         if (default_profile->offlinemode == offlinemode)
72                 return -EALREADY;
73
74         default_profile->offlinemode = offlinemode;
75         offlinemode_changed(default_profile);
76
77         return 0;
78 }
79
80 int __connman_profile_save_default(void)
81 {
82         DBG("");
83
84         if (default_profile != NULL)
85                 __connman_storage_save_profile(default_profile);
86
87         return 0;
88 }
89
90 const char *__connman_profile_active_ident(void)
91 {
92         DBG("");
93
94         return PROFILE_DEFAULT_IDENT;
95 }
96
97 const char *__connman_profile_active_path(void)
98 {
99         DBG("");
100
101         if (default_profile == NULL)
102                 return NULL;
103
104         return default_profile->path;
105 }
106
107 static guint changed_timeout = 0;
108
109 static gboolean services_changed(gpointer user_data)
110 {
111         changed_timeout = 0;
112
113         if (default_profile == NULL)
114                 return FALSE;
115
116         connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
117                                 CONNMAN_MANAGER_INTERFACE, "Services",
118                                 DBUS_TYPE_OBJECT_PATH, __connman_service_list,
119                                 NULL);
120
121         return FALSE;
122 }
123
124 void __connman_profile_changed(gboolean delayed)
125 {
126         DBG("");
127
128         if (changed_timeout > 0) {
129                 g_source_remove(changed_timeout);
130                 changed_timeout = 0;
131         }
132
133         if (__connman_connection_update_gateway() == TRUE) {
134                 services_changed(NULL);
135                 return;
136         }
137
138         if (delayed == FALSE) {
139                 services_changed(NULL);
140                 return;
141         }
142
143         changed_timeout = g_timeout_add_seconds(1, services_changed, NULL);
144 }
145
146 static void free_profile(struct connman_profile *profile)
147 {
148         g_free(profile->name);
149         g_free(profile->path);
150         g_free(profile->ident);
151         g_free(profile);
152 }
153
154 static int profile_init(void)
155 {
156         DBG("");
157
158         default_profile = g_try_new0(struct connman_profile, 1);
159         if (default_profile == NULL)
160                 return -ENOMEM;
161
162         default_profile->ident = g_strdup(PROFILE_DEFAULT_IDENT);
163         default_profile->path = g_strdup_printf("/profile/%s",
164                                         PROFILE_DEFAULT_IDENT);
165
166         if (default_profile->ident == NULL || default_profile->path == NULL) {
167                 free_profile(default_profile);
168                 return -ENOMEM;
169         }
170
171         default_profile->name = g_strdup("Default");
172
173         __connman_storage_load_profile(default_profile);
174
175         connman_info("Adding default profile");
176
177         DBG("profile %p path %s", default_profile, default_profile->path);
178
179         return 0;
180 }
181
182 static int profile_load(struct connman_profile *profile)
183 {
184         GKeyFile *keyfile;
185         GError *error = NULL;
186         connman_bool_t offlinemode;
187         char *name;
188
189         DBG("profile %p", profile);
190
191         keyfile = __connman_storage_open_profile(profile->ident);
192         if (keyfile == NULL)
193                 return -EIO;
194
195         name = g_key_file_get_string(keyfile, "global", "Name", NULL);
196         if (name != NULL) {
197                 g_free(profile->name);
198                 profile->name = name;
199         }
200
201         offlinemode = g_key_file_get_boolean(keyfile, "global",
202                                                 "OfflineMode", &error);
203         if (error == NULL)
204                 profile->offlinemode = offlinemode;
205         g_clear_error(&error);
206
207         __connman_storage_close_profile(profile->ident, keyfile, FALSE);
208
209         return 0;
210 }
211
212 static int profile_save(struct connman_profile *profile)
213 {
214         GKeyFile *keyfile;
215
216         DBG("profile %p", profile);
217
218         keyfile = __connman_storage_open_profile(profile->ident);
219         if (keyfile == NULL)
220                 return -EIO;
221
222         if (profile->name != NULL)
223                 g_key_file_set_string(keyfile, "global",
224                                                 "Name", profile->name);
225
226         g_key_file_set_boolean(keyfile, "global",
227                                         "OfflineMode", profile->offlinemode);
228
229         __connman_storage_close_profile(profile->ident, keyfile, TRUE);
230
231         return 0;
232 }
233
234 static struct connman_storage profile_storage = {
235         .name           = "profile",
236         .priority       = CONNMAN_STORAGE_PRIORITY_LOW,
237         .profile_init   = profile_init,
238         .profile_load   = profile_load,
239         .profile_save   = profile_save,
240 };
241
242 int __connman_profile_init(void)
243 {
244         DBG("");
245
246         connection = connman_dbus_get_connection();
247         if (connection == NULL)
248                 return -1;
249
250         if (connman_storage_register(&profile_storage) < 0)
251                 connman_error("Failed to register profile storage");
252
253         return 0;
254 }
255
256 void __connman_profile_cleanup(void)
257 {
258         DBG("");
259
260         if (connection == NULL)
261                 return;
262
263         connman_storage_unregister(&profile_storage);
264
265         dbus_connection_unref(connection);
266 }