21 #include "stdaddressbook.h"
24 #include "kresources/manager.h"
29 #include <kstandarddirs.h>
30 #include <kconfiggroup.h>
32 #include <QCoreApplication>
38 class StdAddressBook::Private
46 void init(
bool asynchronous );
50 static bool mAutomaticSave;
54 bool StdAddressBook::Private::mAutomaticSave =
true;
56 static void deleteGlobalStdAddressBook()
58 if ( s_gStdAddressBook ) {
59 delete s_gStdAddressBook;
60 s_gStdAddressBook = 0;
66 return KStandardDirs::locateLocal(
"data", QLatin1String(
"kabc/std.vcf" ) );
71 return KStandardDirs::locateLocal(
"data", QLatin1String(
"kabc/stdvcf" ) );
86 kDebug() <<
"asynchronous=" << asynchronous;
88 if ( !s_gStdAddressBook ) {
91 kDebug() <<
"calling init after instance creation";
92 s_gStdAddressBook->d->init( asynchronous );
103 qAddPostRoutine( deleteGlobalStdAddressBook );
106 return s_gStdAddressBook;
109 StdAddressBook::StdAddressBook()
110 :
AddressBook( QString() ), d( new Private( this ) )
117 StdAddressBook::StdAddressBook(
bool asynchronous )
118 :
AddressBook( QString() ), d( new Private( this ) )
122 d->init( asynchronous );
125 StdAddressBook::StdAddressBook(
bool asynchronous,
bool doInit )
126 :
AddressBook( QString() ), d( new Private( this ) )
131 d->init( asynchronous );
137 if ( Private::mAutomaticSave ) {
144 void StdAddressBook::Private::init(
bool asynchronous )
150 ( *it )->setAddressBook( mParent );
151 if ( !( *it )->open() ) {
152 mParent->error( i18n(
"Unable to open resource '%1'.", ( *it )->resourceName() ) );
155 mParent->connect( *it, SIGNAL(loadingFinished(
Resource*)),
156 mParent, SLOT(resourceLoadingFinished(
Resource*)) );
157 mParent->connect( *it, SIGNAL(savingFinished(
Resource*)),
158 mParent, SLOT(resourceSavingFinished(
Resource*)) );
160 mParent->connect( *it, SIGNAL(loadingError(
Resource*,QString)),
161 mParent, SLOT(resourceLoadingError(
Resource*,QString)) );
162 mParent->connect( *it, SIGNAL(savingError(
Resource*,QString)),
163 mParent, SLOT(resourceSavingError(
Resource*,QString)) );
166 Resource *res = mParent->standardResource();
171 mParent->addResource( res );
173 kDebug() <<
"No resource available!!!";
177 mParent->setStandardResource( res );
180 if ( asynchronous ) {
181 mParent->asyncLoad();
187 bool StdAddressBook::Private::saveAll()
195 if ( !( *it )->readOnly() && ( *it )->isOpen() ) {
196 Ticket *ticket = mParent->requestSaveTicket( *it );
198 mParent->error( i18n(
"Unable to save to resource '%1'. It is locked.",
199 ( *it )->resourceName() ) );
203 if ( !mParent->AddressBook::save( ticket ) ) {
205 mParent->releaseSaveTicket( ticket );
217 if ( s_gStdAddressBook ) {
218 return s_gStdAddressBook->d->saveAll();
226 delete s_gStdAddressBook;
227 s_gStdAddressBook = 0;
232 Private::mAutomaticSave = enable;
237 return Private::mAutomaticSave;
242 KConfig _config( QLatin1String(
"kabcrc" ) );
243 KConfigGroup config(&_config,
"General" );
245 return findByUid( config.readEntry(
"WhoAmI" ) );
250 KConfig _config( QLatin1String(
"kabcrc" ) );
251 KConfigGroup config(&_config,
"General" );
253 config.writeEntry(
"WhoAmI", addr.uid() );