Line | Branch | Exec | Source |
---|---|---|---|
1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- | ||
2 | * | ||
3 | * Copyright (C) 2011-2012 Richard Hughes <richard@hughsie.com> | ||
4 | * | ||
5 | * Licensed under the GNU General Public License Version 2 | ||
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, write to the Free Software | ||
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
20 | */ | ||
21 | |||
22 | #include "config.h" | ||
23 | |||
24 | #include <glib-object.h> | ||
25 | #include <glib/gi18n.h> | ||
26 | #include <NetworkManager.h> | ||
27 | |||
28 | #include "panel-common.h" | ||
29 | |||
30 | #include "net-vpn.h" | ||
31 | |||
32 | #include "connection-editor/net-connection-editor.h" | ||
33 | |||
34 | struct _NetVpn | ||
35 | { | ||
36 | AdwActionRow parent; | ||
37 | |||
38 | GtkBox *box; | ||
39 | GtkSwitch *device_off_switch; | ||
40 | |||
41 | NMClient *client; | ||
42 | NMConnection *connection; | ||
43 | NMActiveConnection *active_connection; | ||
44 | gboolean updating_device; | ||
45 | }; | ||
46 | |||
47 |
6/7✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 6 times.
|
18 | G_DEFINE_TYPE (NetVpn, net_vpn, ADW_TYPE_ACTION_ROW) |
48 | |||
49 | static void | ||
50 | 4 | nm_device_refresh_vpn_ui (NetVpn *self) | |
51 | { | ||
52 | 8 | g_autofree char *title_escaped = NULL; | |
53 | const GPtrArray *acs; | ||
54 | NMActiveConnection *a; | ||
55 | gint i; | ||
56 | NMVpnConnectionState state; | ||
57 | |||
58 | /* update title */ | ||
59 | 4 | title_escaped = g_markup_escape_text (nm_connection_get_id (self->connection), | |
60 | -1); | ||
61 | 4 | adw_preferences_row_set_title (ADW_PREFERENCES_ROW (self), | |
62 | title_escaped); | ||
63 | |||
64 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (self->active_connection) { |
65 | ✗ | g_signal_handlers_disconnect_by_func (self->active_connection, | |
66 | nm_device_refresh_vpn_ui, | ||
67 | self); | ||
68 | ✗ | g_clear_object (&self->active_connection); | |
69 | } | ||
70 | |||
71 | |||
72 | /* Default to disconnected if there is no active connection */ | ||
73 | 4 | state = NM_VPN_CONNECTION_STATE_DISCONNECTED; | |
74 | 4 | acs = nm_client_get_active_connections (self->client); | |
75 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | if (acs != NULL) { |
76 | const gchar *uuid; | ||
77 | 4 | uuid = nm_connection_get_uuid (self->connection); | |
78 | |||
79 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | for (i = 0; i < acs->len; i++) { |
80 | const gchar *auuid; | ||
81 | |||
82 | ✗ | a = (NMActiveConnection*)acs->pdata[i]; | |
83 | |||
84 | ✗ | auuid = nm_active_connection_get_uuid (a); | |
85 | ✗ | if (strcmp (auuid, uuid) == 0) { | |
86 | ✗ | if (NM_IS_VPN_CONNECTION (a)) | |
87 | ✗ | state = nm_vpn_connection_get_vpn_state (NM_VPN_CONNECTION (a)); | |
88 | ✗ | else if (nm_is_wireguard_connection (a)) | |
89 | ✗ | state = nm_active_connection_get_state (a); | |
90 | else { | ||
91 | /* Unknown/Unhandled type */ | ||
92 | ✗ | break; | |
93 | } | ||
94 | ✗ | self->active_connection = g_object_ref (a); | |
95 | ✗ | g_signal_connect_object (a, "notify::vpn-state", | |
96 | G_CALLBACK (nm_device_refresh_vpn_ui), | ||
97 | self, G_CONNECT_SWAPPED); | ||
98 | ✗ | break; | |
99 | } | ||
100 | } | ||
101 | } | ||
102 | |||
103 | 4 | self->updating_device = TRUE; | |
104 |
2/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
|
4 | gtk_switch_set_active (self->device_off_switch, |
105 | state != NM_VPN_CONNECTION_STATE_FAILED && | ||
106 | state != NM_VPN_CONNECTION_STATE_DISCONNECTED); | ||
107 | 4 | self->updating_device = FALSE; | |
108 | 4 | } | |
109 | |||
110 | static void | ||
111 | ✗ | nm_active_connections_changed (NetVpn *self) | |
112 | { | ||
113 | ✗ | nm_device_refresh_vpn_ui (self); | |
114 | ✗ | } | |
115 | |||
116 | static void | ||
117 | ✗ | device_off_toggled (NetVpn *self) | |
118 | { | ||
119 | const GPtrArray *acs; | ||
120 | gboolean active; | ||
121 | gint i; | ||
122 | NMActiveConnection *a; | ||
123 | |||
124 | ✗ | if (self->updating_device) | |
125 | ✗ | return; | |
126 | |||
127 | ✗ | active = gtk_switch_get_active (self->device_off_switch); | |
128 | ✗ | if (active) { | |
129 | ✗ | nm_client_activate_connection_async (self->client, | |
130 | self->connection, NULL, NULL, | ||
131 | NULL, NULL, NULL); | ||
132 | } else { | ||
133 | const gchar *uuid; | ||
134 | |||
135 | ✗ | uuid = nm_connection_get_uuid (self->connection); | |
136 | ✗ | acs = nm_client_get_active_connections (self->client); | |
137 | ✗ | for (i = 0; acs && i < acs->len; i++) { | |
138 | ✗ | a = (NMActiveConnection*)acs->pdata[i]; | |
139 | ✗ | if (strcmp (nm_active_connection_get_uuid (a), uuid) == 0) { | |
140 | ✗ | nm_client_deactivate_connection_async (self->client, a, NULL, NULL, NULL); | |
141 | ✗ | break; | |
142 | } | ||
143 | } | ||
144 | } | ||
145 | } | ||
146 | |||
147 | static void | ||
148 | ✗ | editor_done (NetVpn *self) | |
149 | { | ||
150 | ✗ | nm_device_refresh_vpn_ui (self); | |
151 | ✗ | } | |
152 | |||
153 | static void | ||
154 | ✗ | edit_connection (NetVpn *self) | |
155 | { | ||
156 | NetConnectionEditor *editor; | ||
157 | |||
158 | ✗ | editor = net_connection_editor_new (self->connection, NULL, NULL, self->client); | |
159 | ✗ | gtk_window_set_transient_for (GTK_WINDOW (editor), GTK_WINDOW (gtk_widget_get_native (GTK_WIDGET (self)))); | |
160 | ✗ | net_connection_editor_set_title (editor, nm_connection_get_id (self->connection)); | |
161 | |||
162 | ✗ | g_signal_connect_object (editor, "done", G_CALLBACK (editor_done), self, G_CONNECT_SWAPPED); | |
163 | ✗ | gtk_window_present (GTK_WINDOW (editor)); | |
164 | ✗ | } | |
165 | |||
166 | static void | ||
167 | 3 | net_vpn_finalize (GObject *object) | |
168 | { | ||
169 | 3 | NetVpn *self = NET_VPN (object); | |
170 | |||
171 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (self->active_connection) |
172 | ✗ | g_signal_handlers_disconnect_by_func (self->active_connection, | |
173 | nm_device_refresh_vpn_ui, | ||
174 | self); | ||
175 | |||
176 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | g_clear_object (&self->active_connection); |
177 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | g_clear_object (&self->client); |
178 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | g_clear_object (&self->connection); |
179 | |||
180 | 3 | G_OBJECT_CLASS (net_vpn_parent_class)->finalize (object); | |
181 | 3 | } | |
182 | |||
183 | static void | ||
184 | 1 | net_vpn_class_init (NetVpnClass *klass) | |
185 | { | ||
186 | 1 | GObjectClass *object_class = G_OBJECT_CLASS (klass); | |
187 | 1 | GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); | |
188 | |||
189 | 1 | object_class->finalize = net_vpn_finalize; | |
190 | |||
191 | 1 | gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/control-center/network/network-vpn.ui"); | |
192 | |||
193 | 1 | gtk_widget_class_bind_template_child (widget_class, NetVpn, device_off_switch); | |
194 | |||
195 | 1 | gtk_widget_class_bind_template_callback (widget_class, device_off_toggled); | |
196 | 1 | gtk_widget_class_bind_template_callback (widget_class, edit_connection); | |
197 | 1 | } | |
198 | |||
199 | static void | ||
200 | 3 | net_vpn_init (NetVpn *self) | |
201 | { | ||
202 | 3 | gtk_widget_init_template (GTK_WIDGET (self)); | |
203 | 3 | } | |
204 | |||
205 | NetVpn * | ||
206 | 3 | net_vpn_new (NMClient *client, | |
207 | NMConnection *connection) | ||
208 | { | ||
209 | NetVpn *self; | ||
210 | |||
211 | 3 | self = g_object_new (net_vpn_get_type (), NULL); | |
212 | 3 | self->client = g_object_ref (client); | |
213 | 3 | self->connection = g_object_ref (connection); | |
214 | |||
215 | 3 | g_signal_connect_object (connection, | |
216 | NM_CONNECTION_CHANGED, | ||
217 | G_CALLBACK (nm_device_refresh_vpn_ui), | ||
218 | self, G_CONNECT_SWAPPED); | ||
219 | |||
220 | 3 | nm_device_refresh_vpn_ui (self); | |
221 | |||
222 | 3 | g_signal_connect_object (client, | |
223 | "notify::active-connections", | ||
224 | G_CALLBACK (nm_active_connections_changed), | ||
225 | self, G_CONNECT_SWAPPED); | ||
226 | |||
227 | 3 | return self; | |
228 | } | ||
229 | |||
230 | NMConnection * | ||
231 | 1 | net_vpn_get_connection (NetVpn *self) | |
232 | { | ||
233 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | g_return_val_if_fail (NET_IS_VPN (self), NULL); |
234 | 1 | return self->connection; | |
235 | } | ||
236 | |||
237 | gboolean | ||
238 | 2 | nm_is_wireguard_connection (NMActiveConnection *c) { | |
239 | const GPtrArray *devices; | ||
240 | 2 | devices = nm_active_connection_get_devices (c); | |
241 |
3/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
|
4 | for (int j = 0; devices && j < devices->len; j++) { |
242 |
4/8✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
|
2 | if (NM_IS_DEVICE_WIREGUARD (g_ptr_array_index (devices, j))) |
243 | ✗ | return TRUE; | |
244 | } | ||
245 | 2 | return FALSE; | |
246 | } | ||
247 |