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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
|
/*
* Copyright 2020 Han Young <hanyoung@protonmail.com>
* Copyright 2020-2022 Devin Lin <devin@kde.org>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "alarm.h"
#include "alarmadaptor.h"
#include "kclockdsettings.h"
#include "utilities.h"
#include <KConfigGroup>
#include <KSharedConfig>
#include <QDBusConnection>
#include <QDBusMessage>
#include <QDateTime>
#include <QJsonDocument>
#include <QJsonObject>
// alarm from json (loaded from storage)
Alarm::Alarm(const QString &serialized, AlarmModel *parent)
: QObject{parent}
{
if (!serialized.isEmpty()) {
QJsonDocument doc = QJsonDocument::fromJson(serialized.toUtf8());
QJsonObject obj = doc.object();
m_uuid = QUuid::fromString(obj[QStringLiteral("uuid")].toString());
m_name = obj[QStringLiteral("name")].toString();
m_enabled = obj[QStringLiteral("enabled")].toBool();
m_hours = obj[QStringLiteral("hours")].toInt();
m_minutes = obj[QStringLiteral("minutes")].toInt();
m_daysOfWeek = obj[QStringLiteral("daysOfWeek")].toInt();
m_audioPath = QUrl::fromLocalFile(obj[QStringLiteral("audioPath")].toString());
m_ringDuration = obj[QStringLiteral("ringDuration")].toInt(5);
m_snoozeDuration = obj[QStringLiteral("snoozeDuration")].toInt(5);
m_snoozedLength = obj[QStringLiteral("snoozedLength")].toInt();
}
init(parent);
}
Alarm::Alarm(const QString &name, int hours, int minutes, int daysOfWeek, QString audioPath, int ringDuration, int snoozeDuration, AlarmModel *parent)
: QObject{parent}
, m_uuid{QUuid::createUuid()}
, m_name{name}
, m_enabled{true} // default the alarm to be on
, m_hours{hours}
, m_minutes{minutes}
, m_daysOfWeek{daysOfWeek}
, m_audioPath{QUrl::fromLocalFile(audioPath.replace(QStringLiteral("file://"), QString()))}
, m_ringDuration{ringDuration}
, m_snoozeDuration{snoozeDuration}
{
init(parent);
}
Alarm::~Alarm()
{
// ensure alarm doesn't continue ringing after deletion
if (m_ringing) {
AlarmPlayer::instance().stop();
}
}
void Alarm::init(AlarmModel *parent)
{
// setup notification
m_notification->setIconName(QStringLiteral("kclock"));
m_notification->setTitle(name() == QString() ? i18n("Alarm") : name());
m_notification->setText(QLocale::system().toString(QTime::currentTime(), QLocale::ShortFormat));
m_notification->setAutoDelete(false); // don't auto-delete when closing
auto defaultAction = m_notification->addDefaultAction(i18n("View"));
connect(defaultAction, &KNotificationAction::activated, this, &Alarm::dismiss);
auto dismissAction = m_notification->addAction(i18n("Dismiss"));
connect(dismissAction, &KNotificationAction::activated, this, &Alarm::dismiss);
auto snoozeAction = m_notification->addAction(i18n("Snooze"));
connect(snoozeAction, &KNotificationAction::activated, this, &Alarm::snooze);
connect(m_notification, &KNotification::closed, this, &Alarm::dismiss);
// setup signals
connect(this, &Alarm::rescheduleRequested, this, &Alarm::save);
connect(this, &Alarm::rescheduleRequested, this, &Alarm::calculateNextRingTime);
calculateNextRingTime();
if (parent) {
// must be after calculateNextTime signal, to ensure the ring time is calculated before the alarm is scheduled
connect(this, &Alarm::rescheduleRequested, parent, &AlarmModel::scheduleAlarm);
}
// setup dbus
new AlarmAdaptor(this);
QDBusConnection::sessionBus().registerObject(QStringLiteral("/Alarms/") + m_uuid.toString(QUuid::Id128), this);
connect(this, &QObject::destroyed, [this] {
QDBusConnection::sessionBus().unregisterObject(QStringLiteral("/Alarms/") + m_uuid.toString(QUuid::Id128), QDBusConnection::UnregisterNode);
});
// setup ringing length timer
connect(m_ringTimer, &QTimer::timeout, this, []() {
AlarmPlayer::instance().stop();
});
}
// alarm to json
QString Alarm::serialize()
{
QJsonObject obj;
obj[QStringLiteral("uuid")] = m_uuid.toString();
obj[QStringLiteral("name")] = m_name;
obj[QStringLiteral("enabled")] = m_enabled;
obj[QStringLiteral("hours")] = m_hours;
obj[QStringLiteral("minutes")] = m_minutes;
obj[QStringLiteral("daysOfWeek")] = m_daysOfWeek;
obj[QStringLiteral("audioPath")] = m_audioPath.toLocalFile();
obj[QStringLiteral("ringDuration")] = m_ringDuration;
obj[QStringLiteral("snoozeDuration")] = m_snoozeDuration;
obj[QStringLiteral("snoozedLength")] = m_snoozedLength;
QByteArray data = QJsonDocument(obj).toJson(QJsonDocument::Compact);
return QString::fromStdString(data.toStdString());
}
QString Alarm::uuid() const
{
return m_uuid.toString();
}
QString Alarm::name() const
{
return m_name;
}
void Alarm::setName(const QString &name)
{
if (name != m_name) {
m_name = name;
Q_EMIT nameChanged();
}
}
bool Alarm::enabled() const
{
return m_enabled;
}
void Alarm::setEnabled(bool enabled)
{
if (m_enabled != enabled) {
setSnoozedLength(0); // reset snooze value
setNextRingTime(0); // reset next ring time
m_enabled = enabled;
Q_EMIT enabledChanged();
Q_EMIT rescheduleRequested(); // notify the AlarmModel to reschedule
}
}
int Alarm::hours() const
{
return m_hours;
}
void Alarm::setHours(int hours)
{
if (hours != m_hours) {
m_hours = hours;
Q_EMIT hoursChanged();
Q_EMIT rescheduleRequested();
}
}
int Alarm::minutes() const
{
return m_minutes;
}
void Alarm::setMinutes(int minutes)
{
if (minutes != m_minutes) {
m_minutes = minutes;
Q_EMIT minutesChanged();
Q_EMIT rescheduleRequested();
}
}
int Alarm::daysOfWeek() const
{
return m_daysOfWeek;
}
void Alarm::setDaysOfWeek(int daysOfWeek)
{
if (daysOfWeek != m_daysOfWeek) {
m_daysOfWeek = daysOfWeek;
Q_EMIT daysOfWeekChanged();
Q_EMIT rescheduleRequested();
}
}
QString Alarm::audioPath() const
{
return m_audioPath.toLocalFile();
}
void Alarm::setAudioPath(QString path)
{
if (m_audioPath.path() != path) {
path = path.replace(QStringLiteral("file://"), QString());
m_audioPath = QUrl::fromLocalFile(path);
Q_EMIT audioPathChanged();
}
}
int Alarm::ringDuration() const
{
return m_ringDuration;
}
void Alarm::setRingDuration(int ringDuration)
{
if (ringDuration != m_ringDuration) {
m_ringDuration = ringDuration;
Q_EMIT ringDurationChanged();
}
}
int Alarm::snoozeDuration() const
{
return m_snoozeDuration;
}
void Alarm::setSnoozeDuration(int snoozeDuration)
{
if (snoozeDuration != m_snoozeDuration) {
m_snoozeDuration = snoozeDuration;
Q_EMIT snoozeDurationChanged();
}
}
int Alarm::snoozedLength() const
{
return m_snoozedLength;
}
void Alarm::setSnoozedLength(int snoozedLength)
{
if (snoozedLength != m_snoozedLength) {
m_snoozedLength = snoozedLength;
Q_EMIT snoozedLengthChanged();
}
}
bool Alarm::ringing() const
{
return m_ringing;
}
void Alarm::setRinging(bool ringing)
{
if (ringing != m_ringing) {
m_ringing = ringing;
Q_EMIT ringingChanged();
}
}
void Alarm::save()
{
// save this alarm to the config
auto config = KSharedConfig::openConfig();
KConfigGroup group = config->group(ALARM_CFG_GROUP);
group.writeEntry(m_uuid.toString(), this->serialize());
group.sync();
}
void Alarm::ring()
{
if (!m_enabled) {
return;
}
qDebug() << "Ringing alarm" << m_name << "and sending notification...";
// save ring time
if (m_snoozedLength == 0) {
m_originalRingTime = m_nextRingTime;
}
// reset snoozed length (if snooze happens, this will get set again)
setSnoozedLength(0);
// send notification
m_notification->setText(QLocale::system().toString(QTime::currentTime(), QLocale::ShortFormat));
m_notification->sendEvent();
// wake up device
Utilities::wakeupNow();
// pause playing mpris media sources
Utilities::pauseMprisSources();
// play sound (it will loop)
qDebug() << "Alarm sound: " << m_audioPath;
// TODO clean up usage of audio path as URL.
AlarmPlayer::instance().setSource(m_audioPath.toLocalFile());
AlarmPlayer::instance().play();
setRinging(true);
// stop ringing after duration
m_ringTimer->start(m_ringDuration * 60 * 1000);
}
void Alarm::dismiss()
{
qDebug() << "Alarm" << m_name << "dismissed";
// ignore if the snooze button was pressed and dismiss is still called
if (!m_justSnoozed && daysOfWeek() == 0) {
// disable alarm if set to run once
setEnabled(false);
}
m_justSnoozed = false;
AlarmPlayer::instance().stop();
Utilities::resumeMprisSources();
setRinging(false);
m_ringTimer->stop();
m_notification->close();
Q_EMIT rescheduleRequested();
Utilities::instance().decfActiveCount();
}
void Alarm::snooze()
{
qDebug() << "Alarm snoozed (" << m_snoozeDuration << " minutes)";
AlarmPlayer::instance().stop();
Utilities::resumeMprisSources();
setRinging(false);
m_ringTimer->stop();
m_notification->close();
m_justSnoozed = true;
// add snooze amount, and enable alarm
setSnoozedLength((QDateTime::currentSecsSinceEpoch() + 60 * m_snoozeDuration) - m_originalRingTime);
m_enabled = true; // can't use setEnabled, since it will reset snoozedLength
Q_EMIT enabledChanged();
Q_EMIT rescheduleRequested();
Utilities::instance().decfActiveCount();
}
void Alarm::calculateNextRingTime()
{
if (!this->m_enabled) { // if not enabled, means this would never ring
setNextRingTime(0);
return;
}
// get the time that the alarm will ring on the day
QTime alarmTime = QTime(m_hours, m_minutes, 0).addSecs(m_snoozedLength);
QDateTime date = QDateTime::currentDateTime();
if (this->m_daysOfWeek == 0) { // alarm does not repeat (no days of the week are specified)
// either the alarm occurs today or tomorrow
bool alarmOccursToday = alarmTime >= date.time();
setNextRingTime(QDateTime(date.date().addDays(alarmOccursToday ? 0 : 1), alarmTime).toSecsSinceEpoch());
} else { // repeat alarm
bool first = true;
// keeping looping forward a single day until the day of week is accepted
while (((this->m_daysOfWeek & (1 << (date.date().dayOfWeek() - 1))) == 0) // check day
|| (first && (alarmTime < date.time()))) // check time if the current day is accepted (keep looping forward if alarmTime has passed)
{
date = date.addDays(1); // go forward a day
first = false;
}
setNextRingTime(QDateTime(date.date(), alarmTime).toSecsSinceEpoch());
}
}
void Alarm::setNextRingTime(quint64 nextRingTime)
{
if (m_nextRingTime != nextRingTime) {
m_nextRingTime = nextRingTime;
Q_EMIT nextRingTimeChanged();
}
}
quint64 Alarm::nextRingTime()
{
// day changed, re-calculate
if (this->m_nextRingTime <= static_cast<unsigned long long>(QDateTime::currentSecsSinceEpoch())) {
calculateNextRingTime();
}
return m_nextRingTime;
}
#include "moc_alarm.cpp"
|