2012-07-18 15:08:07 +01:00
|
|
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011-2012 Richard Hughes <richard@hughsie.com>
|
2012-08-29 14:39:52 -04:00
|
|
|
* Copyright (C) 2012 Red Hat, Inc.
|
2012-07-18 15:08:07 +01: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>
|
2012-07-18 15:08:07 +01:00
|
|
|
|
|
|
|
#include "panel-common.h"
|
|
|
|
|
2023-11-17 14:50:17 +01:00
|
|
|
#include "connection-editor/net-connection-editor.h"
|
|
|
|
|
2019-10-23 11:15:00 +13:00
|
|
|
#include "net-device-bluetooth.h"
|
2012-07-18 15:08:07 +01:00
|
|
|
|
2019-10-23 11:15:00 +13:00
|
|
|
struct _NetDeviceBluetooth
|
2012-07-18 15:08:07 +01:00
|
|
|
{
|
2022-01-27 04:35:07 -08:00
|
|
|
AdwActionRow parent;
|
2019-10-23 11:15:00 +13:00
|
|
|
|
2019-10-15 12:25:48 +13:00
|
|
|
GtkSwitch *device_off_switch;
|
|
|
|
GtkButton *options_button;
|
|
|
|
|
2019-10-23 14:45:52 +13:00
|
|
|
NMClient *client;
|
2019-10-24 13:04:14 +13:00
|
|
|
NMDevice *device;
|
2019-10-23 14:45:52 +13:00
|
|
|
gboolean updating_device;
|
2019-10-23 11:02:55 +13:00
|
|
|
};
|
2012-07-18 15:08:07 +01:00
|
|
|
|
2022-01-27 04:35:07 -08:00
|
|
|
G_DEFINE_TYPE (NetDeviceBluetooth, net_device_bluetooth, ADW_TYPE_ACTION_ROW)
|
2017-08-22 23:07:51 -03:00
|
|
|
|
2012-07-18 15:08:07 +01:00
|
|
|
static void
|
|
|
|
update_off_switch_from_device_state (GtkSwitch *sw,
|
|
|
|
NMDeviceState state,
|
2019-10-23 11:15:00 +13:00
|
|
|
NetDeviceBluetooth *self)
|
2012-07-18 15:08:07 +01:00
|
|
|
{
|
2019-10-23 11:02:55 +13:00
|
|
|
self->updating_device = TRUE;
|
2012-07-18 15:08:07 +01:00
|
|
|
switch (state) {
|
|
|
|
case NM_DEVICE_STATE_UNMANAGED:
|
|
|
|
case NM_DEVICE_STATE_UNAVAILABLE:
|
|
|
|
case NM_DEVICE_STATE_DISCONNECTED:
|
|
|
|
case NM_DEVICE_STATE_DEACTIVATING:
|
|
|
|
case NM_DEVICE_STATE_FAILED:
|
|
|
|
gtk_switch_set_active (sw, FALSE);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
gtk_switch_set_active (sw, TRUE);
|
|
|
|
break;
|
|
|
|
}
|
2019-10-23 11:02:55 +13:00
|
|
|
self->updating_device = FALSE;
|
2012-07-18 15:08:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-10-23 11:15:00 +13:00
|
|
|
nm_device_bluetooth_refresh_ui (NetDeviceBluetooth *self)
|
2012-07-18 15:08:07 +01:00
|
|
|
{
|
|
|
|
NMDeviceState state;
|
2019-12-16 15:09:24 +13:00
|
|
|
g_autofree gchar *path = NULL;
|
2012-07-18 15:08:07 +01:00
|
|
|
|
|
|
|
/* set up the device on/off switch */
|
2019-10-24 13:04:14 +13:00
|
|
|
state = nm_device_get_state (self->device);
|
2019-10-23 11:02:55 +13:00
|
|
|
gtk_widget_set_visible (GTK_WIDGET (self->device_off_switch),
|
2012-07-18 15:08:07 +01:00
|
|
|
state != NM_DEVICE_STATE_UNAVAILABLE
|
|
|
|
&& state != NM_DEVICE_STATE_UNMANAGED);
|
2019-10-23 11:02:55 +13:00
|
|
|
update_off_switch_from_device_state (self->device_off_switch, state, self);
|
2012-07-18 15:08:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-10-24 20:29:35 +13:00
|
|
|
device_off_switch_changed_cb (NetDeviceBluetooth *self)
|
2012-07-18 15:08:07 +01:00
|
|
|
{
|
|
|
|
const GPtrArray *acs;
|
|
|
|
gboolean active;
|
|
|
|
gint i;
|
|
|
|
NMActiveConnection *a;
|
|
|
|
NMConnection *connection;
|
|
|
|
|
2019-10-23 11:02:55 +13:00
|
|
|
if (self->updating_device)
|
2012-07-18 15:08:07 +01:00
|
|
|
return;
|
|
|
|
|
2019-10-24 13:04:14 +13:00
|
|
|
connection = net_device_get_find_connection (self->client, self->device);
|
2019-10-23 14:14:48 +13:00
|
|
|
if (connection == NULL)
|
|
|
|
return;
|
|
|
|
|
2019-10-23 11:02:55 +13:00
|
|
|
active = gtk_switch_get_active (self->device_off_switch);
|
2012-07-18 15:08:07 +01:00
|
|
|
if (active) {
|
2019-10-23 14:45:52 +13:00
|
|
|
nm_client_activate_connection_async (self->client,
|
2016-04-29 16:05:54 +02:00
|
|
|
connection,
|
2019-10-24 13:04:14 +13:00
|
|
|
self->device,
|
2016-04-29 16:05:54 +02:00
|
|
|
NULL, NULL, NULL, NULL);
|
2012-07-18 15:08:07 +01:00
|
|
|
} else {
|
2016-04-29 16:05:54 +02:00
|
|
|
const gchar *uuid;
|
|
|
|
|
|
|
|
uuid = nm_connection_get_uuid (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-18 15:08:07 +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-18 15:08:07 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-17 14:50:17 +01:00
|
|
|
static void
|
|
|
|
editor_done (NetDeviceBluetooth *self)
|
|
|
|
{
|
|
|
|
nm_device_bluetooth_refresh_ui (self);
|
|
|
|
}
|
|
|
|
|
2012-07-18 15:08:07 +01:00
|
|
|
static void
|
2019-10-24 20:29:35 +13:00
|
|
|
options_button_clicked_cb (NetDeviceBluetooth *self)
|
2012-07-18 15:08:07 +01:00
|
|
|
{
|
2019-10-18 15:42:05 +13:00
|
|
|
NMConnection *connection;
|
2023-11-17 14:50:17 +01:00
|
|
|
NetConnectionEditor *editor;
|
2019-10-18 15:42:05 +13:00
|
|
|
|
2019-10-24 13:04:14 +13:00
|
|
|
connection = net_device_get_find_connection (self->client, self->device);
|
2023-11-17 14:50:17 +01:00
|
|
|
|
|
|
|
editor = net_connection_editor_new (connection, self->device, NULL, self->client);
|
|
|
|
gtk_window_set_transient_for (GTK_WINDOW (editor), GTK_WINDOW (gtk_widget_get_native (GTK_WIDGET (self))));
|
|
|
|
net_connection_editor_set_title (editor, _("Bluetooth"));
|
|
|
|
g_signal_connect_object (editor, "done", G_CALLBACK (editor_done), self, G_CONNECT_SWAPPED);
|
|
|
|
gtk_window_present (GTK_WINDOW (editor));
|
2012-07-18 15:08:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-10-23 11:15:00 +13:00
|
|
|
net_device_bluetooth_finalize (GObject *object)
|
2012-07-18 15:08:07 +01:00
|
|
|
{
|
2019-10-23 11:15:00 +13:00
|
|
|
NetDeviceBluetooth *self = NET_DEVICE_BLUETOOTH (object);
|
2012-07-18 15:08:07 +01:00
|
|
|
|
2019-10-23 14:45:52 +13:00
|
|
|
g_clear_object (&self->client);
|
2019-10-24 13:04:14 +13:00
|
|
|
g_clear_object (&self->device);
|
2012-07-18 15:08:07 +01:00
|
|
|
|
2019-10-23 11:15:00 +13:00
|
|
|
G_OBJECT_CLASS (net_device_bluetooth_parent_class)->finalize (object);
|
2012-08-29 14:39:52 -04:00
|
|
|
}
|
|
|
|
|
2012-07-18 15:08:07 +01:00
|
|
|
static void
|
2019-10-23 11:15:00 +13:00
|
|
|
net_device_bluetooth_class_init (NetDeviceBluetoothClass *klass)
|
2012-07-18 15:08:07 +01:00
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
2019-10-24 14:30:28 +13:00
|
|
|
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
|
2012-08-29 14:39:52 -04:00
|
|
|
|
2019-10-23 11:15:00 +13:00
|
|
|
object_class->finalize = net_device_bluetooth_finalize;
|
2019-10-24 14:30:28 +13:00
|
|
|
|
|
|
|
gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/control-center/network/network-bluetooth.ui");
|
|
|
|
|
|
|
|
gtk_widget_class_bind_template_child (widget_class, NetDeviceBluetooth, device_off_switch);
|
|
|
|
gtk_widget_class_bind_template_child (widget_class, NetDeviceBluetooth, options_button);
|
2019-10-24 20:29:35 +13:00
|
|
|
|
|
|
|
gtk_widget_class_bind_template_callback (widget_class, device_off_switch_changed_cb);
|
|
|
|
gtk_widget_class_bind_template_callback (widget_class, options_button_clicked_cb);
|
|
|
|
|
2012-07-18 15:08:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-10-23 11:15:00 +13:00
|
|
|
net_device_bluetooth_init (NetDeviceBluetooth *self)
|
2012-07-18 15:08:07 +01:00
|
|
|
{
|
2019-10-24 14:30:28 +13:00
|
|
|
gtk_widget_init_template (GTK_WIDGET (self));
|
2012-08-29 14:39:52 -04:00
|
|
|
}
|
|
|
|
|
2019-10-23 11:15:00 +13:00
|
|
|
NetDeviceBluetooth *
|
2019-10-23 13:51:47 +13:00
|
|
|
net_device_bluetooth_new (NMClient *client, NMDevice *device)
|
2019-10-23 10:49:03 +13:00
|
|
|
{
|
2019-10-23 14:45:52 +13:00
|
|
|
NetDeviceBluetooth *self;
|
|
|
|
|
2019-10-24 14:30:28 +13:00
|
|
|
self = g_object_new (net_device_bluetooth_get_type (), NULL);
|
2019-10-23 14:45:52 +13:00
|
|
|
self->client = g_object_ref (client);
|
2019-10-24 13:04:14 +13:00
|
|
|
self->device = g_object_ref (device);
|
2019-10-23 14:45:52 +13:00
|
|
|
|
2019-10-24 13:48:25 +13:00
|
|
|
g_signal_connect_object (device, "state-changed", G_CALLBACK (nm_device_bluetooth_refresh_ui), self, G_CONNECT_SWAPPED);
|
2019-10-24 12:13:40 +13:00
|
|
|
|
2019-10-24 11:54:45 +13:00
|
|
|
nm_device_bluetooth_refresh_ui (self);
|
2019-10-23 14:45:52 +13:00
|
|
|
|
|
|
|
return self;
|
2019-10-23 10:49:03 +13:00
|
|
|
}
|
2019-10-24 13:04:14 +13:00
|
|
|
|
|
|
|
NMDevice *
|
|
|
|
net_device_bluetooth_get_device (NetDeviceBluetooth *self)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (NET_IS_DEVICE_BLUETOOTH (self), NULL);
|
|
|
|
return self->device;
|
|
|
|
}
|