akonadi
resourcesynchronizationjob.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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
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"