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