21 #include "resourcedir.h" 22 #include "resourcedirconfig.h" 24 #include "kabc/addressbook.h" 25 #include "kabc/formatfactory.h" 26 #include "kabc/stdaddressbook.h" 27 #include "kabc/lock.h" 29 #include <kconfiggroup.h> 31 #include <kgenericfactory.h> 33 #include <klocalizedstring.h> 34 #include <kstandarddirs.h> 35 #include <kurlrequester.h> 37 #include <sys/types.h> 45 class ResourceDir::Private
49 : mParent( parent ), mFormat( 0 ), mAsynchronous( false )
60 void init(
const QString &
path,
const QString &
format );
74 void ResourceDir::Private::init(
const QString &path,
const QString &format )
79 mFormat = factory->
format( mFormatName );
82 mFormatName = QLatin1String(
"vcard" );
83 mFormat = factory->
format( mFormatName );
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()) );
92 mParent->setPath( path );
95 void ResourceDir::Private::pathChanged()
97 if ( !mParent->addressBook() ) {
102 if ( mAsynchronous ) {
103 mParent->asyncLoad();
106 mParent->addressBook()->emitAddressBookChanged();
110 ResourceDir::ResourceDir()
111 :
Resource(), d( new Private( this ) )
116 ResourceDir::ResourceDir(
const KConfigGroup &group )
117 :
Resource( group ), d( new Private( this ) )
120 group.readEntry(
"FileFormat",
"vcard" ) );
123 ResourceDir::ResourceDir(
const QString &path,
const QString &format )
124 :
Resource(), d( new Private( this ) )
126 d->init( path, format );
129 ResourceDir::~ResourceDir()
139 group.deleteEntry(
"FilePath" );
141 group.writePathEntry(
"FilePath", d->mPath );
144 group.writeEntry(
"FileFormat", d->mFormatName );
156 d->mLock =
new Lock( d->mPath );
158 if ( d->mLock->lock() ) {
162 kDebug() <<
"Unable to lock path '" << d->mPath
163 <<
"':" << d->mLock->error();
178 bool ResourceDir::doOpen()
180 QDir dir( d->mPath );
181 if ( !dir.exists() ) {
182 return dir.mkdir( dir.path() );
184 const QStringList lst = dir.entryList( QDir::Files );
185 if ( lst.isEmpty() ) {
188 QString testName = lst.first();
189 QFile file( d->mPath + QDir::separator() + testName );
190 if ( file.open( QIODevice::ReadOnly ) ) {
193 if ( file.size() == 0 ) {
197 bool ok = d->mFormat->checkFormat( &file );
203 void ResourceDir::doClose()
209 kDebug() << d->mPath <<
"'";
211 d->mAsynchronous =
false;
213 QDir dir( d->mPath );
214 QStringList files = dir.entryList( QDir::Files );
216 QStringList::Iterator it;
218 for ( it = files.begin(); it != files.end(); ++it ) {
219 QFile file( d->mPath + QDir::separator() + ( *it ) );
221 if ( !file.open( QIODevice::ReadOnly ) ) {
222 addressBook()->
error( i18n(
"Unable to open file '%1' for reading", file.fileName() ) );
227 if ( !d->mFormat->loadAll(
addressBook(),
this, &file ) ) {
239 d->mAsynchronous =
true;
253 kDebug() << d->mPath <<
"'";
255 Addressee::Map::Iterator it;
258 d->mDirWatch.stopScan();
261 if ( !it.value().changed() ) {
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() ) );
271 d->mFormat->save( *it, &file );
274 ( *it ).setChanged(
false );
279 d->mDirWatch.startScan();
286 bool ok =
save( ticket );
297 d->mDirWatch.stopScan();
298 if ( d->mDirWatch.contains( d->mPath ) ) {
299 d->mDirWatch.removeDir( d->mPath );
303 d->mDirWatch.addDir( d->mPath, KDirWatch::WatchFiles );
304 d->mDirWatch.startScan();
319 d->mFormat = factory->
format( d->mFormatName );
324 return d->mFormatName;
329 QFile::remove( d->mPath + QDir::separator() + addr.
uid() );
333 #include "moc_resourcedir.cpp"
void setPath(const QString &)
Set path to be used for saving.
This class provides locking functionality for a file, directory or an arbitrary string-represented re...
void error(const QString &msg)
Shows GUI independent error messages.
virtual bool save(Ticket *ticket)
Saves all addressees synchronously.
Helper class for handling coordinated save of address books.
QString uid() const
Return unique identifier.
void savingFinished(Resource *resource)
This signal is emitted when the resource has finished the saving of all addressees from the internal ...
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...
virtual void removeAddressee(const Addressee &addr)
Remove a addressee from its source.
static QString directoryName()
Returns the default directory name for vcard-based addressbook.
void loadingFinished(Resource *resource)
This signal is emitted when the resource has finished the loading of all addressees from the backend ...
QString format() const
Returns the format name.
virtual bool load()
Loads all addressees synchronously.
void savingError(Resource *resource, const QString &msg)
This signal is emitted when an error occurred during saving the addressees from the internal cache to...
Class that holds a Calendar Url (FBURL/CALADRURI/CALURI)
virtual QString resourceName() const
void setFormat(const QString &format)
Set the format by name.
Ticket * createTicket(Resource *)
Factory method, just creates and returns a new Ticket for the given resource.
void emitAddressBookLocked()
Emits the signal addressBookLocked() using this as the parameter.
virtual void releaseSaveTicket(Ticket *ticket)
Releases the ticket previousely requested with requestSaveTicket().
virtual void writeConfig(KConfigGroup &group)
Writes the resource specific config to file.
virtual bool asyncSave(Ticket *ticket)
Saves all addressees asynchronously.
virtual bool asyncLoad()
Loads all addressees asyncronously.
AddressBook * addressBook()
Returns a pointer to the addressbook.
Addressee::Map mAddrMap
A mapping from KABC UIDs to the respective addressee.
QString path() const
Return path used for loading and saving the address book.
virtual void writeConfig(KConfigGroup &group)
Writes the resource specific config to file.
virtual Ticket * requestSaveTicket()
Request a ticket, you have to pass through save() to allow locking.