Merge lp:~kay20/switchboard-plug-power/clean-up into lp:~elementary-apps/switchboard-plug-power/trunk
- clean-up
- Merge into trunk
Proposed by
kay van der Zander
Status: | Merged | ||||
---|---|---|---|---|---|
Approved by: | xapantu | ||||
Approved revision: | 253 | ||||
Merged at revision: | 253 | ||||
Proposed branch: | lp:~kay20/switchboard-plug-power/clean-up | ||||
Merge into: | lp:~elementary-apps/switchboard-plug-power/trunk | ||||
Diff against target: |
945 lines (+486/-435) 3 files modified
src/ActionComboBox.vala (+59/-40) src/Plug.vala (+344/-331) src/TimeoutComboBox.vala (+83/-64) |
||||
To merge this branch: | bzr merge lp:~kay20/switchboard-plug-power/clean-up | ||||
Related bugs: |
|
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
xapantu (community) | Approve | ||
kay van der Zander (community) | Approve | ||
Review via email: mp+268876@code.launchpad.net |
Commit message
Applied new code style.
Cleaned up the code by appling the new code style.
Removed all 'this.' use.
Added copy right header.
Description of the change
this branch cleaned the code.
- removed all 'this.' use because it is not needed.
- changed tabs to spaces.
- added copy right header
To post a comment you must log in.
Revision history for this message
Marcus Wichelmann (l-admin-3) wrote : | # |
- 253. By kay van der Zander
-
clean up code. following the new code style
Revision history for this message
kay van der Zander (kay20) wrote : | # |
tested the branch local all what worked before the clean up does work after the clean up.
review:
Approve
Revision history for this message
xapantu (xapantu) wrote : | # |
Looks fine, I did not test every feature but there does not seem to be anything else than coding style here.
Thanks :)
review:
Approve
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'src/ActionComboBox.vala' |
2 | --- src/ActionComboBox.vala 2015-06-24 21:21:36 +0000 |
3 | +++ src/ActionComboBox.vala 2015-08-24 17:16:29 +0000 |
4 | @@ -1,41 +1,60 @@ |
5 | +/* |
6 | + * Copyright (C) 2015 Pantheon Developers (http://launchpad.net/switchboard-plug-power) |
7 | + * |
8 | + * This program or library is free software; you can redistribute it |
9 | + * and/or modify it under the terms of the GNU Lesser General Public |
10 | + * License as published by the Free Software Foundation; either |
11 | + * version 3 of the License, or (at your option) any later version. |
12 | + * |
13 | + * This library is distributed in the hope that it will be useful, |
14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 | + * Lesser General Public License for more details. |
17 | + * |
18 | + * You should have received a copy of the GNU Lesser General |
19 | + * Public License along with this library; if not, write to the |
20 | + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
21 | + * Boston, MA 02110-1301 USA. |
22 | + */ |
23 | + |
24 | namespace Power { |
25 | - class ActionComboBox : Gtk.ComboBoxText { |
26 | - |
27 | - public Gtk.Label label; |
28 | - private string key; |
29 | - |
30 | - // this maps combobox indices to gsettings enums |
31 | - private int[] map_to_sett = {1, 2, 3, 4, 5}; |
32 | - // and vice-versa |
33 | - private int[] map_to_list = {4, 0, 1, 2, 3, 4}; |
34 | - |
35 | - public ActionComboBox (string label, string key) { |
36 | - this.key = key; |
37 | - this.label = new Gtk.Label (label); |
38 | - this.label.halign = Gtk.Align.END; |
39 | - ((Gtk.Misc) this.label).xalign = 1.0f; |
40 | - |
41 | - this.append_text (_("Suspend")); |
42 | - this.append_text (_("Shutdown")); |
43 | - this.append_text (_("Hibernate")); |
44 | - this.append_text (_("Ask me")); |
45 | - this.append_text (_("Do nothing")); |
46 | - |
47 | - this.hexpand = true; |
48 | - |
49 | - update_combo (); |
50 | - |
51 | - this.changed.connect (update_settings); |
52 | - settings.changed[key].connect (update_combo); |
53 | - } |
54 | - |
55 | - private void update_settings () { |
56 | - settings.set_enum (key, map_to_sett[active]); |
57 | - } |
58 | - |
59 | - private void update_combo () { |
60 | - int val = settings.get_enum (key); |
61 | - active = map_to_list [val]; |
62 | - } |
63 | - } |
64 | -} |
65 | + class ActionComboBox : Gtk.ComboBoxText { |
66 | + |
67 | + public Gtk.Label label; |
68 | + private string key; |
69 | + |
70 | + // this maps combobox indices to gsettings enums |
71 | + private int[] map_to_sett = {1, 2, 3, 4, 5}; |
72 | + // and vice-versa |
73 | + private int[] map_to_list = {4, 0, 1, 2, 3, 4}; |
74 | + |
75 | + public ActionComboBox (string label_name, string key_value) { |
76 | + key = key_value; |
77 | + label = new Gtk.Label (label_name); |
78 | + label.halign = Gtk.Align.END; |
79 | + ((Gtk.Misc) label).xalign = 1.0f; |
80 | + |
81 | + append_text (_("Suspend")); |
82 | + append_text (_("Shutdown")); |
83 | + append_text (_("Hibernate")); |
84 | + append_text (_("Ask me")); |
85 | + append_text (_("Do nothing")); |
86 | + |
87 | + hexpand = true; |
88 | + |
89 | + update_combo (); |
90 | + |
91 | + changed.connect (update_settings); |
92 | + settings.changed[key].connect (update_combo); |
93 | + } |
94 | + |
95 | + private void update_settings () { |
96 | + settings.set_enum (key, map_to_sett[active]); |
97 | + } |
98 | + |
99 | + private void update_combo () { |
100 | + int val = settings.get_enum (key); |
101 | + active = map_to_list [val]; |
102 | + } |
103 | + } |
104 | +} |
105 | \ No newline at end of file |
106 | |
107 | === modified file 'src/Plug.vala' |
108 | --- src/Plug.vala 2015-08-20 11:02:25 +0000 |
109 | +++ src/Plug.vala 2015-08-24 17:16:29 +0000 |
110 | @@ -1,336 +1,349 @@ |
111 | +/* |
112 | + * Copyright (C) 2015 Pantheon Developers (http://launchpad.net/switchboard-plug-power) |
113 | + * |
114 | + * This program or library is free software; you can redistribute it |
115 | + * and/or modify it under the terms of the GNU Lesser General Public |
116 | + * License as published by the Free Software Foundation; either |
117 | + * version 3 of the License, or (at your option) any later version. |
118 | + * |
119 | + * This library is distributed in the hope that it will be useful, |
120 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
121 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
122 | + * Lesser General Public License for more details. |
123 | + * |
124 | + * You should have received a copy of the GNU Lesser General |
125 | + * Public License along with this library; if not, write to the |
126 | + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
127 | + * Boston, MA 02110-1301 USA. |
128 | + */ |
129 | + |
130 | namespace Power { |
131 | |
132 | - GLib.Settings settings; |
133 | - Gtk.Box stack_container; |
134 | - |
135 | - [DBus (name = "org.gnome.SettingsDaemon.Power.Screen")] |
136 | - interface PowerSettings : GLib.Object { |
137 | -#if OLD_GSD |
138 | - public abstract uint GetPercentage () throws IOError; |
139 | - public abstract uint SetPercentage (uint percentage) throws IOError; |
140 | -#else |
141 | - // use the Brightness property after updateing g-s-d to 3.10 or above |
142 | - public abstract int Brightness {get; set; } |
143 | -#endif |
144 | - } |
145 | - |
146 | - [DBus (name = "org.freedesktop.UPower")] |
147 | - interface UPowerSettings : GLib.Object { |
148 | - public abstract string[] EnumerateDevices () throws IOError; |
149 | - } |
150 | - |
151 | - [DBus (name = "org.freedesktop.UPower.Device")] |
152 | - interface UPowerDevice : GLib.Object { |
153 | - public abstract uint Type {get; set;} |
154 | - } |
155 | - |
156 | - |
157 | - public class Plug : Switchboard.Plug { |
158 | - |
159 | - private PowerSettings screen; |
160 | - private UPowerSettings upower; |
161 | - private Gtk.SizeGroup label_size; |
162 | - private GLib.Settings pantheon_dpms_settings; |
163 | - |
164 | - public Plug () { |
165 | - Object (category: Category.HARDWARE, |
166 | - code_name: "system-pantheon-power", |
167 | - display_name: _("Power"), |
168 | - description: _("Set display brightness, power button behavior, and sleep preferences"), |
169 | - icon: "preferences-system-power"); |
170 | - |
171 | - settings = new GLib.Settings ("org.gnome.settings-daemon.plugins.power"); |
172 | - pantheon_dpms_settings = new GLib.Settings ("org.pantheon.dpms"); |
173 | - try { |
174 | - screen = Bus.get_proxy_sync (BusType.SESSION, |
175 | - "org.gnome.SettingsDaemon", |
176 | - "/org/gnome/SettingsDaemon/Power"); |
177 | - } catch (IOError e) { |
178 | - warning ("Failed to get settings daemon for brightness setting"); |
179 | - } |
180 | - |
181 | - try { |
182 | - upower = Bus.get_proxy_sync (BusType.SYSTEM, |
183 | - "org.freedesktop.UPower", |
184 | - "/org/freedesktop/UPower"); |
185 | - } catch (IOError e) { |
186 | - warning ("Failed to get settings daemon for brightness setting"); |
187 | - } |
188 | - |
189 | - } |
190 | - |
191 | - public override Gtk.Widget get_widget () { |
192 | - if (stack_container == null) { |
193 | - //setup_info (); |
194 | - setup_ui (); |
195 | - } |
196 | - return stack_container; |
197 | - } |
198 | - |
199 | - public override void shown () { |
200 | - |
201 | - } |
202 | - |
203 | - public override void hidden () { |
204 | - |
205 | - } |
206 | - |
207 | - public override void search_callback (string location) { |
208 | - |
209 | - } |
210 | - |
211 | - // 'search' returns results like ("Keyboard → Behavior → Duration", "keyboard<sep>behavior") |
212 | - public override async Gee.TreeMap<string, string> search (string search) { |
213 | - return new Gee.TreeMap<string, string> (null, null); |
214 | - } |
215 | - |
216 | - void setup_ui () { |
217 | - stack_container = new Gtk.Box (Gtk.Orientation.VERTICAL, 0); |
218 | - label_size = new Gtk.SizeGroup (Gtk.SizeGroupMode.HORIZONTAL); |
219 | - |
220 | - var separator = new Gtk.Separator (Gtk.Orientation.HORIZONTAL); |
221 | - separator.vexpand = true; |
222 | - separator.valign = Gtk.Align.START; |
223 | - |
224 | - var common_settings = create_common_settings (); |
225 | - var stack = new Gtk.Stack (); |
226 | - var stack_switcher = new Gtk.StackSwitcher (); |
227 | - stack_switcher.halign = Gtk.Align.CENTER; |
228 | - stack_switcher.stack = stack; |
229 | - |
230 | - var plug_grid = create_notebook_pages ("ac"); |
231 | - stack.add_titled (plug_grid, "ac", _("Plugged In")); |
232 | - |
233 | - if (laptop_detect () || have_ups ()) { // when its not laptop, we check for ups |
234 | - var battery_grid = create_notebook_pages ("battery"); |
235 | - stack.add_titled (battery_grid, "battery", _("On Battery")); |
236 | - } |
237 | - |
238 | - stack_container.pack_start (common_settings); |
239 | - stack_container.pack_start (separator); |
240 | - stack_container.pack_start(stack_switcher, false, false, 0); |
241 | - stack_container.pack_start(stack, true, true, 0); |
242 | - stack_container.margin = 12; |
243 | - stack_container.show_all (); |
244 | - |
245 | - // hide stack switcher we only have ac line |
246 | - stack_switcher.set_visible (stack.get_children ().length () > 1); |
247 | - separator.set_visible (stack.get_children ().length () > 1); |
248 | - } |
249 | - |
250 | - private Gtk.Grid create_common_settings () { |
251 | - var grid = new Gtk.Grid (); |
252 | - grid.margin = 12; |
253 | - grid.column_spacing = 12; |
254 | - grid.row_spacing = 12; |
255 | - |
256 | - var brightness_label = new Gtk.Label (_("Display brightness:")); |
257 | - ((Gtk.Misc) brightness_label).xalign = 1.0f; |
258 | - label_size.add_widget (brightness_label); |
259 | - brightness_label.halign = Gtk.Align.END; |
260 | - |
261 | - var scale = new Gtk.Scale.with_range (Gtk.Orientation.HORIZONTAL, 0, 100, 10); |
262 | - scale.set_draw_value (false); |
263 | - scale.hexpand = true; |
264 | - scale.width_request = 480; |
265 | - |
266 | - var dim_label = new Gtk.Label (_("Dim screen when inactive:")); |
267 | - ((Gtk.Misc) dim_label).xalign = 1.0f; |
268 | - var dim_switch = new Gtk.Switch (); |
269 | - dim_switch.halign = Gtk.Align.START; |
270 | - |
271 | - settings.bind ("idle-dim", dim_switch, "active", SettingsBindFlags.DEFAULT); |
272 | - |
273 | - // Checking if device is a laptop or desktop. If it's a desktop, hide the brightness scale |
274 | - // Prevents plug freezing on desktops. |
275 | - bool show_brightness = false; |
276 | - try { |
277 | - var power_devices = upower.EnumerateDevices (); |
278 | - foreach (string device in power_devices) { |
279 | - if (device.contains ("battery")) { |
280 | - show_brightness = true; |
281 | - } |
282 | - } |
283 | - |
284 | - if (show_brightness) { |
285 | -#if OLD_GSD |
286 | - scale.set_value (screen.GetPercentage ()); |
287 | -#else |
288 | - scale.set_value (screen.Brightness); |
289 | -#endif |
290 | - } else { |
291 | - warning ("No battery found, hiding brightness settings"); |
292 | - } |
293 | - |
294 | - } catch (Error e) { |
295 | - warning ("Brightness setter not available, hiding brightness settings"); |
296 | - show_brightness = false; |
297 | - } |
298 | - |
299 | - if (show_brightness == false) { |
300 | - brightness_label.no_show_all = true; |
301 | - scale.no_show_all = true; |
302 | - dim_label.no_show_all = true; |
303 | - dim_switch.no_show_all = true; |
304 | - } |
305 | - |
306 | - scale.value_changed.connect (() => { |
307 | - var val = (int) scale.get_value (); |
308 | - try { |
309 | -#if OLD_GSD |
310 | - screen.SetPercentage (val); |
311 | -#else |
312 | - screen.Brightness = val; |
313 | -#endif |
314 | - } catch (IOError ioe) { |
315 | - // ignore, because if we have GetPercentage, we have SetPercentage |
316 | - // otherwise the scale won't be visible to change |
317 | - } |
318 | - }); |
319 | - |
320 | - grid.attach (brightness_label, 0, 1, 1, 1); |
321 | - grid.attach (scale, 1, 1, 1, 1); |
322 | - |
323 | - grid.attach (dim_label, 0, 2, 1, 1); |
324 | - grid.attach (dim_switch, 1, 2, 1, 1); |
325 | - |
326 | - string[] labels = {_("Sleep button:"), _("Suspend button:"), _("Hibernate button:"), _("Power button:")}; |
327 | - string[] keys = {"button-sleep", "button-suspend", "button-hibernate", "button-power"}; |
328 | - |
329 | - for (int i = 0; i < labels.length; i++) { |
330 | - var box = new ActionComboBox (labels[i], keys[i]); |
331 | - grid.attach (box.label, 0, i+3, 1, 1); |
332 | - label_size.add_widget (box.label); |
333 | - grid.attach (box, 1, i+3, 1, 1); |
334 | - } |
335 | - |
336 | - var screen_timeout_label = new Gtk.Label (_("Turn off screen when inactive after:")); |
337 | - label_size.add_widget (screen_timeout_label); |
338 | - ((Gtk.Misc) screen_timeout_label).xalign = 1.0f; |
339 | - var screen_timeout = new TimeoutComboBox (pantheon_dpms_settings, "standby-time"); |
340 | - screen_timeout.changed.connect (run_dpms_helper); |
341 | - |
342 | - grid.attach (screen_timeout_label, 0, labels.length+3, 1, 1); |
343 | - grid.attach (screen_timeout, 1, labels.length+3, 1, 1); |
344 | - |
345 | - return grid; |
346 | - } |
347 | - |
348 | - private Gtk.Grid create_notebook_pages (string type) { |
349 | - var grid = new Gtk.Grid (); |
350 | - grid.margin = 12; |
351 | - grid.column_spacing = 12; |
352 | - grid.row_spacing = 12; |
353 | - |
354 | - var sleep_timeout_label = new Gtk.Label (_("Sleep when inactive after:")); |
355 | - ((Gtk.Misc) sleep_timeout_label).xalign = 1.0f; |
356 | - label_size.add_widget (sleep_timeout_label); |
357 | - |
358 | - var scale_settings = @"sleep-inactive-$type-timeout"; |
359 | - var sleep_timeout = new TimeoutComboBox (settings, scale_settings); |
360 | - |
361 | - grid.attach (sleep_timeout_label, 0, 0, 1, 1); |
362 | - grid.attach (sleep_timeout, 1, 0, 1, 1); |
363 | - |
364 | - if (type != "ac") { |
365 | - var critical_box = new ActionComboBox (_("When power is critically low:"), "critical-battery-action"); |
366 | - grid.attach (critical_box.label, 0, 2, 1, 1); |
367 | - label_size.add_widget (critical_box.label); |
368 | - grid.attach (critical_box, 1, 2, 1, 1); |
369 | - } |
370 | - |
371 | - return grid; |
372 | - } |
373 | - |
374 | - private bool laptop_detect () { |
375 | - string test_laptop_detect = Environment.find_program_in_path ("laptop-detect"); |
376 | - if (test_laptop_detect == null && |
377 | - FileUtils.test ("/usr/sbin/laptop-detect", FileTest.EXISTS) && |
378 | - FileUtils.test ("/usr/sbin/laptop-detect", FileTest.IS_REGULAR) && |
379 | - FileUtils.test ("/usr/sbin/laptop-detect", FileTest.IS_EXECUTABLE)) { |
380 | - test_laptop_detect = "/usr/sbin/laptop-detect"; |
381 | - } |
382 | - if (test_laptop_detect != null) { |
383 | - int exit_status; |
384 | - string standard_output, standard_error; |
385 | - try { |
386 | - Process.spawn_command_line_sync (test_laptop_detect, out standard_output, |
387 | - out standard_error, |
388 | - out exit_status); |
389 | - if (exit_status == 0) { |
390 | - debug ("Laptop detect return true"); |
391 | - return true; |
392 | - } else { |
393 | - debug ("Laptop detect return false"); |
394 | - return false; |
395 | - } |
396 | - } |
397 | - catch (SpawnError err) { |
398 | - warning (err.message); |
399 | - return false; |
400 | - } |
401 | - } else { |
402 | - warning ("Laptop detect not find"); |
403 | - /* TODO check upower, and /proc files as laptop-detect does to find batteries */ |
404 | - return false; |
405 | - } |
406 | - } |
407 | - |
408 | - private void run_dpms_helper () { |
409 | - Settings.sync (); |
410 | - |
411 | - try { |
412 | - Process.spawn_async (null, { "elementary-dpms-helper" }, Environ.get (), |
413 | - SpawnFlags.SEARCH_PATH|SpawnFlags.STDERR_TO_DEV_NULL|SpawnFlags.STDOUT_TO_DEV_NULL, |
414 | - null, null); |
415 | - } catch (SpawnError e) { |
416 | - warning ("Failed to reset dpms settings: %s", e.message); |
417 | - } |
418 | - } |
419 | - |
420 | - enum Type { |
421 | - UNKNOWN = 0, |
422 | - LINE_POWER, |
423 | - BATTERY, |
424 | - UPS, |
425 | - MONITOR, |
426 | - MOUSE, |
427 | - KEYBOARD, |
428 | - PDA |
429 | - } |
430 | - |
431 | - private bool have_ups () { |
432 | - /* TODO:check for ups using upower */ |
433 | - string[] devices; |
434 | - |
435 | - try { |
436 | - devices = upower.EnumerateDevices (); |
437 | - } catch (Error e) { |
438 | - message (e.message); |
439 | - return false; |
440 | - } |
441 | - |
442 | - foreach (var device in devices) { |
443 | - try { |
444 | - UPowerDevice upower_device = Bus.get_proxy_sync (BusType.SYSTEM, |
445 | - "org.freedesktop.UPower", |
446 | - device); |
447 | - if (upower_device.Type == Type.UPS) { |
448 | - debug ("found UPS in %s", device); |
449 | - return true; |
450 | - } |
451 | - } catch (Error e) { |
452 | - debug (e.message); |
453 | - } |
454 | - } |
455 | - |
456 | - return false; |
457 | - } |
458 | - } |
459 | + GLib.Settings settings; |
460 | + Gtk.Box stack_container; |
461 | + |
462 | + [DBus (name = "org.gnome.SettingsDaemon.Power.Screen")] |
463 | + interface PowerSettings : GLib.Object { |
464 | +#if OLD_GSD |
465 | + public abstract uint GetPercentage () throws IOError; |
466 | + public abstract uint SetPercentage (uint percentage) throws IOError; |
467 | +#else |
468 | + // use the Brightness property after updateing g-s-d to 3.10 or above |
469 | + public abstract int Brightness {get; set; } |
470 | +#endif |
471 | + } |
472 | + |
473 | + [DBus (name = "org.freedesktop.UPower")] |
474 | + interface UPower : GLib.Object { |
475 | + public abstract string[] EnumerateDevices () throws IOError; |
476 | + } |
477 | + |
478 | + [DBus (name = "org.freedesktop.UPower.Device")] |
479 | + interface UPowerDevice : GLib.Object { |
480 | + public abstract uint Type {get; set;} |
481 | + } |
482 | + |
483 | + public class Plug : Switchboard.Plug { |
484 | + |
485 | + private PowerSettings screen; |
486 | + private UPower upower; |
487 | + private Gtk.SizeGroup label_size; |
488 | + private GLib.Settings pantheon_dpms_settings; |
489 | + |
490 | + public Plug () { |
491 | + Object (category: Category.HARDWARE, |
492 | + code_name: "system-pantheon-power", |
493 | + display_name: _("Power"), |
494 | + description: _("Set display brightness, power button behavior, and sleep preferences"), |
495 | + icon: "preferences-system-power"); |
496 | + |
497 | + settings = new GLib.Settings ("org.gnome.settings-daemon.plugins.power"); |
498 | + pantheon_dpms_settings = new GLib.Settings ("org.pantheon.dpms"); |
499 | + try { |
500 | + screen = Bus.get_proxy_sync (BusType.SESSION, |
501 | + "org.gnome.SettingsDaemon", "/org/gnome/SettingsDaemon/Power"); |
502 | + } catch (IOError e) { |
503 | + warning ("Failed to get settings daemon for brightness setting"); |
504 | + } |
505 | + |
506 | + try { |
507 | + upower = Bus.get_proxy_sync (BusType.SYSTEM, |
508 | + "org.freedesktop.UPower", "/org/freedesktop/UPower"); |
509 | + } catch (IOError e) { |
510 | + warning ("Failed to get settings daemon for brightness setting"); |
511 | + } |
512 | + } |
513 | + |
514 | + public override Gtk.Widget get_widget () { |
515 | + if (stack_container == null) { |
516 | + //setup_info (); |
517 | + setup_ui (); |
518 | + } |
519 | + return stack_container; |
520 | + } |
521 | + |
522 | + public override void shown () { |
523 | + |
524 | + } |
525 | + |
526 | + public override void hidden () { |
527 | + |
528 | + } |
529 | + |
530 | + public override void search_callback (string location) { |
531 | + |
532 | + } |
533 | + |
534 | + // 'search' returns results like ("Keyboard → Behavior → Duration", "keyboard<sep>behavior") |
535 | + public override async Gee.TreeMap<string, string> search (string search) { |
536 | + return new Gee.TreeMap<string, string> (null, null); |
537 | + } |
538 | + |
539 | + void setup_ui () { |
540 | + stack_container = new Gtk.Box (Gtk.Orientation.VERTICAL, 0); |
541 | + label_size = new Gtk.SizeGroup (Gtk.SizeGroupMode.HORIZONTAL); |
542 | + |
543 | + var separator = new Gtk.Separator (Gtk.Orientation.HORIZONTAL); |
544 | + separator.vexpand = true; |
545 | + separator.valign = Gtk.Align.START; |
546 | + |
547 | + var common_settings = create_common_settings (); |
548 | + var stack = new Gtk.Stack (); |
549 | + var stack_switcher = new Gtk.StackSwitcher (); |
550 | + stack_switcher.halign = Gtk.Align.CENTER; |
551 | + stack_switcher.stack = stack; |
552 | + |
553 | + var plug_grid = create_notebook_pages ("ac"); |
554 | + stack.add_titled (plug_grid, "ac", _("Plugged In")); |
555 | + |
556 | + if (laptop_detect () || have_ups ()) { // when its not laptop, we check for ups |
557 | + var battery_grid = create_notebook_pages ("battery"); |
558 | + stack.add_titled (battery_grid, "battery", _("On Battery")); |
559 | + } |
560 | + |
561 | + stack_container.pack_start (common_settings); |
562 | + stack_container.pack_start (separator); |
563 | + stack_container.pack_start(stack_switcher, false, false, 0); |
564 | + stack_container.pack_start(stack, true, true, 0); |
565 | + stack_container.margin = 12; |
566 | + stack_container.show_all (); |
567 | + |
568 | + // hide stack switcher we only have ac line |
569 | + stack_switcher.set_visible (stack.get_children ().length () > 1); |
570 | + separator.set_visible (stack.get_children ().length () > 1); |
571 | + } |
572 | + |
573 | + private Gtk.Grid create_common_settings () { |
574 | + var grid = new Gtk.Grid (); |
575 | + grid.margin = 12; |
576 | + grid.column_spacing = 12; |
577 | + grid.row_spacing = 12; |
578 | + |
579 | + var brightness_label = new Gtk.Label (_("Display brightness:")); |
580 | + ((Gtk.Misc) brightness_label).xalign = 1.0f; |
581 | + label_size.add_widget (brightness_label); |
582 | + brightness_label.halign = Gtk.Align.END; |
583 | + |
584 | + var scale = new Gtk.Scale.with_range (Gtk.Orientation.HORIZONTAL, 0, 100, 10); |
585 | + scale.set_draw_value (false); |
586 | + scale.hexpand = true; |
587 | + scale.width_request = 480; |
588 | + |
589 | + var dim_label = new Gtk.Label (_("Dim screen when inactive:")); |
590 | + ((Gtk.Misc) dim_label).xalign = 1.0f; |
591 | + var dim_switch = new Gtk.Switch (); |
592 | + dim_switch.halign = Gtk.Align.START; |
593 | + |
594 | + settings.bind ("idle-dim", dim_switch, "active", SettingsBindFlags.DEFAULT); |
595 | + |
596 | + // Checking if device is a laptop or desktop. If it's a desktop, hide the brightness scale |
597 | + // Prevents plug freezing on desktops. |
598 | + bool show_brightness = false; |
599 | + try { |
600 | + var power_devices = upower.EnumerateDevices (); |
601 | + foreach (string device in power_devices) { |
602 | + if (device.contains ("battery")) { |
603 | + show_brightness = true; |
604 | + } |
605 | + } |
606 | + |
607 | + if (show_brightness) { |
608 | +#if OLD_GSD |
609 | + scale.set_value (screen.GetPercentage ()); |
610 | +#else |
611 | + scale.set_value (screen.Brightness); |
612 | +#endif |
613 | + } else { |
614 | + warning ("No battery found, hiding brightness settings"); |
615 | + } |
616 | + |
617 | + } catch (Error e) { |
618 | + warning ("Brightness setter not available, hiding brightness settings"); |
619 | + show_brightness = false; |
620 | + } |
621 | + |
622 | + if (show_brightness == false) { |
623 | + brightness_label.no_show_all = true; |
624 | + scale.no_show_all = true; |
625 | + dim_label.no_show_all = true; |
626 | + dim_switch.no_show_all = true; |
627 | + } |
628 | + |
629 | + scale.value_changed.connect (() => { |
630 | + var val = (int) scale.get_value (); |
631 | + try { |
632 | +#if OLD_GSD |
633 | + screen.SetPercentage (val); |
634 | +#else |
635 | + screen.Brightness = val; |
636 | +#endif |
637 | + } catch (IOError ioe) { |
638 | + // ignore, because if we have GetPercentage, we have SetPercentage |
639 | + // otherwise the scale won't be visible to change |
640 | + } |
641 | + }); |
642 | + |
643 | + grid.attach (brightness_label, 0, 1, 1, 1); |
644 | + grid.attach (scale, 1, 1, 1, 1); |
645 | + |
646 | + grid.attach (dim_label, 0, 2, 1, 1); |
647 | + grid.attach (dim_switch, 1, 2, 1, 1); |
648 | + |
649 | + string[] labels = {_("Sleep button:"), _("Suspend button:"), _("Hibernate button:"), _("Power button:")}; |
650 | + string[] keys = {"button-sleep", "button-suspend", "button-hibernate", "button-power"}; |
651 | + |
652 | + for (int i = 0; i < labels.length; i++) { |
653 | + var box = new ActionComboBox (labels[i], keys[i]); |
654 | + grid.attach (box.label, 0, i+3, 1, 1); |
655 | + label_size.add_widget (box.label); |
656 | + grid.attach (box, 1, i+3, 1, 1); |
657 | + } |
658 | + |
659 | + var screen_timeout_label = new Gtk.Label (_("Turn off screen when inactive after:")); |
660 | + label_size.add_widget (screen_timeout_label); |
661 | + ((Gtk.Misc) screen_timeout_label).xalign = 1.0f; |
662 | + var screen_timeout = new TimeoutComboBox (pantheon_dpms_settings, "standby-time"); |
663 | + screen_timeout.changed.connect (run_dpms_helper); |
664 | + |
665 | + grid.attach (screen_timeout_label, 0, labels.length+3, 1, 1); |
666 | + grid.attach (screen_timeout, 1, labels.length+3, 1, 1); |
667 | + |
668 | + return grid; |
669 | + } |
670 | + |
671 | + private Gtk.Grid create_notebook_pages (string type) { |
672 | + var grid = new Gtk.Grid (); |
673 | + grid.margin = 12; |
674 | + grid.column_spacing = 12; |
675 | + grid.row_spacing = 12; |
676 | + |
677 | + var sleep_timeout_label = new Gtk.Label (_("Sleep when inactive after:")); |
678 | + ((Gtk.Misc) sleep_timeout_label).xalign = 1.0f; |
679 | + label_size.add_widget (sleep_timeout_label); |
680 | + |
681 | + var scale_settings = @"sleep-inactive-$type-timeout"; |
682 | + var sleep_timeout = new TimeoutComboBox (settings, scale_settings); |
683 | + |
684 | + grid.attach (sleep_timeout_label, 0, 0, 1, 1); |
685 | + grid.attach (sleep_timeout, 1, 0, 1, 1); |
686 | + |
687 | + if (type != "ac") { |
688 | + var critical_box = new ActionComboBox (_("When power is critically low:"), "critical-battery-action"); |
689 | + grid.attach (critical_box.label, 0, 2, 1, 1); |
690 | + label_size.add_widget (critical_box.label); |
691 | + grid.attach (critical_box, 1, 2, 1, 1); |
692 | + } |
693 | + |
694 | + return grid; |
695 | + } |
696 | + |
697 | + private bool laptop_detect () { |
698 | + string test_laptop_detect = Environment.find_program_in_path ("laptop-detect"); |
699 | + if (test_laptop_detect == null && |
700 | + FileUtils.test ("/usr/sbin/laptop-detect", FileTest.EXISTS) && |
701 | + FileUtils.test ("/usr/sbin/laptop-detect", FileTest.IS_REGULAR) && |
702 | + FileUtils.test ("/usr/sbin/laptop-detect", FileTest.IS_EXECUTABLE)) { |
703 | + test_laptop_detect = "/usr/sbin/laptop-detect"; |
704 | + } |
705 | + |
706 | + if (test_laptop_detect != null) { |
707 | + int exit_status; |
708 | + string standard_output, standard_error; |
709 | + try { |
710 | + Process.spawn_command_line_sync (test_laptop_detect, out standard_output, |
711 | + out standard_error, out exit_status); |
712 | + if (exit_status == 0) { |
713 | + debug ("Laptop detect return true"); |
714 | + return true; |
715 | + } else { |
716 | + debug ("Laptop detect return false"); |
717 | + return false; |
718 | + } |
719 | + } catch (SpawnError err) { |
720 | + warning (err.message); |
721 | + return false; |
722 | + } |
723 | + } else { |
724 | + warning ("Laptop detect not find"); |
725 | + /* TODO check upower, and /proc files as laptop-detect does to find batteries */ |
726 | + return false; |
727 | + } |
728 | + } |
729 | + |
730 | + private void run_dpms_helper () { |
731 | + Settings.sync (); |
732 | + |
733 | + try { |
734 | + Process.spawn_async (null, { "elementary-dpms-helper" }, Environ.get (), |
735 | + SpawnFlags.SEARCH_PATH|SpawnFlags.STDERR_TO_DEV_NULL|SpawnFlags.STDOUT_TO_DEV_NULL, |
736 | + null, null); |
737 | + } catch (SpawnError e) { |
738 | + warning ("Failed to reset dpms settings: %s", e.message); |
739 | + } |
740 | + } |
741 | + |
742 | + enum Type { |
743 | + UNKNOWN = 0, |
744 | + LINE_POWER, |
745 | + BATTERY, |
746 | + UPS, |
747 | + MONITOR, |
748 | + MOUSE, |
749 | + KEYBOARD, |
750 | + PDA |
751 | + } |
752 | + |
753 | + private bool have_ups () { |
754 | + /* TODO:check for ups using upower */ |
755 | + string[] devices; |
756 | + |
757 | + try { |
758 | + devices = upower.EnumerateDevices (); |
759 | + } catch (Error e) { |
760 | + message (e.message); |
761 | + return false; |
762 | + } |
763 | + |
764 | + foreach (var device in devices) { |
765 | + try { |
766 | + UPowerDevice upower_device = Bus.get_proxy_sync (BusType.SYSTEM, |
767 | + "org.freedesktop.UPower", device); |
768 | + if (upower_device.Type == Type.UPS) { |
769 | + debug ("found UPS in %s", device); |
770 | + return true; |
771 | + } |
772 | + } catch (Error e) { |
773 | + debug (e.message); |
774 | + } |
775 | + } |
776 | + |
777 | + return false; |
778 | + } |
779 | + } |
780 | } |
781 | |
782 | public Switchboard.Plug get_plug (Module module) { |
783 | - debug ("Activating Power plug"); |
784 | - var plug = new Power.Plug (); |
785 | - return plug; |
786 | -} |
787 | + debug ("Activating Power plug"); |
788 | + var plug = new Power.Plug (); |
789 | + return plug; |
790 | +} |
791 | \ No newline at end of file |
792 | |
793 | === modified file 'src/TimeoutComboBox.vala' |
794 | --- src/TimeoutComboBox.vala 2015-02-28 09:04:35 +0000 |
795 | +++ src/TimeoutComboBox.vala 2015-08-24 17:16:29 +0000 |
796 | @@ -1,65 +1,84 @@ |
797 | +/* |
798 | + * Copyright (C) 2015 Pantheon Developers (http://launchpad.net/switchboard-plug-power) |
799 | + * |
800 | + * This program or library is free software; you can redistribute it |
801 | + * and/or modify it under the terms of the GNU Lesser General Public |
802 | + * License as published by the Free Software Foundation; either |
803 | + * version 3 of the License, or (at your option) any later version. |
804 | + * |
805 | + * This library is distributed in the hope that it will be useful, |
806 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
807 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
808 | + * Lesser General Public License for more details. |
809 | + * |
810 | + * You should have received a copy of the GNU Lesser General |
811 | + * Public License along with this library; if not, write to the |
812 | + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
813 | + * Boston, MA 02110-1301 USA. |
814 | + */ |
815 | + |
816 | namespace Power { |
817 | - class TimeoutComboBox : Gtk.ComboBoxText { |
818 | - |
819 | - private GLib.Settings schema; |
820 | - private string key; |
821 | - |
822 | - private const int SECS_IN_MINUTE = 60; |
823 | - private const int[] timeout = { |
824 | - 0, |
825 | - 5 * SECS_IN_MINUTE, |
826 | - 10 * SECS_IN_MINUTE, |
827 | - 15 * SECS_IN_MINUTE, |
828 | - 30 * SECS_IN_MINUTE, |
829 | - 45 * SECS_IN_MINUTE, |
830 | - 60 * SECS_IN_MINUTE, |
831 | - 120 * SECS_IN_MINUTE |
832 | - }; |
833 | - |
834 | - public TimeoutComboBox (GLib.Settings schema, string key) { |
835 | - this.key = key; |
836 | - this.schema = schema; |
837 | - |
838 | - this.append_text (_("Never")); |
839 | - this.append_text (_("5 min")); |
840 | - this.append_text (_("10 min")); |
841 | - this.append_text (_("15 min")); |
842 | - this.append_text (_("30 min")); |
843 | - this.append_text (_("45 min")); |
844 | - this.append_text (_("1 hour")); |
845 | - this.append_text (_("2 hours")); |
846 | - |
847 | - this.hexpand = true; |
848 | - |
849 | - update_combo (); |
850 | - |
851 | - this.changed.connect (update_settings); |
852 | - this.schema.changed[key].connect (update_combo); |
853 | - } |
854 | - |
855 | - private void update_settings () { |
856 | - schema.set_int (key, timeout[active]); |
857 | - } |
858 | - |
859 | - // find closest timeout to our level |
860 | - private int find_closest (int second) { |
861 | - int key = 0; |
862 | - |
863 | - foreach (int i in timeout) { |
864 | - if (second > i) |
865 | - key++; |
866 | - else |
867 | - break; |
868 | - } |
869 | - |
870 | - return key; |
871 | - } |
872 | - |
873 | - private void update_combo () { |
874 | - int val = schema.get_int (key); |
875 | - |
876 | - // need to process value to comply our timeout level |
877 | - this.active = find_closest (val); |
878 | - } |
879 | - } |
880 | -} |
881 | + class TimeoutComboBox : Gtk.ComboBoxText { |
882 | + |
883 | + private GLib.Settings schema; |
884 | + private string key; |
885 | + |
886 | + private const int SECS_IN_MINUTE = 60; |
887 | + private const int[] timeout = { |
888 | + 0, |
889 | + 5 * SECS_IN_MINUTE, |
890 | + 10 * SECS_IN_MINUTE, |
891 | + 15 * SECS_IN_MINUTE, |
892 | + 30 * SECS_IN_MINUTE, |
893 | + 45 * SECS_IN_MINUTE, |
894 | + 60 * SECS_IN_MINUTE, |
895 | + 120 * SECS_IN_MINUTE |
896 | + }; |
897 | + |
898 | + public TimeoutComboBox (GLib.Settings schema_name, string key_value) { |
899 | + key = key_value; |
900 | + schema = schema_name; |
901 | + |
902 | + append_text (_("Never")); |
903 | + append_text (_("5 min")); |
904 | + append_text (_("10 min")); |
905 | + append_text (_("15 min")); |
906 | + append_text (_("30 min")); |
907 | + append_text (_("45 min")); |
908 | + append_text (_("1 hour")); |
909 | + append_text (_("2 hours")); |
910 | + |
911 | + hexpand = true; |
912 | + |
913 | + update_combo (); |
914 | + |
915 | + changed.connect (update_settings); |
916 | + schema.changed[key].connect (update_combo); |
917 | + } |
918 | + |
919 | + private void update_settings () { |
920 | + schema.set_int (key, timeout[active]); |
921 | + } |
922 | + |
923 | + // find closest timeout to our level |
924 | + private int find_closest (int second) { |
925 | + int key = 0; |
926 | + |
927 | + foreach (int i in timeout) { |
928 | + if (second > i) |
929 | + key++; |
930 | + else |
931 | + break; |
932 | + } |
933 | + |
934 | + return key; |
935 | + } |
936 | + |
937 | + private void update_combo () { |
938 | + int val = schema.get_int (key); |
939 | + |
940 | + // need to process value to comply our timeout level |
941 | + active = find_closest (val); |
942 | + } |
943 | + } |
944 | +} |
945 | \ No newline at end of file |
Thanks for your work.
I planned to run the codestyle-bot for this project anyway, so you wouldn't have to do this, but at least the tabs are now replaced so the diff of the bot will be cleaner :-)
And please see the slack discussion about the "this" before we think about merging this.