kabc Library API Documentation

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., 59 Temple Place - Suite 330,
00018     Boston, MA 02111-1307, 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     save();
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::save()
00140 {
00141   kdDebug(5700) << "StdAddressBook::save()" << endl;
00142 
00143   bool ok = true;
00144   AddressBook *ab = self();
00145 
00146   ab->deleteRemovedAddressees();
00147 
00148   KRES::Manager<Resource>::ActiveIterator it;
00149   KRES::Manager<Resource> *manager = ab->resourceManager();
00150   for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) {
00151     if ( !(*it)->readOnly() && (*it)->isOpen() ) {
00152       Ticket *ticket = ab->requestSaveTicket( *it );
00153       if ( !ticket ) {
00154         ab->error( i18n( "Unable to save to resource '%1'. It is locked." )
00155                    .arg( (*it)->resourceName() ) );
00156         return false;
00157       }
00158 
00159       if ( !ab->save( ticket ) ) {
00160         ok = false;
00161         ab->releaseSaveTicket( ticket );
00162       }
00163     }
00164   }
00165 
00166   return ok;
00167 }
00168 
00169 void StdAddressBook::close()
00170 {
00171   addressBookDeleter.destructObject();
00172 }
00173 
00174 void StdAddressBook::setAutomaticSave( bool enable )
00175 {
00176   mAutomaticSave = enable;
00177 }
00178 
00179 bool StdAddressBook::automaticSave()
00180 {
00181   return mAutomaticSave;
00182 }
00183 
00184 // should get const for 4.X
00185 Addressee StdAddressBook::whoAmI()
00186 {
00187   KConfig config( "kabcrc" );
00188   config.setGroup( "General" );
00189 
00190   return findByUid( config.readEntry( "WhoAmI" ) );
00191 }
00192 
00193 void StdAddressBook::setWhoAmI( const Addressee &addr )
00194 {
00195   KConfig config( "kabcrc" );
00196   config.setGroup( "General" );
00197 
00198   config.writeEntry( "WhoAmI", addr.uid() );
00199 }
KDE Logo
This file is part of the documentation for kabc Library Version 3.2.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Mar 4 22:45:22 2004 by doxygen 1.3.6-20040222 written by Dimitri van Heesch, © 1997-2003