stdaddressbook.cpp
/*
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., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include <stdlib.h>
#include <kapplication.h>
#include <kcrash.h>
#include <kdebug.h>
#include <klocale.h>
#include <kresources/manager.h>
#include <ksimpleconfig.h>
#include <kstandarddirs.h>
#include <kstaticdeleter.h>
#include "resource.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()
{
return locateLocal( "data", "kabc/stdvcf" );
}
void StdAddressBook::handleCrash()
{
}
StdAddressBook *StdAddressBook::self()
{
if ( !mSelf )
addressBookDeleter.setObject( mSelf, new StdAddressBook );
return mSelf;
}
StdAddressBook *StdAddressBook::self( bool asynchronous )
{
if ( !mSelf )
addressBookDeleter.setObject( mSelf, new StdAddressBook( asynchronous ) );
return mSelf;
}
StdAddressBook::StdAddressBook()
: AddressBook( "" )
{
kdDebug(5700) << "StdAddressBook::StdAddressBook()" << endl;
init( false );
}
StdAddressBook::StdAddressBook( bool asynchronous )
: AddressBook( "" )
{
kdDebug(5700) << "StdAddressBook::StdAddressBook( bool )" << endl;
init( asynchronous );
}
StdAddressBook::~StdAddressBook()
{
if ( mAutomaticSave )
saveAll();
}
void StdAddressBook::init( bool asynchronous )
{
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() ) );
continue;
}
connect( *it, SIGNAL( loadingFinished( Resource* ) ),
this, SLOT( resourceLoadingFinished( Resource* ) ) );
connect( *it, SIGNAL( savingFinished( Resource* ) ),
this, SLOT( resourceSavingFinished( Resource* ) ) );
connect( *it, SIGNAL( loadingError( Resource*, const QString& ) ),
this, SLOT( resourceLoadingError( Resource*, const QString& ) ) );
connect( *it, SIGNAL( savingError( Resource*, const QString& ) ),
this, SLOT( resourceSavingError( Resource*, const QString& ) ) );
}
Resource *res = standardResource();
if ( !res ) {
res = manager->createResource( "file" );
if ( res )
addResource( res );
else
kdDebug(5700) << "No resource available!!!" << endl;
}
setStandardResource( res );
manager->writeConfig();
if ( asynchronous )
asyncLoad();
else
load();
}
bool StdAddressBook::saveAll()
{
kdDebug(5700) << "StdAddressBook::saveAll()" << endl;
bool ok = true;
deleteRemovedAddressees();
KRES::Manager<Resource>::ActiveIterator it;
KRES::Manager<Resource> *manager = resourceManager();
for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) {
if ( !(*it)->readOnly() && (*it)->isOpen() ) {
Ticket *ticket = requestSaveTicket( *it );
if ( !ticket ) {
error( i18n( "Unable to save to resource '%1'. It is locked." )
.arg( (*it)->resourceName() ) );
return false;
}
if ( !AddressBook::save( ticket ) ) {
ok = false;
releaseSaveTicket( ticket );
}
}
}
return ok;
}
bool StdAddressBook::save()
{
kdDebug(5700) << "StdAddressBook::save()" << endl;
if ( mSelf )
return mSelf->saveAll();
else
return true;
}
void StdAddressBook::close()
{
addressBookDeleter.destructObject();
}
void StdAddressBook::setAutomaticSave( bool enable )
{
mAutomaticSave = enable;
}
bool StdAddressBook::automaticSave()
{
return mAutomaticSave;
}
// should get const for 4.X
Addressee StdAddressBook::whoAmI()
{
KConfig config( "kabcrc" );
config.setGroup( "General" );
return findByUid( config.readEntry( "WhoAmI" ) );
}
void StdAddressBook::setWhoAmI( const Addressee &addr )
{
KConfig config( "kabcrc" );
config.setGroup( "General" );
config.writeEntry( "WhoAmI", addr.uid() );
}
KABC::AddressBook::requestSaveTicket
Ticket * requestSaveTicket(Resource *resource=0)
Requests a ticket for saving the addressbook.
Definition: addressbook.cpp:488
KABC::StdAddressBook
Standard KDE address book.
Definition: stdaddressbook.h:56
locateLocal
QString locateLocal(const char *type, const QString &filename, const KInstance *instance=KGlobal::instance())
KABC::AddressBook::error
void error(const QString &msg)
Shows GUI independent error messages.
Definition: addressbook.cpp:772
KStaticDeleter
KABC::StdAddressBook::automaticSave
static bool automaticSave()
Returns whether the address book is saved at destruction time.
Definition: stdaddressbook.cpp:183
KABC::AddressBook::releaseSaveTicket
void releaseSaveTicket(Ticket *ticket)
Releases the ticket requested previously with requestSaveTicket().
Definition: addressbook.cpp:508
KABC::StdAddressBook::setAutomaticSave
static void setAutomaticSave(bool state)
Sets the automatic save property of the address book.
Definition: stdaddressbook.cpp:178
KABC::StdAddressBook::fileName
static QString fileName()
Returns the default file name for vcard-based addressbook.
Definition: stdaddressbook.cpp:43
KABC::AddressBook::addResource
bool addResource(Resource *resource)
Adds a resource to the address book.
Definition: addressbook.cpp:704
KABC::AddressBook::loadingFinished
void loadingFinished(Resource *resource)
Emitted when the asynchronous loading of one resource has finished after calling asyncLoad().
KABC::StdAddressBook::save
static bool save() KDE_DEPRECATED
Saves the standard address book to disk.
Definition: stdaddressbook.cpp:163
kdDebug
kdbgstream kdDebug(int area=0)
klocale.h
KABC::Ticket
Helper class for handling coordinated save of address books.
Definition: resource.h:37
KABC::StdAddressBook::handleCrash
static void handleCrash() KDE_DEPRECATED
Definition: stdaddressbook.cpp:53
KABC::StdAddressBook::~StdAddressBook
~StdAddressBook()
Destructor.
Definition: stdaddressbook.cpp:89
KABC::StdAddressBook::directoryName
static QString directoryName()
Returns the default directory name for vcard-based addressbook.
Definition: stdaddressbook.cpp:48
KABC::StdAddressBook::setWhoAmI
void setWhoAmI(const Addressee &addr)
Sets the users contact.
Definition: stdaddressbook.cpp:197
KABC::Addressee
address book entry
Definition: addressee.src.h:74
KABC::StdAddressBook::whoAmI
Addressee whoAmI()
Returns the contact, that is associated with the owner of the address book.
Definition: stdaddressbook.cpp:189
KConfig
KConfig::readEntry
QString readEntry(const QString &pKey, const QString &aDefault=QString::null) const
KABC::Addressee::uid
QString uid() const
Return unique identifier.
Definition: addressee.src.cpp:173
KABC::AddressBook::savingFinished
void savingFinished(Resource *resource)
Emitted when the asynchronous saving of one resource has finished after calling asyncSave().
KABC::StdAddressBook::self
static StdAddressBook * self()
Returns the standard addressbook object.
Definition: stdaddressbook.cpp:57
KABC::StdAddressBook::close
static void close()
Closes the address book.
Definition: stdaddressbook.cpp:173
endl
kndbgstream & endl(kndbgstream &s)
KConfig::setGroup
void setGroup(const QString &group)
KABC::AddressBook::save
bool save(Ticket *ticket)
Saves all addressees of one resource synchronously.
Definition: addressbook.cpp:373
KABC::AddressBook::asyncLoad
bool asyncLoad()
Loads all addressees asynchronously.
Definition: addressbook.cpp:354
KConfig::writeEntry
void writeEntry(const QString &pKey, const QString &pValue, bool bPersistent=true, bool bGlobal=false, bool bNLS=false)
KABC::AddressBook
Address Book.
Definition: addressbook.h:43
KABC::AddressBook::findByUid
Addressee findByUid(const QString &uid)
Searches an addressee with the specified unique identifier.
Definition: addressbook.cpp:569
KABC::AddressBook::load
bool load()
Loads all addressees synchronously.
Definition: addressbook.cpp:336

KDE Home | KDE Accessibility Home | Description of Access Keys