-rw-r--r-- | kabc/stdaddressbook.cpp | 249 |
1 files changed, 249 insertions, 0 deletions
diff --git a/kabc/stdaddressbook.cpp b/kabc/stdaddressbook.cpp new file mode 100644 index 0000000..f9fc70b --- a/dev/null +++ b/kabc/stdaddressbook.cpp @@ -0,0 +1,249 @@ +/* + 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 DESKTOP_VERSION +#include <qpe/global.h> +#else + +#include <qdir.h> +#endif +#ifdef KAB_EMBEDDED + +#include <stdlib.h> +#else //KAB_EMBEDDED + +#include <stdlib.h> + +//US#include <kresources/manager.h> +#include <kapplication.h> +#include <kcrash.h> +#include <ksimpleconfig.h> + + +#endif //KAB_EMBEDDED + +#include "resource.h" +#include <kresources/manager.h> +#include <kdebug.h> +#include <klocale.h> +#include <kstaticdeleter.h> +#include <kstandarddirs.h> + +#include "stdaddressbook.h" + +using namespace KABC; + +StdAddressBook *StdAddressBook::mSelf = 0; +bool StdAddressBook::mAutomaticSave = true; + +static KStaticDeleter<StdAddressBook> addressBookDeleter; + +QString StdAddressBook::fileName() +{ + return locateLocal( "data", "kabc/std.vcf" ); +} + +QString StdAddressBook::directoryName() +{ + qDebug("StdAddressBook::directoryName() WILL FAIL OR NOT" ); + return locateLocal( "data", "kabc/stdvcf" ); +} + +void StdAddressBook::handleCrash() +{ + StdAddressBook::self()->cleanUp(); +} + +StdAddressBook *StdAddressBook::self() +{ + + if ( !mSelf ) + { + QString appdir = StdAddressBook::setTempAppDir(); + + kdDebug(5700) << "StdAddressBook::self()" << endl; +// US im am not sure why I have to use the other format here?? +#ifdef KAB_EMBEDDED + mSelf = addressBookDeleter.setObject( new StdAddressBook ); +#else //KAB_EMBEDDED + addressBookDeleter.setObject( mSelf, new StdAddressBook ); +#endif //KAB_EMBEDDED + KStandardDirs::setAppDir( appdir ); + } + + return mSelf; +} + +QString StdAddressBook::setTempAppDir() +{ + QString appDIR = KStandardDirs::appDir(); +#ifdef DESKTOP_VERSION + QString appdir = QDir::homeDirPath(); + if ( appdir.right(1) == "\\" || appdir.right(1) == "/" ) + appdir += "kaddressbook/"; + else + appdir += "/kaddressbook/"; + KStandardDirs::setAppDir( QDir::convertSeparators( appdir )); +#else + KStandardDirs::setAppDir( Global::applicationFileName( "kaddressbook", "" ) ); +#endif + + return appDIR; +} +StdAddressBook *StdAddressBook::self( bool onlyFastResources ) +{ + + if ( !mSelf ) + { + QString appdir =StdAddressBook::setTempAppDir(); +#ifdef KAB_EMBEDDED + mSelf = addressBookDeleter.setObject( new StdAddressBook( onlyFastResources ) ); +#else //KAB_EMBEDDED + addressBookDeleter.setObject( mSelf, new StdAddressBook( onlyFastResources ) ); +#endif //KAB_EMBEDDED + KStandardDirs::setAppDir( appdir ); + } + return mSelf; +} + +StdAddressBook::StdAddressBook() +//US : AddressBook( "kabcrc" ) + : AddressBook( locateLocal( "config", "kabcrc") ) +{ + + init( false ); +} + +StdAddressBook::StdAddressBook( bool onlyFastResources ) +//US : AddressBook( "kabcrc" ) + : AddressBook( locateLocal( "config", "kabcrc") ) +{ + + init( onlyFastResources ); +} + +StdAddressBook::~StdAddressBook() +{ + if ( mAutomaticSave ) + save(); +} + +void StdAddressBook::init( bool ) +{ + KRES::Manager<Resource> *manager = resourceManager(); + KRES::Manager<Resource>::ActiveIterator it; + + for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) { + (*it)->setAddressBook( this ); + if ( !(*it)->open() ) + error( QString( "Unable to open resource '%1'!" ).arg( (*it)->resourceName() ) ); + } + + Resource *res = standardResource(); + if ( !res ) { + res = manager->createResource( "file" ); + if ( res ) + { + addResource( res ); + } + else + kdDebug(5700) << "No resource available!!!" << endl; + } + + setStandardResource( res ); + manager->writeConfig(); + + load(); +} + +bool StdAddressBook::save() +{ + kdDebug(5700) << "StdAddressBook::save()" << endl; + + bool ok = true; + AddressBook *ab = self(); + + ab->deleteRemovedAddressees(); + + KRES::Manager<Resource>::ActiveIterator it; + KRES::Manager<Resource> *manager = ab->resourceManager(); + for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) { + if ( !(*it)->readOnly() && (*it)->isOpen() ) { + Ticket *ticket = ab->requestSaveTicket( *it ); +// qDebug("StdAddressBook::save '%s'", (*it)->resourceName().latin1() ); + if ( !ticket ) { + ab->error( i18n( "Unable to save to resource '%1'. It is locked." ) + .arg( (*it)->resourceName() ) ); + return false; + } + + if ( !ab->save( ticket ) ) + ok = false; + } + } + + return ok; +} + +void StdAddressBook::close() +{ +//US destructObject is not defined on my system???. Is setObject(0) the same ??? +#ifndef KAB_EMBEDDED + addressBookDeleter.destructObject(); +#else //KAB_EMBEDDED + addressBookDeleter.setObject(0); +#endif //KAB_EMBEDDED + +} + +void StdAddressBook::setAutomaticSave( bool enable ) +{ + mAutomaticSave = enable; +} + +bool StdAddressBook::automaticSave() +{ + return mAutomaticSave; +} + +// should get const for 4.X +Addressee StdAddressBook::whoAmI() +{ +//US KConfig config( "kabcrc" ); + KConfig config( locateLocal("config", "kabcrc") ); + config.setGroup( "General" ); + + return findByUid( config.readEntry( "WhoAmI" ) ); +} + +void StdAddressBook::setWhoAmI( const Addressee &addr ) +{ +//US KConfig config( "kabcrc" ); + KConfig config( locateLocal("config", "kabcrc") ); + config.setGroup( "General" ); + + config.writeEntry( "WhoAmI", addr.uid() ); +} |