Merge lp:~seb128/ubuntu-system-settings/battery-full-charge-info into lp:ubuntu-system-settings

Proposed by Sebastien Bacher
Status: Merged
Approved by: Iain Lane
Approved revision: 264
Merged at revision: 264
Proposed branch: lp:~seb128/ubuntu-system-settings/battery-full-charge-info
Merge into: lp:ubuntu-system-settings
Diff against target: 407 lines (+163/-40)
4 files modified
plugins/battery/PageComponent.qml (+33/-7)
plugins/battery/battery.cpp (+56/-12)
plugins/battery/battery.h (+14/-1)
po/ubuntu-system-settings.pot (+60/-20)
To merge this branch: bzr merge lp:~seb128/ubuntu-system-settings/battery-full-charge-info
Reviewer Review Type Date Requested Status
Iain Lane Approve
PS Jenkins bot continuous-integration Approve
Ken VanDine Pending
Review via email: mp+180146@code.launchpad.net

Commit message

battery: display the last full charge

Description of the change

battery: display the last full charge

Some comments for the reviewer(s):

- in timeDeltaString(): I'm not sure how to/if we can do plurial in i18n.tr() (we have a 2 arguments format already which is used to specify a domain)

- the isCharging property is needed because chargeState is not a property (it takes a battery number as parameter) so it doesn't get dynamic update

- changing the line to be a bit thicker, that works better on the device

- I'm keeping the QDebug include while working on the panel at least, it's annoying to comment it/put it back for every commit

- getLastFullCharge() iterates on an higher timerange than getHistory

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Iain Lane (laney) wrote :

Hey, looks mostly good. It seems to be correctly dynamic which is great to see.

I agree it's not the most beautiful but I don't have ideas about how to fix the plural stuff.

Couple of comments:

        if (timeDelta > 3600 && timeDelta < 86400)
            return i18n.tr("%1 hours ago").arg(Math.round(timeDelta/3600))

If timedelta is 86399 then this will show "24 hours ago" instead of "1 day ago" which you want, won't it?

And similarly for Math.round(3601/3600) = 1 → "1 hours ago"?

I guess this algorithm can be improved by splitting the rounding and display logic.

158 + if (up_history_item_get_state(item) == UP_DEVICE_STATE_FULLY_CHARGED) {
159 + m_lastFullCharge = (int)(((gint32) up_history_item_get_time(item) - offset)*-1);
160 + Q_EMIT(lastFullChargeChanged());
161 + return;

What's the -1 for (comment it) and also can the repeated copies of this code be factored out?

review: Needs Fixing
Revision history for this message
Sebastien Bacher (seb128) wrote :

Thanks for the review

> I guess this algorithm can be improved by splitting the rounding and display logic.

working on that

> What's the -1 for (comment it)

doing that

> can the repeated copies of this code be factored out?

you are speaking about the loop iterating on the charge datas I guess? Not easily, but I guess I can add a parameter to the function, make the code in the middle do if/else and call different functions

260. By Sebastien Bacher

better computer the time delta labels

261. By Sebastien Bacher

don't work with negative offsets, easier to read this way

262. By Sebastien Bacher

use a new function for the lastfullcharge update

263. By Sebastien Bacher

don't leak values

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
264. By Sebastien Bacher

use the correct rounding values

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Iain Lane (laney) wrote :

OK, it'd be good to comment what those numbers are but seems good

thanks!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'plugins/battery/PageComponent.qml'
2--- plugins/battery/PageComponent.qml 2013-08-12 17:39:21 +0000
3+++ plugins/battery/PageComponent.qml 2013-08-20 14:54:26 +0000
4@@ -32,6 +32,27 @@
5 title: i18n.tr("Battery")
6 flickable: scrollWidget
7
8+ property bool isCharging
9+
10+ function timeDeltaString(timeDelta) {
11+ if (timeDelta === 1)
12+ return i18n.tr("1 second ago")
13+ if (timeDelta < 60)
14+ return i18n.tr("%1 seconds ago").arg(timeDelta)
15+ if (timeDelta >= 60 && timeDelta < 90)
16+ return i18n.tr("1 minute ago")
17+ if (timeDelta >= 90 && timeDelta < 3570)
18+ return i18n.tr("%1 minutes ago").arg(Math.round(timeDelta/60))
19+ if (timeDelta >= 3570 && timeDelta < 5400)
20+ return i18n.tr("1 hour ago")
21+ if (timeDelta >= 5400 && timeDelta < 84600)
22+ return i18n.tr("%1 hours ago").arg(Math.round(timeDelta/3600))
23+ if (timeDelta >= 84600 && timeDelta < 129600)
24+ return i18n.tr("1 day ago")
25+ if (timeDelta >= 129600)
26+ return i18n.tr("%1 days ago").arg(Math.round(timeDelta/86400))
27+ }
28+
29 GSettings {
30 id: powerSettings
31 schema.id: batteryBackend.powerdRunning ? "com.canonical.powerd" : "org.gnome.settings-daemon.plugins.power"
32@@ -46,12 +67,15 @@
33 onChargingStateChanged: {
34 if (state === BatteryInfo.Charging) {
35 chargingEntry.text = i18n.tr("Charging now")
36- chargingEntry.value = ""
37-
38+ isCharging = true
39 }
40- else {
41+ else if (state === BatteryInfo.Discharging) {
42 chargingEntry.text = i18n.tr("Last full charge")
43- chargingEntry.value = i18n.tr("N/A") // TODO: find a way to get that information
44+ isCharging = false
45+ }
46+ else if (state === BatteryInfo.Full || state === BatteryInfo.NotCharging) {
47+ chargingEntry.text = i18n.tr("Fully charged")
48+ isCharging = true
49 }
50 }
51 onRemainingCapacityChanged: {
52@@ -88,6 +112,8 @@
53
54 ListItem.SingleValue {
55 id: chargingEntry
56+ value: isCharging ?
57+ "" : (batteryBackend.lastFullCharge ? timeDeltaString(batteryBackend.lastFullCharge) : i18n.tr("N/A"))
58 showDivider: false
59 }
60
61@@ -112,7 +138,7 @@
62 ctx.stroke()
63
64 /* Display the charge history in orange color */
65- ctx.lineWidth = units.dp(1)
66+ ctx.lineWidth = units.dp(2)
67 ctx.strokeStyle = UbuntuColors.orange
68
69 /* Get infos from battery0, on a day (60*24*24=86400 seconds), with 150 points on the graph */
70@@ -121,9 +147,9 @@
71 /* time is the offset in seconds compared to the current time (negative value)
72 we display the charge on a day, which is 86400 seconds, the value is the %
73 the coordinates are adjusted to the canvas, top,left is 0,0 */
74- ctx.moveTo((86400+chargeDatas[0].time)/86400*canvas.width, (1-chargeDatas[0].value/100)*canvas.height)
75+ ctx.moveTo((86400-chargeDatas[0].time)/86400*canvas.width, (1-chargeDatas[0].value/100)*canvas.height)
76 for (var i=1; i < chargeDatas.length; i++) {
77- ctx.lineTo((86400+chargeDatas[i].time)/86400*canvas.width, (1-chargeDatas[i].value/100)*canvas.height)
78+ ctx.lineTo((86400-chargeDatas[i].time)/86400*canvas.width, (1-chargeDatas[i].value/100)*canvas.height)
79 }
80 ctx.stroke()
81 ctx.restore();
82
83=== modified file 'plugins/battery/battery.cpp'
84--- plugins/battery/battery.cpp 2013-08-12 20:01:37 +0000
85+++ plugins/battery/battery.cpp 2013-08-20 14:54:26 +0000
86@@ -23,7 +23,7 @@
87 #include <libupower-glib/upower.h>
88 #include <QEvent>
89 #include <QDBusReply>
90-#include <QPair>
91+#include <QtCore/QDebug>
92
93 Battery::Battery(QObject *parent) :
94 QObject(parent),
95@@ -31,10 +31,14 @@
96 m_powerdIface ("com.canonical.powerd",
97 "/com/canonical/powerd",
98 "com.canonical.powerd",
99- m_systemBusConnection)
100+ m_systemBusConnection),
101+ m_deviceString("")
102 {
103 m_device = up_device_new();
104
105+ buildDeviceString();
106+ getLastFullCharge();
107+
108 if (!m_powerdIface.isValid()) {
109 m_powerdRunning = false;
110 return;
111@@ -48,7 +52,7 @@
112 return m_powerdRunning;
113 }
114
115-QString Battery::deviceString() {
116+void Battery::buildDeviceString() {
117 UpClient *client;
118 gboolean returnIsOk;
119 GPtrArray *devices;
120@@ -59,7 +63,7 @@
121 returnIsOk = up_client_enumerate_devices_sync(client, NULL, NULL);
122
123 if(!returnIsOk)
124- return QString("");
125+ return;
126
127 devices = up_client_get_devices(client);
128
129@@ -67,20 +71,58 @@
130 device = (UpDevice *)g_ptr_array_index(devices, i);
131 g_object_get(device, "kind", &kind, NULL);
132 if (kind == UP_DEVICE_KIND_BATTERY) {
133- QString deviceId(up_device_get_object_path(device));
134- g_ptr_array_unref(devices);
135- g_object_unref(client);
136- return deviceId;
137+ m_deviceString = QString(up_device_get_object_path(device));
138 }
139 }
140
141 g_ptr_array_unref(devices);
142 g_object_unref(client);
143- return QString("");
144+}
145+
146+QString Battery::deviceString()
147+{
148+ return m_deviceString;
149+}
150+
151+int Battery::lastFullCharge()
152+{
153+ return m_lastFullCharge;
154+}
155+
156+bool Battery::updateLastFullCharge(UpHistoryItem *item, int offset)
157+{
158+ if (up_history_item_get_state(item) == UP_DEVICE_STATE_FULLY_CHARGED) {
159+ m_lastFullCharge = (int)((offset - (gint32) up_history_item_get_time(item)));
160+ Q_EMIT(lastFullChargeChanged());
161+ return true; /* return true if the charge value got updated */
162+ }
163+ return false;
164+}
165+
166+void Battery::getLastFullCharge()
167+{
168+ UpHistoryItem *item;
169+ GPtrArray *values;
170+ gint32 offset = 0;
171+ GTimeVal timeval;
172+
173+ g_get_current_time(&timeval);
174+ offset = timeval.tv_sec;
175+ up_device_set_object_path_sync(m_device, m_deviceString.toStdString().c_str(), NULL, NULL);
176+ values = up_device_get_history_sync(m_device, "charge", 864000, 1000, NULL, NULL);
177+ for (uint i=values->len-1; i > 0; i--) {
178+ item = (UpHistoryItem *) g_ptr_array_index(values, i);
179+
180+ if (updateLastFullCharge(item, offset) == true) {
181+ g_ptr_array_unref (values);
182+ return;
183+ }
184+ }
185+ g_ptr_array_unref (values);
186 }
187
188 /* TODO: refresh values over time for dynamic update */
189-QVariantList Battery::getHistory(const QString &deviceString, const int timespan, const int resolution) const
190+QVariantList Battery::getHistory(const QString &deviceString, const int timespan, const int resolution)
191 {
192 UpHistoryItem *item;
193 GPtrArray *values;
194@@ -99,11 +141,13 @@
195 if (up_history_item_get_state(item) == UP_DEVICE_STATE_UNKNOWN)
196 continue;
197
198- listItem.insert("time",((gint32) up_history_item_get_time(item) - offset));
199+ updateLastFullCharge(item, offset);
200+
201+ listItem.insert("time",(offset - (gint32) up_history_item_get_time(item)));
202 listItem.insert("value",up_history_item_get_value(item));
203 listValues += listItem;
204 }
205-
206+ g_ptr_array_unref (values);
207 return listValues;
208 }
209
210
211=== modified file 'plugins/battery/battery.h'
212--- plugins/battery/battery.h 2013-08-12 19:59:57 +0000
213+++ plugins/battery/battery.h 2013-08-20 14:54:26 +0000
214@@ -38,12 +38,20 @@
215 READ deviceString
216 CONSTANT)
217
218+ Q_PROPERTY( int lastFullCharge
219+ READ lastFullCharge
220+ NOTIFY lastFullChargeChanged)
221+
222 public:
223 explicit Battery(QObject *parent = 0);
224 ~Battery();
225 bool powerdRunning();
226 QString deviceString();
227- Q_INVOKABLE QVariantList getHistory(const QString &deviceString, const int timespan, const int resolution) const;
228+ int lastFullCharge();
229+ Q_INVOKABLE QVariantList getHistory(const QString &deviceString, const int timespan, const int resolution);
230+
231+Q_SIGNALS:
232+ void lastFullChargeChanged();
233
234 private:
235 QDBusConnection m_systemBusConnection;
236@@ -51,6 +59,11 @@
237 QDBusInterface m_powerdIface;
238 bool m_powerdRunning;
239 UpDevice *m_device;
240+ QString m_deviceString;
241+ int m_lastFullCharge;
242+ void buildDeviceString();
243+ void getLastFullCharge();
244+ bool updateLastFullCharge(UpHistoryItem *item, int offset);
245 };
246
247 #endif // BATTERY_H
248
249=== modified file 'po/ubuntu-system-settings.pot'
250--- po/ubuntu-system-settings.pot 2013-08-13 13:24:21 +0000
251+++ po/ubuntu-system-settings.pot 2013-08-20 14:54:26 +0000
252@@ -8,7 +8,7 @@
253 msgstr ""
254 "Project-Id-Version: ubuntu-system-settings\n"
255 "Report-Msgid-Bugs-To: \n"
256-"POT-Creation-Date: 2013-08-13 09:22-0400\n"
257+"POT-Creation-Date: 2013-08-14 15:54+0200\n"
258 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
259 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
260 "Language-Team: LANGUAGE <LL@li.org>\n"
261@@ -26,8 +26,8 @@
262 msgstr ""
263
264 #: ../plugins/about/PageComponent.qml:77 ../plugins/about/PageComponent.qml:84
265-#: ../plugins/battery/PageComponent.qml:54
266-#: ../plugins/battery/PageComponent.qml:85
267+#: ../plugins/battery/PageComponent.qml:109
268+#: ../plugins/battery/PageComponent.qml:116
269 #: ../plugins/cellular/PageComponent.qml:75
270 msgid "N/A"
271 msgstr ""
272@@ -45,8 +45,8 @@
273 msgstr ""
274
275 #: ../plugins/about/PageComponent.qml:98
276-#: ../plugins/battery/PageComponent.qml:180
277-#: ../plugins/battery/PageComponent.qml:185
278+#: ../plugins/battery/PageComponent.qml:206
279+#: ../plugins/battery/PageComponent.qml:211
280 #: ../plugins/battery/SleepValues.qml:76
281 msgid "Never"
282 msgstr ""
283@@ -153,38 +153,78 @@
284 msgid "Battery"
285 msgstr ""
286
287-#: ../plugins/battery/PageComponent.qml:48
288+#: ../plugins/battery/PageComponent.qml:39
289+msgid "1 second ago"
290+msgstr ""
291+
292+#: ../plugins/battery/PageComponent.qml:41
293+#, qt-format
294+msgid "%1 seconds ago"
295+msgstr ""
296+
297+#: ../plugins/battery/PageComponent.qml:43
298+msgid "1 minute ago"
299+msgstr ""
300+
301+#: ../plugins/battery/PageComponent.qml:45
302+#, qt-format
303+msgid "%1 minutes ago"
304+msgstr ""
305+
306+#: ../plugins/battery/PageComponent.qml:47
307+msgid "1 hour ago"
308+msgstr ""
309+
310+#: ../plugins/battery/PageComponent.qml:49
311+#, qt-format
312+msgid "%1 hours ago"
313+msgstr ""
314+
315+#: ../plugins/battery/PageComponent.qml:51
316+msgid "1 day ago"
317+msgstr ""
318+
319+#: ../plugins/battery/PageComponent.qml:53
320+#, qt-format
321+msgid "%1 days ago"
322+msgstr ""
323+
324+#: ../plugins/battery/PageComponent.qml:69
325 msgid "Charging now"
326 msgstr ""
327
328-#: ../plugins/battery/PageComponent.qml:53
329+#: ../plugins/battery/PageComponent.qml:73
330 msgid "Last full charge"
331 msgstr ""
332
333-#: ../plugins/battery/PageComponent.qml:59
334+#: ../plugins/battery/PageComponent.qml:77
335+msgid "Fully charged"
336+msgstr ""
337+
338+#: ../plugins/battery/PageComponent.qml:83
339 #, qt-format
340 msgid "%1 %"
341 msgstr ""
342
343-#: ../plugins/battery/PageComponent.qml:84
344+#: ../plugins/battery/PageComponent.qml:108
345 msgid "Charge level"
346 msgstr ""
347
348-#: ../plugins/battery/PageComponent.qml:134
349+#: ../plugins/battery/PageComponent.qml:160
350 msgid "Ways to reduce battery use:"
351 msgstr ""
352
353-#: ../plugins/battery/PageComponent.qml:138
354+#: ../plugins/battery/PageComponent.qml:164
355 msgid "Display brightness"
356 msgstr ""
357
358-#: ../plugins/battery/PageComponent.qml:175
359+#: ../plugins/battery/PageComponent.qml:201
360 #: ../plugins/battery/SleepValues.qml:31
361 msgid "Auto sleep"
362 msgstr ""
363
364-#: ../plugins/battery/PageComponent.qml:179
365-#: ../plugins/battery/PageComponent.qml:184
366+#: ../plugins/battery/PageComponent.qml:205
367+#: ../plugins/battery/PageComponent.qml:210
368 #: ../plugins/battery/SleepValues.qml:71 ../plugins/battery/SleepValues.qml:72
369 #: ../plugins/battery/SleepValues.qml:73 ../plugins/battery/SleepValues.qml:74
370 #: ../plugins/battery/SleepValues.qml:75
371@@ -192,19 +232,19 @@
372 msgid "After %1 minutes"
373 msgstr ""
374
375-#: ../plugins/battery/PageComponent.qml:192 ../.build/settings.js:19
376+#: ../plugins/battery/PageComponent.qml:218 ../.build/settings.js:19
377 msgid "Wi-Fi"
378 msgstr ""
379
380-#: ../plugins/battery/PageComponent.qml:200 ../.build/settings.js:5
381+#: ../plugins/battery/PageComponent.qml:226 ../.build/settings.js:5
382 msgid "Bluetooth"
383 msgstr ""
384
385-#: ../plugins/battery/PageComponent.qml:208 ../.build/settings.js:10
386+#: ../plugins/battery/PageComponent.qml:234 ../.build/settings.js:10
387 msgid "GPS"
388 msgstr ""
389
390-#: ../plugins/battery/PageComponent.qml:216
391+#: ../plugins/battery/PageComponent.qml:242
392 msgid "Accurate location detection requires GPS and/or Wi-Fi."
393 msgstr ""
394
395@@ -221,11 +261,11 @@
396 msgid "Carrier"
397 msgstr ""
398
399-#: ../plugins/cellular/ChooseCarrier.qml:83
400+#: ../plugins/cellular/ChooseCarrier.qml:80
401 msgid "Refresh"
402 msgstr ""
403
404-#: ../plugins/cellular/ChooseCarrier.qml:100
405+#: ../plugins/cellular/ChooseCarrier.qml:97
406 msgid "Searching"
407 msgstr ""
408

Subscribers

People subscribed via source and target branches