Merge lp:~julien-spautz/switchboard-plug-power/code-cleanup into lp:~elementary-apps/switchboard-plug-power/trunk

Proposed by Julien Spautz
Status: Merged
Merged at revision: 53
Proposed branch: lp:~julien-spautz/switchboard-plug-power/code-cleanup
Merge into: lp:~elementary-apps/switchboard-plug-power/trunk
Diff against target: 456 lines (+132/-319)
1 file modified
src/power.vala (+132/-319)
To merge this branch: bzr merge lp:~julien-spautz/switchboard-plug-power/code-cleanup
Reviewer Review Type Date Requested Status
Cody Garver (community) Approve
David Gomes (community) Needs Fixing
Review via email: mp+147555@code.launchpad.net

Description of the change

src/power.vala has been mostly rewritten to avoid redundancy, following other changes have been made:

- a string has been changed to "When battery power is critically low:" (bug-fix)
- the comboboxes support all possible gsettings values (bug-fix)
- everything is in the same grid for better alignment (bug-fix)
- external changes (e.g. from dconf-editor) are monitored and changed in the plug accordingly
- suspend and hibernate buttons are now supported

To post a comment you must log in.
53. By Julien Spautz

fixed coding style

54. By Julien Spautz

minor code simplifications

Revision history for this message
David Gomes (davidgomes) wrote :

Code style stuff:
-> Don't align stuff like on diff's 420 and 378, and 437-438 and 442-443 (there are others I think).
-> Don't add an empty newline like on diff's 339, 371 and 451 (but there are others).
-> Like 375 and 376, those aligned lines, fix them (there are others).
-> Line 392 - "var scale_settings = "sleep-inactive-"+type+"-timeout";", space out the plus signs - "var scale_settings = "sleep-inactive-" + type + "-timeout";" and maybe format the string.

Basically, most of what is wrong is aligning stuff.

It's "Needs Fixing" for now because of the code.

I tested the plug and it looks alright, but I can't run a full test because I don't use elementary OS.

review: Needs Fixing
Revision history for this message
Cody Garver (codygarver) wrote :

I will review the rest. Adding to my todo list.

55. By Julien Spautz

fixed code alignment and stuff

Revision history for this message
Cody Garver (codygarver) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/power.vala'
2--- src/power.vala 2012-10-26 22:55:11 +0000
3+++ src/power.vala 2013-02-24 10:23:21 +0000
4@@ -1,320 +1,133 @@
5-public class PowerPlug.PowerPlug : Pantheon.Switchboard.Plug {
6-
7- Gtk.ListStore liststore_sleep;
8- Gtk.ListStore liststore_power;
9- public Gtk.ListStore liststore_critical;
10- Gtk.ListStore liststore_time;
11- public Gtk.ListStore liststore_lid;
12-
13- Gtk.TreeIter iter;
14-
15- Gtk.ComboBox critical_power;
16- Gtk.ComboBox power_button;
17- Gtk.ComboBox slp_button;
18- Gtk.ComboBox lid_closed_battery;
19- Gtk.ComboBox lid_closed_ac;
20-
21- Gtk.CellRendererText cell;
22-
23- GLib.Settings settings;
24- Granite.Widgets.StaticNotebook staticnotebook;
25- Gtk.VBox vbox;
26-
27- public PowerPlug () {
28- settings = new GLib.Settings ("org.gnome.settings-daemon.plugins.power");
29- /***********************/
30-
31- /*ListStore Sleep*/
32- liststore_sleep = new Gtk.ListStore (2, typeof (string), typeof (int));
33-
34- liststore_sleep.append (out iter);
35- liststore_sleep.set (iter, 0, _("Suspend"), 1, 1);
36- liststore_sleep.append (out iter);
37- liststore_sleep.set (iter, 0, _("Hibernate"), 1, 3);
38-
39- /*ListStore Power*/
40- liststore_power = new Gtk.ListStore (2, typeof (string), typeof (int));
41-
42- liststore_power.append (out iter);
43- liststore_power.set (iter, 0, _("Suspend"), 1, 1);
44- liststore_power.append (out iter);
45- liststore_power.set (iter, 0, _("Hibernate"), 1, 3);
46- liststore_power.append (out iter);
47- liststore_power.set (iter, 0, _("Do nothing"), 1, 5);
48- liststore_power.append (out iter);
49- liststore_power.set (iter, 0, _("Ask me"), 1, 4);
50- liststore_power.append (out iter);
51- liststore_power.set (iter, 0, _("Shutdown"), 1, 2);
52-
53- /*ListStore Critical*/
54- liststore_critical = new Gtk.ListStore (2, typeof (string), typeof (int));
55-
56- liststore_critical.append (out iter);
57- liststore_critical.set (iter, 0, _("Hibernate"), 1, 3);
58- liststore_critical.append (out iter);
59- liststore_critical.set (iter, 0, _("Shutdown"), 1, 2);
60-
61- /*ListStore Time*/
62- liststore_time = new Gtk.ListStore (2, typeof (string), typeof (int));
63-
64- liststore_time.append (out iter);
65- liststore_time.set (iter, 0, _("5 minutes"), 1, 300);
66- liststore_time.append (out iter);
67- liststore_time.set (iter, 0, _("10 minutes"), 1, 500);
68- liststore_time.append (out iter);
69- liststore_time.set (iter, 0, _("30 minutes"), 1, 1800);
70- liststore_time.append (out iter);
71- liststore_time.set (iter, 0, _("1 hour"), 1, 3600);
72- liststore_time.append (out iter);
73- liststore_time.set (iter, 0, _("Don't suspend"), 1, 0);
74-
75- /*ListStore Lid closed*/
76- liststore_lid = new Gtk.ListStore (2, typeof (string), typeof (int));
77-
78- liststore_lid.append (out iter);
79- liststore_lid.set (iter, 0, _("Suspend"), 1, 1);
80- liststore_lid.append (out iter);
81- liststore_lid.set (iter, 0, _("Hibernate"), 1, 3);
82- liststore_lid.append (out iter);
83- liststore_lid.set (iter, 0, _("Do nothing"), 1, 0);
84-
85- /***********************/
86-
87- create_ui ();
88-
89- add (vbox);
90-
91- }
92-
93- void set_value_for_combo (Gtk.ComboBox combo, int val) {
94- Gtk.TreeIter iter;
95- Gtk.TreeModel model;
96- int value_tmp;
97- bool ret;
98-
99- /* get entry */
100- model = combo.get_model ();
101- ret = model.get_iter_first (out iter);
102- if (!ret)
103- return;
104-
105- /* try to make the UI match the setting */
106- do {
107- model.get (iter, 1, out value_tmp);
108- if (val == value_tmp) {
109- combo.set_active_iter (iter);
110- break;
111- }
112- } while (model.iter_next (ref iter));
113- }
114-
115- void update_pow_crit () {
116- Gtk.TreeIter iter;
117- bool ret = critical_power.get_active_iter (out iter);
118- if (!ret)
119- return;
120-
121- /* get entry */
122- var model = critical_power.get_model ();
123- int val;
124- model.get (iter, 1, out val);
125-
126- settings.set_enum ("critical-battery-action", val);
127- }
128-
129- void update_power_button () {
130- Gtk.TreeIter iter;
131- bool ret = power_button.get_active_iter (out iter);
132- if (!ret)
133- return;
134-
135- /* get entry */
136- var model = power_button.get_model ();
137- int val;
138- model.get (iter, 1, out val);
139-
140- settings.set_enum ("button-power", val);
141- }
142-
143- void update_slp_button () {
144- Gtk.TreeIter iter;
145- bool ret = slp_button.get_active_iter (out iter);
146- if (!ret)
147- return;
148-
149- /* get entry */
150- var model = slp_button.get_model ();
151- int val;
152- model.get (iter, 1, out val);
153-
154- settings.set_enum ("button-sleep", val);
155- }
156-
157- void update_lid_closed_combobox (Gtk.ComboBox combobox, string type) {
158- Gtk.TreeIter iter;
159- bool ret = combobox.get_active_iter (out iter);
160- if (!ret)
161- return;
162-
163- /* get entry */
164- var model = combobox.get_model ();
165- int val;
166- model.get (iter, 1, out val);
167-
168- if (type == "ac") {
169- settings.set_enum ("lid-close-ac-action", val);
170- } else {
171- settings.set_enum ("lid-close-battery-action", val);
172- }
173- }
174-
175- void create_ui () {
176- int val;
177-
178- staticnotebook = new Granite.Widgets.StaticNotebook (false);
179- var plug_grid = create_notebook_pages ("ac");
180- var battery_grid = create_notebook_pages ("battery");
181-
182- staticnotebook.append_page (plug_grid, new Gtk.Label(_("Plugged In")));
183- staticnotebook.append_page (battery_grid, new Gtk.Label(_("Battery Power")));
184-
185- // Power button row
186- var power_button_label = new Gtk.Label (_("When the power button is pressed:"));
187- power_button_label.halign = Gtk.Align.END;
188-
189- power_button = new Gtk.ComboBox.with_model (liststore_power);
190- cell = new Gtk.CellRendererText();
191- power_button.pack_start( cell, false );
192- power_button.set_attributes( cell, "text", 0 );
193- power_button.hexpand = true;
194-
195- val = settings.get_enum ("button-power");
196- set_value_for_combo (power_button, val);
197- power_button.changed.connect (update_power_button);
198-
199- //Sleep button row
200- var slp_button_label = new Gtk.Label (_("When the sleep button is pressed:"));
201- slp_button_label.halign = Gtk.Align.END;
202-
203- slp_button = new Gtk.ComboBox.with_model (liststore_sleep);
204- cell = new Gtk.CellRendererText();
205- slp_button.pack_start( cell, false );
206- slp_button.set_attributes( cell, "text", 0 );
207- slp_button.hexpand = true;
208-
209- val = settings.get_enum ("button-sleep");
210- set_value_for_combo (slp_button, val);
211- slp_button.changed.connect (update_slp_button);
212-
213- var grid = new Gtk.Grid ();
214- grid.margin = 32;
215- grid.attach (power_button_label, 0, 0, 1, 1);
216- grid.attach (power_button, 1, 0, 1, 1);
217- grid.attach (slp_button_label, 0, 1, 1, 1);
218- grid.attach (slp_button, 1, 1, 1, 1);
219-
220- vbox = new Gtk.VBox (false, 4);
221- vbox.pack_start (staticnotebook, true, true, 0);
222- vbox.pack_start (new Gtk.HSeparator(), true, true, 0);
223- vbox.pack_start (grid, true, true, 0);
224-
225- }
226-
227- Gtk.Grid create_notebook_pages (string type)
228- {
229- int val;
230- double dval;
231-
232- var grid = new Gtk.Grid ();
233- grid.margin = 32;
234- grid.column_spacing = grid.row_spacing = 4;
235-
236- var scale_label = new Gtk.Label (_("Put the computer to sleep when inactive:"));
237- grid.attach (scale_label, 0, 0, 1, 1);
238-
239- var scale = new Gtk.Scale.with_range (Gtk.Orientation.HORIZONTAL, 0, 4000, 300);
240- scale.set_draw_value (false);
241- scale.add_mark (300, Gtk.PositionType.BOTTOM, _("5 min"));
242- scale.add_mark (600, Gtk.PositionType.BOTTOM, _("10 min"));
243- scale.add_mark (1800, Gtk.PositionType.BOTTOM, _("30 min"));
244- scale.add_mark (3600, Gtk.PositionType.BOTTOM, _("1 hour"));
245- scale.add_mark (4000, Gtk.PositionType.BOTTOM, _("Never"));
246- scale.hexpand = true;
247- scale.width_request = 480;
248- grid.attach (scale, 1, 0, 1, 4);
249-
250- dval = (double) settings.get_int ("sleep-inactive-"+type+"-timeout");
251- if (dval == 0) {
252- scale.set_value (4000);
253- } else {
254- scale.set_value (dval);
255- }
256-
257- scale.value_changed.connect (() => {
258- int vale = (int)scale.get_value ();
259- if (vale <= 3600) {
260- settings.set_int ("sleep-inactive-"+type+"-timeout", vale); }
261- else if (vale == 4000) {
262- settings.set_int ("sleep-inactive-"+type+"-timeout", 0);
263- }
264- });
265-
266- if (type == "ac") {
267- var lid_closed_label = new Gtk.Label (_("When the lid is closed:"));
268- lid_closed_label.halign = Gtk.Align.END;
269- grid.attach (lid_closed_label, 0, 4, 1, 1);
270-
271- lid_closed_ac = new Gtk.ComboBox.with_model (liststore_lid);
272- cell = new Gtk.CellRendererText();
273- lid_closed_ac.pack_start( cell, false );
274- lid_closed_ac.set_attributes( cell, "text", 0 );
275- grid.attach (lid_closed_ac, 1, 4, 2, 1);
276-
277- val = settings.get_enum ("lid-close-"+type+"-action");
278- set_value_for_combo (lid_closed_ac, val);
279- lid_closed_ac.changed.connect (() => {update_lid_closed_combobox (lid_closed_ac, "ac");});
280- } else {
281- var lid_closed_label = new Gtk.Label (_("When the lid is closed:"));
282- lid_closed_label.halign = Gtk.Align.END;
283- grid.attach (lid_closed_label, 0, 4, 1, 1);
284-
285- lid_closed_battery = new Gtk.ComboBox.with_model (liststore_lid);
286- cell = new Gtk.CellRendererText();
287- lid_closed_battery.pack_start( cell, false );
288- lid_closed_battery.set_attributes( cell, "text", 0 );
289- grid.attach (lid_closed_battery, 1, 4, 2, 1);
290-
291- val = settings.get_enum ("lid-close-"+type+"-action");
292- set_value_for_combo (lid_closed_battery, val);
293- lid_closed_battery.changed.connect (() => {update_lid_closed_combobox (lid_closed_battery, "battery");});
294- }
295-
296- if (type != "ac") {
297- var critical_label = new Gtk.Label (_("When the power is critically low:"));
298- critical_label.halign = Gtk.Align.END;
299- grid.attach (critical_label, 0, 5, 1, 1);
300-
301- critical_power = new Gtk.ComboBox.with_model (liststore_critical);
302- cell = new Gtk.CellRendererText();
303- critical_power.pack_start( cell, false );
304- critical_power.set_attributes( cell, "text", 0 );
305- grid.attach (critical_power, 1, 5, 2, 1);
306-
307- val = settings.get_enum ("critical-battery-action");
308- set_value_for_combo (critical_power, val);
309- critical_power.changed.connect (update_pow_crit);
310- }
311-
312- return grid;
313- }
314-}
315-
316-public static int main (string[] args) {
317-
318- Gtk.init (ref args);
319- var plug = new PowerPlug.PowerPlug ();
320- plug.register (_("Power"));
321- plug.show_all ();
322- Gtk.main ();
323- return 0;
324+namespace Power {
325+
326+ GLib.Settings settings;
327+
328+ class ComboBox : Gtk.ComboBoxText {
329+
330+ public Gtk.Label label;
331+ private string key;
332+
333+ // this maps combobox indices to gsettings enums
334+ private int[] map_to_sett = {1, 2, 3, 4, 5};
335+ // and vice-versa
336+ private int[] map_to_list = {4, 0, 1, 2, 3, 4};
337+
338+ public ComboBox (string label, string key) {
339+ this.key = key;
340+ this.label = new Gtk.Label (label);
341+ this.label.halign = Gtk.Align.END;
342+
343+ this.append_text (_("Suspend"));
344+ this.append_text (_("Shutdown"));
345+ this.append_text (_("Hibernate"));
346+ this.append_text (_("Ask me"));
347+ this.append_text (_("Do nothing"));
348+
349+ this.hexpand = true;
350+
351+ update_combo ();
352+
353+ this.changed.connect (update_settings);
354+ settings.changed[key].connect (update_combo);
355+ }
356+
357+ private void update_settings () {
358+ settings.set_enum (key, map_to_sett[active]);
359+ }
360+
361+ private void update_combo () {
362+ int val = settings.get_enum (key);
363+ active = map_to_list [val];
364+ }
365+ }
366+
367+ public class PowerPlug : Pantheon.Switchboard.Plug {
368+
369+ public PowerPlug () {
370+ settings = new GLib.Settings ("org.gnome.settings-daemon.plugins.power");
371+
372+ var staticnotebook = new Granite.Widgets.StaticNotebook (false);
373+ var plug_grid = create_notebook_pages ("ac");
374+ var battery_grid = create_notebook_pages ("battery");
375+
376+ staticnotebook.append_page (plug_grid, new Gtk.Label(_("Plugged In")));
377+ staticnotebook.append_page (battery_grid, new Gtk.Label(_("Battery Power")));
378+ staticnotebook.margin = 12;
379+
380+ add (staticnotebook);
381+ }
382+
383+ private Gtk.Grid create_notebook_pages (string type) {
384+ var grid = new Gtk.Grid ();
385+ grid.margin = 12;
386+ grid.column_spacing = 12;
387+ grid.row_spacing = 12;
388+
389+ var scale_label = new Gtk.Label (_("Put the computer to sleep when inactive:"));
390+ var scale_settings = @"sleep-inactive-$type-timeout";
391+
392+ var scale = new Gtk.Scale.with_range (Gtk.Orientation.HORIZONTAL, 0, 4000, 300);
393+ scale.set_draw_value (false);
394+ scale.add_mark (300, Gtk.PositionType.BOTTOM, _("5 min"));
395+ scale.add_mark (600, Gtk.PositionType.BOTTOM, _("10 min"));
396+ scale.add_mark (1800, Gtk.PositionType.BOTTOM, _("30 min"));
397+ scale.add_mark (3600, Gtk.PositionType.BOTTOM, _("1 hour"));
398+ scale.add_mark (4000, Gtk.PositionType.BOTTOM, _("Never"));
399+ scale.hexpand = true;
400+ scale.width_request = 480;
401+
402+ var dval = (double) settings.get_int (scale_settings);
403+
404+ if (dval == 0)
405+ scale.set_value (4000);
406+ else
407+ scale.set_value (dval);
408+
409+ scale.value_changed.connect (() => {
410+ var val = (int) scale.get_value ();
411+ if (val <= 3600)
412+ settings.set_int (scale_settings, val);
413+ else if (val == 4000)
414+ settings.set_int (scale_settings, 0);
415+ });
416+
417+ grid.attach (scale_label, 0, 0, 1, 1);
418+ grid.attach (scale, 1, 0, 1, 1);
419+
420+ var lid_closed_box = new ComboBox (_("When the lid is closed:"), @"lid-close-$type-action");
421+ grid.attach (lid_closed_box.label, 0, 1, 1, 1);
422+ grid.attach (lid_closed_box, 1, 1, 1, 1);
423+
424+ if (type != "ac") {
425+ var critical_box = new ComboBox (_("When battery power is critically low:"), "critical-battery-action");
426+ grid.attach (critical_box.label, 0, 2, 1, 1);
427+ grid.attach (critical_box, 1, 2, 1, 1);
428+ }
429+
430+ var separator = new Gtk.Separator (Gtk.Orientation.HORIZONTAL);
431+ separator.vexpand = true;
432+ separator.valign = Gtk.Align.END;
433+ grid.attach (separator, 0, 3, 2, 1);
434+
435+ string[] labels = {_("Sleep button:"), _("Suspend button:"), _("Hibernate button:"), _("Power button:")};
436+ string[] keys = {"button-sleep", "button-suspend", "button-hibernate", "button-power"};
437+
438+ for (int i = 0; i < labels.length; i++) {
439+ var box = new Power.ComboBox (labels[i], keys[i]);
440+ grid.attach (box.label, 0, i+4, 1, 1);
441+ grid.attach (box, 1, i+4, 1, 1);
442+ }
443+
444+ return grid;
445+ }
446+ }
447+
448+ public static int main (string[] args) {
449+ Gtk.init (ref args);
450+ var plug = new Power.PowerPlug ();
451+ plug.register (_("Power"));
452+ plug.show_all ();
453+ Gtk.main ();
454+ return 0;
455+ }
456 }

Subscribers

People subscribed via source and target branches

to all changes: