This repository has been archived by the owner on Nov 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoreNotifier.cpp
48 lines (46 loc) · 1.69 KB
/
CoreNotifier.cpp
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
#include "CoreNotifier.h"
#include "AlarmModel.h"
#include <QTimer>
#include "AlarmData.h"
#include <QDateTime>
#include "HeAlarm.h"
CoreNotifier::CoreNotifier(AlarmModel* model, QObject *parent) :
QObject{parent},
m_model {model}
{
connect(m_model, &AlarmModel::dataChanged, this, &CoreNotifier::restartTimers);
connect(m_model, &AlarmModel::layoutChanged, this, &CoreNotifier::restartTimers);
connect(m_model, &AlarmModel::rowsInserted, this, &CoreNotifier::restartTimers);
connect(m_model, &AlarmModel::rowsMoved, this, &CoreNotifier::restartTimers);
connect(m_model, &AlarmModel::rowsRemoved, this, &CoreNotifier::restartTimers);
restartTimers();
}
void CoreNotifier::restartTimers()
{
for (auto pt : m_timerList)
delete pt;
m_timerList.clear();
const auto& data = m_model->rawData();
for (int i = 0; i < data.size(); i++)
{
const auto& alm = data.at(i);
if (!alm.isActive)
continue;
auto current = QDateTime::currentDateTime();
if ((alm.activeDays != 0) && ((alm.activeDays & HeAlarm::fromQtDayOfWeek(static_cast<Qt::DayOfWeek>(current.date().dayOfWeek()))) == 0))
continue;
auto targetTime = QDateTime(QDate::currentDate(), {alm.hour, alm.minute});
auto duration = current.msecsTo(targetTime);
if (duration <= 0)
continue;
qInfo() << "Starting alarm " << alm.title << "after" << duration << "ms";
auto timer = new QTimer(this);
m_timerList.push_back(timer);
connect(timer, &QTimer::timeout, this, [this, alm]() {emit this->alarmTriggered(QVariant::fromValue(alm));});
if (alm.activeDays == 0)
connect(timer, &QTimer::timeout, m_model, [this, i]() {this->m_model->setIsActive(i, false);});
timer->setInterval(duration);
timer->setSingleShot(true);
timer->start();
}
}