• Skip to content
  • Skip to link menu
KDE 4.4 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • Contact Us
 

akonadi

agentmanager.cpp

00001 /*
00002     Copyright (c) 2006-2008 Tobias Koenig <tokoe@kde.org>
00003 
00004     This library is free software; you can redistribute it and/or modify it
00005     under the terms of the GNU Library General Public License as published by
00006     the Free Software Foundation; either version 2 of the License, or (at your
00007     option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful, but WITHOUT
00010     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00011     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00012     License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to the
00016     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017     02110-1301, USA.
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 // @cond PRIVATE
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   // Ignore agent types we already know about, for example because we called
00050   // readAgentTypes before.
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     // The Akonadi ServerManager assumes that the server is up and running as soon
00059     // as it knows about at least one agent type.
00060     // If we emit the typeAdded() signal here, it therefore thinks the server is
00061     // running. However, the AgentManager does not know about all agent types yet,
00062     // as the server might still have pending agentTypeAdded() signals, even though
00063     // it internally knows all agent types already.
00064     // This can cause situations where the client gets told by the ServerManager that
00065     // the server is running, yet the client will encounter an error because the
00066     // AgentManager doesn't know all types yet.
00067     //
00068     // Therefore, we read all agent types from the server here so they are known.
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     // It is possible that this function is called when the instance is already
00090     // in our list we filled initially in the constructor.
00091     // This happens when the constructor is called during Akonadi startup, when
00092     // the agent processes are not fully loaded and have no D-Bus interface yet.
00093     // The server-side agent manager then emits the instance added signal when
00094     // the D-Bus interface for the agent comes up.
00095     // In this case, we simply notify that the instance status has changed.
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   // if the D-Bus interface is not valid at this point, watch for the
00336   // service to appear and create a new one then
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 // @endcond
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"

akonadi

Skip menu "akonadi"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kblog
  • kcal
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal