• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.9.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 <QtGui/QIcon>
27 
28 #include <klocale.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  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  mInstances[ i ] = instance;
75 
76  const QModelIndex idx = mParent->index( i, 0 );
77  emit mParent->dataChanged( idx, idx );
78 
79  return;
80  }
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  if ( index.row() < 0 || index.row() >= d->mInstances.count() )
132  return QVariant();
133 
134  const AgentInstance &instance = d->mInstances[ index.row() ];
135 
136  switch ( role ) {
137  case Qt::DisplayRole:
138  return instance.name();
139  case Qt::DecorationRole:
140  return instance.type().icon();
141  case InstanceRole:
142  {
143  QVariant var;
144  var.setValue( instance );
145  return var;
146  }
147  case InstanceIdentifierRole:
148  return instance.identifier();
149  case Qt::ToolTipRole:
150  return QString::fromLatin1( "<qt><h4>%1</h4>%2</qt>" ).arg( instance.name(), instance.type().description() );
151  case StatusRole:
152  return instance.status();
153  case StatusMessageRole:
154  return instance.statusMessage();
155  case ProgressRole:
156  return instance.progress();
157  case OnlineRole:
158  return instance.isOnline();
159  case TypeRole:
160  {
161  QVariant var;
162  var.setValue( instance.type() );
163  return var;
164  }
165  case TypeIdentifierRole:
166  return instance.type().identifier();
167  case DescriptionRole:
168  return instance.type().description();
169  case CapabilitiesRole:
170  return instance.type().capabilities();
171  case MimeTypesRole:
172  return instance.type().mimeTypes();
173  }
174  return QVariant();
175 }
176 
177 QVariant AgentInstanceModel::headerData( int section, Qt::Orientation orientation, int role ) const
178 {
179  if ( orientation == Qt::Vertical )
180  return QVariant();
181 
182  if ( role != Qt::DisplayRole )
183  return QVariant();
184 
185  switch ( section ) {
186  case 0:
187  return i18nc( "@title:column, name of a thing", "Name" );
188  break;
189  default:
190  return QVariant();
191  break;
192  }
193 }
194 
195 QModelIndex AgentInstanceModel::index( int row, int column, const QModelIndex& ) const
196 {
197  if ( row < 0 || row >= d->mInstances.count() )
198  return QModelIndex();
199 
200  if ( column != 0 )
201  return QModelIndex();
202 
203  return createIndex( row, column, 0 );
204 }
205 
206 QModelIndex AgentInstanceModel::parent( const QModelIndex& ) const
207 {
208  return QModelIndex();
209 }
210 
211 Qt::ItemFlags AgentInstanceModel::flags( const QModelIndex & index ) const
212 {
213  if ( !index.isValid() || index.row() < 0 || index.row() >= d->mInstances.count() )
214  return QAbstractItemModel::flags( index );
215 
216  return QAbstractItemModel::flags( index ) | Qt::ItemIsEditable;
217 }
218 
219 bool AgentInstanceModel::setData( const QModelIndex & index, const QVariant & value, int role )
220 {
221  if ( !index.isValid() )
222  return false;
223 
224  if ( index.row() < 0 || index.row() >= d->mInstances.count() )
225  return false;
226 
227  AgentInstance &instance = d->mInstances[ index.row() ];
228 
229  switch ( role ) {
230  case OnlineRole:
231  instance.setIsOnline( value.toBool() );
232  emit dataChanged( index, index );
233  return true;
234  default:
235  return false;
236  }
237 
238  return false;
239 }
240 
241 #include "agentinstancemodel.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Wed Nov 28 2012 21:51:21 by doxygen 1.8.1.2 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.9.3 API Reference

Skip menu "kdepimlibs-4.9.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • 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