Commit 4656729e authored by Bertjan Broeksema's avatar Bertjan Broeksema
Browse files

We don't use the autosave timer anymore but schedule a write after

changes are made to the resource. If new changes are made before the
write is performed these will be scheduled before the write by the
scheduler.

Also a minor fix in singlefileresource.h. When calling readLocalFile()
it cannot be garuanteed that the call succeeds (e.g. the file has
invalid content). So now readLocalFile returns a bool and the status
of the resource is set appropriate when the file could not be read.

TODO: Remove the autosave timer from the settings(dialog)

svn path=/branches/work/akonadi-ports/kdepim/akonadi/; revision=1045642
parent 2a722227
......@@ -89,7 +89,7 @@ void ICalResource::itemAdded( const Akonadi::Item & item, const Akonadi::Collect
calendar()->addIncidence( i.get()->clone() );
Item it( item );
it.setRemoteId( i->uid() );
fileDirty();
scheduleWrite();
changeCommitted( it );
}
......@@ -121,7 +121,7 @@ void ICalResource::itemChanged( const Akonadi::Item &item, const QSet<QByteArray
calendar()->addIncidence( payload.get()->clone() );
}
}
fileDirty();
scheduleWrite();
changeCommitted( item );
}
......
......@@ -103,7 +103,7 @@ void ICalResourceBase::itemRemoved(const Akonadi::Item & item)
Incidence *i = mCalendar->incidence( item.remoteId() );
if ( i )
mCalendar->deleteIncidence( i );
fileDirty();
scheduleWrite();
changeProcessed();
}
......
......@@ -190,7 +190,7 @@ void MboxResource::itemAdded( const Akonadi::Item &item, const Akonadi::Collecti
return;
}
fileDirty();
scheduleWrite();
const QString rid = QString::number( collection.id() ) + "::"
+ collection.remoteId() + "::" + QString::number( offset );
......@@ -243,7 +243,7 @@ void MboxResource::itemRemoved( const Akonadi::Item &item )
&& Settings::self()->messageCount() == static_cast<uint>( attr->offsetCount() + 1 ) ) {
kDebug() << "Compacting mbox file";
mMBox->purge( attr->deletedItemOffsets() << itemOffset( item.remoteId() ) );
fileDirty();
scheduleWrite();
mboxCollection.removeAttribute<DeletedItemsAttribute>();
} else {
attr->addDeletedItemOffset( itemOffset( item.remoteId() ) );
......
......@@ -49,17 +49,6 @@ class SingleFileResource : public SingleFileResourceBase
setNeedsNetwork( !KUrl( Settings::self()->path() ).isLocalFile() );
}
/**
* Indicate that there are pending changes.
*/
void fileDirty()
{
if( !mDirtyTimer.isActive() ) {
mDirtyTimer.setInterval( Settings::self()->autosaveInterval() * 60 * 1000 );
mDirtyTimer.start();
}
}
/**
* Read changes from the backend file.
*/
......@@ -105,7 +94,12 @@ class SingleFileResource : public SingleFileResourceBase
}
}
readLocalFile( mCurrentUrl.toLocalFile() );
// Cache, because readLocalFile will clear mCurrentUrl on failure.
const QString localFileName = mCurrentUrl.toLocalFile();
if ( !readLocalFile( mCurrentUrl.toLocalFile() ) ) {
emit status( Broken, i18n( "Could not read file '%1'", localFileName ) );
return;
}
if ( Settings::self()->monitorFile() )
KDirWatch::self()->addFile( mCurrentUrl.toLocalFile() );
......@@ -149,8 +143,6 @@ class SingleFileResource : public SingleFileResourceBase
return;
}
mDirtyTimer.stop();
// We don't use the Settings::self()->path() here as that might have changed
// and in that case it would probably cause data lose.
if ( mCurrentUrl.isEmpty() ) {
......
......@@ -39,9 +39,6 @@ using namespace Akonadi;
SingleFileResourceBase::SingleFileResourceBase( const QString & id )
: ResourceBase( id ), mDownloadJob( 0 ), mUploadJob( 0 )
{
connect( &mDirtyTimer, SIGNAL( timeout() ), SLOT( writeFile() ) );
mDirtyTimer.setSingleShot( true );
connect( this, SIGNAL( reloadConfiguration() ), SLOT( reloadFile() ) );
QTimer::singleShot( 0, this, SLOT( readFile() ) );
......@@ -61,7 +58,7 @@ KSharedConfig::Ptr SingleFileResourceBase::runtimeConfig() const
return KSharedConfig::openConfig( name() + "rc", KConfig::SimpleConfig, "cache" );
}
void SingleFileResourceBase::readLocalFile( const QString &fileName )
bool SingleFileResourceBase::readLocalFile( const QString &fileName )
{
const QByteArray newHash = calculateHash( fileName );
if ( mCurrentHash != newHash ) {
......@@ -74,7 +71,7 @@ void SingleFileResourceBase::readLocalFile( const QString &fileName )
if ( !readFromFile( fileName ) ) {
mCurrentHash.clear();
mCurrentUrl = KUrl(); // reset so we don't accidentally overwrite the file
return;
return false;
}
if ( mCurrentHash.isEmpty() ) {
......@@ -99,6 +96,7 @@ void SingleFileResourceBase::readLocalFile( const QString &fileName )
mPreviousHash = mCurrentHash;
mCurrentHash = newHash;
return true;
}
void SingleFileResourceBase::setLocalFileName( const QString &fileName )
......@@ -202,7 +200,7 @@ void SingleFileResourceBase::fileChanged( const QString & fileName )
return;
// handle conflicts
if ( mDirtyTimer.isActive() && !mCurrentUrl.isEmpty() ) {
if ( !mCurrentUrl.isEmpty() ) {
const KUrl prevUrl = mCurrentUrl;
int i = 0;
QString lostFoundFileName;
......
......@@ -70,8 +70,9 @@ class SingleFileResourceBase : public ResourceBase, public Akonadi::AgentBase::O
* and notifies implementing resources to handle a hash change if the
* previous known hash was not empty. Finally this method clears the cache
* and calls synchronize.
* Returns true on succes, false otherwise.
*/
void readLocalFile( const QString &fileName );
bool readLocalFile( const QString &fileName );
/**
* Reimplement to read your data from the given file.
......@@ -136,7 +137,6 @@ class SingleFileResourceBase : public ResourceBase, public Akonadi::AgentBase::O
void saveHash( const QByteArray &hash ) const;
protected:
QTimer mDirtyTimer;
KUrl mCurrentUrl;
QStringList mSupportedMimetypes;
QString mCollectionIcon;
......@@ -145,10 +145,12 @@ class SingleFileResourceBase : public ResourceBase, public Akonadi::AgentBase::O
QByteArray mPreviousHash;
QByteArray mCurrentHash;
protected Q_SLOTS:
void scheduleWrite(); /// Called when changes are added to the ChangeRecorder.
private Q_SLOTS:
void handleProgress( KJob *, unsigned long );
void fileChanged( const QString &fileName );
void scheduleWrite(); /// Called when changes are added to the ChangeRecorder.
void slotDownloadJobResult( KJob * );
void slotUploadJobResult( KJob * );
};
......
......@@ -92,7 +92,7 @@ void VCardResource::itemAdded( const Akonadi::Item &item, const Akonadi::Collect
i.setRemoteId( addressee.uid() );
changeCommitted( i );
fileDirty();
scheduleWrite();
} else {
changeProcessed();
}
......@@ -111,7 +111,7 @@ void VCardResource::itemChanged( const Akonadi::Item &item, const QSet<QByteArra
i.setRemoteId( addressee.uid() );
changeCommitted( i );
fileDirty();
scheduleWrite();
} else {
changeProcessed();
}
......@@ -122,7 +122,7 @@ void VCardResource::itemRemoved(const Akonadi::Item & item)
if ( mAddressees.contains( item.remoteId() ) )
mAddressees.remove( item.remoteId() );
fileDirty();
scheduleWrite();
changeProcessed();
}
......
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