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