kabc
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>
32 #include <ksavefile.h>
33 #include <ktemporaryfile.h>
34 #include <kurlrequester.h>
35 #include <kconfiggroup.h>
37 #include <QtCore/QFile>
41 class ResourceNet::ResourceNetPrivate
51 ResourceNet::ResourceNet()
54 d( new ResourceNetPrivate )
56 init( KUrl(), QLatin1String(
"vcard" ) );
59 ResourceNet::ResourceNet(
const KConfigGroup &group )
62 d( new ResourceNetPrivate )
64 init( KUrl( group.readPathEntry(
"NetUrl", QString() ) ), group.readEntry(
"NetFormat" ) );
67 ResourceNet::ResourceNet(
const KUrl &url,
const QString &format )
70 d( new ResourceNetPrivate )
75 void 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 );
94 ResourceNet::~ResourceNet()
96 if ( d->mIsLoading ) {
109 deleteLocalTempFile();
116 group.writePathEntry(
"NetUrl", mUrl.url() );
117 group.writeEntry(
"NetFormat", mFormatName );
132 bool ResourceNet::doOpen()
137 void 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 );
167 bool 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*)) );
204 void ResourceNet::abortAsyncLoading()
213 deleteLocalTempFile();
214 d->mIsLoading =
false;
217 void 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*)) );
298 bool ResourceNet::createLocalTempFile()
300 deleteStaleTempFile();
301 mTempFile =
new KTemporaryFile();
302 return mTempFile->open();
305 void ResourceNet::deleteStaleTempFile()
307 if ( hasTempFile() ) {
308 kDebug() <<
"stale temp file detected" << mTempFile->fileName();
309 deleteLocalTempFile();
313 void ResourceNet::deleteLocalTempFile()
319 void ResourceNet::saveToFile( QFile *file )
340 mFormat = factory->
format( mFormatName );
348 void 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();
376 void 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 bool asyncLoad()
Loads all addressees asyncronously.
void loadingFinished(Resource *resource)
This signal is emitted when the resource has finished the loading of all addressees from the backend ...
KUrl url() const
Return url of directory used for loading and saving the address book.
void savingError(Resource *resource, const QString &msg)
This signal is emitted when an error occurred during saving the addressees from the internal cache to...
Class that holds a Calendar Url (FBURL/CALADRURI/CALURI)
Ticket * createTicket(Resource *)
Factory method, just creates and returns a new Ticket for the given resource.
virtual Ticket * requestSaveTicket()
Request a ticket, you have to pass through save() to allow locking.
virtual void writeConfig(KConfigGroup &group)
Writes the resource specific config to file.
virtual bool save(Ticket *ticket)
Saves all addressees synchronously.
virtual void writeConfig(KConfigGroup &group)
Writes the resource specific config to file.
virtual void clear()
Removes all addressees and distribution lists from the resource.
AddressBook * addressBook()
Returns a pointer to the addressbook.
void setFormat(const QString &name)
Sets a new format by name.
QString format() const
Returns the format name.
virtual void releaseSaveTicket(Ticket *ticket)
Releases the ticket previousely requested with requestSaveTicket().
void setUrl(const KUrl &url)
Set url of directory to be used for saving.
void savingFinished(Resource *resource)
This signal is emitted when the resource has finished the saving of all addressees from the internal ...
virtual bool load()
Loads all addressees synchronously.
Helper class for handling coordinated save of address books.
QString format() const
Returns the format name.
virtual bool asyncSave(Ticket *ticket)
Saves all addressees asynchronously.
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...
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Tue Jul 28 2020 00:00:00 by
doxygen 1.8.18 written
by
Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.