Merge lp:~charlesk/indicator-power/lp-1100546-not-present-mouse into lp:indicator-power

Proposed by Charles Kerr
Status: Merged
Approved by: Charles Kerr
Approved revision: 302
Merged at revision: 302
Proposed branch: lp:~charlesk/indicator-power/lp-1100546-not-present-mouse
Merge into: lp:indicator-power
Diff against target: 546 lines (+144/-73)
8 files modified
debian/changelog (+6/-0)
src/device-provider-upower.c (+5/-1)
src/device.c (+37/-5)
src/device.h (+4/-1)
src/service.c (+22/-6)
src/testing.c (+2/-1)
tests/test-device.cc (+62/-56)
tests/test-notify.cc (+6/-3)
To merge this branch: bzr merge lp:~charlesk/indicator-power/lp-1100546-not-present-mouse
Reviewer Review Type Date Requested Status
Charles Kerr (community) Approve
Review via email: mp+314869@code.launchpad.net

Commit message

When sorting devices by priority, give preference to devices that power the system.

Description of the change

When sorting devices by priority, give preference to devices that power the system.

To post a comment you must log in.
Revision history for this message
Charles Kerr (charlesk) wrote :

Self-approving as I'm just the code reviewer; Shih-Yuan Lee authored the patch and submitted as a debdiff.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'debian/changelog'
2--- debian/changelog 2016-12-01 22:34:02 +0000
3+++ debian/changelog 2017-01-16 19:06:20 +0000
4@@ -1,3 +1,9 @@
5+indicator-power (12.10.6+17.04.20161201-0ubuntu2) zesty; urgency=medium
6+
7+ * Make device with power supply has higher sorting priority. (LP: #1100546)
8+
9+ -- Shih-Yuan Lee (FourDollars) <fourdollars@ubuntu.com> Thu, 05 Jan 2017 11:54:01 +0800
10+
11 indicator-power (12.10.6+17.04.20161201-0ubuntu1) zesty; urgency=medium
12
13 [ Michael Terry ]
14
15=== modified file 'src/device-provider-upower.c'
16--- src/device-provider-upower.c 2014-09-08 14:50:22 +0000
17+++ src/device-provider-upower.c 2017-01-16 19:06:20 +0000
18@@ -113,6 +113,7 @@
19 gint64 time_to_empty = 0;
20 gint64 time_to_full = 0;
21 gint64 time;
22+ gboolean power_supply = FALSE;
23 IndicatorPowerDevice * device;
24 priv_t * p = get_priv(data->self);
25 GVariant * dict = g_variant_get_child_value (response, 0);
26@@ -122,6 +123,7 @@
27 g_variant_lookup (dict, "Percentage", "d", &percentage);
28 g_variant_lookup (dict, "TimeToEmpty", "x", &time_to_empty);
29 g_variant_lookup (dict, "TimeToFull", "x", &time_to_full);
30+ g_variant_lookup (dict, "PowerSupply", "b", &power_supply);
31 time = time_to_empty ? time_to_empty : time_to_full;
32
33 if ((device = g_hash_table_lookup (p->devices, data->path)))
34@@ -131,6 +133,7 @@
35 INDICATOR_POWER_DEVICE_OBJECT_PATH, data->path,
36 INDICATOR_POWER_DEVICE_PERCENTAGE, percentage,
37 INDICATOR_POWER_DEVICE_TIME, time,
38+ INDICATOR_POWER_DEVICE_POWER_SUPPLY, power_supply,
39 NULL);
40 }
41 else
42@@ -139,7 +142,8 @@
43 kind,
44 percentage,
45 state,
46- (time_t)time);
47+ (time_t)time,
48+ power_supply);
49
50 g_hash_table_insert (p->devices,
51 g_strdup (data->path),
52
53=== modified file 'src/device.c'
54--- src/device.c 2016-05-26 18:32:08 +0000
55+++ src/device.c 2017-01-16 19:06:20 +0000
56@@ -42,6 +42,7 @@
57 the time-remaining field for this device, or 0 if not applicable.
58 This is used when generating the time-remaining string. */
59 GTimer * inestimable;
60+ gboolean power_supply;
61 };
62
63 /* Properties */
64@@ -53,6 +54,7 @@
65 PROP_OBJECT_PATH,
66 PROP_PERCENTAGE,
67 PROP_TIME,
68+ PROP_POWER_SUPPLY,
69 N_PROPERTIES
70 };
71
72@@ -116,6 +118,12 @@
73 0,
74 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
75
76+ properties[PROP_POWER_SUPPLY] = g_param_spec_boolean (INDICATOR_POWER_DEVICE_POWER_SUPPLY,
77+ "power supply",
78+ "The device's power supply",
79+ FALSE,
80+ G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
81+
82 g_object_class_install_properties (object_class, N_PROPERTIES, properties);
83 }
84
85@@ -132,6 +140,7 @@
86 priv->object_path = NULL;
87 priv->percentage = 0.0;
88 priv->time = 0;
89+ priv->power_supply = FALSE;
90
91 self->priv = priv;
92 }
93@@ -190,6 +199,10 @@
94 g_value_set_uint64 (value, (guint64)priv->time);
95 break;
96
97+ case PROP_POWER_SUPPLY:
98+ g_value_set_boolean (value, priv->power_supply);
99+ break;
100+
101 default:
102 G_OBJECT_WARN_INVALID_PROPERTY_ID(o, prop_id, pspec);
103 break;
104@@ -225,6 +238,10 @@
105 p->time = (time_t) g_value_get_uint64(value);
106 break;
107
108+ case PROP_POWER_SUPPLY:
109+ p->power_supply = g_value_get_boolean (value);
110+ break;
111+
112 default:
113 G_OBJECT_WARN_INVALID_PROPERTY_ID(o, prop_id, pspec);
114 break;
115@@ -304,6 +321,16 @@
116 return device->priv->time;
117 }
118
119+gboolean
120+indicator_power_device_get_power_supply (const IndicatorPowerDevice * device)
121+{
122+ /* LCOV_EXCL_START */
123+ g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), FALSE);
124+ /* LCOV_EXCL_STOP */
125+
126+ return device->priv->power_supply;
127+}
128+
129 /***
130 ****
131 ****
132@@ -867,7 +894,8 @@
133 UpDeviceKind kind,
134 gdouble percentage,
135 UpDeviceState state,
136- time_t timestamp)
137+ time_t timestamp,
138+ gboolean power_supply)
139 {
140 GObject * o = g_object_new (INDICATOR_POWER_DEVICE_TYPE,
141 INDICATOR_POWER_DEVICE_KIND, kind,
142@@ -875,6 +903,7 @@
143 INDICATOR_POWER_DEVICE_OBJECT_PATH, object_path,
144 INDICATOR_POWER_DEVICE_PERCENTAGE, percentage,
145 INDICATOR_POWER_DEVICE_TIME, (guint64)timestamp,
146+ INDICATOR_POWER_DEVICE_POWER_SUPPLY, power_supply,
147 NULL);
148 return INDICATOR_POWER_DEVICE(o);
149 }
150@@ -882,7 +911,7 @@
151 IndicatorPowerDevice *
152 indicator_power_device_new_from_variant (GVariant * v)
153 {
154- g_return_val_if_fail (g_variant_is_of_type (v, G_VARIANT_TYPE("(susdut)")), NULL);
155+ g_return_val_if_fail (g_variant_is_of_type (v, G_VARIANT_TYPE("(susdutb)")), NULL);
156
157 UpDeviceKind kind = UP_DEVICE_KIND_UNKNOWN;
158 UpDeviceState state = UP_DEVICE_STATE_UNKNOWN;
159@@ -890,18 +919,21 @@
160 const gchar * object_path = NULL;
161 gdouble percentage = 0;
162 guint64 time = 0;
163+ gboolean power_supply = FALSE;
164
165- g_variant_get (v, "(&su&sdut)",
166+ g_variant_get (v, "(&su&sdutb)",
167 &object_path,
168 &kind,
169 &icon,
170 &percentage,
171 &state,
172- &time);
173+ &time,
174+ &power_supply);
175
176 return indicator_power_device_new (object_path,
177 kind,
178 percentage,
179 state,
180- (time_t)time);
181+ (time_t)time,
182+ power_supply);
183 }
184
185=== modified file 'src/device.h'
186--- src/device.h 2014-07-24 20:20:16 +0000
187+++ src/device.h 2017-01-16 19:06:20 +0000
188@@ -44,6 +44,7 @@
189 #define INDICATOR_POWER_DEVICE_OBJECT_PATH "object-path"
190 #define INDICATOR_POWER_DEVICE_PERCENTAGE "percentage"
191 #define INDICATOR_POWER_DEVICE_TIME "time"
192+#define INDICATOR_POWER_DEVICE_POWER_SUPPLY "power-supply"
193
194 typedef enum
195 {
196@@ -107,7 +108,8 @@
197 UpDeviceKind kind,
198 gdouble percentage,
199 UpDeviceState state,
200- time_t time);
201+ time_t time,
202+ gboolean power_supply);
203
204 /**
205 * Convenience wrapper around indicator_power_device_new()
206@@ -121,6 +123,7 @@
207 const gchar * indicator_power_device_get_object_path (const IndicatorPowerDevice * device);
208 gdouble indicator_power_device_get_percentage (const IndicatorPowerDevice * device);
209 time_t indicator_power_device_get_time (const IndicatorPowerDevice * device);
210+gboolean indicator_power_device_get_power_supply (const IndicatorPowerDevice * device);
211
212 GStrv indicator_power_device_get_icon_names (const IndicatorPowerDevice * device);
213 GIcon * indicator_power_device_get_gicon (const IndicatorPowerDevice * device);
214
215=== modified file 'src/service.c'
216--- src/service.c 2016-05-26 18:21:09 +0000
217+++ src/service.c 2017-01-16 19:06:20 +0000
218@@ -162,11 +162,12 @@
219 }
220
221 /* sort devices from most interesting to least interesting on this criteria:
222- 1. discharging items from least time remaining until most time remaining
223- 2. charging items from most time left to charge to least time left to charge
224- 3. charging items with an unknown time remaining
225- 4. discharging items with an unknown time remaining
226- 5. batteries, then non-line power, then line-power */
227+ 1. device that supplied the power to the system
228+ 2. discharging items from least time remaining until most time remaining
229+ 3. charging items from most time left to charge to least time left to charge
230+ 4. charging items with an unknown time remaining
231+ 5. discharging items with an unknown time remaining
232+ 6. batteries, then non-line power, then line-power */
233 static gint
234 device_compare_func (gconstpointer ga, gconstpointer gb)
235 {
236@@ -174,6 +175,8 @@
237 int state;
238 const IndicatorPowerDevice * a = ga;
239 const IndicatorPowerDevice * b = gb;
240+ const gboolean a_power_supply = indicator_power_device_get_power_supply (a);
241+ const gboolean b_power_supply = indicator_power_device_get_power_supply (b);
242 const int a_state = indicator_power_device_get_state (a);
243 const int b_state = indicator_power_device_get_state (b);
244 const gdouble a_percentage = indicator_power_device_get_percentage (a);
245@@ -183,6 +186,18 @@
246
247 ret = 0;
248
249+ if (!ret && (a_power_supply != b_power_supply))
250+ {
251+ if (a_power_supply) /* a provides power to the system */
252+ {
253+ ret = -1;
254+ }
255+ else /* b provides power to the system */
256+ {
257+ ret = 1;
258+ }
259+ }
260+
261 state = UP_DEVICE_STATE_DISCHARGING;
262 if (!ret && (((a_state == state) && a_time) ||
263 ((b_state == state) && b_time)))
264@@ -1436,7 +1451,8 @@
265 UP_DEVICE_KIND_BATTERY,
266 percent,
267 state,
268- time_left);
269+ time_left,
270+ TRUE);
271 }
272
273 return device;
274
275=== modified file 'src/testing.c'
276--- src/testing.c 2014-10-14 19:50:47 +0000
277+++ src/testing.c 2017-01-16 19:06:20 +0000
278@@ -301,7 +301,8 @@
279 UP_DEVICE_KIND_BATTERY,
280 50.0,
281 UP_DEVICE_STATE_DISCHARGING,
282- 60*30);
283+ 60*30,
284+ TRUE);
285
286
287 /* Mock Provider */
288
289=== modified file 'tests/test-device.cc'
290--- tests/test-device.cc 2016-05-26 19:02:10 +0000
291+++ tests/test-device.cc 2017-01-16 19:06:20 +0000
292@@ -205,7 +205,8 @@
293 UP_DEVICE_KIND_BATTERY,
294 50.0,
295 UP_DEVICE_STATE_CHARGING,
296- 30);
297+ 30,
298+ TRUE);
299 ASSERT_TRUE (device != NULL);
300 ASSERT_TRUE (INDICATOR_IS_POWER_DEVICE(device));
301 ASSERT_EQ (UP_DEVICE_KIND_BATTERY, indicator_power_device_get_kind(device));
302@@ -213,6 +214,7 @@
303 ASSERT_STREQ ("/object/path", indicator_power_device_get_object_path(device));
304 ASSERT_EQ (50, int(indicator_power_device_get_percentage(device)));
305 ASSERT_EQ (30, indicator_power_device_get_time(device));
306+ ASSERT_TRUE (indicator_power_device_get_power_supply(device));
307
308 // cleanup
309 g_object_unref (device);
310@@ -220,13 +222,14 @@
311
312 TEST_F(DeviceTest, NewFromVariant)
313 {
314- auto variant = g_variant_new("(susdut)",
315+ auto variant = g_variant_new("(susdutb)",
316 "/object/path",
317 guint32(UP_DEVICE_KIND_BATTERY),
318 "icon",
319 50.0,
320 guint32(UP_DEVICE_STATE_CHARGING),
321- guint64(30));
322+ guint64(30),
323+ TRUE);
324 IndicatorPowerDevice * device = indicator_power_device_new_from_variant (variant);
325 ASSERT_TRUE (variant != NULL);
326 ASSERT_TRUE (device != NULL);
327@@ -236,6 +239,7 @@
328 ASSERT_STREQ ("/object/path", indicator_power_device_get_object_path(device));
329 ASSERT_EQ (50, int(indicator_power_device_get_percentage(device)));
330 ASSERT_EQ (30, indicator_power_device_get_time(device));
331+ ASSERT_TRUE (indicator_power_device_get_power_supply(device));
332
333 // cleanup
334 g_object_unref (device);
335@@ -810,7 +814,8 @@
336 << ' ' << state2str(indicator_power_device_get_state(device))
337 << ' ' << indicator_power_device_get_time(device)<<'m'
338 << ' ' << int(ceil(indicator_power_device_get_percentage(device)))<<'%'
339- << ' ' << (path ? path : "nopath");
340+ << ' ' << (path ? path : "nopath")
341+ << ' ' << (indicator_power_device_get_power_supply(device) ? "1" : "0");
342
343 return o.str();
344 }
345@@ -818,13 +823,14 @@
346 IndicatorPowerDevice* str2device(const std::string& str)
347 {
348 auto tokens = g_strsplit(str.c_str(), " ", 0);
349- g_assert(5u == g_strv_length(tokens));
350+ g_assert(6u == g_strv_length(tokens));
351 const auto kind = str2kind(tokens[0]);
352 const auto state = str2state(tokens[1]);
353 const time_t time = atoi(tokens[2]);
354 const double pct = strtod(tokens[3],nullptr);
355 const char* path = !g_strcmp0(tokens[4],"nopath") ? nullptr : tokens[4];
356- auto ret = indicator_power_device_new(path, kind, pct, state, time);
357+ const gboolean power_supply = atoi(tokens[5]);
358+ auto ret = indicator_power_device_new(path, kind, pct, state, time, power_supply);
359 g_strfreev(tokens);
360 return ret;
361 }
362@@ -847,100 +853,100 @@
363 } tests[] = {
364 {
365 "one discharging battery",
366- "battery discharging 10m 60% bat01",
367- { "battery discharging 10m 60% bat01" }
368+ "battery discharging 10m 60% bat01 1",
369+ { "battery discharging 10m 60% bat01 1" }
370 },
371 {
372 "merge two discharging batteries",
373- "battery discharging 20m 70% nopath",
374- { "battery discharging 10m 60% bat01", "battery discharging 20m 80% bat02" }
375+ "battery discharging 20m 70% nopath 1",
376+ { "battery discharging 10m 60% bat01 1", "battery discharging 20m 80% bat02 1" }
377 },
378 {
379 "merge two other discharging batteries",
380- "battery discharging 30m 90% nopath",
381- { "battery discharging 20m 80% bat01", "battery discharging 30m 100% bat02" }
382+ "battery discharging 30m 90% nopath 1",
383+ { "battery discharging 20m 80% bat01 1", "battery discharging 30m 100% bat02 1" }
384 },
385 {
386 "merge three discharging batteries",
387- "battery discharging 30m 80% nopath",
388- { "battery discharging 10m 60% bat01", "battery discharging 20m 80% bat02", "battery discharging 30m 100% bat03" }
389+ "battery discharging 30m 80% nopath 1",
390+ { "battery discharging 10m 60% bat01 1", "battery discharging 20m 80% bat02 1", "battery discharging 30m 100% bat03 1" }
391 },
392 {
393 "one charging battery",
394- "battery charging 10m 60% bat01",
395- { "battery charging 10m 60% bat01" }
396+ "battery charging 10m 60% bat01 1",
397+ { "battery charging 10m 60% bat01 1" }
398 },
399 {
400 "merge two charging batteries",
401- "battery charging 20m 70% nopath",
402- { "battery charging 10m 60% bat01", "battery charging 20m 80% bat02" }
403+ "battery charging 20m 70% nopath 1",
404+ { "battery charging 10m 60% bat01 1", "battery charging 20m 80% bat02 1" }
405 },
406 {
407 "merge two other charging batteries",
408- "battery charging 30m 90% nopath",
409- { "battery charging 20m 80% bat01", "battery charging 30m 100% bat02" }
410+ "battery charging 30m 90% nopath 1",
411+ { "battery charging 20m 80% bat01 1", "battery charging 30m 100% bat02 1" }
412 },
413 {
414 "merge three charging batteries",
415- "battery charging 30m 80% nopath",
416- { "battery charging 10m 60% bat01", "battery charging 20m 80% bat02", "battery charging 30m 100% bat03" }
417+ "battery charging 30m 80% nopath 1",
418+ { "battery charging 10m 60% bat01 1", "battery charging 20m 80% bat02 1", "battery charging 30m 100% bat03 1" }
419 },
420 {
421 "one charged battery",
422- "battery charged 0m 100% bat01",
423- { "battery charged 0m 100% bat01" }
424+ "battery charged 0m 100% bat01 1",
425+ { "battery charged 0m 100% bat01 1" }
426 },
427 {
428 "merge one charged, one discharging",
429- "battery discharging 10m 80% nopath",
430- { "battery charged 0m 100% bat01", "battery discharging 10m 60% bat02" }
431+ "battery discharging 10m 80% nopath 1",
432+ { "battery charged 0m 100% bat01 1", "battery discharging 10m 60% bat02 1" }
433 },
434 {
435 "merged one charged, one charging",
436- "battery charging 10m 80% nopath",
437- { "battery charged 0m 100% bat01", "battery charging 10m 60% bat02" }
438+ "battery charging 10m 80% nopath 1",
439+ { "battery charged 0m 100% bat01 1", "battery charging 10m 60% bat02 1" }
440 },
441 {
442 "merged one charged, one charging, one discharging",
443- "battery discharging 10m 74% nopath",
444- { "battery charged 0m 100% bat01", "battery charging 10m 60% bat02", "battery discharging 10m 60% bat03" }
445- },
446- {
447- "one discharging mouse and one discharging battery. pick the one with the least time left",
448- "battery discharging 10m 60% bat01",
449- { "battery discharging 10m 60% bat01", "mouse discharging 20m 80% mouse01" }
450- },
451- {
452- "one discharging mouse and a different discharging battery. pick the one with the least time left",
453- "mouse discharging 20m 80% mouse01",
454- { "battery discharging 30m 100% bat01", "mouse discharging 20m 80% mouse01" }
455+ "battery discharging 10m 74% nopath 1",
456+ { "battery charged 0m 100% bat01 1", "battery charging 10m 60% bat02 1", "battery discharging 10m 60% bat03 1" }
457+ },
458+ {
459+ "one discharging mouse and one discharging battery. ignore mouse because it doesn't supply the power",
460+ "battery discharging 10m 60% bat01 1",
461+ { "battery discharging 10m 60% bat01 1", "mouse discharging 20m 80% mouse01 0" }
462+ },
463+ {
464+ "one discharging mouse and a different discharging battery. ignore mouse because it doesn't supply the power",
465+ "battery discharging 30m 100% bat01 1",
466+ { "battery discharging 30m 100% bat01 1", "mouse discharging 20m 80% mouse01 0" }
467 },
468 {
469 "everything comes before power lines #1",
470- "battery discharging 10m 60% bat01",
471- { "battery discharging 10m 60% bat01", "line-power unknown 0m 0% lp01" }
472- },
473- {
474- "everything comes before power lines #2",
475- "battery charging 10m 60% bat01",
476- { "battery charging 10m 60% bat01", "line-power unknown 0m 0% lp01" }
477- },
478- {
479- "everything comes before power lines #2",
480- "mouse discharging 20m 80% mouse01",
481- { "mouse discharging 20m 80% mouse01", "line-power unknown 0m 0% lp01" }
482+ "battery discharging 10m 60% bat01 1",
483+ { "battery discharging 10m 60% bat01 1", "line-power unknown 0m 0% lp01 1" }
484+ },
485+ {
486+ "everything comes before power lines #2",
487+ "battery charging 10m 60% bat01 1",
488+ { "battery charging 10m 60% bat01 1", "line-power unknown 0m 0% lp01 1" }
489+ },
490+ {
491+ "everything comes before power lines #3 except that the mouse doesn't supply the power",
492+ "line-power unknown 0m 0% lp01 1",
493+ { "mouse discharging 20m 80% mouse01 0", "line-power unknown 0m 0% lp01 1" }
494 },
495 {
496 // https://bugs.launchpad.net/ubuntu/+source/indicator-power/+bug/1470080/comments/10
497 "don't select a device with unknown state when we have another device with a known state...",
498- "battery charged 0m 100% bat01",
499- { "battery charged 0m 100% bat01", "phone unknown 0m 61% phone01" }
500+ "battery charged 0m 100% bat01 1",
501+ { "battery charged 0m 100% bat01 1", "phone unknown 0m 61% phone01 1" }
502 },
503 {
504 // https://bugs.launchpad.net/ubuntu/+source/indicator-power/+bug/1470080/comments/10
505 "...but do select the unknown state device if nothing else is available",
506- "phone unknown 0m 61% phone01",
507- { "phone unknown 0m 61% phone01" }
508+ "phone unknown 0m 61% phone01 1",
509+ { "phone unknown 0m 61% phone01 1" }
510 }
511 };
512
513
514=== modified file 'tests/test-notify.cc'
515--- tests/test-notify.cc 2016-05-16 17:59:03 +0000
516+++ tests/test-notify.cc 2017-01-16 19:06:20 +0000
517@@ -235,7 +235,8 @@
518 UP_DEVICE_KIND_BATTERY,
519 50.0,
520 UP_DEVICE_STATE_DISCHARGING,
521- 30);
522+ 30,
523+ TRUE);
524
525 // confirm that the power levels trigger at the right percentages
526 for (int i=100; i>=0; --i)
527@@ -313,7 +314,8 @@
528 UP_DEVICE_KIND_BATTERY,
529 50.0,
530 UP_DEVICE_STATE_DISCHARGING,
531- 30);
532+ 30,
533+ TRUE);
534
535 // set up a notifier and give it the battery so changing the battery's
536 // charge should show up on the bus.
537@@ -385,7 +387,8 @@
538 UP_DEVICE_KIND_BATTERY,
539 percent_low + 1.0,
540 UP_DEVICE_STATE_DISCHARGING,
541- 30);
542+ 30,
543+ TRUE);
544
545 // the file we expect to play on a low battery notification...
546 const char* expected_file = XDG_DATA_HOME "/" GETTEXT_PACKAGE "/sounds/" LOW_BATTERY_SOUND;

Subscribers

People subscribed via source and target branches