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