Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions panels/notification/common/dataaccessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class DataAccessor
virtual int fetchEntityCount(const QString &appName, int processedType) const { Q_UNUSED(appName); Q_UNUSED(processedType); return 0; }
virtual NotifyEntity fetchLastEntity(const QString &appName, int processedType) { Q_UNUSED(appName); Q_UNUSED(processedType); return {}; }
virtual NotifyEntity fetchLastEntity(uint notifyId) { Q_UNUSED(notifyId); return {}; }
virtual QList<NotifyEntity> fetchExpiredEntities(qint64 expiredTime) { Q_UNUSED(expiredTime); return {}; }
virtual QList<NotifyEntity> fetchEntities(const QString &appName, int processedType, int maxCount)
{
Q_UNUSED(appName)
Expand Down
5 changes: 5 additions & 0 deletions panels/notification/common/dataaccessorproxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ NotifyEntity DataAccessorProxy::fetchLastEntity(uint notifyId)
return m_source->fetchLastEntity(notifyId);
}

QList<NotifyEntity> DataAccessorProxy::fetchExpiredEntities(qint64 expiredTime)
{
return m_source->fetchExpiredEntities(expiredTime);
}

QList<NotifyEntity> DataAccessorProxy::fetchEntities(const QString &appName, int processedType, int maxCount)
{
if (processedType == NotifyEntity::NotProcessed) {
Expand Down
1 change: 1 addition & 0 deletions panels/notification/common/dataaccessorproxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class DataAccessorProxy : public DataAccessor
virtual int fetchEntityCount(const QString &appName, int processedType) const override;
virtual NotifyEntity fetchLastEntity(const QString &appName, int processedType) override;
virtual NotifyEntity fetchLastEntity(uint notifyId) override;
virtual QList<NotifyEntity> fetchExpiredEntities(qint64 expiredTime) override;
virtual QList<NotifyEntity> fetchEntities(const QString &appName, int processedType, int maxCount) override;
virtual QList<QString> fetchApps(int maxCount) const override;

Expand Down
26 changes: 26 additions & 0 deletions panels/notification/common/dbaccessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,32 @@ NotifyEntity DBAccessor::fetchLastEntity(uint notifyId)
return {};
}

QList<NotifyEntity> DBAccessor::fetchExpiredEntities(qint64 expiredTime)
{
BENCHMARK();

QMutexLocker locker(&m_mutex);
QSqlQuery query(m_connection);

QString cmd = QString("SELECT %1 FROM notifications2 WHERE CTime < :expiredTime ORDER BY CTime ASC").arg(EntityFields.join(","));
query.prepare(cmd);
query.bindValue(":expiredTime", expiredTime);

if (!query.exec()) {
qWarning(notifyDBLog) << "Query execution error:" << query.lastError().text();
return {};
}

QList<NotifyEntity> expiredEntities;
while (query.next()) {
auto entity = parseEntity(query);
if (entity.isValid()) {
expiredEntities.append(entity);
}
}
return expiredEntities;
}

QList<QString> DBAccessor::fetchApps(int maxCount) const
{
BENCHMARK();
Expand Down
1 change: 1 addition & 0 deletions panels/notification/common/dbaccessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class DBAccessor : public DataAccessor
NotifyEntity fetchLastEntity(const QString &appName, int processedType) override;
QList<NotifyEntity> fetchEntities(const QString &appName, int processedType, int maxCount) override;
NotifyEntity fetchLastEntity(uint notifyId) override;
QList<NotifyEntity> fetchExpiredEntities(qint64 expiredTime) override;
QList<QString> fetchApps(int maxCount) const override;

void removeEntity(qint64 id) override;
Expand Down
19 changes: 19 additions & 0 deletions panels/notification/common/memoryaccessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
// SPDX-License-Identifier: GPL-3.0-or-later

#include "memoryaccessor.h"
#include <QDebug>

Check warning on line 6 in panels/notification/common/memoryaccessor.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDebug> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <limits>

Check warning on line 7 in panels/notification/common/memoryaccessor.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <limits> not found. Please note: Cppcheck does not need standard library headers to get proper results.

namespace notification
{
Expand Down Expand Up @@ -102,6 +103,24 @@
return {};
}

QList<NotifyEntity> MemoryAccessor::fetchExpiredEntities(qint64 expiredTime)
{
QMutexLocker locker(&m_mutex);
QList<NotifyEntity> expiredEntities;

for (const auto &entity : m_entities) {
if (entity.cTime() < expiredTime) {
expiredEntities.append(entity);
}
}

std::sort(expiredEntities.begin(), expiredEntities.end(), [](const NotifyEntity &a, const NotifyEntity &b) {
return a.cTime() < b.cTime();
});

return expiredEntities;
}

QList<NotifyEntity> MemoryAccessor::fetchEntities(const QString &appName, int processedType, int maxCount)
{
QMutexLocker locker(&m_mutex);
Expand Down
1 change: 1 addition & 0 deletions panels/notification/common/memoryaccessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class MemoryAccessor : public DataAccessor
virtual int fetchEntityCount(const QString &appName, int processedType) const override;
virtual NotifyEntity fetchLastEntity(const QString &appName, int processedType) override;
virtual NotifyEntity fetchLastEntity(uint notifyId) override;
virtual QList<NotifyEntity> fetchExpiredEntities(qint64 expiredTime) override;
virtual QList<NotifyEntity> fetchEntities(const QString &appName, int processedType, int maxCount) override;
virtual QList<QString> fetchApps(int maxCount) const override;

Expand Down
1 change: 1 addition & 0 deletions panels/notification/common/notifyentity.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class NotifyEntity
Dismissed = 2,
Closed = 3,
Unknown = 4,
Timeout = 5,
};

NotifyEntity();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,17 @@
"description[zh_CN]": "应用名称映射",
"permissions": "readwrite",
"visibility": "private"
},
"notificationCleanupDays": {
"value": 7,
"serial": 0,
"flags": [],
"name": "notification cleanup days",
"name[zh_CN]": "通知清理天数",
"description": "Number of days after which notifications will be automatically cleaned up",
"description[zh_CN]": "通知自动清理的天数,超过此天数的通知将被自动删除",
"permissions": "readwrite",
"visibility": "public"
}
}
}
21 changes: 20 additions & 1 deletion panels/notification/server/notificationmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,14 @@ NotificationManager::NotificationManager(QObject *parent)
, m_persistence(DataAccessorProxy::instance())
, m_setting(new NotificationSetting(this))
, m_pendingTimeout(new QTimer(this))
, m_cleanupTimer(new QTimer(this))
{
m_pendingTimeout->setSingleShot(true);
connect(m_pendingTimeout, &QTimer::timeout, this, &NotificationManager::onHandingPendingEntities);

m_cleanupTimer->setInterval(1000);
connect(m_cleanupTimer, &QTimer::timeout, this, &NotificationManager::onCleanupExpiredNotifications);
m_cleanupTimer->start();

DataAccessorProxy::instance()->setSource(DBAccessor::instance());

Expand All @@ -77,7 +82,9 @@ NotificationManager::NotificationManager(QObject *parent)
m_systemApps = config->value("systemApps").toStringList();
// TODO temporary fix for AppNamesMap
m_appNamesMap = config->value("AppNamesMap").toMap();

if(!config->value("notificationCleanupDays").isNull()) {
m_cleanupDays = config->value("notificationCleanupDays").toInt();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个时间,可能是个无效的,

}
if (QStringLiteral("wayland") != QGuiApplication::platformName()) {
initScreenLockedState();
}
Expand Down Expand Up @@ -457,6 +464,8 @@ void NotificationManager::updateEntityProcessed(qint64 id, uint reason)
if (entity.isValid()) {
if ((reason == NotifyEntity::Closed || reason == NotifyEntity::Dismissed) && entity.processedType() == NotifyEntity::NotProcessed) {
entity.setProcessedType(NotifyEntity::Removed);
} else if (reason == NotifyEntity::Timeout) {
entity.setProcessedType(NotifyEntity::Removed);
} else {
entity.setProcessedType(NotifyEntity::Processed);
}
Expand Down Expand Up @@ -675,4 +684,14 @@ void NotificationManager::onScreenLockedChanged(bool screenLocked)
m_screenLocked = screenLocked;
}

void NotificationManager::onCleanupExpiredNotifications()
{
const qint64 cutoffTime = QDateTime::currentDateTime().addDays(-m_cleanupDays).toMSecsSinceEpoch();
auto expiredEntities = m_persistence->fetchExpiredEntities(cutoffTime);

for (const auto &entity : expiredEntities) {
notificationClosed(entity.id(), entity.bubbleId(), NotifyEntity::Timeout);
}
}

} // notification
3 changes: 3 additions & 0 deletions panels/notification/server/notificationmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ private slots:
void onHandingPendingEntities();
void removePendingEntity(const NotifyEntity &entity);
void onScreenLockedChanged(bool);
void onCleanupExpiredNotifications();

private:
uint m_replacesCount = 0;
Expand All @@ -91,10 +92,12 @@ private slots:
DataAccessor *m_persistence = nullptr;
NotificationSetting *m_setting = nullptr;
QTimer *m_pendingTimeout = nullptr;
QTimer *m_cleanupTimer = nullptr;
qint64 m_lastTimeoutPoint = std::numeric_limits<qint64>::max();
QMultiHash<qint64, NotifyEntity> m_pendingTimeoutEntities;
QStringList m_systemApps;
QMap<QString, QVariant> m_appNamesMap;
int m_cleanupDays = 7;
};

} // notification