• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • Contact Us
 

kabc

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

kabc

Skip menu "kabc"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  • kabc
  • kblog
  • kcal
  • kimap
  • kioslave
  •   imap4
  •   mbox
  • kldap
  • kmime
  • kpimidentities
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.6
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal