aboutsummaryrefslogtreecommitdiff
path: root/src/alarm-queue-simple.cpp
blob: b5356032721e915f3e5c4ea5c1df988f6075917e (plain)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * Copyright 2014 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/alarm-queue-simple.h>

#include <cmath>
#include <set>

namespace ayatana {
namespace indicator {
namespace datetime {

/***
****
***/

class SimpleAlarmQueue::Impl
{
public:

    Impl(const std::shared_ptr<Clock>& clock,
         const std::shared_ptr<Planner>& planner,
         const std::shared_ptr<WakeupTimer>& timer):
      m_clock{clock},
      m_planner{planner},
      m_timer{timer},
      m_datetime{clock->localtime()}
    {
        m_planner->appointments().changed().connect([this](const std::vector<Appointment>&){
            g_debug("AlarmQueue %p calling requeue() due to appointments changed", this);
            requeue();
        });

        m_clock->minute_changed.connect([this]{
            const auto now = m_clock->localtime();
            constexpr auto skew_threshold_usec = int64_t{90} * G_USEC_PER_SEC;
            const bool clock_jumped = std::abs(now - m_datetime) > skew_threshold_usec;
            m_datetime = now;
            if (clock_jumped) {
                g_debug("AlarmQueue %p calling requeue() due to clock skew", this);
                requeue();
            }
        });

        m_timer->timeout().connect([this](){
            g_debug("AlarmQueue %p calling requeue() due to timeout", this);
            requeue();
        });

        requeue();
    }

    ~Impl()
    {
    }

    core::Signal<const Appointment&, const Alarm&>& alarm_reached()
    {
        return m_alarm_reached;
    }

private:

    void requeue()
    {
        const auto appointments = m_planner->appointments().get();
        const Alarm* alarm;

        // kick any current alarms
        for (const auto& appointment : appointments)
        {
            if ((alarm = appointment_get_current_alarm(appointment)))
            {
                m_triggered.insert(std::make_pair(appointment.uid, alarm->time));
                m_alarm_reached(appointment, *alarm);
            }
        }

        // idle until the next alarm
        if ((alarm = find_next_alarm(appointments)))
        {
            g_debug ("setting timer to wake up for next appointment '%s' at %s",
                     alarm->text.c_str(),
                     alarm->time.format("%F %T").c_str());

            m_timer->set_wakeup_time(alarm->time);
        }
    }

    bool already_triggered (const Appointment& appt, const Alarm& alarm) const
    {
        const std::pair<const std::string&,const DateTime&> key{appt.uid, alarm.time};
        return m_triggered.count(key) != 0;
    }

    // return the next Alarm (if any) that will kick now or in the future
    const Alarm* find_next_alarm(const std::vector<Appointment>& appointments) const
    {
        const Alarm* best {};
        const auto now = m_clock->localtime();
        const auto beginning_of_minute = now.start_of_minute();

        g_debug ("planner has %zu appointments in it", (size_t)appointments.size());

        for(const auto& appointment : appointments)
        {
            for(const auto& alarm : appointment.alarms)
            {
                if (already_triggered(appointment, alarm))
                    continue;

                if (alarm.time < beginning_of_minute) // has this one already passed?
                    continue;

                if (best && (best->time < alarm.time)) // do we already have a better match?
                    continue;

                best = &alarm;
            }
        }

        return best;
    }

    // return the Appointment's current Alarm (if any)
    const Alarm* appointment_get_current_alarm(const Appointment& appointment) const
    {
        const auto now = m_clock->localtime();

        for (const auto& alarm : appointment.alarms)
            if (!already_triggered(appointment, alarm) && DateTime::is_same_minute(now, alarm.time))
                return &alarm;

        return nullptr;
    }


    std::set<std::pair<std::string,DateTime>> m_triggered;
    const std::shared_ptr<Clock> m_clock;
    const std::shared_ptr<Planner> m_planner;
    const std::shared_ptr<WakeupTimer> m_timer;
    core::Signal<const Appointment&, const Alarm&> m_alarm_reached;
    DateTime m_datetime;
};

/***
****  Public API
***/


SimpleAlarmQueue::SimpleAlarmQueue(const std::shared_ptr<Clock>& clock,
                                   const std::shared_ptr<Planner>& planner,
                                   const std::shared_ptr<WakeupTimer>& timer):
    impl{new Impl{clock, planner, timer}}
{
}

SimpleAlarmQueue::~SimpleAlarmQueue()
{
}

core::Signal<const Appointment&, const Alarm&>&
SimpleAlarmQueue::alarm_reached()
{
    return impl->alarm_reached();
}

/***
****
***/

} // namespace datetime
} // namespace indicator
} // namespace ayatana