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 for ( int i = 0; i < mInstances.count(); ++i ) { 00072 if ( mInstances[ i ] == instance ) { 00073 mInstances[ i ] = instance; 00074 00075 const QModelIndex idx = mParent->index( i, 0 ); 00076 emit mParent->dataChanged( idx, idx ); 00077 00078 return; 00079 } 00080 } 00081 } 00082 00083 00084 AgentInstanceModel::AgentInstanceModel( QObject *parent ) 00085 : QAbstractItemModel( parent ), d( new Private( this ) ) 00086 { 00087 d->mInstances = AgentManager::self()->instances(); 00088 00089 QHash<int, QByteArray> roles = roleNames(); 00090 roles.insert( StatusRole, "status" ); 00091 roles.insert( StatusMessageRole, "statusMessage" ); 00092 roles.insert( ProgressRole, "progress" ); 00093 roles.insert( OnlineRole, "online" ); 00094 setRoleNames( roles ); 00095 00096 connect( AgentManager::self(), SIGNAL( instanceAdded( const Akonadi::AgentInstance& ) ), 00097 this, SLOT( instanceAdded( const Akonadi::AgentInstance& ) ) ); 00098 connect( AgentManager::self(), SIGNAL( instanceRemoved( const Akonadi::AgentInstance& ) ), 00099 this, SLOT( instanceRemoved( const Akonadi::AgentInstance& ) ) ); 00100 connect( AgentManager::self(), SIGNAL( instanceStatusChanged( const Akonadi::AgentInstance& ) ), 00101 this, SLOT( instanceChanged( const Akonadi::AgentInstance& ) ) ); 00102 connect( AgentManager::self(), SIGNAL( instanceProgressChanged( const Akonadi::AgentInstance& ) ), 00103 this, SLOT( instanceChanged( const Akonadi::AgentInstance& ) ) ); 00104 connect( AgentManager::self(), SIGNAL( instanceNameChanged( const Akonadi::AgentInstance& ) ), 00105 this, SLOT( instanceChanged( const Akonadi::AgentInstance& ) ) ); 00106 connect( AgentManager::self(), SIGNAL( instanceOnline( const Akonadi::AgentInstance&, bool ) ), 00107 this, SLOT( instanceChanged( const Akonadi::AgentInstance& ) ) ); 00108 } 00109 00110 AgentInstanceModel::~AgentInstanceModel() 00111 { 00112 delete d; 00113 } 00114 00115 int AgentInstanceModel::columnCount( const QModelIndex& ) const 00116 { 00117 return 1; 00118 } 00119 00120 int AgentInstanceModel::rowCount( const QModelIndex& ) const 00121 { 00122 return d->mInstances.count(); 00123 } 00124 00125 QVariant AgentInstanceModel::data( const QModelIndex &index, int role ) const 00126 { 00127 if ( !index.isValid() ) 00128 return QVariant(); 00129 00130 if ( index.row() < 0 || index.row() >= d->mInstances.count() ) 00131 return QVariant(); 00132 00133 const AgentInstance &instance = d->mInstances[ index.row() ]; 00134 00135 switch ( role ) { 00136 case Qt::DisplayRole: 00137 return instance.name(); 00138 case Qt::DecorationRole: 00139 return instance.type().icon(); 00140 case InstanceRole: 00141 { 00142 QVariant var; 00143 var.setValue( instance ); 00144 return var; 00145 } 00146 case InstanceIdentifierRole: 00147 return instance.identifier(); 00148 case Qt::ToolTipRole: 00149 return QString::fromLatin1( "<qt><h4>%1</h4>%2</qt>" ).arg( instance.name(), instance.type().description() ); 00150 case StatusRole: 00151 return instance.status(); 00152 case StatusMessageRole: 00153 return instance.statusMessage(); 00154 case ProgressRole: 00155 return instance.progress(); 00156 case OnlineRole: 00157 return instance.isOnline(); 00158 case TypeRole: 00159 { 00160 QVariant var; 00161 var.setValue( instance.type() ); 00162 return var; 00163 } 00164 case TypeIdentifierRole: 00165 return instance.type().identifier(); 00166 case DescriptionRole: 00167 return instance.type().description(); 00168 case CapabilitiesRole: 00169 return instance.type().capabilities(); 00170 case MimeTypesRole: 00171 return instance.type().mimeTypes(); 00172 } 00173 return QVariant(); 00174 } 00175 00176 QVariant AgentInstanceModel::headerData( int section, Qt::Orientation orientation, int role ) const 00177 { 00178 if ( orientation == Qt::Vertical ) 00179 return QVariant(); 00180 00181 if ( role != Qt::DisplayRole ) 00182 return QVariant(); 00183 00184 switch ( section ) { 00185 case 0: 00186 return i18nc( "@title:column, name of a thing", "Name" ); 00187 break; 00188 default: 00189 return QVariant(); 00190 break; 00191 } 00192 } 00193 00194 QModelIndex AgentInstanceModel::index( int row, int column, const QModelIndex& ) const 00195 { 00196 if ( row < 0 || row >= d->mInstances.count() ) 00197 return QModelIndex(); 00198 00199 if ( column != 0 ) 00200 return QModelIndex(); 00201 00202 return createIndex( row, column, 0 ); 00203 } 00204 00205 QModelIndex AgentInstanceModel::parent( const QModelIndex& ) const 00206 { 00207 return QModelIndex(); 00208 } 00209 00210 Qt::ItemFlags AgentInstanceModel::flags( const QModelIndex & index ) const 00211 { 00212 if ( !index.isValid() || index.row() < 0 || index.row() >= d->mInstances.count() ) 00213 return QAbstractItemModel::flags( index ); 00214 00215 return QAbstractItemModel::flags( index ) | Qt::ItemIsEditable; 00216 } 00217 00218 bool AgentInstanceModel::setData( const QModelIndex & index, const QVariant & value, int role ) 00219 { 00220 if ( !index.isValid() ) 00221 return false; 00222 00223 if ( index.row() < 0 || index.row() >= d->mInstances.count() ) 00224 return false; 00225 00226 AgentInstance &instance = d->mInstances[ index.row() ]; 00227 00228 switch ( role ) { 00229 case OnlineRole: 00230 instance.setIsOnline( value.toBool() ); 00231 emit dataChanged( index, index ); 00232 return true; 00233 default: 00234 return false; 00235 } 00236 00237 return false; 00238 } 00239 00240 #include "agentinstancemodel.moc"