From b9aad1f15dc600e4dbe4c62d3fcced6363188ba3 Mon Sep 17 00:00:00 2001 From: zautrix Date: Sat, 26 Jun 2004 19:01:18 +0000 Subject: Initial revision --- (limited to 'kabc/plugins') diff --git a/kabc/plugins/dir/resourcedir.cpp b/kabc/plugins/dir/resourcedir.cpp new file mode 100644 index 0000000..f354a9e --- a/dev/null +++ b/kabc/plugins/dir/resourcedir.cpp @@ -0,0 +1,363 @@ +/* + This file is part of libkabc. + Copyright (c) 2002 Tobias Koenig + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +/* +Enhanced Version of the file for platform independent KDE tools. +Copyright (c) 2004 Ulf Schenk + +$Id$ +*/ + + +#include +#include +#ifndef _WIN32_ +#include +#endif + +#include +#include +#include + +#include +#include +#include +//US #include +#include +#include +#include +#include + +#include "addressbook.h" + +//US #include "formatfactory.h" + +#include "resourcedirconfig.h" +#include "stdaddressbook.h" + +//US +#include +#include +#include + +#include "resourcedir.h" + +using namespace KABC; + +extern "C" +{ + void *init_kabc_dir() + { + qDebug("resourcedir.cpp : init_kabc_dir has to be changed"); +//US return new KRES::PluginFactory(); + return 0; + } +} + + +ResourceDir::ResourceDir( const KConfig *config ) + : Resource( config ) +{ + QString path; + + KConfig *cfg = (KConfig *)config; + if ( cfg ) { +//US path = config->readEntry( "FilePath" ); + path = cfg->readEntry( "FilePath", StdAddressBook::directoryName() ); +//US mFormatName = config->readEntry( "FileFormat" ); + mFormatName = cfg->readEntry( "FileFormat", "vcard" ); + } else { + path = StdAddressBook::directoryName(); + mFormatName = "vcard"; + } + + +/*US FormatFactory *factory = FormatFactory::self(); + mFormat = factory->format( mFormatName ); + + if ( !mFormat ) { + mFormatName = "vcard"; + mFormat = factory->format( mFormatName ); + } +*/ + +//US qDebug("ResourceDir::ResourceDir initialized with format %s ", mFormatName.latin1()); + if (mFormatName == "vcard") + mFormat = new VCardFormatPlugin2(); + else if (mFormatName == "binary") + mFormat = new BinaryFormat(); + else + qDebug("ResourceFile::init format unknown !!! %s ", mFormatName.latin1()); + + +/*US we have no KDirWatch. SO simulate the signals from inside the apropriate methods + connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( pathChanged() ) ); + connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( pathChanged() ) ); + connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( pathChanged() ) ); +*/ + + setPath( path ); +} + +ResourceDir::~ResourceDir() +{ + delete mFormat; + mFormat = 0; +} + +void ResourceDir::writeConfig( KConfig *config ) +{ + Resource::writeConfig( config ); + + config->writeEntry( "FilePath", mPath ); + config->writeEntry( "FileFormat", mFormatName ); +} + +Ticket *ResourceDir::requestSaveTicket() +{ + kdDebug(5700) << "ResourceDir::requestSaveTicket()" << endl; + + if ( !addressBook() ) return 0; + + if ( !lock( mPath ) ) { + kdDebug(5700) << "ResourceDir::requestSaveTicket(): Unable to lock path '" + << mPath << "'" << endl; + return 0; + } + return createTicket( this ); +} + + +bool ResourceDir::doOpen() +{ + QDir dir( mPath ); + if ( !dir.exists() ) { // no directory available + return dir.mkdir( dir.path() ); + } else { + QString testName = dir.entryList( QDir::Files )[0]; + if ( testName.isNull() || testName.isEmpty() ) // no file in directory + return true; + + QFile file( mPath + "/" + testName ); + if ( file.open( IO_ReadOnly ) ) + return true; + + if ( file.size() == 0 ) + return true; + + bool ok = mFormat->checkFormat( &file ); + file.close(); + return ok; + } +} + +void ResourceDir::doClose() +{ +} + +bool ResourceDir::load() +{ + kdDebug(5700) << "ResourceDir::load(): '" << mPath << "'" << endl; + + QDir dir( mPath ); + QStringList files = dir.entryList( QDir::Files ); + + QStringList::Iterator it; + bool ok = true; + for ( it = files.begin(); it != files.end(); ++it ) { + QFile file( mPath + "/" + (*it) ); + + if ( !file.open( IO_ReadOnly ) ) { + addressBook()->error( i18n( "Unable to open file '%1' for reading" ).arg( file.name() ) ); + ok = false; + continue; + } + + if ( !mFormat->loadAll( addressBook(), this, &file ) ) + ok = false; + + file.close(); + } + + return ok; +} + +bool ResourceDir::save( Ticket *ticket ) +{ + kdDebug(5700) << "ResourceDir::save(): '" << mPath << "'" << endl; + + AddressBook::Iterator it; + bool ok = true; + + for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) { + if ( (*it).resource() != this || !(*it).changed() ) + continue; + + QFile file( mPath + "/" + (*it).uid() ); + if ( !file.open( IO_WriteOnly ) ) { + addressBook()->error( i18n( "Unable to open file '%1' for writing" ).arg( file.name() ) ); + continue; + } + + mFormat->save( *it, &file ); + + // mark as unchanged + (*it).setChanged( false ); + + file.close(); + } + + delete ticket; + unlock( mPath ); + + return ok; +} + +bool ResourceDir::lock( const QString &path ) +{ + kdDebug(5700) << "ResourceDir::lock()" << endl; + + QString p = path; +//US change the implementation how the lockfilename is getting created +//US p.replace( QRegExp("/"), "_" ); +//US QString lockName = locateLocal( "data", "kabc/lock/" + p + ".lock" ); + KURL url(p); + QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); + + + kdDebug(5700) << "-- lock name: " << lockName << endl; + + if ( QFile::exists( lockName ) ) return false; + + QString lockUniqueName; + lockUniqueName = p + KApplication::randomString( 8 ); + + url = lockUniqueName; +//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); + mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); + + kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; + + // Create unique file + QFile file( mLockUniqueName ); + file.open( IO_WriteOnly ); + file.close(); + + // Create lock file +#ifdef _WIN32_ + int result = 0; + qDebug("WARNING: ResourceDir::lock cannot link "); +#else + int result = ::link( QFile::encodeName( mLockUniqueName ), + QFile::encodeName( lockName ) ); +#endif + if ( result == 0 ) { + addressBook()->emitAddressBookLocked(); + return true; + } + + // TODO: check stat + + return false; +} + +void ResourceDir::unlock( const QString &path ) +{ + QString p = path; +//US change the implementation how the lockfilename is getting created +//US p.replace( QRegExp( "/" ), "_" ); +//US QString lockName = locate( "data", "kabc/lock/" + p + ".lock" ); + KURL url(p); + QString lockName = locate( "data", "kabc/lock/" + url.fileName() + ".lock" ); + + ::unlink( QFile::encodeName( lockName ) ); + QFile::remove( mLockUniqueName ); + addressBook()->emitAddressBookUnlocked(); +} + +void ResourceDir::setPath( const QString &path ) +{ +/*US ToDo: no synchronization so far. Has to be changed in the future + mDirWatch.stopScan(); + mDirWatch.removeDir( mPath ); +*/ + mPath = path; + +/*US ToDo: no synchronization so far. Has to be changed in the future + mDirWatch.addDir( mPath, true ); + mDirWatch.startScan(); +*/ + +//US simulate KDirWatch event + pathChanged(); + +} + +QString ResourceDir::path() const +{ + return mPath; +} + +void ResourceDir::setFormat( const QString &format ) +{ + mFormatName = format; + + if ( mFormat ) + delete mFormat; + +//US FormatFactory *factory = FormatFactory::self(); +//US mFormat = factory->format( mFormatName ); + +qDebug("ResourceDir::setFormat initialized with format %s ", format.latin1()); + if (mFormatName == "vcard") + mFormat = new VCardFormatPlugin2(); + else if (mFormatName == "binary") + mFormat = new BinaryFormat(); + else + qDebug("ResourceDir::setFormat format unknown !!! %s ", format.latin1()); + + +} + +QString ResourceDir::format() const +{ + return mFormatName; +} + +void ResourceDir::pathChanged() +{ + if ( !addressBook() ) + return; + + load(); + addressBook()->emitAddressBookChanged(); +} + +void ResourceDir::removeAddressee( const Addressee& addr ) +{ + QFile::remove( mPath + "/" + addr.uid() ); +} + +void ResourceDir::cleanUp() +{ + unlock( mPath ); +} + +//US #include "resourcedir.moc" diff --git a/kabc/plugins/dir/resourcedir.h b/kabc/plugins/dir/resourcedir.h new file mode 100644 index 0000000..6e35695 --- a/dev/null +++ b/kabc/plugins/dir/resourcedir.h @@ -0,0 +1,115 @@ +/* + This file is part of libkabc. + Copyright (c) 2002 Tobias Koenig + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +/* +Enhanced Version of the file for platform independent KDE tools. +Copyright (c) 2004 Ulf Schenk + +$Id$ +*/ + +#ifndef KABC_RESOURCEDIR_H +#define KABC_RESOURCEDIR_H + +#include +#include + +#include + +#include "resource.h" + +class QTimer; + +namespace KABC { + +class FormatPlugin; + +/** + @internal +*/ +class ResourceDir : public Resource +{ + Q_OBJECT + +public: + ResourceDir( const KConfig* ); + ~ResourceDir(); + + virtual void writeConfig( KConfig* ); + + virtual bool doOpen(); + virtual void doClose(); + + virtual Ticket *requestSaveTicket(); + + virtual bool load(); + virtual bool save( Ticket * ); + + /** + * Set path to be used for saving. + */ + void setPath( const QString & ); + + /** + * Return path used for loading and saving the address book. + */ + QString path() const; + + /** + * Set the format by name. + */ + void setFormat( const QString &format ); + + /** + * Returns the format name. + */ + QString format() const; + + /** + * Remove a addressee from its source. + * This method is mainly called by KABC::AddressBook. + */ + virtual void removeAddressee( const Addressee& addr ); + + /** + * This method is called by an error handler if the application + * crashed + */ + virtual void cleanUp(); + +protected slots: + void pathChanged(); + +protected: + bool lock( const QString &path ); + void unlock( const QString &path ); + +private: + FormatPlugin *mFormat; + + KDirWatch mDirWatch; + + QString mPath; + QString mFormatName; + QString mLockUniqueName; +}; + +} +#endif diff --git a/kabc/plugins/dir/resourcedirconfig.cpp b/kabc/plugins/dir/resourcedirconfig.cpp new file mode 100644 index 0000000..98d18fe --- a/dev/null +++ b/kabc/plugins/dir/resourcedirconfig.cpp @@ -0,0 +1,123 @@ +/* + This file is part of libkabc. + Copyright (c) 2002 Tobias Koenig + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +/* +Enhanced Version of the file for platform independent KDE tools. +Copyright (c) 2004 Ulf Schenk + +$Id$ +*/ + +#include +#include + +#include +#include +#include +#include + +//US #include "formatfactory.h" +#include "resourcedir.h" +#include "stdaddressbook.h" + +#include "resourcedirconfig.h" + +using namespace KABC; + +ResourceDirConfig::ResourceDirConfig( QWidget* parent, const char* name ) + : KRES::ConfigWidget( parent, name ) +{ + QGridLayout *mainLayout = new QGridLayout( this, 2, 2, 0, + KDialog::spacingHint() ); + + QLabel *label = new QLabel( i18n( "Format:" ), this ); + mFormatBox = new KComboBox( this ); + + mainLayout->addWidget( label, 0, 0 ); + mainLayout->addWidget( mFormatBox, 0, 1 ); + + label = new QLabel( i18n( "Location:" ), this ); + mFileNameEdit = new KURLRequester( this ); +//US mFileNameEdit->setMode( KFile::Directory ); + + mainLayout->addWidget( label, 1, 0 ); + mainLayout->addWidget( mFileNameEdit, 1, 1 ); + +/*US lets hardcode the formats instead of using a factory + FormatFactory *factory = FormatFactory::self(); + QStringList formats = factory->formats(); + QStringList::Iterator it; + for ( it = formats.begin(); it != formats.end(); ++it ) { + FormatInfo *info = factory->info( *it ); + if ( info ) { + mFormatTypes << (*it); + mFormatBox->insertItem( info->nameLabel ); + } + } +*/ + mFormatTypes << "vcard"; + mFormatTypes << "binary"; + mFormatBox->insertItem( "vcard" ); + mFormatBox->insertItem( "binary" ); + + + mInEditMode = false; +} + +void ResourceDirConfig::setEditMode( bool value ) +{ + mFormatBox->setEnabled( !value ); + mInEditMode = value; +} + +void ResourceDirConfig::loadSettings( KRES::Resource *res ) +{ +//US ResourceDir *resource = dynamic_cast( res ); + ResourceDir *resource = (ResourceDir*)( res ); + + if ( !resource ) { + kdDebug(5700) << "ResourceDirConfig::loadSettings(): cast failed" << endl; + return; + } + + mFormatBox->setCurrentItem( mFormatTypes.findIndex( resource->format() ) ); + + mFileNameEdit->setURL( resource->path() ); + if ( mFileNameEdit->url().isEmpty() ) + mFileNameEdit->setURL( KABC::StdAddressBook::directoryName() ); +} + +void ResourceDirConfig::saveSettings( KRES::Resource *res ) +{ +//US ResourceDir *resource = dynamic_cast( res ); + ResourceDir *resource = (ResourceDir*)( res ); + + if ( !resource ) { + kdDebug(5700) << "ResourceDirConfig::loadSettings(): cast failed" << endl; + return; + } + + if ( mInEditMode ) + resource->setFormat( mFormatTypes[ mFormatBox->currentItem() ] ); + + resource->setPath( mFileNameEdit->url() ); +} + +//US #include "resourcedirconfig.moc" diff --git a/kabc/plugins/dir/resourcedirconfig.h b/kabc/plugins/dir/resourcedirconfig.h new file mode 100644 index 0000000..5af38a5 --- a/dev/null +++ b/kabc/plugins/dir/resourcedirconfig.h @@ -0,0 +1,61 @@ +/* + This file is part of libkabc. + Copyright (c) 2002 Tobias Koenig + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +/* +Enhanced Version of the file for platform independent KDE tools. +Copyright (c) 2004 Ulf Schenk + +$Id$ +*/ + +#ifndef RESOURCEDIRCONFIG_H +#define RESOURCEDIRCONFIG_H + +#include +#include + +#include + +namespace KABC { + +class ResourceDirConfig : public KRES::ConfigWidget +{ + Q_OBJECT + +public: + ResourceDirConfig( QWidget* parent = 0, const char* name = 0 ); + + void setEditMode( bool value ); + +public slots: + void loadSettings( KRES::Resource* ); + void saveSettings( KRES::Resource* ); + +private: + KComboBox* mFormatBox; + KURLRequester* mFileNameEdit; + + QStringList mFormatTypes; + + bool mInEditMode; +}; + +} +#endif diff --git a/kabc/plugins/file/resourcefile.cpp b/kabc/plugins/file/resourcefile.cpp new file mode 100644 index 0000000..d30ed2f --- a/dev/null +++ b/kabc/plugins/file/resourcefile.cpp @@ -0,0 +1,389 @@ +/* + This file is part of libkabc. + Copyright (c) 2001 Cornelius Schumacher + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +/* +Enhanced Version of the file for platform independent KDE tools. +Copyright (c) 2004 Ulf Schenk + +$Id$ +*/ + +#include +#include +#ifndef _WIN32_ +#include +#endif + +#include +#include +#include +#include + +#include +#include +#include +#include +//US #include +#include + +//US #include "formatfactory.h" + +#include "resource.h" +#include "resourcefileconfig.h" +#include "stdaddressbook.h" + +#include +#include + + +#include "resourcefile.h" + +using namespace KABC; + +extern "C" +{ + void *init_kabc_file() + { + qDebug("!!!resourcefile.cpp : init_kabc_file has to be changed"); +//US return new KRES::PluginFactory(); + return 0; + } +} + + +ResourceFile::ResourceFile( const KConfig *config ) + : Resource( config ) , mFormat( 0 ) +{ + QString fileName, formatName; + + KConfig *cfg = (KConfig *)config; + if ( cfg ) { + fileName = cfg->readEntry( "FileName", StdAddressBook::fileName() ); + formatName = cfg->readEntry( "FileFormat", "vcard" ); +// qDebug("ResourceFile::ResourceFile : 1 %s, %s", fileName.latin1(), formatName.latin1() ); + } else { + fileName = StdAddressBook::fileName(); + formatName = "vcard"; +// qDebug("ResourceFile::ResourceFile : 2 %s, %s", fileName.latin1(), formatName.latin1() ); + } + + init( fileName, formatName ); +} + +ResourceFile::ResourceFile( const QString &fileName, + const QString &formatName ) + : Resource( 0 ) +{ +// qDebug("ResourceFile::ResourceFile : 3 %s, %s", fileName.latin1(), formatName.latin1()); + init( fileName, formatName ); +} + +void ResourceFile::init( const QString &fileName, const QString &formatName ) +{ + mFormatName = formatName; + +/*US FormatFactory *factory = FormatFactory::self(); + mFormat = factory->format( mFormatName ); + + if ( !mFormat ) { + mFormatName = "vcard"; + mFormat = factory->format( mFormatName ); + } +*/ + +//US qDebug("ResourceFile::init initialized with format %s ", formatName.latin1()); + if (mFormatName == "vcard") { + mFormat = new VCardFormatPlugin2(); +// qDebug("ResourceFile::init format VCardFormatPlugin2"); + } + else if (mFormatName == "binary") { + mFormat = new BinaryFormat(); +// qDebug("ResourceFile::init format BinaryFormat"); + } + else + qDebug("ResourceFile::init format unknown !!! %s ", formatName.latin1()); + +/*US we have no KDirWatch. SO simulate the signals from inside the apropriate methods + connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) ); + connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) ); + connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) ); +*/ + + setFileName( fileName ); +} + +ResourceFile::~ResourceFile() +{ + delete mFormat; + mFormat = 0; +} + +void ResourceFile::writeConfig( KConfig *config ) +{ + Resource::writeConfig( config ); + + config->writeEntry( "FileName", mFileName ); + config->writeEntry( "FileFormat", mFormatName ); + +// qDebug("ResourceFile::writeConfig format %s, %s", mFileName.latin1(), mFormatName.latin1()); + +} + +Ticket *ResourceFile::requestSaveTicket() +{ + kdDebug(5700) << "ResourceFile::requestSaveTicket()" << endl; + + if ( !addressBook() ) return 0; + + if ( !lock( mFileName ) ) { + kdDebug(5700) << "ResourceFile::requestSaveTicket(): Unable to lock file '" + << mFileName << "'" << endl; + return 0; + } + return createTicket( this ); +} + + +bool ResourceFile::doOpen() +{ + QFile file( mFileName ); + + if ( !file.exists() ) { + // try to create the file + bool ok = file.open( IO_WriteOnly ); + if ( ok ) + file.close(); + + return ok; + } else { + if ( !file.open( IO_ReadWrite ) ) + return false; + + if ( file.size() == 0 ) { + file.close(); + return true; + } + + bool ok = mFormat->checkFormat( &file ); + file.close(); + + return ok; + } +} + +void ResourceFile::doClose() +{ +} + +bool ResourceFile::load() +{ + kdDebug(5700) << "ResourceFile::load(): '" << mFileName << "'" << endl; + + QFile file( mFileName ); + if ( !file.open( IO_ReadOnly ) ) { + addressBook()->error( i18n( "Unable to open file '%1'." ).arg( mFileName ) ); + return false; + } + +// qDebug("ResourceFile::load format %s, %s", mFileName.latin1(), mFormatName.latin1()); + + return mFormat->loadAll( addressBook(), this, &file ); +} + +bool ResourceFile::save( Ticket *ticket ) +{ +// qDebug("ResourceFile::save format %s, %s", mFileName.latin1(), mFormatName.latin1()); + kdDebug(5700) << "ResourceFile::save()" << endl; + + // create backup file + QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() ); + +/*US we use a simpler method to create a backupfile + + (void) KSaveFile::backupFile( mFileName, QString::null /*directory*/ +/*US ,extension ); + + KSaveFile saveFile( mFileName ); + bool ok = false; + if ( saveFile.status() == 0 && saveFile.file() ) + { + mFormat->saveAll( addressBook(), this, saveFile.file() ); + ok = saveFile.close(); + } +*/ + +//US ToDo: write backupfile + QFile info; + info.setName( mFileName ); + bool ok = info.open( IO_WriteOnly ); + if ( ok ) { + mFormat->saveAll( addressBook(), this, &info ); + + info.close(); + ok = true; + } + else { + + } + + if ( !ok ) + addressBook()->error( i18n( "Unable to save file '%1'." ).arg( mFileName ) ); + + delete ticket; + unlock( mFileName ); + + return ok; + + qDebug("ResourceFile::save has to be changed"); + return true; +} + +bool ResourceFile::lock( const QString &fileName ) +{ + kdDebug(5700) << "ResourceFile::lock()" << endl; + + QString fn = fileName; + +//US change the implementation how the lockfilename is getting created +//US fn.replace( QRegExp("/"), "_" ); +//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); + + KURL url(fn); + QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); + + kdDebug(5700) << "-- lock name: " << lockName << endl; + + if (QFile::exists( lockName )) return false; + + QString lockUniqueName; + lockUniqueName = fn + KApplication::randomString( 8 ); + + url = lockUniqueName; +//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); + mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); + kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; + + // Create unique file + QFile file( mLockUniqueName ); + file.open( IO_WriteOnly ); + file.close(); + + // Create lock file + int result = 0;//::link( QFile::encodeName( mLockUniqueName ), + // QFile::encodeName( lockName ) ); + qDebug("lock files %s, %s needs to be fixed", mLockUniqueName.latin1(), lockName.latin1() ); + + if ( result == 0 ) { + addressBook()->emitAddressBookLocked(); + return true; + } + + // TODO: check stat + + return false; +} + +void ResourceFile::unlock( const QString &fileName ) +{ + QString fn = fileName; +//US change the implementation how the lockfilename is getting created +//US fn.replace( QRegExp( "/" ), "_" ); +//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); +//US QString lockName = fn + ".lock"; + KURL url(fn); + QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); + + QFile::remove( lockName ); + QFile::remove( mLockUniqueName ); + addressBook()->emitAddressBookUnlocked(); +} + +void ResourceFile::setFileName( const QString &fileName ) +{ +/*US ToDo: no synchronization so far. Has to be changed in the future + mDirWatch.stopScan(); + mDirWatch.removeFile( mFileName ); +*/ + mFileName = fileName; + + +/*US ToDo: no synchronization so far. Has to be changed in the future + mDirWatch.addFile( mFileName ); + mDirWatch.startScan(); +*/ +//US simulate KDirWatch event + fileChanged(); +} + +QString ResourceFile::fileName() const +{ + return mFileName; +} + +void ResourceFile::setFormat( const QString &format ) +{ + mFormatName = format; + delete mFormat; + +//US FormatFactory *factory = FormatFactory::self(); +//US mFormat = factory->format( mFormatName ); + +//qDebug("ResourceFile::setFormat initialized with format %s ", format.latin1()); + if (mFormatName == "vcard") { + mFormat = new VCardFormatPlugin2(); +// qDebug("ResourceFile::setFormat format %s", mFormatName.latin1()); + } + else if (mFormatName == "binary") { + mFormat = new BinaryFormat(); +// qDebug("ResourceFile::setFormat format %s", mFormatName.latin1()); + } + else + qDebug("ResourceFile::setFormat format unknown !!! %s ", format.latin1()); + +} + +QString ResourceFile::format() const +{ + return mFormatName; +} + +void ResourceFile::fileChanged() +{ + // There is a small theoretical chance that KDirWatch calls us before + // we are fully constructed + if (!addressBook()) + return; + load(); + addressBook()->emitAddressBookChanged(); +} + +void ResourceFile::removeAddressee( const Addressee &addr ) +{ + QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/photos/" ) + addr.uid() ) ); + QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/logos/" ) + addr.uid() ) ); + QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/sounds/" ) + addr.uid() ) ); +} + +void ResourceFile::cleanUp() +{ + unlock( mFileName ); +} + +//US #include "resourcefile.moc" diff --git a/kabc/plugins/file/resourcefile.h b/kabc/plugins/file/resourcefile.h new file mode 100644 index 0000000..4522d78 --- a/dev/null +++ b/kabc/plugins/file/resourcefile.h @@ -0,0 +1,162 @@ +/* + This file is part of libkabc. + Copyright (c) 2001 Cornelius Schumacher + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +/* +Enhanced Version of the file for platform independent KDE tools. +Copyright (c) 2004 Ulf Schenk + +$Id$ +*/ + + +#ifndef KABC_RESOURCEFILE_H +#define KABC_RESOURCEFILE_H + +#include +#include + +#include + +#include + +class QTimer; +class FormatPlugin; + +namespace KABC { + +//US class FormatPlugin; +class ResourceConfigWidget; + +/** + @internal +*/ +class ResourceFile : public Resource +{ + Q_OBJECT + +public: + + /** + Constructor. + + @param cfg The config object where custom resource settings are stored. + */ + ResourceFile( const KConfig *cfg ); + + /** + Construct file resource on file @arg fileName using format @arg formatName. + */ + ResourceFile( const QString &fileName, const QString &formatName = "vcard" ); + + /** + * Destructor. + */ + ~ResourceFile(); + + /** + Writes the config back. + */ + virtual void writeConfig( KConfig *cfg ); + + /** + * Tries to open the file and checks for the proper format. + * This method should be called before @ref load(). + */ + virtual bool doOpen(); + + /** + * Closes the file again. + */ + virtual void doClose(); + + /** + * Requests a save ticket, that is used by @ref save() + */ + virtual Ticket *requestSaveTicket(); + + /** + * Loads all addressees from file to the address book. + * Returns true if all addressees could be loaded otherwise false. + */ + virtual bool load(); + + /** + * Saves all addresses from address book to file. + * Returns true if all addressees could be saved otherwise false. + * + * @param ticket The ticket returned by @ref requestSaveTicket() + */ + virtual bool save( Ticket *ticket ); + + /** + * Set name of file to be used for saving. + */ + void setFileName( const QString & ); + + /** + * Return name of file used for loading and saving the address book. + */ + QString fileName() const; + + /** + Sets a new format by name. + */ + void setFormat( const QString &name ); + + /** + Returns the format name. + */ + QString format() const; + + /** + * Remove a addressee from its source. + * This method is mainly called by KABC::AddressBook. + */ + virtual void removeAddressee( const Addressee& addr ); + + /** + * This method is called by an error handler if the application + * crashed + */ + virtual void cleanUp(); + +protected slots: + void fileChanged(); + +protected: + void init( const QString &fileName, const QString &format ); + + bool lock( const QString &fileName ); + void unlock( const QString &fileName ); + +private: + QString mFileName; + QString mFormatName; + + FormatPlugin *mFormat; + + QString mLockUniqueName; + + KDirWatch mDirWatch; +}; + +} + +#endif diff --git a/kabc/plugins/file/resourcefileconfig.cpp b/kabc/plugins/file/resourcefileconfig.cpp new file mode 100644 index 0000000..b63775d --- a/dev/null +++ b/kabc/plugins/file/resourcefileconfig.cpp @@ -0,0 +1,144 @@ +/* + This file is part of libkabc. + Copyright (c) 2002 Tobias Koenig + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +/* +Enhanced Version of the file for platform independent KDE tools. +Copyright (c) 2004 Ulf Schenk + +$Id$ +*/ + +#include +#include +#include + +#include +#include +#include +#include +#ifndef _WIN32_ +#include +#endif +//US #include "formatfactory.h" +#include +#include "resourcefile.h" +#include "stdaddressbook.h" + +#include "resourcefileconfig.h" + +using namespace KABC; + +ResourceFileConfig::ResourceFileConfig( QWidget* parent, const char* name ) + : ConfigWidget( parent, name ) +{ +//qDebug("ResourceFileConfig::ResourceFileConfig"); + + QGridLayout *mainLayout = new QGridLayout( this, 2, 2, 0, + KDialog::spacingHint() ); + + QLabel *label = new QLabel( i18n( "Format:" ), this ); + mFormatBox = new KComboBox( this ); + + mainLayout->addWidget( label, 0, 0 ); + mainLayout->addWidget( mFormatBox, 0, 1 ); + + label = new QLabel( i18n( "Location:" ), this ); + mFileNameEdit = new KURLRequester( this ); + + connect( mFileNameEdit, SIGNAL( textChanged( const QString & ) ), + SLOT( checkFilePermissions( const QString & ) ) ); + + mainLayout->addWidget( label, 1, 0 ); + mainLayout->addWidget( mFileNameEdit, 1, 1 ); + + +/*US lets hardcode the formats instead of using a factory + FormatFactory *factory = FormatFactory::self(); + QStringList formats = factory->formats(); + QStringList::Iterator it; + + for ( it = formats.begin(); it != formats.end(); ++it ) { + FormatInfo *info = factory->info( *it ); + if ( info ) { + mFormatTypes << (*it); + mFormatBox->insertItem( info->nameLabel ); + } + } +*/ + mFormatTypes << "vcard"; + mFormatTypes << "binary"; + mFormatBox->insertItem( "vcard" ); + mFormatBox->insertItem( "binary" ); + + mInEditMode = false; +} + +void ResourceFileConfig::setEditMode( bool value ) +{ + mFormatBox->setEnabled( !value ); + mInEditMode = value; +} + +void ResourceFileConfig::loadSettings( KRES::Resource *res ) +{ +//US ResourceFile *resource = dynamic_cast( res ); + ResourceFile *resource = (ResourceFile*)( res ); + + if ( !resource ) { + kdDebug(5700) << "ResourceFileConfig::loadSettings(): cast failed" << endl; + return; + } + + mFormatBox->setCurrentItem( mFormatTypes.findIndex( resource->format() ) ); + + mFileNameEdit->setURL( resource->fileName() ); + if ( mFileNameEdit->url().isEmpty() ) + mFileNameEdit->setURL( KABC::StdAddressBook::fileName() ); +} + +void ResourceFileConfig::saveSettings( KRES::Resource *res ) +{ +//US ResourceFile *resource = dynamic_cast( res ); + ResourceFile *resource = (ResourceFile*)( res ); + + if ( !resource ) { + kdDebug(5700) << "ResourceFileConfig::saveSettings(): cast failed" << endl; + return; + } + + if ( !mInEditMode ) + resource->setFormat( mFormatTypes[ mFormatBox->currentItem() ] ); + + resource->setFileName( mFileNameEdit->url() ); +} +void ResourceFileConfig::checkFilePermissions( const QString& fileName ) +{ + // If file exist but is not writeable... +#ifdef _WIN32_ + QFileInfo fi ( QFile::encodeName( fileName ) ); + if ( fi.exists() ) + emit setReadOnly(!fi.isReadable() ); +#else + if ( access( QFile::encodeName( fileName ), F_OK ) == 0 ) + emit setReadOnly( access( QFile::encodeName( fileName ), W_OK ) < 0 ); +#endif +} + +//US #include "resourcefileconfig.moc" diff --git a/kabc/plugins/file/resourcefileconfig.h b/kabc/plugins/file/resourcefileconfig.h new file mode 100644 index 0000000..31ccaaf --- a/dev/null +++ b/kabc/plugins/file/resourcefileconfig.h @@ -0,0 +1,65 @@ +/* + This file is part of libkabc. + Copyright (c) 2002 Tobias Koenig + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + + +/* +Enhanced Version of the file for platform independent KDE tools. +Copyright (c) 2004 Ulf Schenk + +$Id$ +*/ + +#ifndef RESOURCEFILECONFIG_H +#define RESOURCEFILECONFIG_H + +#include +#include + +#include + +namespace KABC { + +class ResourceFileConfig : public KRES::ConfigWidget +{ + Q_OBJECT + +public: + ResourceFileConfig( QWidget* parent = 0, const char* name = 0 ); + + void setEditMode( bool value ); + +public slots: + void loadSettings( KRES::Resource *resource ); + void saveSettings( KRES::Resource *resource ); + +protected slots: + void checkFilePermissions( const QString& fileName ); + +private: + KComboBox* mFormatBox; + KURLRequester* mFileNameEdit; + bool mInEditMode; + + QStringList mFormatTypes; +}; + +} + +#endif diff --git a/kabc/plugins/ldap/resourceldap.cpp b/kabc/plugins/ldap/resourceldap.cpp new file mode 100644 index 0000000..1c54f63 --- a/dev/null +++ b/kabc/plugins/ldap/resourceldap.cpp @@ -0,0 +1,444 @@ +/* + This file is part of libkabc. + Copyright (c) 2002 Tobias Koenig + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +/* +Enhanced Version of the file for platform independent KDE tools. +Copyright (c) 2004 Ulf Schenk + +$Id$ +*/ + +#include +#include +#include +#include +#include +#include + +#include + +#include "resourceldap.h" +#include "resourceldapconfig.h" + +using namespace KABC; + +extern "C" +{ + void *init_kabc_ldap() + { + qDebug("resourceldap.cpp : init_kabc_ldap has to be changed"); +//US return new KRES::PluginFactory(); + } +} + +void addModOp( LDAPMod ***pmods, const QString &attr, const QString &value ); + + +ResourceLDAP::ResourceLDAP( const KConfig *config ) + : Resource( config ), mPort( 389 ), mLdap( 0 ) +{ + KConfig *cfg = (KConfig *)config; + if ( cfg ) { + mUser = cfg->readEntry( "LdapUser" ); + mPassword = KStringHandler::obscure( cfg->readEntry( "LdapPassword" ) ); + mDn = cfg->readEntry( "LdapDn" ); + mHost = cfg->readEntry( "LdapHost" ); + mPort = cfg->readNumEntry( "LdapPort", 389 ); + mFilter = cfg->readEntry( "LdapFilter" ); + mAnonymous = cfg->readBoolEntry( "LdapAnonymous" ); + + QStringList attributes = cfg->readListEntry( "LdapAttributes" ); + for ( uint pos = 0; pos < attributes.count(); pos += 2 ) + mAttributes.insert( attributes[ pos ], attributes[ pos + 1 ] ); + } + + /** + If you want to add new attributes, append them here, add a + translation string in the ctor of AttributesDialog and + handle them in the load() method below. + These are the default values from + */ + if ( mAttributes.count() == 0 ) { + mAttributes.insert( "commonName", "cn" ); + mAttributes.insert( "formattedName", "displayName" ); + mAttributes.insert( "familyName", "sn" ); + mAttributes.insert( "givenName", "givenName" ); + mAttributes.insert( "mail", "mail" ); + mAttributes.insert( "mailAlias", "" ); + mAttributes.insert( "phoneNumber", "telephoneNumber" ); + mAttributes.insert( "uid", "uid" ); + } +} + +void ResourceLDAP::writeConfig( KConfig *config ) +{ + Resource::writeConfig( config ); + + config->writeEntry( "LdapUser", mUser ); + config->writeEntry( "LdapPassword", KStringHandler::obscure( mPassword ) ); + config->writeEntry( "LdapDn", mDn ); + config->writeEntry( "LdapHost", mHost ); + config->writeEntry( "LdapPort", mPort ); + config->writeEntry( "LdapFilter", mFilter ); + config->writeEntry( "LdapAnonymous", mAnonymous ); + + QStringList attributes; + QMap::Iterator it; + for ( it = mAttributes.begin(); it != mAttributes.end(); ++it ) + attributes << it.key() << it.data(); + + config->writeEntry( "LdapAttributes", attributes ); +} + +Ticket *ResourceLDAP::requestSaveTicket() +{ + if ( !addressBook() ) { + kdDebug(5700) << "no addressbook" << endl; + return 0; + } + + return createTicket( this ); +} + +bool ResourceLDAP::doOpen() +{ + if ( mLdap ) + return false; + + if ( !mPort ) + mPort = 389; + + mLdap = ldap_init( mHost.local8Bit(), mPort ); + if ( !mLdap ) { + addressBook()->error( i18n( "Unable to connect to server '%1' on port '%2'" ).arg( mHost ).arg( mPort ) ); + return false; + } + + if ( !mUser.isEmpty() && !mAnonymous ) { + if ( ldap_simple_bind_s( mLdap, mUser.local8Bit(), mPassword.local8Bit() ) != LDAP_SUCCESS ) { + addressBook()->error( i18n( "Unable to bind to server '%1'" ).arg( mHost ) ); + return false; + } + + kdDebug(5700) << "ResourceLDAP: bind to server successfully" << endl; + } else { + if ( ldap_simple_bind_s( mLdap, NULL, NULL ) != LDAP_SUCCESS ) { + addressBook()->error( i18n( "Unable to bind anonymously to server '%1'" ).arg( mHost ) ); + return false; + } + + kdDebug( 5700 ) << "ResourceLDAP: bind anonymously to server successfully" << endl; + } + + int deref = LDAP_DEREF_ALWAYS; + if ( ldap_set_option( mLdap, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS ) { + kdDebug(5700) << "ResourceLDAP: can't set 'deref' option" << endl; + return false; + } + + if ( ldap_set_option( mLdap, LDAP_OPT_REFERRALS, LDAP_OPT_ON ) != LDAP_OPT_SUCCESS ) { + kdDebug(5700) << "ResourceLDAP: can't set 'referrals' option" << endl; + return false; + } + + return true; +} + +void ResourceLDAP::doClose() +{ + if ( ldap_unbind_s( mLdap ) != LDAP_SUCCESS ) { + kdDebug(5700) << "ResourceLDAP: can't unbind from server" << endl; + return; + } + + mLdap = 0; +} + +bool ResourceLDAP::load() +{ + LDAPMessage *res; + LDAPMessage *msg; + BerElement *track; + char *names; + char **values; + + char **LdapSearchAttr = new char*[ mAttributes.count() + 1 ]; + + QMap::Iterator it; + int i = 0; + for ( it = mAttributes.begin(); it != mAttributes.end(); ++it ) { + if ( !it.data().isEmpty() ) { + unsigned int len = it.data().utf8().length(); + LdapSearchAttr[ i ] = new char[ len+1 ]; + memcpy( LdapSearchAttr[ i ], it.data().utf8(), len ); + LdapSearchAttr[ i ][ len ] = 0; + ++i; + } + } + LdapSearchAttr[ i ] = 0; + + QString filter = mFilter; + if ( filter.isEmpty() ) + filter = "cn=*"; + + int result; + if ( ( result = ldap_search_s( mLdap, mDn.local8Bit(), LDAP_SCOPE_SUBTREE, QString( "(%1)" ).arg( filter ).local8Bit(), + LdapSearchAttr, 0, &res ) != LDAP_SUCCESS ) ) { + addressBook()->error( i18n( "Unable to search on server '%1': %2" ) + .arg( mHost ) + .arg( ldap_err2string( result ) ) ); + + for ( i = 0; LdapSearchAttr[ i ]; ++i ) + delete [] LdapSearchAttr[ i ]; + delete [] LdapSearchAttr; + + return false; + } + + for ( msg = ldap_first_entry( mLdap, res ); msg; msg = ldap_next_entry( mLdap, msg ) ) { + Addressee addr; + addr.setResource( this ); + for ( names = ldap_first_attribute( mLdap, msg, &track ); names; names = ldap_next_attribute( mLdap, msg, track ) ) { + values = ldap_get_values( mLdap, msg, names ); + for ( int i = 0; i < ldap_count_values( values ); ++i ) { + QString name = QString::fromUtf8( names ).lower(); + QString value = QString::fromUtf8( values[ i ] ); + + if ( name == mAttributes[ "commonName" ].lower() ) { + if ( !addr.formattedName().isEmpty() ) { + QString fn = addr.formattedName(); + addr.setNameFromString( value ); + addr.setFormattedName( fn ); + } else + addr.setNameFromString( value ); + } else if ( name == mAttributes[ "formattedName" ].lower() ) { + addr.setFormattedName( value ); + } else if ( name == mAttributes[ "givenName" ].lower() ) { + addr.setGivenName( value ); + } else if ( name == mAttributes[ "mail" ].lower() ) { + addr.insertEmail( value, true ); + } else if ( name == mAttributes[ "mailAlias" ].lower() ) { + addr.insertEmail( value, false ); + } else if ( name == mAttributes[ "phoneNumber" ].lower() ) { + PhoneNumber phone; + phone.setNumber( value ); + addr.insertPhoneNumber( phone ); + break; // read only the home number + } else if ( name == mAttributes[ "familyName" ].lower() ) { + addr.setFamilyName( value ); + } else if ( name == mAttributes[ "uid" ].lower() ) { + addr.setUid( value ); + } + } + ldap_value_free( values ); + } + ber_free( track, 0 ); + + addressBook()->insertAddressee( addr ); + } + + ldap_msgfree( res ); + + for ( i = 0; LdapSearchAttr[ i ]; ++i ) + delete [] LdapSearchAttr[ i ]; + delete [] LdapSearchAttr; + + return true; +} + +bool ResourceLDAP::save( Ticket * ) +{ + AddressBook::Iterator it; + for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) { + if ( (*it).resource() == this && (*it).changed() ) { + LDAPMod **mods = NULL; + + addModOp( &mods, "objectClass", "organizationalPerson" ); + addModOp( &mods, "objectClass", "person" ); + addModOp( &mods, "objectClass", "Top" ); + addModOp( &mods, mAttributes[ "commonName" ].utf8(), (*it).assembledName() ); + addModOp( &mods, mAttributes[ "formattedName" ].utf8(), (*it).formattedName() ); + addModOp( &mods, mAttributes[ "givenName" ].utf8(), (*it).givenName() ); + addModOp( &mods, mAttributes[ "familyName" ].utf8(), (*it).familyName() ); + addModOp( &mods, mAttributes[ "uid" ].utf8(), (*it).uid() ); + + QStringList emails = (*it).emails(); + QStringList::ConstIterator mailIt; + bool first = true; + for ( mailIt = emails.begin(); mailIt != emails.end(); ++mailIt ) { + if ( first ) { + addModOp( &mods, mAttributes[ "mail" ].utf8(), (*mailIt) ); + first = false; + } else + addModOp( &mods, mAttributes[ "mailAlias" ].utf8(), (*mailIt) ); + } + + PhoneNumber number = (*it).phoneNumber( PhoneNumber::Home ); + addModOp( &mods, mAttributes[ "phoneNumber" ].utf8(), number.number() ); + + QString dn = "cn=" + (*it).assembledName() + "," + mDn; + + int retval; + if ( (retval = ldap_add_s( mLdap, dn.local8Bit(), mods )) != LDAP_SUCCESS ) + addressBook()->error( i18n( "Unable to modify '%1' on server '%2'" ).arg( (*it).uid() ).arg( mHost ) ); + + ldap_mods_free( mods, 1 ); + + // mark as unchanged + (*it).setChanged( false ); + } + } + + return true; +} + +void ResourceLDAP::removeAddressee( const Addressee &addr ) +{ + LDAPMessage *res; + LDAPMessage *msg; + + QString filter = QString( "(&(uid=%1)(%2))" ).arg( addr.uid() ).arg( mFilter ); + + kdDebug(5700) << "ldap:removeAddressee" << filter << endl; + + ldap_search_s( mLdap, mDn.local8Bit(), LDAP_SCOPE_SUBTREE, filter.local8Bit(), + 0, 0, &res ); + + for ( msg = ldap_first_entry( mLdap, res ); msg; msg = ldap_next_entry( mLdap, msg ) ) { + char *dn = ldap_get_dn( mLdap, msg ); + kdDebug(5700) << "found " << dn << endl; + if ( ldap_delete_s( mLdap, dn ) != LDAP_SUCCESS ) + addressBook()->error( i18n( "Unable to delete '%1' on server '%2'" ).arg( dn ).arg( mHost ) ); + ldap_memfree( dn ); + } + + ldap_msgfree( res ); +} + +void ResourceLDAP::setUser( const QString &user ) +{ + mUser = user; +} + +QString ResourceLDAP::user() const +{ + return mUser; +} + +void ResourceLDAP::setPassword( const QString &password ) +{ + mPassword = password; +} + +QString ResourceLDAP::password() const +{ + return mPassword; +} + +void ResourceLDAP::setDn( const QString &dn ) +{ + mDn = dn; +} + +QString ResourceLDAP::dn() const +{ + return mDn; +} + +void ResourceLDAP::setHost( const QString &host ) +{ + mHost = host; +} + +QString ResourceLDAP::host() const +{ + return mHost; +} + +void ResourceLDAP::setPort( int port ) +{ + mPort = port; +} + +int ResourceLDAP::port() const +{ + return mPort; +} + +void ResourceLDAP::setFilter( const QString &filter ) +{ + mFilter = filter; +} + +QString ResourceLDAP::filter() const +{ + return mFilter; +} + +void ResourceLDAP::setIsAnonymous( bool value ) +{ + mAnonymous = value; +} + +bool ResourceLDAP::isAnonymous() const +{ + return mAnonymous; +} + +void ResourceLDAP::setAttributes( const QMap &attributes ) +{ + mAttributes = attributes; +} + +QMap ResourceLDAP::attributes() const +{ + return mAttributes; +} + +void addModOp( LDAPMod ***pmods, const QString &attr, const QString &value ) +{ + if ( value.isNull() ) + return; + + LDAPMod **mods; + + mods = *pmods; + + uint i = 0; + if ( mods != 0 ) + for ( ; mods[ i ] != 0; ++i ); + + if (( mods = (LDAPMod **)realloc( mods, (i + 2) * sizeof( LDAPMod * ))) == 0 ) { + kdError() << "ResourceLDAP: realloc" << endl; + return; + } + + *pmods = mods; + mods[ i + 1 ] = 0; + + mods[ i ] = new LDAPMod; + + mods[ i ]->mod_op = 0; + mods[ i ]->mod_type = strdup( attr.utf8() ); + mods[ i ]->mod_values = new char*[ 2 ]; + mods[ i ]->mod_values[ 0 ] = strdup( value.utf8() ); + mods[ i ]->mod_values[ 1 ] = 0; +} + diff --git a/kabc/plugins/ldap/resourceldap.h b/kabc/plugins/ldap/resourceldap.h new file mode 100644 index 0000000..0625f30 --- a/dev/null +++ b/kabc/plugins/ldap/resourceldap.h @@ -0,0 +1,99 @@ +/* + This file is part of libkabc. + Copyright (c) 2002 Tobias Koenig + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +/* +Enhanced Version of the file for platform independent KDE tools. +Copyright (c) 2004 Ulf Schenk + +$Id$ +*/ + +#ifndef KABC_RESOURCELDAP_H +#define KABC_RESOURCELDAP_H + + +#include +#include + +#include "addressbook.h" +#include "resource.h" + +class KConfig; + +namespace KABC { + +class ResourceLDAP : public Resource +{ +public: + + ResourceLDAP( const KConfig* ); + + virtual void writeConfig( KConfig* ); + + virtual bool doOpen(); + virtual void doClose(); + + virtual Ticket *requestSaveTicket(); + + virtual bool load(); + virtual bool save( Ticket * ); + + virtual void removeAddressee( const Addressee& addr ); + + void setUser( const QString &user ); + QString user() const; + + void setPassword( const QString &password ); + QString password() const; + + void setDn( const QString &dn ); + QString dn() const; + + void setHost( const QString &host ); + QString host() const; + + void setPort( int port ); + int port() const; + + void setFilter( const QString &filter ); + QString filter() const; + + void setIsAnonymous( bool value ); + bool isAnonymous() const; + + void setAttributes( const QMap &attributes ); + QMap attributes() const; + +private: + QString mUser; + QString mPassword; + QString mDn; + QString mHost; + QString mFilter; + int mPort; + bool mAnonymous; + QMap mAttributes; + + LDAP *mLdap; +}; + +} + +#endif diff --git a/kabc/plugins/ldap/resourceldapconfig.cpp b/kabc/plugins/ldap/resourceldapconfig.cpp new file mode 100644 index 0000000..2c0d030 --- a/dev/null +++ b/kabc/plugins/ldap/resourceldapconfig.cpp @@ -0,0 +1,254 @@ +/* + This file is part of libkabc. + Copyright (c) 2002 Tobias Koenig + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +/* +Enhanced Version of the file for platform independent KDE tools. +Copyright (c) 2004 Ulf Schenk + +$Id$ +*/ + +#include +#include +#include +#include +#include +#include + +//US #include +#include +#include +#include +#include +#include + +#include "resourceldap.h" + +#include "resourceldapconfig.h" + +using namespace KABC; + +ResourceLDAPConfig::ResourceLDAPConfig( QWidget* parent, const char* name ) + : KRES::ConfigWidget( parent, name ) +{ + QGridLayout *mainLayout = new QGridLayout( this, 8, 2, 0, + KDialog::spacingHint() ); + + QLabel *label = new QLabel( i18n( "User:" ), this ); + mUser = new KLineEdit( this ); + + mainLayout->addWidget( label, 0, 0 ); + mainLayout->addWidget( mUser, 0, 1 ); + + label = new QLabel( i18n( "Password:" ), this ); + mPassword = new KLineEdit( this ); + mPassword->setEchoMode( KLineEdit::Password ); + + mainLayout->addWidget( label, 1, 0 ); + mainLayout->addWidget( mPassword, 1, 1 ); + + label = new QLabel( i18n( "Host:" ), this ); + mHost = new KLineEdit( this ); + + mainLayout->addWidget( label, 2, 0 ); + mainLayout->addWidget( mHost, 2, 1 ); + + label = new QLabel( i18n( "Port:" ), this ); + QVBox *box = new QVBox( this ); + mPort = new QSpinBox( 0, 65535, 1, box ); + mPort->setSizePolicy( QSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred ) ); + mPort->setValue( 389 ); + new QWidget( box, "dummy" ); + + mainLayout->addWidget( label, 3, 0 ); + mainLayout->addWidget( box, 3, 1 ); + + label = new QLabel( i18n( "Dn:" ), this ); + mDn = new KLineEdit( this ); + + mainLayout->addWidget( label, 4, 0 ); + mainLayout->addWidget( mDn, 4, 1 ); + + label = new QLabel( i18n( "Filter:" ), this ); + mFilter = new KLineEdit( this ); + + mainLayout->addWidget( label, 5, 0 ); + mainLayout->addWidget( mFilter, 5, 1 ); + + mAnonymous = new QCheckBox( i18n( "Anonymous login" ), this ); + mainLayout->addMultiCellWidget( mAnonymous, 6, 6, 0, 1 ); + + mEditButton = new QPushButton( i18n( "Edit Attributes..." ), this ); + mainLayout->addMultiCellWidget( mEditButton, 7, 7, 0, 1 ); + + connect( mAnonymous, SIGNAL( toggled(bool) ), mUser, SLOT( setDisabled(bool) ) ); + connect( mAnonymous, SIGNAL( toggled(bool) ), mPassword, SLOT( setDisabled(bool) ) ); + connect( mEditButton, SIGNAL( clicked() ), SLOT( editAttributes() ) ); +} + +void ResourceLDAPConfig::loadSettings( KRES::Resource *res ) +{ +//US ResourceLDAP *resource = dynamic_cast( res ); + ResourceLDAP *resource = (ResourceLDAP*)( res ); + + if ( !resource ) { + kdDebug(5700) << "ResourceLDAPConfig::loadSettings(): cast failed" << endl; + return; + } + + mUser->setText( resource->user() ); + mPassword->setText( resource->password() ); + mHost->setText( resource->host() ); + mPort->setValue( resource->port() ); + mDn->setText( resource->dn() ); + mFilter->setText( resource->filter() ); + mAnonymous->setChecked( resource->isAnonymous() ); + mAttributes = resource->attributes(); +} + +void ResourceLDAPConfig::saveSettings( KRES::Resource *res ) +{ +//US ResourceLDAP *resource = dynamic_cast( res ); + ResourceLDAP *resource = (ResourceLDAP*)( res ); + + if ( !resource ) { + kdDebug(5700) << "ResourceLDAPConfig::saveSettings(): cast failed" << endl; + return; + } + + resource->setUser( mUser->text() ); + resource->setPassword( mPassword->text() ); + resource->setHost( mHost->text() ); + resource->setPort( mPort->value() ); + resource->setDn( mDn->text() ); + resource->setFilter( mFilter->text() ); + resource->setIsAnonymous( mAnonymous->isChecked() ); + resource->setAttributes( mAttributes ); +} + +void ResourceLDAPConfig::editAttributes() +{ + AttributesDialog dlg( mAttributes, this ); + if ( dlg.exec() ) + mAttributes = dlg.attributes(); +} + +AttributesDialog::AttributesDialog( const QMap &attributes, + QWidget *parent, const char *name ) + : KDialogBase( Plain, i18n( "Attributes Configuration" ), Ok | Cancel, + Ok, parent, name, true, true ) +{ + mNameDict.setAutoDelete( true ); + mNameDict.insert( "commonName", new QString( i18n( "Common name" ) ) ); + mNameDict.insert( "formattedName", new QString( i18n( "Formatted name" ) ) ); + mNameDict.insert( "familyName", new QString( i18n( "Family name" ) ) ); + mNameDict.insert( "givenName", new QString( i18n( "Given name" ) ) ); + mNameDict.insert( "mail", new QString( i18n( "Email" ) ) ); + mNameDict.insert( "mailAlias", new QString( i18n( "Email alias" ) ) ); + mNameDict.insert( "phoneNumber", new QString( i18n( "Telephone number" ) ) ); + mNameDict.insert( "uid", new QString( i18n( "UID" ) ) ); + + // overwrite the default values here + QMap kolabMap, netscapeMap, evolutionMap, outlookMap; + + // kolab + kolabMap.insert( "formattedName", "display-name" ); + kolabMap.insert( "mailAlias", "mailalias" ); + + // evolution + evolutionMap.insert( "formattedName", "fileAs" ); + + mMapList.append( attributes ); + mMapList.append( kolabMap ); + mMapList.append( netscapeMap ); + mMapList.append( evolutionMap ); + mMapList.append( outlookMap ); + + QFrame *page = plainPage(); + QGridLayout *layout = new QGridLayout( page, 2, attributes.count() + 1, + 0, spacingHint() ); + + QLabel *label = new QLabel( i18n( "Template:" ), page ); + layout->addWidget( label, 0, 0 ); + mMapCombo = new KComboBox( page ); + layout->addWidget( mMapCombo, 0, 1 ); + + mMapCombo->insertItem( i18n( "User Defined" ) ); + mMapCombo->insertItem( i18n( "Kolab" ) ); + mMapCombo->insertItem( i18n( "Netscape" ) ); + mMapCombo->insertItem( i18n( "Evolution" ) ); + mMapCombo->insertItem( i18n( "Outlook" ) ); + connect( mMapCombo, SIGNAL( activated( int ) ), SLOT( mapChanged( int ) ) ); + + QMap::ConstIterator it; + int i; + for ( i = 1, it = attributes.begin(); it != attributes.end(); ++it, ++i ) { + label = new QLabel( *mNameDict[ it.key() ] + ":", page ); + KLineEdit *lineedit = new KLineEdit( page ); + mLineEditDict.insert( it.key(), lineedit ); + lineedit->setText( it.data() ); + label->setBuddy( lineedit ); + layout->addWidget( label, i, 0 ); + layout->addWidget( lineedit, i, 1 ); + } + +//US KAcceleratorManager::manage( this ); +} + +AttributesDialog::~AttributesDialog() +{ +} + +QMap AttributesDialog::attributes() const +{ + QMap map; + + QDictIterator it( mLineEditDict ); + for ( ; it.current(); ++it ) + map.insert( it.currentKey(), it.current()->text() ); + + return map; +} + +void AttributesDialog::mapChanged( int pos ) +{ + // default map + QMap defaultMap; + defaultMap.insert( "commonName", "cn" ); + defaultMap.insert( "formattedName", "displayName" ); + defaultMap.insert( "familyName", "sn" ); + defaultMap.insert( "givenName", "givenName" ); + defaultMap.insert( "mail", "mail" ); + defaultMap.insert( "mailAlias", "" ); + defaultMap.insert( "phoneNumber", "telephoneNumber" ); + defaultMap.insert( "uid", "uid" ); + + // apply first the default and than the spezific changes + QMap::Iterator it; + for ( it = defaultMap.begin(); it != defaultMap.end(); ++it ) + mLineEditDict[ it.key() ]->setText( it.data() ); + + for ( it = mMapList[ pos ].begin(); it != mMapList[ pos ].end(); ++it ) { + if ( !it.data().isEmpty() ) + mLineEditDict[ it.key() ]->setText( it.data() ); + } +} + +//US #include "resourceldapconfig.moc" diff --git a/kabc/plugins/ldap/resourceldapconfig.h b/kabc/plugins/ldap/resourceldapconfig.h new file mode 100644 index 0000000..42d30ff --- a/dev/null +++ b/kabc/plugins/ldap/resourceldapconfig.h @@ -0,0 +1,100 @@ +/* + This file is part of libkabc. + Copyright (c) 2002 Tobias Koenig + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +/* +Enhanced Version of the file for platform independent KDE tools. +Copyright (c) 2004 Ulf Schenk + +$Id$ +*/ + +#ifndef RESOURCELDAPCONFIG_H +#define RESOURCELDAPCONFIG_H + +#include +//US +#include + +#include +#include + +class QCheckBox; +class QPushButton; +class QSpinBox; +class QString; + +class KComboBox; +class KLineEdit; + +namespace KABC { + +class ResourceLDAPConfig : public KRES::ConfigWidget +{ + Q_OBJECT + + public: + ResourceLDAPConfig( QWidget* parent = 0, const char* name = 0 ); + + public slots: + void loadSettings( KRES::Resource* ); + void saveSettings( KRES::Resource* ); + + private slots: + void editAttributes(); + + private: + KLineEdit *mUser; + KLineEdit *mPassword; + KLineEdit *mHost; + QSpinBox *mPort; + KLineEdit *mDn; + KLineEdit *mFilter; + QCheckBox *mAnonymous; + QPushButton *mEditButton; + QMap mAttributes; +}; + +class AttributesDialog : public KDialogBase +{ + Q_OBJECT + + public: + AttributesDialog( const QMap &attributes, QWidget *parent, + const char *name = 0 ); + ~AttributesDialog(); + + QMap attributes() const; + + private slots: + void mapChanged( int pos ); + + private: + enum { UserMap, KolabMap, NetscapeMap, EvolutionMap, OutlookMap }; + + KComboBox *mMapCombo; + QValueList< QMap > mMapList; + + QDict mLineEditDict; + QDict mNameDict; +}; + +} + +#endif -- cgit v0.9.0.2