-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathMainView.vala
More file actions
216 lines (179 loc) · 6.77 KB
/
MainView.vala
File metadata and controls
216 lines (179 loc) · 6.77 KB
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
// -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-
/*-
* Copyright (c) 2016-2018 elementary LLC.
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* Authored by: Corentin Noël <corentin@elementary.io>
* Oleksandr Lynok <oleksandr.lynok@gmail.com>
*/
public class Bluetooth.MainView : Switchboard.SettingsPage {
private Gtk.ListBox list_box;
private Granite.OverlayBar overlaybar;
private Services.ObjectManager manager;
public signal void quit_plug ();
public MainView () {
Object (
title: _("Bluetooth"),
activatable: true
);
}
construct {
manager = Bluetooth.Services.ObjectManager.get_default ();
var empty_alert = new Granite.Placeholder (_("No Devices Found")) {
description = _("Please ensure that your devices are visible and ready for pairing.")
};
list_box = new Gtk.ListBox () {
activate_on_single_click = false,
selection_mode = BROWSE
};
list_box.add_css_class (Granite.STYLE_CLASS_RICH_LIST);
list_box.set_sort_func ((Gtk.ListBoxSortFunc) compare_rows);
list_box.set_header_func ((Gtk.ListBoxUpdateHeaderFunc) title_rows);
list_box.set_placeholder (empty_alert);
var scrolled = new Gtk.ScrolledWindow () {
child = list_box,
hexpand = true,
vexpand = true
};
var overlay = new Gtk.Overlay () {
child = scrolled
};
overlaybar = new Granite.OverlayBar (overlay) {
label = _("Discovering"),
active = true
};
var frame = new Gtk.Frame (null) {
child = overlay
};
child = frame;
if (manager.retrieve_finished) {
complete_setup ();
} else {
manager.notify["retrieve-finished"].connect (complete_setup);
}
list_box.row_activated.connect ((row) => {
((DeviceRow) row).on_activate.begin ();
});
status_switch.notify["active"].connect (() => {
manager.set_global_state.begin (status_switch.active);
});
}
private void complete_setup () {
foreach (var device in manager.get_devices ()) {
var row = new DeviceRow (device);
list_box.append (row);
}
var first_row = list_box.get_row_at_index (0);
if (first_row != null) {
list_box.select_row (first_row);
list_box.row_activated (first_row);
}
update_description ();
status_switch.active = manager.is_powered;
/* Now retrieve finished, we can connect manager signals */
manager.device_added.connect ((device) => {
var row = new DeviceRow (device);
list_box.append (row);
if (list_box.get_selected_row () == null) {
list_box.select_row (row);
list_box.row_activated (row);
}
});
manager.device_removed.connect_after ((device) => {
var child = list_box.get_first_child ();
while (child != null) {
if (((DeviceRow) child).device == device) {
list_box.remove (child);
break;
}
child = child.get_next_sibling ();
}
});
manager.adapter_added.connect ((adapter) => {
update_description ();
});
manager.adapter_removed.connect ((adapter) => {
if (!manager.has_object) {
quit_plug ();
} else {
update_description ();
}
});
manager.notify["discoverable"].connect (() => {
update_description ();
});
manager.notify["is-powered"].connect (() => {
update_description ();
});
manager.bind_property ("is-discovering", overlaybar, "visible", GLib.BindingFlags.DEFAULT);
manager.bind_property ("is-powered", status_switch, "active", GLib.BindingFlags.DEFAULT);
}
private void update_description () {
string? name = manager.get_name ();
var powered = manager.is_powered;
if (powered && manager.discoverable) {
//TRANSLATORS: \"%s\" represents the name of the adapter
description = _("Now discoverable as \"%s\". Not discoverable when this page is closed").printf (name ?? _("Unknown"));
} else if (!powered) {
description = _("Not discoverable while Bluetooth is powered off");
} else {
description = _("Not discoverable");
}
if (powered) {
icon = new ThemedIcon ("bluetooth");
} else {
icon = new ThemedIcon ("bluetooth-disabled");
}
}
[CCode (instance_pos = -1)]
private int compare_rows (DeviceRow row1, DeviceRow row2) {
unowned Services.Device device1 = row1.device;
unowned Services.Device device2 = row2.device;
if (device1.paired && !device2.paired) {
return -1;
}
if (!device1.paired && device2.paired) {
return 1;
}
if (device1.connected && !device2.connected) {
return -1;
}
if (!device1.connected && device2.connected) {
return 1;
}
if (device1.name != null && device2.name == null) {
return -1;
}
if (device1.name == null && device2.name != null) {
return 1;
}
var name1 = device1.name ?? device1.address;
var name2 = device2.name ?? device2.address;
return name1.collate (name2);
}
[CCode (instance_pos = -1)]
private void title_rows (DeviceRow row1, DeviceRow? row2) {
if (row2 == null && row1.device.paired) {
var label = new Granite.HeaderLabel (_("Paired Devices"));
row1.set_header (label);
} else if (row2 == null || row1.device.paired != row2.device.paired) {
/* This header may not appear, so cannot contain discovery spinner */
var label = new Granite.HeaderLabel (_("Nearby Devices"));
row1.set_header (label);
} else {
row1.set_header (null);
}
}
}