stdaddressbook.cpp
00001 /* 00002 This file is part of libkabc. 00003 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to 00017 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 Boston, MA 02110-1301, USA. 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 TQString StdAddressBook::fileName() 00044 { 00045 return locateLocal( "data", "kabc/std.vcf" ); 00046 } 00047 00048 TQString 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( TQString( "Unable to open resource '%1'!" ).arg( (*it)->resourceName() ) ); 00104 continue; 00105 } 00106 connect( *it, TQT_SIGNAL( loadingFinished( Resource* ) ), 00107 this, TQT_SLOT( resourceLoadingFinished( Resource* ) ) ); 00108 connect( *it, TQT_SIGNAL( savingFinished( Resource* ) ), 00109 this, TQT_SLOT( resourceSavingFinished( Resource* ) ) ); 00110 00111 connect( *it, TQT_SIGNAL( loadingError( Resource*, const TQString& ) ), 00112 this, TQT_SLOT( resourceLoadingError( Resource*, const TQString& ) ) ); 00113 connect( *it, TQT_SIGNAL( savingError( Resource*, const TQString& ) ), 00114 this, TQT_SLOT( resourceSavingError( Resource*, const TQString& ) ) ); 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 // should get const for 4.X 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 }