24 #include <tdeio/netaccess.h>
25 #include <tdeio/scheduler.h>
27 #include <ksavefile.h>
28 #include <tdetempfile.h>
29 #include <kurlrequester.h>
31 #include "addressbook.h"
32 #include "formatfactory.h"
33 #include "resourcenetconfig.h"
34 #include "stdaddressbook.h"
36 #include "resourcenet.h"
40 class ResourceNet::ResourceNetPrivate
49 TQString mLastErrorString;
52 ResourceNet::ResourceNet(
const TDEConfig *config )
53 : Resource( config ), mFormat( 0 ),
55 d( new ResourceNetPrivate )
60 init(
KURL(), TQString(
"vcard").latin1() );
64 ResourceNet::ResourceNet(
const KURL &url,
const TQString &format )
65 : Resource( 0 ), mFormat( 0 ),
67 d( new ResourceNetPrivate )
72 void ResourceNet::init(
const KURL &url,
const TQString &format )
75 d->mIsLoading =
false;
82 mFormat = factory->
format( mFormatName );
84 mFormatName = TQString(
"vcard").latin1();
85 mFormat = factory->
format( mFormatName );
91 ResourceNet::~ResourceNet()
104 deleteLocalTempFile();
107 void ResourceNet::writeConfig(
TDEConfig *config )
109 Resource::writeConfig( config );
112 config->
writeEntry(
"NetFormat", mFormatName );
115 Ticket *ResourceNet::requestSaveTicket()
117 kdDebug(5700) <<
"ResourceNet::requestSaveTicket()" <<
endl;
119 return createTicket(
this );
122 void ResourceNet::releaseSaveTicket(
Ticket *ticket )
127 bool ResourceNet::doOpen()
132 void ResourceNet::doClose()
136 bool ResourceNet::load()
140 if ( !TDEIO::NetAccess::download( mUrl, tempFile, 0 ) ) {
141 addressBook()->error( i18n(
"Unable to download file '%1'." ).arg( mUrl.prettyURL() ) );
145 TQFile file( tempFile );
146 if ( !file.open( IO_ReadOnly ) ) {
147 addressBook()->error( i18n(
"Unable to open file '%1'." ).arg( tempFile ) );
148 TDEIO::NetAccess::removeTempFile( tempFile );
152 bool result = clearAndLoad( &file );
154 addressBook()->error( i18n(
"Problems during parsing file '%1'." ).arg( tempFile ) );
156 TDEIO::NetAccess::removeTempFile( tempFile );
161 bool ResourceNet::clearAndLoad( TQFile *file )
164 return mFormat->loadAll( addressBook(),
this, file );
167 bool ResourceNet::asyncLoad()
169 if ( d->mIsLoading ) {
174 kdWarning(5700) <<
"Aborted asyncLoad() because we're still asyncSave()ing!" <<
endl;
178 bool ok = createLocalTempFile();
181 ok = mTempFile->close();
184 emit loadingError(
this, i18n(
"Unable to open file '%1'." ).arg( mTempFile->name() ) );
185 deleteLocalTempFile();
190 dest.
setPath( mTempFile->name() );
192 TDEIO::Scheduler::checkSlaveOnHold(
true );
193 d->mLoadJob = TDEIO::file_copy( mUrl, dest, -1,
true,
false,
false );
194 d->mIsLoading =
true;
195 connect( d->mLoadJob, TQ_SIGNAL( result( TDEIO::Job* ) ),
196 this, TQ_SLOT( downloadFinished( TDEIO::Job* ) ) );
201 void ResourceNet::abortAsyncLoading()
203 kdDebug(5700) <<
"ResourceNet::abortAsyncLoading()" <<
endl;
210 deleteLocalTempFile();
211 d->mIsLoading =
false;
214 void ResourceNet::abortAsyncSaving()
216 kdDebug(5700) <<
"ResourceNet::abortAsyncSaving()" <<
endl;
223 deleteLocalTempFile();
224 d->mIsSaving =
false;
227 bool ResourceNet::save(
Ticket* )
239 if ( tempFile.
status() == 0 && tempFile.
file() ) {
240 saveToFile( tempFile.
file() );
242 ok = tempFile.
close();
246 addressBook()->error( i18n(
"Unable to save file '%1'." ).arg( tempFile.
name() ) );
250 ok = TDEIO::NetAccess::upload( tempFile.
name(), mUrl, 0 );
252 addressBook()->error( i18n(
"Unable to upload to '%1'." ).arg( mUrl.prettyURL() ) );
257 bool ResourceNet::asyncSave(
Ticket* )
259 kdDebug(5700) <<
"ResourceNet::asyncSave()" <<
endl;
266 kdWarning(5700) <<
"Aborted asyncSave() because we're still asyncLoad()ing!" <<
endl;
270 bool ok = createLocalTempFile();
272 saveToFile( mTempFile->file() );
274 ok = mTempFile->close();
278 emit savingError(
this, i18n(
"Unable to save file '%1'." ).arg( mTempFile->name() ) );
279 deleteLocalTempFile();
284 src.
setPath( mTempFile->name() );
286 TDEIO::Scheduler::checkSlaveOnHold(
true );
288 d->mSaveJob = TDEIO::file_copy( src, mUrl, -1,
true,
false,
false );
289 connect( d->mSaveJob, TQ_SIGNAL( result( TDEIO::Job* ) ),
290 this, TQ_SLOT( uploadFinished( TDEIO::Job* ) ) );
295 bool ResourceNet::createLocalTempFile()
297 deleteStaleTempFile();
299 mTempFile->setAutoDelete(
true );
300 return mTempFile->status() == 0;
303 void ResourceNet::deleteStaleTempFile()
305 if ( hasTempFile() ) {
306 kdDebug(5700) <<
"stale temp file detected " << mTempFile->name() <<
endl;
307 deleteLocalTempFile();
311 void ResourceNet::deleteLocalTempFile()
317 void ResourceNet::saveToFile( TQFile *file )
319 mFormat->saveAll( addressBook(),
this, file );
322 void ResourceNet::setUrl(
const KURL &url )
327 KURL ResourceNet::url()
const
332 void ResourceNet::setFormat(
const TQString &name )
339 mFormat = factory->
format( mFormatName );
342 TQString ResourceNet::format()
const
347 void ResourceNet::downloadFinished( TDEIO::Job* )
349 kdDebug(5700) <<
"ResourceNet::downloadFinished()" <<
endl;
351 d->mIsLoading =
false;
353 if ( !hasTempFile() || mTempFile->status() != 0 ) {
354 d->mLastErrorString = i18n(
"Download failed: Unable to create temporary file" );
355 TQTimer::singleShot( 0,
this, TQ_SLOT( signalError() ) );
359 TQFile file( mTempFile->name() );
360 if ( file.open( IO_ReadOnly ) ) {
361 if ( clearAndLoad( &file ) )
362 emit loadingFinished(
this );
364 emit loadingError(
this, i18n(
"Problems during parsing file '%1'." ).arg( mTempFile->name() ) );
367 emit loadingError(
this, i18n(
"Unable to open file '%1'." ).arg( mTempFile->name() ) );
370 deleteLocalTempFile();
373 void ResourceNet::uploadFinished( TDEIO::Job *job )
375 kdDebug(5700) <<
"ResourceFile::uploadFinished()" <<
endl;
377 d->mIsSaving =
false;
380 emit savingError(
this, job->errorString() );
382 emit savingFinished(
this );
384 deleteLocalTempFile();
387 void ResourceNet::signalError()
389 emit loadingError(
this, d->mLastErrorString );
390 d->mLastErrorString.truncate( 0 );
393 #include "resourcenet.moc"
void setAutoDelete(bool autoDelete)
void setPath(const TQString &path)
Helper class for handling coordinated save of address books.
TQString readEntry(const TQString &pKey, const TQString &aDefault=TQString::null) const
void writePathEntry(const TQString &pKey, const TQString &path, bool bPersistent=true, bool bGlobal=false, bool bNLS=false)
void writeEntry(const TQString &pKey, const TQString &pValue, bool bPersistent=true, bool bGlobal=false, bool bNLS=false)
TQString readPathEntry(const TQString &pKey, const TQString &aDefault=TQString::null) const
kndbgstream & endl(kndbgstream &s)
kdbgstream kdWarning(int area=0)
kdbgstream kdDebug(int area=0)
TDEAction * clear(const TQObject *recvr, const char *slot, TDEActionCollection *parent, const char *name=0)
static data, shared by ALL addressee objects
TQString name(StdAccel id)