File indexing completed on 2024-04-28 16:54:37

0001 /*
0002     SPDX-FileCopyrightText: 2018-2019 Kai Uwe Broulik <kde@privat.broulik.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include "notificationsortproxymodel_p.h"
0008 
0009 #include <QDateTime>
0010 
0011 #include "notifications.h"
0012 
0013 using namespace NotificationManager;
0014 
0015 NotificationSortProxyModel::NotificationSortProxyModel(QObject *parent)
0016     : QSortFilterProxyModel(parent)
0017 {
0018     setRecursiveFilteringEnabled(true);
0019     sort(0);
0020 }
0021 
0022 NotificationSortProxyModel::~NotificationSortProxyModel() = default;
0023 
0024 Notifications::SortMode NotificationSortProxyModel::sortMode() const
0025 {
0026     return m_sortMode;
0027 }
0028 
0029 void NotificationSortProxyModel::setSortMode(Notifications::SortMode sortMode)
0030 {
0031     if (m_sortMode != sortMode) {
0032         m_sortMode = sortMode;
0033         invalidate();
0034         Q_EMIT sortModeChanged();
0035     }
0036 }
0037 
0038 Qt::SortOrder NotificationSortProxyModel::sortOrder() const
0039 {
0040     return m_sortOrder;
0041 }
0042 
0043 void NotificationSortProxyModel::setSortOrder(Qt::SortOrder sortOrder)
0044 {
0045     if (m_sortOrder != sortOrder) {
0046         m_sortOrder = sortOrder;
0047         invalidate();
0048         Q_EMIT sortOrderChanged();
0049     }
0050 }
0051 
0052 int sortScore(const QModelIndex &idx)
0053 {
0054     const auto urgency = idx.data(Notifications::UrgencyRole).toInt();
0055     if (urgency == Notifications::CriticalUrgency) {
0056         return 3;
0057     }
0058 
0059     if (idx.data(Notifications::TypeRole).toInt() == Notifications::JobType) {
0060         return 2;
0061     }
0062 
0063     if (urgency == Notifications::NormalUrgency) {
0064         return 1;
0065     }
0066 
0067     if (urgency == Notifications::LowUrgency) {
0068         return 0;
0069     }
0070 
0071     return -1;
0072 }
0073 
0074 bool NotificationSortProxyModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
0075 {
0076     // Sort order is (descending):
0077     // - Critical notifications
0078     // - Jobs
0079     // - Normal notifications
0080     // - Low urgency notifications
0081     // Within each group it's descending by created or last modified
0082 
0083     int scoreLeft = 0;
0084     int scoreRight = 0;
0085 
0086     if (m_sortMode == Notifications::SortByTypeAndUrgency) {
0087         scoreLeft = sortScore(source_left);
0088         Q_ASSERT(scoreLeft >= 0);
0089         scoreRight = sortScore(source_right);
0090         Q_ASSERT(scoreRight >= 0);
0091     }
0092 
0093     if (scoreLeft == scoreRight) {
0094         const QDateTime timeLeft = source_left.data(Notifications::CreatedRole).toDateTime();
0095         const QDateTime timeRight = source_right.data(Notifications::CreatedRole).toDateTime();
0096 
0097         if (m_sortOrder == Qt::DescendingOrder) {
0098             return timeLeft > timeRight;
0099         } else {
0100             return timeLeft < timeRight;
0101         }
0102     }
0103 
0104     return scoreLeft > scoreRight;
0105 }