1 #include "dvpnlib-internal.h"
2 #include "dvpnlib-vpn-manager.h"
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;
12 static void connection_added(GVariant *parameters)
14 struct vpn_connection *connection;
18 if (add_vpn_connection(¶meters, &connection)) {
19 if (vpn_manager->connection_added_cb) {
20 vpn_manager->connection_added_cb(connection,
21 vpn_manager->connection_added_cb_data);
26 static void connection_removed(GVariant *parameters)
30 const gchar *connection_path;
31 struct vpn_connection *connection;
33 g_variant_get(parameters, "(o)", &connection_path);
35 connection = get_connection_by_path(connection_path);
36 if (connection == NULL)
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);
44 remove_vpn_connection(connection);
47 static void manager_signal_handler(GDBusProxy *proxy, gchar *sender_name,
48 gchar *signal_name, GVariant *parameters,
51 DBG("signal_name: %s", signal_name);
53 if (!g_strcmp0(signal_name, "ConnectionAdded"))
54 connection_added(parameters);
55 else if (!g_strcmp0(signal_name, "ConnectionRemoved"))
56 connection_removed(parameters);
59 void free_vpn_manager(struct vpn_manager *manager)
66 if (manager->dbus_proxy != NULL)
67 g_object_unref(manager->dbus_proxy);
72 struct vpn_manager *create_vpn_manager(void)
75 struct vpn_manager *manager;
78 manager = g_try_new0(struct vpn_manager, 1);
79 if (manager == NULL) {
84 manager->dbus_proxy = g_dbus_proxy_new_for_bus_sync(
86 G_DBUS_PROXY_FLAGS_NONE, NULL,
87 VPN_NAME, VPN_MANAGER_PATH,
88 VPN_MANAGER_INTERFACE, NULL, &error);
90 if (manager->dbus_proxy == NULL) {
91 ERROR("error info: %s", error->message);
93 free_vpn_manager(manager);
97 g_signal_connect(manager->dbus_proxy, "g-signal",
98 G_CALLBACK(manager_signal_handler), NULL);
103 GDBusProxy *get_vpn_manager_dbus_proxy(void)
105 return vpn_manager->dbus_proxy;
109 * Asynchronous Methods Create/Remove callback
111 static void create_remove_callback(GObject *source_object,
112 GAsyncResult *res, gpointer user_data)
114 GError *error = NULL;
115 enum dvpnlib_err error_type = DVPNLIB_ERR_NONE;
117 struct common_reply_data *reply_data;
119 reply_data = user_data;
126 if (!vpn_manager->dbus_proxy)
129 ret = g_dbus_proxy_call_finish(vpn_manager->dbus_proxy, res, &error);
131 DBG("%s", error->message);
132 error_type = get_error_type(error);
136 g_variant_unref(ret);
138 if (reply_data->cb) {
139 dvpnlib_reply_cb callback = reply_data->cb;
140 callback(error_type, reply_data->data);
147 /* Implementation vpn-manager.h */
149 static void print_variant(const gchar *s, GVariant *v)
151 gchar *temp = g_variant_print(v, true);
152 DBG("%s => %s", s, temp);
157 settings_from_hash_table(GHashTable *hash_table)
159 GVariantBuilder builder;
161 g_variant_builder_init(&builder, G_VARIANT_TYPE("(a{sv})"));
162 g_variant_builder_open(&builder, G_VARIANT_TYPE("a{sv}"));
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}",
171 g_variant_new_string(value));
174 g_variant_builder_close(&builder);
176 return g_variant_builder_end(&builder);
179 enum dvpnlib_err dvpnlib_vpn_manager_create(GHashTable *settings,
180 dvpnlib_reply_cb callback,
183 GVariant *settings_v;
185 assert(vpn_manager != NULL);
186 assert(settings != NULL);
188 settings_v = settings_from_hash_table(settings);
189 print_variant("Settings", settings_v);
191 struct common_reply_data *reply_data;
194 common_reply_data_new(callback, user_data, NULL, TRUE);
196 if (NULL == reply_data) {
197 DBG("No Memory Available!");
198 return DVPNLIB_ERR_FAILED;
201 return common_set_interface_call_method(
202 vpn_manager->dbus_proxy,
205 (GAsyncReadyCallback)create_remove_callback,
209 enum dvpnlib_err dvpnlib_vpn_manager_remove(const char *path,
210 dvpnlib_reply_cb callback,
214 enum dvpnlib_err ret = DVPNLIB_ERR_NONE;
216 assert(vpn_manager != NULL);
218 if (!g_variant_is_object_path(path))
219 return DVPNLIB_ERR_FAILED;
221 value = g_variant_new("(o)", path);
223 struct common_reply_data *reply_data;
226 common_reply_data_new(callback, user_data, NULL, TRUE);
228 if (NULL == reply_data) {
229 DBG("No Memory Available!");
230 return DVPNLIB_ERR_FAILED;
234 ret = common_set_interface_call_method(
235 vpn_manager->dbus_proxy,
238 (GAsyncReadyCallback)create_remove_callback,
244 enum dvpnlib_err dvpnlib_vpn_manager_register_agent(const char *path)
248 assert(vpn_manager != NULL);
250 if (!g_variant_is_object_path(path))
251 return DVPNLIB_ERR_FAILED;
253 value = g_variant_new("(o)", path);
255 return common_set_interface_call_method_sync(
256 vpn_manager->dbus_proxy,
261 enum dvpnlib_err dvpnlib_vpn_manager_unregister_agent(const char *path)
265 assert(vpn_manager != NULL);
267 if (!g_variant_is_object_path(path))
268 return DVPNLIB_ERR_FAILED;
270 value = g_variant_new("(o)", path);
272 return common_set_interface_call_method_sync(
273 vpn_manager->dbus_proxy,
278 void dvpnlib_vpn_manager_set_connection_added_cb(
279 vpn_connection_added_cb cb,
284 assert(vpn_manager != NULL);
286 vpn_manager->connection_added_cb = cb;
287 vpn_manager->connection_added_cb_data = user_data;
290 void dvpnlib_vpn_manager_unset_connection_added_cb()
294 assert(vpn_manager != NULL);
296 vpn_manager->connection_added_cb = NULL;
297 vpn_manager->connection_added_cb_data = NULL;
300 void dvpnlib_vpn_manager_set_connection_removed_cb(
301 vpn_connection_removed_cb cb,
306 assert(vpn_manager != NULL);
308 vpn_manager->connection_removed_cb = cb;
309 vpn_manager->connection_removed_cb_data = user_data;
312 void dvpnlib_vpn_manager_unset_connection_removed_cb()
316 assert(vpn_manager != NULL);
318 vpn_manager->connection_removed_cb = NULL;
319 vpn_manager->connection_removed_cb_data = NULL;