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
33 #define PROFILE_DEFAULT_IDENT "default"
35 struct connman_profile {
39 connman_bool_t offlinemode;
42 static struct connman_profile *default_profile = NULL;
44 static DBusConnection *connection = NULL;
46 static void offlinemode_changed(struct connman_profile *profile)
48 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
49 CONNMAN_MANAGER_INTERFACE, "OfflineMode",
50 DBUS_TYPE_BOOLEAN, &profile->offlinemode);
53 connman_bool_t __connman_profile_get_offlinemode(void)
55 if (default_profile == NULL)
58 DBG("offlinemode %d", default_profile->offlinemode);
60 return default_profile->offlinemode;
63 int __connman_profile_set_offlinemode(connman_bool_t offlinemode,
64 connman_bool_t all_devices)
66 DBG("offlinemode %d", offlinemode);
68 if (default_profile == NULL)
71 if (default_profile->offlinemode == offlinemode)
74 default_profile->offlinemode = offlinemode;
75 offlinemode_changed(default_profile);
78 __connman_device_set_offlinemode(offlinemode);
83 int __connman_profile_save_default(void)
87 if (default_profile != NULL)
88 __connman_storage_save_profile(default_profile);
93 const char *__connman_profile_active_ident(void)
97 return PROFILE_DEFAULT_IDENT;
100 const char *__connman_profile_active_path(void)
104 if (default_profile == NULL)
107 return default_profile->path;
110 static guint changed_timeout = 0;
112 static gboolean services_changed(gpointer user_data)
116 if (default_profile == NULL)
119 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
120 CONNMAN_MANAGER_INTERFACE, "Services",
121 DBUS_TYPE_OBJECT_PATH, __connman_service_list,
127 void __connman_profile_changed(gboolean delayed)
131 if (changed_timeout > 0) {
132 g_source_remove(changed_timeout);
136 if (__connman_connection_update_gateway() == TRUE) {
137 services_changed(NULL);
141 if (delayed == FALSE) {
142 services_changed(NULL);
146 changed_timeout = g_timeout_add_seconds(1, services_changed, NULL);
149 static void free_profile(struct connman_profile *profile)
151 g_free(profile->name);
152 g_free(profile->path);
153 g_free(profile->ident);
157 static int profile_init(void)
161 default_profile = g_try_new0(struct connman_profile, 1);
162 if (default_profile == NULL)
165 default_profile->ident = g_strdup(PROFILE_DEFAULT_IDENT);
166 default_profile->path = g_strdup_printf("/profile/%s",
167 PROFILE_DEFAULT_IDENT);
169 if (default_profile->ident == NULL || default_profile->path == NULL) {
170 free_profile(default_profile);
174 default_profile->name = g_strdup("Default");
176 __connman_storage_load_profile(default_profile);
178 connman_info("Adding default profile");
180 DBG("profile %p path %s", default_profile, default_profile->path);
185 static int profile_load(struct connman_profile *profile)
188 GError *error = NULL;
189 connman_bool_t offlinemode;
192 DBG("profile %p", profile);
194 keyfile = __connman_storage_open_profile(profile->ident);
198 name = g_key_file_get_string(keyfile, "global", "Name", NULL);
200 g_free(profile->name);
201 profile->name = name;
204 offlinemode = g_key_file_get_boolean(keyfile, "global",
205 "OfflineMode", &error);
207 profile->offlinemode = offlinemode;
208 g_clear_error(&error);
210 __connman_storage_close_profile(profile->ident, keyfile, FALSE);
215 static int profile_save(struct connman_profile *profile)
219 DBG("profile %p", profile);
221 keyfile = __connman_storage_open_profile(profile->ident);
225 if (profile->name != NULL)
226 g_key_file_set_string(keyfile, "global",
227 "Name", profile->name);
229 g_key_file_set_boolean(keyfile, "global",
230 "OfflineMode", profile->offlinemode);
232 __connman_storage_close_profile(profile->ident, keyfile, TRUE);
237 static struct connman_storage profile_storage = {
239 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
240 .profile_init = profile_init,
241 .profile_load = profile_load,
242 .profile_save = profile_save,
245 int __connman_profile_init(void)
249 connection = connman_dbus_get_connection();
250 if (connection == NULL)
253 if (connman_storage_register(&profile_storage) < 0)
254 connman_error("Failed to register profile storage");
259 void __connman_profile_cleanup(void)
263 if (connection == NULL)
266 connman_storage_unregister(&profile_storage);
268 dbus_connection_unref(connection);