Akonadi

agentinstancemodel.cpp
1/*
2 SPDX-FileCopyrightText: 2006 Tobias Koenig <tokoe@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "agentinstancemodel.h"
8
9#include "agentinstance.h"
10#include "agentmanager.h"
11
12#include <QIcon>
13
14#include <KLocalizedString>
15
16using namespace Akonadi;
17
18/**
19 * @internal
20 */
21class Akonadi::AgentInstanceModelPrivate
22{
23public:
24 explicit AgentInstanceModelPrivate(AgentInstanceModel *parent)
25 : mParent(parent)
26 {
27 }
28
29 AgentInstanceModel *const mParent;
30 AgentInstance::List mInstances;
31
32 void instanceAdded(const AgentInstance & /*instance*/);
33 void instanceRemoved(const AgentInstance & /*instance*/);
34 void instanceChanged(const AgentInstance & /*instance*/);
35};
36
37void AgentInstanceModelPrivate::instanceAdded(const AgentInstance &instance)
38{
39 mParent->beginInsertRows(QModelIndex(), mInstances.count(), mInstances.count());
40 mInstances.append(instance);
41 mParent->endInsertRows();
42}
43
44void AgentInstanceModelPrivate::instanceRemoved(const AgentInstance &instance)
45{
46 const int index = mInstances.indexOf(instance);
47 if (index == -1) {
48 return;
49 }
50
51 mParent->beginRemoveRows(QModelIndex(), index, index);
52 mInstances.removeAll(instance);
53 mParent->endRemoveRows();
54}
55
56void AgentInstanceModelPrivate::instanceChanged(const AgentInstance &instance)
57{
58 const int numberOfInstance(mInstances.count());
59 for (int i = 0; i < numberOfInstance; ++i) {
60 if (mInstances[i] == instance) {
61 // TODO why reassign it if equals ?
62 mInstances[i] = instance;
63
64 const QModelIndex idx = mParent->index(i, 0);
65 Q_EMIT mParent->dataChanged(idx, idx);
66
67 return;
68 }
69 }
70}
71
73 : QAbstractItemModel(parent)
74 , d(new AgentInstanceModelPrivate(this))
75{
76 d->mInstances = AgentManager::self()->instances();
77
79 d->instanceAdded(inst);
80 });
82 d->instanceRemoved(inst);
83 });
85 d->instanceChanged(inst);
86 });
88 d->instanceChanged(inst);
89 });
91 d->instanceChanged(inst);
92 });
94 d->instanceChanged(inst);
95 });
96}
97
99
100QHash<int, QByteArray> AgentInstanceModel::roleNames() const
101{
103 roles.insert(NameRole, QByteArrayLiteral("name"));
104 roles.insert(StatusRole, QByteArrayLiteral("status"));
105 roles.insert(StatusMessageRole, QByteArrayLiteral("statusMessage"));
106 roles.insert(ProgressRole, QByteArrayLiteral("progress"));
107 roles.insert(OnlineRole, QByteArrayLiteral("online"));
108 roles.insert(IconNameRole, QByteArrayLiteral("iconName"));
109 return roles;
110}
111
112int AgentInstanceModel::columnCount(const QModelIndex &index) const
113{
114 return index.isValid() ? 0 : 1;
115}
116
117int AgentInstanceModel::rowCount(const QModelIndex &index) const
118{
119 return index.isValid() ? 0 : d->mInstances.count();
120}
121
122QVariant AgentInstanceModel::data(const QModelIndex &index, int role) const
123{
124 if (!index.isValid()) {
125 return QVariant();
126 }
127
128 if (index.row() < 0 || index.row() >= d->mInstances.count()) {
129 return QVariant();
130 }
131
132 const AgentInstance &instance = d->mInstances[index.row()];
133
134 switch (role) {
135 case Qt::DisplayRole:
136 case NameRole:
137 return instance.name();
139 return instance.type().icon();
140 case IconNameRole:
141 return instance.type().icon().name();
142 case InstanceRole: {
143 QVariant var;
144 var.setValue(instance);
145 return var;
146 }
148 return instance.identifier();
149 case Qt::ToolTipRole:
150 return QStringLiteral("<qt><h4>%1</h4>%2</qt>").arg(instance.name(), instance.type().description());
151 case StatusRole:
152 return instance.status();
154 return instance.statusMessage();
155 case ProgressRole:
156 return instance.progress();
157 case OnlineRole:
158 return instance.isOnline();
159 case ActivitiesRole:
160 return instance.activities();
162 return instance.activitiesEnabled();
163 case TypeRole: {
164 QVariant var;
165 var.setValue(instance.type());
166 return var;
167 }
169 return instance.type().identifier();
170 case DescriptionRole:
171 return instance.type().description();
172 case CapabilitiesRole:
173 return instance.type().capabilities();
174 case MimeTypesRole:
175 return instance.type().mimeTypes();
176 }
177 return QVariant();
178}
179
180QVariant AgentInstanceModel::headerData(int section, Qt::Orientation orientation, int role) const
181{
182 if (orientation == Qt::Vertical) {
183 return QVariant();
184 }
185
186 if (role != Qt::DisplayRole) {
187 return QVariant();
188 }
189
190 switch (section) {
191 case 0:
192 return i18nc("@title:column, name of a thing", "Name");
193 default:
194 return QVariant();
195 }
196}
197
198QModelIndex AgentInstanceModel::index(int row, int column, const QModelIndex & /*parent*/) const
199{
200 if (row < 0 || row >= d->mInstances.count()) {
201 return QModelIndex();
202 }
203
204 if (column != 0) {
205 return QModelIndex();
206 }
207
208 return createIndex(row, column);
209}
210
212{
213 return QModelIndex();
214}
215
216Qt::ItemFlags AgentInstanceModel::flags(const QModelIndex &index) const
217{
218 if (!index.isValid() || index.row() < 0 || index.row() >= d->mInstances.count()) {
219 return QAbstractItemModel::flags(index);
220 }
221
223}
224
225bool AgentInstanceModel::setData(const QModelIndex &index, const QVariant &value, int role)
226{
227 if (!index.isValid()) {
228 return false;
229 }
230
231 if (index.row() < 0 || index.row() >= d->mInstances.count()) {
232 return false;
233 }
234
235 AgentInstance &instance = d->mInstances[index.row()];
236
237 switch (role) {
238 case OnlineRole:
239 instance.setIsOnline(value.toBool());
240 Q_EMIT dataChanged(index, index);
241 return true;
242 default:
243 return false;
244 }
245
246 return false;
247}
248
249#include "moc_agentinstancemodel.cpp"
Represents one agent instance and takes care of communication with it.
QString identifier() const
Set/get the unique identifier of this AgentInstance.
Provides a data model for agent instances.
AgentInstanceModel(QObject *parent=nullptr)
Creates a new agent instance model.
@ StatusMessageRole
A textual presentation of the current status.
@ ActivitiesRole
A list of activities.
@ OnlineRole
The current online/offline status.
@ StatusRole
The current status (numerical) of the instance.
@ MimeTypesRole
A list of supported mimetypes.
@ InstanceRole
The agent instance itself.
@ DescriptionRole
A description of the agent type.
@ InstanceIdentifierRole
The identifier of the agent instance.
@ TypeIdentifierRole
The icon name of the agent.
@ CapabilitiesRole
A list of supported capabilities.
@ ActivitiesEnabledRole
Return true if activities is enabled on agent.
@ IconNameRole
The display name of the agent type.
@ TypeRole
The agent type itself.
@ ProgressRole
The current progress (numerical in percent) of an operation.
~AgentInstanceModel() override
Destroys the agent instance model.
A representation of an agent instance.
void instanceProgressChanged(const Akonadi::AgentInstance &instance)
This signal is emitted whenever the progress of an agent instance has changed.
static AgentManager * self()
Returns the global instance of the agent manager.
void instanceRemoved(const Akonadi::AgentInstance &instance)
This signal is emitted whenever an agent instance was removed.
void instanceNameChanged(const Akonadi::AgentInstance &instance)
This signal is emitted whenever the name of the agent instance has changed.
void instanceAdded(const Akonadi::AgentInstance &instance)
This signal is emitted whenever a new agent instance was created.
void instanceOnline(const Akonadi::AgentInstance &instance, bool online)
This signal is emitted whenever the online state of an agent changed.
void instanceStatusChanged(const Akonadi::AgentInstance &instance)
This signal is emitted whenever the status of an agent instance has changed.
AgentInstance::List instances() const
Returns the list of all available agent instances.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
Helper integration between Akonadi and Qt.
void beginInsertRows(const QModelIndex &parent, int first, int last)
void beginRemoveRows(const QModelIndex &parent, int first, int last)
QModelIndex createIndex(int row, int column, const void *ptr) const const
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList< int > &roles)
virtual Qt::ItemFlags flags(const QModelIndex &index) const const
virtual QHash< int, QByteArray > roleNames() const const
iterator insert(const Key &key, const T &value)
void append(QList< T > &&value)
qsizetype count() const const
qsizetype indexOf(const AT &value, qsizetype from) const const
qsizetype removeAll(const AT &t)
bool isValid() const const
int row() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
QString arg(Args &&... args) const const
DisplayRole
typedef ItemFlags
Orientation
void setValue(QVariant &&value)
bool toBool() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:31:58 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.