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