20 #include "agentmanager.h"
21 #include "agentmanager_p.h"
23 #include "agenttype_p.h"
24 #include "agentinstance_p.h"
25 #include "dbusconnectionpool.h"
27 #include <akonadi/private/protocol_p.h>
29 #include "collection.h"
31 #include <QtDBus/QDBusServiceWatcher>
32 #include <QtGui/QWidget>
37 using namespace Akonadi;
43 const QString &identifier = mManager->createAgentInstance( type.
identifier() );
44 if ( identifier.isEmpty() )
47 return fillAgentInstanceLight( identifier );
50 void AgentManagerPrivate::agentTypeAdded(
const QString &identifier )
54 if ( mTypes.contains( identifier ) )
57 const AgentType type = fillAgentType( identifier );
59 mTypes.insert( identifier, type );
78 void AgentManagerPrivate::agentTypeRemoved(
const QString &identifier )
80 if ( !mTypes.contains( identifier ) )
83 const AgentType type = mTypes.take( identifier );
87 void AgentManagerPrivate::agentInstanceAdded(
const QString &identifier )
89 const AgentInstance instance = fillAgentInstance( identifier );
99 const bool newAgentInstance = !mInstances.contains( identifier );
100 if ( newAgentInstance ) {
101 mInstances.insert( identifier, instance );
104 mInstances.remove( identifier );
105 mInstances.insert( identifier, instance );
111 void AgentManagerPrivate::agentInstanceRemoved(
const QString &identifier )
113 if ( !mInstances.contains( identifier ) )
116 const AgentInstance instance = mInstances.take( identifier );
120 void AgentManagerPrivate::agentInstanceStatusChanged(
const QString &identifier,
int status,
const QString &msg )
122 if ( !mInstances.contains( identifier ) )
126 instance.d->mStatus = status;
127 instance.d->mStatusMessage = msg;
132 void AgentManagerPrivate::agentInstanceProgressChanged(
const QString &identifier, uint progress,
const QString &msg )
134 if ( !mInstances.contains( identifier ) )
138 instance.d->mProgress = progress;
139 if ( !msg.isEmpty() )
140 instance.d->mStatusMessage = msg;
145 void AgentManagerPrivate::agentInstanceWarning(
const QString &identifier,
const QString &msg )
147 if ( !mInstances.contains( identifier ) )
154 void AgentManagerPrivate::agentInstanceError(
const QString &identifier,
const QString &msg )
156 if ( !mInstances.contains( identifier ) )
163 void AgentManagerPrivate::agentInstanceOnlineChanged(
const QString &identifier,
bool state )
165 if ( !mInstances.contains( identifier ) )
169 instance.d->mIsOnline = state;
173 void AgentManagerPrivate::agentInstanceNameChanged(
const QString &identifier,
const QString &name )
175 if ( !mInstances.contains( identifier ) )
179 instance.d->mName = name;
186 const QDBusReply<QStringList> types = mManager->agentTypes();
187 if ( types.isValid() ) {
188 foreach (
const QString &type, types.value() ) {
189 if ( !mTypes.contains( type ) )
190 agentTypeAdded( type );
197 const QDBusReply<QStringList> instances = mManager->agentInstances();
198 if ( instances.isValid() ) {
199 foreach (
const QString &instance, instances.value() ) {
200 if ( !mInstances.contains( instance ) ) {
201 agentInstanceAdded( instance );
207 AgentType AgentManagerPrivate::fillAgentType(
const QString &identifier )
const
210 type.d->mIdentifier = identifier;
211 type.d->mName = mManager->agentName( identifier, KGlobal::locale()->language() );
212 type.d->mDescription = mManager->agentComment( identifier, KGlobal::locale()->language() );
213 type.d->mIconName = mManager->agentIcon( identifier );
214 type.d->mMimeTypes = mManager->agentMimeTypes( identifier );
215 type.d->mCapabilities = mManager->agentCapabilities( identifier );
220 void AgentManagerPrivate::setName(
const AgentInstance &instance,
const QString &name )
222 mManager->setAgentInstanceName( instance.
identifier(), name );
225 void AgentManagerPrivate::setOnline(
const AgentInstance &instance,
bool state )
227 mManager->setAgentInstanceOnline( instance.
identifier(), state );
230 void AgentManagerPrivate::configure(
const AgentInstance &instance, QWidget *parent )
234 winId = (qlonglong)( parent->window()->winId() );
236 mManager->agentInstanceConfigure( instance.
identifier(), winId );
239 void AgentManagerPrivate::synchronize(
const AgentInstance &instance )
241 mManager->agentInstanceSynchronize( instance.
identifier() );
244 void AgentManagerPrivate::synchronizeCollectionTree(
const AgentInstance &instance )
246 mManager->agentInstanceSynchronizeCollectionTree( instance.
identifier() );
249 AgentInstance AgentManagerPrivate::fillAgentInstance(
const QString &identifier )
const
253 const QString agentTypeIdentifier = mManager->agentInstanceType( identifier );
254 if ( !mTypes.contains( agentTypeIdentifier ) )
257 instance.d->mType = mTypes.value( agentTypeIdentifier );
258 instance.d->mIdentifier = identifier;
259 instance.d->mName = mManager->agentInstanceName( identifier );
260 instance.d->mStatus = mManager->agentInstanceStatus( identifier );
261 instance.d->mStatusMessage = mManager->agentInstanceStatusMessage( identifier );
262 instance.d->mProgress = mManager->agentInstanceProgress( identifier );
263 instance.d->mIsOnline = mManager->agentInstanceOnline( identifier );
268 AgentInstance AgentManagerPrivate::fillAgentInstanceLight(
const QString &identifier )
const
272 const QString agentTypeIdentifier = mManager->agentInstanceType( identifier );
273 Q_ASSERT_X( mTypes.contains( agentTypeIdentifier ),
"fillAgentInstanceLight",
"Requests non-existing agent type" );
275 instance.d->mType = mTypes.value( agentTypeIdentifier );
276 instance.d->mIdentifier = identifier;
281 void AgentManagerPrivate::serviceOwnerChanged(
const QString&,
const QString &oldOwner,
const QString& )
283 if ( oldOwner.isEmpty() ) {
289 void AgentManagerPrivate::createDBusInterface()
295 mManager =
new org::freedesktop::Akonadi::AgentManager( QLatin1String( AKONADI_DBUS_CONTROL_SERVICE ),
296 QLatin1String(
"/AgentManager" ),
297 DBusConnectionPool::threadConnection(), mParent );
299 QObject::connect( mManager, SIGNAL(agentTypeAdded(QString)),
300 mParent, SLOT(agentTypeAdded(QString)) );
301 QObject::connect( mManager, SIGNAL(agentTypeRemoved(QString)),
302 mParent, SLOT(agentTypeRemoved(QString)) );
303 QObject::connect( mManager, SIGNAL(agentInstanceAdded(QString)),
304 mParent, SLOT(agentInstanceAdded(QString)) );
305 QObject::connect( mManager, SIGNAL(agentInstanceRemoved(QString)),
306 mParent, SLOT(agentInstanceRemoved(QString)) );
307 QObject::connect( mManager, SIGNAL(agentInstanceStatusChanged(QString,
int,QString)),
308 mParent, SLOT(agentInstanceStatusChanged(QString,
int,QString)) );
309 QObject::connect( mManager, SIGNAL(agentInstanceProgressChanged(QString,uint,QString)),
310 mParent, SLOT(agentInstanceProgressChanged(QString,uint,QString)) );
311 QObject::connect( mManager, SIGNAL(agentInstanceNameChanged(QString,QString)),
312 mParent, SLOT(agentInstanceNameChanged(QString,QString)) );
313 QObject::connect( mManager, SIGNAL(agentInstanceWarning(QString,QString)),
314 mParent, SLOT(agentInstanceWarning(QString,QString)) );
315 QObject::connect( mManager, SIGNAL(agentInstanceError(QString,QString)),
316 mParent, SLOT(agentInstanceError(QString,QString)) );
317 QObject::connect( mManager, SIGNAL(agentInstanceOnlineChanged(QString,
bool)),
318 mParent, SLOT(agentInstanceOnlineChanged(QString,
bool)) );
320 if ( mManager->isValid() ) {
321 QDBusReply<QStringList> result = mManager->agentTypes();
322 if ( result.isValid() ) {
323 foreach (
const QString &type, result.value() ) {
324 const AgentType agentType = fillAgentType( type );
325 mTypes.insert( type, agentType );
328 result = mManager->agentInstances();
329 if ( result.isValid() ) {
330 foreach (
const QString &instance, result.value() ) {
331 const AgentInstance agentInstance = fillAgentInstance( instance );
332 mInstances.insert( instance, agentInstance );
336 kWarning() <<
"AgentManager failed to get a valid AgentManager DBus interface. Error is:" << mManager->lastError().type() << mManager->lastError().name() << mManager->lastError().message();
342 AgentManager::AgentManager()
346 qRegisterMetaType<Akonadi::AgentType>();
347 qRegisterMetaType<Akonadi::AgentInstance>();
349 d->createDBusInterface();
351 QDBusServiceWatcher *watcher =
new QDBusServiceWatcher( QLatin1String( AKONADI_DBUS_CONTROL_SERVICE ),
352 DBusConnectionPool::threadConnection(),
353 QDBusServiceWatcher::WatchForOwnerChange,
this );
354 connect( watcher, SIGNAL(serviceOwnerChanged(QString,QString,QString)),
355 this, SLOT(serviceOwnerChanged(QString,QString,QString)) );
367 if ( !AgentManagerPrivate::mSelf )
370 return AgentManagerPrivate::mSelf;
375 return d->mTypes.values();
380 return d->mTypes.value( identifier );
385 return d->mInstances.values();
390 return d->mInstances.value( identifier );
395 d->mManager->removeAgentInstance( instance.
identifier() );
405 const QString resId = collection.
resource();
406 Q_ASSERT( !resId.isEmpty() );
407 d->mManager->agentInstanceSynchronizeCollection( resId, collection.
id(), recursive );
410 #include "agentmanager.moc"