author | ulf69 <ulf69> | 2004-10-13 21:27:10 (UTC) |
---|---|---|
committer | ulf69 <ulf69> | 2004-10-13 21:27:10 (UTC) |
commit | 737183a5abd7d7b02048e8a939bafd0a8e803918 (patch) (side-by-side diff) | |
tree | ddacf864a06a6669911b1d48862e564399eeb33b /kabc/plugins | |
parent | 7bb6c5f55d29ad4632b1f5c7361eee4c0d8b7cb0 (diff) | |
download | kdepimpi-737183a5abd7d7b02048e8a939bafd0a8e803918.zip kdepimpi-737183a5abd7d7b02048e8a939bafd0a8e803918.tar.gz kdepimpi-737183a5abd7d7b02048e8a939bafd0a8e803918.tar.bz2 |
removal of syncresources
-rw-r--r-- | kabc/plugins/dir/resourcedir.cpp | 7 | ||||
-rw-r--r-- | kabc/plugins/dir/resourcedir.h | 2 | ||||
-rw-r--r-- | kabc/plugins/file/resourcefile.cpp | 16 | ||||
-rw-r--r-- | kabc/plugins/file/resourcefile.h | 4 | ||||
-rw-r--r-- | kabc/plugins/ldap/resourceldap.cpp | 7 | ||||
-rw-r--r-- | kabc/plugins/ldap/resourceldap.h | 2 | ||||
-rw-r--r-- | kabc/plugins/opie/resourceopie.cpp | 11 | ||||
-rw-r--r-- | kabc/plugins/opie/resourceopie.h | 4 | ||||
-rw-r--r-- | kabc/plugins/qtopia/resourceqtopia.cpp | 11 | ||||
-rw-r--r-- | kabc/plugins/qtopia/resourceqtopia.h | 4 | ||||
-rw-r--r-- | kabc/plugins/sharpdtm/resourcesharpdtm.cpp | 8 | ||||
-rw-r--r-- | kabc/plugins/sharpdtm/resourcesharpdtm.h | 4 |
12 files changed, 35 insertions, 45 deletions
diff --git a/kabc/plugins/dir/resourcedir.cpp b/kabc/plugins/dir/resourcedir.cpp index e58e4f7..e2b7b08 100644 --- a/kabc/plugins/dir/resourcedir.cpp +++ b/kabc/plugins/dir/resourcedir.cpp @@ -1,375 +1,374 @@ /* This file is part of libkabc. Copyright (c) 2002 Tobias Koenig <tokoe@kde.org> 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 <sys/types.h> #include <sys/stat.h> #ifndef _WIN32_ #include <unistd.h> #endif #include <qregexp.h> #include <qtimer.h> #include <qwidget.h> #include <kapplication.h> #include <kconfig.h> #include <kdebug.h> //US #include <kgenericfactory.h> #include <kglobal.h> #include <klocale.h> #include <kstandarddirs.h> #include <kurlrequester.h> #include <kmessagebox.h> #include "addressbook.h" #include "formatfactory.h" #include "resourcedirconfig.h" #include "stdaddressbook.h" //US #include <qdir.h> #define NO_DIRWATCH #include "resourcedir.h" -#include "syncprefwidget.h" //#define ALLOW_LOCKING using namespace KABC; extern "C" #ifdef _WIN32_ __declspec(dllexport) #else { #endif //US void *init_kabc_dir() void *init_microkabc_dir() { - return new KRES::PluginFactory<ResourceDir,ResourceDirConfig, SyncPrefWidgetContainer>(); + return new KRES::PluginFactory<ResourceDir,ResourceDirConfig>(); } #ifndef _WIN32_ } #endif -ResourceDir::ResourceDir( const KConfig *config, bool syncable ) - : Resource( config, syncable ) +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"; } FormatFactory *factory = FormatFactory::self(); mFormat = factory->format( mFormatName ); if ( !mFormat ) { mFormatName = "vcard"; mFormat = factory->format( mFormatName ); } /*US //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()); */ #ifndef NO_DIRWATCH connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( pathChanged() ) ); connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( pathChanged() ) ); connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( pathChanged() ) ); #endif setPath( path ); } ResourceDir::~ResourceDir() { delete mFormat; mFormat = 0; } void ResourceDir::writeConfig( KConfig *config ) { config->setGroup( "Resource_" + identifier() ); Resource::writeConfig( config ); config->writeEntry( "FilePath", mPath ); config->writeEntry( "FileFormat", mFormatName ); } Ticket *ResourceDir::requestSaveTicket() { if ( !addressBook() ) return 0; #ifdef ALLOW_LOCKING if ( !lock( mPath ) ) { return 0; } #endif 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() { 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 ) { AddressBook::Iterator it; bool ok = true; #ifndef NO_DIRWATCH mDirWatch.stopScan(); #endif 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(); } #ifndef NO_DIRWATCH mDirWatch.startScan(); #endif delete ticket; #ifdef ALLOW_LOCKING unlock( mPath ); #endif return ok; } bool ResourceDir::lock( const QString &path ) { #ifdef ALLOW_LOCKING 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" ); 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() ); // Create unique file QFile file( mLockUniqueName ); file.open( IO_WriteOnly ); file.close(); // Create lock file int result = 0; #ifndef _WIN32_ result = ::link( QFile::encodeName( mLockUniqueName ), QFile::encodeName( lockName ) ); #endif if ( result == 0 ) { addressBook()->emitAddressBookLocked(); return true; } // TODO: check stat return false; #else return true; #endif } void ResourceDir::unlock( const QString &path ) { #ifdef ALLOW_LOCKING 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(); #else return; #endif } void ResourceDir::setPath( const QString &path ) { #ifndef NO_DIRWATCH mDirWatch.stopScan(); mDirWatch.removeDir( mPath ); mPath = path; mDirWatch.addDir( mPath, true ); mDirWatch.startScan(); #else mPath = path; #endif //US simulate KDirWatch event //US pathChanged(); } QString ResourceDir::path() const { return mPath; } void ResourceDir::setFormat( const QString &format ) { mFormatName = format; if ( mFormat ) delete mFormat; FormatFactory *factory = FormatFactory::self(); mFormat = factory->format( mFormatName ); /*US 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; QString text( i18n( "Dir resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( mPath ) ); if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) { 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 index 77cd18c..6c1e922 100644 --- a/kabc/plugins/dir/resourcedir.h +++ b/kabc/plugins/dir/resourcedir.h @@ -1,116 +1,116 @@ /* This file is part of libkabc. Copyright (c) 2002 Tobias Koenig <tokoe@kde.org> 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 <kconfig.h> #include <kdirwatch.h> #include <sys/types.h> #include "resource.h" class QTimer; namespace KABC { class FormatPlugin; /** @internal */ class ResourceDir : public Resource { Q_OBJECT public: - ResourceDir( const KConfig*, bool syncable ); + 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; #ifndef NO_DIRWATCH KDirWatch mDirWatch; #endif QString mPath; QString mFormatName; QString mLockUniqueName; }; } #endif diff --git a/kabc/plugins/file/resourcefile.cpp b/kabc/plugins/file/resourcefile.cpp index 163f2b4..dc5932f 100644 --- a/kabc/plugins/file/resourcefile.cpp +++ b/kabc/plugins/file/resourcefile.cpp @@ -1,408 +1,404 @@ /* This file is part of libkabc. Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> 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 <sys/types.h> #include <sys/stat.h> #ifndef _WIN32_ #include <unistd.h> #endif #include <qfile.h> #include <qfileinfo.h> #include <qregexp.h> #include <qtimer.h> #include <kapplication.h> #include <kconfig.h> #include <kdebug.h> #include <klocale.h> //US #include <ksavefile.h> #include <kstandarddirs.h> #include <kmessagebox.h> #include "formatfactory.h" #include "resource.h" #include "resourcefileconfig.h" #include "stdaddressbook.h" #define NO_DIRWATCH #include "resourcefile.h" -#include "syncprefwidget.h" //#define ALLOW_LOCKING using namespace KABC; extern "C" #ifdef _WIN32_ __declspec(dllexport) #else { #endif //US void *init_kabc_file() void *init_microkabc_file() { - return new KRES::PluginFactory<ResourceFile,ResourceFileConfig, SyncPrefWidgetContainer>(); + return new KRES::PluginFactory<ResourceFile,ResourceFileConfig>(); } #ifndef _WIN32_ } #endif -ResourceFile::ResourceFile( const KConfig *config, bool syncable ) - : Resource( config, syncable ) , mFormat( 0 ) +ResourceFile::ResourceFile( const KConfig *config ) + : Resource( config ) , mFormat( 0 ) { QString fileName, formatName, default_fileName; - if (syncable == true) - default_fileName = "/home/polo/kdepim/apps/kabc/localfile.vcf"; - else - default_fileName = StdAddressBook::fileName(); + default_fileName = StdAddressBook::fileName(); KConfig *cfg = (KConfig *)config; if ( cfg ) { fileName = cfg->readEntry( "FileName", default_fileName ); formatName = cfg->readEntry( "FileFormat", "vcard" ); } else { fileName = default_fileName; formatName = "vcard"; } init( fileName, formatName ); } -ResourceFile::ResourceFile( const QString &fileName, bool syncable , +ResourceFile::ResourceFile( const QString &fileName , const QString &formatName ) - : Resource( 0, syncable ) + : 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; FormatFactory *factory = FormatFactory::self(); mFormat = factory->format( mFormatName ); if ( !mFormat ) { mFormatName = "vcard"; mFormat = factory->format( mFormatName ); } #ifndef NO_DIRWATCH connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) ); connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) ); connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) ); #endif setFileName( fileName ); } ResourceFile::~ResourceFile() { delete mFormat; mFormat = 0; } void ResourceFile::writeConfig( KConfig *config ) { config->setGroup( "Resource_" + identifier() ); 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; #ifdef ALLOW_LOCKING if ( !lock( mFileName ) ) { qDebug("unablt to lock file "); return 0; } #endif return createTicket( this ); } bool ResourceFile::doOpen() { QFile file( mFileName ); qDebug("ResourceFile::openfile %s ", mFileName.latin1()); 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() { 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()); // 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 ,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 #ifndef NO_DIRWATCH mDirWatch.stopScan(); #endif 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 ) ); #ifndef NO_DIRWATCH mDirWatch.startScan(); #endif delete ticket; #ifdef ALLOW_LOCKING unlock( mFileName ); #endif return ok; } bool ResourceFile::lock( const QString &fileName ) { #ifdef ALLOW_LOCKING 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" ); 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; #ifndef _WIN32_ result = ::link( QFile::encodeName( mLockUniqueName ), QFile::encodeName( lockName ) ); #endif if ( result == 0 ) { addressBook()->emitAddressBookLocked(); return true; } // TODO: check stat return false; #else return true; #endif } void ResourceFile::unlock( const QString &fileName ) { #ifdef ALLOW_LOCKING 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(); #else return; #endif } void ResourceFile::setFileName( const QString &fileName ) { #ifndef NO_DIRWATCH mDirWatch.stopScan(); mDirWatch.removeFile( mFileName ); mFileName = fileName; mDirWatch.addFile( mFileName ); mDirWatch.startScan(); #else mFileName = fileName; #endif //US simulate KDirWatch event //US fileChanged(); } QString ResourceFile::fileName() const { return mFileName; } void ResourceFile::setFormat( const QString &format ) { mFormatName = format; delete mFormat; FormatFactory *factory = FormatFactory::self(); mFormat = factory->format( mFormatName ); /*US //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; QString text( i18n( "File resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( mFileName ) ); if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) { 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 index 8339340..b4421b2 100644 --- a/kabc/plugins/file/resourcefile.h +++ b/kabc/plugins/file/resourcefile.h @@ -1,163 +1,163 @@ /* This file is part of libkabc. Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> 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 <kconfig.h> #include <kdirwatch.h> #include <sys/types.h> #include <resource.h> 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, bool syncable ); + ResourceFile( const KConfig *cfg ); /** Construct file resource on file @arg fileName using format @arg formatName. */ - ResourceFile( const QString &fileName, bool syncable , const QString &formatName = "vcard" ); + 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; #ifndef NO_DIRWATCH KDirWatch mDirWatch; #endif }; } #endif diff --git a/kabc/plugins/ldap/resourceldap.cpp b/kabc/plugins/ldap/resourceldap.cpp index 17f115d..55c43af 100644 --- a/kabc/plugins/ldap/resourceldap.cpp +++ b/kabc/plugins/ldap/resourceldap.cpp @@ -1,445 +1,444 @@ /* This file is part of libkabc. Copyright (c) 2002 Tobias Koenig <tokoe@kde.org> 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 <kdebug.h> #include <kglobal.h> #include <klineedit.h> #include <klocale.h> #include <kconfig.h> #include <kstringhandler.h> #include <stdlib.h> #include "resourceldap.h" #include "resourceldapconfig.h" -#include "syncprefwidget.h" using namespace KABC; extern "C" { //US void *init_kabc_ldap() void *init_microkabc_ldap() { - return new KRES::PluginFactory<ResourceLDAP,ResourceLDAPConfig, SyncPrefWidgetContainer>(); + return new KRES::PluginFactory<ResourceLDAP,ResourceLDAPConfig>(); } } void addModOp( LDAPMod ***pmods, const QString &attr, const QString &value ); -ResourceLDAP::ResourceLDAP( const KConfig *config, bool syncable ) - : Resource( config, syncable ), mPort( 389 ), mLdap( 0 ) +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<QString, QString>::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<QString, QString>::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<QString, QString> &attributes ) { mAttributes = attributes; } QMap<QString, QString> 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 index 0aad3c1..0625f30 100644 --- a/kabc/plugins/ldap/resourceldap.h +++ b/kabc/plugins/ldap/resourceldap.h @@ -1,99 +1,99 @@ /* This file is part of libkabc. Copyright (c) 2002 Tobias Koenig <tokoe@kde.org> 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 <lber.h> #include <ldap.h> #include "addressbook.h" #include "resource.h" class KConfig; namespace KABC { class ResourceLDAP : public Resource { public: - ResourceLDAP( const KConfig*, bool syncable ); + 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<QString, QString> &attributes ); QMap<QString, QString> attributes() const; private: QString mUser; QString mPassword; QString mDn; QString mHost; QString mFilter; int mPort; bool mAnonymous; QMap<QString, QString> mAttributes; LDAP *mLdap; }; } #endif diff --git a/kabc/plugins/opie/resourceopie.cpp b/kabc/plugins/opie/resourceopie.cpp index 22237dc..f610b38 100644 --- a/kabc/plugins/opie/resourceopie.cpp +++ b/kabc/plugins/opie/resourceopie.cpp @@ -1,350 +1,349 @@ /* This file is part of libkabc. Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> 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 <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <qdir.h> #include <qfile.h> #include <qfileinfo.h> #include <qregexp.h> #include <kapplication.h> #include <kconfig.h> #include <kdebug.h> #include <klocale.h> //US #include <ksavefile.h> #include <kstandarddirs.h> #include <kmessagebox.h> //US #include "formatfactory.h" //US #include <qpe/qpeapplication.h> #include <opie/ocontactaccess.h> #include <opie/ocontactaccessbackend_xml.h> #include "resourceopieconfig.h" #include "stdaddressbook.h" -#include "syncprefwidget.h" #include "opieconverter.h" #include "resourceopie.h" using namespace KABC; extern "C" { void *init_microkabc_opie() { - return new KRES::PluginFactory<ResourceOpie,ResourceOpieConfig, SyncPrefWidgetContainer>(); + return new KRES::PluginFactory<ResourceOpie,ResourceOpieConfig>(); } } -ResourceOpie::ResourceOpie( const KConfig *config, bool syncable ) - : Resource( config, syncable ), mAccess(0), mConverter (0) +ResourceOpie::ResourceOpie( const KConfig *config ) + : Resource( config ), mAccess(0), mConverter (0) { QString fileName = QDir::homeDirPath() + "/Applications/addressbook/addressbook.xml"; KConfig *cfg = (KConfig *)config; if ( cfg ) { fileName = cfg->readEntry( "FileName", fileName ); } init( fileName ); } -ResourceOpie::ResourceOpie( const QString &fileName, bool syncable ) - : Resource( 0, syncable ) +ResourceOpie::ResourceOpie( const QString &fileName ) + : Resource( 0 ) { init( fileName ); } void ResourceOpie::init( const QString &fileName ) { qDebug("ResourceOpie::init()"); 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 ); } ResourceOpie::~ResourceOpie() { if (mConverter != 0) delete mConverter; if(mAccess != 0) delete mAccess; } void ResourceOpie::writeConfig( KConfig *config ) { Resource::writeConfig( config ); config->writeEntry( "FileName", fileName() ); } Ticket *ResourceOpie::requestSaveTicket() { kdDebug(5700) << "ResourceOpie::requestSaveTicket()" << endl; qDebug("ResourceOpie::requestSaveTicket: %s", fileName().latin1()); if ( !addressBook() ) return 0; if ( !lock( fileName() ) ) { kdDebug(5700) << "ResourceOpie::requestSaveTicket(): Unable to lock file '" << fileName() << "'" << endl; return 0; } return createTicket( this ); } bool ResourceOpie::doOpen() { qDebug("ResourceOpie::doOpen: %s", fileName().latin1()); OContactAccessBackend_XML* backend = new OContactAccessBackend_XML( "KDEPim/Pi", fileName() ); mAccess = new OContactAccess("KDEPim/Pi", 0l, backend, false); if ( !mAccess ) { qDebug("Unable to load file() %s", fileName().latin1()); return false; } mAccess->setReadAhead( 32 ); // Use ReadAhead-Cache if available if (mConverter == 0) { mConverter = new OpieConverter(); bool res = mConverter->init(); if ( !res ) { qDebug("Unable to initialize opie converter. Most likely a problem with the category file"); delete mAccess; mAccess = 0; return false; } } return true; } void ResourceOpie::doClose() { qDebug("ResourceOpie::doClose: %s", fileName().latin1()); if(mAccess) { delete mAccess; mAccess = 0; } // it seems so, that deletion of access deletes backend as well //delete backend; return; } bool ResourceOpie::load() { qDebug("ResourceOpie::load: %s", fileName().latin1()); bool res = false; OContactAccess::List::Iterator it; OContactAccess::List allList = mAccess->allRecords(); for ( it = allList.begin(); it != allList.end(); ++it ) { const OContact c = (*it); KABC::Addressee addressee; res = mConverter->opieToAddressee( c, addressee ); if ( !addressee.isEmpty() && res ) { addressee.setResource( this ); addressBook()->insertAddressee( addressee ); } } return true; } bool ResourceOpie::save( Ticket *ticket ) { qDebug("ResourceOpie::save: %s", fileName().latin1()); mDirWatch.stopScan(); KABC::AddressBook::Iterator it; bool res; for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) { OContact c; KABC::Addressee addressee = (*it); res = mConverter->addresseeToOpie( *it, c ); if (res == true) { res = mAccess->add(c); if (res == false) qDebug("Unable to append Contact %s", c.fullName().latin1()); } else { qDebug("Unable to convert Addressee %s", addressee.formattedName().latin1()); } } mAccess->save(); mDirWatch.startScan(); delete ticket; unlock( fileName() ); return true; } bool ResourceOpie::lock( const QString &lockfileName ) { qDebug("ResourceOpie::lock: %s", fileName().latin1()); kdDebug(5700) << "ResourceOpie::lock()" << endl; QString fn = lockfileName; KURL url(fn); QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); kdDebug(5700) << "-- lock name: " << lockName << endl; if (QFile::exists( lockName )) { qDebug("ResourceOpie::lock: lockfile %s already exists. (Delete it before continuing if nobody else is accessing the resourcefile %s)", lockName.latin1(), fileName().latin1()); 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; #ifndef _WIN32_ result = ::link( QFile::encodeName( mLockUniqueName ), QFile::encodeName( lockName ) ); #endif if ( result == 0 ) { addressBook()->emitAddressBookLocked(); return true; } // TODO: check stat return false; } void ResourceOpie::unlock( const QString &fileName ) { qDebug("ResourceOpie::unlock() %s", fileName.latin1()); QString fn = fileName; KURL url(fn); QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); QFile::remove( lockName ); QFile::remove( mLockUniqueName ); addressBook()->emitAddressBookUnlocked(); } void ResourceOpie::setFileName( const QString &newFileName ) { mDirWatch.stopScan(); mDirWatch.removeFile( fileName() ); Resource::setFileName( newFileName ); mDirWatch.addFile( fileName() ); mDirWatch.startScan(); } void ResourceOpie::fileChanged() { // There is a small theoretical chance that KDirWatch calls us before // we are fully constructed if (!addressBook()) return; QString text( i18n( "Opie resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( fileName() ) ); if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) { load(); addressBook()->emitAddressBookChanged(); } load(); addressBook()->emitAddressBookChanged(); } void ResourceOpie::removeAddressee( const Addressee &addr ) { } void ResourceOpie::cleanUp() { // qDebug("ResourceOpie::cleanup() %s", mFileName.latin1()); unlock( fileName() ); } //US #include "resourceopie.moc" diff --git a/kabc/plugins/opie/resourceopie.h b/kabc/plugins/opie/resourceopie.h index d5b4ebd..9d05c70 100644 --- a/kabc/plugins/opie/resourceopie.h +++ b/kabc/plugins/opie/resourceopie.h @@ -1,146 +1,146 @@ /* This file is part of libkabc. Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> 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_RESOURCEOPIE_H #define KABC_RESOURCEOPIE_H #include <kconfig.h> #include <kdirwatch.h> #include <sys/types.h> #include "resource.h" class OContactAccess; namespace KABC { class ResourceConfigWidget; class OpieConverter; /** @internal */ class ResourceOpie : public Resource { Q_OBJECT public: /** Constructor. @param cfg The config object where custom resource settings are stored. */ - ResourceOpie( const KConfig *cfg, bool syncable ); + ResourceOpie( const KConfig *cfg ); /** Construct file resource on file @arg fileName using format @arg formatName. */ - ResourceOpie( const QString &fileName, bool syncable ); + ResourceOpie( const QString &fileName ); /** * Destructor. */ ~ResourceOpie(); /** 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 ); /** * Remove a addressee from its source. * This method is mainly called by KABC::AddressBook. */ virtual void removeAddressee( const Addressee& addr ); /** * Set name of file to be used for saving. */ virtual void setFileName( const QString & ); /** * 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 ); bool lock( const QString &fileName ); void unlock( const QString &fileName ); private: OContactAccess* mAccess; OpieConverter* mConverter; QString mLockUniqueName; KDirWatch mDirWatch; }; } #endif diff --git a/kabc/plugins/qtopia/resourceqtopia.cpp b/kabc/plugins/qtopia/resourceqtopia.cpp index f9cabcb..deb218b 100644 --- a/kabc/plugins/qtopia/resourceqtopia.cpp +++ b/kabc/plugins/qtopia/resourceqtopia.cpp @@ -1,322 +1,321 @@ /* This file is part of libkabc. Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> 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 <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <qdir.h> #include <qfile.h> #include <qfileinfo.h> #include <qregexp.h> //US #include <qtimer.h> #include <kapplication.h> #include <kconfig.h> #include <kdebug.h> #include <klocale.h> //US #include <ksavefile.h> #include <kstandarddirs.h> #include <kmessagebox.h> #include <qpe/pim/addressbookaccess.h> #include "resourceqtopiaconfig.h" #include "stdaddressbook.h" #include "qtopiaconverter.h" -#include "syncprefwidget.h" #include "resourceqtopia.h" using namespace KABC; extern "C" { void *init_microkabc_qtopia() { - return new KRES::PluginFactory<ResourceQtopia,ResourceQtopiaConfig, SyncPrefWidgetContainer>(); + return new KRES::PluginFactory<ResourceQtopia,ResourceQtopiaConfig>(); } } -ResourceQtopia::ResourceQtopia( const KConfig *config, bool syncable ) - : Resource( config, syncable ), mConverter (0) +ResourceQtopia::ResourceQtopia( const KConfig *config ) + : Resource( config ), mConverter (0) { // we can not choose the filename. Therefore use the default to display QString fileName = QDir::homeDirPath() + "/Applications/addressbook/addressbook.xml"; init( fileName ); } -ResourceQtopia::ResourceQtopia( const QString &fileName, bool syncable ) - : Resource( 0, syncable ) +ResourceQtopia::ResourceQtopia( const QString &fileName ) + : Resource( 0 ) { init( fileName ); } void ResourceQtopia::init( const QString &fileName ) { 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 ); } ResourceQtopia::~ResourceQtopia() { if (mConverter != 0) delete mConverter; if(mAccess != 0) delete mAccess; } void ResourceQtopia::writeConfig( KConfig *config ) { Resource::writeConfig( config ); } Ticket *ResourceQtopia::requestSaveTicket() { kdDebug(5700) << "ResourceQtopia::requestSaveTicket()" << endl; qDebug("ResourceQtopia::requestSaveTicket: %s", fileName().latin1()); if ( !addressBook() ) return 0; if ( !lock( fileName() ) ) { kdDebug(5700) << "ResourceQtopia::requestSaveTicket(): Unable to lock file '" << fileName() << "'" << endl; return 0; } return createTicket( this ); } bool ResourceQtopia::doOpen() { qDebug("ResourceQtopia::doOpen(): %s", fileName().latin1()); mAccess = new AddressBookAccess(); if ( !mAccess ) { qDebug("Unable to load file() %s", fileName().latin1()); return false; } if (mConverter == 0) { mConverter = new QtopiaConverter(); bool res = mConverter->init(); if ( !res ) { QString msg("Unable to initialize qtopia converter. Most likely a problem with the category file"); qDebug(msg); delete mAccess; mAccess = 0; return false; } } return true; } void ResourceQtopia::doClose() { qDebug("ResourceQtopia::doClose: %s", fileName().latin1()); if(mAccess) { delete mAccess; mAccess = 0; } // it seems so, that deletion of access deletes backend as well //delete backend; return; } bool ResourceQtopia::load() { qDebug("ResourceQtopia::load: %s", fileName().latin1()); AddressBookIterator it(*mAccess); const PimContact* contact; bool res; for (contact=it.toFirst(); it.current(); ++it) { contact = it.current(); KABC::Addressee addressee; res = mConverter->qtopiaToAddressee( (*contact), addressee ); if ( !addressee.isEmpty() && res ) { addressee.setResource( this ); addressBook()->insertAddressee( addressee ); } } return true; } bool ResourceQtopia::save( Ticket *ticket ) { qDebug("ResourceQtopia::save: %s", fileName().latin1()); mDirWatch.stopScan(); KABC::AddressBook::Iterator it; bool res; for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) { PimContact c; KABC::Addressee addressee = (*it); res = mConverter->addresseeToQtopia( *it, c ); if (res == true) { mAccess->addContact(c); // if (res == false) // qDebug("Unable to append Contact %s", c.fullName().latin1()); } else { qDebug("Unable to convert Addressee %s", addressee.formattedName().latin1()); } } // mAccess->addressBookUpdated(); mDirWatch.startScan(); delete ticket; unlock( fileName() ); return true; } bool ResourceQtopia::lock( const QString &lockfileName ) { qDebug("ResourceQtopia::lock: %s", fileName().latin1()); kdDebug(5700) << "ResourceQtopia::lock()" << endl; QString fn = lockfileName; KURL url(fn); QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); kdDebug(5700) << "-- lock name: " << lockName << endl; if (QFile::exists( lockName )) { qDebug("ResourceOpie::lock: lockfile %s already exists. (Delete it before continuing if nobody else is accessing the resourcefile %s)", lockName.latin1(), fileName().latin1()); 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; #ifndef _WIN32_ result = ::link( QFile::encodeName( mLockUniqueName ), QFile::encodeName( lockName ) ); #endif if ( result == 0 ) { addressBook()->emitAddressBookLocked(); return true; } // TODO: check stat return false; } void ResourceQtopia::unlock( const QString &fileName ) { qDebug("ResourceQtopia::unlock() %s", fileName.latin1()); QString fn = fileName; KURL url(fn); QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); QFile::remove( lockName ); QFile::remove( mLockUniqueName ); addressBook()->emitAddressBookUnlocked(); } void ResourceQtopia::setFileName( const QString &newFileName ) { mDirWatch.stopScan(); mDirWatch.removeFile( fileName() ); Resource::setFileName( newFileName ); mDirWatch.addFile( fileName() ); mDirWatch.startScan(); } void ResourceQtopia::fileChanged() { // There is a small theoretical chance that KDirWatch calls us before // we are fully constructed if (!addressBook()) return; QString text( i18n( "Qtopia resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( fileName() ) ); if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) { load(); addressBook()->emitAddressBookChanged(); } } void ResourceQtopia::removeAddressee( const Addressee &addr ) { } void ResourceQtopia::cleanUp() { unlock( fileName() ); } //US #include "resourceqtopia.moc" diff --git a/kabc/plugins/qtopia/resourceqtopia.h b/kabc/plugins/qtopia/resourceqtopia.h index eace280..ff6350d 100644 --- a/kabc/plugins/qtopia/resourceqtopia.h +++ b/kabc/plugins/qtopia/resourceqtopia.h @@ -1,145 +1,145 @@ /* This file is part of libkabc. Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> 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_RESOURCEOPIE_H #define KABC_RESOURCEOPIE_H #include <kconfig.h> #include <kdirwatch.h> #include <sys/types.h> #include "resource.h" class AddressBookAccess; namespace KABC { //US class FormatPlugin; class ResourceConfigWidget; class QtopiaConverter; /** @internal */ class ResourceQtopia : public Resource { Q_OBJECT public: /** Constructor. @param cfg The config object where custom resource settings are stored. */ - ResourceQtopia( const KConfig *cfg, bool syncable ); + ResourceQtopia( const KConfig *cfg ); /** Construct file resource on file @arg fileName using format @arg formatName. */ - ResourceQtopia( const QString &fileName, bool syncable ); + ResourceQtopia( const QString &fileName ); /** * Destructor. */ ~ResourceQtopia(); /** 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 ); /** * Remove a addressee from its source. * This method is mainly called by KABC::AddressBook. */ virtual void removeAddressee( const Addressee& addr ); /** * Set name of file to be used for saving. */ virtual void setFileName( const QString & ); /** * 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 ); bool lock( const QString &fileName ); void unlock( const QString &fileName ); private: AddressBookAccess* mAccess; QtopiaConverter* mConverter; QString mLockUniqueName; KDirWatch mDirWatch; }; } #endif diff --git a/kabc/plugins/sharpdtm/resourcesharpdtm.cpp b/kabc/plugins/sharpdtm/resourcesharpdtm.cpp index c013f52..2cdf4bf 100644 --- a/kabc/plugins/sharpdtm/resourcesharpdtm.cpp +++ b/kabc/plugins/sharpdtm/resourcesharpdtm.cpp @@ -1,408 +1,406 @@ /* This file is part of libkabc. Copyright (c) 2004 Ulf Schenk 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 <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <qdir.h> #include <qfile.h> #include <qfileinfo.h> #include <qregexp.h> //US #include <qtimer.h> #include <kapplication.h> #include <kconfig.h> #include <kdebug.h> #include <klocale.h> //US #include <ksavefile.h> #include <kstandarddirs.h> #include <kmessagebox.h> #include <sl/slzdb.h> #include <libkdepim/ksyncprofile.h> #include "resourcesharpdtmconfig.h" #include "resourcesharpdtm.h" -#include "syncprefwidget.h" - #include "stdaddressbook.h" #include "sharpdtmconverter.h" //#define ALLOW_LOCKING using namespace KABC; extern "C" { void *init_microkabc_sharpdtm() { - return new KRES::PluginFactory<ResourceSharpDTM,ResourceSharpDTMConfig,SyncPrefWidgetContainer>(); + return new KRES::PluginFactory<ResourceSharpDTM,ResourceSharpDTMConfig>(); } } -ResourceSharpDTM::ResourceSharpDTM( const KConfig *config, bool syncable ) - : Resource( config, syncable ), mConverter (0) +ResourceSharpDTM::ResourceSharpDTM( const KConfig *config ) + : Resource( config ), mConverter (0) { // we can not choose the filename. Therefore use the default to display QString fileName = SlZDataBase::addressbookFileName(); init( fileName ); } ResourceSharpDTM::ResourceSharpDTM( const QString &fileName, bool syncable ) : Resource( 0, syncable ) { init( fileName ); } void ResourceSharpDTM::init( const QString &fileName ) { 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 ); } ResourceSharpDTM::~ResourceSharpDTM() { if (mConverter != 0) delete mConverter; if(mAccess != 0) delete mAccess; } void ResourceSharpDTM::writeConfig( KConfig *config ) { Resource::writeConfig( config ); } Ticket *ResourceSharpDTM::requestSaveTicket() { qDebug("ResourceSharpDTM::requestSaveTicket: %s", fileName().latin1()); if ( !addressBook() ) return 0; #ifdef ALLOW_LOCKING if ( !lock( fileName() ) ) { qDebug("ResourceSharpDTM::requestSaveTicket(): Unable to lock file "); return 0; } #endif return createTicket( this ); } bool ResourceSharpDTM::doOpen() { qDebug("ResourceSharpDTM::doOpen: %s", fileName().latin1()); // the last parameter in the SlZDataBase constructor means "readonly" mAccess = new SlZDataBase(fileName(), SlZDataBase::addressbookItems(), NULL, false); if ( !mAccess ) { qDebug("Unable to load file() %s", fileName().latin1()); return false; } if (mConverter == 0) { mConverter = new SharpDTMConverter(); bool res = mConverter->init(); if ( !res ) { QString msg("Unable to initialize sharp converter. Most likely a problem with the category file"); qDebug(msg); delete mAccess; mAccess = 0; return false; } } return true; } void ResourceSharpDTM::doClose() { qDebug("ResourceSharpDTM::doClose: %s", fileName().latin1()); if(mAccess) { delete mAccess; mAccess = 0; } // it seems so, that deletion of access deletes backend as well //delete backend; return; } bool ResourceSharpDTM::load() { qDebug("ResourceSharpDTM::load: %s", fileName().latin1()); bool res = false; CardId id; for (bool res=mAccess->first(); res == true; res=mAccess->next()) { id = mAccess->cardId(); KABC::Addressee addressee; res = mConverter->sharpToAddressee( id, mAccess, addressee ); if ( !addressee.isEmpty() && res ) { addressee.setResource( this ); addressBook()->insertAddressee( addressee ); } } return true; } bool ResourceSharpDTM::save( Ticket *ticket ) { qDebug("ResourceSharpDTM::save: %s", fileName().latin1()); mDirWatch.stopScan(); KABC::AddressBook::Iterator it; bool res; KABC::Addressee::List changedAddressees; typedef QMap<int,QString> AddresseeMap; AddresseeMap map; CardId id ; for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) { if ( (*it).tempSyncStat() != SYNC_TEMPSTATE_NEW_EXTERNAL ) { QString uid = (*it).originalExternalUID(); bool res; if ( uid.isEmpty() ) id = 0; else id = uid.toUInt(); KABC::Addressee addressee = (*it); if ( (*it).tempSyncStat() == SYNC_TEMPSTATE_ADDED_EXTERNAL ) { res = mAccess->startEditCard(id); if (res == true) { res = mConverter->addresseeToSharp( (*it), mAccess, id ); if (res == true) { res = mAccess->finishEditCard(&id); //(*it).setTempSyncStat( SYNC_TEMPSTATE_NEW_ID ); //(*it).setExternalUID( QString::number( id ) ); //(*it).setOriginalExternalUID( QString::number( id ) ); map.insert(id,(*it).uid()); if (res == false) qDebug("Unable to append Contact: %s", addressee.formattedName().latin1()); } else { qDebug("Unable to convert Addressee: %s", addressee.formattedName().latin1()); mAccess->cancelEditCard(); } } } else if ( (*it).tempSyncStat() == SYNC_TEMPSTATE_DELETE ) { res = mAccess->deleteCard(&id); if ( !res ) qDebug("delete error "); } else if ( (*it).tempSyncStat() != SYNC_TEMPSTATE_NEW_EXTERNAL ) { //changed res = mAccess->startEditCard(id); if (res == true) { res = mConverter->addresseeToSharp( (*it), mAccess, id ); if (res == true) { res = mAccess->finishEditCard(&id); //(*it).setTempSyncStat( SYNC_TEMPSTATE_NEW_CSUM ); map.insert(id,(*it).uid()); if (res == false) qDebug("Unable to append Contact: %s", addressee.formattedName().latin1()); } else { qDebug("Unable to convert Addressee: %s", addressee.formattedName().latin1()); mAccess->cancelEditCard(); } } } } } AddresseeMap::Iterator itam; for ( res=mAccess->first(); res == true; res=mAccess->next()) { id = mAccess->cardId(); int idint = id; itam = map.find( idint ); if ( itam != map.end() ) { KABC::Addressee addressee; res = mConverter->sharpToAddressee( id, mAccess, addressee ); if ( !addressee.isEmpty() && res ) { addressee.setResource( this ); addressee.setUid( itam.data() ); addressee.setTempSyncStat( SYNC_TEMPSTATE_NEW_ID ); addressBook()->insertAddressee( addressee , false ); } } } //US mAccess->save(); mDirWatch.startScan(); delete ticket; unlock( fileName() ); return true; } bool ResourceSharpDTM::lock( const QString &lockfileName ) { #ifdef ALLOW_LOCKING qDebug("ResourceSharpDTM::lock: %s", fileName().latin1()); kdDebug(5700) << "ResourceSharpDTM::lock()" << endl; QString fn = lockfileName; KURL url(fn); QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); kdDebug(5700) << "-- lock name: " << lockName << endl; if (QFile::exists( lockName )) { qDebug("ResourceSharpDTM::lock: lockfile %s already exists. (Delete it before continuing if nobody else is accessing the resourcefile %s)", lockName.latin1(), fileName()); 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; #ifndef _WIN32_ result = ::link( QFile::encodeName( mLockUniqueName ), QFile::encodeName( lockName ) ); #endif if ( result == 0 ) { addressBook()->emitAddressBookLocked(); return true; } // TODO: check stat return false; #else return true; #endif } void ResourceSharpDTM::unlock( const QString &fileName ) { #ifdef ALLOW_LOCKING qDebug("ResourceSharpDTM::unlock() %s", fileName.latin1()); QString fn = fileName; KURL url(fn); QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); QFile::remove( lockName ); QFile::remove( mLockUniqueName ); addressBook()->emitAddressBookUnlocked(); #endif } void ResourceSharpDTM::setFileName( const QString &newFileName ) { mDirWatch.stopScan(); mDirWatch.removeFile( fileName() ); Resource::setFileName( newFileName ); mDirWatch.addFile( fileName() ); mDirWatch.startScan(); } void ResourceSharpDTM::fileChanged() { // There is a small theoretical chance that KDirWatch calls us before // we are fully constructed if (!addressBook()) return; QString text( i18n( "Sharp DTM resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( fileName() ) ); if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) { load(); addressBook()->emitAddressBookChanged(); } } void ResourceSharpDTM::removeAddressee( const Addressee &addr ) { } void ResourceSharpDTM::cleanUp() { unlock( fileName() ); } diff --git a/kabc/plugins/sharpdtm/resourcesharpdtm.h b/kabc/plugins/sharpdtm/resourcesharpdtm.h index f386e69..64d06f9 100644 --- a/kabc/plugins/sharpdtm/resourcesharpdtm.h +++ b/kabc/plugins/sharpdtm/resourcesharpdtm.h @@ -1,146 +1,146 @@ /* This file is part of libkabc. Copyright (c) 2004 Ulf Schenk 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_RESOURCESHARPDTM_H #define KABC_RESOURCESHARPDTM_H #include <kconfig.h> #include <kdirwatch.h> #include <sys/types.h> #include "resource.h" class SlZDataBase; namespace KABC { class ResourceConfigWidget; class SharpDTMConverter; /** @internal */ class ResourceSharpDTM : public Resource { Q_OBJECT public: /** Constructor. @param cfg The config object where custom resource settings are stored. */ - ResourceSharpDTM( const KConfig *cfg, bool syncable ); + ResourceSharpDTM( const KConfig *cfg ); /** Construct file resource on file @arg fileName using format @arg formatName. */ - ResourceSharpDTM( const QString &fileName, bool syncable ); + ResourceSharpDTM( const QString &fileName ); /** * Destructor. */ ~ResourceSharpDTM(); /** 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 ); /** * Remove a addressee from its source. * This method is mainly called by KABC::AddressBook. */ virtual void removeAddressee( const Addressee& addr ); /** * Set name of file to be used for saving. */ virtual void setFileName( const QString & ); /** * 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 ); bool lock( const QString &fileName ); void unlock( const QString &fileName ); private: SlZDataBase* mAccess; SharpDTMConverter* mConverter; QString mLockUniqueName; KDirWatch mDirWatch; }; } #endif |