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 "dbusconnectionpool.h" 00020 #include "kjobprivatebase_p.h" 00021 00022 #include <akonadi/agentinstance.h> 00023 #include <akonadi/agentmanager.h> 00024 00025 #include <KDebug> 00026 #include <KLocale> 00027 00028 #include <QDBusConnection> 00029 #include <QDBusInterface> 00030 #include <QTimer> 00031 00032 namespace Akonadi 00033 { 00034 00035 class ResourceSynchronizationJobPrivate : public KJobPrivateBase 00036 { 00037 public: 00038 ResourceSynchronizationJobPrivate( ResourceSynchronizationJob* parent ) : 00039 q( parent ), 00040 interface( 0 ), 00041 safetyTimer( 0 ), 00042 timeoutCount( 0 ), 00043 collectionTreeOnly( false ) 00044 {} 00045 00046 void doStart(); 00047 00048 ResourceSynchronizationJob *q; 00049 AgentInstance instance; 00050 QDBusInterface* interface; 00051 QTimer* safetyTimer; 00052 int timeoutCount; 00053 bool collectionTreeOnly; 00054 static int timeoutCountLimit; 00055 00056 void slotSynchronized(); 00057 void slotTimeout(); 00058 }; 00059 00060 int ResourceSynchronizationJobPrivate::timeoutCountLimit = 60; 00061 00062 ResourceSynchronizationJob::ResourceSynchronizationJob(const AgentInstance& instance, QObject* parent) : 00063 KJob( parent ), 00064 d( new ResourceSynchronizationJobPrivate( this ) ) 00065 { 00066 d->instance = instance; 00067 d->safetyTimer = new QTimer( this ); 00068 connect( d->safetyTimer, SIGNAL(timeout()), SLOT(slotTimeout()) ); 00069 d->safetyTimer->setInterval( 10 * 1000 ); 00070 d->safetyTimer->setSingleShot( false ); 00071 } 00072 00073 ResourceSynchronizationJob::~ResourceSynchronizationJob() 00074 { 00075 delete d; 00076 } 00077 00078 void ResourceSynchronizationJob::start() 00079 { 00080 d->start(); 00081 } 00082 00083 bool ResourceSynchronizationJob::collectionTreeOnly() const 00084 { 00085 return d->collectionTreeOnly; 00086 } 00087 00088 void ResourceSynchronizationJob::setCollectionTreeOnly( bool b ) 00089 { 00090 d->collectionTreeOnly = b; 00091 } 00092 00093 void ResourceSynchronizationJobPrivate::doStart() 00094 { 00095 if ( !instance.isValid() ) { 00096 q->setError( KJob::UserDefinedError ); 00097 q->setErrorText( i18n( "Invalid resource instance." ) ); 00098 q->emitResult(); 00099 return; 00100 } 00101 00102 interface = new QDBusInterface( QString::fromLatin1( "org.freedesktop.Akonadi.Resource.%1" ).arg( instance.identifier() ), 00103 QString::fromLatin1( "/" ), 00104 QString::fromLatin1( "org.freedesktop.Akonadi.Resource" ), 00105 DBusConnectionPool::threadConnection(), this ); 00106 if ( collectionTreeOnly ) 00107 connect( interface, SIGNAL(collectionTreeSynchronized()), q, SLOT(slotSynchronized()) ); 00108 else 00109 connect( interface, SIGNAL(synchronized()), q, SLOT(slotSynchronized()) ); 00110 00111 if ( interface->isValid() ) { 00112 if ( collectionTreeOnly ) 00113 instance.synchronizeCollectionTree(); 00114 else 00115 instance.synchronize(); 00116 00117 safetyTimer->start(); 00118 } else { 00119 q->setError( KJob::UserDefinedError ); 00120 q->setErrorText( i18n( "Unable to obtain D-Bus interface for resource '%1'", instance.identifier() ) ); 00121 q->emitResult(); 00122 return; 00123 } 00124 } 00125 00126 void ResourceSynchronizationJobPrivate::slotSynchronized() 00127 { 00128 if ( collectionTreeOnly ) 00129 q->disconnect( interface, SIGNAL(collectionTreeSynchronized()), q, SLOT(slotSynchronized()) ); 00130 else 00131 q->disconnect( interface, SIGNAL(synchronized()), q, SLOT(slotSynchronized()) ); 00132 safetyTimer->stop(); 00133 q->emitResult(); 00134 } 00135 00136 void ResourceSynchronizationJobPrivate::slotTimeout() 00137 { 00138 instance = AgentManager::self()->instance( instance.identifier() ); 00139 timeoutCount++; 00140 00141 if ( timeoutCount > timeoutCountLimit ) { 00142 safetyTimer->stop(); 00143 q->setError( KJob::UserDefinedError ); 00144 q->setErrorText( i18n( "Resource synchronization timed out." ) ); 00145 q->emitResult(); 00146 return; 00147 } 00148 00149 if ( instance.status() == AgentInstance::Idle ) { 00150 // try again, we might have lost the synchronized()/synchronizedCollectionTree() signal 00151 kDebug() << "trying again to sync resource" << instance.identifier(); 00152 if ( collectionTreeOnly ) 00153 instance.synchronizeCollectionTree(); 00154 else 00155 instance.synchronize(); 00156 } 00157 } 00158 00159 AgentInstance ResourceSynchronizationJob::resource() const 00160 { 00161 return d->instance; 00162 } 00163 00164 } 00165 00166 #include "resourcesynchronizationjob.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Apr 30 2012 21:49:15 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Apr 30 2012 21:49:15 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.