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