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 instance.d->mStatusMessage = msg;
00135
00136 emit mParent->instanceProgressChanged( instance );
00137 }
00138
00139 void AgentManagerPrivate::agentInstanceWarning( const QString &identifier, const QString &msg )
00140 {
00141 if ( !mInstances.contains( identifier ) )
00142 return;
00143
00144 AgentInstance &instance = mInstances[ identifier ];
00145 emit mParent->instanceWarning( instance, msg );
00146 }
00147
00148 void AgentManagerPrivate::agentInstanceError( const QString &identifier, const QString &msg )
00149 {
00150 if ( !mInstances.contains( identifier ) )
00151 return;
00152
00153 AgentInstance &instance = mInstances[ identifier ];
00154 emit mParent->instanceError( instance, msg );
00155 }
00156
00157 void AgentManagerPrivate::agentInstanceOnlineChanged( const QString &identifier, bool state )
00158 {
00159 if ( !mInstances.contains( identifier ) )
00160 return;
00161
00162 AgentInstance &instance = mInstances[ identifier ];
00163 instance.d->mIsOnline = state;
00164 emit mParent->instanceOnline( instance, state );
00165 }
00166
00167 void AgentManagerPrivate::agentInstanceNameChanged( const QString &identifier, const QString &name )
00168 {
00169 if ( !mInstances.contains( identifier ) )
00170 return;
00171
00172 AgentInstance &instance = mInstances[ identifier ];
00173 instance.d->mName = name;
00174
00175 emit mParent->instanceNameChanged( instance );
00176 }
00177
00178 void AgentManagerPrivate::readAgentTypes()
00179 {
00180 QDBusReply<QStringList> types = mManager->agentTypes();
00181 if ( types.isValid() ) {
00182 foreach ( const QString &type, types.value() ) {
00183 if ( !mTypes.contains( type ) )
00184 agentTypeAdded( type );
00185 }
00186 }
00187 }
00188
00189 AgentType AgentManagerPrivate::fillAgentType( const QString &identifier ) const
00190 {
00191 AgentType type;
00192 type.d->mIdentifier = identifier;
00193 type.d->mName = mManager->agentName( identifier );
00194 type.d->mDescription = mManager->agentComment( identifier );
00195 type.d->mIconName = mManager->agentIcon( identifier );
00196 type.d->mMimeTypes = mManager->agentMimeTypes( identifier );
00197 type.d->mCapabilities = mManager->agentCapabilities( identifier );
00198
00199 return type;
00200 }
00201
00202 void AgentManagerPrivate::setName( const AgentInstance &instance, const QString &name )
00203 {
00204 mManager->setAgentInstanceName( instance.identifier(), name );
00205 }
00206
00207 void AgentManagerPrivate::setOnline( const AgentInstance &instance, bool state )
00208 {
00209 mManager->setAgentInstanceOnline( instance.identifier(), state );
00210 }
00211
00212 void AgentManagerPrivate::configure( const AgentInstance &instance, QWidget *parent )
00213 {
00214 qlonglong winId = 0;
00215 if ( parent )
00216 winId = (qlonglong)( parent->window()->winId() );
00217
00218 mManager->agentInstanceConfigure( instance.identifier(), winId );
00219 }
00220
00221 void AgentManagerPrivate::synchronize( const AgentInstance &instance )
00222 {
00223 mManager->agentInstanceSynchronize( instance.identifier() );
00224 }
00225
00226 void AgentManagerPrivate::synchronizeCollectionTree( const AgentInstance &instance )
00227 {
00228 mManager->agentInstanceSynchronizeCollectionTree( instance.identifier() );
00229 }
00230
00231 AgentInstance AgentManagerPrivate::fillAgentInstance( const QString &identifier ) const
00232 {
00233 AgentInstance instance;
00234
00235 const QString agentTypeIdentifier = mManager->agentInstanceType( identifier );
00236 Q_ASSERT_X( mTypes.contains( agentTypeIdentifier ), "fillAgentInstance", "Requests non-existing agent type" );
00237
00238 instance.d->mType = mTypes.value( agentTypeIdentifier );
00239 instance.d->mIdentifier = identifier;
00240 instance.d->mName = mManager->agentInstanceName( identifier );
00241 instance.d->mStatus = mManager->agentInstanceStatus( identifier );
00242 instance.d->mStatusMessage = mManager->agentInstanceStatusMessage( identifier );
00243 instance.d->mProgress = mManager->agentInstanceProgress( identifier );
00244 instance.d->mIsOnline = mManager->agentInstanceOnline( identifier );
00245
00246 return instance;
00247 }
00248
00249 AgentInstance AgentManagerPrivate::fillAgentInstanceLight( const QString &identifier ) const
00250 {
00251 AgentInstance instance;
00252
00253 const QString agentTypeIdentifier = mManager->agentInstanceType( identifier );
00254 Q_ASSERT_X( mTypes.contains( agentTypeIdentifier ), "fillAgentInstanceLight", "Requests non-existing agent type" );
00255
00256 instance.d->mType = mTypes.value( agentTypeIdentifier );
00257 instance.d->mIdentifier = identifier;
00258
00259 return instance;
00260 }
00261
00262 AgentManager* AgentManagerPrivate::mSelf = 0;
00263
00264 AgentManager::AgentManager()
00265 : QObject( 0 ), d( new AgentManagerPrivate( this ) )
00266 {
00267 d->mManager = new org::freedesktop::Akonadi::AgentManager( QLatin1String( "org.freedesktop.Akonadi.Control" ),
00268 QLatin1String( "/AgentManager" ),
00269 QDBusConnection::sessionBus(), this );
00270
00271 connect( d->mManager, SIGNAL( agentTypeAdded( const QString& ) ),
00272 this, SLOT( agentTypeAdded( const QString& ) ) );
00273 connect( d->mManager, SIGNAL( agentTypeRemoved( const QString& ) ),
00274 this, SLOT( agentTypeRemoved( const QString& ) ) );
00275 connect( d->mManager, SIGNAL( agentInstanceAdded( const QString& ) ),
00276 this, SLOT( agentInstanceAdded( const QString& ) ) );
00277 connect( d->mManager, SIGNAL( agentInstanceRemoved( const QString& ) ),
00278 this, SLOT( agentInstanceRemoved( const QString& ) ) );
00279 connect( d->mManager, SIGNAL( agentInstanceStatusChanged( const QString&, int, const QString& ) ),
00280 this, SLOT( agentInstanceStatusChanged( const QString&, int, const QString& ) ) );
00281 connect( d->mManager, SIGNAL( agentInstanceProgressChanged( const QString&, uint, const QString& ) ),
00282 this, SLOT( agentInstanceProgressChanged( const QString&, uint, const QString& ) ) );
00283 connect( d->mManager, SIGNAL( agentInstanceNameChanged( const QString&, const QString& ) ),
00284 this, SLOT( agentInstanceNameChanged( const QString&, const QString& ) ) );
00285 connect( d->mManager, SIGNAL( agentInstanceWarning( const QString&, const QString& ) ),
00286 this, SLOT( agentInstanceWarning( const QString&, const QString& ) ) );
00287 connect( d->mManager, SIGNAL( agentInstanceError( const QString&, const QString& ) ),
00288 this, SLOT( agentInstanceError( const QString&, const QString& ) ) );
00289 connect( d->mManager, SIGNAL(agentInstanceOnlineChanged(QString,bool)),
00290 SLOT(agentInstanceOnlineChanged(QString,bool)) );
00291
00292 const QStringList typeIdentifiers = d->mManager->agentTypes();
00293 foreach( const QString &type, typeIdentifiers ) {
00294 const AgentType agentType = d->fillAgentType( type );
00295 d->mTypes.insert( type, agentType );
00296 }
00297
00298 const QStringList instanceIdentifiers = d->mManager->agentInstances();
00299 foreach( const QString &instance, instanceIdentifiers ) {
00300 const AgentInstance agentInstance = d->fillAgentInstance( instance );
00301 d->mInstances.insert( instance, agentInstance );
00302 }
00303 }
00304
00305
00306
00307 AgentManager::~AgentManager()
00308 {
00309 delete d;
00310 }
00311
00312 AgentManager* AgentManager::self()
00313 {
00314 if ( !AgentManagerPrivate::mSelf )
00315 AgentManagerPrivate::mSelf = new AgentManager();
00316
00317 return AgentManagerPrivate::mSelf;
00318 }
00319
00320 AgentType::List AgentManager::types() const
00321 {
00322 return d->mTypes.values();
00323 }
00324
00325 AgentType AgentManager::type( const QString &identifier ) const
00326 {
00327 return d->mTypes.value( identifier );
00328 }
00329
00330 AgentInstance::List AgentManager::instances() const
00331 {
00332 return d->mInstances.values();
00333 }
00334
00335 AgentInstance AgentManager::instance( const QString &identifier ) const
00336 {
00337 return d->mInstances.value( identifier );
00338 }
00339
00340 void AgentManager::removeInstance( const AgentInstance &instance )
00341 {
00342 d->mManager->removeAgentInstance( instance.identifier() );
00343 }
00344
00345 void AgentManager::synchronizeCollection(const Collection & collection)
00346 {
00347 const QString resId = collection.resource();
00348 Q_ASSERT( !resId.isEmpty() );
00349 d->mManager->agentInstanceSynchronizeCollection( resId, collection.id() );
00350 }
00351
00352 #include "agentmanager.moc"