summaryrefslogtreecommitdiffabout
path: root/kabc/plugins/opie/resourceopie.cpp
Side-by-side diff
Diffstat (limited to 'kabc/plugins/opie/resourceopie.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--kabc/plugins/opie/resourceopie.cpp302
1 files changed, 149 insertions, 153 deletions
diff --git a/kabc/plugins/opie/resourceopie.cpp b/kabc/plugins/opie/resourceopie.cpp
index aaee801..1222242 100644
--- a/kabc/plugins/opie/resourceopie.cpp
+++ b/kabc/plugins/opie/resourceopie.cpp
@@ -52,2 +52,3 @@ $Id$
#include "stdaddressbook.h"
+#include "syncwidget.h"
@@ -62,3 +63,3 @@ extern "C"
{
- return new KRES::PluginFactory<ResourceOpie,ResourceOpieConfig>();
+ return new KRES::PluginFactory<ResourceOpie,ResourceOpieConfig, KRES::SyncWidget>();
}
@@ -66,4 +67,4 @@ extern "C"
-ResourceOpie::ResourceOpie( const KConfig *config )
- : Resource( config ), mConverter (0)
+ResourceOpie::ResourceOpie( const KConfig *config, bool syncable )
+ : Resource( config, syncable ), mAccess(0), mConverter (0)
{
@@ -74,3 +75,3 @@ ResourceOpie::ResourceOpie( const KConfig *config )
fileName = cfg->readEntry( "FileName", fileName );
-
+
}
@@ -78,3 +79,3 @@ ResourceOpie::ResourceOpie( const KConfig *config )
// qDebug("ResourceOpie::ResourceOpie : %s", fileName.latin1() );
-
+
init( fileName );
@@ -82,4 +83,4 @@ ResourceOpie::ResourceOpie( const KConfig *config )
-ResourceOpie::ResourceOpie( const QString &fileName )
- : Resource( 0 )
+ResourceOpie::ResourceOpie( const QString &fileName, bool syncable )
+ : Resource( 0, syncable )
{
@@ -95,6 +96,6 @@ void ResourceOpie::init( const QString &fileName )
connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) );
-
+
setFileName( fileName );
-
+
}
@@ -105,2 +106,6 @@ ResourceOpie::~ResourceOpie()
delete mConverter;
+
+ if(mAccess != 0)
+ delete mAccess;
+
}
@@ -111,6 +116,3 @@ void ResourceOpie::writeConfig( KConfig *config )
- config->writeEntry( "FileName", mFileName );
-
-// qDebug("ResourceFile::writeConfig format %s, %s", mFileName.latin1(), mFormatName.latin1());
-
+ config->writeEntry( "FileName", fileName() );
}
@@ -121,7 +123,9 @@ Ticket *ResourceOpie::requestSaveTicket()
+ qDebug("ResourceOpie::requestSaveTicket: %s", fileName().latin1());
+
if ( !addressBook() ) return 0;
- if ( !lock( mFileName ) ) {
+ if ( !lock( fileName() ) ) {
kdDebug(5700) << "ResourceOpie::requestSaveTicket(): Unable to lock file '"
- << mFileName << "'" << endl;
+ << fileName() << "'" << endl;
return 0;
@@ -134,58 +138,15 @@ bool ResourceOpie::doOpen()
{
-// qDebug("ResourceOpie::doOpen() %s", mFileName.latin1());
-/*US
- 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;
- }
+ qDebug("ResourceOpie::doOpen: %s", fileName().latin1());
-//US bool ok = mFormat->checkFormat( &file );
- bool ok = true;
-
- file.close();
+ OContactAccessBackend_XML* backend = new OContactAccessBackend_XML( "KDEPim/Pi", fileName() );
+ mAccess = new OContactAccess("KDEPim/Pi", 0l, backend, false);
- return ok;
+ if ( !mAccess ) {
+ qDebug("Unable to load file() %s", fileName().latin1());
+ return false;
}
-*/
- qDebug("ResourceOpie::doOpen() has to be fixed - %s", mFileName.latin1());
- return true;
-}
-void ResourceOpie::doClose()
-{
-// qDebug("ResourceOpie::doClose() %s", mFileName.latin1());
-}
+ mAccess->setReadAhead( 32 ); // Use ReadAhead-Cache if available
-bool ResourceOpie::load()
-{
-// qDebug("ResourceOpie::load() %s", mFileName.latin1());
- kdDebug(5700) << "ResourceOpie::load(): '" << mFileName << "'" << endl;
-
- qDebug("ResourceOpie::load: Try to load file() %s", mFileName.latin1());
-
- OContactAccessBackend_XML* backend = new OContactAccessBackend_XML( "KDEPim/Pi", mFileName );
- OContactAccess* access = new OContactAccess("KDEPim/Pi", 0l, backend, false);
-
- if ( !access ) {
- qDebug("Unable to load file() %s", mFileName.latin1());
- addressBook()->error( i18n( "Unable to load file '%1'." ).arg( mFileName ) );
- return false;
- }
-
- access -> setReadAhead( 32 ); // Use ReadAhead-Cache if available
- bool res = false;
if (mConverter == 0)
@@ -193,3 +154,3 @@ bool ResourceOpie::load()
mConverter = new OpieConverter();
- res = mConverter->init();
+ bool res = mConverter->init();
if ( !res )
@@ -197,4 +158,4 @@ bool ResourceOpie::load()
qDebug("Unable to initialize opie converter. Most likely a problem with the category file");
- addressBook()->error( i18n( "Unable to initialize opie converter. Most likely a problem with the category file" ) );
- delete access;
+ delete mAccess;
+ mAccess = 0;
return false;
@@ -204,4 +165,31 @@ bool ResourceOpie::load()
- OContactAccess::List::Iterator it;
- OContactAccess::List allList = access->allRecords();
+
+ 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 )
@@ -209,7 +197,7 @@ bool ResourceOpie::load()
const OContact c = (*it);
-
+
KABC::Addressee addressee;
-
- res = mConverter->opieToAddressee( c, addressee );
-
+
+ res = mConverter->opieToAddressee( c, addressee );
+
if ( !addressee.isEmpty() && res )
@@ -219,10 +207,6 @@ bool ResourceOpie::load()
}
-
-// qDebug("found %s", c.fullName().latin1());
+
}
-
- delete access;
- // it seems so, that deletion of access deletes backend as well
- //delete backend;
-
+
+
return true;
@@ -232,55 +216,40 @@ bool ResourceOpie::save( Ticket *ticket )
{
- qDebug("ResourceOpie::save() has to be fixed - %s", mFileName.latin1());
-/*US
-
- qDebug("ResourceOpie::save %s", mFileName.latin1());
- kdDebug(5700) << "ResourceOpie::save()" << endl;
-
- // create backup file
- QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() );
-*/
-/*US we use a simpler method to create a backupfile
-
- (void) KSaveFile::backupFile( mFileName, QString::null
- ,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 );
-
- info.close();
- ok = true;
- }
- else {
-
+ qDebug("ResourceOpie::save: %s", fileName().latin1());
+
+
+
+ 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());
+ }
}
-
- if ( !ok )
- addressBook()->error( i18n( "Unable to save file '%1'." ).arg( mFileName ) );
-
+
+ mAccess->save();
+
delete ticket;
- unlock( mFileName );
-
- return ok;
+ unlock( fileName() );
- qDebug("ResourceOpie::save has to be changed");
-*/
return true;
-
+
}
-bool ResourceOpie::lock( const QString &fileName )
+bool ResourceOpie::lock( const QString &lockfileName )
{
+ qDebug("ResourceOpie::lock: %s", fileName().latin1());
+
+
// qDebug("ResourceOpie::lock() %s", fileName.latin1());
@@ -288,3 +257,3 @@ bool ResourceOpie::lock( const QString &fileName )
- QString fn = fileName;
+ QString fn = lockfileName;
@@ -293,6 +262,6 @@ bool ResourceOpie::lock( const QString &fileName )
//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" );
-
- KURL url(fn);
+
+ KURL url(fn);
QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
-
+
kdDebug(5700) << "-- lock name: " << lockName << endl;
@@ -303,4 +272,4 @@ bool ResourceOpie::lock( const QString &fileName )
lockUniqueName = fn + KApplication::randomString( 8 );
-
- url = lockUniqueName;
+
+ url = lockUniqueName;
//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName );
@@ -332,4 +301,4 @@ void ResourceOpie::unlock( const QString &fileName )
{
-// qDebug("ResourceOpie::unlock() %s", fileName.latin1());
-
+ qDebug("ResourceOpie::unlock() %s", fileName.latin1());
+
QString fn = fileName;
@@ -339,5 +308,5 @@ void ResourceOpie::unlock( const QString &fileName )
//US QString lockName = fn + ".lock";
- KURL url(fn);
+ KURL url(fn);
QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
-
+
QFile::remove( lockName );
@@ -347,20 +316,14 @@ void ResourceOpie::unlock( const QString &fileName )
-void ResourceOpie::setFileName( const QString &fileName )
+void ResourceOpie::setFileName( const QString &newFileName )
{
mDirWatch.stopScan();
- mDirWatch.removeFile( mFileName );
-
- mFileName = fileName;
-
- mDirWatch.addFile( mFileName );
+ mDirWatch.removeFile( fileName() );
+
+ Resource::setFileName( newFileName );
+
+ mDirWatch.addFile( fileName() );
mDirWatch.startScan();
-//US simulate KDirWatch event
-//US fileChanged();
}
-QString ResourceOpie::fileName() const
-{
- return mFileName;
-}
@@ -372,4 +335,4 @@ void ResourceOpie::fileChanged()
return;
-
- QString text( i18n( "Opie resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( mFileName ) );
+
+ 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 ) {
@@ -378,3 +341,3 @@ void ResourceOpie::fileChanged()
}
-
+
load();
@@ -391,5 +354,38 @@ void ResourceOpie::cleanUp()
- unlock( mFileName );
+ unlock( fileName() );
+}
+
+
+/**
+ * This method returns the number of elements that are currently in the resource.
+ */
+int ResourceOpie::count() const
+{
+ qDebug("ResourceOpie::count: %x", mAccess);
+
+ if (mAccess != 0)
+ {
+ OContactAccess::List contactList = mAccess->allRecords();
+ return contactList.count();
+ }
+ else
+ return 0;
+}
+
+
+/**
+ * This method removes all elements from the resource!! (Not from the addressbook)
+ */
+bool ResourceOpie::clear()
+{
+ if (mAccess != 0) {
+ mAccess->clear();
+ return true;
+ }
+ else
+ return false;
}
+
+
//US #include "resourceopie.moc"