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