-rw-r--r-- | kabc/plugins/dir/resourcedir.cpp | 32 | ||||
-rw-r--r-- | kabc/plugins/file/resourcefile.cpp | 16 | ||||
-rw-r--r-- | kabc/plugins/sharpdtm/resourcesharpdtm.cpp | 15 |
3 files changed, 42 insertions, 21 deletions
diff --git a/kabc/plugins/dir/resourcedir.cpp b/kabc/plugins/dir/resourcedir.cpp index fac06a1..7d96cb7 100644 --- a/kabc/plugins/dir/resourcedir.cpp +++ b/kabc/plugins/dir/resourcedir.cpp @@ -38,48 +38,50 @@ $Id$ #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> #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>(); } #ifndef _WIN32_ } #endif ResourceDir::ResourceDir( const KConfig *config, bool syncable ) : Resource( config, syncable ) { QString path; KConfig *cfg = (KConfig *)config; @@ -115,207 +117,207 @@ ResourceDir::ResourceDir( const KConfig *config, bool syncable ) 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 ) { config->setGroup( "Resource_" + identifier() ); Resource::writeConfig( config ); config->writeEntry( "FilePath", mPath ); config->writeEntry( "FileFormat", mFormatName ); } Ticket *ResourceDir::requestSaveTicket() { - kdDebug(5700) << "ResourceDir::requestSaveTicket()" << endl; - if ( !addressBook() ) return 0; + if ( !addressBook() ) return 0; +#ifdef ALLOW_LOCKING if ( !lock( mPath ) ) { - kdDebug(5700) << "ResourceDir::requestSaveTicket(): Unable to lock path '" - << mPath << "'" << endl; + 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() { - 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; mDirWatch.stopScan(); 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(); } mDirWatch.startScan(); delete ticket; +#ifdef ALLOW_LOCKING unlock( mPath ); - +#endif return ok; } bool ResourceDir::lock( const QString &path ) { - kdDebug(5700) << "ResourceDir::lock()" << endl; - +#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" ); - 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 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 ) { mDirWatch.stopScan(); mDirWatch.removeDir( mPath ); mPath = path; mDirWatch.addDir( mPath, true ); mDirWatch.startScan(); //US simulate KDirWatch event //US pathChanged(); } QString ResourceDir::path() const { return mPath; } void ResourceDir::setFormat( const QString &format ) { diff --git a/kabc/plugins/file/resourcefile.cpp b/kabc/plugins/file/resourcefile.cpp index fa6bc49..17263ae 100644 --- a/kabc/plugins/file/resourcefile.cpp +++ b/kabc/plugins/file/resourcefile.cpp @@ -32,48 +32,49 @@ $Id$ #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" #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>(); } #ifndef _WIN32_ } #endif ResourceFile::ResourceFile( const KConfig *config, bool syncable ) : Resource( config, syncable ) , mFormat( 0 ) { QString fileName, formatName, default_fileName; @@ -126,53 +127,54 @@ 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 ) ) { - kdDebug(5700) << "ResourceFile::requestSaveTicket(): Unable to lock file '" - << mFileName << "'" << endl; + qDebug("unablt to lock file "); return 0; } +#endif 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; } @@ -225,112 +227,122 @@ bool ResourceFile::save( Ticket *ticket ) */ //US ToDo: write backupfile mDirWatch.stopScan(); 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 ) ); mDirWatch.startScan(); 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 ) { mDirWatch.stopScan(); mDirWatch.removeFile( mFileName ); mFileName = fileName; mDirWatch.addFile( mFileName ); mDirWatch.startScan(); //US simulate KDirWatch event //US fileChanged(); } QString ResourceFile::fileName() const { return mFileName; } void ResourceFile::setFormat( const QString &format ) { diff --git a/kabc/plugins/sharpdtm/resourcesharpdtm.cpp b/kabc/plugins/sharpdtm/resourcesharpdtm.cpp index 8a795e9..867bc07 100644 --- a/kabc/plugins/sharpdtm/resourcesharpdtm.cpp +++ b/kabc/plugins/sharpdtm/resourcesharpdtm.cpp @@ -33,49 +33,49 @@ $Id$ #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 "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>(); } } ResourceSharpDTM::ResourceSharpDTM( const KConfig *config, bool syncable ) : Resource( config, syncable ), 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 ); } @@ -84,59 +84,60 @@ 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() { - kdDebug(5700) << "ResourceSharpDTM::requestSaveTicket()" << endl; + qDebug("ResourceSharpDTM::requestSaveTicket: %s", fileName().latin1()); if ( !addressBook() ) return 0; +#ifdef ALLOW_LOCKING if ( !lock( fileName() ) ) { - kdDebug(5700) << "ResourceSharpDTM::requestSaveTicket(): Unable to lock file '" - << fileName() << "'" << endl; + 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, true); if ( !mAccess ) { qDebug("Unable to load file() %s", fileName().latin1()); return false; } if (mConverter == 0) { mConverter = new SharpDTMConverter(); bool res = mConverter->init(); if ( !res ) { @@ -219,106 +220,112 @@ bool ResourceSharpDTM::save( Ticket *ticket ) else { qDebug("Unable to convert Addressee: %s", addressee.formattedName().latin1()); mAccess->cancelEditCard(); } } else { qDebug("Unable to add new card. Addressee: %s", addressee.formattedName().latin1()); } } //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(); |