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)
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);
80 int __connman_profile_save_default(void)
84 if (default_profile != NULL)
85 __connman_storage_save_profile(default_profile);
90 const char *__connman_profile_active_ident(void)
94 return PROFILE_DEFAULT_IDENT;
97 const char *__connman_profile_active_path(void)
101 if (default_profile == NULL)
104 return default_profile->path;
107 static guint changed_timeout = 0;
109 static gboolean services_changed(gpointer user_data)
113 if (default_profile == NULL)
116 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
117 CONNMAN_MANAGER_INTERFACE, "Services",
118 DBUS_TYPE_OBJECT_PATH, __connman_service_list,
124 void __connman_profile_changed(gboolean delayed)
128 if (changed_timeout > 0) {
129 g_source_remove(changed_timeout);
133 if (__connman_connection_update_gateway() == TRUE) {
134 services_changed(NULL);
138 if (delayed == FALSE) {
139 services_changed(NULL);
143 changed_timeout = g_timeout_add_seconds(1, services_changed, NULL);
146 static void free_profile(struct connman_profile *profile)
148 g_free(profile->name);
149 g_free(profile->path);
150 g_free(profile->ident);
154 static int profile_init(void)
158 default_profile = g_try_new0(struct connman_profile, 1);
159 if (default_profile == NULL)
162 default_profile->ident = g_strdup(PROFILE_DEFAULT_IDENT);
163 default_profile->path = g_strdup_printf("/profile/%s",
164 PROFILE_DEFAULT_IDENT);
166 if (default_profile->ident == NULL || default_profile->path == NULL) {
167 free_profile(default_profile);
171 default_profile->name = g_strdup("Default");
173 __connman_storage_load_profile(default_profile);
175 connman_info("Adding default profile");
177 DBG("profile %p path %s", default_profile, default_profile->path);
182 static int profile_load(struct connman_profile *profile)
185 GError *error = NULL;
186 connman_bool_t offlinemode;
189 DBG("profile %p", profile);
191 keyfile = __connman_storage_open_profile(profile->ident);
195 name = g_key_file_get_string(keyfile, "global", "Name", NULL);
197 g_free(profile->name);
198 profile->name = name;
201 offlinemode = g_key_file_get_boolean(keyfile, "global",
202 "OfflineMode", &error);
204 profile->offlinemode = offlinemode;
205 g_clear_error(&error);
207 __connman_storage_close_profile(profile->ident, keyfile, FALSE);
212 static int profile_save(struct connman_profile *profile)
216 DBG("profile %p", profile);
218 keyfile = __connman_storage_open_profile(profile->ident);
222 if (profile->name != NULL)
223 g_key_file_set_string(keyfile, "global",
224 "Name", profile->name);
226 g_key_file_set_boolean(keyfile, "global",
227 "OfflineMode", profile->offlinemode);
229 __connman_storage_close_profile(profile->ident, keyfile, TRUE);
234 static struct connman_storage profile_storage = {
236 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
237 .profile_init = profile_init,
238 .profile_load = profile_load,
239 .profile_save = profile_save,
242 int __connman_profile_init(void)
246 connection = connman_dbus_get_connection();
247 if (connection == NULL)
250 if (connman_storage_register(&profile_storage) < 0)
251 connman_error("Failed to register profile storage");
256 void __connman_profile_cleanup(void)
260 if (connection == NULL)
263 connman_storage_unregister(&profile_storage);
265 dbus_connection_unref(connection);