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 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   kdDebug(5700) << "StdAddressBook::self()" << endl;
00060 
00061   if ( !mSelf )
00062     addressBookDeleter.setObject( mSelf, new StdAddressBook );
00063 
00064   return mSelf;
00065 }
00066 
00067 StdAddressBook *StdAddressBook::self( bool asynchronous )
00068 {
00069   kdDebug(5700) << "StdAddressBook::self()" << endl;
00070 
00071   if ( !mSelf )
00072     addressBookDeleter.setObject( mSelf, new StdAddressBook( asynchronous ) );
00073 
00074   return mSelf;
00075 }
00076 
00077 StdAddressBook::StdAddressBook()
00078   : AddressBook( "" )
00079 {
00080   kdDebug(5700) << "StdAddressBook::StdAddressBook()" << endl;
00081 
00082   init( false );
00083 }
00084 
00085 StdAddressBook::StdAddressBook( bool asynchronous )
00086   : AddressBook( "" )
00087 {
00088   kdDebug(5700) << "StdAddressBook::StdAddressBook( bool )" << endl;
00089 
00090   init( asynchronous );
00091 }
00092 
00093 StdAddressBook::~StdAddressBook()
00094 {
00095   if ( mAutomaticSave )
00096     saveAll();
00097 }
00098 
00099 void StdAddressBook::init( bool asynchronous )
00100 {
00101   KRES::Manager<Resource> *manager = resourceManager();
00102 
00103   KRES::Manager<Resource>::ActiveIterator it;
00104   for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) {
00105     (*it)->setAddressBook( this );
00106     if ( !(*it)->open() ) {
00107       error( QString( "Unable to open resource '%1'!" ).arg( (*it)->resourceName() ) );
00108       continue;
00109     }
00110     connect( *it, SIGNAL( loadingFinished( Resource* ) ),
00111              this, SLOT( resourceLoadingFinished( Resource* ) ) );
00112     connect( *it, SIGNAL( savingFinished( Resource* ) ),
00113              this, SLOT( resourceSavingFinished( Resource* ) ) );
00114 
00115     connect( *it, SIGNAL( loadingError( Resource*, const QString& ) ),
00116              this, SLOT( resourceLoadingError( Resource*, const QString& ) ) );
00117     connect( *it, SIGNAL( savingError( Resource*, const QString& ) ),
00118              this, SLOT( resourceSavingError( Resource*, const QString& ) ) );
00119   }
00120 
00121   Resource *res = standardResource();
00122   if ( !res ) {
00123     res = manager->createResource( "file" );
00124     if ( res )
00125       addResource( res );
00126     else
00127       kdDebug(5700) << "No resource available!!!" << endl;
00128   }
00129 
00130   setStandardResource( res );
00131   manager->writeConfig();
00132 
00133   if ( asynchronous )
00134     asyncLoad();
00135   else
00136     load();
00137 }
00138 
00139 bool StdAddressBook::saveAll()
00140 {
00141   kdDebug(5700) << "StdAddressBook::saveAll()" << endl;
00142   bool ok = true;
00143 
00144   deleteRemovedAddressees();
00145 
00146   KRES::Manager<Resource>::ActiveIterator it;
00147   KRES::Manager<Resource> *manager = resourceManager();
00148   for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) {
00149     if ( !(*it)->readOnly() && (*it)->isOpen() ) {
00150       Ticket *ticket = requestSaveTicket( *it );
00151       if ( !ticket ) {
00152         error( i18n( "Unable to save to resource '%1'. It is locked." )
00153                    .arg( (*it)->resourceName() ) );
00154         return false;
00155       }
00156 
00157       if ( !AddressBook::save( ticket ) ) {
00158         ok = false;
00159         releaseSaveTicket( ticket );
00160       }
00161     }
00162   }
00163 
00164   return ok;
00165 }
00166 
00167 bool StdAddressBook::save()
00168 {
00169   kdDebug(5700) << "StdAddressBook::save()" << endl;
00170 
00171   if ( mSelf ) 
00172     return mSelf->saveAll();
00173   else
00174     return true;  
00175 }
00176 
00177 void StdAddressBook::close()
00178 {
00179   addressBookDeleter.destructObject();
00180 }
00181 
00182 void StdAddressBook::setAutomaticSave( bool enable )
00183 {
00184   mAutomaticSave = enable;
00185 }
00186 
00187 bool StdAddressBook::automaticSave()
00188 {
00189   return mAutomaticSave;
00190 }
00191 
00192 // should get const for 4.X
00193 Addressee StdAddressBook::whoAmI()
00194 {
00195   KConfig config( "kabcrc" );
00196   config.setGroup( "General" );
00197 
00198   return findByUid( config.readEntry( "WhoAmI" ) );
00199 }
00200 
00201 void StdAddressBook::setWhoAmI( const Addressee &addr )
00202 {
00203   KConfig config( "kabcrc" );
00204   config.setGroup( "General" );
00205 
00206   config.writeEntry( "WhoAmI", addr.uid() );
00207 }
KDE Home | KDE Accessibility Home | Description of Access Keys