akonadi
agentinstancemodel.cpp
00001 /* 00002 Copyright (c) 2006 Tobias Koenig <tokoe@kde.org> 00003 00004 This library is free software; you can redistribute it and/or modify it 00005 under the terms of the GNU Library General Public License as published by 00006 the Free Software Foundation; either version 2 of the License, or (at your 00007 option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, but WITHOUT 00010 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00011 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00012 License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to the 00016 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00017 02110-1301, USA. 00018 */ 00019 00020 #include "agentinstancemodel.h" 00021 00022 #include "agentinstance.h" 00023 #include "agentmanager.h" 00024 00025 #include <QtCore/QStringList> 00026 #include <QtGui/QIcon> 00027 00028 #include <klocale.h> 00029 00030 using namespace Akonadi; 00031 00035 class AgentInstanceModel::Private 00036 { 00037 public: 00038 Private( AgentInstanceModel *parent ) 00039 : mParent( parent ) 00040 { 00041 } 00042 00043 AgentInstanceModel *mParent; 00044 AgentInstance::List mInstances; 00045 00046 void instanceAdded( const AgentInstance& ); 00047 void instanceRemoved( const AgentInstance& ); 00048 void instanceChanged( const AgentInstance& ); 00049 }; 00050 00051 void AgentInstanceModel::Private::instanceAdded( const AgentInstance &instance ) 00052 { 00053 mParent->beginInsertRows( QModelIndex(), mInstances.count(), mInstances.count() ); 00054 mInstances.append( instance ); 00055 mParent->endInsertRows(); 00056 } 00057 00058 void AgentInstanceModel::Private::instanceRemoved( const AgentInstance &instance ) 00059 { 00060 const int index = mInstances.indexOf( instance ); 00061 if ( index == -1 ) 00062 return; 00063 00064 mParent->beginRemoveRows( QModelIndex(), index, index ); 00065 mInstances.removeAll( instance ); 00066 mParent->endRemoveRows(); 00067 } 00068 00069 void AgentInstanceModel::Private::instanceChanged( const AgentInstance &instance ) 00070 { 00071 const int numberOfInstance( mInstances.count() ); 00072 for ( int i = 0; i < numberOfInstance; ++i ) { 00073 if ( mInstances[ i ] == instance ) { 00074 mInstances[ i ] = instance; 00075 00076 const QModelIndex idx = mParent->index( i, 0 ); 00077 emit mParent->dataChanged( idx, idx ); 00078 00079 return; 00080 } 00081 } 00082 } 00083 00084 00085 AgentInstanceModel::AgentInstanceModel( QObject *parent ) 00086 : QAbstractItemModel( parent ), d( new Private( this ) ) 00087 { 00088 d->mInstances = AgentManager::self()->instances(); 00089 00090 QHash<int, QByteArray> roles = roleNames(); 00091 roles.insert( StatusRole, "status" ); 00092 roles.insert( StatusMessageRole, "statusMessage" ); 00093 roles.insert( ProgressRole, "progress" ); 00094 roles.insert( OnlineRole, "online" ); 00095 setRoleNames( roles ); 00096 00097 connect( AgentManager::self(), SIGNAL(instanceAdded(Akonadi::AgentInstance)), 00098 this, SLOT(instanceAdded(Akonadi::AgentInstance)) ); 00099 connect( AgentManager::self(), SIGNAL(instanceRemoved(Akonadi::AgentInstance)), 00100 this, SLOT(instanceRemoved(Akonadi::AgentInstance)) ); 00101 connect( AgentManager::self(), SIGNAL(instanceStatusChanged(Akonadi::AgentInstance)), 00102 this, SLOT(instanceChanged(Akonadi::AgentInstance)) ); 00103 connect( AgentManager::self(), SIGNAL(instanceProgressChanged(Akonadi::AgentInstance)), 00104 this, SLOT(instanceChanged(Akonadi::AgentInstance)) ); 00105 connect( AgentManager::self(), SIGNAL(instanceNameChanged(Akonadi::AgentInstance)), 00106 this, SLOT(instanceChanged(Akonadi::AgentInstance)) ); 00107 connect( AgentManager::self(), SIGNAL(instanceOnline(Akonadi::AgentInstance,bool)), 00108 this, SLOT(instanceChanged(Akonadi::AgentInstance)) ); 00109 } 00110 00111 AgentInstanceModel::~AgentInstanceModel() 00112 { 00113 delete d; 00114 } 00115 00116 int AgentInstanceModel::columnCount( const QModelIndex& ) const 00117 { 00118 return 1; 00119 } 00120 00121 int AgentInstanceModel::rowCount( const QModelIndex& ) const 00122 { 00123 return d->mInstances.count(); 00124 } 00125 00126 QVariant AgentInstanceModel::data( const QModelIndex &index, int role ) const 00127 { 00128 if ( !index.isValid() ) 00129 return QVariant(); 00130 00131 if ( index.row() < 0 || index.row() >= d->mInstances.count() ) 00132 return QVariant(); 00133 00134 const AgentInstance &instance = d->mInstances[ index.row() ]; 00135 00136 switch ( role ) { 00137 case Qt::DisplayRole: 00138 return instance.name(); 00139 case Qt::DecorationRole: 00140 return instance.type().icon(); 00141 case InstanceRole: 00142 { 00143 QVariant var; 00144 var.setValue( instance ); 00145 return var; 00146 } 00147 case InstanceIdentifierRole: 00148 return instance.identifier(); 00149 case Qt::ToolTipRole: 00150 return QString::fromLatin1( "<qt><h4>%1</h4>%2</qt>" ).arg( instance.name(), instance.type().description() ); 00151 case StatusRole: 00152 return instance.status(); 00153 case StatusMessageRole: 00154 return instance.statusMessage(); 00155 case ProgressRole: 00156 return instance.progress(); 00157 case OnlineRole: 00158 return instance.isOnline(); 00159 case TypeRole: 00160 { 00161 QVariant var; 00162 var.setValue( instance.type() ); 00163 return var; 00164 } 00165 case TypeIdentifierRole: 00166 return instance.type().identifier(); 00167 case DescriptionRole: 00168 return instance.type().description(); 00169 case CapabilitiesRole: 00170 return instance.type().capabilities(); 00171 case MimeTypesRole: 00172 return instance.type().mimeTypes(); 00173 } 00174 return QVariant(); 00175 } 00176 00177 QVariant AgentInstanceModel::headerData( int section, Qt::Orientation orientation, int role ) const 00178 { 00179 if ( orientation == Qt::Vertical ) 00180 return QVariant(); 00181 00182 if ( role != Qt::DisplayRole ) 00183 return QVariant(); 00184 00185 switch ( section ) { 00186 case 0: 00187 return i18nc( "@title:column, name of a thing", "Name" ); 00188 break; 00189 default: 00190 return QVariant(); 00191 break; 00192 } 00193 } 00194 00195 QModelIndex AgentInstanceModel::index( int row, int column, const QModelIndex& ) const 00196 { 00197 if ( row < 0 || row >= d->mInstances.count() ) 00198 return QModelIndex(); 00199 00200 if ( column != 0 ) 00201 return QModelIndex(); 00202 00203 return createIndex( row, column, 0 ); 00204 } 00205 00206 QModelIndex AgentInstanceModel::parent( const QModelIndex& ) const 00207 { 00208 return QModelIndex(); 00209 } 00210 00211 Qt::ItemFlags AgentInstanceModel::flags( const QModelIndex & index ) const 00212 { 00213 if ( !index.isValid() || index.row() < 0 || index.row() >= d->mInstances.count() ) 00214 return QAbstractItemModel::flags( index ); 00215 00216 return QAbstractItemModel::flags( index ) | Qt::ItemIsEditable; 00217 } 00218 00219 bool AgentInstanceModel::setData( const QModelIndex & index, const QVariant & value, int role ) 00220 { 00221 if ( !index.isValid() ) 00222 return false; 00223 00224 if ( index.row() < 0 || index.row() >= d->mInstances.count() ) 00225 return false; 00226 00227 AgentInstance &instance = d->mInstances[ index.row() ]; 00228 00229 switch ( role ) { 00230 case OnlineRole: 00231 instance.setIsOnline( value.toBool() ); 00232 emit dataChanged( index, index ); 00233 return true; 00234 default: 00235 return false; 00236 } 00237 00238 return false; 00239 } 00240 00241 #include "agentinstancemodel.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Apr 30 2012 21:49:14 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Apr 30 2012 21:49:14 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.