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

akonadi

agentbase.cpp

00001 /*
00002     Copyright (c) 2006 Till Adam <adam@kde.org>
00003     Copyright (c) 2007 Volker Krause <vkrause@kde.org>
00004     Copyright (c) 2007 Bruno Virlet <bruno.virlet@gmail.com>
00005     Copyright (c) 2008 Kevin Krammer <kevin.krammer@gmx.at>
00006 
00007     This library is free software; you can redistribute it and/or modify it
00008     under the terms of the GNU Library General Public License as published by
00009     the Free Software Foundation; either version 2 of the License, or (at your
00010     option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful, but WITHOUT
00013     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00015     License for more details.
00016 
00017     You should have received a copy of the GNU Library General Public License
00018     along with this library; see the file COPYING.LIB.  If not, write to the
00019     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00020     02110-1301, USA.
00021 */
00022 
00023 #include "agentbase.h"
00024 #include "agentbase_p.h"
00025 
00026 #include "controladaptor.h"
00027 #include "statusadaptor.h"
00028 #include "monitor_p.h"
00029 #include "xdgbasedirs_p.h"
00030 
00031 #include "session.h"
00032 #include "session_p.h"
00033 #include "changerecorder.h"
00034 #include "itemfetchjob.h"
00035 
00036 #include <kaboutdata.h>
00037 #include <kcmdlineargs.h>
00038 #include <kdebug.h>
00039 #include <klocale.h>
00040 
00041 #include <QtCore/QDir>
00042 #include <QtCore/QSettings>
00043 #include <QtCore/QTimer>
00044 #include <QtGui/QApplication>
00045 #include <QtDBus/QtDBus>
00046 
00047 #include <signal.h>
00048 #include <stdlib.h>
00049 
00050 using namespace Akonadi;
00051 
00052 static AgentBase *sAgentBase = 0;
00053 
00054 AgentBase::Observer::Observer()
00055 {
00056 }
00057 
00058 AgentBase::Observer::~Observer()
00059 {
00060 }
00061 
00062 void AgentBase::Observer::itemAdded( const Item &item, const Collection &collection )
00063 {
00064   kDebug() << "sAgentBase=" << (void*) sAgentBase << "this=" << (void*) this;
00065   Q_UNUSED( item );
00066   Q_UNUSED( collection );
00067   if ( sAgentBase != 0 )
00068     sAgentBase->d_ptr->changeProcessed();
00069 }
00070 
00071 void AgentBase::Observer::itemChanged( const Item &item, const QSet<QByteArray> &partIdentifiers )
00072 {
00073   kDebug() << "sAgentBase=" << (void*) sAgentBase << "this=" << (void*) this;
00074   Q_UNUSED( item );
00075   Q_UNUSED( partIdentifiers );
00076   if ( sAgentBase != 0 )
00077     sAgentBase->d_ptr->changeProcessed();
00078 }
00079 
00080 void AgentBase::Observer::itemRemoved( const Item &item )
00081 {
00082   kDebug() << "sAgentBase=" << (void*) sAgentBase << "this=" << (void*) this;
00083   Q_UNUSED( item );
00084   if ( sAgentBase != 0 )
00085     sAgentBase->d_ptr->changeProcessed();
00086 }
00087 
00088 void AgentBase::Observer::collectionAdded( const Akonadi::Collection &collection, const Akonadi::Collection &parent )
00089 {
00090   kDebug() << "sAgentBase=" << (void*) sAgentBase << "this=" << (void*) this;
00091   Q_UNUSED( collection );
00092   Q_UNUSED( parent );
00093   if ( sAgentBase != 0 )
00094     sAgentBase->d_ptr->changeProcessed();
00095 }
00096 
00097 void AgentBase::Observer::collectionChanged( const Collection &collection )
00098 {
00099   kDebug() << "sAgentBase=" << (void*) sAgentBase << "this=" << (void*) this;
00100   Q_UNUSED( collection );
00101   if ( sAgentBase != 0 )
00102     sAgentBase->d_ptr->changeProcessed();
00103 }
00104 
00105 void AgentBase::Observer::collectionRemoved( const Collection &collection )
00106 {
00107   kDebug() << "sAgentBase=" << (void*) sAgentBase << "this=" << (void*) this;
00108   Q_UNUSED( collection );
00109   if ( sAgentBase != 0 )
00110     sAgentBase->d_ptr->changeProcessed();
00111 }
00112 
00113 //@cond PRIVATE
00114 
00115 AgentBasePrivate::AgentBasePrivate( AgentBase *parent )
00116   : q_ptr( parent ),
00117     mStatusCode( AgentBase::Idle ),
00118     mProgress( 0 ),
00119     mOnline( false ),
00120     mSettings( 0 ),
00121     mObserver( 0 )
00122 {
00123 }
00124 
00125 AgentBasePrivate::~AgentBasePrivate()
00126 {
00127   mMonitor->setConfig( 0 );
00128   delete mSettings;
00129 }
00130 
00131 void AgentBasePrivate::init()
00132 {
00133   Q_Q( AgentBase );
00134 
00138   SessionPrivate::createDefaultSession( mId.toLatin1() );
00139 
00140   mTracer = new org::freedesktop::Akonadi::Tracer( QLatin1String( "org.freedesktop.Akonadi" ), QLatin1String( "/tracing" ),
00141                                            QDBusConnection::sessionBus(), q );
00142 
00143   new ControlAdaptor( q );
00144   new StatusAdaptor( q );
00145   if ( !QDBusConnection::sessionBus().registerObject( QLatin1String( "/" ), q, QDBusConnection::ExportAdaptors ) )
00146     q->error( QString::fromLatin1( "Unable to register object at dbus: %1" ).arg( QDBusConnection::sessionBus().lastError().message() ) );
00147 
00148   mSettings = new QSettings( QString::fromLatin1( "%1/agent_config_%2" ).arg( XdgBaseDirs::saveDir( "config", QLatin1String( "akonadi" ) ), mId ), QSettings::IniFormat );
00149 
00150   mMonitor = new ChangeRecorder( q );
00151   mMonitor->ignoreSession( Session::defaultSession() );
00152   mMonitor->itemFetchScope().setCacheOnly( true );
00153   mMonitor->setConfig( mSettings );
00154 
00155   mOnline = mSettings->value( QLatin1String( "Agent/Online" ), true ).toBool();
00156 
00157   connect( mMonitor, SIGNAL( itemAdded( const Akonadi::Item&, const Akonadi::Collection& ) ),
00158            SLOT( itemAdded( const Akonadi::Item&, const Akonadi::Collection& ) ) );
00159   connect( mMonitor, SIGNAL( itemChanged( const Akonadi::Item&, const QSet<QByteArray>& ) ),
00160            SLOT( itemChanged( const Akonadi::Item&, const QSet<QByteArray>& ) ) );
00161   connect( mMonitor, SIGNAL( itemRemoved( const Akonadi::Item& ) ),
00162            SLOT( itemRemoved( const Akonadi::Item& ) ) );
00163   connect( mMonitor, SIGNAL(collectionAdded(Akonadi::Collection,Akonadi::Collection)),
00164            SLOT(collectionAdded(Akonadi::Collection,Akonadi::Collection)) );
00165   connect( mMonitor, SIGNAL( collectionChanged( const Akonadi::Collection& ) ),
00166            SLOT( collectionChanged( const Akonadi::Collection& ) ) );
00167   connect( mMonitor, SIGNAL( collectionRemoved( const Akonadi::Collection& ) ),
00168            SLOT( collectionRemoved( const Akonadi::Collection& ) ) );
00169 
00170   connect( q, SIGNAL( status( int, QString ) ), q, SLOT( slotStatus( int, QString ) ) );
00171   connect( q, SIGNAL( percent( int ) ), q, SLOT( slotPercent( int ) ) );
00172   connect( q, SIGNAL( warning( QString ) ), q, SLOT( slotWarning( QString ) ) );
00173   connect( q, SIGNAL( error( QString ) ), q, SLOT( slotError( QString ) ) );
00174 
00175   QTimer::singleShot( 0, q, SLOT( delayedInit() ) );
00176 }
00177 
00178 void AgentBasePrivate::delayedInit()
00179 {
00180   Q_Q( AgentBase );
00181   if ( !QDBusConnection::sessionBus().registerService( QLatin1String( "org.freedesktop.Akonadi.Agent." ) + mId ) )
00182     kFatal() << "Unable to register service at dbus:" << QDBusConnection::sessionBus().lastError().message();
00183   q->setOnline( mOnline );
00184 }
00185 
00186 void AgentBasePrivate::itemAdded( const Akonadi::Item &item, const Akonadi::Collection &collection )
00187 {
00188   kDebug() << "mObserver=" << (void*) mObserver << "this=" << (void*) this;
00189   if ( mObserver != 0 )
00190     mObserver->itemAdded( item, collection );
00191 }
00192 
00193 void AgentBasePrivate::itemChanged( const Akonadi::Item &item, const QSet<QByteArray> &partIdentifiers )
00194 {
00195   kDebug() << "mObserver=" << (void*) mObserver << "this=" << (void*) this;
00196   if ( mObserver != 0 )
00197     mObserver->itemChanged( item, partIdentifiers );
00198 }
00199 
00200 void AgentBasePrivate::itemRemoved( const Akonadi::Item &item )
00201 {
00202   kDebug() << "mObserver=" << (void*) mObserver << "this=" << (void*) this;
00203   if ( mObserver != 0 )
00204     mObserver->itemRemoved( item );
00205 }
00206 
00207 void AgentBasePrivate::collectionAdded( const Akonadi::Collection &collection, const Akonadi::Collection &parent )
00208 {
00209   kDebug() << "mObserver=" << (void*) mObserver << "this=" << (void*) this;
00210   if ( mObserver != 0 )
00211     mObserver->collectionAdded( collection, parent );
00212 }
00213 
00214 void AgentBasePrivate::collectionChanged( const Akonadi::Collection &collection )
00215 {
00216   kDebug() << "mObserver=" << (void*) mObserver << "this=" << (void*) this;
00217   if ( mObserver != 0 )
00218     mObserver->collectionChanged( collection );
00219 }
00220 
00221 void AgentBasePrivate::collectionRemoved( const Akonadi::Collection &collection )
00222 {
00223   kDebug() << "mObserver=" << (void*) mObserver << "this=" << (void*) this;
00224   if ( mObserver != 0 )
00225     mObserver->collectionRemoved( collection );
00226 }
00227 
00228 void AgentBasePrivate::changeProcessed()
00229 {
00230   mMonitor->changeProcessed();
00231   QTimer::singleShot( 0, mMonitor, SLOT( replayNext() ) );
00232 }
00233 
00234 void AgentBasePrivate::slotStatus( int status, const QString &message )
00235 {
00236   mStatusMessage = message;
00237   mStatusCode = 0;
00238 
00239   switch ( status ) {
00240     case AgentBase::Idle:
00241       if ( mStatusMessage.isEmpty() )
00242         mStatusMessage = defaultReadyMessage();
00243 
00244       mStatusCode = 0;
00245       break;
00246     case AgentBase::Running:
00247       if ( mStatusMessage.isEmpty() )
00248         mStatusMessage = defaultSyncingMessage();
00249 
00250       mStatusCode = 1;
00251       break;
00252     case AgentBase::Broken:
00253       if ( mStatusMessage.isEmpty() )
00254         mStatusMessage = defaultErrorMessage();
00255 
00256       mStatusCode = 2;
00257       break;
00258     default:
00259       Q_ASSERT( !"Unknown status passed" );
00260       break;
00261   }
00262 }
00263 
00264 void AgentBasePrivate::slotPercent( int progress )
00265 {
00266   mProgress = progress;
00267 }
00268 
00269 void AgentBasePrivate::slotWarning( const QString& message )
00270 {
00271   mTracer->warning( QString::fromLatin1( "AgentBase(%1)" ).arg( mId ), message );
00272 }
00273 
00274 void AgentBasePrivate::slotError( const QString& message )
00275 {
00276   mTracer->error( QString::fromLatin1( "AgentBase(%1)" ).arg( mId ), message );
00277 }
00278 
00279 
00280 AgentBase::AgentBase( const QString & id )
00281   : d_ptr( new AgentBasePrivate( this ) )
00282 {
00283   sAgentBase = this;
00284   d_ptr->mId = id;
00285   d_ptr->init();
00286 }
00287 
00288 // @endcond
00289 
00290 AgentBase::AgentBase( AgentBasePrivate* d, const QString &id ) :
00291     d_ptr( d )
00292 {
00293   sAgentBase = this;
00294   d_ptr->mId = id;
00295   d_ptr->init();
00296 }
00297 
00298 AgentBase::~AgentBase()
00299 {
00300   delete d_ptr;
00301 }
00302 
00303 static char* sAgentAppName = 0;
00304 
00305 QString AgentBase::parseArguments( int argc, char **argv )
00306 {
00307   QString identifier;
00308   if ( argc < 3 ) {
00309     kDebug( 5250 ) << "Not enough arguments passed...";
00310     exit( 1 );
00311   }
00312 
00313   for ( int i = 1; i < argc - 1; ++i ) {
00314     if ( QLatin1String( argv[ i ] ) == QLatin1String( "--identifier" ) )
00315       identifier = QLatin1String( argv[ i + 1 ] );
00316   }
00317 
00318   if ( identifier.isEmpty() ) {
00319     kDebug( 5250 ) << "Identifier argument missing";
00320     exit( 1 );
00321   }
00322 
00323   sAgentAppName = qstrdup( identifier.toLatin1().constData() );
00324   KCmdLineArgs::init( argc, argv, sAgentAppName, 0, ki18n("Akonadi Agent"),"0.1" ,
00325                       ki18n("Akonadi Agent") );
00326 
00327   KCmdLineOptions options;
00328   options.add("identifier <argument>", ki18n("Agent identifier"));
00329   KCmdLineArgs::addCmdLineOptions( options );
00330 
00331   return identifier;
00332 }
00333 
00334 int AgentBase::init( AgentBase *r )
00335 {
00336   QApplication::setQuitOnLastWindowClosed( false );
00337   int rv = kapp->exec();
00338   delete r;
00339   delete[] sAgentAppName;
00340   return rv;
00341 }
00342 
00343 int AgentBase::status() const
00344 {
00345   Q_D( const AgentBase );
00346 
00347   return d->mStatusCode;
00348 }
00349 
00350 QString AgentBase::statusMessage() const
00351 {
00352   Q_D( const AgentBase );
00353 
00354   return d->mStatusMessage;
00355 }
00356 
00357 int AgentBase::progress() const
00358 {
00359   Q_D( const AgentBase );
00360 
00361   return d->mProgress;
00362 }
00363 
00364 QString AgentBase::progressMessage() const
00365 {
00366   Q_D( const AgentBase );
00367 
00368   return d->mProgressMessage;
00369 }
00370 
00371 bool AgentBase::isOnline() const
00372 {
00373   Q_D( const AgentBase );
00374 
00375   return d->mOnline;
00376 }
00377 
00378 void AgentBase::setOnline( bool state )
00379 {
00380   Q_D( AgentBase );
00381   d->mOnline = state;
00382   d->mSettings->setValue( QLatin1String( "Agent/Online" ), state );
00383   doSetOnline( state );
00384 }
00385 
00386 void AgentBase::doSetOnline( bool online )
00387 {
00388   Q_UNUSED( online );
00389 }
00390 
00391 void AgentBase::configure( WId windowId )
00392 {
00393   Q_UNUSED( windowId );
00394 }
00395 
00396 #ifdef Q_OS_WIN
00397 void AgentBase::configure( qlonglong windowId )
00398 {
00399   configure( reinterpret_cast<WId>( windowId ) );
00400 }
00401 #endif
00402 
00403 WId AgentBase::winIdForDialogs() const
00404 {
00405   bool registered = QDBusConnection::sessionBus().interface()->isServiceRegistered( QLatin1String("org.freedesktop.akonaditray") );
00406   if ( !registered )
00407     return 0;
00408 
00409   QDBusInterface dbus( QLatin1String("org.freedesktop.akonaditray"), QLatin1String("/Actions"),
00410                        QLatin1String("org.freedesktop.Akonadi.Tray") );
00411   QDBusMessage reply = dbus.call( QLatin1String("getWinId") );
00412 
00413   if ( reply.type() == QDBusMessage::ErrorMessage )
00414     return 0;
00415 
00416   WId winid = (WId)reply.arguments().at( 0 ).toLongLong();
00417   return winid;
00418 }
00419 
00420 void AgentBase::quit()
00421 {
00422   Q_D( AgentBase );
00423   aboutToQuit();
00424 
00425   if ( d->mSettings ) {
00426     d->mMonitor->setConfig( 0 );
00427     d->mSettings->sync();
00428   }
00429 
00430   QCoreApplication::exit( 0 );
00431 }
00432 
00433 void AgentBase::aboutToQuit()
00434 {
00435 }
00436 
00437 void AgentBase::cleanup()
00438 {
00439   Q_D( AgentBase );
00440   const QString fileName = d->mSettings->fileName();
00441 
00442   /*
00443    * First destroy the settings object...
00444    */
00445   d->mMonitor->setConfig( 0 );
00446   delete d->mSettings;
00447   d->mSettings = 0;
00448 
00449   /*
00450    * ... then remove the file from hd.
00451    */
00452   QFile::remove( fileName );
00453 
00454   QCoreApplication::quit();
00455 }
00456 
00457 void AgentBase::registerObserver( Observer *observer )
00458 {
00459   kDebug() << "observer=" << (void*) observer << "this=" << (void*) this;
00460   d_ptr->mObserver = observer;
00461 }
00462 
00463 QString AgentBase::identifier() const
00464 {
00465   return d_ptr->mId;
00466 }
00467 
00468 void AgentBase::changeProcessed()
00469 {
00470   Q_D( AgentBase );
00471   d->changeProcessed();
00472 }
00473 
00474 ChangeRecorder * AgentBase::changeRecorder() const
00475 {
00476   return d_ptr->mMonitor;
00477 }
00478 
00479 #include "agentbase.moc"
00480 #include "agentbase_p.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
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.4
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