• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeabc
 

tdeabc

  • tdeabc
  • plugins
  • net
resourcenet.cpp
1/*
2 This file is part of libtdeabc.
3 Copyright (c) 2003 Tobias Koenig <tokoe@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21#include <tqfile.h>
22
23#include <kdebug.h>
24#include <tdeio/netaccess.h>
25#include <tdeio/scheduler.h>
26#include <tdelocale.h>
27#include <ksavefile.h>
28#include <tdetempfile.h>
29#include <kurlrequester.h>
30
31#include "addressbook.h"
32#include "formatfactory.h"
33#include "resourcenetconfig.h"
34#include "stdaddressbook.h"
35
36#include "resourcenet.h"
37
38using namespace TDEABC;
39
40class ResourceNet::ResourceNetPrivate
41{
42 public:
43 TDEIO::Job *mLoadJob;
44 bool mIsLoading;
45
46 TDEIO::Job *mSaveJob;
47 bool mIsSaving;
48
49 TQString mLastErrorString;
50};
51
52ResourceNet::ResourceNet( const TDEConfig *config )
53 : Resource( config ), mFormat( 0 ),
54 mTempFile( 0 ),
55 d( new ResourceNetPrivate )
56{
57 if ( config ) {
58 init( KURL( config->readPathEntry( "NetUrl" ) ), config->readEntry( "NetFormat" ) );
59 } else {
60 init( KURL(), TQString("vcard").latin1() );
61 }
62}
63
64ResourceNet::ResourceNet( const KURL &url, const TQString &format )
65 : Resource( 0 ), mFormat( 0 ),
66 mTempFile( 0 ),
67 d( new ResourceNetPrivate )
68{
69 init( url, format );
70}
71
72void ResourceNet::init( const KURL &url, const TQString &format )
73{
74 d->mLoadJob = 0;
75 d->mIsLoading = false;
76 d->mSaveJob = 0;
77 d->mIsSaving = false;
78
79 mFormatName = format;
80
81 FormatFactory *factory = FormatFactory::self();
82 mFormat = factory->format( mFormatName );
83 if ( !mFormat ) {
84 mFormatName = TQString("vcard").latin1();
85 mFormat = factory->format( mFormatName );
86 }
87
88 setUrl( url );
89}
90
91ResourceNet::~ResourceNet()
92{
93 if ( d->mIsLoading )
94 d->mLoadJob->kill();
95 if ( d->mIsSaving )
96 d->mSaveJob->kill();
97
98 delete d;
99 d = 0;
100
101 delete mFormat;
102 mFormat = 0;
103
104 deleteLocalTempFile();
105}
106
107void ResourceNet::writeConfig( TDEConfig *config )
108{
109 Resource::writeConfig( config );
110
111 config->writePathEntry( "NetUrl", mUrl.url() );
112 config->writeEntry( "NetFormat", mFormatName );
113}
114
115Ticket *ResourceNet::requestSaveTicket()
116{
117 kdDebug(5700) << "ResourceNet::requestSaveTicket()" << endl;
118
119 return createTicket( this );
120}
121
122void ResourceNet::releaseSaveTicket( Ticket *ticket )
123{
124 delete ticket;
125}
126
127bool ResourceNet::doOpen()
128{
129 return true;
130}
131
132void ResourceNet::doClose()
133{
134}
135
136bool ResourceNet::load()
137{
138 TQString tempFile;
139
140 if ( !TDEIO::NetAccess::download( mUrl, tempFile, 0 ) ) {
141 addressBook()->error( i18n( "Unable to download file '%1'." ).arg( mUrl.prettyURL() ) );
142 return false;
143 }
144
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 );
149 return false;
150 }
151
152 bool result = clearAndLoad( &file );
153 if ( !result )
154 addressBook()->error( i18n( "Problems during parsing file '%1'." ).arg( tempFile ) );
155
156 TDEIO::NetAccess::removeTempFile( tempFile );
157
158 return result;
159}
160
161bool ResourceNet::clearAndLoad( TQFile *file )
162{
163 clear();
164 return mFormat->loadAll( addressBook(), this, file );
165}
166
167bool ResourceNet::asyncLoad()
168{
169 if ( d->mIsLoading ) {
170 abortAsyncLoading();
171 }
172
173 if (d->mIsSaving) {
174 kdWarning(5700) << "Aborted asyncLoad() because we're still asyncSave()ing!" << endl;
175 return false;
176 }
177
178 bool ok = createLocalTempFile();
179 if ( ok )
180 mTempFile->sync();
181 ok = mTempFile->close();
182
183 if ( !ok ) {
184 emit loadingError( this, i18n( "Unable to open file '%1'." ).arg( mTempFile->name() ) );
185 deleteLocalTempFile();
186 return false;
187 }
188
189 KURL dest;
190 dest.setPath( mTempFile->name() );
191
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* ) ) );
197
198 return true;
199}
200
201void ResourceNet::abortAsyncLoading()
202{
203 kdDebug(5700) << "ResourceNet::abortAsyncLoading()" << endl;
204
205 if ( d->mLoadJob ) {
206 d->mLoadJob->kill(); // result not emitted
207 d->mLoadJob = 0;
208 }
209
210 deleteLocalTempFile();
211 d->mIsLoading = false;
212}
213
214void ResourceNet::abortAsyncSaving()
215{
216 kdDebug(5700) << "ResourceNet::abortAsyncSaving()" << endl;
217
218 if ( d->mSaveJob ) {
219 d->mSaveJob->kill(); // result not emitted
220 d->mSaveJob = 0;
221 }
222
223 deleteLocalTempFile();
224 d->mIsSaving = false;
225}
226
227bool ResourceNet::save( Ticket* )
228{
229 kdDebug(5700) << "ResourceNet::save()" << endl;
230
231 if (d->mIsSaving) {
232 abortAsyncSaving();
233 }
234
235 KTempFile tempFile;
236 tempFile.setAutoDelete( true );
237 bool ok = false;
238
239 if ( tempFile.status() == 0 && tempFile.file() ) {
240 saveToFile( tempFile.file() );
241 tempFile.sync();
242 ok = tempFile.close();
243 }
244
245 if ( !ok ) {
246 addressBook()->error( i18n( "Unable to save file '%1'." ).arg( tempFile.name() ) );
247 return false;
248 }
249
250 ok = TDEIO::NetAccess::upload( tempFile.name(), mUrl, 0 );
251 if ( !ok )
252 addressBook()->error( i18n( "Unable to upload to '%1'." ).arg( mUrl.prettyURL() ) );
253
254 return ok;
255}
256
257bool ResourceNet::asyncSave( Ticket* )
258{
259 kdDebug(5700) << "ResourceNet::asyncSave()" << endl;
260
261 if (d->mIsSaving) {
262 abortAsyncSaving();
263 }
264
265 if (d->mIsLoading) {
266 kdWarning(5700) << "Aborted asyncSave() because we're still asyncLoad()ing!" << endl;
267 return false;
268 }
269
270 bool ok = createLocalTempFile();
271 if ( ok ) {
272 saveToFile( mTempFile->file() );
273 mTempFile->sync();
274 ok = mTempFile->close();
275 }
276
277 if ( !ok ) {
278 emit savingError( this, i18n( "Unable to save file '%1'." ).arg( mTempFile->name() ) );
279 deleteLocalTempFile();
280 return false;
281 }
282
283 KURL src;
284 src.setPath( mTempFile->name() );
285
286 TDEIO::Scheduler::checkSlaveOnHold( true );
287 d->mIsSaving = 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* ) ) );
291
292 return true;
293}
294
295bool ResourceNet::createLocalTempFile()
296{
297 deleteStaleTempFile();
298 mTempFile = new KTempFile();
299 mTempFile->setAutoDelete( true );
300 return mTempFile->status() == 0;
301}
302
303void ResourceNet::deleteStaleTempFile()
304{
305 if ( hasTempFile() ) {
306 kdDebug(5700) << "stale temp file detected " << mTempFile->name() << endl;
307 deleteLocalTempFile();
308 }
309}
310
311void ResourceNet::deleteLocalTempFile()
312{
313 delete mTempFile;
314 mTempFile = 0;
315}
316
317void ResourceNet::saveToFile( TQFile *file )
318{
319 mFormat->saveAll( addressBook(), this, file );
320}
321
322void ResourceNet::setUrl( const KURL &url )
323{
324 mUrl = url;
325}
326
327KURL ResourceNet::url() const
328{
329 return mUrl;
330}
331
332void ResourceNet::setFormat( const TQString &name )
333{
334 mFormatName = name;
335 if ( mFormat )
336 delete mFormat;
337
338 FormatFactory *factory = FormatFactory::self();
339 mFormat = factory->format( mFormatName );
340}
341
342TQString ResourceNet::format() const
343{
344 return mFormatName;
345}
346
347void ResourceNet::downloadFinished( TDEIO::Job* )
348{
349 kdDebug(5700) << "ResourceNet::downloadFinished()" << endl;
350
351 d->mIsLoading = false;
352
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() ) );
356 return;
357 }
358
359 TQFile file( mTempFile->name() );
360 if ( file.open( IO_ReadOnly ) ) {
361 if ( clearAndLoad( &file ) )
362 emit loadingFinished( this );
363 else
364 emit loadingError( this, i18n( "Problems during parsing file '%1'." ).arg( mTempFile->name() ) );
365 }
366 else {
367 emit loadingError( this, i18n( "Unable to open file '%1'." ).arg( mTempFile->name() ) );
368 }
369
370 deleteLocalTempFile();
371}
372
373void ResourceNet::uploadFinished( TDEIO::Job *job )
374{
375 kdDebug(5700) << "ResourceFile::uploadFinished()" << endl;
376
377 d->mIsSaving = false;
378
379 if ( job->error() )
380 emit savingError( this, job->errorString() );
381 else
382 emit savingFinished( this );
383
384 deleteLocalTempFile();
385}
386
387void ResourceNet::signalError()
388{
389 emit loadingError( this, d->mLastErrorString );
390 d->mLastErrorString.truncate( 0 );
391}
392
393#include "resourcenet.moc"
KTempFile
KTempFile::file
TQFile * file()
KTempFile::status
int status() const
KTempFile::setAutoDelete
void setAutoDelete(bool autoDelete)
KTempFile::close
bool close()
KTempFile::sync
bool sync()
KTempFile::name
TQString name() const
KURL
KURL::setPath
void setPath(const TQString &path)
TDEABC::FormatFactory
Class for loading format plugins.
Definition: formatfactory.h:58
TDEABC::FormatFactory::format
FormatPlugin * format(const TQString &type)
Returns a pointer to a format object or a null pointer if format type doesn't exist.
Definition: formatfactory.cpp:109
TDEABC::Ticket
Helper class for handling coordinated save of address books.
Definition: resource.h:38
TDEConfigBase::readEntry
TQString readEntry(const TQString &pKey, const TQString &aDefault=TQString::null) const
TDEConfigBase::writePathEntry
void writePathEntry(const TQString &pKey, const TQString &path, bool bPersistent=true, bool bGlobal=false, bool bNLS=false)
TDEConfigBase::writeEntry
void writeEntry(const TQString &pKey, const TQString &pValue, bool bPersistent=true, bool bGlobal=false, bool bNLS=false)
TDEConfigBase::readPathEntry
TQString readPathEntry(const TQString &pKey, const TQString &aDefault=TQString::null) const
TDEConfig
kdWarning
kdbgstream kdWarning(int area=0)
endl
kndbgstream & endl(kndbgstream &s)
kdDebug
kdbgstream kdDebug(int area=0)
KStdAction::clear
TDEAction * clear(const TQObject *recvr, const char *slot, TDEActionCollection *parent, const char *name=0)
TDEABC
static data, shared by ALL addressee objects
Definition: address.h:48
TDEStdAccel::name
TQString name(StdAccel id)
tdelocale.h

tdeabc

Skip menu "tdeabc"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdeabc

Skip menu "tdeabc"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeabc by doxygen 1.9.4
This website is maintained by Timothy Pearson.