• Skip to content
  • Skip to link menu
KDE 4.3 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 initially 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   if ( !msg.isEmpty() )
00135     instance.d->mStatusMessage = msg;
00136 
00137   emit mParent->instanceProgressChanged( instance );
00138 }
00139 
00140 void AgentManagerPrivate::agentInstanceWarning( const QString &identifier, const QString &msg )
00141 {
00142   if ( !mInstances.contains( identifier ) )
00143     return;
00144 
00145   AgentInstance &instance = mInstances[ identifier ];
00146   emit mParent->instanceWarning( instance, msg );
00147 }
00148 
00149 void AgentManagerPrivate::agentInstanceError( const QString &identifier, const QString &msg )
00150 {
00151   if ( !mInstances.contains( identifier ) )
00152     return;
00153 
00154   AgentInstance &instance = mInstances[ identifier ];
00155   emit mParent->instanceError( instance, msg );
00156 }
00157 
00158 void AgentManagerPrivate::agentInstanceOnlineChanged( const QString &identifier, bool state )
00159 {
00160   if ( !mInstances.contains( identifier ) )
00161     return;
00162 
00163   AgentInstance &instance = mInstances[ identifier ];
00164   instance.d->mIsOnline = state;
00165   emit mParent->instanceOnline( instance, state );
00166 }
00167 
00168 void AgentManagerPrivate::agentInstanceNameChanged( const QString &identifier, const QString &name )
00169 {
00170   if ( !mInstances.contains( identifier ) )
00171     return;
00172 
00173   AgentInstance &instance = mInstances[ identifier ];
00174   instance.d->mName = name;
00175 
00176   emit mParent->instanceNameChanged( instance );
00177 }
00178 
00179 void AgentManagerPrivate::readAgentTypes()
00180 {
00181   QDBusReply<QStringList> types = mManager->agentTypes();
00182   if ( types.isValid() ) {
00183     foreach ( const QString &type, types.value() ) {
00184       if ( !mTypes.contains( type ) )
00185         agentTypeAdded( type );
00186     }
00187   }
00188 }
00189 
00190 AgentType AgentManagerPrivate::fillAgentType( const QString &identifier ) const
00191 {
00192   AgentType type;
00193   type.d->mIdentifier = identifier;
00194   type.d->mName = mManager->agentName( identifier );
00195   type.d->mDescription = mManager->agentComment( identifier );
00196   type.d->mIconName = mManager->agentIcon( identifier );
00197   type.d->mMimeTypes = mManager->agentMimeTypes( identifier );
00198   type.d->mCapabilities = mManager->agentCapabilities( identifier );
00199 
00200   return type;
00201 }
00202 
00203 void AgentManagerPrivate::setName( const AgentInstance &instance, const QString &name )
00204 {
00205   mManager->setAgentInstanceName( instance.identifier(), name );
00206 }
00207 
00208 void AgentManagerPrivate::setOnline( const AgentInstance &instance, bool state )
00209 {
00210   mManager->setAgentInstanceOnline( instance.identifier(), state );
00211 }
00212 
00213 void AgentManagerPrivate::configure( const AgentInstance &instance, QWidget *parent )
00214 {
00215   qlonglong winId = 0;
00216   if ( parent )
00217     winId = (qlonglong)( parent->window()->winId() );
00218 
00219   mManager->agentInstanceConfigure( instance.identifier(), winId );
00220 }
00221 
00222 void AgentManagerPrivate::synchronize( const AgentInstance &instance )
00223 {
00224   mManager->agentInstanceSynchronize( instance.identifier() );
00225 }
00226 
00227 void AgentManagerPrivate::synchronizeCollectionTree( const AgentInstance &instance )
00228 {
00229   mManager->agentInstanceSynchronizeCollectionTree( instance.identifier() );
00230 }
00231 
00232 AgentInstance AgentManagerPrivate::fillAgentInstance( const QString &identifier ) const
00233 {
00234   AgentInstance instance;
00235 
00236   const QString agentTypeIdentifier = mManager->agentInstanceType( identifier );
00237   if ( !mTypes.contains( agentTypeIdentifier ) )
00238     return instance;
00239 
00240   instance.d->mType = mTypes.value( agentTypeIdentifier );
00241   instance.d->mIdentifier = identifier;
00242   instance.d->mName = mManager->agentInstanceName( identifier );
00243   instance.d->mStatus = mManager->agentInstanceStatus( identifier );
00244   instance.d->mStatusMessage = mManager->agentInstanceStatusMessage( identifier );
00245   instance.d->mProgress = mManager->agentInstanceProgress( identifier );
00246   instance.d->mIsOnline = mManager->agentInstanceOnline( identifier );
00247 
00248   return instance;
00249 }
00250 
00251 AgentInstance AgentManagerPrivate::fillAgentInstanceLight( const QString &identifier ) const
00252 {
00253   AgentInstance instance;
00254 
00255   const QString agentTypeIdentifier = mManager->agentInstanceType( identifier );
00256   Q_ASSERT_X( mTypes.contains( agentTypeIdentifier ), "fillAgentInstanceLight", "Requests non-existing agent type" );
00257 
00258   instance.d->mType = mTypes.value( agentTypeIdentifier );
00259   instance.d->mIdentifier = identifier;
00260 
00261   return instance;
00262 }
00263 
00264 AgentManager* AgentManagerPrivate::mSelf = 0;
00265 
00266 AgentManager::AgentManager()
00267   : QObject( 0 ), d( new AgentManagerPrivate( this ) )
00268 {
00269   d->mManager = new org::freedesktop::Akonadi::AgentManager( QLatin1String( "org.freedesktop.Akonadi.Control" ),
00270                                                      QLatin1String( "/AgentManager" ),
00271                                                      QDBusConnection::sessionBus(), this );
00272 
00273   connect( d->mManager, SIGNAL( agentTypeAdded( const QString& ) ),
00274            this, SLOT( agentTypeAdded( const QString& ) ) );
00275   connect( d->mManager, SIGNAL( agentTypeRemoved( const QString& ) ),
00276            this, SLOT( agentTypeRemoved( const QString& ) ) );
00277   connect( d->mManager, SIGNAL( agentInstanceAdded( const QString& ) ),
00278            this, SLOT( agentInstanceAdded( const QString& ) ) );
00279   connect( d->mManager, SIGNAL( agentInstanceRemoved( const QString& ) ),
00280            this, SLOT( agentInstanceRemoved( const QString& ) ) );
00281   connect( d->mManager, SIGNAL( agentInstanceStatusChanged( const QString&, int, const QString& ) ),
00282            this, SLOT( agentInstanceStatusChanged( const QString&, int, const QString& ) ) );
00283   connect( d->mManager, SIGNAL( agentInstanceProgressChanged( const QString&, uint, const QString& ) ),
00284            this, SLOT( agentInstanceProgressChanged( const QString&, uint, const QString& ) ) );
00285   connect( d->mManager, SIGNAL( agentInstanceNameChanged( const QString&, const QString& ) ),
00286            this, SLOT( agentInstanceNameChanged( const QString&, const QString& ) ) );
00287   connect( d->mManager, SIGNAL( agentInstanceWarning( const QString&, const QString& ) ),
00288            this, SLOT( agentInstanceWarning( const QString&, const QString& ) ) );
00289   connect( d->mManager, SIGNAL( agentInstanceError( const QString&, const QString& ) ),
00290            this, SLOT( agentInstanceError( const QString&, const QString& ) ) );
00291   connect( d->mManager, SIGNAL(agentInstanceOnlineChanged(QString,bool)),
00292            SLOT(agentInstanceOnlineChanged(QString,bool)) );
00293 
00294   if ( d->mManager->isValid() ) {
00295     QDBusReply<QStringList> result = d->mManager->agentTypes();
00296     if ( result.isValid() ) {
00297       foreach( const QString &type, result.value() ) {
00298         const AgentType agentType = d->fillAgentType( type );
00299         d->mTypes.insert( type, agentType );
00300       }
00301     }
00302 
00303     result = d->mManager->agentInstances();
00304     if ( result.isValid() ) {
00305       foreach( const QString &instance, result.value() ) {
00306         const AgentInstance agentInstance = d->fillAgentInstance( instance );
00307         d->mInstances.insert( instance, agentInstance );
00308       }
00309     }
00310   }
00311 }
00312 
00313 // @endcond
00314 
00315 AgentManager::~AgentManager()
00316 {
00317   delete d;
00318 }
00319 
00320 AgentManager* AgentManager::self()
00321 {
00322   if ( !AgentManagerPrivate::mSelf )
00323     AgentManagerPrivate::mSelf = new AgentManager();
00324 
00325   return AgentManagerPrivate::mSelf;
00326 }
00327 
00328 AgentType::List AgentManager::types() const
00329 {
00330   return d->mTypes.values();
00331 }
00332 
00333 AgentType AgentManager::type( const QString &identifier ) const
00334 {
00335   return d->mTypes.value( identifier );
00336 }
00337 
00338 AgentInstance::List AgentManager::instances() const
00339 {
00340   return d->mInstances.values();
00341 }
00342 
00343 AgentInstance AgentManager::instance( const QString &identifier ) const
00344 {
00345   return d->mInstances.value( identifier );
00346 }
00347 
00348 void AgentManager::removeInstance( const AgentInstance &instance )
00349 {
00350   d->mManager->removeAgentInstance( instance.identifier() );
00351 }
00352 
00353 void AgentManager::synchronizeCollection(const Collection & collection)
00354 {
00355   const QString resId = collection.resource();
00356   Q_ASSERT( !resId.isEmpty() );
00357   d->mManager->agentInstanceSynchronizeCollection( resId, collection.id() );
00358 }
00359 
00360 #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
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  • kldap
  • kmime
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.9
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