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