00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "agentmanager.h"
00021 #include "agentmanager_p.h"
00022
00023 #include "agenttype_p.h"
00024 #include "agentinstance_p.h"
00025
00026 #include "collection.h"
00027
00028 #include <QtGui/QWidget>
00029
00030
00031 using namespace Akonadi;
00032
00033
00034
00035 AgentInstance AgentManagerPrivate::createInstance( const AgentType &type )
00036 {
00037 const QString &identifier = mManager->createAgentInstance( type.identifier() );
00038 if ( identifier.isEmpty() )
00039 return AgentInstance();
00040
00041 return fillAgentInstanceLight( identifier );
00042 }
00043
00044 void AgentManagerPrivate::agentTypeAdded( const QString &identifier )
00045 {
00046
00047
00048 if ( mTypes.contains( identifier ) )
00049 return;
00050
00051 const AgentType type = fillAgentType( identifier );
00052 if ( type.isValid() ) {
00053 mTypes.insert( identifier, type );
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 readAgentTypes();
00067
00068 emit mParent->typeAdded( type );
00069 }
00070 }
00071
00072 void AgentManagerPrivate::agentTypeRemoved( const QString &identifier )
00073 {
00074 if ( !mTypes.contains( identifier ) )
00075 return;
00076
00077 const AgentType type = mTypes.take( identifier );
00078 emit mParent->typeRemoved( type );
00079 }
00080
00081 void AgentManagerPrivate::agentInstanceAdded( const QString &identifier )
00082 {
00083 const AgentInstance instance = fillAgentInstance( identifier );
00084 if ( instance.isValid() ) {
00085
00086
00087
00088
00089
00090
00091
00092
00093 bool newAgentInstance = !mInstances.contains( identifier );
00094 if ( newAgentInstance ) {
00095 mInstances.insert( identifier, instance );
00096 emit mParent->instanceAdded( instance );
00097 }
00098 else {
00099 mInstances.remove( identifier );
00100 mInstances.insert( identifier, instance );
00101 emit mParent->instanceStatusChanged( instance );
00102 }
00103 }
00104 }
00105
00106 void AgentManagerPrivate::agentInstanceRemoved( const QString &identifier )
00107 {
00108 if ( !mInstances.contains( identifier ) )
00109 return;
00110
00111 const AgentInstance instance = mInstances.take( identifier );
00112 emit mParent->instanceRemoved( instance );
00113 }
00114
00115 void AgentManagerPrivate::agentInstanceStatusChanged( const QString &identifier, int status, const QString &msg )
00116 {
00117 if ( !mInstances.contains( identifier ) )
00118 return;
00119
00120 AgentInstance &instance = mInstances[ identifier ];
00121 instance.d->mStatus = status;
00122 instance.d->mStatusMessage = msg;
00123
00124 emit mParent->instanceStatusChanged( instance );
00125 }
00126
00127 void AgentManagerPrivate::agentInstanceProgressChanged( const QString &identifier, uint progress, const QString &msg )
00128 {
00129 if ( !mInstances.contains( identifier ) )
00130 return;
00131
00132 AgentInstance &instance = mInstances[ identifier ];
00133 instance.d->mProgress = progress;
00134 if ( !msg.isEmpty() )
00135 instance.d->mStatusMessage = msg;
00136
00137 emit mParent->instanceProgressChanged( instance );
00138 }
00139
00140 void AgentManagerPrivate::agentInstanceWarning( const QString &identifier, const QString &msg )
00141 {
00142 if ( !mInstances.contains( identifier ) )
00143 return;
00144
00145 AgentInstance &instance = mInstances[ identifier ];
00146 emit mParent->instanceWarning( instance, msg );
00147 }
00148
00149 void AgentManagerPrivate::agentInstanceError( const QString &identifier, const QString &msg )
00150 {
00151 if ( !mInstances.contains( identifier ) )
00152 return;
00153
00154 AgentInstance &instance = mInstances[ identifier ];
00155 emit mParent->instanceError( instance, msg );
00156 }
00157
00158 void AgentManagerPrivate::agentInstanceOnlineChanged( const QString &identifier, bool state )
00159 {
00160 if ( !mInstances.contains( identifier ) )
00161 return;
00162
00163 AgentInstance &instance = mInstances[ identifier ];
00164 instance.d->mIsOnline = state;
00165 emit mParent->instanceOnline( instance, state );
00166 }
00167
00168 void AgentManagerPrivate::agentInstanceNameChanged( const QString &identifier, const QString &name )
00169 {
00170 if ( !mInstances.contains( identifier ) )
00171 return;
00172
00173 AgentInstance &instance = mInstances[ identifier ];
00174 instance.d->mName = name;
00175
00176 emit mParent->instanceNameChanged( instance );
00177 }
00178
00179 void AgentManagerPrivate::readAgentTypes()
00180 {
00181 QDBusReply<QStringList> types = mManager->agentTypes();
00182 if ( types.isValid() ) {
00183 foreach ( const QString &type, types.value() ) {
00184 if ( !mTypes.contains( type ) )
00185 agentTypeAdded( type );
00186 }
00187 }
00188 }
00189
00190 AgentType AgentManagerPrivate::fillAgentType( const QString &identifier ) const
00191 {
00192 AgentType type;
00193 type.d->mIdentifier = identifier;
00194 type.d->mName = mManager->agentName( identifier );
00195 type.d->mDescription = mManager->agentComment( identifier );
00196 type.d->mIconName = mManager->agentIcon( identifier );
00197 type.d->mMimeTypes = mManager->agentMimeTypes( identifier );
00198 type.d->mCapabilities = mManager->agentCapabilities( identifier );
00199
00200 return type;
00201 }
00202
00203 void AgentManagerPrivate::setName( const AgentInstance &instance, const QString &name )
00204 {
00205 mManager->setAgentInstanceName( instance.identifier(), name );
00206 }
00207
00208 void AgentManagerPrivate::setOnline( const AgentInstance &instance, bool state )
00209 {
00210 mManager->setAgentInstanceOnline( instance.identifier(), state );
00211 }
00212
00213 void AgentManagerPrivate::configure( const AgentInstance &instance, QWidget *parent )
00214 {
00215 qlonglong winId = 0;
00216 if ( parent )
00217 winId = (qlonglong)( parent->window()->winId() );
00218
00219 mManager->agentInstanceConfigure( instance.identifier(), winId );
00220 }
00221
00222 void AgentManagerPrivate::synchronize( const AgentInstance &instance )
00223 {
00224 mManager->agentInstanceSynchronize( instance.identifier() );
00225 }
00226
00227 void AgentManagerPrivate::synchronizeCollectionTree( const AgentInstance &instance )
00228 {
00229 mManager->agentInstanceSynchronizeCollectionTree( instance.identifier() );
00230 }
00231
00232 AgentInstance AgentManagerPrivate::fillAgentInstance( const QString &identifier ) const
00233 {
00234 AgentInstance instance;
00235
00236 const QString agentTypeIdentifier = mManager->agentInstanceType( identifier );
00237 if ( !mTypes.contains( agentTypeIdentifier ) )
00238 return instance;
00239
00240 instance.d->mType = mTypes.value( agentTypeIdentifier );
00241 instance.d->mIdentifier = identifier;
00242 instance.d->mName = mManager->agentInstanceName( identifier );
00243 instance.d->mStatus = mManager->agentInstanceStatus( identifier );
00244 instance.d->mStatusMessage = mManager->agentInstanceStatusMessage( identifier );
00245 instance.d->mProgress = mManager->agentInstanceProgress( identifier );
00246 instance.d->mIsOnline = mManager->agentInstanceOnline( identifier );
00247
00248 return instance;
00249 }
00250
00251 AgentInstance AgentManagerPrivate::fillAgentInstanceLight( const QString &identifier ) const
00252 {
00253 AgentInstance instance;
00254
00255 const QString agentTypeIdentifier = mManager->agentInstanceType( identifier );
00256 Q_ASSERT_X( mTypes.contains( agentTypeIdentifier ), "fillAgentInstanceLight", "Requests non-existing agent type" );
00257
00258 instance.d->mType = mTypes.value( agentTypeIdentifier );
00259 instance.d->mIdentifier = identifier;
00260
00261 return instance;
00262 }
00263
00264 AgentManager* AgentManagerPrivate::mSelf = 0;
00265
00266 AgentManager::AgentManager()
00267 : QObject( 0 ), d( new AgentManagerPrivate( this ) )
00268 {
00269 d->mManager = new org::freedesktop::Akonadi::AgentManager( QLatin1String( "org.freedesktop.Akonadi.Control" ),
00270 QLatin1String( "/AgentManager" ),
00271 QDBusConnection::sessionBus(), this );
00272
00273 connect( d->mManager, SIGNAL( agentTypeAdded( const QString& ) ),
00274 this, SLOT( agentTypeAdded( const QString& ) ) );
00275 connect( d->mManager, SIGNAL( agentTypeRemoved( const QString& ) ),
00276 this, SLOT( agentTypeRemoved( const QString& ) ) );
00277 connect( d->mManager, SIGNAL( agentInstanceAdded( const QString& ) ),
00278 this, SLOT( agentInstanceAdded( const QString& ) ) );
00279 connect( d->mManager, SIGNAL( agentInstanceRemoved( const QString& ) ),
00280 this, SLOT( agentInstanceRemoved( const QString& ) ) );
00281 connect( d->mManager, SIGNAL( agentInstanceStatusChanged( const QString&, int, const QString& ) ),
00282 this, SLOT( agentInstanceStatusChanged( const QString&, int, const QString& ) ) );
00283 connect( d->mManager, SIGNAL( agentInstanceProgressChanged( const QString&, uint, const QString& ) ),
00284 this, SLOT( agentInstanceProgressChanged( const QString&, uint, const QString& ) ) );
00285 connect( d->mManager, SIGNAL( agentInstanceNameChanged( const QString&, const QString& ) ),
00286 this, SLOT( agentInstanceNameChanged( const QString&, const QString& ) ) );
00287 connect( d->mManager, SIGNAL( agentInstanceWarning( const QString&, const QString& ) ),
00288 this, SLOT( agentInstanceWarning( const QString&, const QString& ) ) );
00289 connect( d->mManager, SIGNAL( agentInstanceError( const QString&, const QString& ) ),
00290 this, SLOT( agentInstanceError( const QString&, const QString& ) ) );
00291 connect( d->mManager, SIGNAL(agentInstanceOnlineChanged(QString,bool)),
00292 SLOT(agentInstanceOnlineChanged(QString,bool)) );
00293
00294 if ( d->mManager->isValid() ) {
00295 QDBusReply<QStringList> result = d->mManager->agentTypes();
00296 if ( result.isValid() ) {
00297 foreach( const QString &type, result.value() ) {
00298 const AgentType agentType = d->fillAgentType( type );
00299 d->mTypes.insert( type, agentType );
00300 }
00301 }
00302
00303 result = d->mManager->agentInstances();
00304 if ( result.isValid() ) {
00305 foreach( const QString &instance, result.value() ) {
00306 const AgentInstance agentInstance = d->fillAgentInstance( instance );
00307 d->mInstances.insert( instance, agentInstance );
00308 }
00309 }
00310 }
00311 }
00312
00313
00314
00315 AgentManager::~AgentManager()
00316 {
00317 delete d;
00318 }
00319
00320 AgentManager* AgentManager::self()
00321 {
00322 if ( !AgentManagerPrivate::mSelf )
00323 AgentManagerPrivate::mSelf = new AgentManager();
00324
00325 return AgentManagerPrivate::mSelf;
00326 }
00327
00328 AgentType::List AgentManager::types() const
00329 {
00330 return d->mTypes.values();
00331 }
00332
00333 AgentType AgentManager::type( const QString &identifier ) const
00334 {
00335 return d->mTypes.value( identifier );
00336 }
00337
00338 AgentInstance::List AgentManager::instances() const
00339 {
00340 return d->mInstances.values();
00341 }
00342
00343 AgentInstance AgentManager::instance( const QString &identifier ) const
00344 {
00345 return d->mInstances.value( identifier );
00346 }
00347
00348 void AgentManager::removeInstance( const AgentInstance &instance )
00349 {
00350 d->mManager->removeAgentInstance( instance.identifier() );
00351 }
00352
00353 void AgentManager::synchronizeCollection(const Collection & collection)
00354 {
00355 const QString resId = collection.resource();
00356 Q_ASSERT( !resId.isEmpty() );
00357 d->mManager->agentInstanceSynchronizeCollection( resId, collection.id() );
00358 }
00359
00360 #include "agentmanager.moc"