-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Device.vala
286 lines (256 loc) · 9.97 KB
/
Device.vala
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
/*-
* Copyright (c) 2015-2023 elementary LLC. (https://elementary.io)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Library 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 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 Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
public class BluetoothIndicator.Widgets.Device : Gtk.ListBoxRow {
private const string DEFAULT_ICON = "bluetooth";
private const string OBEX_AGENT = "org.bluez.obex.Agent1";
private const string OBEX_PATH = "/org/bluez/obex/elementary";
public signal void show_device (BluetoothIndicator.Services.Device device);
public BluetoothIndicator.Services.Device device { get; construct; }
public BluetoothIndicator.Services.ObexManager obex_manager { get; construct; }
public BluetoothIndicator.Services.Obex.Transfer transfer;
private Gtk.Label status_label;
private Gtk.Label name_label;
private Gtk.Label progress_label;
private Gtk.Label file_label;
private Gtk.Image icon_image;
private Gtk.Image status_image;
private Gtk.Spinner spinner;
private Gtk.Revealer progress_revealer;
private Gtk.ProgressBar progressbar;
public Device (BluetoothIndicator.Services.Device device, BluetoothIndicator.Services.ObexManager obex_manager) {
Object (device: device,
obex_manager: obex_manager
);
}
construct {
obex_manager.transfer_added.connect (on_obex_transfer_added);
obex_manager.transfer_removed.connect (on_obex_transfer_removed);
obex_manager.transfer_active.connect (on_obex_transfer_active);
name_label = new Gtk.Label (null) {
halign = Gtk.Align.START,
use_markup = true,
valign = Gtk.Align.END,
vexpand = true
};
status_label = new Gtk.Label (_("Not Connected")) {
halign = Gtk.Align.START,
valign = Gtk.Align.START,
vexpand = true
};
spinner = new Gtk.Spinner () {
halign = Gtk.Align.START,
valign = Gtk.Align.START,
hexpand = true
};
var size_group = new Gtk.SizeGroup (Gtk.SizeGroupMode.VERTICAL);
size_group.add_widget (status_label);
size_group.add_widget (spinner);
icon_image = new Gtk.Image.from_icon_name (device.icon == null ? DEFAULT_ICON : device.icon, Gtk.IconSize.DIALOG);
status_image = new Gtk.Image.from_icon_name ("emblem-disabled", Gtk.IconSize.MENU) {
halign = Gtk.Align.END,
valign = Gtk.Align.END
};
progress_label = new Gtk.Label (null) {
halign = Gtk.Align.START,
valign = Gtk.Align.END,
use_markup = true,
hexpand = true
};
progressbar = new Gtk.ProgressBar () {
hexpand = true
};
file_label = new Gtk.Label (null) {
ellipsize = Pango.EllipsizeMode.MIDDLE,
halign = Gtk.Align.START,
valign = Gtk.Align.END,
use_markup = true,
hexpand = true
};
var content_grid = new Gtk.Grid ();
content_grid.attach (file_label, 0, 0);
content_grid.attach (progressbar, 0, 1);
content_grid.attach (progress_label, 0, 2);
progress_revealer = new Gtk.Revealer () {
transition_type = Gtk.RevealerTransitionType.SLIDE_DOWN,
margin_start = 5,
margin_end = 5
};
progress_revealer.add (content_grid);
var overlay = new Gtk.Overlay ();
overlay.add (icon_image);
overlay.add_overlay (status_image);
var grid = new Gtk.Grid () {
column_spacing = 5,
margin_end = 5
};
grid.attach (overlay, 0, 0, 1, 2);
grid.attach (name_label, 1, 0, 2, 1);
grid.attach (status_label, 1, 1, 1, 1);
grid.attach (spinner, 2, 1, 1, 1);
var box_grid = new Gtk.Grid ();
box_grid.attach (grid, 0, 0);
box_grid.attach (progress_revealer, 0, 1);
add (box_grid);
((DBusProxy) device).g_properties_changed.connect (update_status);
update_status ();
get_style_context ().add_class (Gtk.STYLE_CLASS_MENUITEM);
selectable = false;
obex_manager.active_transfers.foreach ((transfer, address)=> {
on_obex_transfer_added (address, transfer);
});
}
private void on_obex_transfer_removed (BluetoothIndicator.Services.Obex.Transfer transfer) {
hide_action ();
}
private void on_obex_transfer_active (string address) {
if (address == device.address) {
update_transfer_progress ();
}
}
private void on_obex_transfer_added (string address, BluetoothIndicator.Services.Obex.Transfer transfer) {
if (address == device.address) {
this.transfer = transfer;
}
}
private void update_transfer_progress () {
switch (transfer.status) {
case "error":
hide_action ();
break;
case "queued":
hide_action ();
break;
case "active":
progressbar.fraction = (double) transfer.transferred / (double) transfer.size;
progress_revealer.reveal_child = true;
string name = transfer.name;
if (name != null) {
file_label.set_markup (_("<b>Filename</b>: %s").printf (GLib.Markup.escape_text (name)));
}
string filename = transfer.filename;
if (filename != null) {
if (bt_status (filename)) {
progress_label.label = _("Receiving… %s of %s").printf (
format_size (transfer.transferred), format_size (transfer.size)
);
} else {
progress_label.label = _("Sending… %s of %s").printf (
format_size (transfer.transferred), format_size (transfer.size)
);
}
}
break;
case "complete":
hide_action ();
break;
}
}
private bool bt_status (string filename) {
return filename.contains ("/.cache/obexd")? true : false;
}
public void hide_action () {
progress_label.label = "";//nothing pango ellipsize should free label
progress_revealer.reveal_child = false;
}
public async void toggle_device () {
if (progress_revealer.child_revealed) {
try {
var connection = yield GLib.Bus.get (BusType.SESSION);
yield connection.call (
OBEX_AGENT,
OBEX_PATH,
OBEX_AGENT,
"TransferActive",
new Variant ("(s)",
transfer.session),
null,
GLib.DBusCallFlags.NONE,
-1
);
} catch (Error e) {
critical (e.message);
}
return;
}
if (spinner.active) {
return;
}
spinner.active = true;
status_image.icon_name = "emblem-mixed";
try {
if (!device.connected) {
status_label.label = _("Connecting…");
yield device.connect ();
} else {
status_label.label = _("Disconnecting…");
yield device.disconnect ();
}
} catch (Error e) {
critical (e.message);
status_label.label = _("Unable to Connect");
status_image.icon_name = "emblem-error";
}
spinner.active = false;
}
private void update_status () {
string? device_name = device.alias;
if (device_name == null) {
if (device.icon != null) {
switch (device.icon) {
case "audio-card":
device_name = _("Speaker");
break;
case "audio-headphones":
device_name = _("Headphones");
break;
case "input-gaming":
device_name = _("Controller");
break;
case "input-keyboard":
device_name = _("Keyboard");
break;
case "input-mouse":
device_name = _("Mouse");
break;
case "input-tablet":
device_name = _("Tablet");
break;
case "input-touchpad":
device_name = _("Touchpad");
break;
case "phone":
device_name = _("Phone");
break;
default:
device_name = device.address;
break;
}
} else {
device_name = device.address;
}
}
name_label.label = "<b>%s</b>".printf (Markup.escape_text (device_name));
if (device.connected) {
status_label.label = _("Connected");
status_image.icon_name = "emblem-enabled";
} else {
status_label.label = _("Not Connected");
status_image.icon_name = "emblem-disabled";
}
icon_image.icon_name = device.icon == null ? DEFAULT_ICON : device.icon;
}
}