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
|
/*
SPDX-FileCopyrightText: 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 "jobsmodel.h"
#include "jobsmodel_p.h"
#include "utils_p.h"
#include <QDebug>
#include <KJob>
#include <KLocalizedString>
#include <Plasma/DataEngine>
#include <Plasma/DataEngineConsumer>
#include <Plasma/ServiceJob>
#include "job.h"
#include "job_p.h"
using namespace NotificationManager;
JobsModel::JobsModel()
: QAbstractListModel(nullptr)
, d(new JobsModelPrivate(this))
{
connect(d, &JobsModelPrivate::jobViewAboutToBeAdded, this, [this](int row, Job *job) {
Q_UNUSED(job);
beginInsertRows(QModelIndex(), row, row);
});
connect(d, &JobsModelPrivate::jobViewAdded, this, [this](int row) {
Q_UNUSED(row);
endInsertRows();
});
connect(d, &JobsModelPrivate::jobViewAboutToBeRemoved, this, [this](int row) {
beginRemoveRows(QModelIndex(), row, row);
});
connect(d, &JobsModelPrivate::jobViewRemoved, this, [this](int row) {
Q_UNUSED(row);
endRemoveRows();
});
connect(d, &JobsModelPrivate::jobViewChanged, this, [this](int row, Job *job, const QVector<int> &roles) {
Q_UNUSED(job);
const QModelIndex idx = index(row, 0);
Q_EMIT dataChanged(idx, idx, roles);
});
connect(d, &JobsModelPrivate::serviceOwnershipLost, this, &JobsModel::serviceOwnershipLost);
}
JobsModel::~JobsModel() = default;
JobsModel::Ptr JobsModel::createJobsModel()
{
static QWeakPointer<JobsModel> s_instance;
if (!s_instance) {
QSharedPointer<JobsModel> ptr(new JobsModel());
s_instance = ptr.toWeakRef();
return ptr;
}
return s_instance.toStrongRef();
}
bool JobsModel::init()
{
return d->init();
}
bool JobsModel::isValid() const
{
return d->m_valid;
}
QVariant JobsModel::data(const QModelIndex &index, int role) const
{
if (!checkIndex(index, QAbstractItemModel::CheckIndexOption::IndexIsValid)) {
return QVariant();
}
Job *job = d->m_jobViews.at(index.row());
switch (role) {
case Notifications::IdRole:
return job->id();
case Notifications::TypeRole:
return Notifications::JobType;
// basically when it started
case Notifications::CreatedRole:
if (job->created().isValid()) {
return job->created();
}
break;
// basically when it finished
case Notifications::UpdatedRole:
if (job->updated().isValid()) {
return job->updated();
}
break;
case Notifications::SummaryRole:
return job->summary();
case Notifications::BodyRole:
return job->text();
case Qt::AccessibleDescriptionRole:
return i18nc("@info %1 notification body %2 job name", "%1 from %2", job->text(), job->applicationName());
case Notifications::DesktopEntryRole:
return job->desktopEntry();
case Notifications::ApplicationNameRole:
return job->applicationName();
case Notifications::ApplicationIconNameRole:
return job->applicationIconName();
case Notifications::JobStateRole:
return job->state();
case Notifications::PercentageRole:
return job->percentage();
case Notifications::JobErrorRole:
return job->error();
case Notifications::SuspendableRole:
return job->suspendable();
case Notifications::KillableRole:
return job->killable();
case Notifications::JobDetailsRole:
return QVariant::fromValue(job);
// successfully finished jobs timeout like a regular notifiation
// whereas running or error'd jobs are persistent
case Notifications::TimeoutRole:
return job->state() == Notifications::JobStateStopped && !job->error() ? -1 : 0;
case Notifications::ClosableRole:
return job->state() == Notifications::JobStateStopped;
case Notifications::ConfigurableRole:
return false;
case Notifications::ExpiredRole:
return job->expired();
case Notifications::DismissedRole:
return job->dismissed();
// A job is usually either a long lasting operation you're aware about
// or a quick job you don't care about.
// When it's running, it's there, when it failed, it's persistent.
// There's hardly a reason why it should show up as "unread".
case Notifications::ReadRole:
return true;
}
return QVariant();
}
bool JobsModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (!checkIndex(index, QAbstractItemModel::CheckIndexOption::IndexIsValid)) {
return false;
}
Job *job = d->m_jobViews.at(index.row());
switch (role) {
case Notifications::DismissedRole:
if (value.toBool() != job->dismissed()) {
job->setDismissed(value.toBool());
return true;
}
break;
}
return false;
}
int JobsModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid()) {
return 0;
}
return d->m_jobViews.count();
}
QHash<int, QByteArray> JobsModel::roleNames() const
{
return Utils::roleNames();
}
void JobsModel::close(const QModelIndex &idx)
{
if (checkIndex(idx, QAbstractItemModel::CheckIndexOption::IndexIsValid)) {
d->removeAt(idx.row());
}
}
void JobsModel::expire(const QModelIndex &idx)
{
if (checkIndex(idx, QAbstractItemModel::CheckIndexOption::IndexIsValid)) {
d->m_jobViews.at(idx.row())->setExpired(true);
}
}
void JobsModel::suspend(const QModelIndex &idx)
{
if (checkIndex(idx, QAbstractItemModel::CheckIndexOption::IndexIsValid)) {
d->m_jobViews.at(idx.row())->suspend();
}
}
void JobsModel::resume(const QModelIndex &idx)
{
if (checkIndex(idx, QAbstractItemModel::CheckIndexOption::IndexIsValid)) {
d->m_jobViews.at(idx.row())->resume();
}
}
void JobsModel::kill(const QModelIndex &idx)
{
if (checkIndex(idx, QAbstractItemModel::CheckIndexOption::IndexIsValid)) {
d->m_jobViews.at(idx.row())->kill();
}
}
void JobsModel::clear(Notifications::ClearFlags flags)
{
if (d->m_jobViews.isEmpty()) {
return;
}
for (int i = d->m_jobViews.count() - 1; i >= 0; --i) {
Job *job = d->m_jobViews.at(i);
bool clear = (flags.testFlag(Notifications::ClearExpired) && job->expired());
// Compared to notifications, the number of jobs is typically small
// so for simplicity we can just delete one item at a time
if (clear) {
d->removeAt(i);
}
}
}
|