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

akonadi

  • sources
  • kde-4.12
  • 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 ), d( new Private( this ) )
87 {
88  d->mInstances = AgentManager::self()->instances();
89 
90  QHash<int, QByteArray> roles = roleNames();
91  roles.insert( StatusRole, "status" );
92  roles.insert( StatusMessageRole, "statusMessage" );
93  roles.insert( ProgressRole, "progress" );
94  roles.insert( OnlineRole, "online" );
95  setRoleNames( roles );
96 
97  connect( AgentManager::self(), SIGNAL(instanceAdded(Akonadi::AgentInstance)),
98  this, SLOT(instanceAdded(Akonadi::AgentInstance)) );
99  connect( AgentManager::self(), SIGNAL(instanceRemoved(Akonadi::AgentInstance)),
100  this, SLOT(instanceRemoved(Akonadi::AgentInstance)) );
101  connect( AgentManager::self(), SIGNAL(instanceStatusChanged(Akonadi::AgentInstance)),
102  this, SLOT(instanceChanged(Akonadi::AgentInstance)) );
103  connect( AgentManager::self(), SIGNAL(instanceProgressChanged(Akonadi::AgentInstance)),
104  this, SLOT(instanceChanged(Akonadi::AgentInstance)) );
105  connect( AgentManager::self(), SIGNAL(instanceNameChanged(Akonadi::AgentInstance)),
106  this, SLOT(instanceChanged(Akonadi::AgentInstance)) );
107  connect( AgentManager::self(), SIGNAL(instanceOnline(Akonadi::AgentInstance,bool)),
108  this, SLOT(instanceChanged(Akonadi::AgentInstance)) );
109 }
110 
111 AgentInstanceModel::~AgentInstanceModel()
112 {
113  delete d;
114 }
115 
116 int AgentInstanceModel::columnCount( const QModelIndex& ) const
117 {
118  return 1;
119 }
120 
121 int AgentInstanceModel::rowCount( const QModelIndex& ) const
122 {
123  return d->mInstances.count();
124 }
125 
126 QVariant AgentInstanceModel::data( const QModelIndex &index, int role ) const
127 {
128  if ( !index.isValid() ) {
129  return QVariant();
130  }
131 
132  if ( index.row() < 0 || index.row() >= d->mInstances.count() ) {
133  return QVariant();
134  }
135 
136  const AgentInstance &instance = d->mInstances[ index.row() ];
137 
138  switch ( role ) {
139  case Qt::DisplayRole:
140  return instance.name();
141  case Qt::DecorationRole:
142  return instance.type().icon();
143  case InstanceRole:
144  {
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  {
163  QVariant var;
164  var.setValue( instance.type() );
165  return var;
166  }
167  case TypeIdentifierRole:
168  return instance.type().identifier();
169  case DescriptionRole:
170  return instance.type().description();
171  case CapabilitiesRole:
172  return instance.type().capabilities();
173  case MimeTypesRole:
174  return instance.type().mimeTypes();
175  }
176  return QVariant();
177 }
178 
179 QVariant AgentInstanceModel::headerData( int section, Qt::Orientation orientation, int role ) const
180 {
181  if ( orientation == Qt::Vertical ) {
182  return QVariant();
183  }
184 
185  if ( role != Qt::DisplayRole ) {
186  return QVariant();
187  }
188 
189  switch ( section ) {
190  case 0:
191  return i18nc( "@title:column, name of a thing", "Name" );
192  break;
193  default:
194  return QVariant();
195  break;
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 
212 QModelIndex AgentInstanceModel::parent( const QModelIndex& ) const
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  emit dataChanged( index, index );
242  return true;
243  default:
244  return false;
245  }
246 
247  return false;
248 }
249 
250 #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:111
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:399
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: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
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:90
Akonadi::AgentInstance::setIsOnline
void setIsOnline(bool online)
Sets online status of the agent instance.
Definition: agentinstance.cpp:100
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
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:65
Akonadi::AgentManager::self
static AgentManager * self()
Returns the global instance of the agent manager.
Definition: agentmanager.cpp:380
Akonadi::AgentInstance::status
Status status() const
Returns the status of the agent instance.
Definition: agentinstance.cpp:70
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
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
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:00:26 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
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

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