KCGroups

kapplicationscopelistmodel.cpp
1// SPDX-FileCopyrightText: 2020 Henri Chain <henri.chain@enioka.com>
2// SPDX-FileCopyrightText: 2020 Kevin Ottens <kevin.ottens@enioka.com>
3//
4// SPDX-License-Identifier: LGPL-2.1-or-later
5
6#include "kapplicationscopelistmodel.h"
7#include "kapplicationscopelistmodel_p.h"
8#include "kcgroups_debug.h"
9#include <QDebug>
10#include <algorithm>
11
12KApplicationScopeListModel::KApplicationScopeListModel(QObject *parent)
13 : KApplicationScopeListModel(nullptr, parent)
14{
15}
16
17KApplicationScopeListModel::KApplicationScopeListModel(KApplicationScopeLister *lister, QObject *parent)
18 : QAbstractListModel(parent)
19 , d_ptr(new KApplicationScopeListModelPrivate(lister, this))
20{
21}
22
23KApplicationScopeListModel::~KApplicationScopeListModel()
24{
25 delete d_ptr;
26}
27
28QVariant KApplicationScopeListModel::data(const QModelIndex &index, int role) const
29{
30 const auto row = index.row();
31 if (row < d_ptr->m_apps.length()) {
32 auto *app = d_ptr->m_apps[row];
33 switch (role) {
34 case Qt::DisplayRole:
35 return !app->description().isEmpty() ? app->description() : !app->desktopName().isEmpty() ? app->desktopName() : app->id();
36 case ObjectRole:
37 return QVariant::fromValue(app);
38 }
39 }
40 return QVariant();
41}
42
43int KApplicationScopeListModel::rowCount(const QModelIndex &parent) const
44{
45 Q_UNUSED(parent);
46 return d_ptr->m_apps.size();
47}
48
49QHash<int, QByteArray> KApplicationScopeListModel::roleNames() const
50{
51 return {{Qt::DisplayRole, "display"}, {ObjectRole, "object"}};
52}
53
54KApplicationScopeListModelPrivate::KApplicationScopeListModelPrivate(KApplicationScopeLister *lister,
56 : m_apps({})
57 , q_ptr(parent)
58 , m_lister(lister == nullptr ? new KApplicationScopeLister(parent) : lister)
59{
60 QObject::connect(m_lister,
62 q_ptr,
63 [this](const QString &path, const QString &id) { handleNewApp(path, id); });
64 QObject::connect(m_lister, &KApplicationScopeLister::pathRemoved, q_ptr, [this](const QString &path) {
65 handleRemovedApp(path);
66 });
67}
68
69void KApplicationScopeListModelPrivate::handleNewApp(const QString &path, const QString &id)
70{
71 const auto row = m_apps.size();
72 q_ptr->beginInsertRows(QModelIndex(), row, row);
73 const auto app = new KApplicationScope(path, id, q_ptr);
74 QObject::connect(app, &KApplicationScope::propertyChanged, q_ptr, [this, app]() {
75 const auto row = m_apps.indexOf(app);
76 if (row >= 0) {
77 const auto idx = q_ptr->index(row, 0);
78 emit q_ptr->dataChanged(idx, idx);
79 }
80 });
81 m_apps.append(app);
82 q_ptr->endInsertRows();
83}
84
85void KApplicationScopeListModelPrivate::handleRemovedApp(const QString &path)
86{
87 const auto it = std::find_if(
88 m_apps.begin(), m_apps.end(), [&path](const KApplicationScope *app) { return app->path() == path; });
89 if (it != m_apps.end()) {
90 const auto *app = *it;
91 const auto row = static_cast<int>(std::distance(m_apps.begin(), it));
92 q_ptr->beginRemoveRows(QModelIndex(), row, row);
93 delete app;
94 m_apps.removeAt(row);
95 q_ptr->endRemoveRows();
96 } else {
97 qCDebug(KCGROUPS_LOG) << "app not found:" << path;
98 }
99}
A ListModel for use in QML to interact with an updated list of KApplicationScope's.
@ ObjectRole
Corresponds to KApplicationScope object.
Keeps an updated list of desktop application systemd scopes.
void pathRemoved(const QString &path)
emitted when an application is stopped
void pathAdded(const QString &path, const QString &id)
emitted when a new application is launched
A desktop application in a systemd transient scope.
void propertyChanged(const QString &propertyName)
emitted when any cgroup resource property has changed
QString path(const QString &relativePath)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
int row() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
DisplayRole
QVariant fromValue(T &&value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:14:41 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.