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