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

kabc

  • 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
43using namespace KABC;
44
45class 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
74void 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
95void 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
110ResourceDir::ResourceDir()
111 : Resource(), d( new Private( this ) )
112{
113 d->init( StdAddressBook::directoryName(), QLatin1String( "vcard" ) );
114}
115
116ResourceDir::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
123ResourceDir::ResourceDir( const QString &path, const QString &format )
124 : Resource(), d( new Private( this ) )
125{
126 d->init( path, format );
127}
128
129ResourceDir::~ResourceDir()
130{
131 delete d;
132}
133
134void 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
147Ticket *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
170void ResourceDir::releaseSaveTicket( Ticket *ticket )
171{
172 delete ticket;
173
174 delete d->mLock;
175 d->mLock = 0;
176}
177
178bool 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
203void ResourceDir::doClose()
204{
205}
206
207bool 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
237bool 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
251bool 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
284bool 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
295void 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
307QString ResourceDir::path() const
308{
309 return d->mPath;
310}
311
312void 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
322QString ResourceDir::format() const
323{
324 return d->mFormatName;
325}
326
327void 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::AddressBook::error
void error(const QString &msg)
Shows GUI independent error messages.
Definition addressbook.cpp:901
KABC::AddressBook::emitAddressBookLocked
void emitAddressBookLocked()
Emits the signal addressBookLocked() using this as the parameter.
Definition addressbook.h:589
KABC::Addressee
address book entry
Definition addressee.h:79
KABC::Addressee::uid
QString uid() const
Return unique identifier.
Definition addressee.cpp:442
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
Base class for address book formats.
Definition format.h:43
KABC::Format::save
virtual void save(const Addressee &, QFile *file)=0
Save a single Addressee to file.
KABC::Format::loadAll
virtual bool loadAll(AddressBook *, Resource *, QFile *file)=0
Load whole addressbook from file.
KABC::Format::checkFormat
virtual bool checkFormat(QFile *file) const =0
Checks if given file contains the right format.
KABC::Lock
This class provides locking functionality for a file, directory or an arbitrary string-represented re...
Definition lock.h:35
KABC::Lock::error
virtual QString error() const
Returns the lastest error message.
Definition lock.cpp:179
KABC::Lock::lock
virtual bool lock()
Lock resource.
Definition lock.cpp:108
KABC::ResourceDir
Definition resourcedir.h:38
KABC::ResourceDir::load
virtual bool load()
Loads all addressees synchronously.
Definition resourcedir.cpp:207
KABC::ResourceDir::writeConfig
virtual void writeConfig(KConfigGroup &group)
Writes the resource specific config to file.
Definition resourcedir.cpp:134
KABC::ResourceDir::asyncSave
virtual bool asyncSave(Ticket *ticket)
Saves all addressees asynchronously.
Definition resourcedir.cpp:284
KABC::ResourceDir::removeAddressee
virtual void removeAddressee(const Addressee &addr)
Remove a addressee from its source.
Definition resourcedir.cpp:327
KABC::ResourceDir::setFormat
void setFormat(const QString &format)
Set the format by name.
Definition resourcedir.cpp:312
KABC::ResourceDir::releaseSaveTicket
virtual void releaseSaveTicket(Ticket *ticket)
Releases the ticket previousely requested with requestSaveTicket().
Definition resourcedir.cpp:170
KABC::ResourceDir::format
QString format() const
Returns the format name.
Definition resourcedir.cpp:322
KABC::ResourceDir::save
virtual bool save(Ticket *ticket)
Saves all addressees synchronously.
Definition resourcedir.cpp:251
KABC::ResourceDir::path
QString path() const
Return path used for loading and saving the address book.
Definition resourcedir.cpp:307
KABC::ResourceDir::requestSaveTicket
virtual Ticket * requestSaveTicket()
Request a ticket, you have to pass through save() to allow locking.
Definition resourcedir.cpp:147
KABC::ResourceDir::asyncLoad
virtual bool asyncLoad()
Loads all addressees asyncronously.
Definition resourcedir.cpp:237
KABC::ResourceDir::setPath
void setPath(const QString &)
Set path to be used for saving.
Definition resourcedir.cpp:295
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::mAddrMap
Addressee::Map mAddrMap
A mapping from KABC UIDs to the respective addressee.
Definition resource.h:527
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::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::StdAddressBook::directoryName
static QString directoryName()
Returns the default directory name for vcard-based addressbook.
Definition stdaddressbook.cpp:69
KABC::Ticket
Helper class for handling coordinated save of address books.
Definition resource.h:38
KRES::Resource::resourceName
virtual QString resourceName() const
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-2024 The KDE developers.
Generated on Wed Jan 24 2024 00:00:00 by doxygen 1.10.0 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