-rw-r--r-- | kabc/plugins/qtopia/resourceqtopia.cpp | 95 |
1 files changed, 35 insertions, 60 deletions
diff --git a/kabc/plugins/qtopia/resourceqtopia.cpp b/kabc/plugins/qtopia/resourceqtopia.cpp index dc88272..4a35f19 100644 --- a/kabc/plugins/qtopia/resourceqtopia.cpp +++ b/kabc/plugins/qtopia/resourceqtopia.cpp @@ -40,50 +40,48 @@ $Id$ #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 "syncwidget.h" +#include "syncprefwidget.h" #include "resourceqtopia.h" using namespace KABC; extern "C" { void *init_microkabc_qtopia() { - return new KRES::PluginFactory<ResourceQtopia,ResourceQtopiaConfig, KRES::SyncWidget>(); + return new KRES::PluginFactory<ResourceQtopia,ResourceQtopiaConfig, SyncPrefWidget>(); } } ResourceQtopia::ResourceQtopia( const KConfig *config, bool syncable ) : Resource( config, syncable ), 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 ) { -// qDebug("ResourceFile::ResourceFile : 3 %s, %s", fileName.latin1(), formatName.latin1()); 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 ); } @@ -97,51 +95,53 @@ ResourceQtopia::~ResourceQtopia() 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; } - bool res = false; + if (mConverter == 0) { mConverter = new QtopiaConverter(); - res = mConverter->init(); + 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; } @@ -180,129 +180,104 @@ bool ResourceQtopia::load() if ( !addressee.isEmpty() && res ) { addressee.setResource( this ); addressBook()->insertAddressee( addressee ); } } return true; } bool ResourceQtopia::save( Ticket *ticket ) { -/*US -// qDebug("ResourceFile::save format %s, %s", mFileName.latin1(), mFormatName.latin1()); - kdDebug(5700) << "ResourceQtopia::save()" << endl; - - // create backup file - QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() ); + qDebug("ResourceQtopia::save: %s", fileName().latin1()); -/*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 -//US ToDo: write backupfile - QFile info; - info.setName( mFileName ); - bool ok = info.open( IO_WriteOnly ); - if ( ok ) { -//US mFormat->saveAll( addressBook(), this, &info ); + KABC::AddressBook::Iterator it; + bool res; - info.close(); - ok = true; - } - else { + 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()); + } } - if ( !ok ) - addressBook()->error( i18n( "Unable to save file '%1'." ).arg( mFileName ) ); +// mAccess->addressBookUpdated(); delete ticket; - unlock( mFileName ); - - return ok; - - qDebug("ResourceQtopia::save has to be changed"); - - - access->save(); - -*/ + unlock( fileName() ); return true; } -bool ResourceQtopia::lock( const QString &fileName ) +bool ResourceQtopia::lock( const QString &lockfileName ) { - kdDebug(5700) << "ResourceQtopia::lock()" << endl; + qDebug("ResourceQtopia::lock: %s", fileName().latin1()); - QString fn = fileName; + kdDebug(5700) << "ResourceQtopia::lock()" << endl; -//US change the implementation how the lockfilename is getting created -//US fn.replace( QRegExp("/"), "_" ); -//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); + 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 )) 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 = ::link( QFile::encodeName( mLockUniqueName ), + 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; -//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 ResourceQtopia::setFileName( const QString &newFileName ) { mDirWatch.stopScan(); mDirWatch.removeFile( fileName() ); |