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, "name");
104 roles.insert(StatusRole, "status");
105 roles.insert(StatusMessageRole, "statusMessage");
106 roles.insert(ProgressRole, "progress");
107 roles.insert(OnlineRole, "online");
108 roles.insert(IconNameRole, "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: {
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 TypeRole: {
161 var.setValue(instance.type());
162 return var;
163 }
165 return instance.type().identifier();
166 case DescriptionRole:
167 return instance.type().description();
168 case CapabilitiesRole:
169 return instance.type().capabilities();
170 case MimeTypesRole:
171 return instance.type().mimeTypes();
172 }
173 return QVariant();
174}
175
176QVariant AgentInstanceModel::headerData(int section, Qt::Orientation orientation, int role) const
177{
178 if (orientation == Qt::Vertical) {
179 return QVariant();
180 }
181
182 if (role != Qt::DisplayRole) {
183 return QVariant();
184 }
185
186 switch (section) {
187 case 0:
188 return i18nc("@title:column, name of a thing", "Name");
189 default:
190 return QVariant();
191 }
192}
193
194QModelIndex AgentInstanceModel::index(int row, int column, const QModelIndex & /*parent*/) const
195{
196 if (row < 0 || row >= d->mInstances.count()) {
197 return QModelIndex();
198 }
199
200 if (column != 0) {
201 return QModelIndex();
202 }
203
204 return createIndex(row, column);
205}
206
208{
209 return QModelIndex();
210}
211
212Qt::ItemFlags AgentInstanceModel::flags(const QModelIndex &index) const
213{
214 if (!index.isValid() || index.row() < 0 || index.row() >= d->mInstances.count()) {
215 return QAbstractItemModel::flags(index);
216 }
217
219}
220
221bool AgentInstanceModel::setData(const QModelIndex &index, const QVariant &value, int role)
222{
223 if (!index.isValid()) {
224 return false;
225 }
226
227 if (index.row() < 0 || index.row() >= d->mInstances.count()) {
228 return false;
229 }
230
231 AgentInstance &instance = d->mInstances[index.row()];
232
233 switch (role) {
234 case OnlineRole:
235 instance.setIsOnline(value.toBool());
236 Q_EMIT dataChanged(index, index);
237 return true;
238 default:
239 return false;
240 }
241
242 return false;
243}
244
245#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.
@ 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.
@ 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
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
T qobject_cast(QObject *object)
QString arg(Args &&... args) const const
DisplayRole
typedef ItemFlags
Orientation
bool toBool() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:13:38 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.