Debian Codegraph @

src/vpn.c

raw
1 /*
2 * ConnMan GTK GUI
3 *
4 * Copyright (C) 2015 Intel Corporation. All rights reserved.
5 * Author: Jaakko Hannikainen <[email protected]>
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 as published by
9 * the Free Software Foundation, either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <glib.h>
22 #include <glib/gi18n.h>
23
24 #include "config.h"
25 #include "dialog.h"
26 #include "interfaces.h"
27 #include "main.h"
28 #include "technology.h"
29 #include "service.h"
30 #include "vpn.h"
31
32 static struct technology *tech;
33 static GDBusConnection *connection;
34 static GDBusProxy *proxy;
35 int connection_count;
36
37 static void add_connection(GDBusConnection *connection, GVariant *parameters)
38 {
39 GVariant *path_v, *properties;
40 const gchar *path;
41
42 path_v = g_variant_get_child_value(parameters, 0);
43 properties = g_variant_get_child_value(parameters, 1);
44 path = g_variant_get_string(path_v, NULL);
45
46 gtk_widget_show(tech->list_item->item);
47 gtk_widget_show(tech->settings->grid);
48 connection_count++;
49 modify_service(g_dbus_proxy_get_connection(proxy), path,
50 properties);
51
52 g_variant_unref(path_v);
53 g_variant_unref(properties);
54 }
55
56 static void remove_connection(GVariant *parameters)
57 {
58 GVariant *path_v;
59 const gchar *path;
60
61 path_v = g_variant_get_child_value(parameters, 0);
62 path = g_variant_get_string(path_v, NULL);
63 connection_count--;
64
65 if(!connection_count) {
66 gtk_widget_hide(tech->list_item->item);
67 gtk_widget_hide(tech->settings->grid);
68 }
69
70 remove_service(path);
71
72 g_variant_unref(path_v);
73 }
74
75 static void vpn_signal(GDBusProxy *proxy, gchar *sender, gchar *signal,
76 GVariant *parameters, gpointer user_data)
77 {
78 if(!strcmp(signal, "ConnectionAdded"))
79 add_connection(g_dbus_proxy_get_connection(proxy), parameters);
80 else if(!strcmp(signal, "ConnectionRemoved"))
81 remove_connection(parameters);
82 }
83
84 static void add_all_connections(GDBusConnection *Connection,
85 GVariant *connections_v)
86 {
87 int i;
88 int size = g_variant_n_children(connections_v);
89 for(i = 0; i < size; i++) {
90 GVariant *child = g_variant_get_child_value(connections_v, i);
91 add_connection(connection, child);
92 g_variant_unref(child);
93 }
94 }
95
96 static struct technology *create_vpn_technology(void)
97 {
98 GVariant *properties;
99 GVariantBuilder *b;
100
101 b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
102 g_variant_builder_add(b, "{sv}", "Name",
103 g_variant_new_string(_("VPN")));
104 g_variant_builder_add(b, "{sv}", "Type",
105 g_variant_new_string("vpn"));
106 g_variant_builder_add(b, "{sv}", "Connected",
107 g_variant_new_boolean(FALSE));
108 g_variant_builder_add(b, "{sv}", "Powered",
109 g_variant_new_boolean(TRUE));
110 properties = g_variant_builder_end(b);
111 tech = technology_create(proxy, "/net/connman/technologies/vpn",
112 properties);
113 gtk_widget_hide(tech->settings->power_switch);
114 gtk_widget_hide(tech->settings->tethering);
115 gtk_widget_hide(tech->list_item->item);
116 gtk_widget_hide(tech->settings->grid);
117 g_variant_unref(properties);
118 g_variant_builder_unref(b);
119
120 return tech;
121 }
122
123 struct technology *vpn_register(GDBusConnection *conn, GtkWidget *list,
124 GtkWidget *notebook)
125 {
126 GDBusNodeInfo *info;
127 GDBusInterfaceInfo *interface;
128 GError *error = NULL;
129
130 connection = conn;
131 info = g_dbus_node_info_new_for_xml(VPN_MANAGER_INTERFACE, &error);
132 if(error) {
133 g_critical("Failed to load VPN manager interface: %s",
134 error->message);
135 g_error_free(error);
136 return NULL;
137 }
138
139 interface = g_dbus_node_info_lookup_interface(info, VPN_MANAGER_NAME);
140 proxy = g_dbus_proxy_new_sync(connection, G_DBUS_PROXY_FLAGS_NONE,
141 interface, CONNMAN_PATH ".vpn", "/",
142 VPN_MANAGER_NAME, NULL, &error);
143 g_dbus_node_info_unref(info);
144 if(error) {
145 g_warning("Failed to connect to ConnMan: %s", error->message);
146 show_error(_("Failed to connect to ConnMan."), error->message);
147 g_error_free(error);
148 return NULL;
149 }
150
151 g_signal_connect(proxy, "g-signal", G_CALLBACK(vpn_signal), NULL);
152 tech = create_vpn_technology();
153 return tech;
154 }
155
156 void vpn_update_status(struct technology *tech)
157 {
158 GHashTableIter iter;
159 gpointer key, value;
160 struct technology_settings *item;
161 int status = 0;
162
163 item = tech->settings;
164 g_hash_table_iter_init(&iter, tech->services);
165 while(g_hash_table_iter_next(&iter, &key, &value)) {
166 gchar *state;
167
168 state = service_get_property_string_raw(value, "State", NULL);
169 if(status < 2 && !strcmp(state, "ready"))
170 status = 2;
171 else if(status < 1 && !strcmp(state, "configuration"))
172 status = 1;
173
174 g_free(state);
175 }
176
177 gtk_label_set_text(GTK_LABEL(item->title), _("VPN"));
178 if(status == 2)
179 gtk_label_set_text(GTK_LABEL(item->status), _("Connected"));
180 else if(status == 1)
181 gtk_label_set_text(GTK_LABEL(item->status), _("Connecting"));
182 else
183 gtk_label_set_text(GTK_LABEL(item->status), _("Not connected"));
184 }
185
186 void vpn_get_connections(GDBusProxy *manager_proxy)
187 {
188 GVariant *data, *child;
189 GError *error = NULL;
190 proxy = manager_proxy;
191 data = g_dbus_proxy_call_sync(proxy, "GetConnections", NULL,
192 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
193 if(error) {
194 g_warning("failed to get vpn connections: %s", error->message);
195 g_error_free(error);
196 g_object_unref(proxy);
197 return;
198 }
199
200 child = g_variant_get_child_value(data, 0);
201 add_all_connections(connection, child);
202 g_variant_unref(data);
203 g_variant_unref(child);
204 }
205
206 void vpn_release(void)
207 {
208 connection_count = 0;
209 proxy = NULL;
210 tech = NULL;
211 }