Merge lp:~aacid/ubuntu-ui-toolkit/LiveTimer into lp:ubuntu-ui-toolkit/staging

Proposed by Albert Astals Cid
Status: Merged
Approved by: Zsombor Egri
Approved revision: 1607
Merged at revision: 1603
Proposed branch: lp:~aacid/ubuntu-ui-toolkit/LiveTimer
Merge into: lp:ubuntu-ui-toolkit/staging
Diff against target: 702 lines (+624/-2)
9 files modified
components.api (+10/-0)
src/Ubuntu/Components/plugin/livetimer.cpp (+142/-0)
src/Ubuntu/Components/plugin/livetimer.h (+68/-0)
src/Ubuntu/Components/plugin/livetimer_p.cpp (+169/-0)
src/Ubuntu/Components/plugin/livetimer_p.h (+58/-0)
src/Ubuntu/Components/plugin/plugin.cpp (+2/-0)
src/Ubuntu/Components/plugin/plugin.pri (+7/-2)
src/Ubuntu/Components/plugin/timeutils_p.h (+117/-0)
tests/unit/tst_components/tst_livetimer.qml (+51/-0)
To merge this branch: bzr merge lp:~aacid/ubuntu-ui-toolkit/LiveTimer
Reviewer Review Type Date Requested Status
PS Jenkins bot continuous-integration Approve
Zsombor Egri Approve
Review via email: mp+267784@code.launchpad.net

Commit message

Added LiveTimer

Description of the change

Added LiveTimer

To post a comment you must log in.
1603. By Nick Dedekind

Added LiveTimer

1604. By Nick Dedekind

added test for LiveTimer

1605. By Nick Dedekind

updated api

1606. By Nick Dedekind

Document LiveTimer

1607. By Nick Dedekind

Fixed docs

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Albert Astals Cid (aacid) wrote :
Revision history for this message
Zsombor Egri (zsombi) wrote :

Was reviewed before, so let's land it.

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

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'components.api'
2--- components.api 2015-08-11 13:02:37 +0000
3+++ components.api 2015-08-12 09:56:54 +0000
4@@ -489,6 +489,16 @@
5 function swipeEvent(SwipeEvent event)
6 function rebound()
7 property Animation snapAnimation
8+Ubuntu.Components.LiveTimer 1.3: QtObject
9+ property Frequency frequency
10+ signal trigger()
11+ property QDateTime relativeTime
12+Ubuntu.Components.LiveTimer.Frequency: Enum
13+ Disabled
14+ Hour
15+ Minute
16+ Relative
17+ Second
18 Ubuntu.Components.MainView 1.0 0.1: MainViewBase
19 property bool automaticOrientation
20 default readonly property QtObject contentsItem
21
22=== added file 'src/Ubuntu/Components/plugin/livetimer.cpp'
23--- src/Ubuntu/Components/plugin/livetimer.cpp 1970-01-01 00:00:00 +0000
24+++ src/Ubuntu/Components/plugin/livetimer.cpp 2015-08-12 09:56:54 +0000
25@@ -0,0 +1,142 @@
26+/*
27+ * Copyright 2015 Canonical Ltd.
28+ *
29+ * This program is free software; you can redistribute it and/or modify
30+ * it under the terms of the GNU General Public License as published by
31+ * the Free Software Foundation; version 3.
32+ *
33+ * This program is distributed in the hope that it will be useful,
34+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
35+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36+ * GNU General Public License for more details.
37+ *
38+ * You should have received a copy of the GNU General Public License
39+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
40+ */
41+
42+#include "livetimer.h"
43+#include "livetimer_p.h"
44+
45+/*! \qmltype LiveTimer
46+ \instantiates LiveTimer
47+ \inqmlmodule Ubuntu.Components 1.3
48+ \ingroup ubuntu
49+ \since Ubuntu.Components 1.3
50+ \brief A live timing source providing peridioc updates.
51+
52+ The LiveTimer is a source for periodic signals triggered on second/minute/hour boundaries. The
53+ timer can also be set up to provide signals with an increasing frequency the closer a given time
54+ is to current time.
55+
56+ Examples:
57+
58+ \qml
59+ import Ubuntu.Components 1.3
60+
61+ LiveTimer {
62+ frequency: LiveTimer.Second
63+ onTrigger: console.log("The time is now", new Date().toString());
64+ }
65+ \endqml
66+
67+ \qml
68+ import Ubuntu.Components 1.3
69+
70+ LiveTimer {
71+ frequency: LiveTimer.Relative
72+ relativeTime: new Date()
73+ }
74+ \endqml
75+*/
76+LiveTimer::LiveTimer(QObject *parent)
77+ : QObject(parent)
78+ , m_frequency(Disabled)
79+ , m_effectiveFrequency(Disabled)
80+ , m_lastUpdate(0)
81+{
82+}
83+
84+LiveTimer::~LiveTimer()
85+{
86+ unregisterTimer();
87+}
88+
89+/*!
90+ * \qmlsignal LiveTimer::trigger()
91+ * Signal called when the timer is triggered
92+ */
93+
94+/*! \qmlproperty enumeration LiveTimer::frequency
95+ \since Ubuntu.Components 1.3
96+
97+ This properties defines the frequency at which the LiveTimer signals notifications.
98+
99+ \list
100+ \li \b LiveTimer.Disabled - disable the LiveTimer \l trigger signal
101+ \li \b LiveTimer.Second - emit the \l trigger signal on every change of second.
102+ \li \b LiveTimer.Minute - emit the \l trigger signal on every change of minute.
103+ \li \b LiveTimer.Hour - emit the \l trigger signal on every change of hour.
104+ \li \b LiveTimer.Relative - emit the \l trigger signal periodically depending on how close current time is to to
105+ \l relativeTime. If \l relativeTime is within 30 seconds of the current time, trigger every 30 seconds. Within an hour,
106+ trigger every minute. Otherwise, trigger every hour until the relative time is more than a week past current time, after which
107+ updates are disabled.
108+
109+ \note Setting the frequency to LiveTimer.Relative will disable the timer until a \l relativeTime is set.
110+
111+ \endlist
112+*/
113+void LiveTimer::setFrequency(LiveTimer::Frequency frequency)
114+{
115+ if (m_frequency != frequency) {
116+ m_frequency = frequency;
117+ Q_EMIT frequencyChanged();
118+
119+ if (m_frequency != Disabled && (m_frequency != Relative || m_relativeTime.isValid())) {
120+ registerTimer();
121+ } else {
122+ unregisterTimer();
123+ }
124+ }
125+}
126+
127+/*! \qmlproperty datetime LiveTimer::relativeTime
128+ \since Ubuntu.Components 1.3
129+
130+ This properties defines the value used for proximity evaluation when using Relative mode.
131+
132+ \note This property has no impact unless the \l frequency is set to LiveTimer.Relative
133+*/
134+void LiveTimer::setRelativeTime(const QDateTime &relativeTime)
135+{
136+ if (m_relativeTime != relativeTime) {
137+ m_relativeTime = relativeTime;
138+ Q_EMIT relativeTimeChanged();
139+
140+ if (m_frequency == Relative) {
141+ if (m_relativeTime.isValid()) {
142+ registerTimer();
143+ } else {
144+ unregisterTimer();
145+ }
146+ }
147+ }
148+}
149+
150+void LiveTimer::registerTimer()
151+{
152+ SharedLiveTimer::instance().registerTimer(this);
153+
154+ QObject::connect(&SharedLiveTimer::instance(), &SharedLiveTimer::trigger, this, &LiveTimer::trigger);
155+}
156+
157+void LiveTimer::unregisterTimer()
158+{
159+ QObject::disconnect(&SharedLiveTimer::instance(), &SharedLiveTimer::trigger, this, 0);
160+ SharedLiveTimer::instance().unregisterTimer(this);
161+
162+}
163+
164+void LiveTimer::setEffectiveFrequency(LiveTimer::Frequency frequency)
165+{
166+ m_effectiveFrequency = frequency;
167+}
168
169=== added file 'src/Ubuntu/Components/plugin/livetimer.h'
170--- src/Ubuntu/Components/plugin/livetimer.h 1970-01-01 00:00:00 +0000
171+++ src/Ubuntu/Components/plugin/livetimer.h 2015-08-12 09:56:54 +0000
172@@ -0,0 +1,68 @@
173+/*
174+ * Copyright 2015 Canonical Ltd.
175+ *
176+ * This program is free software; you can redistribute it and/or modify
177+ * it under the terms of the GNU General Public License as published by
178+ * the Free Software Foundation; version 3.
179+ *
180+ * This program is distributed in the hope that it will be useful,
181+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
182+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
183+ * GNU General Public License for more details.
184+ *
185+ * You should have received a copy of the GNU General Public License
186+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
187+ */
188+
189+#ifndef LIVETIMER_H
190+#define LIVETIMER_H
191+
192+#include <QObject>
193+#include <QDateTime>
194+
195+class LiveTimer : public QObject
196+{
197+ Q_OBJECT
198+ Q_ENUMS(Frequency)
199+ Q_PROPERTY(Frequency frequency READ frequency WRITE setFrequency NOTIFY frequencyChanged)
200+ Q_PROPERTY(QDateTime relativeTime READ relativeTime WRITE setRelativeTime NOTIFY relativeTimeChanged)
201+public:
202+ explicit LiveTimer(QObject *parent = 0);
203+ ~LiveTimer();
204+
205+ enum Frequency {
206+ Disabled = 0,
207+ Second = 1,
208+ Minute = 2,
209+ Hour = 3,
210+ Relative = 4
211+ };
212+
213+ Frequency frequency() const { return m_frequency; }
214+ void setFrequency(Frequency frequency);
215+
216+ QDateTime relativeTime() const { return m_relativeTime; }
217+ void setRelativeTime(const QDateTime& relativeTime);
218+
219+ Frequency effectiveFrequency() const { return m_effectiveFrequency; }
220+
221+Q_SIGNALS:
222+ void frequencyChanged();
223+ void relativeTimeChanged();
224+
225+ void trigger();
226+
227+private:
228+ void registerTimer();
229+ void unregisterTimer();
230+ void setEffectiveFrequency(Frequency frequency);
231+
232+ Frequency m_frequency;
233+ Frequency m_effectiveFrequency;
234+ QDateTime m_relativeTime;
235+ quint64 m_lastUpdate;
236+
237+ friend class SharedLiveTimer;
238+};
239+
240+#endif // LIVETIMER_H
241
242=== added file 'src/Ubuntu/Components/plugin/livetimer_p.cpp'
243--- src/Ubuntu/Components/plugin/livetimer_p.cpp 1970-01-01 00:00:00 +0000
244+++ src/Ubuntu/Components/plugin/livetimer_p.cpp 2015-08-12 09:56:54 +0000
245@@ -0,0 +1,169 @@
246+/*
247+ * Copyright 2015 Canonical Ltd.
248+ *
249+ * This program is free software; you can redistribute it and/or modify
250+ * it under the terms of the GNU General Public License as published by
251+ * the Free Software Foundation; version 3.
252+ *
253+ * This program is distributed in the hope that it will be useful,
254+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
255+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
256+ * GNU General Public License for more details.
257+ *
258+ * You should have received a copy of the GNU General Public License
259+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
260+ */
261+
262+#include "livetimer_p.h"
263+#include "timeutils_p.h"
264+
265+#include <QDBusConnection>
266+
267+SharedLiveTimer::SharedLiveTimer(QObject* parent)
268+ : QObject(parent)
269+ , m_frequency(LiveTimer::Disabled)
270+{
271+ m_timer.setSingleShot(true);
272+ connect(&m_timer, &QTimer::timeout, this, &SharedLiveTimer::timeout);
273+
274+ QDBusConnection::systemBus().connect("org.freedesktop.timedate1",
275+ "/org/freedesktop/timedate1",
276+ "org.freedesktop.DBus.Properties",
277+ "PropertiesChanged",
278+ this,
279+ SLOT(timedate1PropertiesChanged(QString, QVariantMap, QStringList)));
280+}
281+
282+void SharedLiveTimer::registerTimer(LiveTimer *timer)
283+{
284+ if (m_liveTimers.contains(timer)) {
285+ unregisterTimer(timer);
286+ }
287+ m_liveTimers.append(timer);
288+ updateFrequency();
289+}
290+
291+void SharedLiveTimer::unregisterTimer(LiveTimer *timer)
292+{
293+ if (!m_liveTimers.contains(timer)) return;
294+
295+ m_liveTimers.removeAll(timer);
296+ updateFrequency();
297+}
298+
299+void SharedLiveTimer::updateFrequency()
300+{
301+ LiveTimer::Frequency newFreq = LiveTimer::Disabled;
302+ if (m_liveTimers.count() == 0) {
303+ newFreq = LiveTimer::Disabled;
304+ } else {
305+ Q_FOREACH(LiveTimer* timer, m_liveTimers) {
306+ LiveTimer::Frequency freq = timer->frequency();
307+ if (freq == LiveTimer::Relative) {
308+ date_proximity_t proximity = getDateProximity(QDateTime::currentDateTime(), timer->relativeTime());
309+ freq = frequencyForProximity(proximity);
310+ }
311+ timer->setEffectiveFrequency(freq);
312+
313+ if (freq != LiveTimer::Disabled && (newFreq == LiveTimer::Disabled || freq < newFreq)) {
314+ newFreq = freq;
315+ }
316+ }
317+ }
318+ if (newFreq != m_frequency) {
319+ m_frequency = newFreq;
320+ reInitTimer();
321+ }
322+}
323+
324+void SharedLiveTimer::reInitTimer()
325+{
326+ QDateTime now(QDateTime::currentDateTime());
327+ m_nextUpdate = now;
328+
329+ switch(m_frequency) {
330+ case LiveTimer::Second:
331+ m_nextUpdate.setTime(QTime(now.time().hour(), now.time().minute(), now.time().second(), 0));
332+ m_nextUpdate = m_nextUpdate.addSecs(1);
333+ break;
334+
335+ case LiveTimer::Minute:
336+ m_nextUpdate.setTime(QTime(now.time().hour(), now.time().minute(), 0, 0));
337+ m_nextUpdate = m_nextUpdate.addSecs(60);
338+ break;
339+
340+ case LiveTimer::Hour:
341+ m_nextUpdate.setTime(QTime(now.time().hour(), 0, 0, 0));
342+ m_nextUpdate = m_nextUpdate.addSecs(60*60);
343+ break;
344+
345+ default:
346+ m_timer.stop();
347+ return;
348+ }
349+
350+ qint64 diff = m_nextUpdate.toMSecsSinceEpoch() - now.toMSecsSinceEpoch();
351+ m_timer.start(diff);
352+}
353+
354+void SharedLiveTimer::timeout()
355+{
356+ QDateTime now(QDateTime::currentDateTime());
357+ qint64 currentMSecsSinceEpoch = now.currentMSecsSinceEpoch();
358+ qint64 earlyMs = m_nextUpdate.toMSecsSinceEpoch() - currentMSecsSinceEpoch;
359+ if (earlyMs > 0) { // timer shouldn't have happened yet.
360+ reInitTimer();
361+ return;
362+ }
363+
364+ bool isHourUpdate = m_lastUpdate.date() != now.date() ||
365+ m_lastUpdate.time().hour() != now.time().hour();
366+ bool isMinuteUpdate = isHourUpdate ||
367+ m_lastUpdate.time().minute() != now.time().minute();
368+ bool isSecondUpdate = isMinuteUpdate ||
369+ m_lastUpdate.time().second() != now.time().second();
370+
371+ bool needsFrequencyUpdate = false;
372+ QList<LiveTimer*> tmpTimers(m_liveTimers);
373+ Q_FOREACH(LiveTimer* timer, tmpTimers) {
374+
375+ LiveTimer::Frequency effectiveFrequency = timer->effectiveFrequency();
376+ if (effectiveFrequency == LiveTimer::Disabled) continue;
377+
378+ if (isHourUpdate) {
379+ Q_EMIT timer->trigger();
380+ } else if (isMinuteUpdate) {
381+ if (effectiveFrequency >= LiveTimer::Second && effectiveFrequency <= LiveTimer::Minute) {
382+ Q_EMIT timer->trigger();
383+ }
384+ } else if (isSecondUpdate) {
385+ if (effectiveFrequency == LiveTimer::Second) {
386+ Q_EMIT timer->trigger();
387+ }
388+ }
389+
390+ // re-evaluate relative timer frequency
391+ if (timer->frequency() == LiveTimer::Relative) {
392+ date_proximity_t newProximity = getDateProximity(now, timer->relativeTime());
393+ needsFrequencyUpdate |= effectiveFrequency != frequencyForProximity(newProximity);
394+ }
395+ }
396+
397+ if (needsFrequencyUpdate) {
398+ updateFrequency();
399+ }
400+ reInitTimer();
401+ m_lastUpdate = now;
402+}
403+
404+void SharedLiveTimer::timedate1PropertiesChanged(const QString &interface, const QVariantMap &changed, const QStringList &)
405+{
406+ if (interface != "org.freedesktop.timedate1") return;
407+ if (!changed.contains("Timezone")) return;
408+
409+ QList<LiveTimer*> tmpTimers(m_liveTimers);
410+ Q_FOREACH(LiveTimer* timer, tmpTimers) {
411+ Q_EMIT timer->trigger();
412+ }
413+ reInitTimer();
414+}
415
416=== added file 'src/Ubuntu/Components/plugin/livetimer_p.h'
417--- src/Ubuntu/Components/plugin/livetimer_p.h 1970-01-01 00:00:00 +0000
418+++ src/Ubuntu/Components/plugin/livetimer_p.h 2015-08-12 09:56:54 +0000
419@@ -0,0 +1,58 @@
420+/*
421+ * Copyright 2015 Canonical Ltd.
422+ *
423+ * This program is free software; you can redistribute it and/or modify
424+ * it under the terms of the GNU General Public License as published by
425+ * the Free Software Foundation; version 3.
426+ *
427+ * This program is distributed in the hope that it will be useful,
428+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
429+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
430+ * GNU General Public License for more details.
431+ *
432+ * You should have received a copy of the GNU General Public License
433+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
434+ */
435+
436+#ifndef LIVETIMER_P_H
437+#define LIVETIMER_P_H
438+
439+#include "livetimer.h"
440+
441+#include <QTimer>
442+
443+class SharedLiveTimer : public QObject
444+{
445+ Q_OBJECT
446+public:
447+ SharedLiveTimer(QObject* parent = NULL);
448+
449+ static SharedLiveTimer& instance()
450+ {
451+ static SharedLiveTimer instance;
452+ return instance;
453+ }
454+
455+ void registerTimer(LiveTimer* timer);
456+ void unregisterTimer(LiveTimer* timer);
457+
458+private Q_SLOTS:
459+ void timeout();
460+ void timedate1PropertiesChanged(const QString &interface, const QVariantMap &changed, const QStringList&);
461+
462+Q_SIGNALS:
463+ void trigger();
464+
465+private:
466+ void updateFrequency();
467+ void reInitTimer();
468+
469+ QList<LiveTimer*> m_liveTimers;
470+ QTimer m_timer;
471+ LiveTimer::Frequency m_frequency;
472+
473+ QDateTime m_nextUpdate;
474+ QDateTime m_lastUpdate;
475+};
476+
477+#endif // LIVETIMER_P_H
478
479=== modified file 'src/Ubuntu/Components/plugin/plugin.cpp'
480--- src/Ubuntu/Components/plugin/plugin.cpp 2015-08-11 13:02:37 +0000
481+++ src/Ubuntu/Components/plugin/plugin.cpp 2015-08-12 09:56:54 +0000
482@@ -27,6 +27,7 @@
483 #include <QtQml/QQmlContext>
484 #include "i18n.h"
485 #include "listener.h"
486+#include "livetimer.h"
487 #include "ucunits.h"
488 #include "ucscalingimageprovider.h"
489 #include "ucqquickimageextension.h"
490@@ -218,6 +219,7 @@
491 qmlRegisterType<UCAction, 1>(uri, 1, 3, "Action");
492 qmlRegisterType<UCUbuntuShape, 2>(uri, 1, 3, "UbuntuShape");
493 qmlRegisterType<UCProportionalShape>(uri, 1, 3, "ProportionalShape");
494+ qmlRegisterType<LiveTimer>(uri, 1, 3, "LiveTimer");
495 }
496
497 void UbuntuComponentsPlugin::initializeEngine(QQmlEngine *engine, const char *uri)
498
499=== modified file 'src/Ubuntu/Components/plugin/plugin.pri'
500--- src/Ubuntu/Components/plugin/plugin.pri 2015-08-11 13:02:37 +0000
501+++ src/Ubuntu/Components/plugin/plugin.pri 2015-08-12 09:56:54 +0000
502@@ -77,7 +77,10 @@
503 $$PWD/ucnamespace.h \
504 $$PWD/ucdeprecatedtheme.h \
505 $$PWD/ucdefaulttheme.h \
506- $$PWD/ucstylehints.h
507+ $$PWD/ucstylehints.h \
508+ $$PWD/livetimer.h \
509+ $$PWD/livetimer_p.h \
510+ $$PWD/timeutils_p.h
511
512 SOURCES += $$PWD/plugin.cpp \
513 $$PWD/uctheme.cpp \
514@@ -127,7 +130,9 @@
515 $$PWD/ucnamespace.cpp \
516 $$PWD/ucdeprecatedtheme.cpp \
517 $$PWD/ucdefaulttheme.cpp \
518- $$PWD/ucstylehints.cpp
519+ $$PWD/ucstylehints.cpp \
520+ $$PWD/livetimer.cpp \
521+ $$PWD/livetimer_p.cpp
522
523 # adapters
524 SOURCES += $$PWD/adapters/alarmsadapter_organizer.cpp
525
526=== added file 'src/Ubuntu/Components/plugin/timeutils_p.h'
527--- src/Ubuntu/Components/plugin/timeutils_p.h 1970-01-01 00:00:00 +0000
528+++ src/Ubuntu/Components/plugin/timeutils_p.h 2015-08-12 09:56:54 +0000
529@@ -0,0 +1,117 @@
530+/*
531+ * Copyright 2014 Canonical Ltd.
532+ *
533+ * This program is free software; you can redistribute it and/or modify
534+ * it under the terms of the GNU General Public License as published by
535+ * the Free Software Foundation; version 3.
536+ *
537+ * This program is distributed in the hope that it will be useful,
538+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
539+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
540+ * GNU General Public License for more details.
541+ *
542+ * You should have received a copy of the GNU General Public License
543+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
544+ */
545+
546+#ifndef TIMEUTILS_P_H
547+#define TIMEUTILS_P_H
548+
549+#include "livetimer.h"
550+
551+#include <QDateTime>
552+#include <QObject>
553+#include <QTimer>
554+
555+typedef enum
556+{
557+ DATE_PROXIMITY_NOW,
558+ DATE_PROXIMITY_HOUR,
559+ DATE_PROXIMITY_TODAY,
560+ DATE_PROXIMITY_YESTERDAY,
561+ DATE_PROXIMITY_TOMORROW,
562+ DATE_PROXIMITY_LAST_WEEK,
563+ DATE_PROXIMITY_NEXT_WEEK,
564+ DATE_PROXIMITY_FAR_BACK,
565+ DATE_PROXIMITY_FAR_FORWARD
566+} date_proximity_t;
567+
568+inline date_proximity_t getDateProximity(const QDateTime& now, const QDateTime& time)
569+{
570+ int now_day = now.date().day();
571+ int now_month = now.date().month();
572+ int now_year = now.date().year();
573+
574+ int time_day = time.date().day();
575+ int time_month = time.date().month();
576+ int time_year = time.date().year();
577+
578+ qint64 diff = time.toMSecsSinceEpoch() - now.toMSecsSinceEpoch();
579+ if (qAbs(diff) < 30000) return DATE_PROXIMITY_NOW;
580+ else if (qAbs(diff) < 3600000) return DATE_PROXIMITY_HOUR;
581+
582+ // does it happen today?
583+ if ((now_year == time_year) && (now_month == time_month) && (now_day == time_day)) {
584+ return DATE_PROXIMITY_TODAY;
585+ }
586+
587+ // did it happen yesterday?
588+ QDateTime yesterday(now.addDays(-1));
589+ int yesterday_day = yesterday.date().day();
590+ int yesterday_month = yesterday.date().month();
591+ int yesterday_year = yesterday.date().year();
592+ if ((yesterday_year == time_year) && (yesterday_month == time_month) && (yesterday_day == time_day)) {
593+ return DATE_PROXIMITY_YESTERDAY;
594+ }
595+
596+ // does it happen tomorrow?
597+ QDateTime tomorrow(now.addDays(1));
598+ int tomorrow_day = tomorrow.date().day();
599+ int tomorrow_month = tomorrow.date().month();
600+ int tomorrow_year = tomorrow.date().year();
601+ if ((tomorrow_year == time_year) && (tomorrow_month == time_month) && (tomorrow_day == time_day)) {
602+ return DATE_PROXIMITY_TOMORROW;
603+ }
604+
605+ if (time < now) {
606+ QDateTime lastWeek(now.addDays(-6));
607+ QDateTime lastWeekBound(lastWeek.date(), QTime(0, 0, 0, 0));
608+
609+ // does it happen last week?
610+ if (time >= lastWeekBound) {
611+ return DATE_PROXIMITY_LAST_WEEK;
612+ }
613+ return DATE_PROXIMITY_FAR_BACK;
614+ } else {
615+ QDateTime nextWeek(now.addDays(6));
616+ QDateTime nextWeekBound(nextWeek.date(), QTime(23, 59, 59, 999));
617+
618+ // does it happen this week?
619+ if (time <= nextWeekBound) {
620+ return DATE_PROXIMITY_NEXT_WEEK;
621+ }
622+ return DATE_PROXIMITY_FAR_FORWARD;
623+ }
624+}
625+
626+inline LiveTimer::Frequency frequencyForProximity(date_proximity_t proximity) {
627+ switch(proximity) {
628+ case DATE_PROXIMITY_NOW:
629+ return LiveTimer::Second;
630+ case DATE_PROXIMITY_HOUR:
631+ return LiveTimer::Minute;
632+ case DATE_PROXIMITY_TODAY:
633+ case DATE_PROXIMITY_TOMORROW:
634+ case DATE_PROXIMITY_YESTERDAY:
635+ case DATE_PROXIMITY_LAST_WEEK:
636+ case DATE_PROXIMITY_NEXT_WEEK:
637+ case DATE_PROXIMITY_FAR_FORWARD:
638+ return LiveTimer::Hour;
639+ case DATE_PROXIMITY_FAR_BACK:
640+ default:
641+ break;
642+ }
643+ return LiveTimer::Disabled;
644+}
645+
646+#endif // TIMEUTILS_P_H
647
648=== added file 'tests/unit/tst_components/tst_livetimer.qml'
649--- tests/unit/tst_components/tst_livetimer.qml 1970-01-01 00:00:00 +0000
650+++ tests/unit/tst_components/tst_livetimer.qml 2015-08-12 09:56:54 +0000
651@@ -0,0 +1,51 @@
652+/*
653+ * Copyright 2014 Canonical Ltd.
654+ *
655+ * This program is free software; you can redistribute it and/or modify
656+ * it under the terms of the GNU Lesser General Public License as published by
657+ * the Free Software Foundation; version 3.
658+ *
659+ * This program is distributed in the hope that it will be useful,
660+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
661+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
662+ * GNU Lesser General Public License for more details.
663+ *
664+ * You should have received a copy of the GNU Lesser General Public License
665+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
666+ */
667+
668+import QtQuick 2.0
669+import QtTest 1.0
670+import Ubuntu.Components 1.3
671+
672+TestCase {
673+ name: "LiveTimer"
674+
675+ function test_0_defaults() {
676+ compare(liveTimer.frequency, LiveTimer.Disabled, "Default frequency");
677+ }
678+
679+ function test_frequency_data() {
680+ return [
681+ { tag:"Disabled", frequency: LiveTimer.Disabled },
682+ { tag:"Second", frequency: LiveTimer.Second },
683+ { tag:"Minute", frequency: LiveTimer.Minute },
684+ { tag:"Hour", frequency: LiveTimer.Hour },
685+ { tag:"Relative", frequency: LiveTimer.Relative },
686+ ];
687+ }
688+
689+ function test_frequency(data) {
690+ liveTimer.frequency = data.frequency;
691+ compare(liveTimer.frequency, data.frequency, "LiveTimer frequency can be set.");
692+ }
693+
694+ function test_relativeTime() {
695+ liveTimer.relativeTime = new Date(2015, 0, 0, 0, 0, 0, 0);
696+ compare(liveTimer.relativeTime, new Date(2015, 0, 0, 0, 0, 0, 0), "Can set/get relativeTime")
697+ }
698+
699+ LiveTimer {
700+ id: liveTimer
701+ }
702+}

Subscribers

People subscribed via source and target branches