File indexing completed on 2024-05-12 05:37:08

0001 /*
0002     SPDX-FileCopyrightText: 2022 Kai Uwe Broulik <kde@broulik.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include "jobaggregator.h"
0008 
0009 #include "notifications.h"
0010 
0011 JobAggregator::JobAggregator(QObject *parent)
0012     : QObject(parent)
0013 {
0014 }
0015 
0016 JobAggregator::~JobAggregator() = default;
0017 
0018 QAbstractItemModel *JobAggregator::sourceModel() const
0019 {
0020     return m_model.data();
0021 }
0022 
0023 void JobAggregator::setSourceModel(QAbstractItemModel *sourceModel)
0024 {
0025     if (m_model == sourceModel) {
0026         return;
0027     }
0028 
0029     if (m_model) {
0030         disconnect(m_model, nullptr, this, nullptr);
0031     }
0032     m_model = sourceModel;
0033     if (m_model) {
0034         connect(m_model.data(), &QAbstractItemModel::modelReset, this, &JobAggregator::update);
0035         connect(m_model.data(), &QAbstractItemModel::rowsInserted, this, &JobAggregator::update);
0036         connect(m_model.data(), &QAbstractItemModel::rowsRemoved, this, &JobAggregator::update);
0037 
0038         connect(m_model.data(),
0039                 &QAbstractItemModel::dataChanged,
0040                 this,
0041                 [this](const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList<int> &roles) {
0042                     Q_UNUSED(topLeft);
0043                     Q_UNUSED(bottomRight);
0044 
0045                     if (roles.isEmpty() || roles.contains(NotificationManager::Notifications::SummaryRole)
0046                         || roles.contains(NotificationManager::Notifications::PercentageRole)
0047                         || roles.contains(NotificationManager::Notifications::JobStateRole)) {
0048                         update();
0049                     }
0050                 });
0051     }
0052     Q_EMIT sourceModelChanged();
0053 
0054     update();
0055 }
0056 
0057 int JobAggregator::count() const
0058 {
0059     return m_count;
0060 }
0061 
0062 QString JobAggregator::summary() const
0063 {
0064     return m_summary;
0065 }
0066 
0067 int JobAggregator::percentage() const
0068 {
0069     return m_percentage;
0070 }
0071 
0072 void JobAggregator::update()
0073 {
0074     if (!m_model) {
0075         return;
0076     }
0077 
0078     int count = 0;
0079     QString combinedSummary;
0080     int totalPercentage = 0;
0081 
0082     for (int i = 0; i < m_model->rowCount(); ++i) {
0083         const QModelIndex idx = m_model->index(i, 0);
0084 
0085         if (idx.data(NotificationManager::Notifications::JobStateRole).toInt() == NotificationManager::Notifications::JobStateStopped
0086             || idx.data(NotificationManager::Notifications::TypeRole).toInt() != NotificationManager::Notifications::JobType) {
0087             continue;
0088         }
0089 
0090         const QString summary = idx.data(NotificationManager::Notifications::SummaryRole).toString();
0091 
0092         // Include summary only if it's the same for all jobs
0093         if (i == 0) {
0094             combinedSummary = summary;
0095         } else if (combinedSummary != summary) {
0096             combinedSummary.clear();
0097         }
0098 
0099         const int percentage = idx.data(NotificationManager::Notifications::PercentageRole).toInt();
0100         totalPercentage += percentage;
0101 
0102         ++count;
0103     }
0104 
0105     if (m_count != count) {
0106         m_count = count;
0107         Q_EMIT countChanged();
0108     }
0109 
0110     if (m_summary != combinedSummary) {
0111         m_summary = combinedSummary;
0112         Q_EMIT summaryChanged();
0113     }
0114 
0115     const int percentage = (count > 0 ? totalPercentage / count : 0);
0116     if (m_percentage != percentage) {
0117         m_percentage = percentage;
0118         Q_EMIT percentageChanged();
0119     }
0120 }