20 #include "agentmanager.h"
21 #include "agentmanager_p.h"
23 #include "agenttype_p.h"
24 #include "agentinstance_p.h"
25 #include "dbusconnectionpool.h"
26 #include "servermanager.h"
28 #include "collection.h"
30 #include <QtDBus/QDBusServiceWatcher>
36 using namespace Akonadi;
42 const QString &identifier = mManager->createAgentInstance( type.
identifier() );
43 if ( identifier.isEmpty() ) {
47 return fillAgentInstanceLight( identifier );
50 void AgentManagerPrivate::agentTypeAdded(
const QString &identifier )
54 if ( mTypes.contains( identifier ) ) {
58 const AgentType type = fillAgentType( identifier );
60 mTypes.insert( identifier, type );
79 void AgentManagerPrivate::agentTypeRemoved(
const QString &identifier )
81 if ( !mTypes.contains( identifier ) ) {
85 const AgentType type = mTypes.take( identifier );
89 void AgentManagerPrivate::agentInstanceAdded(
const QString &identifier )
91 const AgentInstance instance = fillAgentInstance( identifier );
101 const bool newAgentInstance = !mInstances.contains( identifier );
102 if ( newAgentInstance ) {
103 mInstances.insert( identifier, instance );
106 mInstances.remove( identifier );
107 mInstances.insert( identifier, instance );
113 void AgentManagerPrivate::agentInstanceRemoved(
const QString &identifier )
115 if ( !mInstances.contains( identifier ) ) {
119 const AgentInstance instance = mInstances.take( identifier );
123 void AgentManagerPrivate::agentInstanceStatusChanged(
const QString &identifier,
int status,
const QString &msg )
125 if ( !mInstances.contains( identifier ) ) {
130 instance.d->mStatus = status;
131 instance.d->mStatusMessage = msg;
136 void AgentManagerPrivate::agentInstanceProgressChanged(
const QString &identifier, uint progress,
const QString &msg )
138 if ( !mInstances.contains( identifier ) ) {
143 instance.d->mProgress = progress;
144 if ( !msg.isEmpty() ) {
145 instance.d->mStatusMessage = msg;
151 void AgentManagerPrivate::agentInstanceWarning(
const QString &identifier,
const QString &msg )
153 if ( !mInstances.contains( identifier ) ) {
161 void AgentManagerPrivate::agentInstanceError(
const QString &identifier,
const QString &msg )
163 if ( !mInstances.contains( identifier ) ) {
171 void AgentManagerPrivate::agentInstanceOnlineChanged(
const QString &identifier,
bool state )
173 if ( !mInstances.contains( identifier ) ) {
178 instance.d->mIsOnline = state;
182 void AgentManagerPrivate::agentInstanceNameChanged(
const QString &identifier,
const QString &name )
184 if ( !mInstances.contains( identifier ) ) {
189 instance.d->mName = name;
196 const QDBusReply<QStringList> types = mManager->agentTypes();
197 if ( types.isValid() ) {
198 foreach (
const QString &type, types.value() ) {
199 if ( !mTypes.contains( type ) ) {
200 agentTypeAdded( type );
208 const QDBusReply<QStringList> instances = mManager->agentInstances();
209 if ( instances.isValid() ) {
210 foreach (
const QString &instance, instances.value() ) {
211 if ( !mInstances.contains( instance ) ) {
212 agentInstanceAdded( instance );
218 AgentType AgentManagerPrivate::fillAgentType(
const QString &identifier )
const
221 type.d->mIdentifier = identifier;
222 type.d->mName = mManager->agentName( identifier, KGlobal::locale()->language() );
223 type.d->mDescription = mManager->agentComment( identifier, KGlobal::locale()->language() );
224 type.d->mIconName = mManager->agentIcon( identifier );
225 type.d->mMimeTypes = mManager->agentMimeTypes( identifier );
226 type.d->mCapabilities = mManager->agentCapabilities( identifier );
231 void AgentManagerPrivate::setName(
const AgentInstance &instance,
const QString &name )
233 mManager->setAgentInstanceName( instance.
identifier(), name );
236 void AgentManagerPrivate::setOnline(
const AgentInstance &instance,
bool state )
238 mManager->setAgentInstanceOnline( instance.
identifier(), state );
241 void AgentManagerPrivate::configure(
const AgentInstance &instance, QWidget *parent )
245 winId = (qlonglong)( parent->window()->winId() );
248 mManager->agentInstanceConfigure( instance.
identifier(), winId );
251 void AgentManagerPrivate::synchronize(
const AgentInstance &instance )
253 mManager->agentInstanceSynchronize( instance.
identifier() );
256 void AgentManagerPrivate::synchronizeCollectionTree(
const AgentInstance &instance )
258 mManager->agentInstanceSynchronizeCollectionTree( instance.
identifier() );
261 AgentInstance AgentManagerPrivate::fillAgentInstance(
const QString &identifier )
const
265 const QString agentTypeIdentifier = mManager->agentInstanceType( identifier );
266 if ( !mTypes.contains( agentTypeIdentifier ) ) {
270 instance.d->mType = mTypes.value( agentTypeIdentifier );
271 instance.d->mIdentifier = identifier;
272 instance.d->mName = mManager->agentInstanceName( identifier );
273 instance.d->mStatus = mManager->agentInstanceStatus( identifier );
274 instance.d->mStatusMessage = mManager->agentInstanceStatusMessage( identifier );
275 instance.d->mProgress = mManager->agentInstanceProgress( identifier );
276 instance.d->mIsOnline = mManager->agentInstanceOnline( identifier );
281 AgentInstance AgentManagerPrivate::fillAgentInstanceLight(
const QString &identifier )
const
285 const QString agentTypeIdentifier = mManager->agentInstanceType( identifier );
286 Q_ASSERT_X( mTypes.contains( agentTypeIdentifier ),
"fillAgentInstanceLight",
"Requests non-existing agent type" );
288 instance.d->mType = mTypes.value( agentTypeIdentifier );
289 instance.d->mIdentifier = identifier;
294 void AgentManagerPrivate::serviceOwnerChanged(
const QString&,
const QString &oldOwner,
const QString& )
296 if ( oldOwner.isEmpty() ) {
302 void AgentManagerPrivate::createDBusInterface()
309 QLatin1String(
"/AgentManager" ),
310 DBusConnectionPool::threadConnection(), mParent );
312 QObject::connect( mManager, SIGNAL(agentTypeAdded(QString)),
313 mParent, SLOT(agentTypeAdded(QString)) );
314 QObject::connect( mManager, SIGNAL(agentTypeRemoved(QString)),
315 mParent, SLOT(agentTypeRemoved(QString)) );
316 QObject::connect( mManager, SIGNAL(agentInstanceAdded(QString)),
317 mParent, SLOT(agentInstanceAdded(QString)) );
318 QObject::connect( mManager, SIGNAL(agentInstanceRemoved(QString)),
319 mParent, SLOT(agentInstanceRemoved(QString)) );
320 QObject::connect( mManager, SIGNAL(agentInstanceStatusChanged(QString,
int,QString)),
321 mParent, SLOT(agentInstanceStatusChanged(QString,
int,QString)) );
322 QObject::connect( mManager, SIGNAL(agentInstanceProgressChanged(QString,uint,QString)),
323 mParent, SLOT(agentInstanceProgressChanged(QString,uint,QString)) );
324 QObject::connect( mManager, SIGNAL(agentInstanceNameChanged(QString,QString)),
325 mParent, SLOT(agentInstanceNameChanged(QString,QString)) );
326 QObject::connect( mManager, SIGNAL(agentInstanceWarning(QString,QString)),
327 mParent, SLOT(agentInstanceWarning(QString,QString)) );
328 QObject::connect( mManager, SIGNAL(agentInstanceError(QString,QString)),
329 mParent, SLOT(agentInstanceError(QString,QString)) );
330 QObject::connect( mManager, SIGNAL(agentInstanceOnlineChanged(QString,
bool)),
331 mParent, SLOT(agentInstanceOnlineChanged(QString,
bool)) );
333 if ( mManager->isValid() ) {
334 QDBusReply<QStringList> result = mManager->agentTypes();
335 if ( result.isValid() ) {
336 foreach (
const QString &type, result.value() ) {
337 const AgentType agentType = fillAgentType( type );
338 mTypes.insert( type, agentType );
341 result = mManager->agentInstances();
342 if ( result.isValid() ) {
343 foreach (
const QString &instance, result.value() ) {
344 const AgentInstance agentInstance = fillAgentInstance( instance );
345 mInstances.insert( instance, agentInstance );
349 kWarning() <<
"AgentManager failed to get a valid AgentManager DBus interface. Error is:" << mManager->lastError().type() << mManager->lastError().name() << mManager->lastError().message();
355 AgentManager::AgentManager()
359 qRegisterMetaType<Akonadi::AgentType>();
360 qRegisterMetaType<Akonadi::AgentInstance>();
362 d->createDBusInterface();
365 DBusConnectionPool::threadConnection(),
366 QDBusServiceWatcher::WatchForOwnerChange,
this );
367 connect( watcher, SIGNAL(serviceOwnerChanged(QString,QString,QString)),
368 this, SLOT(serviceOwnerChanged(QString,QString,QString)) );
380 if ( !AgentManagerPrivate::mSelf ) {
384 return AgentManagerPrivate::mSelf;
389 return d->mTypes.values();
394 return d->mTypes.value( identifier );
399 return d->mInstances.values();
404 return d->mInstances.value( identifier );
409 d->mManager->removeAgentInstance( instance.
identifier() );
419 const QString resId = collection.
resource();
420 Q_ASSERT( !resId.isEmpty() );
421 d->mManager->agentInstanceSynchronizeCollection( resId, collection.
id(), recursive );
424 #include "moc_agentmanager.cpp"