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