Akonadi

agentinstancemodel.cpp
1 /*
2  Copyright (c) 2006 Tobias Koenig <[email protected]>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 */
19 
20 #include "agentinstancemodel.h"
21 
22 #include "agentinstance.h"
23 #include "agentmanager.h"
24 
25 #include <QIcon>
26 
27 #include <KLocalizedString>
28 
29 using namespace Akonadi;
30 
34 class Q_DECL_HIDDEN AgentInstanceModel::Private
35 {
36 public:
37  Private(AgentInstanceModel *parent)
38  : mParent(parent)
39  {
40  }
41 
42  AgentInstanceModel *mParent = nullptr;
43  AgentInstance::List mInstances;
44 
45  void instanceAdded(const AgentInstance &);
46  void instanceRemoved(const AgentInstance &);
47  void instanceChanged(const AgentInstance &);
48 };
49 
50 void AgentInstanceModel::Private::instanceAdded(const AgentInstance &instance)
51 {
52  mParent->beginInsertRows(QModelIndex(), mInstances.count(), mInstances.count());
53  mInstances.append(instance);
54  mParent->endInsertRows();
55 }
56 
57 void AgentInstanceModel::Private::instanceRemoved(const AgentInstance &instance)
58 {
59  const int index = mInstances.indexOf(instance);
60  if (index == -1) {
61  return;
62  }
63 
64  mParent->beginRemoveRows(QModelIndex(), index, index);
65  mInstances.removeAll(instance);
66  mParent->endRemoveRows();
67 }
68 
69 void AgentInstanceModel::Private::instanceChanged(const AgentInstance &instance)
70 {
71  const int numberOfInstance(mInstances.count());
72  for (int i = 0; i < numberOfInstance; ++i) {
73  if (mInstances[i] == instance) {
74  //TODO why reassign it if equals ?
75  mInstances[i] = instance;
76 
77  const QModelIndex idx = mParent->index(i, 0);
78  Q_EMIT mParent->dataChanged(idx, idx);
79 
80  return;
81  }
82  }
83 }
84 
86  : QAbstractItemModel(parent)
87  , d(new Private(this))
88 {
89  d->mInstances = AgentManager::self()->instances();
90 
92  this, [this](const Akonadi::AgentInstance &inst) { d->instanceAdded(inst);});
94  this, [this](const Akonadi::AgentInstance &inst) { d->instanceRemoved(inst);});
96  this, [this](const Akonadi::AgentInstance &inst) { d->instanceChanged(inst);});
98  this, [this](const Akonadi::AgentInstance &inst) { d->instanceChanged(inst);});
100  this, [this](const Akonadi::AgentInstance &inst) { d->instanceChanged(inst);});
102  this, [this](const Akonadi::AgentInstance &inst) { d->instanceChanged(inst);});
103 }
104 
106 {
107  delete d;
108 }
109 
110 QHash<int, QByteArray> AgentInstanceModel::roleNames() const
111 {
113  roles.insert(StatusRole, "status");
114  roles.insert(StatusMessageRole, "statusMessage");
115  roles.insert(ProgressRole, "progress");
116  roles.insert(OnlineRole, "online");
117  return roles;
118 }
119 
120 int AgentInstanceModel::columnCount(const QModelIndex &index) const
121 {
122  return index.isValid() ? 0 : 1;
123 }
124 
125 int AgentInstanceModel::rowCount(const QModelIndex &index) const
126 {
127  return index.isValid() ? 0 : d->mInstances.count();
128 }
129 
130 QVariant AgentInstanceModel::data(const QModelIndex &index, int role) const
131 {
132  if (!index.isValid()) {
133  return QVariant();
134  }
135 
136  if (index.row() < 0 || index.row() >= d->mInstances.count()) {
137  return QVariant();
138  }
139 
140  const AgentInstance &instance = d->mInstances[index.row()];
141 
142  switch (role) {
143  case Qt::DisplayRole:
144  return instance.name();
145  case Qt::DecorationRole:
146  return instance.type().icon();
147  case InstanceRole: {
148  QVariant var;
149  var.setValue(instance);
150  return var;
151  }
153  return instance.identifier();
154  case Qt::ToolTipRole:
155  return QStringLiteral("<qt><h4>%1</h4>%2</qt>").arg(instance.name(), instance.type().description());
156  case StatusRole:
157  return instance.status();
158  case StatusMessageRole:
159  return instance.statusMessage();
160  case ProgressRole:
161  return instance.progress();
162  case OnlineRole:
163  return instance.isOnline();
164  case TypeRole: {
165  QVariant var;
166  var.setValue(instance.type());
167  return var;
168  }
169  case TypeIdentifierRole:
170  return instance.type().identifier();
171  case DescriptionRole:
172  return instance.type().description();
173  case CapabilitiesRole:
174  return instance.type().capabilities();
175  case MimeTypesRole:
176  return instance.type().mimeTypes();
177  }
178  return QVariant();
179 }
180 
181 QVariant AgentInstanceModel::headerData(int section, Qt::Orientation orientation, int role) const
182 {
183  if (orientation == Qt::Vertical) {
184  return QVariant();
185  }
186 
187  if (role != Qt::DisplayRole) {
188  return QVariant();
189  }
190 
191  switch (section) {
192  case 0:
193  return i18nc("@title:column, name of a thing", "Name");
194  default:
195  return QVariant();
196  }
197 }
198 
199 QModelIndex AgentInstanceModel::index(int row, int column, const QModelIndex &) const
200 {
201  if (row < 0 || row >= d->mInstances.count()) {
202  return QModelIndex();
203  }
204 
205  if (column != 0) {
206  return QModelIndex();
207  }
208 
209  return createIndex(row, column);
210 }
211 
213 {
214  return QModelIndex();
215 }
216 
217 Qt::ItemFlags AgentInstanceModel::flags(const QModelIndex &index) const
218 {
219  if (!index.isValid() || index.row() < 0 || index.row() >= d->mInstances.count()) {
220  return QAbstractItemModel::flags(index);
221  }
222 
223  return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
224 }
225 
226 bool AgentInstanceModel::setData(const QModelIndex &index, const QVariant &value, int role)
227 {
228  if (!index.isValid()) {
229  return false;
230  }
231 
232  if (index.row() < 0 || index.row() >= d->mInstances.count()) {
233  return false;
234  }
235 
236  AgentInstance &instance = d->mInstances[index.row()];
237 
238  switch (role) {
239  case OnlineRole:
240  instance.setIsOnline(value.toBool());
241  Q_EMIT dataChanged(index, index);
242  return true;
243  default:
244  return false;
245  }
246 
247  return false;
248 }
249 
250 #include "moc_agentinstancemodel.cpp"
void instanceStatusChanged(const Akonadi::AgentInstance &instance)
This signal is emitted whenever the status of an agent instance has changed.
void instanceRemoved(const Akonadi::AgentInstance &instance)
This signal is emitted whenever an agent instance was removed.
QHash::iterator insert(const Key &key, const T &value)
AgentInstance::List instances() const
Returns the list of all available agent instances.
A textual presentation of the current status.
QVector< AgentInstance > List
Describes a list of agent instances.
QIcon icon() const
Returns the icon of the agent type.
The current online/offline status.
The current status (numerical) of the instance.
virtual QHash< int, QByteArray > roleNames() const const
AgentType type() const
Returns the agent type of this instance.
QString description() const
Returns the description of the agent type.
void instanceProgressChanged(const Akonadi::AgentInstance &instance)
This signal is emitted whenever the progress of an agent instance has changed.
void instanceNameChanged(const Akonadi::AgentInstance &instance)
This signal is emitted whenever the name of the agent instance has changed.
QString identifier() const
Returns the unique identifier of the agent instance.
QString identifier() const
Returns the unique identifier of the agent type.
void instanceAdded(const Akonadi::AgentInstance &instance)
This signal is emitted whenever a new agent instance was created.
bool isValid() const const
QStringList mimeTypes() const
Returns the list of supported mime types of the agent type.
~AgentInstanceModel() override
Destroys the agent instance model.
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector< int > &roles)
int progress() const
Returns the progress of the agent instance in percent, or -1 if no progress information are available...
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void setIsOnline(bool online)
Sets online status of the agent instance.
int row() const const
bool isOnline() const
Returns whether the agent instance is online currently.
QStringList capabilities() const
Returns the list of supported capabilities of the agent type.
Provides a data model for agent instances.
A description of the agent type.
QModelIndex createIndex(int row, int column, void *ptr) const const
A list of supported capabilities.
void instanceOnline(const Akonadi::AgentInstance &instance, bool online)
This signal is emitted whenever the online state of an agent changed.
void setValue(const T &value)
The current progress (numerical in percent) of an operation.
The identifier of the agent instance.
QString name() const
Returns the user visible name of the agent instance.
Helper integration between Akonadi and Qt.
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
bool toBool() const const
static AgentManager * self()
Returns the global instance of the agent manager.
Status status() const
Returns the status of the agent instance.
virtual Qt::ItemFlags flags(const QModelIndex &index) const const
A representation of an agent instance.
AgentInstanceModel(QObject *parent=nullptr)
Creates a new agent instance model.
QString statusMessage() const
Returns a textual presentation of the status of the agent instance.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
Q_EMITQ_EMIT
typedef ItemFlags
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Jun 5 2020 23:08:53 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.