Merge lp:~donadigo/switchboard-plug-power/desktop-machines-fixes into lp:~elementary-apps/switchboard-plug-power/trunk

Proposed by Adam Bieńkowski
Status: Merged
Approved by: Cody Garver
Approved revision: 313
Merged at revision: 311
Proposed branch: lp:~donadigo/switchboard-plug-power/desktop-machines-fixes
Merge into: lp:~elementary-apps/switchboard-plug-power/trunk
Diff against target: 395 lines (+89/-86)
5 files modified
src/Battery.vala (+18/-21)
src/CliCommunicator.vala (+16/-14)
src/Interfaces.vala (+2/-0)
src/Plug.vala (+40/-39)
src/PowerSupply.vala (+13/-12)
To merge this branch: bzr merge lp:~donadigo/switchboard-plug-power/desktop-machines-fixes
Reviewer Review Type Date Requested Status
Cody Garver (community) testing, desktop Approve
Danielle Foré testing, laptop Approve
Review via email: mp+296501@code.launchpad.net

Commit message

Fix bug #1556607: "Power plug crashes on ALL desktop machines".
* Rename "return_value" to the actual name of returned variables.
* Code style fixes.
* Remove duplicated variables and replace them with constants.
* Remove unneded variables.
* Grammar fixes.
* Check if DBus interfaces are null to prevent crashes.

Description of the change

Fixes bug #1556607: "Power plug crashes on ALL desktop machines".

This branch makes an overall cleanup of the code, making it more stable and logical including:
* Renaming "return_value" to the actual name of returned variables.
* Code style fixes.
* Removing duplicated variables and replacing them with constants.
* Removing unneded variables.
* Gramatic fixes.
* Checking if DBus interfaces are null to prevent crashes.

I would like to have at least 2 reviews for either battery and desktop devices in order to merge this.

To post a comment you must log in.
312. By Adam Bieńkowski

Simplify condition

313. By Adam Bieńkowski

Remove unneded line

Revision history for this message
Danielle Foré (danrabbit) wrote :

I can confirm that this is working as expected on my notebook. No problems here :)

review: Approve (testing, laptop)
Revision history for this message
Cody Garver (codygarver) :
review: Approve (testing, desktop)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/Battery.vala'
2--- src/Battery.vala 2016-04-30 22:14:22 +0000
3+++ src/Battery.vala 2016-06-05 12:08:16 +0000
4@@ -18,11 +18,7 @@
5 */
6
7 namespace Power {
8- public class Battery {
9-
10- private const string dbus_upower_name = "org.freedesktop.UPower";
11- private const string dbus_upower_root_path = "/org/freedesktop/UPower";
12-
13+ public class Battery : Object {
14 private Upower? upower;
15 private UpowerDevice? upower_device;
16
17@@ -35,38 +31,39 @@
18 }
19
20 public bool check_present () {
21- bool return_value = false;
22+ bool present = false;
23 if (laptop) {
24- if (upower.OnBattery) {
25- return_value = true;
26- } else if (upower_device.IsPresent) {
27- return_value = true;
28- }
29- }
30- return return_value;
31+ if (upower.OnBattery || upower_device.IsPresent) {
32+ present = true;
33+ }
34+ }
35+
36+ return present;
37 }
38
39- private string get_dbus_path(Upower upow) {
40- string return_value = "";
41+ private string get_dbus_path (Upower upow) {
42+ string path = "";
43 try {
44 ObjectPath[] devs = upow.EnumerateDevices();
45- for (int i =0; i<devs.length; i++) {
46- if (devs[i].contains("BAT0")) {
47- return_value = devs[i].to_string();
48+ for (int i = 0; i < devs.length; i++) {
49+ if (devs[i].contains ("BAT0")) {
50+ path = devs[i].to_string();
51+ break;
52 }
53 }
54 } catch (Error e) {
55 critical("acpi couldn't get upower devices");
56 }
57- return return_value;
58+
59+ return path;
60 }
61
62 private void connect_dbus () {
63 try {
64- upower = Bus.get_proxy_sync (BusType.SYSTEM, dbus_upower_name, dbus_upower_root_path, DBusProxyFlags.NONE);
65+ upower = Bus.get_proxy_sync (BusType.SYSTEM, DBUS_UPOWER_NAME, DBUS_UPOWER_PATH, DBusProxyFlags.NONE);
66 dbus_upower_battery_path = get_dbus_path(upower);
67 if (dbus_upower_battery_path != "" && dbus_upower_battery_path != null) {
68- upower_device = Bus.get_proxy_sync (BusType.SYSTEM, dbus_upower_name, dbus_upower_battery_path, DBusProxyFlags.GET_INVALIDATED_PROPERTIES);
69+ upower_device = Bus.get_proxy_sync (BusType.SYSTEM, DBUS_UPOWER_NAME, dbus_upower_battery_path, DBusProxyFlags.GET_INVALIDATED_PROPERTIES);
70
71 laptop = true;
72 debug ("battery path:%s , its a laptop, dbus connected", dbus_upower_battery_path);
73
74=== modified file 'src/CliCommunicator.vala'
75--- src/CliCommunicator.vala 2016-04-30 17:36:20 +0000
76+++ src/CliCommunicator.vala 2016-06-05 12:08:16 +0000
77@@ -40,51 +40,53 @@
78 }
79
80 private string action_to_string (Action value) {
81- string return_val = "NOT_SUPPORTED";
82+ string str = "NOT_SUPPORTED";
83 switch (value) {
84 case Action.IGNORE:
85- return_val = "ignore";
86+ str = "ignore";
87 break;
88 case Action.POWEROFF:
89- return_val = "poweroff";
90+ str = "poweroff";
91 break;
92 case Action.LOCK:
93- return_val = "lock";
94+ str = "lock";
95 break;
96 case Action.SUSPEND:
97- return_val = "suspend";
98+ str = "suspend";
99 break;
100 case Action.HALT:
101- return_val = "halt";
102+ str = "halt";
103 break;
104 default:
105 break;
106 }
107- return return_val;
108+
109+ return str;
110 }
111
112 private Action string_to_action (string value) {
113- Action return_val = Action.NOT_SUPPORTED;
114+ Action action = Action.NOT_SUPPORTED;
115 switch (value) {
116 case "ignore":
117- return_val = Action.IGNORE;
118+ action = Action.IGNORE;
119 break;
120 case "poweroff":
121- return_val = Action.POWEROFF;
122+ action = Action.POWEROFF;
123 break;
124 case "lock":
125- return_val = Action.LOCK;
126+ action = Action.LOCK;
127 break;
128 case "suspend":
129- return_val = Action.SUSPEND;
130+ action = Action.SUSPEND;
131 break;
132 case "halt":
133- return_val = Action.HALT;
134+ action = Action.HALT;
135 break;
136 default:
137 break;
138 }
139- return return_val;
140+
141+ return action;
142 }
143
144 private void seperate_string (string value) {
145
146=== modified file 'src/Interfaces.vala'
147--- src/Interfaces.vala 2016-05-24 17:21:48 +0000
148+++ src/Interfaces.vala 2016-06-05 12:08:16 +0000
149@@ -18,6 +18,8 @@
150 */
151
152 namespace Power {
153+ public const string DBUS_UPOWER_NAME = "org.freedesktop.UPower";
154+ public const string DBUS_UPOWER_PATH = "/org/freedesktop/UPower";
155
156 [DBus (name = "org.gnome.SettingsDaemon.Power.Screen")]
157 interface PowerSettings : GLib.Object {
158
159=== modified file 'src/Plug.vala'
160--- src/Plug.vala 2016-05-01 00:04:56 +0000
161+++ src/Plug.vala 2016-06-05 12:08:16 +0000
162@@ -24,7 +24,6 @@
163 Gtk.Grid main_grid;
164
165 public class Plug : Switchboard.Plug {
166-
167 private Gtk.SizeGroup label_size;
168 private Gtk.StackSwitcher stack_switcher;
169 private GLib.Settings pantheon_dpms_settings;
170@@ -36,7 +35,8 @@
171 private Gtk.Image lock_image;
172 private Gtk.Image lock_image2;
173
174- private const string no_permission_string = _("You do not have permission to change this");
175+ private const string NO_PERMISSION_STRING = _("You do not have permission to change this");
176+ private const string LAPTOP_DETECT_BINARY = "/usr/sbin/laptop-detect";
177
178 public Plug () {
179 Object (category: Category.HARDWARE,
180@@ -62,10 +62,10 @@
181 }
182
183 public override void shown () {
184- if (power_supply.check_present ()) {
185+ if (battery.check_present ()) {
186+ stack_switcher.get_stack ().visible_child_name = "battery";
187+ } else {
188 stack_switcher.get_stack ().visible_child_name = "ac";
189- } else {
190- stack_switcher.get_stack ().visible_child_name = "battery";
191 }
192 }
193
194@@ -99,13 +99,16 @@
195
196 Gtk.Stack stack = new Gtk.Stack ();
197
198- Gtk.Grid plug_grid = create_notebook_pages ("ac");
199+ Gtk.Grid plug_grid = create_notebook_pages (true);
200 stack.add_titled (plug_grid, "ac", _("Plugged In"));
201
202 stack_container.add (info_bars);
203
204+ stack_switcher = new Gtk.StackSwitcher ();
205+ stack_switcher.stack = stack;
206+
207 if (laptop_detect () || battery.laptop) {
208- Gtk.Grid battery_grid = create_notebook_pages ("battery");
209+ Gtk.Grid battery_grid = create_notebook_pages (false);
210 stack.add_titled (battery_grid, "battery", _("On Battery"));
211
212 var left_sep = new Gtk.Separator (Gtk.Orientation.HORIZONTAL);
213@@ -114,9 +117,6 @@
214 var right_sep = new Gtk.Separator (Gtk.Orientation.HORIZONTAL);
215 right_sep.hexpand = true;
216
217- stack_switcher = new Gtk.StackSwitcher ();
218- stack_switcher.stack = stack;
219-
220 var switcher_grid = new Gtk.Grid ();
221 switcher_grid.margin_top = 24;
222 switcher_grid.margin_bottom = 12;
223@@ -132,7 +132,8 @@
224
225 stack_container.margin_bottom = 12;
226 stack_container.show_all ();
227- // hide stack switcher we only have ac line
228+
229+ // hide stack switcher if we only have ac line
230 stack_switcher.visible = stack.get_children ().length () > 1;
231 }
232
233@@ -197,10 +198,10 @@
234
235 private Gtk.Grid create_common_settings () {
236 lock_image = new Gtk.Image.from_icon_name ("changes-prevent-symbolic", Gtk.IconSize.BUTTON);
237- lock_image.tooltip_text = no_permission_string;
238+ lock_image.tooltip_text = NO_PERMISSION_STRING;
239 lock_image.sensitive = false;
240 lock_image2 = new Gtk.Image.from_icon_name ("changes-prevent-symbolic", Gtk.IconSize.BUTTON);
241- lock_image2.tooltip_text = no_permission_string;
242+ lock_image2.tooltip_text = NO_PERMISSION_STRING;
243 lock_image2.sensitive = false;
244
245 if (laptop_detect () || battery.laptop) {
246@@ -258,7 +259,7 @@
247 return main_grid;
248 }
249
250- private Gtk.Grid create_notebook_pages (string type) {
251+ private Gtk.Grid create_notebook_pages (bool ac) {
252 var grid = new Gtk.Grid ();
253 grid.column_spacing = 12;
254 grid.row_spacing = 12;
255@@ -267,6 +268,11 @@
256 ((Gtk.Misc) sleep_timeout_label).xalign = 1.0f;
257 label_size.add_widget (sleep_timeout_label);
258
259+ string type = "battery";
260+ if (ac) {
261+ type = "ac";
262+ }
263+
264 var scale_settings = @"sleep-inactive-$type-timeout";
265 var sleep_timeout = new TimeoutComboBox (settings, scale_settings);
266
267@@ -276,7 +282,7 @@
268 var lid_dock_box = new LidCloseActionComboBox (_("When docked and lid is closed:"), cli_communicator);
269 var lid_closed_box = new LidCloseActionComboBox (_("When lid is closed:"), cli_communicator);
270
271- if (type != "ac") {
272+ if (!ac) {
273 var dim_label = new Gtk.Label (_("Dim display when inactive:"));
274 ((Gtk.Misc) dim_label).xalign = 1.0f;
275 label_size.add_widget (dim_label);
276@@ -332,35 +338,30 @@
277 }
278
279 private bool laptop_detect () {
280- string test_laptop_detect = Environment.find_program_in_path ("laptop-detect");
281- if (test_laptop_detect == null &&
282- FileUtils.test ("/usr/sbin/laptop-detect", FileTest.EXISTS) &&
283- FileUtils.test ("/usr/sbin/laptop-detect", FileTest.IS_REGULAR) &&
284- FileUtils.test ("/usr/sbin/laptop-detect", FileTest.IS_EXECUTABLE)) {
285- test_laptop_detect = "/usr/sbin/laptop-detect";
286+ string? laptop_detect_executable = Environment.find_program_in_path ("laptop-detect");
287+ if (laptop_detect_executable == null ||
288+ !FileUtils.test (laptop_detect_executable, FileTest.IS_EXECUTABLE) ||
289+ !FileUtils.test (LAPTOP_DETECT_BINARY, FileTest.IS_EXECUTABLE)) {
290+ warning ("Laptop detect not found");
291+ return false;
292 }
293
294- if (test_laptop_detect != null) {
295- int exit_status;
296- string standard_output, standard_error;
297- try {
298- Process.spawn_command_line_sync (test_laptop_detect, out standard_output,
299- out standard_error, out exit_status);
300- if (exit_status == 0) {
301- debug ("Laptop detect return true");
302- return true;
303- } else {
304- debug ("Laptop detect return false");
305- return false;
306- }
307- } catch (SpawnError err) {
308- warning (err.message);
309- return false;
310+ int exit_status;
311+ string standard_output, standard_error;
312+ try {
313+ Process.spawn_command_line_sync (laptop_detect_executable, out standard_output,
314+ out standard_error, out exit_status);
315+ if (exit_status == 0) {
316+ debug ("Laptop detect returned true");
317+ return true;
318 }
319- } else {
320- warning ("Laptop detect not find");
321+ } catch (SpawnError err) {
322+ warning (err.message);
323 return false;
324 }
325+
326+ debug ("Laptop detect returned false");
327+ return false;
328 }
329
330 private void run_dpms_helper () {
331
332=== modified file 'src/PowerSupply.vala'
333--- src/PowerSupply.vala 2016-05-24 17:21:48 +0000
334+++ src/PowerSupply.vala 2016-06-05 12:08:16 +0000
335@@ -21,9 +21,6 @@
336 public class PowerSupply {
337 private Upower? upower;
338 private UpowerDevice? upower_device;
339-
340- private const string dbus_upower_name = "org.freedesktop.UPower";
341- private const string dbus_upower_root_path = "/org/freedesktop/UPower";
342 private const uint LINE_POWER_TYPE = 1;
343 private string dbus_upower_ac_path;
344
345@@ -31,37 +28,41 @@
346 connect_dbus ();
347 }
348
349-
350 public bool check_present () {
351- bool return_value = false;
352+ bool present = false;
353+ if (upower_device == null) {
354+ return false;
355+ }
356
357 try {
358 upower_device.Refresh ();
359
360 if (upower_device.Online && upower_device.PowerSupply) {
361- return_value = true;
362+ present = true;
363 }
364 } catch (Error e) {
365- warning ("power supply:%s", e.message);
366+ warning ("power supply: %s", e.message);
367 }
368- return return_value;
369+
370+ return present;
371 }
372
373 private void connect_dbus () {
374- try{
375- upower = Bus.get_proxy_sync (BusType.SYSTEM, dbus_upower_name, dbus_upower_root_path, DBusProxyFlags.NONE);
376+ try {
377+ upower = Bus.get_proxy_sync (BusType.SYSTEM, DBUS_UPOWER_NAME, DBUS_UPOWER_PATH, DBusProxyFlags.NONE);
378 get_upower_ac_device (upower);
379 } catch (Error e) {
380 critical ("power supply dbus connection to upower fault");
381 }
382- debug ("power supply path:%s dbus connected", dbus_upower_ac_path);
383+
384+ debug ("power supply path: %s dbus connected", dbus_upower_ac_path);
385 }
386
387 private void get_upower_ac_device (Upower upow) {
388 try {
389 ObjectPath[] devs = upow.EnumerateDevices ();
390 for (int i = 0; i < devs.length; i++) {
391- UpowerDevice dev = Bus.get_proxy_sync (BusType.SYSTEM, dbus_upower_name, devs[i], DBusProxyFlags.GET_INVALIDATED_PROPERTIES);
392+ UpowerDevice dev = Bus.get_proxy_sync (BusType.SYSTEM, DBUS_UPOWER_NAME, devs[i], DBusProxyFlags.GET_INVALIDATED_PROPERTIES);
393 if (dev.Type == LINE_POWER_TYPE) {
394 upower_device = dev;
395 return;

Subscribers

People subscribed via source and target branches

to all changes: