21#include "resourcenet.h"
22#include "resourcenetconfig.h"
24#include "kabc/addressbook.h"
25#include "kabc/formatfactory.h"
26#include "kabc/stdaddressbook.h"
28#include <kio/netaccess.h>
29#include <kio/scheduler.h>
31#include <klocalizedstring.h>
33#include <ktemporaryfile.h>
34#include <kurlrequester.h>
35#include <kconfiggroup.h>
37#include <QtCore/QFile>
41class ResourceNet::ResourceNetPrivate
51ResourceNet::ResourceNet()
54 d( new ResourceNetPrivate )
56 init( KUrl(), QLatin1String(
"vcard" ) );
59ResourceNet::ResourceNet(
const KConfigGroup &group )
62 d( new ResourceNetPrivate )
64 init( KUrl( group.readPathEntry(
"NetUrl", QString() ) ), group.readEntry(
"NetFormat" ) );
67ResourceNet::ResourceNet(
const KUrl &url,
const QString &format )
70 d( new ResourceNetPrivate )
75void ResourceNet::init(
const KUrl &url,
const QString &format )
78 d->mIsLoading =
false;
85 mFormat = factory->
format( mFormatName );
87 mFormatName = QLatin1String(
"vcard" );
88 mFormat = factory->
format( mFormatName );
94ResourceNet::~ResourceNet()
96 if ( d->mIsLoading ) {
109 deleteLocalTempFile();
116 group.writePathEntry(
"NetUrl", mUrl.url() );
117 group.writeEntry(
"NetFormat", mFormatName );
132bool ResourceNet::doOpen()
137void ResourceNet::doClose()
145 if ( !KIO::NetAccess::download( mUrl, tempFile, 0 ) ) {
146 addressBook()->
error( i18n(
"Unable to download file '%1'.", mUrl.prettyUrl() ) );
150 QFile file( tempFile );
151 if ( !file.open( QIODevice::ReadOnly ) ) {
153 KIO::NetAccess::removeTempFile( tempFile );
157 bool result = clearAndLoad( &file );
162 KIO::NetAccess::removeTempFile( tempFile );
167bool ResourceNet::clearAndLoad( QFile *file )
175 if ( d->mIsLoading ) {
179 if ( d->mIsSaving ) {
180 kWarning() <<
"Aborted asyncLoad() because we're still saving!";
184 bool ok = createLocalTempFile();
187 emit
loadingError(
this, i18n(
"Unable to open file '%1'.", mTempFile->fileName() ) );
188 deleteLocalTempFile();
193 dest.setPath( mTempFile->fileName() );
195 KIO::Scheduler::checkSlaveOnHold(
true );
196 d->mLoadJob = KIO::file_copy( mUrl, dest, -1, KIO::Overwrite | KIO::HideProgressInfo );
197 d->mIsLoading =
true;
198 connect( d->mLoadJob, SIGNAL(result(KJob*)),
199 this, SLOT(downloadFinished(KJob*)) );
204void ResourceNet::abortAsyncLoading()
213 deleteLocalTempFile();
214 d->mIsLoading =
false;
217void ResourceNet::abortAsyncSaving()
226 deleteLocalTempFile();
227 d->mIsSaving =
false;
235 if ( d->mIsSaving ) {
239 KTemporaryFile tempFile;
240 bool ok = tempFile.open();
243 saveToFile( &tempFile );
248 addressBook()->
error( i18n(
"Unable to save file '%1'.", tempFile.fileName() ) );
252 ok = KIO::NetAccess::upload( tempFile.fileName(), mUrl, 0 );
254 addressBook()->
error( i18n(
"Unable to upload to '%1'.", mUrl.prettyUrl() ) );
265 if ( d->mIsSaving ) {
269 if ( d->mIsLoading ) {
270 kWarning() <<
"Aborted asyncSave() because we're still loading!";
274 bool ok = createLocalTempFile();
276 saveToFile( mTempFile );
281 emit
savingError(
this, i18n(
"Unable to save file '%1'.", mTempFile->fileName() ) );
282 deleteLocalTempFile();
287 src.setPath( mTempFile->fileName() );
289 KIO::Scheduler::checkSlaveOnHold(
true );
291 d->mSaveJob = KIO::file_copy( src, mUrl, -1, KIO::Overwrite | KIO::HideProgressInfo );
292 connect( d->mSaveJob, SIGNAL(result(KJob*)),
293 this, SLOT(uploadFinished(KJob*)) );
298bool ResourceNet::createLocalTempFile()
300 deleteStaleTempFile();
301 mTempFile =
new KTemporaryFile();
302 return mTempFile->open();
305void ResourceNet::deleteStaleTempFile()
307 if ( hasTempFile() ) {
308 kDebug() <<
"stale temp file detected" << mTempFile->fileName();
309 deleteLocalTempFile();
313void ResourceNet::deleteLocalTempFile()
319void ResourceNet::saveToFile( QFile *file )
340 mFormat = factory->
format( mFormatName );
348void ResourceNet::downloadFinished( KJob *job )
353 d->mIsLoading =
false;
355 if ( !hasTempFile() ) {
356 emit
loadingError(
this, i18n(
"Download failed, could not create temporary file" ) );
360 QFile file( mTempFile->fileName() );
361 if ( file.open( QIODevice::ReadOnly ) ) {
362 if ( clearAndLoad( &file ) ) {
365 emit
loadingError(
this, i18n(
"Problems during parsing file '%1'.",
366 mTempFile->fileName() ) );
369 emit
loadingError(
this, i18n(
"Unable to open file '%1'.",
370 mTempFile->fileName() ) );
373 deleteLocalTempFile();
376void ResourceNet::uploadFinished( KJob *job )
380 d->mIsSaving =
false;
382 if ( job->error() ) {
388 deleteLocalTempFile();
void error(const QString &msg)
Shows GUI independent error messages.
virtual Ticket * requestSaveTicket()
Request a ticket, you have to pass through save() to allow locking.
void setFormat(const QString &name)
Sets a new format by name.
virtual void releaseSaveTicket(Ticket *ticket)
Releases the ticket previousely requested with requestSaveTicket().
virtual bool save(Ticket *ticket)
Saves all addressees synchronously.
KUrl url() const
Return url of directory used for loading and saving the address book.
QString format() const
Returns the format name.
virtual void writeConfig(KConfigGroup &group)
Writes the resource specific config to file.
virtual bool asyncLoad()
Loads all addressees asyncronously.
void setUrl(const KUrl &url)
Set url of directory to be used for saving.
virtual bool asyncSave(Ticket *ticket)
Saves all addressees asynchronously.
virtual bool load()
Loads all addressees synchronously.
Ticket * createTicket(Resource *)
Factory method, just creates and returns a new Ticket for the given resource.
void savingFinished(Resource *resource)
This signal is emitted when the resource has finished the saving of all addressees from the internal ...
void savingError(Resource *resource, const QString &msg)
This signal is emitted when an error occurred during saving the addressees from the internal cache to...
AddressBook * addressBook()
Returns a pointer to the addressbook.
virtual void clear()
Removes all addressees and distribution lists from the resource.
virtual void writeConfig(KConfigGroup &group)
Writes the resource specific config to file.
void loadingError(Resource *resource, const QString &msg)
This signal is emitted when an error occurred during loading the addressees from the backend to the i...
void loadingFinished(Resource *resource)
This signal is emitted when the resource has finished the loading of all addressees from the backend ...
Helper class for handling coordinated save of address books.
Class that holds a Calendar Url (FBURL/CALADRURI/CALURI)