1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
/*
* Copyright 2013 Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3, as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranties of
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Charles Kerr <charles.kerr@canonical.com>
*/
#include <datetime/clock.h>
namespace unity {
namespace indicator {
namespace datetime {
class LiveClock::Impl
{
public:
Impl(LiveClock& owner, const std::shared_ptr<Timezones>& tzd):
owner_(owner),
timezones_(tzd)
{
if (timezones_)
{
timezones_->timezone.changed().connect ([this](const std::string& z) {setTimezone(z);});
setTimezone(timezones_->timezone.get());
}
owner_.skewTestIntervalSec.changed().connect([this](unsigned int intervalSec) {setInterval(intervalSec);});
setInterval(owner_.skewTestIntervalSec.get());
}
~Impl()
{
clearTimer();
g_clear_pointer (&timezone_, g_time_zone_unref);
}
GDateTime* localtime() const
{
g_assert (timezone_ != nullptr);
return g_date_time_new_now (timezone_);
}
private:
void setTimezone (const std::string& str)
{
g_clear_pointer (&timezone_, g_time_zone_unref);
timezone_= g_time_zone_new (str.c_str());
owner_.skewDetected();
}
private:
void clearTimer()
{
if (skew_timeout_id_)
{
g_source_remove(skew_timeout_id_);
skew_timeout_id_ = 0;
}
g_clear_pointer(&prev_datetime_, g_date_time_unref);
}
void setInterval(unsigned int seconds)
{
clearTimer();
if (seconds > 0)
{
prev_datetime_ = owner_.localtime();
skew_timeout_id_ = g_timeout_add_seconds(seconds, onTimerPulse, this);
}
}
static gboolean onTimerPulse(gpointer gself)
{
auto self = static_cast<Impl*>(gself);
// check to see if too much time passed since the last check */
GDateTime * now = self->owner_.localtime();
const GTimeSpan diff = g_date_time_difference(now, self->prev_datetime_);
const GTimeSpan fuzz = 5;
const GTimeSpan max = (self->owner_.skewTestIntervalSec.get() + fuzz) * G_USEC_PER_SEC;
if (abs(diff) > max)
self->owner_.skewDetected();
// update prev_datetime
g_clear_pointer(&self->prev_datetime_, g_date_time_unref);
self->prev_datetime_ = now;
return G_SOURCE_CONTINUE;
}
protected:
LiveClock& owner_;
GTimeZone * timezone_ = nullptr;
std::shared_ptr<Timezones> timezones_;
GDateTime * prev_datetime_ = nullptr;
unsigned int skew_timeout_id_ = 0;
unsigned int sleep_subscription_id_ = 0;
};
LiveClock::LiveClock(const std::shared_ptr<Timezones>& tzd):
p (new Impl (*this, tzd))
{
}
LiveClock::~LiveClock() =default;
GDateTime *
LiveClock::localtime() const
{
return p->localtime();
}
} // namespace datetime
} // namespace indicator
} // namespace unity
|