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
=== modified file 'src/Battery.vala'
--- src/Battery.vala 2016-04-30 22:14:22 +0000
+++ src/Battery.vala 2016-06-05 12:08:16 +0000
@@ -18,11 +18,7 @@
18 */18 */
1919
20namespace Power {20namespace Power {
21 public class Battery {21 public class Battery : Object {
22
23 private const string dbus_upower_name = "org.freedesktop.UPower";
24 private const string dbus_upower_root_path = "/org/freedesktop/UPower";
25
26 private Upower? upower;22 private Upower? upower;
27 private UpowerDevice? upower_device;23 private UpowerDevice? upower_device;
2824
@@ -35,38 +31,39 @@
35 }31 }
3632
37 public bool check_present () {33 public bool check_present () {
38 bool return_value = false;34 bool present = false;
39 if (laptop) {35 if (laptop) {
40 if (upower.OnBattery) {36 if (upower.OnBattery || upower_device.IsPresent) {
41 return_value = true;37 present = true;
42 } else if (upower_device.IsPresent) {38 }
43 return_value = true;39 }
44 } 40
45 } 41 return present;
46 return return_value;
47 }42 }
4843
49 private string get_dbus_path(Upower upow) {44 private string get_dbus_path (Upower upow) {
50 string return_value = "";45 string path = "";
51 try {46 try {
52 ObjectPath[] devs = upow.EnumerateDevices();47 ObjectPath[] devs = upow.EnumerateDevices();
53 for (int i =0; i<devs.length; i++) {48 for (int i = 0; i < devs.length; i++) {
54 if (devs[i].contains("BAT0")) {49 if (devs[i].contains ("BAT0")) {
55 return_value = devs[i].to_string();50 path = devs[i].to_string();
51 break;
56 }52 }
57 }53 }
58 } catch (Error e) {54 } catch (Error e) {
59 critical("acpi couldn't get upower devices");55 critical("acpi couldn't get upower devices");
60 }56 }
61 return return_value;57
58 return path;
62 }59 }
6360
64 private void connect_dbus () {61 private void connect_dbus () {
65 try {62 try {
66 upower = Bus.get_proxy_sync (BusType.SYSTEM, dbus_upower_name, dbus_upower_root_path, DBusProxyFlags.NONE);63 upower = Bus.get_proxy_sync (BusType.SYSTEM, DBUS_UPOWER_NAME, DBUS_UPOWER_PATH, DBusProxyFlags.NONE);
67 dbus_upower_battery_path = get_dbus_path(upower);64 dbus_upower_battery_path = get_dbus_path(upower);
68 if (dbus_upower_battery_path != "" && dbus_upower_battery_path != null) {65 if (dbus_upower_battery_path != "" && dbus_upower_battery_path != null) {
69 upower_device = Bus.get_proxy_sync (BusType.SYSTEM, dbus_upower_name, dbus_upower_battery_path, DBusProxyFlags.GET_INVALIDATED_PROPERTIES);66 upower_device = Bus.get_proxy_sync (BusType.SYSTEM, DBUS_UPOWER_NAME, dbus_upower_battery_path, DBusProxyFlags.GET_INVALIDATED_PROPERTIES);
7067
71 laptop = true;68 laptop = true;
72 debug ("battery path:%s , its a laptop, dbus connected", dbus_upower_battery_path);69 debug ("battery path:%s , its a laptop, dbus connected", dbus_upower_battery_path);
7370
=== modified file 'src/CliCommunicator.vala'
--- src/CliCommunicator.vala 2016-04-30 17:36:20 +0000
+++ src/CliCommunicator.vala 2016-06-05 12:08:16 +0000
@@ -40,51 +40,53 @@
40 }40 }
4141
42 private string action_to_string (Action value) {42 private string action_to_string (Action value) {
43 string return_val = "NOT_SUPPORTED";43 string str = "NOT_SUPPORTED";
44 switch (value) {44 switch (value) {
45 case Action.IGNORE:45 case Action.IGNORE:
46 return_val = "ignore";46 str = "ignore";
47 break;47 break;
48 case Action.POWEROFF:48 case Action.POWEROFF:
49 return_val = "poweroff";49 str = "poweroff";
50 break;50 break;
51 case Action.LOCK:51 case Action.LOCK:
52 return_val = "lock";52 str = "lock";
53 break;53 break;
54 case Action.SUSPEND:54 case Action.SUSPEND:
55 return_val = "suspend";55 str = "suspend";
56 break;56 break;
57 case Action.HALT:57 case Action.HALT:
58 return_val = "halt";58 str = "halt";
59 break;59 break;
60 default:60 default:
61 break;61 break;
62 }62 }
63 return return_val;63
64 return str;
64 }65 }
6566
66 private Action string_to_action (string value) {67 private Action string_to_action (string value) {
67 Action return_val = Action.NOT_SUPPORTED;68 Action action = Action.NOT_SUPPORTED;
68 switch (value) {69 switch (value) {
69 case "ignore":70 case "ignore":
70 return_val = Action.IGNORE;71 action = Action.IGNORE;
71 break;72 break;
72 case "poweroff":73 case "poweroff":
73 return_val = Action.POWEROFF;74 action = Action.POWEROFF;
74 break;75 break;
75 case "lock":76 case "lock":
76 return_val = Action.LOCK;77 action = Action.LOCK;
77 break;78 break;
78 case "suspend":79 case "suspend":
79 return_val = Action.SUSPEND;80 action = Action.SUSPEND;
80 break;81 break;
81 case "halt":82 case "halt":
82 return_val = Action.HALT;83 action = Action.HALT;
83 break;84 break;
84 default:85 default:
85 break;86 break;
86 }87 }
87 return return_val;88
89 return action;
88 }90 }
8991
90 private void seperate_string (string value) {92 private void seperate_string (string value) {
9193
=== modified file 'src/Interfaces.vala'
--- src/Interfaces.vala 2016-05-24 17:21:48 +0000
+++ src/Interfaces.vala 2016-06-05 12:08:16 +0000
@@ -18,6 +18,8 @@
18 */18 */
1919
20namespace Power {20namespace Power {
21 public const string DBUS_UPOWER_NAME = "org.freedesktop.UPower";
22 public const string DBUS_UPOWER_PATH = "/org/freedesktop/UPower";
2123
22 [DBus (name = "org.gnome.SettingsDaemon.Power.Screen")]24 [DBus (name = "org.gnome.SettingsDaemon.Power.Screen")]
23 interface PowerSettings : GLib.Object {25 interface PowerSettings : GLib.Object {
2426
=== modified file 'src/Plug.vala'
--- src/Plug.vala 2016-05-01 00:04:56 +0000
+++ src/Plug.vala 2016-06-05 12:08:16 +0000
@@ -24,7 +24,6 @@
24 Gtk.Grid main_grid;24 Gtk.Grid main_grid;
2525
26 public class Plug : Switchboard.Plug {26 public class Plug : Switchboard.Plug {
27
28 private Gtk.SizeGroup label_size;27 private Gtk.SizeGroup label_size;
29 private Gtk.StackSwitcher stack_switcher;28 private Gtk.StackSwitcher stack_switcher;
30 private GLib.Settings pantheon_dpms_settings;29 private GLib.Settings pantheon_dpms_settings;
@@ -36,7 +35,8 @@
36 private Gtk.Image lock_image;35 private Gtk.Image lock_image;
37 private Gtk.Image lock_image2;36 private Gtk.Image lock_image2;
3837
39 private const string no_permission_string = _("You do not have permission to change this");38 private const string NO_PERMISSION_STRING = _("You do not have permission to change this");
39 private const string LAPTOP_DETECT_BINARY = "/usr/sbin/laptop-detect";
4040
41 public Plug () {41 public Plug () {
42 Object (category: Category.HARDWARE,42 Object (category: Category.HARDWARE,
@@ -62,10 +62,10 @@
62 }62 }
6363
64 public override void shown () {64 public override void shown () {
65 if (power_supply.check_present ()) {65 if (battery.check_present ()) {
66 stack_switcher.get_stack ().visible_child_name = "battery";
67 } else {
66 stack_switcher.get_stack ().visible_child_name = "ac";68 stack_switcher.get_stack ().visible_child_name = "ac";
67 } else {
68 stack_switcher.get_stack ().visible_child_name = "battery";
69 }69 }
70 }70 }
7171
@@ -99,13 +99,16 @@
9999
100 Gtk.Stack stack = new Gtk.Stack ();100 Gtk.Stack stack = new Gtk.Stack ();
101101
102 Gtk.Grid plug_grid = create_notebook_pages ("ac");102 Gtk.Grid plug_grid = create_notebook_pages (true);
103 stack.add_titled (plug_grid, "ac", _("Plugged In"));103 stack.add_titled (plug_grid, "ac", _("Plugged In"));
104104
105 stack_container.add (info_bars);105 stack_container.add (info_bars);
106106
107 stack_switcher = new Gtk.StackSwitcher ();
108 stack_switcher.stack = stack;
109
107 if (laptop_detect () || battery.laptop) {110 if (laptop_detect () || battery.laptop) {
108 Gtk.Grid battery_grid = create_notebook_pages ("battery");111 Gtk.Grid battery_grid = create_notebook_pages (false);
109 stack.add_titled (battery_grid, "battery", _("On Battery"));112 stack.add_titled (battery_grid, "battery", _("On Battery"));
110113
111 var left_sep = new Gtk.Separator (Gtk.Orientation.HORIZONTAL);114 var left_sep = new Gtk.Separator (Gtk.Orientation.HORIZONTAL);
@@ -114,9 +117,6 @@
114 var right_sep = new Gtk.Separator (Gtk.Orientation.HORIZONTAL);117 var right_sep = new Gtk.Separator (Gtk.Orientation.HORIZONTAL);
115 right_sep.hexpand = true;118 right_sep.hexpand = true;
116119
117 stack_switcher = new Gtk.StackSwitcher ();
118 stack_switcher.stack = stack;
119
120 var switcher_grid = new Gtk.Grid ();120 var switcher_grid = new Gtk.Grid ();
121 switcher_grid.margin_top = 24;121 switcher_grid.margin_top = 24;
122 switcher_grid.margin_bottom = 12;122 switcher_grid.margin_bottom = 12;
@@ -132,7 +132,8 @@
132132
133 stack_container.margin_bottom = 12;133 stack_container.margin_bottom = 12;
134 stack_container.show_all ();134 stack_container.show_all ();
135 // hide stack switcher we only have ac line135
136 // hide stack switcher if we only have ac line
136 stack_switcher.visible = stack.get_children ().length () > 1;137 stack_switcher.visible = stack.get_children ().length () > 1;
137 }138 }
138139
@@ -197,10 +198,10 @@
197198
198 private Gtk.Grid create_common_settings () {199 private Gtk.Grid create_common_settings () {
199 lock_image = new Gtk.Image.from_icon_name ("changes-prevent-symbolic", Gtk.IconSize.BUTTON);200 lock_image = new Gtk.Image.from_icon_name ("changes-prevent-symbolic", Gtk.IconSize.BUTTON);
200 lock_image.tooltip_text = no_permission_string;201 lock_image.tooltip_text = NO_PERMISSION_STRING;
201 lock_image.sensitive = false;202 lock_image.sensitive = false;
202 lock_image2 = new Gtk.Image.from_icon_name ("changes-prevent-symbolic", Gtk.IconSize.BUTTON);203 lock_image2 = new Gtk.Image.from_icon_name ("changes-prevent-symbolic", Gtk.IconSize.BUTTON);
203 lock_image2.tooltip_text = no_permission_string;204 lock_image2.tooltip_text = NO_PERMISSION_STRING;
204 lock_image2.sensitive = false;205 lock_image2.sensitive = false;
205206
206 if (laptop_detect () || battery.laptop) {207 if (laptop_detect () || battery.laptop) {
@@ -258,7 +259,7 @@
258 return main_grid;259 return main_grid;
259 }260 }
260261
261 private Gtk.Grid create_notebook_pages (string type) {262 private Gtk.Grid create_notebook_pages (bool ac) {
262 var grid = new Gtk.Grid ();263 var grid = new Gtk.Grid ();
263 grid.column_spacing = 12;264 grid.column_spacing = 12;
264 grid.row_spacing = 12;265 grid.row_spacing = 12;
@@ -267,6 +268,11 @@
267 ((Gtk.Misc) sleep_timeout_label).xalign = 1.0f;268 ((Gtk.Misc) sleep_timeout_label).xalign = 1.0f;
268 label_size.add_widget (sleep_timeout_label);269 label_size.add_widget (sleep_timeout_label);
269270
271 string type = "battery";
272 if (ac) {
273 type = "ac";
274 }
275
270 var scale_settings = @"sleep-inactive-$type-timeout";276 var scale_settings = @"sleep-inactive-$type-timeout";
271 var sleep_timeout = new TimeoutComboBox (settings, scale_settings);277 var sleep_timeout = new TimeoutComboBox (settings, scale_settings);
272278
@@ -276,7 +282,7 @@
276 var lid_dock_box = new LidCloseActionComboBox (_("When docked and lid is closed:"), cli_communicator);282 var lid_dock_box = new LidCloseActionComboBox (_("When docked and lid is closed:"), cli_communicator);
277 var lid_closed_box = new LidCloseActionComboBox (_("When lid is closed:"), cli_communicator);283 var lid_closed_box = new LidCloseActionComboBox (_("When lid is closed:"), cli_communicator);
278284
279 if (type != "ac") {285 if (!ac) {
280 var dim_label = new Gtk.Label (_("Dim display when inactive:"));286 var dim_label = new Gtk.Label (_("Dim display when inactive:"));
281 ((Gtk.Misc) dim_label).xalign = 1.0f;287 ((Gtk.Misc) dim_label).xalign = 1.0f;
282 label_size.add_widget (dim_label);288 label_size.add_widget (dim_label);
@@ -332,35 +338,30 @@
332 }338 }
333339
334 private bool laptop_detect () {340 private bool laptop_detect () {
335 string test_laptop_detect = Environment.find_program_in_path ("laptop-detect");341 string? laptop_detect_executable = Environment.find_program_in_path ("laptop-detect");
336 if (test_laptop_detect == null && 342 if (laptop_detect_executable == null ||
337 FileUtils.test ("/usr/sbin/laptop-detect", FileTest.EXISTS) &&343 !FileUtils.test (laptop_detect_executable, FileTest.IS_EXECUTABLE) ||
338 FileUtils.test ("/usr/sbin/laptop-detect", FileTest.IS_REGULAR) &&344 !FileUtils.test (LAPTOP_DETECT_BINARY, FileTest.IS_EXECUTABLE)) {
339 FileUtils.test ("/usr/sbin/laptop-detect", FileTest.IS_EXECUTABLE)) {345 warning ("Laptop detect not found");
340 test_laptop_detect = "/usr/sbin/laptop-detect";346 return false;
341 }347 }
342348
343 if (test_laptop_detect != null) {349 int exit_status;
344 int exit_status;350 string standard_output, standard_error;
345 string standard_output, standard_error;351 try {
346 try {352 Process.spawn_command_line_sync (laptop_detect_executable, out standard_output,
347 Process.spawn_command_line_sync (test_laptop_detect, out standard_output,353 out standard_error, out exit_status);
348 out standard_error, out exit_status);354 if (exit_status == 0) {
349 if (exit_status == 0) {355 debug ("Laptop detect returned true");
350 debug ("Laptop detect return true");356 return true;
351 return true;
352 } else {
353 debug ("Laptop detect return false");
354 return false;
355 }
356 } catch (SpawnError err) {
357 warning (err.message);
358 return false;
359 }357 }
360 } else {358 } catch (SpawnError err) {
361 warning ("Laptop detect not find");359 warning (err.message);
362 return false;360 return false;
363 }361 }
362
363 debug ("Laptop detect returned false");
364 return false;
364 }365 }
365366
366 private void run_dpms_helper () {367 private void run_dpms_helper () {
367368
=== modified file 'src/PowerSupply.vala'
--- src/PowerSupply.vala 2016-05-24 17:21:48 +0000
+++ src/PowerSupply.vala 2016-06-05 12:08:16 +0000
@@ -21,9 +21,6 @@
21 public class PowerSupply {21 public class PowerSupply {
22 private Upower? upower;22 private Upower? upower;
23 private UpowerDevice? upower_device;23 private UpowerDevice? upower_device;
24
25 private const string dbus_upower_name = "org.freedesktop.UPower";
26 private const string dbus_upower_root_path = "/org/freedesktop/UPower";
27 private const uint LINE_POWER_TYPE = 1;24 private const uint LINE_POWER_TYPE = 1;
28 private string dbus_upower_ac_path;25 private string dbus_upower_ac_path;
29 26
@@ -31,37 +28,41 @@
31 connect_dbus ();28 connect_dbus ();
32 }29 }
3330
34
35 public bool check_present () {31 public bool check_present () {
36 bool return_value = false;32 bool present = false;
33 if (upower_device == null) {
34 return false;
35 }
3736
38 try {37 try {
39 upower_device.Refresh ();38 upower_device.Refresh ();
4039
41 if (upower_device.Online && upower_device.PowerSupply) {40 if (upower_device.Online && upower_device.PowerSupply) {
42 return_value = true;41 present = true;
43 }42 }
44 } catch (Error e) {43 } catch (Error e) {
45 warning ("power supply:%s", e.message);44 warning ("power supply: %s", e.message);
46 }45 }
47 return return_value;46
47 return present;
48 }48 }
4949
50 private void connect_dbus () {50 private void connect_dbus () {
51 try{51 try {
52 upower = Bus.get_proxy_sync (BusType.SYSTEM, dbus_upower_name, dbus_upower_root_path, DBusProxyFlags.NONE);52 upower = Bus.get_proxy_sync (BusType.SYSTEM, DBUS_UPOWER_NAME, DBUS_UPOWER_PATH, DBusProxyFlags.NONE);
53 get_upower_ac_device (upower);53 get_upower_ac_device (upower);
54 } catch (Error e) {54 } catch (Error e) {
55 critical ("power supply dbus connection to upower fault");55 critical ("power supply dbus connection to upower fault");
56 }56 }
57 debug ("power supply path:%s dbus connected", dbus_upower_ac_path);57
58 debug ("power supply path: %s dbus connected", dbus_upower_ac_path);
58 }59 }
5960
60 private void get_upower_ac_device (Upower upow) {61 private void get_upower_ac_device (Upower upow) {
61 try {62 try {
62 ObjectPath[] devs = upow.EnumerateDevices ();63 ObjectPath[] devs = upow.EnumerateDevices ();
63 for (int i = 0; i < devs.length; i++) {64 for (int i = 0; i < devs.length; i++) {
64 UpowerDevice dev = Bus.get_proxy_sync (BusType.SYSTEM, dbus_upower_name, devs[i], DBusProxyFlags.GET_INVALIDATED_PROPERTIES);65 UpowerDevice dev = Bus.get_proxy_sync (BusType.SYSTEM, DBUS_UPOWER_NAME, devs[i], DBusProxyFlags.GET_INVALIDATED_PROPERTIES);
65 if (dev.Type == LINE_POWER_TYPE) {66 if (dev.Type == LINE_POWER_TYPE) {
66 upower_device = dev;67 upower_device = dev;
67 return;68 return;

Subscribers

People subscribed via source and target branches

to all changes: