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

kabc

  • sources
  • kde-4.12
  • kdepimlibs
  • 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 
39 using namespace KABC;
40 
41 class ResourceNet::ResourceNetPrivate
42 {
43  public:
44  KIO::Job *mLoadJob;
45  bool mIsLoading;
46 
47  KIO::Job *mSaveJob;
48  bool mIsSaving;
49 };
50 
51 ResourceNet::ResourceNet()
52  : Resource(), mFormat( 0 ),
53  mTempFile( 0 ),
54  d( new ResourceNetPrivate )
55 {
56  init( KUrl(), QLatin1String( "vcard" ) );
57 }
58 
59 ResourceNet::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 
67 ResourceNet::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 
75 void 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 
94 ResourceNet::~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 
112 void ResourceNet::writeConfig( KConfigGroup &group )
113 {
114  Resource::writeConfig( group );
115 
116  group.writePathEntry( "NetUrl", mUrl.url() );
117  group.writeEntry( "NetFormat", mFormatName );
118 }
119 
120 Ticket *ResourceNet::requestSaveTicket()
121 {
122  kDebug();
123 
124  return createTicket( this );
125 }
126 
127 void ResourceNet::releaseSaveTicket( Ticket *ticket )
128 {
129  delete ticket;
130 }
131 
132 bool ResourceNet::doOpen()
133 {
134  return true;
135 }
136 
137 void ResourceNet::doClose()
138 {
139 }
140 
141 bool 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 
167 bool ResourceNet::clearAndLoad( QFile *file )
168 {
169  clear();
170  return mFormat->loadAll( addressBook(), this, file );
171 }
172 
173 bool 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 
204 void 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 
217 void 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 
230 bool 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 
260 bool 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 
298 bool ResourceNet::createLocalTempFile()
299 {
300  deleteStaleTempFile();
301  mTempFile = new KTemporaryFile();
302  return mTempFile->open();
303 }
304 
305 void ResourceNet::deleteStaleTempFile()
306 {
307  if ( hasTempFile() ) {
308  kDebug() << "stale temp file detected" << mTempFile->fileName();
309  deleteLocalTempFile();
310  }
311 }
312 
313 void ResourceNet::deleteLocalTempFile()
314 {
315  delete mTempFile;
316  mTempFile = 0;
317 }
318 
319 void ResourceNet::saveToFile( QFile *file )
320 {
321  mFormat->saveAll( addressBook(), this, file );
322 }
323 
324 void ResourceNet::setUrl( const KUrl &url )
325 {
326  mUrl = url;
327 }
328 
329 KUrl ResourceNet::url() const
330 {
331  return mUrl;
332 }
333 
334 void 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 
343 QString ResourceNet::format() const
344 {
345  return mFormatName;
346 }
347 
348 void 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 
376 void 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::ResourceNet::save
virtual bool save(Ticket *ticket)
Saves all addressees synchronously.
Definition: resourcenet.cpp:230
KABC::FormatFactory
Class for loading format plugins.
Definition: formatfactory.h:94
KABC::FormatFactory::self
static FormatFactory * self()
Returns the global format factory.
Definition: formatfactory.cpp:68
KABC::ResourceNet::format
QString format() const
Returns the format name.
Definition: resourcenet.cpp:343
KABC::Resource::clear
virtual void clear()
Removes all addressees and distribution lists from the resource.
Definition: resource.cpp:354
KABC::ResourceNet::url
KUrl url() const
Return url of directory used for loading and saving the address book.
Definition: resourcenet.cpp:329
KABC::ResourceNet::asyncSave
virtual bool asyncSave(Ticket *ticket)
Saves all addressees asynchronously.
Definition: resourcenet.cpp:260
KABC::AddressBook::error
void error(const QString &msg)
Shows GUI independent error messages.
Definition: addressbook.cpp:901
KABC::ResourceNet::releaseSaveTicket
virtual void releaseSaveTicket(Ticket *ticket)
Releases the ticket previousely requested with requestSaveTicket().
Definition: resourcenet.cpp:127
KABC::ResourceNet::setFormat
void setFormat(const QString &name)
Sets a new format by name.
Definition: resourcenet.cpp:334
KABC::ResourceNet::load
virtual bool load()
Loads all addressees synchronously.
Definition: resourcenet.cpp:141
KABC::Ticket
Helper class for handling coordinated save of address books.
Definition: resource.h:37
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::Resource::savingFinished
void savingFinished(Resource *resource)
This signal is emitted when the resource has finished the saving of all addressees from the internal ...
KABC::ResourceNet::setUrl
void setUrl(const KUrl &url)
Set url of directory to be used for saving.
Definition: resourcenet.cpp:324
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::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::Resource::loadingFinished
void loadingFinished(Resource *resource)
This signal is emitted when the resource has finished the loading of all addressees from the backend ...
KABC::Resource
Definition: resource.h:64
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::ResourceNet::writeConfig
virtual void writeConfig(KConfigGroup &group)
Writes the resource specific config to file.
Definition: resourcenet.cpp:112
KABC::Resource::createTicket
Ticket * createTicket(Resource *)
Factory method, just creates and returns a new Ticket for the given resource.
Definition: resource.cpp:279
KABC::ResourceNet::asyncLoad
virtual bool asyncLoad()
Loads all addressees asyncronously.
Definition: resourcenet.cpp:173
KABC::Resource::addressBook
AddressBook * addressBook()
Returns a pointer to the addressbook.
Definition: resource.cpp:274
KABC::ResourceNet::requestSaveTicket
virtual Ticket * requestSaveTicket()
Request a ticket, you have to pass through save() to allow locking.
Definition: resourcenet.cpp:120
KABC::Resource::writeConfig
virtual void writeConfig(KConfigGroup &group)
Writes the resource specific config to file.
Definition: resource.cpp:264
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:01:05 by doxygen 1.8.7 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 API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

Search



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