GCC Code Coverage Report


Directory: ./
File: panels/network/net-device-bluetooth.c
Date: 2024-05-04 07:58:27
Exec Total Coverage
Lines: 0 74 0.0%
Functions: 0 13 0.0%
Branches: 0 31 0.0%

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 * Copyright (C) 2012 Red Hat, Inc.
5 *
6 * Licensed under the GNU General Public License Version 2
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 #include "config.h"
24
25 #include <glib-object.h>
26 #include <glib/gi18n.h>
27
28 #include <NetworkManager.h>
29
30 #include "panel-common.h"
31
32 #include "connection-editor/net-connection-editor.h"
33
34 #include "net-device-bluetooth.h"
35
36 struct _NetDeviceBluetooth
37 {
38 AdwActionRow parent;
39
40 GtkSwitch *device_off_switch;
41 GtkButton *options_button;
42
43 NMClient *client;
44 NMDevice *device;
45 gboolean updating_device;
46 };
47
48 G_DEFINE_TYPE (NetDeviceBluetooth, net_device_bluetooth, ADW_TYPE_ACTION_ROW)
49
50 static void
51 update_off_switch_from_device_state (GtkSwitch *sw,
52 NMDeviceState state,
53 NetDeviceBluetooth *self)
54 {
55 self->updating_device = TRUE;
56 switch (state) {
57 case NM_DEVICE_STATE_UNMANAGED:
58 case NM_DEVICE_STATE_UNAVAILABLE:
59 case NM_DEVICE_STATE_DISCONNECTED:
60 case NM_DEVICE_STATE_DEACTIVATING:
61 case NM_DEVICE_STATE_FAILED:
62 gtk_switch_set_active (sw, FALSE);
63 break;
64 default:
65 gtk_switch_set_active (sw, TRUE);
66 break;
67 }
68 self->updating_device = FALSE;
69 }
70
71 static void
72 nm_device_bluetooth_refresh_ui (NetDeviceBluetooth *self)
73 {
74 NMDeviceState state;
75 g_autofree gchar *path = NULL;
76
77 /* set up the device on/off switch */
78 state = nm_device_get_state (self->device);
79 gtk_widget_set_visible (GTK_WIDGET (self->device_off_switch),
80 state != NM_DEVICE_STATE_UNAVAILABLE
81 && state != NM_DEVICE_STATE_UNMANAGED);
82 update_off_switch_from_device_state (self->device_off_switch, state, self);
83 }
84
85 static void
86 device_off_switch_changed_cb (NetDeviceBluetooth *self)
87 {
88 const GPtrArray *acs;
89 gboolean active;
90 gint i;
91 NMActiveConnection *a;
92 NMConnection *connection;
93
94 if (self->updating_device)
95 return;
96
97 connection = net_device_get_find_connection (self->client, self->device);
98 if (connection == NULL)
99 return;
100
101 active = gtk_switch_get_active (self->device_off_switch);
102 if (active) {
103 nm_client_activate_connection_async (self->client,
104 connection,
105 self->device,
106 NULL, NULL, NULL, NULL);
107 } else {
108 const gchar *uuid;
109
110 uuid = nm_connection_get_uuid (connection);
111 acs = nm_client_get_active_connections (self->client);
112 for (i = 0; acs && i < acs->len; i++) {
113 a = (NMActiveConnection*)acs->pdata[i];
114 if (strcmp (nm_active_connection_get_uuid (a), uuid) == 0) {
115 nm_client_deactivate_connection_async (self->client, a, NULL, NULL, NULL);
116 break;
117 }
118 }
119 }
120 }
121
122 static void
123 editor_done (NetDeviceBluetooth *self)
124 {
125 nm_device_bluetooth_refresh_ui (self);
126 }
127
128 static void
129 options_button_clicked_cb (NetDeviceBluetooth *self)
130 {
131 NMConnection *connection;
132 NetConnectionEditor *editor;
133
134 connection = net_device_get_find_connection (self->client, self->device);
135
136 editor = net_connection_editor_new (connection, self->device, NULL, self->client);
137 gtk_window_set_transient_for (GTK_WINDOW (editor), GTK_WINDOW (gtk_widget_get_native (GTK_WIDGET (self))));
138 net_connection_editor_set_title (editor, _("Bluetooth"));
139 g_signal_connect_object (editor, "done", G_CALLBACK (editor_done), self, G_CONNECT_SWAPPED);
140 gtk_window_present (GTK_WINDOW (editor));
141 }
142
143 static void
144 net_device_bluetooth_finalize (GObject *object)
145 {
146 NetDeviceBluetooth *self = NET_DEVICE_BLUETOOTH (object);
147
148 g_clear_object (&self->client);
149 g_clear_object (&self->device);
150
151 G_OBJECT_CLASS (net_device_bluetooth_parent_class)->finalize (object);
152 }
153
154 static void
155 net_device_bluetooth_class_init (NetDeviceBluetoothClass *klass)
156 {
157 GObjectClass *object_class = G_OBJECT_CLASS (klass);
158 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
159
160 object_class->finalize = net_device_bluetooth_finalize;
161
162 gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/control-center/network/network-bluetooth.ui");
163
164 gtk_widget_class_bind_template_child (widget_class, NetDeviceBluetooth, device_off_switch);
165 gtk_widget_class_bind_template_child (widget_class, NetDeviceBluetooth, options_button);
166
167 gtk_widget_class_bind_template_callback (widget_class, device_off_switch_changed_cb);
168 gtk_widget_class_bind_template_callback (widget_class, options_button_clicked_cb);
169
170 }
171
172 static void
173 net_device_bluetooth_init (NetDeviceBluetooth *self)
174 {
175 gtk_widget_init_template (GTK_WIDGET (self));
176 }
177
178 NetDeviceBluetooth *
179 net_device_bluetooth_new (NMClient *client, NMDevice *device)
180 {
181 NetDeviceBluetooth *self;
182
183 self = g_object_new (net_device_bluetooth_get_type (), NULL);
184 self->client = g_object_ref (client);
185 self->device = g_object_ref (device);
186
187 g_signal_connect_object (device, "state-changed", G_CALLBACK (nm_device_bluetooth_refresh_ui), self, G_CONNECT_SWAPPED);
188
189 nm_device_bluetooth_refresh_ui (self);
190
191 return self;
192 }
193
194 NMDevice *
195 net_device_bluetooth_get_device (NetDeviceBluetooth *self)
196 {
197 g_return_val_if_fail (NET_IS_DEVICE_BLUETOOTH (self), NULL);
198 return self->device;
199 }
200