• 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
  • dir
resourcedir.cpp
1 /*
2  This file is part of libkabc.
3  Copyright (c) 2002 - 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 "resourcedir.h"
22 #include "resourcedirconfig.h"
23 
24 #include "kabc/addressbook.h"
25 #include "kabc/formatfactory.h"
26 #include "kabc/stdaddressbook.h"
27 #include "kabc/lock.h"
28 
29 #include <kconfiggroup.h>
30 #include <kdebug.h>
31 #include <kgenericfactory.h>
32 #include <kglobal.h>
33 #include <klocalizedstring.h>
34 #include <kstandarddirs.h>
35 #include <kurlrequester.h>
36 
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <errno.h>
40 #include <signal.h>
41 #include <unistd.h>
42 
43 using namespace KABC;
44 
45 class ResourceDir::Private
46 {
47  public:
48  Private( ResourceDir *parent )
49  : mParent( parent ), mFormat( 0 ), mAsynchronous( false )
50  {
51  }
52 
53  ~Private()
54  {
55  delete mFormat;
56  mFormat = 0;
57  }
58 
59  void pathChanged();
60  void init( const QString &path, const QString &format );
61 
62  ResourceDir *mParent;
63  Format *mFormat;
64  KDirWatch mDirWatch;
65 
66  QString mPath;
67  QString mFormatName;
68 
69  Lock *mLock;
70 
71  bool mAsynchronous;
72 };
73 
74 void ResourceDir::Private::init( const QString &path, const QString &format )
75 {
76  mFormatName = format;
77 
78  FormatFactory *factory = FormatFactory::self();
79  mFormat = factory->format( mFormatName );
80 
81  if ( !mFormat ) {
82  mFormatName = QLatin1String( "vcard" );
83  mFormat = factory->format( mFormatName );
84  }
85 
86  mLock = 0;
87 
88  mParent->connect( &mDirWatch, SIGNAL(dirty(QString)), SLOT(pathChanged()) );
89  mParent->connect( &mDirWatch, SIGNAL(created(QString)), SLOT(pathChanged()) );
90  mParent->connect( &mDirWatch, SIGNAL(deleted(QString)), SLOT(pathChanged()) );
91 
92  mParent->setPath( path );
93 }
94 
95 void ResourceDir::Private::pathChanged()
96 {
97  if ( !mParent->addressBook() ) {
98  return;
99  }
100 
101  mParent->clear();
102  if ( mAsynchronous ) {
103  mParent->asyncLoad();
104  } else {
105  mParent->load();
106  mParent->addressBook()->emitAddressBookChanged();
107  }
108 }
109 
110 ResourceDir::ResourceDir()
111  : Resource(), d( new Private( this ) )
112 {
113  d->init( StdAddressBook::directoryName(), QLatin1String( "vcard" ) );
114 }
115 
116 ResourceDir::ResourceDir( const KConfigGroup &group )
117  : Resource( group ), d( new Private( this ) )
118 {
119  d->init( group.readPathEntry( "FilePath", StdAddressBook::directoryName() ),
120  group.readEntry( "FileFormat", "vcard" ) );
121 }
122 
123 ResourceDir::ResourceDir( const QString &path, const QString &format )
124  : Resource(), d( new Private( this ) )
125 {
126  d->init( path, format );
127 }
128 
129 ResourceDir::~ResourceDir()
130 {
131  delete d;
132 }
133 
134 void ResourceDir::writeConfig( KConfigGroup &group )
135 {
136  Resource::writeConfig( group );
137 
138  if ( d->mPath == StdAddressBook::directoryName() ) {
139  group.deleteEntry( "FilePath" );
140  } else {
141  group.writePathEntry( "FilePath", d->mPath );
142  }
143 
144  group.writeEntry( "FileFormat", d->mFormatName );
145 }
146 
147 Ticket *ResourceDir::requestSaveTicket()
148 {
149  kDebug();
150 
151  if ( !addressBook() ) {
152  return 0;
153  }
154 
155  delete d->mLock;
156  d->mLock = new Lock( d->mPath );
157 
158  if ( d->mLock->lock() ) {
159  addressBook()->emitAddressBookLocked();
160  } else {
161  addressBook()->error( d->mLock->error() );
162  kDebug() << "Unable to lock path '" << d->mPath
163  << "':" << d->mLock->error();
164  return 0;
165  }
166 
167  return createTicket( this );
168 }
169 
170 void ResourceDir::releaseSaveTicket( Ticket *ticket )
171 {
172  delete ticket;
173 
174  delete d->mLock;
175  d->mLock = 0;
176 }
177 
178 bool ResourceDir::doOpen()
179 {
180  QDir dir( d->mPath );
181  if ( !dir.exists() ) { // no directory available
182  return dir.mkdir( dir.path() );
183  } else {
184  const QStringList lst = dir.entryList( QDir::Files );
185  if ( lst.isEmpty() ) { //path doesn't exist or list of file empty
186  return true;
187  }
188  QString testName = lst.first();
189  QFile file( d->mPath + QDir::separator() + testName );
190  if ( file.open( QIODevice::ReadOnly ) ) {
191  return true;
192  }
193  if ( file.size() == 0 ) {
194  return true;
195  }
196 
197  bool ok = d->mFormat->checkFormat( &file );
198  file.close();
199  return ok;
200  }
201 }
202 
203 void ResourceDir::doClose()
204 {
205 }
206 
207 bool ResourceDir::load()
208 {
209  kDebug() << d->mPath << "'";
210 
211  d->mAsynchronous = false;
212 
213  QDir dir( d->mPath );
214  QStringList files = dir.entryList( QDir::Files );
215 
216  QStringList::Iterator it;
217  bool ok = true;
218  for ( it = files.begin(); it != files.end(); ++it ) {
219  QFile file( d->mPath + QDir::separator() + ( *it ) );
220 
221  if ( !file.open( QIODevice::ReadOnly ) ) {
222  addressBook()->error( i18n( "Unable to open file '%1' for reading", file.fileName() ) );
223  ok = false;
224  continue;
225  }
226 
227  if ( !d->mFormat->loadAll( addressBook(), this, &file ) ) {
228  ok = false;
229  }
230 
231  file.close();
232  }
233 
234  return ok;
235 }
236 
237 bool ResourceDir::asyncLoad()
238 {
239  d->mAsynchronous = true;
240 
241  bool ok = load();
242  if ( !ok ) {
243  emit loadingError( this, i18n( "Loading resource '%1' failed!", resourceName() ) );
244  } else {
245  emit loadingFinished( this );
246  }
247 
248  return ok;
249 }
250 
251 bool ResourceDir::save( Ticket * )
252 {
253  kDebug() << d->mPath << "'";
254 
255  Addressee::Map::Iterator it;
256  bool ok = true;
257 
258  d->mDirWatch.stopScan();
259 
260  for ( it = mAddrMap.begin(); it != mAddrMap.end(); ++it ) {
261  if ( !it.value().changed() ) {
262  continue;
263  }
264 
265  QFile file( d->mPath + QDir::separator() + ( *it ).uid() );
266  if ( !file.open( QIODevice::WriteOnly ) ) {
267  addressBook()->error( i18n( "Unable to open file '%1' for writing", file.fileName() ) );
268  continue;
269  }
270 
271  d->mFormat->save( *it, &file );
272 
273  // mark as unchanged
274  ( *it ).setChanged( false );
275 
276  file.close();
277  }
278 
279  d->mDirWatch.startScan();
280 
281  return ok;
282 }
283 
284 bool ResourceDir::asyncSave( Ticket *ticket )
285 {
286  bool ok = save( ticket );
287  if ( !ok ) {
288  emit savingError( this, i18n( "Saving resource '%1' failed!", resourceName() ) );
289  } else {
290  emit savingFinished( this );
291  }
292  return ok;
293 }
294 
295 void ResourceDir::setPath( const QString &path )
296 {
297  d->mDirWatch.stopScan();
298  if ( d->mDirWatch.contains( d->mPath ) ) {
299  d->mDirWatch.removeDir( d->mPath );
300  }
301 
302  d->mPath = path;
303  d->mDirWatch.addDir( d->mPath, KDirWatch::WatchFiles );
304  d->mDirWatch.startScan();
305 }
306 
307 QString ResourceDir::path() const
308 {
309  return d->mPath;
310 }
311 
312 void ResourceDir::setFormat( const QString &format )
313 {
314  d->mFormatName = format;
315 
316  delete d->mFormat;
317 
318  FormatFactory *factory = FormatFactory::self();
319  d->mFormat = factory->format( d->mFormatName );
320 }
321 
322 QString ResourceDir::format() const
323 {
324  return d->mFormatName;
325 }
326 
327 void ResourceDir::removeAddressee( const Addressee &addr )
328 {
329  QFile::remove( d->mPath + QDir::separator() + addr.uid() );
330  mAddrMap.remove( addr.uid() );
331 }
332 
333 #include "moc_resourcedir.cpp"
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::ResourceDir::setPath
void setPath(const QString &)
Set path to be used for saving.
Definition: resourcedir.cpp:295
KABC::Lock
This class provides locking functionality for a file, directory or an arbitrary string-represented re...
Definition: lock.h:34
KABC::AddressBook::error
void error(const QString &msg)
Shows GUI independent error messages.
Definition: addressbook.cpp:901
KABC::ResourceDir::save
virtual bool save(Ticket *ticket)
Saves all addressees synchronously.
Definition: resourcedir.cpp:251
KABC::ResourceDir::format
QString format() const
Returns the format name.
Definition: resourcedir.cpp:322
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::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::ResourceDir::removeAddressee
virtual void removeAddressee(const Addressee &addr)
Remove a addressee from its source.
Definition: resourcedir.cpp:327
KABC::StdAddressBook::directoryName
static QString directoryName()
Returns the default directory name for vcard-based addressbook.
Definition: stdaddressbook.cpp:69
KABC::Addressee
address book entry
Definition: addressee.h:74
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::ResourceDir
Definition: resourcedir.h:37
KABC::ResourceDir::load
virtual bool load()
Loads all addressees synchronously.
Definition: resourcedir.cpp:207
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...
KRES::Resource::resourceName
virtual QString resourceName() const
KABC::Addressee::uid
QString uid() const
Return unique identifier.
Definition: addressee.cpp:377
KABC::ResourceDir::setFormat
void setFormat(const QString &format)
Set the format by name.
Definition: resourcedir.cpp:312
KABC::Format::load
virtual bool load(Addressee &, QFile *file)=0
Load single addressee from file.
KABC::Resource::createTicket
Ticket * createTicket(Resource *)
Factory method, just creates and returns a new Ticket for the given resource.
Definition: resource.cpp:279
KABC::AddressBook::emitAddressBookLocked
void emitAddressBookLocked()
Emits the signal addressBookLocked() using this as the parameter.
Definition: addressbook.h:589
KABC::ResourceDir::releaseSaveTicket
virtual void releaseSaveTicket(Ticket *ticket)
Releases the ticket previousely requested with requestSaveTicket().
Definition: resourcedir.cpp:170
KABC::ResourceDir::writeConfig
virtual void writeConfig(KConfigGroup &group)
Writes the resource specific config to file.
Definition: resourcedir.cpp:134
KABC::ResourceDir::path
QString path() const
Return path used for loading and saving the address book.
Definition: resourcedir.cpp:307
KABC::ResourceDir::asyncSave
virtual bool asyncSave(Ticket *ticket)
Saves all addressees asynchronously.
Definition: resourcedir.cpp:284
KABC::ResourceDir::asyncLoad
virtual bool asyncLoad()
Loads all addressees asyncronously.
Definition: resourcedir.cpp:237
KABC::Resource::addressBook
AddressBook * addressBook()
Returns a pointer to the addressbook.
Definition: resource.cpp:274
KABC::Resource::mAddrMap
Addressee::Map mAddrMap
A mapping from KABC UIDs to the respective addressee.
Definition: resource.h:527
KABC::Format
Base class for address book formats.
Definition: format.h:42
KABC::Resource::writeConfig
virtual void writeConfig(KConfigGroup &group)
Writes the resource specific config to file.
Definition: resource.cpp:264
KABC::ResourceDir::requestSaveTicket
virtual Ticket * requestSaveTicket()
Request a ticket, you have to pass through save() to allow locking.
Definition: resourcedir.cpp:147
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