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