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
|
/*
* SPDX-FileCopyrightText: 2021 Reion Wong <reion@cutefishos.com>
* SPDX-FileCopyrightText: 2018-2019 Kai Uwe Broulik <kde@privat.broulik.de>
* SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
#include "notificationsmodel.h"
#include "historymodel.h"
#include "notification.h"
#include "settings.h"
#include <QMetaEnum>
#include <QDebug>
static const int s_notificationsLimit = 1000;
static NotificationsModel *NOTIFICATIONS_MODEL = nullptr;
NotificationsModel *NotificationsModel::self()
{
if (!NOTIFICATIONS_MODEL)
NOTIFICATIONS_MODEL = new NotificationsModel;
return NOTIFICATIONS_MODEL;
}
NotificationsModel::NotificationsModel(QObject *parent)
: QAbstractListModel(parent)
{
m_pendingRemovalTimer.setSingleShot(true);
m_pendingRemovalTimer.setInterval(50);
connect(&m_pendingRemovalTimer, &QTimer::timeout, this, [this] {
QVector<int> rowsToBeRemoved;
rowsToBeRemoved.reserve(m_pendingRemovals.count());
for (uint id : qAsConst(m_pendingRemovals)) {
int row = rowOfNotification(id); // oh the complexity...
if (row == -1) {
continue;
}
rowsToBeRemoved.append(row);
}
removeRows(rowsToBeRemoved);
});
connect(NotificationServer::self(), &NotificationServer::notificationAdded, this, &NotificationsModel::onNotificationAdded);
connect(NotificationServer::self(), &NotificationServer::notificationReplaced, this, &NotificationsModel::onNotificationReplaced);
connect(NotificationServer::self(), &NotificationServer::notificationRemoved, this, &NotificationsModel::onNotificationRemoved);
}
QVariant NotificationsModel::data(const QModelIndex &index, int role) const
{
const Notification ¬ification = m_notifications.at(index.row());
switch (role) {
case NotificationsModel::IdRole:
return notification.id;
case NotificationsModel::SummaryRole:
return notification.summary;
case NotificationsModel::ImageRole:
return "";
case NotificationsModel::CreatedRole:
return notification.created;
case NotificationsModel::BodyRole:
return notification.body;
case NotificationsModel::IconNameRole:
return notification.appIcon;
case NotificationsModel::HasDefaultActionRole:
return notification.actions.contains("default");
default:
break;
}
return QVariant();
}
bool NotificationsModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
// Notification ¬ification = m_notifications[index.row()];
bool dirty = false;
return dirty;
}
int NotificationsModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return m_notifications.size();
}
QHash<int, QByteArray> NotificationsModel::roleNames() const
{
static QHash<int, QByteArray> s_roles;
if (s_roles.isEmpty()) {
// This generates role names from the Roles enum in the form of: FooRole -> foo
const QMetaEnum e = QMetaEnum::fromType<NotificationsModel::Roles>();
// Qt built-in roles we use
s_roles.insert(Qt::DisplayRole, QByteArrayLiteral("display"));
s_roles.insert(Qt::DecorationRole, QByteArrayLiteral("decoration"));
for (int i = 0; i < e.keyCount(); ++i) {
const int value = e.value(i);
QByteArray key(e.key(i));
key[0] = key[0] + 32; // lower case first letter
key.chop(4); // strip "Role" suffix
s_roles.insert(value, key);
}
s_roles.insert(NotificationsModel::IdRole, QByteArrayLiteral("notificationId")); // id is QML-reserved
}
return s_roles;
}
void NotificationsModel::expired(uint id)
{
int row = rowOfNotification(id);
if (row > -1) {
onNotificationRemoved(id, NotificationServer::CloseReason::Expired);
}
}
void NotificationsModel::close(uint id)
{
if (rowOfNotification(id) > -1) {
NotificationServer::self()->closeNotification(id, NotificationServer::CloseReason::DismissedByUser);
}
}
void NotificationsModel::invokeDefaultAction(uint notificationId)
{
const int row = rowOfNotification(notificationId);
if (row == -1) {
return;
}
const Notification ¬ification = m_notifications.at(row);
if (!notification.actions.contains("default")) {
return;
}
NotificationServer::self()->InvokeAction(notificationId, "default");
}
int NotificationsModel::rowOfNotification(uint id) const
{
auto it = std::find_if(m_notifications.constBegin(), m_notifications.constEnd(), [id](const Notification &item) {
return item.id == id;
});
if (it == m_notifications.constEnd()) {
return -1;
}
return std::distance(m_notifications.constBegin(), it);
}
void NotificationsModel::removeRows(const QVector<int> &rows)
{
if (rows.isEmpty()) {
return;
}
QVector<int> rowsToBeRemoved(rows);
std::sort(rowsToBeRemoved.begin(), rowsToBeRemoved.end());
QVector<QPair<int, int>> clearQueue;
QPair<int, int> clearRange{rowsToBeRemoved.first(), rowsToBeRemoved.first()};
for (int row : rowsToBeRemoved) {
if (row > clearRange.second + 1) {
clearQueue.append(clearRange);
clearRange.first = row;
}
clearRange.second = row;
}
if (clearQueue.isEmpty() || clearQueue.last() != clearRange) {
clearQueue.append(clearRange);
}
int rowsRemoved = 0;
for (int i = clearQueue.count() - 1; i >= 0; --i) {
const auto &range = clearQueue.at(i);
beginRemoveRows(QModelIndex(), range.first, range.second);
for (int j = range.second; j >= range.first; --j) {
m_notifications.removeAt(j);
++rowsRemoved;
}
endRemoveRows();
}
Q_ASSERT(rowsRemoved == rowsToBeRemoved.count());
m_pendingRemovals.clear();
}
void NotificationsModel::onNotificationAdded(const Notification ¬ification)
{
// Do Not Disturb Mode:
// Add directly to the historical model.
if (Settings::self()->doNotDisturb()) {
HistoryModel::self()->add(notification);
return;
}
if (m_notifications.size() >= s_notificationsLimit) {
const int cleanupCount = s_notificationsLimit / 2;
beginRemoveRows(QModelIndex(), 0, cleanupCount - 1);
for (int i = 0; i < cleanupCount; ++i) {
m_notifications.removeAt(0);
}
endRemoveRows();
}
beginInsertRows(QModelIndex(), m_notifications.size(), m_notifications.size());
m_notifications.append(std::move(notification));
endInsertRows();
}
void NotificationsModel::onNotificationReplaced(uint replacedId, const Notification ¬ification)
{
}
void NotificationsModel::onNotificationRemoved(uint removedId, NotificationServer::CloseReason reason)
{
const int row = rowOfNotification(removedId);
if (row == -1) {
return;
}
// When a notification expired, keep it around in the history and mark it as such
if (reason == NotificationServer::CloseReason::Expired) {
const QModelIndex idx = NotificationsModel::index(row, 0);
Notification ¬ification = m_notifications[row];
// notification.setExpired(true);
notification.actions.clear();
emit dataChanged(idx, idx);
HistoryModel::self()->add(notification);
// return;
}
// Otherwise if explicitly closed by either user or app, mark it for removal
// some apps are notorious for closing a bunch of notifications at once
// causing newer notifications to move up and have a dialogs created for them
// just to then be discarded causing excess CPU usage
if (!m_pendingRemovals.contains(removedId)) {
m_pendingRemovals.append(removedId);
}
if (!m_pendingRemovalTimer.isActive()) {
m_pendingRemovalTimer.start();
}
}
|