-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfpnotificationprocessor.cpp
93 lines (80 loc) · 2.81 KB
/
fpnotificationprocessor.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
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
/*
* This file is part of FlashbackPrism.
*
* Copyright (c) 2023 Luca Carlon
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY 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/>.
*/
/**
* Author: Luca Carlon
* Company: -
* Date: 2023.12.30
*/
#include <QGuiApplication>
#include <data/fppersistentsetup.h>
#include <lqtutils_ui.h>
#include <lqtutils_qsl.h>
#include "fpnotificationprocessor.h"
FPNotificationProcessor::FPNotificationProcessor(FPPhotoMonitor* photoMonitor, QObject* parent)
: QObject{parent}
, m_photoMonitor(photoMonitor)
{
m_timer.setSingleShot(false);
m_timer.setInterval(std::chrono::milliseconds(std::chrono::minutes(30)).count());
connect(&m_timer, &QTimer::timeout,
this, &FPNotificationProcessor::process);
m_timer.start();
}
void FPNotificationProcessor::process()
{
const QDateTime now = QDateTime::currentDateTime();
const QDate last = FPPersistentSetup().lastNotification();
if (now.time().hour() < 8) {
qDebug() << "Won't send a notification at" << now.time();
return;
}
if (last.isNull()) {
sendNotificationIfNeeded();
return;
}
if (now.date() > last) {
sendNotificationIfNeeded();
return;
}
}
void FPNotificationProcessor::sendNotificationIfNeeded()
{
const int years = m_photoMonitor->flashbackYears()->rowCount();
if (years <= 0) {
qDebug() << "Won't send a notification, no memories for today";
return;
}
int photos = 0;
for (const FPFlashbackYearRef& year : m_photoMonitor->flashbackYearsBackend())
if (year)
photos += year->items().size();
qDebug() << "Send notification";
#ifdef Q_OS_ANDROID
lqt::AndroidSystemNotification notification;
notification.set_icon(QImage(":/qt/qml/FlashbackPrism/assets/icon_notification.png"));
notification.set_activityClass(QSL("org.qtproject.qt.android.bindings.QtActivity"));
#else
lqt::SystemNotification notification;
#endif
notification.set_appName(qApp->applicationName());
notification.set_title(tr("Flashbacks available"));
notification.set_message(tr("You have %1 memories taken in %2 years for today. Have a look!").arg(photos).arg(years));
notification.set_openApp(true);
notification.send();
FPPersistentSetup().set_lastNotification(QDateTime::currentDateTime().date());
}