Commit f194bd60 authored by Volker Krause's avatar Volker Krause
Browse files

Store active alarm information whenever they change

That is, instead of unconditionally every minute for the entire set of
active alarms. Besides being more efficient, this also reliably persists
the state if the daemon is terminated between a change and the next one
minute timer cycle.
parent 78efabab
Pipeline #105626 passed with stage
in 5 minutes and 49 seconds
......@@ -13,8 +13,6 @@
#include <KLocalizedString>
#include <KSharedConfig>
#include <QApplication>
#include <QDBusConnection>
#include <QDateTime>
using namespace KCalendarCore;
......@@ -26,6 +24,8 @@ KalendarAlarmClient::KalendarAlarmClient(QObject *parent)
QDBusConnection::sessionBus().registerObject(QStringLiteral("/ac"), this);
m_notificationHandler = new NotificationHandler(this);
connect(m_notificationHandler, &NotificationHandler::notificationUpdated, this, &KalendarAlarmClient::storeNotification);
connect(m_notificationHandler, &NotificationHandler::notificationRemoved, this, &KalendarAlarmClient::removeNotification);
// Check if Akonadi is already configured
const QString akonadiConfigFile = Akonadi::ServerManager::serverConfigFilePath(Akonadi::ServerManager::ReadWrite);
......@@ -49,7 +49,6 @@ KalendarAlarmClient::KalendarAlarmClient(QObject *parent)
mLastChecked = alarmGroup.readEntry("CalendarsLastChecked", QDateTime::currentDateTime().addDays(-9));
mCheckTimer.start(1000 * interval); // interval in seconds
connect(qApp, &QApplication::commitDataRequest, this, &KalendarAlarmClient::slotCommitData);
restoreSuspendedFromConfig();
}
......@@ -117,27 +116,21 @@ void KalendarAlarmClient::restoreSuspendedFromConfig()
}
}
void KalendarAlarmClient::flushSuspendedToConfig()
void KalendarAlarmClient::storeNotification(AlarmNotification *notification)
{
KConfigGroup suspendedGroup(KSharedConfig::openConfig(), "Suspended");
suspendedGroup.deleteGroup();
const auto notifications = m_notificationHandler->activeNotifications();
if (notifications.isEmpty()) {
qDebug() << "flushSuspendedToConfig: No pending notification exists, nothing to write to config";
KSharedConfig::openConfig()->sync();
return;
}
KConfigGroup notificationGroup(&suspendedGroup, notification->uid());
notificationGroup.writeEntry("UID", notification->uid());
notificationGroup.writeEntry("Text", notification->text());
notificationGroup.writeEntry("RemindAt", notification->remindAt());
KSharedConfig::openConfig()->sync();
}
for (const auto &s : notifications) {
qDebug() << "flushSuspendedToConfig: Flushing alarm" << s->uid() << s->remindAt() << " to config";
KConfigGroup notificationGroup(&suspendedGroup, s->uid());
notificationGroup.writeEntry("UID", s->uid());
notificationGroup.writeEntry("Text", s->text());
notificationGroup.writeEntry("RemindAt", s->remindAt());
}
void KalendarAlarmClient::removeNotification(AlarmNotification *notification)
{
KConfigGroup suspendedGroup(KSharedConfig::openConfig(), "Suspended");
KConfigGroup notificationGroup(&suspendedGroup, notification->uid());
notificationGroup.deleteGroup();
KSharedConfig::openConfig()->sync();
}
......@@ -207,14 +200,6 @@ void KalendarAlarmClient::checkAlarms()
m_notificationHandler->sendNotifications();
saveLastCheckTime();
flushSuspendedToConfig();
}
void KalendarAlarmClient::slotQuit()
{
flushSuspendedToConfig();
saveLastCheckTime();
quit();
}
void KalendarAlarmClient::saveLastCheckTime()
......@@ -230,11 +215,6 @@ void KalendarAlarmClient::quit()
qApp->quit();
}
void KalendarAlarmClient::slotCommitData(QSessionManager &)
{
saveLastCheckTime();
}
void KalendarAlarmClient::forceAlarmCheck()
{
checkAlarms();
......
......@@ -14,9 +14,8 @@
#include <AkonadiCore/ServerManager>
#endif
#include <Akonadi/Calendar/ETMCalendar>
#include <QSessionManager>
class AlarmDockWindow;
class AlarmNotification;
class NotificationHandler;
class KalendarAlarmClient : public QObject
......@@ -34,16 +33,13 @@ public:
Q_REQUIRED_RESULT QString dumpDebug() const;
Q_REQUIRED_RESULT QStringList dumpAlarms() const;
public Q_SLOTS:
void slotQuit();
private:
void deferredInit();
void restoreSuspendedFromConfig();
void flushSuspendedToConfig();
void storeNotification(AlarmNotification *notification);
void removeNotification(AlarmNotification *notification);
void checkAlarms();
void setupAkonadi();
void slotCommitData(QSessionManager &);
Q_REQUIRED_RESULT bool collectionsAvailable() const;
void saveLastCheckTime();
......
......@@ -30,6 +30,7 @@ void NotificationHandler::addNotification(const QString &uid, const QString &txt
notification->setText(txt);
notification->setRemindAt(remindTime);
m_notifications[notification->uid()] = notification;
Q_EMIT notificationUpdated(notification);
}
void NotificationHandler::sendNotifications()
......@@ -47,6 +48,7 @@ void NotificationHandler::sendNotifications()
void NotificationHandler::dismiss(AlarmNotification *notification)
{
qDebug() << "Alarm" << notification->uid() << "dismissed";
Q_EMIT notificationRemoved(notification);
m_notifications.remove(notification->uid());
delete notification;
}
......@@ -55,9 +57,5 @@ void NotificationHandler::suspend(AlarmNotification *notification)
{
qDebug() << ":Alarm " << notification->uid() << "suspended";
notification->setRemindAt(QDateTime(QDateTime::currentDateTime()).addSecs(m_suspend_seconds));
}
QHash<QString, AlarmNotification *> NotificationHandler::activeNotifications() const
{
return m_notifications;
Q_EMIT notificationUpdated(notification);
}
......@@ -33,11 +33,6 @@ public:
*/
void addNotification(const QString &uid, const QString &text, const QDateTime &remindTime);
/**
* @return The list of active notifications. It is the set of notification that should be sent at the next check
*/
QHash<QString, AlarmNotification *> activeNotifications() const;
public Q_SLOTS:
/**
* @brief Dismisses any further notification display for the alarm \p notification.
......@@ -51,6 +46,12 @@ public Q_SLOTS:
*/
void suspend(AlarmNotification *notification);
Q_SIGNALS:
/** Store a new or updated notification. */
void notificationUpdated(AlarmNotification *notification);
/** Remove a dismissed notification. */
void notificationRemoved(AlarmNotification *notification);
private:
QHash<QString, AlarmNotification *> m_notifications;
int m_suspend_seconds;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment