• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.14.10 API Reference
  • KDE Home
  • Contact Us
 

kabc

  • kabc
  • plugins
  • net
resourcenet.cpp
1/*
2 This file is part of libkabc.
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 "resourcenet.h"
22#include "resourcenetconfig.h"
23
24#include "kabc/addressbook.h"
25#include "kabc/formatfactory.h"
26#include "kabc/stdaddressbook.h"
27
28#include <kio/netaccess.h>
29#include <kio/scheduler.h>
30#include <kdebug.h>
31#include <klocalizedstring.h>
32#include <ksavefile.h>
33#include <ktemporaryfile.h>
34#include <kurlrequester.h>
35#include <kconfiggroup.h>
36
37#include <QtCore/QFile>
38
39using namespace KABC;
40
41class ResourceNet::ResourceNetPrivate
42{
43 public:
44 KIO::Job *mLoadJob;
45 bool mIsLoading;
46
47 KIO::Job *mSaveJob;
48 bool mIsSaving;
49};
50
51ResourceNet::ResourceNet()
52 : Resource(), mFormat( 0 ),
53 mTempFile( 0 ),
54 d( new ResourceNetPrivate )
55{
56 init( KUrl(), QLatin1String( "vcard" ) );
57}
58
59ResourceNet::ResourceNet( const KConfigGroup &group )
60 : Resource( group ), mFormat( 0 ),
61 mTempFile( 0 ),
62 d( new ResourceNetPrivate )
63{
64 init( KUrl( group.readPathEntry( "NetUrl", QString() ) ), group.readEntry( "NetFormat" ) );
65}
66
67ResourceNet::ResourceNet( const KUrl &url, const QString &format )
68 : Resource(), mFormat( 0 ),
69 mTempFile( 0 ),
70 d( new ResourceNetPrivate )
71{
72 init( url, format );
73}
74
75void ResourceNet::init( const KUrl &url, const QString &format )
76{
77 d->mLoadJob = 0;
78 d->mIsLoading = false;
79 d->mSaveJob = 0;
80 d->mIsSaving = false;
81
82 mFormatName = format;
83
84 FormatFactory *factory = FormatFactory::self();
85 mFormat = factory->format( mFormatName );
86 if ( !mFormat ) {
87 mFormatName = QLatin1String( "vcard" );
88 mFormat = factory->format( mFormatName );
89 }
90
91 setUrl( url );
92}
93
94ResourceNet::~ResourceNet()
95{
96 if ( d->mIsLoading ) {
97 d->mLoadJob->kill();
98 }
99 if ( d->mIsSaving ) {
100 d->mSaveJob->kill();
101 }
102
103 delete d;
104 d = 0;
105
106 delete mFormat;
107 mFormat = 0;
108
109 deleteLocalTempFile();
110}
111
112void ResourceNet::writeConfig( KConfigGroup &group )
113{
114 Resource::writeConfig( group );
115
116 group.writePathEntry( "NetUrl", mUrl.url() );
117 group.writeEntry( "NetFormat", mFormatName );
118}
119
120Ticket *ResourceNet::requestSaveTicket()
121{
122 kDebug();
123
124 return createTicket( this );
125}
126
127void ResourceNet::releaseSaveTicket( Ticket *ticket )
128{
129 delete ticket;
130}
131
132bool ResourceNet::doOpen()
133{
134 return true;
135}
136
137void ResourceNet::doClose()
138{
139}
140
141bool ResourceNet::load()
142{
143 QString tempFile;
144
145 if ( !KIO::NetAccess::download( mUrl, tempFile, 0 ) ) {
146 addressBook()->error( i18n( "Unable to download file '%1'.", mUrl.prettyUrl() ) );
147 return false;
148 }
149
150 QFile file( tempFile );
151 if ( !file.open( QIODevice::ReadOnly ) ) {
152 addressBook()->error( i18n( "Unable to open file '%1'.", tempFile ) );
153 KIO::NetAccess::removeTempFile( tempFile );
154 return false;
155 }
156
157 bool result = clearAndLoad( &file );
158 if ( !result ) {
159 addressBook()->error( i18n( "Problems parsing file '%1'.", tempFile ) );
160 }
161
162 KIO::NetAccess::removeTempFile( tempFile );
163
164 return result;
165}
166
167bool ResourceNet::clearAndLoad( QFile *file )
168{
169 clear();
170 return mFormat->loadAll( addressBook(), this, file );
171}
172
173bool ResourceNet::asyncLoad()
174{
175 if ( d->mIsLoading ) {
176 abortAsyncLoading();
177 }
178
179 if ( d->mIsSaving ) {
180 kWarning() << "Aborted asyncLoad() because we're still saving!";
181 return false;
182 }
183
184 bool ok = createLocalTempFile();
185
186 if ( !ok ) {
187 emit loadingError( this, i18n( "Unable to open file '%1'.", mTempFile->fileName() ) );
188 deleteLocalTempFile();
189 return false;
190 }
191
192 KUrl dest;
193 dest.setPath( mTempFile->fileName() );
194
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*)) );
200
201 return true;
202}
203
204void ResourceNet::abortAsyncLoading()
205{
206 kDebug();
207
208 if ( d->mLoadJob ) {
209 d->mLoadJob->kill(); // result not emitted
210 d->mLoadJob = 0;
211 }
212
213 deleteLocalTempFile();
214 d->mIsLoading = false;
215}
216
217void ResourceNet::abortAsyncSaving()
218{
219 kDebug();
220
221 if ( d->mSaveJob ) {
222 d->mSaveJob->kill(); // result not emitted
223 d->mSaveJob = 0;
224 }
225
226 deleteLocalTempFile();
227 d->mIsSaving = false;
228}
229
230bool ResourceNet::save( Ticket *ticket )
231{
232 Q_UNUSED( ticket );
233 kDebug();
234
235 if ( d->mIsSaving ) {
236 abortAsyncSaving();
237 }
238
239 KTemporaryFile tempFile;
240 bool ok = tempFile.open();
241
242 if ( ok ) {
243 saveToFile( &tempFile );
244 tempFile.flush();
245 }
246
247 if ( !ok ) {
248 addressBook()->error( i18n( "Unable to save file '%1'.", tempFile.fileName() ) );
249 return false;
250 }
251
252 ok = KIO::NetAccess::upload( tempFile.fileName(), mUrl, 0 );
253 if ( !ok ) {
254 addressBook()->error( i18n( "Unable to upload to '%1'.", mUrl.prettyUrl() ) );
255 }
256
257 return ok;
258}
259
260bool ResourceNet::asyncSave( Ticket *ticket )
261{
262 Q_UNUSED( ticket );
263 kDebug();
264
265 if ( d->mIsSaving ) {
266 abortAsyncSaving();
267 }
268
269 if ( d->mIsLoading ) {
270 kWarning() << "Aborted asyncSave() because we're still loading!";
271 return false;
272 }
273
274 bool ok = createLocalTempFile();
275 if ( ok ) {
276 saveToFile( mTempFile );
277 mTempFile->flush();
278 }
279
280 if ( !ok ) {
281 emit savingError( this, i18n( "Unable to save file '%1'.", mTempFile->fileName() ) );
282 deleteLocalTempFile();
283 return false;
284 }
285
286 KUrl src;
287 src.setPath( mTempFile->fileName() );
288
289 KIO::Scheduler::checkSlaveOnHold( true );
290 d->mIsSaving = 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*)) );
294
295 return true;
296}
297
298bool ResourceNet::createLocalTempFile()
299{
300 deleteStaleTempFile();
301 mTempFile = new KTemporaryFile();
302 return mTempFile->open();
303}
304
305void ResourceNet::deleteStaleTempFile()
306{
307 if ( hasTempFile() ) {
308 kDebug() << "stale temp file detected" << mTempFile->fileName();
309 deleteLocalTempFile();
310 }
311}
312
313void ResourceNet::deleteLocalTempFile()
314{
315 delete mTempFile;
316 mTempFile = 0;
317}
318
319void ResourceNet::saveToFile( QFile *file )
320{
321 mFormat->saveAll( addressBook(), this, file );
322}
323
324void ResourceNet::setUrl( const KUrl &url )
325{
326 mUrl = url;
327}
328
329KUrl ResourceNet::url() const
330{
331 return mUrl;
332}
333
334void ResourceNet::setFormat( const QString &name )
335{
336 mFormatName = name;
337 delete mFormat;
338
339 FormatFactory *factory = FormatFactory::self();
340 mFormat = factory->format( mFormatName );
341}
342
343QString ResourceNet::format() const
344{
345 return mFormatName;
346}
347
348void ResourceNet::downloadFinished( KJob *job )
349{
350 Q_UNUSED( job );
351 kDebug();
352
353 d->mIsLoading = false;
354
355 if ( !hasTempFile() ) {
356 emit loadingError( this, i18n( "Download failed, could not create temporary file" ) );
357 return;
358 }
359
360 QFile file( mTempFile->fileName() );
361 if ( file.open( QIODevice::ReadOnly ) ) {
362 if ( clearAndLoad( &file ) ) {
363 emit loadingFinished( this );
364 } else {
365 emit loadingError( this, i18n( "Problems during parsing file '%1'.",
366 mTempFile->fileName() ) );
367 }
368 } else {
369 emit loadingError( this, i18n( "Unable to open file '%1'.",
370 mTempFile->fileName() ) );
371 }
372
373 deleteLocalTempFile();
374}
375
376void ResourceNet::uploadFinished( KJob *job )
377{
378 kDebug();
379
380 d->mIsSaving = false;
381
382 if ( job->error() ) {
383 emit savingError( this, job->errorString() );
384 } else {
385 emit savingFinished( this );
386 }
387
388 deleteLocalTempFile();
389}
390
KABC::AddressBook::error
void error(const QString &msg)
Shows GUI independent error messages.
Definition: addressbook.cpp:901
KABC::FormatFactory
Class for loading format plugins.
Definition: formatfactory.h:95
KABC::FormatFactory::format
Format * format(const QString &type)
Returns a pointer to a format object or a null pointer if format type doesn't exist.
Definition: formatfactory.cpp:145
KABC::FormatFactory::self
static FormatFactory * self()
Returns the global format factory.
Definition: formatfactory.cpp:68
KABC::Format::saveAll
virtual void saveAll(AddressBook *, Resource *, QFile *file)=0
Save whole addressbook to file.
KABC::Format::loadAll
virtual bool loadAll(AddressBook *, Resource *, QFile *file)=0
Load whole addressbook from file.
KABC::ResourceNet::requestSaveTicket
virtual Ticket * requestSaveTicket()
Request a ticket, you have to pass through save() to allow locking.
Definition: resourcenet.cpp:120
KABC::ResourceNet::setFormat
void setFormat(const QString &name)
Sets a new format by name.
Definition: resourcenet.cpp:334
KABC::ResourceNet::releaseSaveTicket
virtual void releaseSaveTicket(Ticket *ticket)
Releases the ticket previousely requested with requestSaveTicket().
Definition: resourcenet.cpp:127
KABC::ResourceNet::save
virtual bool save(Ticket *ticket)
Saves all addressees synchronously.
Definition: resourcenet.cpp:230
KABC::ResourceNet::url
KUrl url() const
Return url of directory used for loading and saving the address book.
Definition: resourcenet.cpp:329
KABC::ResourceNet::format
QString format() const
Returns the format name.
Definition: resourcenet.cpp:343
KABC::ResourceNet::writeConfig
virtual void writeConfig(KConfigGroup &group)
Writes the resource specific config to file.
Definition: resourcenet.cpp:112
KABC::ResourceNet::asyncLoad
virtual bool asyncLoad()
Loads all addressees asyncronously.
Definition: resourcenet.cpp:173
KABC::ResourceNet::setUrl
void setUrl(const KUrl &url)
Set url of directory to be used for saving.
Definition: resourcenet.cpp:324
KABC::ResourceNet::asyncSave
virtual bool asyncSave(Ticket *ticket)
Saves all addressees asynchronously.
Definition: resourcenet.cpp:260
KABC::ResourceNet::load
virtual bool load()
Loads all addressees synchronously.
Definition: resourcenet.cpp:141
KABC::Resource
Definition: resource.h:65
KABC::Resource::createTicket
Ticket * createTicket(Resource *)
Factory method, just creates and returns a new Ticket for the given resource.
Definition: resource.cpp:279
KABC::Resource::savingFinished
void savingFinished(Resource *resource)
This signal is emitted when the resource has finished the saving of all addressees from the internal ...
KABC::Resource::savingError
void savingError(Resource *resource, const QString &msg)
This signal is emitted when an error occurred during saving the addressees from the internal cache to...
KABC::Resource::addressBook
AddressBook * addressBook()
Returns a pointer to the addressbook.
Definition: resource.cpp:274
KABC::Resource::clear
virtual void clear()
Removes all addressees and distribution lists from the resource.
Definition: resource.cpp:354
KABC::Resource::writeConfig
virtual void writeConfig(KConfigGroup &group)
Writes the resource specific config to file.
Definition: resource.cpp:264
KABC::Resource::loadingError
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...
KABC::Resource::loadingFinished
void loadingFinished(Resource *resource)
This signal is emitted when the resource has finished the loading of all addressees from the backend ...
KABC::Ticket
Helper class for handling coordinated save of address books.
Definition: resource.h:38
KABC
Class that holds a Calendar Url (FBURL/CALADRURI/CALURI)
Definition: address.h:29
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu Jul 21 2022 00:00:00 by doxygen 1.9.5 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kabc

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

kdepimlibs-4.14.10 API Reference

Skip menu "kdepimlibs-4.14.10 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal