Commit 4e61bebd authored by Bertjan Broeksema's avatar Bertjan Broeksema
Browse files

Make sure that before retrieving items for a file cached changes

are written to the file to prevent reading items that are not in
the file yet, or are still in the file but shouldn't be there.

svn path=/branches/work/akonadi-ports/kdepim/akonadi/; revision=1036645
parent 7bcd8eae
......@@ -107,7 +107,7 @@ void ICalResourceBase::itemRemoved(const Akonadi::Item & item)
changeProcessed();
}
void ICalResourceBase::retrieveItems( const Akonadi::Collection & col )
void ICalResourceBase::retrieveItemsFromFile( const Akonadi::Collection & col )
{
if ( !mCalendar )
return;
......
......@@ -44,7 +44,6 @@ class ICalResourceBase : public Akonadi::SingleFileResource<Settings>
virtual void configure( WId windowId );
protected Q_SLOTS:
void retrieveItems( const Akonadi::Collection &col );
bool retrieveItem( const Akonadi::Item &item, const QSet<QByteArray> &parts );
protected:
......@@ -54,6 +53,7 @@ class ICalResourceBase : public Akonadi::SingleFileResource<Settings>
bool readFromFile( const QString &fileName );
bool writeToFile( const QString &fileName );
virtual void aboutToQuit();
virtual void retrieveItemsFromFile( const Akonadi::Collection &col );
/**
* Retrieve an incidence from the calendar, and set it into a new item's payload.
......
......@@ -463,7 +463,7 @@ QByteArray MBox::readEntryHeaders( quint64 offset )
Q_ASSERT( d->mFileLocked );
Q_ASSERT( d->mMboxFile.isOpen() );
Q_ASSERT( d->mMboxFile.size() > 0 );
Q_ASSERT( d->mMboxFile.size() > offset );
Q_ASSERT( static_cast<quint64>(d->mMboxFile.size()) > offset );
d->mMboxFile.seek( offset );
......
......@@ -97,7 +97,7 @@ void MboxResource::configure( WId windowId )
delete dlg;
}
void MboxResource::retrieveItems( const Akonadi::Collection &col )
void MboxResource::retrieveItemsFromFile( const Akonadi::Collection &col )
{
Q_UNUSED( col );
if ( !mMBox )
......
......@@ -37,11 +37,11 @@ class MboxResource : public Akonadi::SingleFileResource<Settings>
virtual void configure( WId windowId );
protected Q_SLOTS:
virtual void retrieveItems( const Akonadi::Collection &col );
virtual bool retrieveItem( const Akonadi::Item &item, const QSet<QByteArray> &parts );
protected:
virtual void aboutToQuit();
virtual void retrieveItemsFromFile( const Akonadi::Collection &col );
virtual void itemAdded( const Akonadi::Item &item, const Akonadi::Collection &collection );
virtual void itemChanged( const Akonadi::Item &item, const QSet<QByteArray> &parts );
......
......@@ -169,6 +169,7 @@ class SingleFileResource : public SingleFileResourceBase
if ( !writeResult )
return;
mHasUnwrittenChanges = false;
emit status( Idle, i18nc( "@info:status", "Ready" ) );
} else {
// Check if there is a download or an upload in progress.
......
......@@ -37,7 +37,7 @@
using namespace Akonadi;
SingleFileResourceBase::SingleFileResourceBase( const QString & id )
: ResourceBase( id ), mDownloadJob( 0 ), mUploadJob( 0 )
: ResourceBase( id ), mDownloadJob( 0 ), mUploadJob( 0 ), mHasUnwrittenChanges(false)
{
connect( &mDirtyTimer, SIGNAL( timeout() ), SLOT( writeFile() ) );
mDirtyTimer.setSingleShot( true );
......@@ -48,12 +48,22 @@ SingleFileResourceBase::SingleFileResourceBase( const QString & id )
changeRecorder()->itemFetchScope().fetchFullPayload();
changeRecorder()->fetchCollection( true );
connect( changeRecorder(), SIGNAL( changesAdded() ), SLOT( markAsDirty() ) );
connect( KDirWatch::self(), SIGNAL( dirty( QString ) ), SLOT( fileChanged( QString ) ) );
connect( KDirWatch::self(), SIGNAL( created( QString ) ), SLOT( fileChanged( QString ) ) );
KGlobal::locale()->insertCatalog( "akonadi_singlefile_resource" );
}
void SingleFileResourceBase::retrieveItems( const Akonadi::Collection &col )
{
if ( mHasUnwrittenChanges )
writeFile();
retrieveItemsFromFile( col );
}
KSharedConfig::Ptr SingleFileResourceBase::runtimeConfig() const
{
return KSharedConfig::openConfig( name() + "rc", KConfig::SimpleConfig, "cache" );
......@@ -230,6 +240,11 @@ void SingleFileResourceBase::fileChanged( const QString & fileName )
}
}
void SingleFileResourceBase::markAsDirty()
{
mHasUnwrittenChanges = true;
}
void SingleFileResourceBase::slotDownloadJobResult( KJob *job )
{
if ( job->error() && job->error() != KIO::ERR_DOES_NOT_EXIST ) {
......@@ -253,6 +268,7 @@ void SingleFileResourceBase::slotUploadJobResult( KJob *job )
mUploadJob = 0;
KGlobal::deref();
mHasUnwrittenChanges = false;
emit status( Idle, i18nc( "@info:status", "Ready" ) );
}
......
......@@ -56,7 +56,22 @@ class SingleFileResourceBase : public ResourceBase, public Akonadi::AgentBase::O
virtual void readFile() = 0;
virtual void writeFile() = 0;
protected Q_SLOTS:
/**
* This does not actually retrieve the items but checks if the resources is
* has unwritten changes and write them before calling retrieveItemsFromFile().
*/
virtual void retrieveItems( const Akonadi::Collection &col );
protected:
/**
* This is the real implementation of retrieveItems( const Akonadi::Collection &col )
* and should be implemented by the specific resourses. When this method is
* called it can be assumed that changes there are no pending changes that
* need to be written to file.
*/
virtual void retrieveItemsFromFile( const Akonadi::Collection &col ) = 0;
/**
* Returns a pointer to the KConfig object which is used to store runtime
* information of the resource.
......@@ -144,10 +159,12 @@ class SingleFileResourceBase : public ResourceBase, public Akonadi::AgentBase::O
KIO::FileCopyJob *mUploadJob;
QByteArray mPreviousHash;
QByteArray mCurrentHash;
bool mHasUnwrittenChanges;
private Q_SLOTS:
void handleProgress( KJob *, unsigned long );
void fileChanged( const QString &fileName );
void markAsDirty(); /// Called when changes are added to the ChangeRecorder.
void slotDownloadJobResult( KJob * );
void slotUploadJobResult( KJob * );
};
......
......@@ -127,7 +127,7 @@ void VCardResource::itemRemoved(const Akonadi::Item & item)
changeProcessed();
}
void VCardResource::retrieveItems( const Akonadi::Collection & col )
void VCardResource::retrieveItemsFromFile( const Akonadi::Collection & col )
{
// VCard does not support folders so we can safely ignore the collection
Q_UNUSED( col );
......
......@@ -38,13 +38,13 @@ class VCardResource : public Akonadi::SingleFileResource<Settings>
virtual void configure( WId windowId );
protected Q_SLOTS:
void retrieveItems( const Akonadi::Collection &col );
bool retrieveItem( const Akonadi::Item &item, const QSet<QByteArray> &parts );
protected:
bool readFromFile( const QString &fileName );
bool writeToFile( const QString &fileName );
virtual void aboutToQuit();
virtual void retrieveItemsFromFile( const Akonadi::Collection &col );
virtual void itemAdded( const Akonadi::Item &item, const Akonadi::Collection &collection );
virtual void itemChanged( const Akonadi::Item &item, const QSet<QByteArray> &parts );
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment