[SVACE] 67131,67132 Fixed svace issues
[platform/core/api/vpn-setting.git] / dvpnlib / src / dvpnlib-vpn-manager.c
1 #include "dvpnlib-internal.h"
2 #include "dvpnlib-vpn-manager.h"
3
4 struct vpn_manager {
5         GDBusProxy *dbus_proxy;
6         void *connection_added_cb_data;
7         void *connection_removed_cb_data;
8         vpn_connection_added_cb connection_added_cb;
9         vpn_connection_removed_cb connection_removed_cb;
10 };
11
12 static void connection_added(GVariant *parameters)
13 {
14         struct vpn_connection *connection;
15
16         DBG("");
17
18         if (add_vpn_connection(&parameters, &connection)) {
19                 if (vpn_manager->connection_added_cb) {
20                         vpn_manager->connection_added_cb(connection,
21                                 vpn_manager->connection_added_cb_data);
22                 }
23         }
24 }
25
26 static void connection_removed(GVariant *parameters)
27 {
28         DBG("");
29
30         const gchar *connection_path;
31         struct vpn_connection *connection;
32
33         g_variant_get(parameters, "(o)", &connection_path);
34
35         connection = get_connection_by_path(connection_path);
36         if (connection == NULL)
37                 return;
38
39         if (vpn_manager->connection_removed_cb) {
40                 void *user_data = vpn_manager->connection_removed_cb_data;
41                 vpn_manager->connection_removed_cb(connection, user_data);
42         }
43
44         remove_vpn_connection(connection);
45 }
46
47 static void manager_signal_handler(GDBusProxy *proxy, gchar *sender_name,
48                                 gchar *signal_name, GVariant *parameters,
49                                 gpointer user_data)
50 {
51         DBG("signal_name: %s", signal_name);
52
53         if (!g_strcmp0(signal_name, "ConnectionAdded"))
54                 connection_added(parameters);
55         else if (!g_strcmp0(signal_name, "ConnectionRemoved"))
56                 connection_removed(parameters);
57 }
58
59 void free_vpn_manager(struct vpn_manager *manager)
60 {
61         DBG("");
62
63         if (manager == NULL)
64                 return;
65
66         if (manager->dbus_proxy != NULL)
67                 g_object_unref(manager->dbus_proxy);
68
69         g_free(manager);
70 }
71
72 struct vpn_manager *create_vpn_manager(void)
73 {
74         GError *error = NULL;
75         struct vpn_manager *manager;
76
77         DBG("");
78         manager = g_try_new0(struct vpn_manager, 1);
79         if (manager == NULL) {
80                 ERROR("no memory");
81                 return NULL;
82         }
83
84         manager->dbus_proxy = g_dbus_proxy_new_for_bus_sync(
85                                         G_BUS_TYPE_SYSTEM,
86                                         G_DBUS_PROXY_FLAGS_NONE, NULL,
87                                         VPN_NAME, VPN_MANAGER_PATH,
88                                         VPN_MANAGER_INTERFACE, NULL, &error);
89
90         if (manager->dbus_proxy == NULL) {
91                 ERROR("error info: %s", error->message);
92                 g_error_free(error);
93                 free_vpn_manager(manager);
94                 return NULL;
95         }
96
97         g_signal_connect(manager->dbus_proxy, "g-signal",
98                         G_CALLBACK(manager_signal_handler), NULL);
99
100         return manager;
101 }
102
103 GDBusProxy *get_vpn_manager_dbus_proxy(void)
104 {
105         return vpn_manager->dbus_proxy;
106 }
107
108 /**
109  * Asynchronous Methods Create/Remove callback
110  */
111 static void create_remove_callback(GObject *source_object,
112                              GAsyncResult *res, gpointer user_data)
113 {
114         GError *error = NULL;
115         enum dvpnlib_err error_type = DVPNLIB_ERR_NONE;
116         GVariant *ret;
117         struct common_reply_data *reply_data;
118
119         reply_data = user_data;
120         if (!reply_data)
121                 return;
122
123         if (!vpn_manager)
124                 goto done;
125
126         if (!vpn_manager->dbus_proxy)
127                 goto done;
128
129         ret = g_dbus_proxy_call_finish(vpn_manager->dbus_proxy, res, &error);
130         if (!ret) {
131                 DBG("%s", error->message);
132                 error_type = get_error_type(error);
133
134                 g_error_free(error);
135         } else
136                 g_variant_unref(ret);
137
138         if (reply_data->cb) {
139                 dvpnlib_reply_cb callback = reply_data->cb;
140                 callback(error_type, reply_data->data);
141         }
142
143 done:
144         g_free(reply_data);
145 }
146
147 /* Implementation vpn-manager.h */
148
149 static void print_variant(const gchar *s, GVariant *v)
150 {
151         gchar *temp = g_variant_print(v, true);
152         DBG("%s => %s", s, temp);
153         g_free(temp);
154 }
155
156 static GVariant *
157 settings_from_hash_table(GHashTable *hash_table)
158 {
159         GVariantBuilder builder;
160
161         g_variant_builder_init(&builder, G_VARIANT_TYPE("(a{sv})"));
162         g_variant_builder_open(&builder, G_VARIANT_TYPE("a{sv}"));
163
164         GHashTableIter iter;
165         gpointer key, value;
166
167         g_hash_table_iter_init(&iter, hash_table);
168         while (g_hash_table_iter_next(&iter, &key, &value)) {
169                 g_variant_builder_add(&builder, "{sv}",
170                                 key,
171                                 g_variant_new_string(value));
172         }
173
174         g_variant_builder_close(&builder);
175
176         return g_variant_builder_end(&builder);
177 }
178
179 enum dvpnlib_err dvpnlib_vpn_manager_create(GHashTable *settings,
180                                  dvpnlib_reply_cb callback,
181                                  void *user_data)
182 {
183         GVariant *settings_v;
184
185         assert(vpn_manager != NULL);
186         assert(settings != NULL);
187
188         settings_v = settings_from_hash_table(settings);
189         print_variant("Settings", settings_v);
190
191         struct common_reply_data *reply_data;
192
193         reply_data =
194                 common_reply_data_new(callback, user_data, NULL, TRUE);
195
196         if (NULL == reply_data) {
197                 DBG("No Memory Available!");
198                 return DVPNLIB_ERR_FAILED;
199         }
200
201         return common_set_interface_call_method(
202                         vpn_manager->dbus_proxy,
203                         "Create",
204                         &settings_v,
205                         (GAsyncReadyCallback)create_remove_callback,
206                         reply_data);
207 }
208
209 enum dvpnlib_err dvpnlib_vpn_manager_remove(const char *path,
210                                  dvpnlib_reply_cb callback,
211                                  void *user_data)
212 {
213         GVariant *value;
214         enum dvpnlib_err ret = DVPNLIB_ERR_NONE;
215
216         assert(vpn_manager != NULL);
217
218         if (!g_variant_is_object_path(path))
219                 return DVPNLIB_ERR_FAILED;
220
221         value = g_variant_new("(o)", path);
222
223         struct common_reply_data *reply_data;
224
225         reply_data =
226             common_reply_data_new(callback, user_data, NULL, TRUE);
227
228         if (NULL == reply_data) {
229                 DBG("No Memory Available!");
230                 return DVPNLIB_ERR_FAILED;
231
232         }
233
234         ret = common_set_interface_call_method(
235                         vpn_manager->dbus_proxy,
236                         "Remove",
237                         &value,
238                         (GAsyncReadyCallback)create_remove_callback,
239                         reply_data);
240
241         return ret;
242 }
243
244 enum dvpnlib_err dvpnlib_vpn_manager_register_agent(const char *path)
245 {
246         GVariant *value;
247
248         assert(vpn_manager != NULL);
249
250         if (!g_variant_is_object_path(path))
251                 return DVPNLIB_ERR_FAILED;
252
253         value = g_variant_new("(o)", path);
254
255         return common_set_interface_call_method_sync(
256                         vpn_manager->dbus_proxy,
257                         "RegisterAgent",
258                         &value);
259 }
260
261 enum dvpnlib_err dvpnlib_vpn_manager_unregister_agent(const char *path)
262 {
263         GVariant *value;
264
265         assert(vpn_manager != NULL);
266
267         if (!g_variant_is_object_path(path))
268                 return DVPNLIB_ERR_FAILED;
269
270         value = g_variant_new("(o)", path);
271
272         return common_set_interface_call_method_sync(
273                         vpn_manager->dbus_proxy,
274                         "UnregisterAgent",
275                         &value);
276 }
277
278 void dvpnlib_vpn_manager_set_connection_added_cb(
279                                 vpn_connection_added_cb cb,
280                                 void *user_data)
281 {
282         DBG("");
283
284         assert(vpn_manager != NULL);
285
286         vpn_manager->connection_added_cb = cb;
287         vpn_manager->connection_added_cb_data = user_data;
288 }
289
290 void dvpnlib_vpn_manager_unset_connection_added_cb()
291 {
292         DBG("");
293
294         assert(vpn_manager != NULL);
295
296         vpn_manager->connection_added_cb = NULL;
297         vpn_manager->connection_added_cb_data = NULL;
298 }
299
300 void dvpnlib_vpn_manager_set_connection_removed_cb(
301                                 vpn_connection_removed_cb cb,
302                                 void *user_data)
303 {
304         DBG("");
305
306         assert(vpn_manager != NULL);
307
308         vpn_manager->connection_removed_cb = cb;
309         vpn_manager->connection_removed_cb_data = user_data;
310 }
311
312 void dvpnlib_vpn_manager_unset_connection_removed_cb()
313 {
314         DBG("");
315
316         assert(vpn_manager != NULL);
317
318         vpn_manager->connection_removed_cb = NULL;
319         vpn_manager->connection_removed_cb_data = NULL;
320 }
321