2011-03-11 12:28:14 +00:00
|
|
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
|
|
|
|
*
|
2012-07-12 16:58:11 +01:00
|
|
|
* Copyright (C) 2011-2012 Richard Hughes <richard@hughsie.com>
|
2011-03-11 12:28:14 +00:00
|
|
|
*
|
|
|
|
* Licensed under the GNU General Public License Version 2
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <glib-object.h>
|
|
|
|
#include <glib/gi18n.h>
|
2016-04-29 16:05:54 +02:00
|
|
|
#include <NetworkManager.h>
|
2011-03-11 12:28:14 +00:00
|
|
|
|
2012-07-12 16:58:11 +01:00
|
|
|
#include "panel-common.h"
|
|
|
|
|
2011-03-11 12:28:14 +00:00
|
|
|
#include "net-vpn.h"
|
|
|
|
|
2013-01-11 11:07:46 -05:00
|
|
|
#include "connection-editor/net-connection-editor.h"
|
|
|
|
|
2018-10-31 20:50:24 -03:00
|
|
|
struct _NetVpn
|
2011-03-11 12:28:14 +00:00
|
|
|
{
|
2022-01-27 04:08:58 -08:00
|
|
|
AdwActionRow parent;
|
2018-10-31 20:50:24 -03:00
|
|
|
|
2019-10-15 12:25:48 +13:00
|
|
|
GtkBox *box;
|
|
|
|
GtkSwitch *device_off_switch;
|
|
|
|
|
2019-10-23 14:45:52 +13:00
|
|
|
NMClient *client;
|
2011-03-11 12:56:14 +00:00
|
|
|
NMConnection *connection;
|
2013-05-15 12:10:39 +01:00
|
|
|
NMActiveConnection *active_connection;
|
2012-07-12 16:58:11 +01:00
|
|
|
gboolean updating_device;
|
2011-03-11 12:28:14 +00:00
|
|
|
};
|
|
|
|
|
2022-01-27 04:08:58 -08:00
|
|
|
G_DEFINE_TYPE (NetVpn, net_vpn, ADW_TYPE_ACTION_ROW)
|
2012-07-12 16:58:11 +01:00
|
|
|
|
|
|
|
static void
|
2019-10-18 11:47:17 +13:00
|
|
|
nm_device_refresh_vpn_ui (NetVpn *self)
|
2012-07-12 16:58:11 +01:00
|
|
|
{
|
2022-07-14 14:29:31 +01:00
|
|
|
g_autofree char *title_escaped = NULL;
|
2012-07-12 16:58:11 +01:00
|
|
|
const GPtrArray *acs;
|
|
|
|
NMActiveConnection *a;
|
|
|
|
gint i;
|
2016-04-29 16:05:54 +02:00
|
|
|
NMVpnConnectionState state;
|
2012-07-12 16:58:11 +01:00
|
|
|
|
|
|
|
/* update title */
|
2022-07-14 14:29:31 +01:00
|
|
|
title_escaped = g_markup_escape_text (nm_connection_get_id (self->connection),
|
|
|
|
-1);
|
2022-02-10 13:10:25 +01:00
|
|
|
adw_preferences_row_set_title (ADW_PREFERENCES_ROW (self),
|
2022-07-14 14:29:31 +01:00
|
|
|
title_escaped);
|
2012-07-12 16:58:11 +01:00
|
|
|
|
2019-10-18 11:47:17 +13:00
|
|
|
if (self->active_connection) {
|
|
|
|
g_signal_handlers_disconnect_by_func (self->active_connection,
|
2013-05-15 12:10:39 +01:00
|
|
|
nm_device_refresh_vpn_ui,
|
2019-10-18 11:47:17 +13:00
|
|
|
self);
|
|
|
|
g_clear_object (&self->active_connection);
|
2013-05-15 12:10:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-08 12:11:06 +01:00
|
|
|
/* Default to disconnected if there is no active connection */
|
|
|
|
state = NM_VPN_CONNECTION_STATE_DISCONNECTED;
|
2019-10-23 14:45:52 +13:00
|
|
|
acs = nm_client_get_active_connections (self->client);
|
2012-07-12 16:58:11 +01:00
|
|
|
if (acs != NULL) {
|
2016-04-29 16:05:54 +02:00
|
|
|
const gchar *uuid;
|
2019-10-18 11:47:17 +13:00
|
|
|
uuid = nm_connection_get_uuid (self->connection);
|
2018-03-08 12:11:06 +01:00
|
|
|
|
2012-07-12 16:58:11 +01:00
|
|
|
for (i = 0; i < acs->len; i++) {
|
2016-04-29 16:05:54 +02:00
|
|
|
const gchar *auuid;
|
|
|
|
|
2012-07-12 16:58:11 +01:00
|
|
|
a = (NMActiveConnection*)acs->pdata[i];
|
|
|
|
|
2016-04-29 16:05:54 +02:00
|
|
|
auuid = nm_active_connection_get_uuid (a);
|
2022-09-24 00:24:35 +02:00
|
|
|
if (strcmp (auuid, uuid) == 0) {
|
|
|
|
if (NM_IS_VPN_CONNECTION (a))
|
|
|
|
state = nm_vpn_connection_get_vpn_state (NM_VPN_CONNECTION (a));
|
|
|
|
else if (nm_is_wireguard_connection (a))
|
|
|
|
state = nm_active_connection_get_state (a);
|
|
|
|
else {
|
|
|
|
/* Unknown/Unhandled type */
|
|
|
|
break;
|
|
|
|
}
|
2019-10-18 11:47:17 +13:00
|
|
|
self->active_connection = g_object_ref (a);
|
2019-11-22 16:21:43 +13:00
|
|
|
g_signal_connect_object (a, "notify::vpn-state",
|
|
|
|
G_CALLBACK (nm_device_refresh_vpn_ui),
|
|
|
|
self, G_CONNECT_SWAPPED);
|
2012-07-12 16:58:11 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-18 11:47:17 +13:00
|
|
|
self->updating_device = TRUE;
|
|
|
|
gtk_switch_set_active (self->device_off_switch,
|
2012-07-12 16:58:11 +01:00
|
|
|
state != NM_VPN_CONNECTION_STATE_FAILED &&
|
|
|
|
state != NM_VPN_CONNECTION_STATE_DISCONNECTED);
|
2019-10-18 11:47:17 +13:00
|
|
|
self->updating_device = FALSE;
|
2012-07-12 16:58:11 +01:00
|
|
|
}
|
|
|
|
|
2013-05-15 12:10:39 +01:00
|
|
|
static void
|
2019-10-18 11:47:17 +13:00
|
|
|
nm_active_connections_changed (NetVpn *self)
|
2013-05-15 12:10:39 +01:00
|
|
|
{
|
2019-10-18 11:47:17 +13:00
|
|
|
nm_device_refresh_vpn_ui (self);
|
2013-05-15 12:10:39 +01:00
|
|
|
}
|
|
|
|
|
2012-07-12 16:58:11 +01:00
|
|
|
static void
|
2019-10-18 11:47:17 +13:00
|
|
|
device_off_toggled (NetVpn *self)
|
2012-07-12 16:58:11 +01:00
|
|
|
{
|
|
|
|
const GPtrArray *acs;
|
|
|
|
gboolean active;
|
|
|
|
gint i;
|
|
|
|
NMActiveConnection *a;
|
|
|
|
|
2019-10-18 11:47:17 +13:00
|
|
|
if (self->updating_device)
|
2012-07-12 16:58:11 +01:00
|
|
|
return;
|
|
|
|
|
2019-10-18 11:47:17 +13:00
|
|
|
active = gtk_switch_get_active (self->device_off_switch);
|
2012-07-12 16:58:11 +01:00
|
|
|
if (active) {
|
2019-10-23 14:45:52 +13:00
|
|
|
nm_client_activate_connection_async (self->client,
|
2019-10-18 11:47:17 +13:00
|
|
|
self->connection, NULL, NULL,
|
2016-04-29 16:05:54 +02:00
|
|
|
NULL, NULL, NULL);
|
2012-07-12 16:58:11 +01:00
|
|
|
} else {
|
2016-04-29 16:05:54 +02:00
|
|
|
const gchar *uuid;
|
|
|
|
|
2019-10-18 11:47:17 +13:00
|
|
|
uuid = nm_connection_get_uuid (self->connection);
|
2019-10-23 14:45:52 +13:00
|
|
|
acs = nm_client_get_active_connections (self->client);
|
2013-09-11 16:12:49 +02:00
|
|
|
for (i = 0; acs && i < acs->len; i++) {
|
2012-07-12 16:58:11 +01:00
|
|
|
a = (NMActiveConnection*)acs->pdata[i];
|
2016-04-29 16:05:54 +02:00
|
|
|
if (strcmp (nm_active_connection_get_uuid (a), uuid) == 0) {
|
2022-05-31 15:38:39 +12:00
|
|
|
nm_client_deactivate_connection_async (self->client, a, NULL, NULL, NULL);
|
2012-07-12 16:58:11 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-11 11:07:46 -05:00
|
|
|
static void
|
2019-10-18 11:47:17 +13:00
|
|
|
editor_done (NetVpn *self)
|
2013-01-11 11:07:46 -05:00
|
|
|
{
|
2019-10-24 11:06:17 +13:00
|
|
|
nm_device_refresh_vpn_ui (self);
|
2013-01-11 11:07:46 -05:00
|
|
|
}
|
|
|
|
|
2012-07-20 09:08:53 +01:00
|
|
|
static void
|
2019-10-18 15:42:05 +13:00
|
|
|
edit_connection (NetVpn *self)
|
2012-07-12 16:58:11 +01:00
|
|
|
{
|
2013-01-11 11:07:46 -05:00
|
|
|
NetConnectionEditor *editor;
|
2012-07-12 16:58:11 +01:00
|
|
|
|
2020-11-19 11:17:22 +13:00
|
|
|
editor = net_connection_editor_new (self->connection, NULL, NULL, self->client);
|
2021-11-29 10:37:01 -03:00
|
|
|
gtk_window_set_transient_for (GTK_WINDOW (editor), GTK_WINDOW (gtk_widget_get_native (GTK_WIDGET (self))));
|
2022-02-10 13:56:41 +01:00
|
|
|
net_connection_editor_set_title (editor, nm_connection_get_id (self->connection));
|
2013-04-12 21:59:09 -04:00
|
|
|
|
2019-11-13 09:56:19 +13:00
|
|
|
g_signal_connect_object (editor, "done", G_CALLBACK (editor_done), self, G_CONNECT_SWAPPED);
|
2020-11-19 11:43:16 +13:00
|
|
|
gtk_window_present (GTK_WINDOW (editor));
|
2012-07-12 16:58:11 +01:00
|
|
|
}
|
|
|
|
|
2011-03-11 12:28:14 +00:00
|
|
|
static void
|
|
|
|
net_vpn_finalize (GObject *object)
|
|
|
|
{
|
2019-10-18 11:47:17 +13:00
|
|
|
NetVpn *self = NET_VPN (object);
|
2013-05-15 12:10:39 +01:00
|
|
|
|
2019-10-23 09:38:55 +13:00
|
|
|
if (self->active_connection)
|
2019-10-18 11:47:17 +13:00
|
|
|
g_signal_handlers_disconnect_by_func (self->active_connection,
|
2013-05-15 12:10:39 +01:00
|
|
|
nm_device_refresh_vpn_ui,
|
2019-10-18 11:47:17 +13:00
|
|
|
self);
|
2011-03-11 12:28:14 +00:00
|
|
|
|
2019-10-23 09:38:55 +13:00
|
|
|
g_clear_object (&self->active_connection);
|
2019-10-23 14:45:52 +13:00
|
|
|
g_clear_object (&self->client);
|
2019-10-23 09:38:55 +13:00
|
|
|
g_clear_object (&self->connection);
|
2013-09-17 16:31:38 +02:00
|
|
|
|
2011-03-11 12:28:14 +00:00
|
|
|
G_OBJECT_CLASS (net_vpn_parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
net_vpn_class_init (NetVpnClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
2019-10-24 14:23:55 +13:00
|
|
|
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
|
2012-07-12 16:58:11 +01:00
|
|
|
|
2011-03-11 12:28:14 +00:00
|
|
|
object_class->finalize = net_vpn_finalize;
|
2019-10-24 14:23:55 +13:00
|
|
|
|
|
|
|
gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/control-center/network/network-vpn.ui");
|
|
|
|
|
|
|
|
gtk_widget_class_bind_template_child (widget_class, NetVpn, device_off_switch);
|
2020-11-05 12:20:15 +13:00
|
|
|
|
|
|
|
gtk_widget_class_bind_template_callback (widget_class, device_off_toggled);
|
|
|
|
gtk_widget_class_bind_template_callback (widget_class, edit_connection);
|
2011-03-11 12:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-10-18 11:47:17 +13:00
|
|
|
net_vpn_init (NetVpn *self)
|
2011-03-11 12:28:14 +00:00
|
|
|
{
|
2019-10-24 14:23:55 +13:00
|
|
|
gtk_widget_init_template (GTK_WIDGET (self));
|
2011-03-11 12:28:14 +00:00
|
|
|
}
|
2019-10-23 12:45:07 +13:00
|
|
|
|
|
|
|
NetVpn *
|
2019-10-24 14:25:41 +13:00
|
|
|
net_vpn_new (NMClient *client,
|
|
|
|
NMConnection *connection)
|
2019-10-23 12:45:07 +13:00
|
|
|
{
|
2019-10-23 14:45:52 +13:00
|
|
|
NetVpn *self;
|
|
|
|
|
2019-10-24 14:23:55 +13:00
|
|
|
self = g_object_new (net_vpn_get_type (), NULL);
|
2019-10-23 14:45:52 +13:00
|
|
|
self->client = g_object_ref (client);
|
|
|
|
self->connection = g_object_ref (connection);
|
|
|
|
|
|
|
|
g_signal_connect_object (connection,
|
|
|
|
NM_CONNECTION_CHANGED,
|
2019-10-24 13:48:25 +13:00
|
|
|
G_CALLBACK (nm_device_refresh_vpn_ui),
|
2019-10-23 14:45:52 +13:00
|
|
|
self, G_CONNECT_SWAPPED);
|
|
|
|
|
|
|
|
nm_device_refresh_vpn_ui (self);
|
|
|
|
|
|
|
|
g_signal_connect_object (client,
|
|
|
|
"notify::active-connections",
|
|
|
|
G_CALLBACK (nm_active_connections_changed),
|
|
|
|
self, G_CONNECT_SWAPPED);
|
|
|
|
|
|
|
|
return self;
|
2019-10-23 12:45:07 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
NMConnection *
|
|
|
|
net_vpn_get_connection (NetVpn *self)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (NET_IS_VPN (self), NULL);
|
|
|
|
return self->connection;
|
|
|
|
}
|
2022-09-24 00:24:35 +02:00
|
|
|
|
|
|
|
gboolean
|
|
|
|
nm_is_wireguard_connection (NMActiveConnection *c) {
|
|
|
|
const GPtrArray *devices;
|
|
|
|
devices = nm_active_connection_get_devices (c);
|
|
|
|
for (int j = 0; devices && j < devices->len; j++) {
|
|
|
|
if (NM_IS_DEVICE_WIREGUARD (g_ptr_array_index (devices, j)))
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|