stdaddressbook.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <stdlib.h>
00022
00023 #include <kapplication.h>
00024 #include <kcrash.h>
00025 #include <kdebug.h>
00026 #include <klocale.h>
00027 #include <kresources/manager.h>
00028 #include <ksimpleconfig.h>
00029 #include <kstandarddirs.h>
00030 #include <kstaticdeleter.h>
00031
00032 #include "resource.h"
00033
00034 #include "stdaddressbook.h"
00035
00036 using namespace KABC;
00037
00038 StdAddressBook *StdAddressBook::mSelf = 0;
00039 bool StdAddressBook::mAutomaticSave = true;
00040
00041 static KStaticDeleter<StdAddressBook> addressBookDeleter;
00042
00043 QString StdAddressBook::fileName()
00044 {
00045 return locateLocal( "data", "kabc/std.vcf" );
00046 }
00047
00048 QString StdAddressBook::directoryName()
00049 {
00050 return locateLocal( "data", "kabc/stdvcf" );
00051 }
00052
00053 void StdAddressBook::handleCrash()
00054 {
00055 }
00056
00057 StdAddressBook *StdAddressBook::self()
00058 {
00059 if ( !mSelf )
00060 addressBookDeleter.setObject( mSelf, new StdAddressBook );
00061
00062 return mSelf;
00063 }
00064
00065 StdAddressBook *StdAddressBook::self( bool asynchronous )
00066 {
00067 if ( !mSelf )
00068 addressBookDeleter.setObject( mSelf, new StdAddressBook( asynchronous ) );
00069
00070 return mSelf;
00071 }
00072
00073 StdAddressBook::StdAddressBook()
00074 : AddressBook( "" )
00075 {
00076 kdDebug(5700) << "StdAddressBook::StdAddressBook()" << endl;
00077
00078 init( false );
00079 }
00080
00081 StdAddressBook::StdAddressBook( bool asynchronous )
00082 : AddressBook( "" )
00083 {
00084 kdDebug(5700) << "StdAddressBook::StdAddressBook( bool )" << endl;
00085
00086 init( asynchronous );
00087 }
00088
00089 StdAddressBook::~StdAddressBook()
00090 {
00091 if ( mAutomaticSave )
00092 saveAll();
00093 }
00094
00095 void StdAddressBook::init( bool asynchronous )
00096 {
00097 KRES::Manager<Resource> *manager = resourceManager();
00098
00099 KRES::Manager<Resource>::ActiveIterator it;
00100 for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) {
00101 (*it)->setAddressBook( this );
00102 if ( !(*it)->open() ) {
00103 error( QString( "Unable to open resource '%1'!" ).arg( (*it)->resourceName() ) );
00104 continue;
00105 }
00106 connect( *it, SIGNAL( loadingFinished( Resource* ) ),
00107 this, SLOT( resourceLoadingFinished( Resource* ) ) );
00108 connect( *it, SIGNAL( savingFinished( Resource* ) ),
00109 this, SLOT( resourceSavingFinished( Resource* ) ) );
00110
00111 connect( *it, SIGNAL( loadingError( Resource*, const QString& ) ),
00112 this, SLOT( resourceLoadingError( Resource*, const QString& ) ) );
00113 connect( *it, SIGNAL( savingError( Resource*, const QString& ) ),
00114 this, SLOT( resourceSavingError( Resource*, const QString& ) ) );
00115 }
00116
00117 Resource *res = standardResource();
00118 if ( !res ) {
00119 res = manager->createResource( "file" );
00120 if ( res )
00121 addResource( res );
00122 else
00123 kdDebug(5700) << "No resource available!!!" << endl;
00124 }
00125
00126 setStandardResource( res );
00127 manager->writeConfig();
00128
00129 if ( asynchronous )
00130 asyncLoad();
00131 else
00132 load();
00133 }
00134
00135 bool StdAddressBook::saveAll()
00136 {
00137 kdDebug(5700) << "StdAddressBook::saveAll()" << endl;
00138 bool ok = true;
00139
00140 deleteRemovedAddressees();
00141
00142 KRES::Manager<Resource>::ActiveIterator it;
00143 KRES::Manager<Resource> *manager = resourceManager();
00144 for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) {
00145 if ( !(*it)->readOnly() && (*it)->isOpen() ) {
00146 Ticket *ticket = requestSaveTicket( *it );
00147 if ( !ticket ) {
00148 error( i18n( "Unable to save to resource '%1'. It is locked." )
00149 .arg( (*it)->resourceName() ) );
00150 return false;
00151 }
00152
00153 if ( !AddressBook::save( ticket ) ) {
00154 ok = false;
00155 releaseSaveTicket( ticket );
00156 }
00157 }
00158 }
00159
00160 return ok;
00161 }
00162
00163 bool StdAddressBook::save()
00164 {
00165 kdDebug(5700) << "StdAddressBook::save()" << endl;
00166
00167 if ( mSelf )
00168 return mSelf->saveAll();
00169 else
00170 return true;
00171 }
00172
00173 void StdAddressBook::close()
00174 {
00175 addressBookDeleter.destructObject();
00176 }
00177
00178 void StdAddressBook::setAutomaticSave( bool enable )
00179 {
00180 mAutomaticSave = enable;
00181 }
00182
00183 bool StdAddressBook::automaticSave()
00184 {
00185 return mAutomaticSave;
00186 }
00187
00188
00189 Addressee StdAddressBook::whoAmI()
00190 {
00191 KConfig config( "kabcrc" );
00192 config.setGroup( "General" );
00193
00194 return findByUid( config.readEntry( "WhoAmI" ) );
00195 }
00196
00197 void StdAddressBook::setWhoAmI( const Addressee &addr )
00198 {
00199 KConfig config( "kabcrc" );
00200 config.setGroup( "General" );
00201
00202 config.writeEntry( "WhoAmI", addr.uid() );
00203 }
|