• Skip to content
  • Skip to link menu
KDE 4.2 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 
00031 using namespace Akonadi;
00032 
00033 // @cond PRIVATE
00034 
00035 AgentInstance AgentManagerPrivate::createInstance( const AgentType &type )
00036 {
00037   const QString &identifier = mManager->createAgentInstance( type.identifier() );
00038   if ( identifier.isEmpty() )
00039     return AgentInstance();
00040 
00041   return fillAgentInstanceLight( identifier );
00042 }
00043 
00044 void AgentManagerPrivate::agentTypeAdded( const QString &identifier )
00045 {
00046   // Ignore agent types we already know about, for example because we called
00047   // readAgentTypes before.
00048   if ( mTypes.contains( identifier ) )
00049     return;
00050 
00051   const AgentType type = fillAgentType( identifier );
00052   if ( type.isValid() ) {
00053     mTypes.insert( identifier, type );
00054 
00055     // The Akonadi ServerManager assumes that the server is up and running as soon
00056     // as it knows about at least one agent type.
00057     // If we emit the typeAdded() signal here, it therefore thinks the server is
00058     // running. However, the AgentManager does not know about all agent types yet,
00059     // as the server might still have pending agentTypeAdded() signals, even though
00060     // it internally knows all agent types already.
00061     // This can cause situations where the client gets told by the ServerManager that
00062     // the server is running, yet the client will encounter an error because the
00063     // AgentManager doesn't know all types yet.
00064     //
00065     // Therefore, we read all agent types from the server here so they are known.
00066     readAgentTypes();
00067 
00068     emit mParent->typeAdded( type );
00069   }
00070 }
00071 
00072 void AgentManagerPrivate::agentTypeRemoved( const QString &identifier )
00073 {
00074   if ( !mTypes.contains( identifier ) )
00075     return;
00076 
00077   const AgentType type = mTypes.take( identifier );
00078   emit mParent->typeRemoved( type );
00079 }
00080 
00081 void AgentManagerPrivate::agentInstanceAdded( const QString &identifier )
00082 {
00083   const AgentInstance instance = fillAgentInstance( identifier );
00084   if ( instance.isValid() ) {
00085 
00086     // It is possible that this function is called when the instance is already
00087     // in our list we filled initally in the constructor.
00088     // This happens when the constructor is called during Akonadi startup, when
00089     // the agent processes are not fully loaded and have no D-Bus interface yet.
00090     // The server-side agent manager then emits the instance added signal when
00091     // the D-Bus interface for the agent comes up.
00092     // In this case, we simply notify that the instance status has changed.
00093     bool newAgentInstance = !mInstances.contains( identifier );
00094     if ( newAgentInstance ) {
00095       mInstances.insert( identifier, instance );
00096       emit mParent->instanceAdded( instance );
00097     }
00098     else {
00099       mInstances.remove( identifier );
00100       mInstances.insert( identifier, instance );
00101       emit mParent->instanceStatusChanged( instance );
00102     }
00103   }
00104 }
00105 
00106 void AgentManagerPrivate::agentInstanceRemoved( const QString &identifier )
00107 {
00108   if ( !mInstances.contains( identifier ) )
00109     return;
00110 
00111   const AgentInstance instance = mInstances.take( identifier );
00112   emit mParent->instanceRemoved( instance );
00113 }
00114 
00115 void AgentManagerPrivate::agentInstanceStatusChanged( const QString &identifier, int status, const QString &msg )
00116 {
00117   if ( !mInstances.contains( identifier ) )
00118     return;
00119 
00120   AgentInstance &instance = mInstances[ identifier ];
00121   instance.d->mStatus = status;
00122   instance.d->mStatusMessage = msg;
00123 
00124   emit mParent->instanceStatusChanged( instance );
00125 }
00126 
00127 void AgentManagerPrivate::agentInstanceProgressChanged( const QString &identifier, uint progress, const QString &msg )
00128 {
00129   if ( !mInstances.contains( identifier ) )
00130     return;
00131 
00132   AgentInstance &instance = mInstances[ identifier ];
00133   instance.d->mProgress = progress;
00134   instance.d->mStatusMessage = msg;
00135 
00136   emit mParent->instanceProgressChanged( instance );
00137 }
00138 
00139 void AgentManagerPrivate::agentInstanceWarning( const QString &identifier, const QString &msg )
00140 {
00141   if ( !mInstances.contains( identifier ) )
00142     return;
00143 
00144   AgentInstance &instance = mInstances[ identifier ];
00145   emit mParent->instanceWarning( instance, msg );
00146 }
00147 
00148 void AgentManagerPrivate::agentInstanceError( const QString &identifier, const QString &msg )
00149 {
00150   if ( !mInstances.contains( identifier ) )
00151     return;
00152 
00153   AgentInstance &instance = mInstances[ identifier ];
00154   emit mParent->instanceError( instance, msg );
00155 }
00156 
00157 void AgentManagerPrivate::agentInstanceOnlineChanged( const QString &identifier, bool state )
00158 {
00159   if ( !mInstances.contains( identifier ) )
00160     return;
00161 
00162   AgentInstance &instance = mInstances[ identifier ];
00163   instance.d->mIsOnline = state;
00164   emit mParent->instanceOnline( instance, state );
00165 }
00166 
00167 void AgentManagerPrivate::agentInstanceNameChanged( const QString &identifier, const QString &name )
00168 {
00169   if ( !mInstances.contains( identifier ) )
00170     return;
00171 
00172   AgentInstance &instance = mInstances[ identifier ];
00173   instance.d->mName = name;
00174 
00175   emit mParent->instanceNameChanged( instance );
00176 }
00177 
00178 void AgentManagerPrivate::readAgentTypes()
00179 {
00180   QDBusReply<QStringList> types = mManager->agentTypes();
00181   if ( types.isValid() ) {
00182     foreach ( const QString &type, types.value() ) {
00183       if ( !mTypes.contains( type ) )
00184         agentTypeAdded( type );
00185     }
00186   }
00187 }
00188 
00189 AgentType AgentManagerPrivate::fillAgentType( const QString &identifier ) const
00190 {
00191   AgentType type;
00192   type.d->mIdentifier = identifier;
00193   type.d->mName = mManager->agentName( identifier );
00194   type.d->mDescription = mManager->agentComment( identifier );
00195   type.d->mIconName = mManager->agentIcon( identifier );
00196   type.d->mMimeTypes = mManager->agentMimeTypes( identifier );
00197   type.d->mCapabilities = mManager->agentCapabilities( identifier );
00198 
00199   return type;
00200 }
00201 
00202 void AgentManagerPrivate::setName( const AgentInstance &instance, const QString &name )
00203 {
00204   mManager->setAgentInstanceName( instance.identifier(), name );
00205 }
00206 
00207 void AgentManagerPrivate::setOnline( const AgentInstance &instance, bool state )
00208 {
00209   mManager->setAgentInstanceOnline( instance.identifier(), state );
00210 }
00211 
00212 void AgentManagerPrivate::configure( const AgentInstance &instance, QWidget *parent )
00213 {
00214   qlonglong winId = 0;
00215   if ( parent )
00216     winId = (qlonglong)( parent->window()->winId() );
00217 
00218   mManager->agentInstanceConfigure( instance.identifier(), winId );
00219 }
00220 
00221 void AgentManagerPrivate::synchronize( const AgentInstance &instance )
00222 {
00223   mManager->agentInstanceSynchronize( instance.identifier() );
00224 }
00225 
00226 void AgentManagerPrivate::synchronizeCollectionTree( const AgentInstance &instance )
00227 {
00228   mManager->agentInstanceSynchronizeCollectionTree( instance.identifier() );
00229 }
00230 
00231 AgentInstance AgentManagerPrivate::fillAgentInstance( const QString &identifier ) const
00232 {
00233   AgentInstance instance;
00234 
00235   const QString agentTypeIdentifier = mManager->agentInstanceType( identifier );
00236   Q_ASSERT_X( mTypes.contains( agentTypeIdentifier ), "fillAgentInstance", "Requests non-existing agent type" );
00237 
00238   instance.d->mType = mTypes.value( agentTypeIdentifier );
00239   instance.d->mIdentifier = identifier;
00240   instance.d->mName = mManager->agentInstanceName( identifier );
00241   instance.d->mStatus = mManager->agentInstanceStatus( identifier );
00242   instance.d->mStatusMessage = mManager->agentInstanceStatusMessage( identifier );
00243   instance.d->mProgress = mManager->agentInstanceProgress( identifier );
00244   instance.d->mIsOnline = mManager->agentInstanceOnline( identifier );
00245 
00246   return instance;
00247 }
00248 
00249 AgentInstance AgentManagerPrivate::fillAgentInstanceLight( const QString &identifier ) const
00250 {
00251   AgentInstance instance;
00252 
00253   const QString agentTypeIdentifier = mManager->agentInstanceType( identifier );
00254   Q_ASSERT_X( mTypes.contains( agentTypeIdentifier ), "fillAgentInstanceLight", "Requests non-existing agent type" );
00255 
00256   instance.d->mType = mTypes.value( agentTypeIdentifier );
00257   instance.d->mIdentifier = identifier;
00258 
00259   return instance;
00260 }
00261 
00262 AgentManager* AgentManagerPrivate::mSelf = 0;
00263 
00264 AgentManager::AgentManager()
00265   : QObject( 0 ), d( new AgentManagerPrivate( this ) )
00266 {
00267   d->mManager = new org::freedesktop::Akonadi::AgentManager( QLatin1String( "org.freedesktop.Akonadi.Control" ),
00268                                                      QLatin1String( "/AgentManager" ),
00269                                                      QDBusConnection::sessionBus(), this );
00270 
00271   connect( d->mManager, SIGNAL( agentTypeAdded( const QString& ) ),
00272            this, SLOT( agentTypeAdded( const QString& ) ) );
00273   connect( d->mManager, SIGNAL( agentTypeRemoved( const QString& ) ),
00274            this, SLOT( agentTypeRemoved( const QString& ) ) );
00275   connect( d->mManager, SIGNAL( agentInstanceAdded( const QString& ) ),
00276            this, SLOT( agentInstanceAdded( const QString& ) ) );
00277   connect( d->mManager, SIGNAL( agentInstanceRemoved( const QString& ) ),
00278            this, SLOT( agentInstanceRemoved( const QString& ) ) );
00279   connect( d->mManager, SIGNAL( agentInstanceStatusChanged( const QString&, int, const QString& ) ),
00280            this, SLOT( agentInstanceStatusChanged( const QString&, int, const QString& ) ) );
00281   connect( d->mManager, SIGNAL( agentInstanceProgressChanged( const QString&, uint, const QString& ) ),
00282            this, SLOT( agentInstanceProgressChanged( const QString&, uint, const QString& ) ) );
00283   connect( d->mManager, SIGNAL( agentInstanceNameChanged( const QString&, const QString& ) ),
00284            this, SLOT( agentInstanceNameChanged( const QString&, const QString& ) ) );
00285   connect( d->mManager, SIGNAL( agentInstanceWarning( const QString&, const QString& ) ),
00286            this, SLOT( agentInstanceWarning( const QString&, const QString& ) ) );
00287   connect( d->mManager, SIGNAL( agentInstanceError( const QString&, const QString& ) ),
00288            this, SLOT( agentInstanceError( const QString&, const QString& ) ) );
00289   connect( d->mManager, SIGNAL(agentInstanceOnlineChanged(QString,bool)),
00290            SLOT(agentInstanceOnlineChanged(QString,bool)) );
00291 
00292   const QStringList typeIdentifiers = d->mManager->agentTypes();
00293   foreach( const QString &type, typeIdentifiers ) {
00294     const AgentType agentType = d->fillAgentType( type );
00295     d->mTypes.insert( type, agentType );
00296   }
00297 
00298   const QStringList instanceIdentifiers = d->mManager->agentInstances();
00299   foreach( const QString &instance, instanceIdentifiers ) {
00300     const AgentInstance agentInstance = d->fillAgentInstance( instance );
00301     d->mInstances.insert( instance, agentInstance );
00302   }
00303 }
00304 
00305 // @endcond
00306 
00307 AgentManager::~AgentManager()
00308 {
00309   delete d;
00310 }
00311 
00312 AgentManager* AgentManager::self()
00313 {
00314   if ( !AgentManagerPrivate::mSelf )
00315     AgentManagerPrivate::mSelf = new AgentManager();
00316 
00317   return AgentManagerPrivate::mSelf;
00318 }
00319 
00320 AgentType::List AgentManager::types() const
00321 {
00322   return d->mTypes.values();
00323 }
00324 
00325 AgentType AgentManager::type( const QString &identifier ) const
00326 {
00327   return d->mTypes.value( identifier );
00328 }
00329 
00330 AgentInstance::List AgentManager::instances() const
00331 {
00332   return d->mInstances.values();
00333 }
00334 
00335 AgentInstance AgentManager::instance( const QString &identifier ) const
00336 {
00337   return d->mInstances.value( identifier );
00338 }
00339 
00340 void AgentManager::removeInstance( const AgentInstance &instance )
00341 {
00342   d->mManager->removeAgentInstance( instance.identifier() );
00343 }
00344 
00345 void AgentManager::synchronizeCollection(const Collection & collection)
00346 {
00347   const QString resId = collection.resource();
00348   Q_ASSERT( !resId.isEmpty() );
00349   d->mManager->agentInstanceSynchronizeCollection( resId, collection.id() );
00350 }
00351 
00352 #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
  • kabc
  • kblog
  • kcal
  • kimap
  • kioslave
  •   imap4
  •   mbox
  • kldap
  • kmime
  • kpimidentities
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.8
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