• 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 <kconfiggroup.h>
00031 
00032  #include <QCoreApplication>
00033 
00034 #include <stdlib.h>
00035 
00036 using namespace KABC;
00037 
00038 class StdAddressBook::Private
00039 {
00040   public:
00041     Private( StdAddressBook *parent )
00042       : mParent( parent )
00043     {
00044     }
00045 
00046     void init( bool asynchronous );
00047     bool saveAll();
00048 
00049     StdAddressBook *mParent;
00050     static bool mAutomaticSave;
00051 };
00052 
00053 static StdAddressBook *s_gStdAddressBook = 0;
00054 bool StdAddressBook::Private::mAutomaticSave = true;
00055 
00056 static void deleteGlobalStdAddressBook()
00057 {
00058   if ( s_gStdAddressBook ) {
00059     delete s_gStdAddressBook;
00060     s_gStdAddressBook = 0;
00061   }
00062 }
00063 
00064 QString StdAddressBook::fileName()
00065 {
00066   return KStandardDirs::locateLocal( "data", "kabc/std.vcf" );
00067 }
00068 
00069 QString StdAddressBook::directoryName()
00070 {
00071   return KStandardDirs::locateLocal( "data", "kabc/stdvcf" );
00072 }
00073 
00074 StdAddressBook *StdAddressBook::self()
00075 {
00076   kDebug();
00077 
00078   if ( !s_gStdAddressBook ) {
00079     s_gStdAddressBook = new StdAddressBook();
00080 
00081     // We don't use a global static here for two reasons:
00082     //
00083     // 1. The K_GLOBAL_STATIC does not allow two different constructor calls,
00084     //    which we need because there are two self() methods
00085     //
00086     // 2. There are problems with the destruction order: The destructor of
00087     //    StdAddressBook calls save(), which for LDAP address books, needs KIO
00088     //    (more specific: KProtocolInfo) to be still alive. However, with a global
00089     //    static, KProtocolInfo is already deleted, and the app will crash.
00090     //
00091     // qAddPostRoutine deletes the objects when the QApplication is destroyed,
00092     // which is earlier than the global statics, so this will work.
00093     qAddPostRoutine( deleteGlobalStdAddressBook );
00094   }
00095 
00096   return s_gStdAddressBook;
00097 }
00098 
00099 StdAddressBook *StdAddressBook::self( bool asynchronous )
00100 {
00101   kDebug();
00102 
00103   if ( !s_gStdAddressBook ) {
00104     s_gStdAddressBook = new StdAddressBook( asynchronous );
00105 
00106     // See comment in the other self() method for this.
00107     qAddPostRoutine( deleteGlobalStdAddressBook );
00108   }
00109 
00110   return s_gStdAddressBook;
00111 }
00112 
00113 StdAddressBook::StdAddressBook()
00114   : AddressBook( "" ), d( new Private( this ) )
00115 {
00116   kDebug();
00117 
00118   d->init( false );
00119 }
00120 
00121 StdAddressBook::StdAddressBook( bool asynchronous )
00122   : AddressBook( "" ), d( new Private( this ) )
00123 {
00124   kDebug();
00125 
00126   d->init( asynchronous );
00127 }
00128 
00129 StdAddressBook::~StdAddressBook()
00130 {
00131   if ( Private::mAutomaticSave ) {
00132     d->saveAll();
00133   }
00134 
00135   delete d;
00136 }
00137 
00138 void StdAddressBook::Private::init( bool asynchronous )
00139 {
00140   KRES::Manager<Resource> *manager = mParent->resourceManager();
00141 
00142   KRES::Manager<Resource>::ActiveIterator it;
00143   for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) {
00144     (*it)->setAddressBook( mParent );
00145     if ( !(*it)->open() ) {
00146       mParent->error( QString( "Unable to open resource '%1'!" ).arg( (*it)->resourceName() ) );
00147       continue;
00148     }
00149     mParent->connect( *it, SIGNAL( loadingFinished( Resource* ) ),
00150                       mParent, SLOT( resourceLoadingFinished( Resource* ) ) );
00151     mParent->connect( *it, SIGNAL( savingFinished( Resource* ) ),
00152                       mParent, SLOT( resourceSavingFinished( Resource* ) ) );
00153 
00154     mParent->connect( *it, SIGNAL( loadingError( Resource*, const QString& ) ),
00155                       mParent, SLOT( resourceLoadingError( Resource*, const QString& ) ) );
00156     mParent->connect( *it, SIGNAL( savingError( Resource*, const QString& ) ),
00157                       mParent, SLOT( resourceSavingError( Resource*, const QString& ) ) );
00158   }
00159 
00160   Resource *res = mParent->standardResource();
00161   if ( !res ) {
00162     res = manager->createResource( "file" );
00163     if ( res ) {
00164       mParent->addResource( res );
00165     } else {
00166       kDebug() << "No resource available!!!";
00167     }
00168   }
00169 
00170   mParent->setStandardResource( res );
00171   manager->writeConfig();
00172 
00173   if ( asynchronous ) {
00174     mParent->asyncLoad();
00175   } else {
00176     mParent->load();
00177   }
00178 }
00179 
00180 bool StdAddressBook::Private::saveAll()
00181 {
00182   kDebug();
00183   bool ok = true;
00184 
00185   KRES::Manager<Resource>::ActiveIterator it;
00186   KRES::Manager<Resource> *manager = mParent->resourceManager();
00187   for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) {
00188     if ( !(*it)->readOnly() && (*it)->isOpen() ) {
00189       Ticket *ticket = mParent->requestSaveTicket( *it );
00190       if ( !ticket ) {
00191         mParent->error( i18n( "Unable to save to resource '%1'. It is locked.",
00192                               (*it)->resourceName() ) );
00193         return false;
00194       }
00195 
00196       if ( !mParent->AddressBook::save( ticket ) ) {
00197         ok = false;
00198         mParent->releaseSaveTicket( ticket );
00199       }
00200     }
00201   }
00202 
00203   return ok;
00204 }
00205 
00206 bool StdAddressBook::save()
00207 {
00208   kDebug();
00209 
00210   if ( s_gStdAddressBook ) {
00211     return s_gStdAddressBook->d->saveAll();
00212   } else {
00213     return true;
00214   }
00215 }
00216 
00217 void StdAddressBook::close()
00218 {
00219   delete s_gStdAddressBook;
00220   s_gStdAddressBook = 0;
00221 }
00222 
00223 void StdAddressBook::setAutomaticSave( bool enable )
00224 {
00225   Private::mAutomaticSave = enable;
00226 }
00227 
00228 bool StdAddressBook::automaticSave()
00229 {
00230   return Private::mAutomaticSave;
00231 }
00232 
00233 Addressee StdAddressBook::whoAmI() const
00234 {
00235   KConfig _config( "kabcrc" );
00236   KConfigGroup config(&_config, "General" );
00237 
00238   return findByUid( config.readEntry( "WhoAmI" ) );
00239 }
00240 
00241 void StdAddressBook::setWhoAmI( const Addressee &addr )
00242 {
00243   KConfig _config( "kabcrc" );
00244   KConfigGroup config(&_config, "General" );
00245 
00246   config.writeEntry( "WhoAmI", addr.uid() );
00247 }

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