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 connman_bool_t __connman_profile_get_offlinemode(void)
49 if (default_profile == NULL)
52 DBG("offlinemode %d", default_profile->offlinemode);
54 return default_profile->offlinemode;
57 int __connman_profile_set_offlinemode(connman_bool_t offlinemode)
59 DBG("offlinemode %d", offlinemode);
61 if (default_profile == NULL)
64 if (default_profile->offlinemode == offlinemode)
67 default_profile->offlinemode = offlinemode;
71 int __connman_profile_save_default(void)
75 if (default_profile != NULL)
76 __connman_storage_save_profile(default_profile);
81 const char *__connman_profile_active_ident(void)
85 return PROFILE_DEFAULT_IDENT;
88 const char *__connman_profile_active_path(void)
92 if (default_profile == NULL)
95 return default_profile->path;
98 static guint changed_timeout = 0;
100 static gboolean services_changed(gpointer user_data)
104 if (default_profile == NULL)
107 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
108 CONNMAN_MANAGER_INTERFACE, "Services",
109 DBUS_TYPE_OBJECT_PATH, __connman_service_list,
115 void __connman_profile_changed(gboolean delayed)
119 if (changed_timeout > 0) {
120 g_source_remove(changed_timeout);
124 if (__connman_connection_update_gateway() == TRUE) {
125 services_changed(NULL);
129 if (delayed == FALSE) {
130 services_changed(NULL);
134 changed_timeout = g_timeout_add_seconds(1, services_changed, NULL);
137 static void free_profile(struct connman_profile *profile)
139 g_free(profile->name);
140 g_free(profile->path);
141 g_free(profile->ident);
145 static int profile_init(void)
149 default_profile = g_try_new0(struct connman_profile, 1);
150 if (default_profile == NULL)
153 default_profile->ident = g_strdup(PROFILE_DEFAULT_IDENT);
154 default_profile->path = g_strdup_printf("/profile/%s",
155 PROFILE_DEFAULT_IDENT);
157 if (default_profile->ident == NULL || default_profile->path == NULL) {
158 free_profile(default_profile);
162 default_profile->name = g_strdup("Default");
164 __connman_storage_load_profile(default_profile);
166 connman_info("Adding default profile");
168 DBG("profile %p path %s", default_profile, default_profile->path);
173 static int profile_load(struct connman_profile *profile)
176 GError *error = NULL;
177 connman_bool_t offlinemode;
180 DBG("profile %p", profile);
182 keyfile = __connman_storage_open_profile(profile->ident);
186 name = g_key_file_get_string(keyfile, "global", "Name", NULL);
188 g_free(profile->name);
189 profile->name = name;
192 offlinemode = g_key_file_get_boolean(keyfile, "global",
193 "OfflineMode", &error);
195 profile->offlinemode = offlinemode;
196 g_clear_error(&error);
198 __connman_storage_close_profile(profile->ident, keyfile, FALSE);
203 static int profile_save(struct connman_profile *profile)
207 DBG("profile %p", profile);
209 keyfile = __connman_storage_open_profile(profile->ident);
213 if (profile->name != NULL)
214 g_key_file_set_string(keyfile, "global",
215 "Name", profile->name);
217 g_key_file_set_boolean(keyfile, "global",
218 "OfflineMode", profile->offlinemode);
220 __connman_storage_close_profile(profile->ident, keyfile, TRUE);
225 static struct connman_storage profile_storage = {
227 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
228 .profile_init = profile_init,
229 .profile_load = profile_load,
230 .profile_save = profile_save,
233 int __connman_profile_init(void)
237 connection = connman_dbus_get_connection();
238 if (connection == NULL)
241 if (connman_storage_register(&profile_storage) < 0)
242 connman_error("Failed to register profile storage");
247 void __connman_profile_cleanup(void)
251 if (connection == NULL)
254 connman_storage_unregister(&profile_storage);
256 dbus_connection_unref(connection);