Merge lp:~donadigo/switchboard-plug-networking/page-in-deviceitem into lp:~elementary-pantheon/switchboard-plug-networking/trunk

Proposed by Adam Bieńkowski
Status: Merged
Approved by: xapantu
Approved revision: 169
Merged at revision: 170
Proposed branch: lp:~donadigo/switchboard-plug-networking/page-in-deviceitem
Merge into: lp:~elementary-pantheon/switchboard-plug-networking/trunk
Diff against target: 357 lines (+79/-103)
6 files modified
src/Plug.vala (+22/-38)
src/Utils.vala (+15/-15)
src/Widgets/Device/DeviceItem.vala (+11/-9)
src/Widgets/DeviceList.vala (+21/-35)
src/Widgets/InfoBox.vala (+0/-1)
src/Widgets/Proxy/ProxyPage.vala (+10/-5)
To merge this branch: bzr merge lp:~donadigo/switchboard-plug-networking/page-in-deviceitem
Reviewer Review Type Date Requested Status
xapantu (community) Approve
Review via email: mp+275099@code.launchpad.net

Commit message

* Soft-code page switching.
* Page variable directly in DeviceItem.
* Do not hardcode proxy item.
* Removed unused methods.
* ProxyPage: changed tabs to spaces.
* Code clean.

Description of the change

This is the new version of page-in-deviceitem refreshed for new revision.

***
The goal here is to remove as much hard-coded functions as possible. Creating page variable directly in the DeviceItem allows us to easily add and manage switching pages just by getting the variable from the device item. In consequence proxy page is no longer a "special" item but normal item that has it's page.

To post a comment you must log in.
Revision history for this message
xapantu (xapantu) wrote :

There are several minor bugs, but I'm going to report them and we can fix them later.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/Plug.vala'
2--- src/Plug.vala 2015-09-11 12:46:58 +0000
3+++ src/Plug.vala 2015-10-20 20:37:51 +0000
4@@ -47,20 +47,24 @@
5
6
7 protected override void add_interface (WidgetNMInterface widget_interface) {
8- device_list.add_device_to_list (widget_interface.device);
9- content.add(widget_interface);
10+ device_list.add_device_to_list (widget_interface);
11+
12+ select_first ();
13+ show_all ();
14+ }
15+
16+ protected override void remove_interface (WidgetNMInterface widget_interface) {
17+ device_list.remove_device_from_list (widget_interface.device);
18+ content.remove (widget_interface);
19
20+ select_first ();
21+ show_all ();
22+ }
23+
24+ private void select_first () {
25 if (network_interface.length () <= 1) {
26 device_list.select_first_item ();
27- }
28- show_all ();
29- }
30-
31- protected override void remove_interface (WidgetNMInterface widget_interface) {
32- device_list.remove_device_from_list (widget_interface.device);
33- content.remove(widget_interface);
34-
35- show_all ();
36+ }
37 }
38
39 protected override void build_ui () {
40@@ -70,7 +74,7 @@
41 content = new Gtk.Stack ();
42
43 var sidebar = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
44- device_list = new Widgets.DeviceList (client);
45+ device_list = new Widgets.DeviceList ();
46
47 footer = new Widgets.Footer (client);
48 footer.hexpand = false;
49@@ -109,34 +113,14 @@
50
51 /* Main function to connect all the signals */
52 private void connect_signals () {
53- device_list.create_proxy_entry ();
54-
55- var proxy_page = new Widgets.ProxyPage ();
56- proxy_page.stack.set_visible_child_name ("configuration");
57-
58- proxy_page.update_status_label.connect ((mode) => {
59- device_list.proxy.switch_status (null, mode);
60- });
61-
62- proxy_page.update_mode ();
63-
64- content.add_named (proxy_page, "proxy-page");
65- device_list.proxy.activate.connect (() => {
66- if (content.get_visible_child_name () != "proxy-page")
67- content.set_visible_child (proxy_page);
68-
69- current_device = null;
70- });
71-
72- device_list.row_changed.connect ((row) => {
73- NM.Device device = (row as Widgets.DeviceItem).get_item_device ();
74- foreach(var w in network_interface) {
75- if(w.is_device(device)) {
76- content.set_visible_child(w);
77- }
78+ device_list.row_activated.connect ((row) => {
79+ if (!Utils.list_contains (content.get_children (), ((Widgets.DeviceItem) row).page)) {
80+ content.add (((Widgets.DeviceItem) row).page);
81 }
82+
83+ content.visible_child = ((Widgets.DeviceItem) row).page;
84 });
85-
86+
87 device_list.show_no_devices.connect ((show) => {
88 if (show) {
89 content.set_visible_child (no_devices);
90
91=== modified file 'src/Utils.vala'
92--- src/Utils.vala 2015-10-09 19:12:42 +0000
93+++ src/Utils.vala 2015-10-20 20:37:51 +0000
94@@ -1,21 +1,11 @@
95 namespace Network.Utils {
96- public static Polkit.Permission? permission = null;
97-
98- public static Polkit.Permission? get_permission () {
99- if (permission != null)
100- return permission;
101- try {
102- var user = Polkit.UnixUser.new_for_name (Environment.get_user_name ()) as Polkit.UnixUser;
103- permission = new Polkit.Permission.sync ("org.freedesktop.NetworkManager.settings.modify.hostname",
104- Polkit.UnixProcess.new_for_owner (Posix.getpid (), 0, user.get_uid ()));
105- return permission;
106- } catch (Error e) {
107- critical (e.message);
108- return null;
109- }
110+ public enum ItemType {
111+ DEVICE = 0,
112+ PROXY,
113+ INVALID
114 }
115
116- public new Gtk.Button get_advanced_button_from_device (NM.Device? device, string title = _("Advanced Settings…")) {
117+ public Gtk.Button get_advanced_button_from_device (NM.Device? device, string title = _("Advanced Settings…")) {
118 var details_btn = new Gtk.Button.with_label (title);
119 details_btn.clicked.connect (() => {
120 new Granite.Services.SimpleCommand ("/usr/bin",
121@@ -25,6 +15,16 @@
122 return details_btn;
123 }
124
125+ public bool list_contains (List list, List.G data) {
126+ foreach (var unit in list.copy ()) {
127+ if (unit == data) {
128+ return true;
129+ }
130+ }
131+
132+ return false;
133+ }
134+
135 public string state_to_string (NM.DeviceState state) {
136 switch (state) {
137 case NM.DeviceState.ACTIVATED:
138
139=== modified file 'src/Widgets/Device/DeviceItem.vala'
140--- src/Widgets/Device/DeviceItem.vala 2015-10-08 19:04:34 +0000
141+++ src/Widgets/Device/DeviceItem.vala 2015-10-20 20:37:51 +0000
142@@ -22,7 +22,10 @@
143
144 namespace Network.Widgets {
145 public class DeviceItem : Gtk.ListBoxRow {
146- public bool special = false;
147+ public NM.Device? device = null;
148+ public Gtk.Box? page = null;
149+ public Utils.ItemType type;
150+
151 public Gtk.Label row_description;
152 private Gtk.Image row_image;
153 private Gtk.Image status_image;
154@@ -33,24 +36,23 @@
155
156 private Gtk.Grid row_grid;
157 private Gtk.Label row_title;
158- public NM.Device device = null;
159
160- public DeviceItem (string _title, string _subtitle, string _icon_name = "network-wired", bool _special = false) {
161- this.special = _special;
162+ public DeviceItem (string _title, string _subtitle, string _icon_name = "network-wired") {
163 this.title = _title;
164 this.subtitle = _subtitle;
165 this.icon_name = _icon_name;
166+ this.type = Utils.ItemType.INVALID;
167
168 create_ui (icon_name);
169 }
170
171- public DeviceItem.from_device (NM.Device _device,
172+ public DeviceItem.from_interface (WidgetNMInterface iface,
173 string _icon_name = "network-wired",
174- bool _special = false,
175 string _title = "") {
176- this.special = _special;
177- this.device = _device;
178-
179+ this.page = iface;
180+ this.device = iface.device;
181+ this.type = Utils.ItemType.DEVICE;
182+
183 if (_title != "") {
184 this.title = _title;
185 } else {
186
187=== modified file 'src/Widgets/DeviceList.vala'
188--- src/Widgets/DeviceList.vala 2015-10-08 19:04:34 +0000
189+++ src/Widgets/DeviceList.vala 2015-10-20 20:37:51 +0000
190@@ -22,7 +22,6 @@
191
192 namespace Network.Widgets {
193 public class DeviceList : Gtk.ListBox {
194- public signal void row_changed (Gtk.ListBoxRow row);
195 public signal void show_no_devices (bool show);
196
197 public NM.Client client;
198@@ -37,12 +36,11 @@
199
200 private int wireless_item = 0;
201
202- public DeviceList (NM.Client _client) {
203+ public DeviceList () {
204 this.selection_mode = Gtk.SelectionMode.SINGLE;
205 this.activate_on_single_click = true;
206 this.set_header_func (update_headers);
207
208- client = _client;
209 items = new List<DeviceItem> ();
210
211 settings_l = new Gtk.Label (_("Virtual"));
212@@ -53,54 +51,42 @@
213 devices_l.get_style_context ().add_class ("h4");
214 devices_l.halign = Gtk.Align.START;
215
216- this.row_selected.connect ((row) => {
217- if (row != null) {
218- if (row == proxy) {
219- proxy.activate ();
220- return;
221- }
222-
223- if (wifi == null || row != wifi) {
224- row_changed (row);
225- } else if (wifi != null && row == wifi) {
226- wifi.activate ();
227- }
228- }
229- });
230-
231 bool show = (items.length () > 0);
232 this.show_no_devices (!show);
233+ this.add_proxy ();
234 }
235
236- public void add_device_to_list (NM.Device device) {
237- if (device.get_device_type () == NM.DeviceType.WIFI) {
238+
239+ public void add_device_to_list (WidgetNMInterface iface) {
240+ if (iface.device.get_device_type () == NM.DeviceType.WIFI) {
241 string title = _("Wireless");
242 if (wireless_item > 0) {
243- title += " " + wireless_item.to_string ();
244+ title += SUFFIX + wireless_item.to_string ();
245 }
246
247- item = new DeviceItem.from_device (device, "network-wireless", false, title);
248+ item = new DeviceItem.from_interface (iface, "network-wireless", title);
249 wireless_item++;
250 } else {
251
252- if (!device.get_managed ()) {
253- warning("Unmanaged device? probably something that has just been added…");
254+ if (!iface.device.get_managed ()) {
255+ warning ("Unmanaged device, probably something that has just been added.");
256 }
257- if (device.get_iface ().has_prefix ("usb")) {
258- item = new DeviceItem.from_device (device, "drive-removable-media");
259+
260+ if (iface.device.get_iface ().has_prefix ("usb")) {
261+ item = new DeviceItem.from_interface (iface, "drive-removable-media");
262 } else {
263- item = new DeviceItem.from_device (device);
264+ item = new DeviceItem.from_interface (iface);
265 }
266 }
267
268 items.append (item);
269- insert (item, (int)(items.length() - 1));
270+ insert (item, (int) items.length () - 1);
271 show_all ();
272 }
273
274 public void remove_device_from_list (NM.Device device) {
275 foreach (var list_item in items) {
276- if(list_item.device == device) {
277+ if (list_item.device == device) {
278 remove_row_from_list (list_item);
279 break;
280 }
281@@ -114,18 +100,18 @@
282 this.select_row (this.get_row_at_index (0));
283 }
284
285- public void create_proxy_entry () {
286- proxy = new DeviceItem (_("Proxy"), "", "preferences-system-network", true);
287+ private void add_proxy () {
288+ proxy = new DeviceItem (_("Proxy"), "", "preferences-system-network");
289+ proxy.page = new Widgets.ProxyPage (proxy);
290+ proxy.type = Utils.ItemType.PROXY;
291 this.add (proxy);
292 }
293-
294 public void select_first_item () {
295- var first_row = this.get_row_at_index (0);
296- this.select_row (first_row);
297+ this.get_row_at_index (0).activate ();
298 }
299
300 private void update_headers (Gtk.ListBoxRow row, Gtk.ListBoxRow? before = null) {
301- if (row == proxy) {
302+ if (((DeviceItem) row).type != Utils.ItemType.DEVICE) {
303 row.set_header (settings_l);
304 } else if (row == items.nth_data (0)) {
305 row.set_header (devices_l);
306
307=== modified file 'src/Widgets/InfoBox.vala'
308--- src/Widgets/InfoBox.vala 2015-09-11 12:46:58 +0000
309+++ src/Widgets/InfoBox.vala 2015-10-20 20:37:51 +0000
310@@ -27,7 +27,6 @@
311 private NM.Device device;
312 private DeviceItem? owner;
313
314- private string status_l = (_("Status:") + SUFFIX);
315 private string ipaddress_l = (_("IP Address:") + SUFFIX);
316 private string mask_l = (_("Subnet mask:") + SUFFIX);
317 private string router_l = (_("Router:") + SUFFIX);
318
319=== modified file 'src/Widgets/Proxy/ProxyPage.vala'
320--- src/Widgets/Proxy/ProxyPage.vala 2015-10-08 19:04:34 +0000
321+++ src/Widgets/Proxy/ProxyPage.vala 2015-10-20 20:37:51 +0000
322@@ -3,7 +3,10 @@
323 public Gtk.Stack stack;
324 public signal void update_status_label (string mode);
325
326- public ProxyPage () {
327+ private DeviceItem owner;
328+
329+ public ProxyPage (DeviceItem _owner) {
330+ this.owner = _owner;
331 this.orientation = Gtk.Orientation.VERTICAL;
332 this.baseline_position = Gtk.BaselinePosition.CENTER;
333 this.margin_top = 20;
334@@ -19,17 +22,19 @@
335 stack.add_titled (exceptions_page, "exceptions", _("Exceptions"));
336 stackswitcher.stack = stack;
337
338- proxy_settings.changed.connect (() => {
339- update_mode ();
340- });
341+ proxy_settings.changed.connect (update_mode);
342+
343+ update_mode ();
344
345 this.add (stackswitcher);
346 this.add (stack);
347 this.show_all ();
348+
349+ stack.visible_child = configuration_page;
350 }
351
352 public void update_mode () {
353- this.update_status_label (proxy_settings.mode);
354+ owner.switch_status (null, proxy_settings.mode);
355 }
356 }
357 }

Subscribers

People subscribed via source and target branches

to all changes: