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 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
|
/*
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 "abstractnotificationsmodel.h"
#include "abstractnotificationsmodel_p.h"
#include "debug.h"
#include "utils_p.h"
#include "notification_p.h"
#include <QDebug>
#include <QProcess>
#include <QTextDocumentFragment>
#include <KLocalizedString>
#include <KShell>
#include <algorithm>
#include <chrono>
#include <functional>
using namespace std::chrono_literals;
static const int s_notificationsLimit = 1000;
using namespace NotificationManager;
AbstractNotificationsModel::Private::Private(AbstractNotificationsModel *q)
: q(q)
, lastRead(QDateTime::currentDateTimeUtc())
{
pendingRemovalTimer.setSingleShot(true);
pendingRemovalTimer.setInterval(50ms);
connect(&pendingRemovalTimer, &QTimer::timeout, q, [this, q] {
QVector<int> rowsToBeRemoved;
rowsToBeRemoved.reserve(pendingRemovals.count());
for (uint id : qAsConst(pendingRemovals)) {
int row = q->rowOfNotification(id); // oh the complexity...
if (row == -1) {
continue;
}
rowsToBeRemoved.append(row);
}
removeRows(rowsToBeRemoved);
});
}
AbstractNotificationsModel::Private::~Private()
{
qDeleteAll(notificationTimeouts);
notificationTimeouts.clear();
}
void AbstractNotificationsModel::Private::onNotificationAdded(const Notification ¬ification)
{
// Once we reach a certain insane number of notifications discard some old ones
// as we keep pixmaps around etc
if (notifications.count() >= s_notificationsLimit) {
const int cleanupCount = s_notificationsLimit / 2;
qCDebug(NOTIFICATIONMANAGER) << "Reached the notification limit of" << s_notificationsLimit << ", discarding the oldest" << cleanupCount
<< "notifications";
q->beginRemoveRows(QModelIndex(), 0, cleanupCount - 1);
for (int i = 0; i < cleanupCount; ++i) {
notifications.removeAt(0);
// TODO close gracefully?
}
q->endRemoveRows();
}
setupNotificationTimeout(notification);
q->beginInsertRows(QModelIndex(), notifications.count(), notifications.count());
notifications.append(std::move(notification));
q->endInsertRows();
}
void AbstractNotificationsModel::Private::onNotificationReplaced(uint replacedId, const Notification ¬ification)
{
const int row = q->rowOfNotification(replacedId);
if (row == -1) {
qCWarning(NOTIFICATIONMANAGER) << "Trying to replace notification with id" << replacedId
<< "which doesn't exist, creating a new one. This is an application bug!";
onNotificationAdded(notification);
return;
}
setupNotificationTimeout(notification);
Notification newNotification(notification);
const Notification &oldNotification = notifications.at(row);
// As per spec a notification must be replaced atomically with no visual cues.
// Transfer over properties that might cause this, such as unread showing the bell again,
// or created() which should indicate the original date, whereas updated() is when it was last updated
newNotification.setCreated(oldNotification.created());
newNotification.setExpired(oldNotification.expired());
newNotification.setDismissed(oldNotification.dismissed());
newNotification.setRead(oldNotification.read());
notifications[row] = newNotification;
const QModelIndex idx = q->index(row, 0);
Q_EMIT q->dataChanged(idx, idx);
}
void AbstractNotificationsModel::Private::onNotificationRemoved(uint removedId, Server::CloseReason reason)
{
const int row = q->rowOfNotification(removedId);
if (row == -1) {
return;
}
q->stopTimeout(removedId);
// When a notification expired, keep it around in the history and mark it as such
if (reason == Server::CloseReason::Expired) {
const QModelIndex idx = q->index(row, 0);
Notification ¬ification = notifications[row];
notification.setExpired(true);
// Since the notification is "closed" it cannot have any actions
// unless it is "resident" which we don't support
notification.setActions(QStringList());
// clang-format off
Q_EMIT q->dataChanged(idx, idx, {
Notifications::ExpiredRole,
// TODO only Q_EMIT those if actually changed?
Notifications::ActionNamesRole,
Notifications::ActionLabelsRole,
Notifications::HasDefaultActionRole,
Notifications::DefaultActionLabelRole,
Notifications::ConfigurableRole
});
// clang-format on
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 (!pendingRemovals.contains(removedId)) {
pendingRemovals.append(removedId);
}
if (!pendingRemovalTimer.isActive()) {
pendingRemovalTimer.start();
}
}
void AbstractNotificationsModel::Private::setupNotificationTimeout(const Notification ¬ification)
{
if (notification.timeout() == 0) {
// In case it got replaced by a persistent notification
q->stopTimeout(notification.id());
return;
}
QTimer *timer = notificationTimeouts.value(notification.id());
if (!timer) {
timer = new QTimer();
timer->setSingleShot(true);
connect(timer, &QTimer::timeout, q, [this, timer] {
const uint id = timer->property("notificationId").toUInt();
q->expire(id);
});
notificationTimeouts.insert(notification.id(), timer);
}
timer->stop();
timer->setProperty("notificationId", notification.id());
timer->setInterval(60000 /*1min*/ + (notification.timeout() == -1 ? 120000 /*2min, max configurable default timeout*/ : notification.timeout()));
timer->start();
}
void AbstractNotificationsModel::Private::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);
q->beginRemoveRows(QModelIndex(), range.first, range.second);
for (int j = range.second; j >= range.first; --j) {
notifications.removeAt(j);
++rowsRemoved;
}
q->endRemoveRows();
}
Q_ASSERT(rowsRemoved == rowsToBeRemoved.count());
pendingRemovals.clear();
}
int AbstractNotificationsModel::rowOfNotification(uint id) const
{
auto it = std::find_if(d->notifications.constBegin(), d->notifications.constEnd(), [id](const Notification &item) {
return item.id() == id;
});
if (it == d->notifications.constEnd()) {
return -1;
}
return std::distance(d->notifications.constBegin(), it);
}
AbstractNotificationsModel::AbstractNotificationsModel()
: QAbstractListModel(nullptr)
, d(new Private(this))
{
}
AbstractNotificationsModel::~AbstractNotificationsModel() = default;
QDateTime AbstractNotificationsModel::lastRead() const
{
return d->lastRead;
}
void AbstractNotificationsModel::setLastRead(const QDateTime &lastRead)
{
if (d->lastRead != lastRead) {
d->lastRead = lastRead;
Q_EMIT lastReadChanged();
}
}
QWindow *AbstractNotificationsModel::window() const
{
return d->window;
}
void AbstractNotificationsModel::setWindow(QWindow *window)
{
if (d->window == window) {
return;
}
if (d->window) {
disconnect(d->window, &QObject::destroyed, this, nullptr);
}
d->window = window;
if (d->window) {
connect(d->window, &QObject::destroyed, this, [this] {
setWindow(nullptr);
});
}
Q_EMIT windowChanged(window);
}
QVariant AbstractNotificationsModel::data(const QModelIndex &index, int role) const
{
if (!checkIndex(index, QAbstractItemModel::CheckIndexOption::IndexIsValid)) {
return QVariant();
}
const Notification ¬ification = d->notifications.at(index.row());
switch (role) {
case Notifications::IdRole:
return notification.id();
case Notifications::TypeRole:
return Notifications::NotificationType;
case Notifications::CreatedRole:
if (notification.created().isValid()) {
return notification.created();
}
break;
case Notifications::UpdatedRole:
if (notification.updated().isValid()) {
return notification.updated();
}
break;
case Notifications::SummaryRole:
return notification.summary();
case Notifications::BodyRole:
return notification.body();
case Qt::AccessibleDescriptionRole:
return i18nc("@info %1 notification body %2 application name",
"%1 from %2",
QTextDocumentFragment::fromHtml(notification.body()).toPlainText(),
notification.applicationName());
case Notifications::IconNameRole:
if (notification.image().isNull()) {
return notification.icon();
}
break;
case Notifications::ImageRole:
if (!notification.image().isNull()) {
return notification.image();
}
break;
case Notifications::DesktopEntryRole:
return notification.desktopEntry();
case Notifications::NotifyRcNameRole:
return notification.notifyRcName();
case Notifications::ApplicationNameRole:
return notification.applicationName();
case Notifications::ApplicationIconNameRole:
return notification.applicationIconName();
case Notifications::OriginNameRole:
return notification.originName();
case Notifications::ActionNamesRole:
return notification.actionNames();
case Notifications::ActionLabelsRole:
return notification.actionLabels();
case Notifications::HasDefaultActionRole:
return notification.hasDefaultAction();
case Notifications::DefaultActionLabelRole:
return notification.defaultActionLabel();
case Notifications::UrlsRole:
return QVariant::fromValue(notification.urls());
case Notifications::UrgencyRole:
return static_cast<int>(notification.urgency());
case Notifications::UserActionFeedbackRole:
return notification.userActionFeedback();
case Notifications::TimeoutRole:
return notification.timeout();
case Notifications::ClosableRole:
return true;
case Notifications::ConfigurableRole:
return notification.configurable();
case Notifications::ConfigureActionLabelRole:
return notification.configureActionLabel();
case Notifications::CategoryRole:
return notification.category();
case Notifications::ExpiredRole:
return notification.expired();
case Notifications::ReadRole:
return notification.read();
case Notifications::ResidentRole:
return notification.resident();
case Notifications::TransientRole:
return notification.transient();
case Notifications::HasReplyActionRole:
return notification.hasReplyAction();
case Notifications::ReplyActionLabelRole:
return notification.replyActionLabel();
case Notifications::ReplyPlaceholderTextRole:
return notification.replyPlaceholderText();
case Notifications::ReplySubmitButtonTextRole:
return notification.replySubmitButtonText();
case Notifications::ReplySubmitButtonIconNameRole:
return notification.replySubmitButtonIconName();
}
return QVariant();
}
bool AbstractNotificationsModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (!checkIndex(index, QAbstractItemModel::CheckIndexOption::IndexIsValid)) {
return false;
}
Notification ¬ification = d->notifications[index.row()];
bool dirty = false;
switch (role) {
case Notifications::ReadRole:
if (value.toBool() != notification.read()) {
notification.setRead(value.toBool());
dirty = true;
}
break;
// Allows to mark a notification as expired without actually sending that out through expire() for persistency
case Notifications::ExpiredRole:
if (value.toBool() != notification.expired()) {
notification.setExpired(value.toBool());
dirty = true;
}
break;
}
if (dirty) {
Q_EMIT dataChanged(index, index, {role});
}
return dirty;
}
int AbstractNotificationsModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid()) {
return 0;
}
return d->notifications.count();
}
QHash<int, QByteArray> AbstractNotificationsModel::roleNames() const
{
return Utils::roleNames();
}
void AbstractNotificationsModel::startTimeout(uint notificationId)
{
const int row = rowOfNotification(notificationId);
if (row == -1) {
return;
}
const Notification ¬ification = d->notifications.at(row);
if (!notification.timeout() || notification.expired()) {
return;
}
d->setupNotificationTimeout(notification);
}
void AbstractNotificationsModel::stopTimeout(uint notificationId)
{
delete d->notificationTimeouts.take(notificationId);
}
void AbstractNotificationsModel::clear(Notifications::ClearFlags flags)
{
if (d->notifications.isEmpty()) {
return;
}
QVector<int> rowsToRemove;
for (int i = 0; i < d->notifications.count(); ++i) {
const Notification ¬ification = d->notifications.at(i);
if (flags.testFlag(Notifications::ClearExpired) && notification.expired()) {
close(notification.id());
}
}
}
void AbstractNotificationsModel::onNotificationAdded(const Notification ¬ification)
{
d->onNotificationAdded(notification);
}
void AbstractNotificationsModel::onNotificationReplaced(uint replacedId, const Notification ¬ification)
{
d->onNotificationReplaced(replacedId, notification);
}
void AbstractNotificationsModel::onNotificationRemoved(uint notificationId, Server::CloseReason reason)
{
d->onNotificationRemoved(notificationId, reason);
}
void AbstractNotificationsModel::setupNotificationTimeout(const Notification ¬ification)
{
d->setupNotificationTimeout(notification);
}
const QVector<Notification> &AbstractNotificationsModel::notifications()
{
return d->notifications;
}
|