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

akonadi

resourcesynchronizationjob.cpp

00001 /*
00002  * Copyright (c) 2009 Volker Krause <vkrause@kde.org>
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2.1 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public
00015  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #include "resourcesynchronizationjob.h"
00019 #include "kjobprivatebase_p.h"
00020 
00021 #include <akonadi/agentinstance.h>
00022 #include <akonadi/agentmanager.h>
00023 
00024 #include <KDebug>
00025 #include <KLocale>
00026 
00027 #include <QDBusConnection>
00028 #include <QDBusInterface>
00029 #include <QTimer>
00030 
00031 namespace Akonadi
00032 {
00033 
00034 class ResourceSynchronizationJobPrivate : public KJobPrivateBase
00035 {
00036   public:
00037     ResourceSynchronizationJobPrivate( ResourceSynchronizationJob* parent ) :
00038       q( parent ),
00039       interface( 0 ),
00040       safetyTimer( 0 ),
00041       timeoutCount( 0 )
00042     {}
00043 
00044     void doStart();
00045 
00046     ResourceSynchronizationJob *q;
00047     AgentInstance instance;
00048     QDBusInterface* interface;
00049     QTimer* safetyTimer;
00050     int timeoutCount;
00051     static int timeoutCountLimit;
00052 
00053     void slotSynchronized();
00054     void slotTimeout();
00055 };
00056 
00057 int ResourceSynchronizationJobPrivate::timeoutCountLimit = 60;
00058 
00059 ResourceSynchronizationJob::ResourceSynchronizationJob(const AgentInstance& instance, QObject* parent) :
00060   KJob( parent ),
00061   d( new ResourceSynchronizationJobPrivate( this ) )
00062 {
00063   d->instance = instance;
00064   d->safetyTimer = new QTimer( this );
00065   connect( d->safetyTimer, SIGNAL( timeout() ), SLOT( slotTimeout() ) );
00066   d->safetyTimer->setInterval( 10 * 1000 );
00067   d->safetyTimer->setSingleShot( false );
00068 }
00069 
00070 ResourceSynchronizationJob::~ResourceSynchronizationJob()
00071 {
00072   delete d;
00073 }
00074 
00075 void ResourceSynchronizationJob::start()
00076 {
00077   d->start();
00078 }
00079 
00080 void ResourceSynchronizationJobPrivate::doStart()
00081 {
00082   if ( !instance.isValid() ) {
00083     q->setError( KJob::UserDefinedError );
00084     q->setErrorText( i18n( "Invalid resource instance." ) );
00085     q->emitResult();
00086     return;
00087   }
00088 
00089   interface = new QDBusInterface( QString::fromLatin1( "org.freedesktop.Akonadi.Resource.%1" ).arg( instance.identifier() ),
00090                                   QString::fromLatin1( "/" ),
00091                                   QString::fromLatin1( "org.freedesktop.Akonadi.Resource" ), QDBusConnection::sessionBus(), this );
00092   connect( interface, SIGNAL( synchronized() ), q, SLOT( slotSynchronized() ) );
00093 
00094   if ( interface->isValid() ) {
00095     instance.synchronize();
00096     safetyTimer->start();
00097   } else {
00098     q->setError( KJob::UserDefinedError );
00099     q->setErrorText( i18n( "Unable to obtain D-Bus interface for resource '%1'", instance.identifier() ) );
00100     q->emitResult();
00101     return;
00102   }
00103 }
00104 
00105 void ResourceSynchronizationJobPrivate::slotSynchronized()
00106 {
00107   q->disconnect( interface, SIGNAL( synchronized() ), q, SLOT( slotSynchronized() ) );
00108   safetyTimer->stop();
00109   q->emitResult();
00110 }
00111 
00112 void ResourceSynchronizationJobPrivate::slotTimeout()
00113 {
00114   instance = AgentManager::self()->instance( instance.identifier() );
00115   timeoutCount++;
00116 
00117   if ( timeoutCount > timeoutCountLimit ) {
00118     safetyTimer->stop();
00119     q->setError( KJob::UserDefinedError );
00120     q->setErrorText( i18n( "Resource synchronization timed out." ) );
00121     q->emitResult();
00122     return;
00123   }
00124 
00125   if ( instance.status() == AgentInstance::Idle ) {
00126     // try again, we might have lost the synchronized() signal
00127     kDebug() << "trying again to sync resource" << instance.identifier();
00128     instance.synchronize();
00129   }
00130 }
00131 
00132 AgentInstance ResourceSynchronizationJob::resource() const
00133 {
00134   return d->instance;
00135 }
00136 
00137 }
00138 
00139 #include "resourcesynchronizationjob.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.7.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