GCC Code Coverage Report


Directory: ./
File: panels/privacy/bolt/cc-bolt-device-entry.c
Date: 2024-05-04 07:58:27
Exec Total Coverage
Lines: 0 76 0.0%
Functions: 0 12 0.0%
Branches: 0 39 0.0%

Line Branch Exec Source
1 /* Copyright (C) 2018 Red Hat, Inc
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authors: Christian J. Kellner <ckellner@redhat.com>
17 *
18 */
19
20 #include <config.h>
21
22 #include "bolt-str.h"
23
24 #include "cc-bolt-device-entry.h"
25
26 #include <glib/gi18n.h>
27
28 #define RESOURCE_UI "/org/gnome/control-center/privacy/bolt/cc-bolt-device-entry.ui"
29
30 struct _CcBoltDeviceEntry
31 {
32 AdwActionRow parent;
33
34 BoltDevice *device;
35
36 /* main ui */
37 GtkLabel *status_warning;
38 gboolean show_warnings;
39 };
40
41 static const char * device_status_to_brief_for_ui (BoltDevice *dev);
42
43 G_DEFINE_TYPE (CcBoltDeviceEntry, cc_bolt_device_entry, ADW_TYPE_ACTION_ROW);
44
45 enum
46 {
47 SIGNAL_STATUS_CHANGED,
48 SIGNAL_LAST
49 };
50
51 static guint signals[SIGNAL_LAST] = { 0, };
52
53 static void
54 entry_set_name (CcBoltDeviceEntry *entry)
55 {
56 g_autofree char *name = NULL;
57 BoltDevice *dev = entry->device;
58
59 g_return_if_fail (dev != NULL);
60
61 name = bolt_device_get_display_name (dev);
62
63 adw_preferences_row_set_title (ADW_PREFERENCES_ROW (entry), name);
64 }
65
66 static void
67 entry_update_status (CcBoltDeviceEntry *entry)
68 {
69 const char *brief;
70 BoltStatus status;
71 gboolean warn;
72
73 status = bolt_device_get_status (entry->device);
74 brief = device_status_to_brief_for_ui (entry->device);
75
76 adw_action_row_set_subtitle (ADW_ACTION_ROW (entry), brief);
77
78 g_signal_emit (entry,
79 signals[SIGNAL_STATUS_CHANGED],
80 0,
81 status);
82
83 warn = entry->show_warnings && bolt_status_is_pending (status);
84 gtk_widget_set_visible (GTK_WIDGET (entry->status_warning), warn);
85 }
86
87 static void
88 on_device_notify_cb (GObject *gobject,
89 GParamSpec *pspec,
90 gpointer user_data)
91 {
92 CcBoltDeviceEntry *entry = CC_BOLT_DEVICE_ENTRY (user_data);
93 const char *what;
94
95 what = g_param_spec_get_name (pspec);
96
97 if (bolt_streq (what, "status"))
98 entry_update_status (entry);
99 else if (bolt_streq (what, "label") ||
100 bolt_streq (what, "name") ||
101 bolt_streq (what, "vendor"))
102 entry_set_name (entry);
103 }
104
105 /* device helpers */
106
107 static const char *
108 device_status_to_brief_for_ui (BoltDevice *dev)
109 {
110 BoltStatus status;
111 BoltAuthFlags aflags;
112 gboolean nopcie;
113
114 status = bolt_device_get_status (dev);
115 aflags = bolt_device_get_authflags(dev);
116 nopcie = bolt_flag_isset (aflags, BOLT_AUTH_NOPCIE);
117
118 switch (status)
119 {
120 case BOLT_STATUS_DISCONNECTED:
121 return C_("Thunderbolt Device Status", "Disconnected");
122
123 case BOLT_STATUS_CONNECTING:
124 return C_("Thunderbolt Device Status", "Connecting");
125
126 case BOLT_STATUS_CONNECTED:
127 case BOLT_STATUS_AUTHORIZED_DPONLY:
128 return C_("Thunderbolt Device Status", "Connected");
129
130 case BOLT_STATUS_AUTH_ERROR:
131 return C_("Thunderbolt Device Status", "Error");
132
133 case BOLT_STATUS_AUTHORIZING:
134 return C_("Thunderbolt Device Status", "Authorizing");
135
136 case BOLT_STATUS_AUTHORIZED:
137 case BOLT_STATUS_AUTHORIZED_NEWKEY:
138 case BOLT_STATUS_AUTHORIZED_SECURE:
139 if (nopcie)
140 return C_("Thunderbolt Device Status", "Connected");
141 else
142 return C_("Thunderbolt Device Status", "Authorized");
143
144 case BOLT_STATUS_UNKNOWN:
145 break; /* use function default */
146 }
147
148 return C_("Thunderbolt Device Status", "Unknown");
149 }
150
151 static void
152 cc_bolt_device_entry_finalize (GObject *object)
153 {
154 CcBoltDeviceEntry *entry = CC_BOLT_DEVICE_ENTRY (object);
155
156 g_clear_object (&entry->device);
157
158 G_OBJECT_CLASS (cc_bolt_device_entry_parent_class)->finalize (object);
159 }
160
161 static void
162 cc_bolt_device_entry_class_init (CcBoltDeviceEntryClass *klass)
163 {
164 GObjectClass *object_class = G_OBJECT_CLASS (klass);
165 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
166
167 object_class->finalize = cc_bolt_device_entry_finalize;
168
169 gtk_widget_class_set_template_from_resource (widget_class, RESOURCE_UI);
170 gtk_widget_class_bind_template_child (widget_class, CcBoltDeviceEntry, status_warning);
171
172 signals[SIGNAL_STATUS_CHANGED] =
173 g_signal_new ("status-changed",
174 G_TYPE_FROM_CLASS (object_class),
175 G_SIGNAL_RUN_LAST,
176 0,
177 NULL, NULL,
178 NULL,
179 G_TYPE_NONE,
180 1, BOLT_TYPE_STATUS);
181 }
182
183 static void
184 cc_bolt_device_entry_init (CcBoltDeviceEntry *entry)
185 {
186 gtk_widget_init_template (GTK_WIDGET (entry));
187 }
188
189 /* public function */
190
191 CcBoltDeviceEntry *
192 cc_bolt_device_entry_new (BoltDevice *device,
193 gboolean show_warnings)
194 {
195 CcBoltDeviceEntry *entry;
196
197 entry = g_object_new (CC_TYPE_BOLT_DEVICE_ENTRY, NULL);
198 entry->device = g_object_ref (device);
199 entry->show_warnings = show_warnings;
200
201 entry_set_name (entry);
202 entry_update_status (entry);
203
204 g_signal_connect_object (entry->device,
205 "notify",
206 G_CALLBACK (on_device_notify_cb),
207 entry,
208 0);
209
210 return entry;
211 }
212
213 BoltDevice *
214 cc_bolt_device_entry_get_device (CcBoltDeviceEntry *entry)
215 {
216 g_return_val_if_fail (entry != NULL, NULL);
217 g_return_val_if_fail (CC_IS_BOLT_DEVICE_ENTRY (entry), NULL);
218
219 return entry->device;
220 }
221