Akonadi

agentinstancemodel.cpp
1 /*
2  SPDX-FileCopyrightText: 2006 Tobias Koenig <[email protected]>
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 
16 using namespace Akonadi;
17 
18 /**
19  * @internal
20  */
21 class Akonadi::AgentInstanceModelPrivate
22 {
23 public:
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 
37 void AgentInstanceModelPrivate::instanceAdded(const AgentInstance &instance)
38 {
39  mParent->beginInsertRows(QModelIndex(), mInstances.count(), mInstances.count());
40  mInstances.append(instance);
41  mParent->endInsertRows();
42 }
43 
44 void 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 
56 void 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 
100 QHash<int, QByteArray> AgentInstanceModel::roleNames() const
101 {
103  roles.insert(StatusRole, "status");
104  roles.insert(StatusMessageRole, "statusMessage");
105  roles.insert(ProgressRole, "progress");
106  roles.insert(OnlineRole, "online");
107  return roles;
108 }
109 
110 int AgentInstanceModel::columnCount(const QModelIndex &index) const
111 {
112  return index.isValid() ? 0 : 1;
113 }
114 
115 int AgentInstanceModel::rowCount(const QModelIndex &index) const
116 {
117  return index.isValid() ? 0 : d->mInstances.count();
118 }
119 
120 QVariant AgentInstanceModel::data(const QModelIndex &index, int role) const
121 {
122  if (!index.isValid()) {
123  return QVariant();
124  }
125 
126  if (index.row() < 0 || index.row() >= d->mInstances.count()) {
127  return QVariant();
128  }
129 
130  const AgentInstance &instance = d->mInstances[index.row()];
131 
132  switch (role) {
133  case Qt::DisplayRole:
134  return instance.name();
135  case Qt::DecorationRole:
136  return instance.type().icon();
137  case InstanceRole: {
138  QVariant var;
139  var.setValue(instance);
140  return var;
141  }
143  return instance.identifier();
144  case Qt::ToolTipRole:
145  return QStringLiteral("<qt><h4>%1</h4>%2</qt>").arg(instance.name(), instance.type().description());
146  case StatusRole:
147  return instance.status();
148  case StatusMessageRole:
149  return instance.statusMessage();
150  case ProgressRole:
151  return instance.progress();
152  case OnlineRole:
153  return instance.isOnline();
154  case TypeRole: {
155  QVariant var;
156  var.setValue(instance.type());
157  return var;
158  }
159  case TypeIdentifierRole:
160  return instance.type().identifier();
161  case DescriptionRole:
162  return instance.type().description();
163  case CapabilitiesRole:
164  return instance.type().capabilities();
165  case MimeTypesRole:
166  return instance.type().mimeTypes();
167  }
168  return QVariant();
169 }
170 
171 QVariant AgentInstanceModel::headerData(int section, Qt::Orientation orientation, int role) const
172 {
173  if (orientation == Qt::Vertical) {
174  return QVariant();
175  }
176 
177  if (role != Qt::DisplayRole) {
178  return QVariant();
179  }
180 
181  switch (section) {
182  case 0:
183  return i18nc("@title:column, name of a thing", "Name");
184  default:
185  return QVariant();
186  }
187 }
188 
189 QModelIndex AgentInstanceModel::index(int row, int column, const QModelIndex & /*parent*/) const
190 {
191  if (row < 0 || row >= d->mInstances.count()) {
192  return QModelIndex();
193  }
194 
195  if (column != 0) {
196  return QModelIndex();
197  }
198 
199  return createIndex(row, column);
200 }
201 
202 QModelIndex AgentInstanceModel::parent(const QModelIndex & /*child*/) const
203 {
204  return QModelIndex();
205 }
206 
207 Qt::ItemFlags AgentInstanceModel::flags(const QModelIndex &index) const
208 {
209  if (!index.isValid() || index.row() < 0 || index.row() >= d->mInstances.count()) {
210  return QAbstractItemModel::flags(index);
211  }
212 
214 }
215 
216 bool AgentInstanceModel::setData(const QModelIndex &index, const QVariant &value, int role)
217 {
218  if (!index.isValid()) {
219  return false;
220  }
221 
222  if (index.row() < 0 || index.row() >= d->mInstances.count()) {
223  return false;
224  }
225 
226  AgentInstance &instance = d->mInstances[index.row()];
227 
228  switch (role) {
229  case OnlineRole:
230  instance.setIsOnline(value.toBool());
231  Q_EMIT dataChanged(index, index);
232  return true;
233  default:
234  return false;
235  }
236 
237  return false;
238 }
239 
240 #include "moc_agentinstancemodel.cpp"
void instanceRemoved(const Akonadi::AgentInstance &instance)
This signal is emitted whenever an agent instance was removed.
@ ProgressRole
The current progress (numerical in percent) of an operation.
QString identifier() const
Returns the unique identifier of the agent instance.
@ InstanceRole
The agent instance itself.
DisplayRole
void setIsOnline(bool online)
Sets online status of the agent instance.
@ InstanceIdentifierRole
The identifier of the agent instance.
Status status() const
Returns the status of the agent instance.
AgentType type() const
Returns the agent type of this instance.
QString description() const
Returns the description of the agent type.
Q_EMITQ_EMIT
@ CapabilitiesRole
A list of supported capabilities.
void instanceNameChanged(const Akonadi::AgentInstance &instance)
This signal is emitted whenever the name of the agent instance has changed.
@ MimeTypesRole
A list of supported mimetypes.
QVector< AgentInstance > List
Describes a list of agent instances.
void instanceProgressChanged(const Akonadi::AgentInstance &instance)
This signal is emitted whenever the progress of an agent instance has changed.
void setValue(const T &value)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QHash::iterator insert(const Key &key, const T &value)
@ TypeRole
The agent type itself.
QStringList capabilities() const
Returns the list of supported capabilities of the agent type.
virtual QHash< int, QByteArray > roleNames() const const
QModelIndex createIndex(int row, int column, void *ptr) const const
typedef ItemFlags
@ DescriptionRole
A description of the agent type.
void instanceAdded(const Akonadi::AgentInstance &instance)
This signal is emitted whenever a new agent instance was created.
@ StatusMessageRole
A textual presentation of the current status.
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector< int > &roles)
Orientation
~AgentInstanceModel() override
Destroys the agent instance model.
virtual Qt::ItemFlags flags(const QModelIndex &index) const const
void instanceOnline(const Akonadi::AgentInstance &instance, bool online)
This signal is emitted whenever the online state of an agent changed.
bool isValid() const const
Provides a data model for agent instances.
bool toBool() const const
QString name() const
Returns the user visible name of the agent instance.
int row() const const
QString identifier() const
Returns the unique identifier of the agent type.
bool isOnline() const
Returns whether the agent instance is online currently.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
static AgentManager * self()
Returns the global instance of the agent manager.
AgentInstance::List instances() const
Returns the list of all available agent instances.
QIcon icon() const
Returns the icon of the agent type.
@ TypeIdentifierRole
The identifier of the agent type.
int progress() const
Returns the progress of the agent instance in percent, or -1 if no progress information are available...
AgentInstanceModel(QObject *parent=nullptr)
Creates a new agent instance model.
@ OnlineRole
The current online/offline status.
@ StatusRole
The current status (numerical) of the instance.
QString statusMessage() const
Returns a textual presentation of the status of the agent instance.
void instanceStatusChanged(const Akonadi::AgentInstance &instance)
This signal is emitted whenever the status of an agent instance has changed.
A representation of an agent instance.
QStringList mimeTypes() const
Returns the list of supported mime types of the agent type.
QObject * parent() const const
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Wed Jun 7 2023 03:53:30 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.