1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
/*
*
* Copyright (C) 2013 Bastien Nocera <hadess@hadess.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "config.h"
#include <gtk/gtk.h>
#include <glib/gi18n-lib.h>
#include "bluetooth-settings-row.h"
#include "bluetooth-enums.h"
#include "bluetooth-utils.h"
#include "gnome-bluetooth-enum-types.h"
struct _BluetoothSettingsRow {
GtkListBoxRow parent_instance;
/* Widget */
GtkWidget *label;
GtkWidget *status;
GtkWidget *spinner;
/* Properties */
GDBusProxy *proxy;
BluetoothDevice *device;
gboolean paired;
gboolean trusted;
BluetoothType type;
gboolean connected;
char *name;
char *alias;
char *bdaddr;
gboolean legacy_pairing;
gint64 time_created;
gboolean pairing;
};
enum {
PROP_0,
PROP_PROXY,
PROP_DEVICE,
PROP_PAIRED,
PROP_TRUSTED,
PROP_TYPE,
PROP_CONNECTED,
PROP_NAME,
PROP_ALIAS,
PROP_ADDRESS,
PROP_PAIRING,
PROP_LEGACY_PAIRING,
PROP_TIME_CREATED
};
G_DEFINE_TYPE(BluetoothSettingsRow, bluetooth_settings_row, GTK_TYPE_LIST_BOX_ROW)
static void
label_might_change (BluetoothSettingsRow *self)
{
if (!self->paired && !self->trusted)
gtk_label_set_text (GTK_LABEL (self->status), _("Not Set Up"));
else if (self->connected)
gtk_label_set_text (GTK_LABEL (self->status), _("Connected"));
else
gtk_label_set_text (GTK_LABEL (self->status), _("Disconnected"));
if (self->pairing)
gtk_widget_show (self->spinner);
else
gtk_widget_show (self->status);
}
static void
bluetooth_settings_row_init (BluetoothSettingsRow *self)
{
gtk_widget_init_template (GTK_WIDGET (self));
/* Placeholder text and spinner */
g_object_bind_property (self->spinner, "visible",
self->status, "visible", G_BINDING_INVERT_BOOLEAN | G_BINDING_BIDIRECTIONAL);
g_object_bind_property (self->spinner, "spinning",
self->status, "visible", G_BINDING_INVERT_BOOLEAN | G_BINDING_BIDIRECTIONAL);
self->time_created = g_get_monotonic_time();
}
static void
bluetooth_settings_row_finalize (GObject *object)
{
BluetoothSettingsRow *self = BLUETOOTH_SETTINGS_ROW (object);
g_clear_object (&self->proxy);
g_clear_object (&self->device);
g_clear_pointer (&self->name, g_free);
g_clear_pointer (&self->alias, g_free);
g_clear_pointer (&self->bdaddr, g_free);
G_OBJECT_CLASS(bluetooth_settings_row_parent_class)->finalize(object);
}
static void
bluetooth_settings_row_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
BluetoothSettingsRow *self = BLUETOOTH_SETTINGS_ROW (object);
switch (property_id) {
case PROP_PROXY:
g_value_set_object (value, self->proxy);
break;
case PROP_DEVICE:
g_value_set_object (value, self->device);
break;
case PROP_PAIRED:
g_value_set_boolean (value, self->paired);
break;
case PROP_TRUSTED:
g_value_set_boolean (value, self->trusted);
break;
case PROP_TYPE:
g_value_set_flags (value, self->type);
break;
case PROP_CONNECTED:
g_value_set_boolean (value, self->connected);
break;
case PROP_NAME:
g_value_set_string (value, self->name);
break;
case PROP_ALIAS:
g_value_set_string (value, self->alias);
break;
case PROP_ADDRESS:
g_value_set_string (value, self->bdaddr);
break;
case PROP_PAIRING:
g_value_set_boolean (value, self->pairing);
break;
case PROP_LEGACY_PAIRING:
g_value_set_boolean (value, self->legacy_pairing);
break;
case PROP_TIME_CREATED:
g_value_set_int64 (value, self->time_created);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
break;
}
}
static void
update_row (BluetoothSettingsRow *self)
{
if (self->name == NULL) {
gtk_label_set_text (GTK_LABEL (self->label),
bluetooth_type_to_string (self->type));
gtk_widget_set_sensitive (GTK_WIDGET (self), FALSE);
} else {
gtk_label_set_text (GTK_LABEL (self->label), self->alias);
gtk_widget_set_sensitive (GTK_WIDGET (self), TRUE);
}
}
static void
bluetooth_settings_row_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
BluetoothSettingsRow *self = BLUETOOTH_SETTINGS_ROW (object);
switch (property_id) {
case PROP_PROXY:
g_clear_object (&self->proxy);
self->proxy = g_value_dup_object (value);
break;
case PROP_DEVICE:
g_assert (!self->device);
self->device = g_value_dup_object (value);
break;
case PROP_PAIRED:
self->paired = g_value_get_boolean (value);
label_might_change (self);
break;
case PROP_TRUSTED:
self->trusted = g_value_get_boolean (value);
label_might_change (self);
break;
case PROP_TYPE:
self->type = g_value_get_flags (value);
update_row (self);
break;
case PROP_CONNECTED:
self->connected = g_value_get_boolean (value);
label_might_change (self);
break;
case PROP_NAME:
g_free (self->name);
self->name = g_value_dup_string (value);
update_row (self);
break;
case PROP_ALIAS:
g_free (self->alias);
self->alias = g_value_dup_string (value);
update_row (self);
break;
case PROP_ADDRESS:
g_free (self->bdaddr);
self->bdaddr = g_value_dup_string (value);
break;
case PROP_PAIRING:
self->pairing = g_value_get_boolean (value);
label_might_change (self);
break;
case PROP_LEGACY_PAIRING:
self->legacy_pairing = g_value_get_boolean (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
break;
}
}
static void
bluetooth_settings_row_class_init (BluetoothSettingsRowClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
object_class->finalize = bluetooth_settings_row_finalize;
object_class->get_property = bluetooth_settings_row_get_property;
object_class->set_property = bluetooth_settings_row_set_property;
g_object_class_install_property (object_class, PROP_PROXY,
g_param_spec_object ("proxy", NULL,
"The D-Bus proxy object of the device",
G_TYPE_DBUS_PROXY, G_PARAM_READWRITE));
g_object_class_install_property (object_class, PROP_DEVICE,
g_param_spec_object ("device", NULL,
"a BluetoothDevice object",
BLUETOOTH_TYPE_DEVICE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (object_class, PROP_PAIRED,
g_param_spec_boolean ("paired", NULL,
"Paired",
FALSE, G_PARAM_READWRITE));
g_object_class_install_property (object_class, PROP_TRUSTED,
g_param_spec_boolean ("trusted", NULL,
"Trusted",
FALSE, G_PARAM_READWRITE));
g_object_class_install_property (object_class, PROP_TYPE,
g_param_spec_flags ("type", NULL,
"Type",
BLUETOOTH_TYPE_TYPE, BLUETOOTH_TYPE_ANY, G_PARAM_READWRITE));
g_object_class_install_property (object_class, PROP_CONNECTED,
g_param_spec_boolean ("connected", NULL,
"Connected",
FALSE, G_PARAM_READWRITE));
g_object_class_install_property (object_class, PROP_NAME,
g_param_spec_string ("name", NULL,
"Name",
NULL, G_PARAM_READWRITE));
g_object_class_install_property (object_class, PROP_ALIAS,
g_param_spec_string ("alias", NULL,
"Alias",
NULL, G_PARAM_READWRITE));
g_object_class_install_property (object_class, PROP_ADDRESS,
g_param_spec_string ("address", NULL,
"Address",
NULL, G_PARAM_READWRITE));
g_object_class_install_property (object_class, PROP_PAIRING,
g_param_spec_boolean ("pairing", NULL,
"Pairing",
FALSE, G_PARAM_READWRITE));
g_object_class_install_property (object_class, PROP_LEGACY_PAIRING,
g_param_spec_boolean ("legacy-pairing", NULL,
"Legacy pairing",
FALSE, G_PARAM_READWRITE));
g_object_class_install_property (object_class, PROP_TIME_CREATED,
g_param_spec_int64 ("time-created", NULL,
"Time Created",
G_MININT64, G_MAXINT64, 0, G_PARAM_READABLE));
/* Bind class to template */
gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/bluetooth/bluetooth-settings-row.ui");
gtk_widget_class_bind_template_child (widget_class, BluetoothSettingsRow, label);
gtk_widget_class_bind_template_child (widget_class, BluetoothSettingsRow, spinner);
gtk_widget_class_bind_template_child (widget_class, BluetoothSettingsRow, status);
}
static gboolean
name_to_visible (GBinding *binding,
const GValue *from_value,
GValue *to_value,
gpointer user_data)
{
const char *name;
name = g_value_get_string (from_value);
g_value_set_boolean (to_value, name != NULL);
return TRUE;
}
/**
* bluetooth_settings_row_new_from_device:
*
* Returns a new #BluetoothSettingsRow widget populated from
* the #BluetoothDevice passed.
*
* Return value: A #BluetoothSettingsRow widget
**/
GtkWidget *
bluetooth_settings_row_new_from_device (BluetoothDevice *device)
{
g_autoptr(GDBusProxy) proxy = NULL;
GtkWidget *row;
const char *props[] = {
"name",
"alias",
"paired",
"trusted",
"connected",
"address",
"type",
"legacy-pairing",
"proxy"
};
guint i;
g_return_val_if_fail (BLUETOOTH_IS_DEVICE (device), NULL);
g_object_get (G_OBJECT (device), "proxy", &proxy, NULL);
row = g_object_new (BLUETOOTH_TYPE_SETTINGS_ROW,
"device", device,
NULL);
for (i = 0; i < G_N_ELEMENTS (props); i++) {
g_object_bind_property (G_OBJECT (device), props[i],
G_OBJECT (row), props[i],
G_BINDING_SYNC_CREATE);
}
g_object_bind_property_full (G_OBJECT (device), "name",
G_OBJECT (row), "visible",
G_BINDING_SYNC_CREATE,
name_to_visible, NULL,
NULL, NULL);
return row;
}
|