• Skip to content
  • Skip to link menu
KDE 4.4 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 
00020 #include <akonadi/agentinstance.h>
00021 #include <akonadi/agentmanager.h>
00022 
00023 #include <KDebug>
00024 #include <KLocale>
00025 
00026 #include <QDBusConnection>
00027 #include <QDBusInterface>
00028 #include <QTimer>
00029 
00030 namespace Akonadi
00031 {
00032 
00033 class ResourceSynchronizationJobPrivate
00034 {
00035   public:
00036     ResourceSynchronizationJobPrivate( ResourceSynchronizationJob* parent ) :
00037       q( parent ),
00038       interface( 0 ),
00039       safetyTimer( 0 ),
00040       timeoutCount( 0 )
00041     {}
00042 
00043     ResourceSynchronizationJob *q;
00044     AgentInstance instance;
00045     QDBusInterface* interface;
00046     QTimer* safetyTimer;
00047     int timeoutCount;
00048     static int timeoutCountLimit;
00049 
00050     void slotSynchronized();
00051     void slotTimeout();
00052 };
00053 
00054 int ResourceSynchronizationJobPrivate::timeoutCountLimit = 60;
00055 
00056 ResourceSynchronizationJob::ResourceSynchronizationJob(const AgentInstance& instance, QObject* parent) :
00057   KJob( parent ),
00058   d( new ResourceSynchronizationJobPrivate( this ) )
00059 {
00060   d->instance = instance;
00061   d->safetyTimer = new QTimer( this );
00062   connect( d->safetyTimer, SIGNAL(timeout()), SLOT(slotTimeout()) );
00063   d->safetyTimer->setInterval( 10 * 1000 );
00064   d->safetyTimer->setSingleShot( false );
00065 }
00066 
00067 ResourceSynchronizationJob::~ResourceSynchronizationJob()
00068 {
00069   delete d;
00070 }
00071 
00072 void ResourceSynchronizationJob::start()
00073 {
00074   if ( !d->instance.isValid() ) {
00075     setError( UserDefinedError );
00076     setErrorText( i18n( "Invalid resource instance." ) );
00077     emitResult();
00078     return;
00079   }
00080 
00081   d->interface = new QDBusInterface( QString::fromLatin1( "org.freedesktop.Akonadi.Resource.%1" ).arg( d->instance.identifier() ),
00082                                       QString::fromLatin1( "/" ),
00083                                       QString::fromLatin1( "org.freedesktop.Akonadi.Resource" ), QDBusConnection::sessionBus(), this );
00084   connect( d->interface, SIGNAL(synchronized()), SLOT(slotSynchronized()) );
00085 
00086   if ( d->interface->isValid() ) {
00087     d->instance.synchronize();
00088     d->safetyTimer->start();
00089   } else {
00090     setError( UserDefinedError );
00091     setErrorText( i18n( "Unable to obtain D-Bus interface for resource '%1'", d->instance.identifier() ) );
00092     emitResult();
00093     return;
00094   }
00095 }
00096 
00097 void ResourceSynchronizationJobPrivate::slotSynchronized()
00098 {
00099   q->disconnect( interface, SIGNAL(synchronized()), q, SLOT(slotSynchronized()) );
00100   safetyTimer->stop();
00101   q->emitResult();
00102 }
00103 
00104 void ResourceSynchronizationJobPrivate::slotTimeout()
00105 {
00106   instance = AgentManager::self()->instance( instance.identifier() );
00107   timeoutCount++;
00108 
00109   if ( timeoutCount > timeoutCountLimit ) {
00110     safetyTimer->stop();
00111     q->setError( KJob::UserDefinedError );
00112     q->setErrorText( i18n( "Resource synchronization timed out." ) );
00113     q->emitResult();
00114     return;
00115   }
00116 
00117   if ( instance.status() == AgentInstance::Idle ) {
00118     // try again, we might have lost the synchronized() signal
00119     kDebug() << "trying again to sync resource" << instance.identifier();
00120     instance.synchronize();
00121   }
00122 }
00123 
00124 AgentInstance ResourceSynchronizationJob::resource() const
00125 {
00126   return d->instance;
00127 }
00128 
00129 }
00130 
00131 #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.6.2-20100208
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