• Skip to content
  • Skip to link menu
KDE 4.3 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", QLatin1String( "kabc/std.vcf" ) );
00067 }
00068 
00069 QString StdAddressBook::directoryName()
00070 {
00071   return KStandardDirs::locateLocal( "data", QLatin1String( "kabc/stdvcf" ) );
00072 }
00073 
00074 StdAddressBook *StdAddressBook::self()
00075 {
00076   kDebug();
00077 
00078   // delegate to other self() method since the only difference
00079   // was the constructor being used and their only difference is
00080   // what they pass to Private::init()
00081   return self( false );
00082 }
00083 
00084 StdAddressBook *StdAddressBook::self( bool asynchronous )
00085 {
00086   kDebug() << "asynchronous=" << asynchronous;
00087 
00088   if ( !s_gStdAddressBook ) {
00089     s_gStdAddressBook = new StdAddressBook( asynchronous, false );
00090 
00091     kDebug() << "calling init after instance creation";
00092     s_gStdAddressBook->d->init( asynchronous );
00093 
00094     // We don't use a global static here for this reason:
00095     //
00096     // There are problems with the destruction order: The destructor of
00097     // StdAddressBook calls save(), which for LDAP address books, needs KIO
00098     // (more specific: KProtocolInfo) to be still alive. However, with a global
00099     // static, KProtocolInfo is already deleted, and the app will crash.
00100     //
00101     // qAddPostRoutine deletes the objects when the QApplication is destroyed,
00102     // which is earlier than the global statics, so this will work.
00103     qAddPostRoutine( deleteGlobalStdAddressBook );
00104   }
00105 
00106   return s_gStdAddressBook;
00107 }
00108 
00109 StdAddressBook::StdAddressBook()
00110   : AddressBook( QString() ), d( new Private( this ) )
00111 {
00112   kDebug();
00113 
00114   d->init( false );
00115 }
00116 
00117 StdAddressBook::StdAddressBook( bool asynchronous )
00118   : AddressBook( QString() ), d( new Private( this ) )
00119 {
00120   kDebug();
00121 
00122   d->init( asynchronous );
00123 }
00124 
00125 StdAddressBook::StdAddressBook( bool asynchronous, bool doInit )
00126   : AddressBook( QString() ), d( new Private( this ) )
00127 {
00128   kDebug();
00129 
00130   if ( doInit ) {
00131     d->init( asynchronous );
00132   }
00133 }
00134 
00135 StdAddressBook::~StdAddressBook()
00136 {
00137   if ( Private::mAutomaticSave ) {
00138     d->saveAll();
00139   }
00140 
00141   delete d;
00142 }
00143 
00144 void StdAddressBook::Private::init( bool asynchronous )
00145 {
00146   KRES::Manager<Resource> *manager = mParent->resourceManager();
00147 
00148   KRES::Manager<Resource>::ActiveIterator it;
00149   for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) {
00150     (*it)->setAddressBook( mParent );
00151     if ( !(*it)->open() ) {
00152       mParent->error( QString::fromLatin1( "Unable to open resource '%1'!" ).
00153                       arg( (*it)->resourceName() ) );
00154       continue;
00155     }
00156     mParent->connect( *it, SIGNAL( loadingFinished( Resource* ) ),
00157                       mParent, SLOT( resourceLoadingFinished( Resource* ) ) );
00158     mParent->connect( *it, SIGNAL( savingFinished( Resource* ) ),
00159                       mParent, SLOT( resourceSavingFinished( Resource* ) ) );
00160 
00161     mParent->connect( *it, SIGNAL( loadingError( Resource*, const QString& ) ),
00162                       mParent, SLOT( resourceLoadingError( Resource*, const QString& ) ) );
00163     mParent->connect( *it, SIGNAL( savingError( Resource*, const QString& ) ),
00164                       mParent, SLOT( resourceSavingError( Resource*, const QString& ) ) );
00165   }
00166 
00167   Resource *res = mParent->standardResource();
00168   if ( !res ) {
00169     res = manager->createResource( QLatin1String( "file" ) );
00170     if ( res ) {
00171       res->setResourceName( i18n( "Default Address Book" ) );
00172       mParent->addResource( res );
00173     } else {
00174       kDebug() << "No resource available!!!";
00175     }
00176   }
00177 
00178   mParent->setStandardResource( res );
00179   manager->writeConfig();
00180 
00181   if ( asynchronous ) {
00182     mParent->asyncLoad();
00183   } else {
00184     mParent->load();
00185   }
00186 }
00187 
00188 bool StdAddressBook::Private::saveAll()
00189 {
00190   kDebug();
00191   bool ok = true;
00192 
00193   KRES::Manager<Resource>::ActiveIterator it;
00194   KRES::Manager<Resource> *manager = mParent->resourceManager();
00195   for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) {
00196     if ( !(*it)->readOnly() && (*it)->isOpen() ) {
00197       Ticket *ticket = mParent->requestSaveTicket( *it );
00198       if ( !ticket ) {
00199         mParent->error( i18n( "Unable to save to resource '%1'. It is locked.",
00200                               (*it)->resourceName() ) );
00201         return false;
00202       }
00203 
00204       if ( !mParent->AddressBook::save( ticket ) ) {
00205         ok = false;
00206         mParent->releaseSaveTicket( ticket );
00207       }
00208     }
00209   }
00210 
00211   return ok;
00212 }
00213 
00214 bool StdAddressBook::save()
00215 {
00216   kDebug();
00217 
00218   if ( s_gStdAddressBook ) {
00219     return s_gStdAddressBook->d->saveAll();
00220   } else {
00221     return true;
00222   }
00223 }
00224 
00225 void StdAddressBook::close()
00226 {
00227   delete s_gStdAddressBook;
00228   s_gStdAddressBook = 0;
00229 }
00230 
00231 void StdAddressBook::setAutomaticSave( bool enable )
00232 {
00233   Private::mAutomaticSave = enable;
00234 }
00235 
00236 bool StdAddressBook::automaticSave()
00237 {
00238   return Private::mAutomaticSave;
00239 }
00240 
00241 Addressee StdAddressBook::whoAmI() const
00242 {
00243   KConfig _config( QLatin1String( "kabcrc" ) );
00244   KConfigGroup config(&_config, "General" );
00245 
00246   return findByUid( config.readEntry( "WhoAmI" ) );
00247 }
00248 
00249 void StdAddressBook::setWhoAmI( const Addressee &addr )
00250 {
00251   KConfig _config( QLatin1String( "kabcrc" ) );
00252   KConfigGroup config(&_config, "General" );
00253 
00254   config.writeEntry( "WhoAmI", addr.uid() );
00255 }

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
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  • kldap
  • kmime
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.8
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