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
34 #define PROFILE_DEFAULT_IDENT "default"
36 struct connman_profile {
40 connman_bool_t offlinemode;
43 static struct connman_profile *default_profile = NULL;
45 static DBusConnection *connection = NULL;
47 static void offlinemode_changed(struct connman_profile *profile)
49 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
50 CONNMAN_MANAGER_INTERFACE, "OfflineMode",
51 DBUS_TYPE_BOOLEAN, &profile->offlinemode);
54 connman_bool_t __connman_profile_get_offlinemode(void)
56 if (default_profile == NULL)
59 DBG("offlinemode %d", default_profile->offlinemode);
61 return default_profile->offlinemode;
64 int __connman_profile_set_offlinemode(connman_bool_t offlinemode,
65 connman_bool_t all_devices)
67 DBG("offlinemode %d", offlinemode);
69 if (default_profile == NULL)
72 if (default_profile->offlinemode == offlinemode)
75 default_profile->offlinemode = offlinemode;
76 offlinemode_changed(default_profile);
79 __connman_device_set_offlinemode(offlinemode);
84 int __connman_profile_save_default(void)
88 if (default_profile != NULL)
89 __connman_storage_save_profile(default_profile);
94 const char *__connman_profile_active_ident(void)
98 return PROFILE_DEFAULT_IDENT;
101 const char *__connman_profile_active_path(void)
105 if (default_profile == NULL)
108 return default_profile->path;
111 static guint changed_timeout = 0;
113 static gboolean services_changed(gpointer user_data)
117 if (default_profile == NULL)
120 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
121 CONNMAN_MANAGER_INTERFACE, "Services",
122 DBUS_TYPE_OBJECT_PATH, __connman_service_list,
128 void __connman_profile_changed(gboolean delayed)
132 if (changed_timeout > 0) {
133 g_source_remove(changed_timeout);
137 if (__connman_connection_update_gateway() == TRUE) {
138 services_changed(NULL);
142 if (delayed == FALSE) {
143 services_changed(NULL);
147 changed_timeout = g_timeout_add_seconds(1, services_changed, NULL);
150 static void free_profile(struct connman_profile *profile)
152 g_free(profile->name);
153 g_free(profile->path);
154 g_free(profile->ident);
158 static int profile_init(void)
162 default_profile = g_try_new0(struct connman_profile, 1);
163 if (default_profile == NULL)
166 default_profile->ident = g_strdup(PROFILE_DEFAULT_IDENT);
167 default_profile->path = g_strdup_printf("/profile/%s",
168 PROFILE_DEFAULT_IDENT);
170 if (default_profile->ident == NULL || default_profile->path == NULL) {
171 free_profile(default_profile);
175 default_profile->name = g_strdup("Default");
177 __connman_storage_load_profile(default_profile);
179 connman_info("Adding default profile");
181 DBG("profile %p path %s", default_profile, default_profile->path);
186 static int profile_load(struct connman_profile *profile)
189 GError *error = NULL;
190 connman_bool_t offlinemode;
193 DBG("profile %p", profile);
195 keyfile = __connman_storage_open_profile(profile->ident);
199 name = g_key_file_get_string(keyfile, "global", "Name", NULL);
201 g_free(profile->name);
202 profile->name = name;
205 offlinemode = g_key_file_get_boolean(keyfile, "global",
206 "OfflineMode", &error);
208 profile->offlinemode = offlinemode;
209 g_clear_error(&error);
211 __connman_storage_close_profile(profile->ident, keyfile, FALSE);
216 static int profile_save(struct connman_profile *profile)
220 DBG("profile %p", profile);
222 keyfile = __connman_storage_open_profile(profile->ident);
226 if (profile->name != NULL)
227 g_key_file_set_string(keyfile, "global",
228 "Name", profile->name);
230 g_key_file_set_boolean(keyfile, "global",
231 "OfflineMode", profile->offlinemode);
233 __connman_storage_close_profile(profile->ident, keyfile, TRUE);
238 static struct connman_storage profile_storage = {
240 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
241 .profile_init = profile_init,
242 .profile_load = profile_load,
243 .profile_save = profile_save,
246 int __connman_profile_init(void)
250 connection = connman_dbus_get_connection();
251 if (connection == NULL)
254 if (connman_storage_register(&profile_storage) < 0)
255 connman_error("Failed to register profile storage");
260 void __connman_profile_cleanup(void)
264 if (connection == NULL)
267 connman_storage_unregister(&profile_storage);
269 dbus_connection_unref(connection);