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