• Skip to content
  • Skip to link menu
KDE 4.6 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • KDE Home
  • 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( i18n( "Unable to open resource '%1'.", (*it)->resourceName() ) );
00153       continue;
00154     }
00155     mParent->connect( *it, SIGNAL( loadingFinished( Resource* ) ),
00156                       mParent, SLOT( resourceLoadingFinished( Resource* ) ) );
00157     mParent->connect( *it, SIGNAL( savingFinished( Resource* ) ),
00158                       mParent, SLOT( resourceSavingFinished( Resource* ) ) );
00159 
00160     mParent->connect( *it, SIGNAL( loadingError( Resource*, const QString& ) ),
00161                       mParent, SLOT( resourceLoadingError( Resource*, const QString& ) ) );
00162     mParent->connect( *it, SIGNAL( savingError( Resource*, const QString& ) ),
00163                       mParent, SLOT( resourceSavingError( Resource*, const QString& ) ) );
00164   }
00165 
00166   Resource *res = mParent->standardResource();
00167   if ( !res ) {
00168     res = manager->createResource( QLatin1String( "file" ) );
00169     if ( res ) {
00170       res->setResourceName( i18n( "Default Address Book" ) );
00171       mParent->addResource( res );
00172     } else {
00173       kDebug() << "No resource available!!!";
00174     }
00175   }
00176 
00177   mParent->setStandardResource( res );
00178   manager->writeConfig();
00179 
00180   if ( asynchronous ) {
00181     mParent->asyncLoad();
00182   } else {
00183     mParent->load();
00184   }
00185 }
00186 
00187 bool StdAddressBook::Private::saveAll()
00188 {
00189   kDebug();
00190   bool ok = true;
00191 
00192   KRES::Manager<Resource>::ActiveIterator it;
00193   KRES::Manager<Resource> *manager = mParent->resourceManager();
00194   for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) {
00195     if ( !(*it)->readOnly() && (*it)->isOpen() ) {
00196       Ticket *ticket = mParent->requestSaveTicket( *it );
00197       if ( !ticket ) {
00198         mParent->error( i18n( "Unable to save to resource '%1'. It is locked.",
00199                               (*it)->resourceName() ) );
00200         return false;
00201       }
00202 
00203       if ( !mParent->AddressBook::save( ticket ) ) {
00204         ok = false;
00205         mParent->releaseSaveTicket( ticket );
00206       }
00207     }
00208   }
00209 
00210   return ok;
00211 }
00212 
00213 bool StdAddressBook::save()
00214 {
00215   kDebug();
00216 
00217   if ( s_gStdAddressBook ) {
00218     return s_gStdAddressBook->d->saveAll();
00219   } else {
00220     return true;
00221   }
00222 }
00223 
00224 void StdAddressBook::close()
00225 {
00226   delete s_gStdAddressBook;
00227   s_gStdAddressBook = 0;
00228 }
00229 
00230 void StdAddressBook::setAutomaticSave( bool enable )
00231 {
00232   Private::mAutomaticSave = enable;
00233 }
00234 
00235 bool StdAddressBook::automaticSave()
00236 {
00237   return Private::mAutomaticSave;
00238 }
00239 
00240 Addressee StdAddressBook::whoAmI() const
00241 {
00242   KConfig _config( QLatin1String( "kabcrc" ) );
00243   KConfigGroup config(&_config, "General" );
00244 
00245   return findByUid( config.readEntry( "WhoAmI" ) );
00246 }
00247 
00248 void StdAddressBook::setWhoAmI( const Addressee &addr )
00249 {
00250   KConfig _config( QLatin1String( "kabcrc" ) );
00251   KConfigGroup config(&_config, "General" );
00252 
00253   config.writeEntry( "WhoAmI", addr.uid() );
00254 }

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
  •   contact
  •   kmime
  • kabc
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.7.3
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