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

akonadi

  • 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 
86 AgentInstanceModel::AgentInstanceModel( QObject *parent )
87  : QAbstractItemModel( parent ), 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  {
146  QVariant var;
147  var.setValue( instance );
148  return var;
149  }
150  case InstanceIdentifierRole:
151  return instance.identifier();
152  case Qt::ToolTipRole:
153  return QString::fromLatin1( "<qt><h4>%1</h4>%2</qt>" ).arg( instance.name(), instance.type().description() );
154  case StatusRole:
155  return instance.status();
156  case StatusMessageRole:
157  return instance.statusMessage();
158  case ProgressRole:
159  return instance.progress();
160  case OnlineRole:
161  return instance.isOnline();
162  case TypeRole:
163  {
164  QVariant var;
165  var.setValue( instance.type() );
166  return var;
167  }
168  case TypeIdentifierRole:
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 
180 QVariant 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  break;
194  default:
195  return QVariant();
196  break;
197  }
198 }
199 
200 QModelIndex AgentInstanceModel::index( int row, int column, const QModelIndex& ) const
201 {
202  if ( row < 0 || row >= d->mInstances.count() ) {
203  return QModelIndex();
204  }
205 
206  if ( column != 0 ) {
207  return QModelIndex();
208  }
209 
210  return createIndex( row, column);
211 }
212 
213 QModelIndex AgentInstanceModel::parent( const QModelIndex& ) const
214 {
215  return QModelIndex();
216 }
217 
218 Qt::ItemFlags AgentInstanceModel::flags( const QModelIndex & index ) const
219 {
220  if ( !index.isValid() || index.row() < 0 || index.row() >= d->mInstances.count() ) {
221  return QAbstractItemModel::flags( index );
222  }
223 
224  return QAbstractItemModel::flags( index ) | Qt::ItemIsEditable;
225 }
226 
227 bool AgentInstanceModel::setData( const QModelIndex & index, const QVariant & value, int role )
228 {
229  if ( !index.isValid() ) {
230  return false;
231  }
232 
233  if ( index.row() < 0 || index.row() >= d->mInstances.count() ) {
234  return false;
235  }
236 
237  AgentInstance &instance = d->mInstances[ index.row() ];
238 
239  switch ( role ) {
240  case OnlineRole:
241  instance.setIsOnline( value.toBool() );
242  emit dataChanged( index, index );
243  return true;
244  default:
245  return false;
246  }
247 
248  return false;
249 }
250 
251 #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
Akonadi::AgentInstance::List
QList< AgentInstance > List
Describes a list of agent instances.
Definition: agentinstance.h:71
Akonadi::AgentManager::instances
AgentInstance::List instances() const
Returns the list of all available agent instances.
Definition: agentmanager.cpp:398
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
Akonadi::AgentInstance::type
AgentType type() const
Returns the agent type of this instance.
Definition: agentinstance.cpp:51
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:56
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
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
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:91
Akonadi::AgentInstance::setIsOnline
void setIsOnline(bool online)
Sets online status of the agent instance.
Definition: agentinstance.cpp:101
Akonadi::AgentInstance::isOnline
bool isOnline() const
Returns whether the agent instance is online currently.
Definition: agentinstance.cpp:96
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
Akonadi::AgentInstanceModel::DescriptionRole
A description of the agent type.
Definition: agentinstancemodel.h:61
Akonadi::AgentInstanceModel::CapabilitiesRole
A list of supported capabilities.
Definition: agentinstancemodel.h:63
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:66
Akonadi::AgentManager::self
static AgentManager * self()
Returns the global instance of the agent manager.
Definition: agentmanager.cpp:379
Akonadi::AgentInstance::status
Status status() const
Returns the status of the agent instance.
Definition: agentinstance.cpp:71
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:86
Akonadi::AgentInstanceModel::AgentInstanceModel
AgentInstanceModel(QObject *parent=0)
Creates a new agent instance model.
Definition: agentinstancemodel.cpp:86
Akonadi::AgentInstanceModel::MimeTypesRole
A list of supported mimetypes.
Definition: agentinstancemodel.h:62
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Tue Nov 26 2013 09:03:15 by doxygen 1.8.5 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-4.11.3 API Reference

Skip menu "kdepimlibs-4.11.3 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
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