• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepimlibs API Reference
  • KDE Home
  • Contact Us
 

akonadi

  • sources
  • kde-4.14
  • kdepimlibs
  • akonadi
agentinstancemodel.cpp
1 /*
2  Copyright (c) 2006 Tobias Koenig <tokoe@kde.org>
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 <QtCore/QStringList>
26 #include <QIcon>
27 
28 #include <klocalizedstring.h>
29 
30 using namespace Akonadi;
31 
35 class AgentInstanceModel::Private
36 {
37 public:
38  Private(AgentInstanceModel *parent)
39  : mParent(parent)
40  {
41  }
42 
43  AgentInstanceModel *mParent;
44  AgentInstance::List mInstances;
45 
46  void instanceAdded(const AgentInstance &);
47  void instanceRemoved(const AgentInstance &);
48  void instanceChanged(const AgentInstance &);
49 };
50 
51 void AgentInstanceModel::Private::instanceAdded(const AgentInstance &instance)
52 {
53  mParent->beginInsertRows(QModelIndex(), mInstances.count(), mInstances.count());
54  mInstances.append(instance);
55  mParent->endInsertRows();
56 }
57 
58 void AgentInstanceModel::Private::instanceRemoved(const AgentInstance &instance)
59 {
60  const int index = mInstances.indexOf(instance);
61  if (index == -1) {
62  return;
63  }
64 
65  mParent->beginRemoveRows(QModelIndex(), index, index);
66  mInstances.removeAll(instance);
67  mParent->endRemoveRows();
68 }
69 
70 void AgentInstanceModel::Private::instanceChanged(const AgentInstance &instance)
71 {
72  const int numberOfInstance(mInstances.count());
73  for (int i = 0; i < numberOfInstance; ++i) {
74  if (mInstances[i] == instance) {
75  mInstances[i] = instance;
76 
77  const QModelIndex idx = mParent->index(i, 0);
78  emit mParent->dataChanged(idx, idx);
79 
80  return;
81  }
82  }
83 }
84 
85 AgentInstanceModel::AgentInstanceModel(QObject *parent)
86  : QAbstractItemModel(parent)
87  , d(new Private(this))
88 {
89  d->mInstances = AgentManager::self()->instances();
90 
91  QHash<int, QByteArray> roles = roleNames();
92  roles.insert(StatusRole, "status");
93  roles.insert(StatusMessageRole, "statusMessage");
94  roles.insert(ProgressRole, "progress");
95  roles.insert(OnlineRole, "online");
96  setRoleNames(roles);
97 
98  connect(AgentManager::self(), SIGNAL(instanceAdded(Akonadi::AgentInstance)),
99  this, SLOT(instanceAdded(Akonadi::AgentInstance)));
100  connect(AgentManager::self(), SIGNAL(instanceRemoved(Akonadi::AgentInstance)),
101  this, SLOT(instanceRemoved(Akonadi::AgentInstance)));
102  connect(AgentManager::self(), SIGNAL(instanceStatusChanged(Akonadi::AgentInstance)),
103  this, SLOT(instanceChanged(Akonadi::AgentInstance)));
104  connect(AgentManager::self(), SIGNAL(instanceProgressChanged(Akonadi::AgentInstance)),
105  this, SLOT(instanceChanged(Akonadi::AgentInstance)));
106  connect(AgentManager::self(), SIGNAL(instanceNameChanged(Akonadi::AgentInstance)),
107  this, SLOT(instanceChanged(Akonadi::AgentInstance)));
108  connect(AgentManager::self(), SIGNAL(instanceOnline(Akonadi::AgentInstance,bool)),
109  this, SLOT(instanceChanged(Akonadi::AgentInstance)));
110 }
111 
112 AgentInstanceModel::~AgentInstanceModel()
113 {
114  delete d;
115 }
116 
117 int AgentInstanceModel::columnCount(const QModelIndex &) const
118 {
119  return 1;
120 }
121 
122 int AgentInstanceModel::rowCount(const QModelIndex &) const
123 {
124  return d->mInstances.count();
125 }
126 
127 QVariant AgentInstanceModel::data(const QModelIndex &index, int role) const
128 {
129  if (!index.isValid()) {
130  return QVariant();
131  }
132 
133  if (index.row() < 0 || index.row() >= d->mInstances.count()) {
134  return QVariant();
135  }
136 
137  const AgentInstance &instance = d->mInstances[index.row()];
138 
139  switch (role) {
140  case Qt::DisplayRole:
141  return instance.name();
142  case Qt::DecorationRole:
143  return instance.type().icon();
144  case InstanceRole: {
145  QVariant var;
146  var.setValue(instance);
147  return var;
148  }
149  case InstanceIdentifierRole:
150  return instance.identifier();
151  case Qt::ToolTipRole:
152  return QString::fromLatin1("<qt><h4>%1</h4>%2</qt>").arg(instance.name(), instance.type().description());
153  case StatusRole:
154  return instance.status();
155  case StatusMessageRole:
156  return instance.statusMessage();
157  case ProgressRole:
158  return instance.progress();
159  case OnlineRole:
160  return instance.isOnline();
161  case TypeRole: {
162  QVariant var;
163  var.setValue(instance.type());
164  return var;
165  }
166  case TypeIdentifierRole:
167  return instance.type().identifier();
168  case DescriptionRole:
169  return instance.type().description();
170  case CapabilitiesRole:
171  return instance.type().capabilities();
172  case MimeTypesRole:
173  return instance.type().mimeTypes();
174  }
175  return QVariant();
176 }
177 
178 QVariant AgentInstanceModel::headerData(int section, Qt::Orientation orientation, int role) const
179 {
180  if (orientation == Qt::Vertical) {
181  return QVariant();
182  }
183 
184  if (role != Qt::DisplayRole) {
185  return QVariant();
186  }
187 
188  switch (section) {
189  case 0:
190  return i18nc("@title:column, name of a thing", "Name");
191  break;
192  default:
193  return QVariant();
194  break;
195  }
196 }
197 
198 QModelIndex AgentInstanceModel::index(int row, int column, const QModelIndex &) 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 
211 QModelIndex AgentInstanceModel::parent(const QModelIndex &) const
212 {
213  return QModelIndex();
214 }
215 
216 Qt::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 
222  return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
223 }
224 
225 bool 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  emit dataChanged(index, index);
241  return true;
242  default:
243  return false;
244  }
245 
246  return false;
247 }
248 
249 #include "moc_agentinstancemodel.cpp"
Akonadi::AgentInstanceModel::InstanceRole
The agent instance itself.
Definition: agentinstancemodel.h:64
Akonadi::AgentInstanceModel::~AgentInstanceModel
virtual ~AgentInstanceModel()
Destroys the agent instance model.
Definition: agentinstancemodel.cpp:112
QModelIndex
QHash::insert
iterator insert(const Key &key, const T &value)
Akonadi::AgentManager::instances
AgentInstance::List instances() const
Returns the list of all available agent instances.
Definition: agentmanager.cpp:396
QAbstractItemModel::setRoleNames
void setRoleNames(const QHash< int, QByteArray > &roleNames)
Akonadi::AgentInstanceModel::StatusMessageRole
A textual presentation of the current status.
Definition: agentinstancemodel.h:67
Akonadi::AgentType::icon
QIcon icon() const
Returns the icon of the agent type.
Definition: agenttype.cpp:66
Akonadi::AgentInstanceModel::OnlineRole
The current online/offline status.
Definition: agentinstancemodel.h:69
Akonadi::AgentInstanceModel::StatusRole
The current status (numerical) of the instance.
Definition: agentinstancemodel.h:66
QAbstractItemModel::roleNames
const QHash< int, QByteArray > & roleNames() const
Akonadi::AgentInstance::type
AgentType type() const
Returns the agent type of this instance.
Definition: agentinstance.cpp:50
Akonadi::AgentType::description
QString description() const
Returns the description of the agent type.
Definition: agenttype.cpp:56
Akonadi::AgentInstance::identifier
QString identifier() const
Returns the unique identifier of the agent instance.
Definition: agentinstance.cpp:55
Akonadi::AgentType::identifier
QString identifier() const
Returns the unique identifier of the agent type.
Definition: agenttype.cpp:46
Akonadi::AgentInstanceModel::TypeRole
The agent type itself.
Definition: agentinstancemodel.h:59
QModelIndex::isValid
bool isValid() const
Akonadi::AgentInstanceModel::TypeIdentifierRole
The identifier of the agent type.
Definition: agentinstancemodel.h:60
Akonadi::AgentType::mimeTypes
QStringList mimeTypes() const
Returns the list of supported mime types of the agent type.
Definition: agenttype.cpp:71
QAbstractItemModel::dataChanged
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
QHash
Akonadi::AgentInstance::progress
int progress() const
Returns the progress of the agent instance in percent, or -1 if no progress information are available...
Definition: agentinstance.cpp:90
QObject
Akonadi::AgentInstance::setIsOnline
void setIsOnline(bool online)
Sets online status of the agent instance.
Definition: agentinstance.cpp:100
QModelIndex::row
int row() const
Akonadi::AgentInstance::isOnline
bool isOnline() const
Returns whether the agent instance is online currently.
Definition: agentinstance.cpp:95
Akonadi::AgentType::capabilities
QStringList capabilities() const
Returns the list of supported capabilities of the agent type.
Definition: agenttype.cpp:76
Akonadi::AgentInstanceModel
Provides a data model for agent instances.
Definition: agentinstancemodel.h:50
QList
Akonadi::AgentInstanceModel::DescriptionRole
A description of the agent type.
Definition: agentinstancemodel.h:61
QAbstractItemModel::createIndex
QModelIndex createIndex(int row, int column, void *ptr) const
Akonadi::AgentInstanceModel::CapabilitiesRole
A list of supported capabilities.
Definition: agentinstancemodel.h:63
QVariant::setValue
void setValue(const T &value)
Akonadi::AgentInstanceModel::ProgressRole
The current progress (numerical in percent) of an operation.
Definition: agentinstancemodel.h:68
Akonadi::AgentInstanceModel::InstanceIdentifierRole
The identifier of the agent instance.
Definition: agentinstancemodel.h:65
Akonadi::AgentInstance::name
QString name() const
Returns the user visible name of the agent instance.
Definition: agentinstance.cpp:65
QVariant::toBool
bool toBool() const
QAbstractItemModel
Akonadi::AgentManager::self
static AgentManager * self()
Returns the global instance of the agent manager.
Definition: agentmanager.cpp:377
Akonadi::AgentInstance::status
Status status() const
Returns the status of the agent instance.
Definition: agentinstance.cpp:70
QString::fromLatin1
QString fromLatin1(const char *str, int size)
QAbstractItemModel::flags
virtual Qt::ItemFlags flags(const QModelIndex &index) const
Akonadi::AgentInstance
A representation of an agent instance.
Definition: agentinstance.h:62
Akonadi::AgentInstance::statusMessage
QString statusMessage() const
Returns a textual presentation of the status of the agent instance.
Definition: agentinstance.cpp:85
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject::parent
QObject * parent() const
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
Akonadi::AgentInstanceModel::AgentInstanceModel
AgentInstanceModel(QObject *parent=0)
Creates a new agent instance model.
Definition: agentinstancemodel.cpp:85
Akonadi::AgentInstanceModel::MimeTypesRole
A list of supported mimetypes.
Definition: agentinstancemodel.h:62
QVariant
Qt::ItemFlags
typedef ItemFlags
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:38:02 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

Skip menu "akonadi"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Modules
  • Related Pages

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal