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 <kapplication.h>
00022 #include <kcrash.h>
00023 #include <kdebug.h>
00024 #include <klocale.h>
00025 #include <ksimpleconfig.h>
00026 #include <kstandarddirs.h>
00027 
00028 #include <signal.h>
00029 
00030 #include "stdaddressbook.h"
00031 
00032 #include "resourcefactory.h"
00033 #include "resourcefile.h"
00034 #include "vcardformatplugin.h"
00035 
00036 using namespace KABC;
00037 
00038 extern "C" {
00039 
00040 static void setSignalHandler( void (*handler)(int) );
00041 
00042 // Crash recovery signal handler
00043 static void signalHandler( int sigId )
00044 {
00045   setSignalHandler( SIG_DFL );
00046   fprintf( stderr, "*** libkabc got signal %d (Exiting)\n", sigId );
00047   // try to cleanup all lock files
00048   StdAddressBook::self()->cleanUp();
00049   ::exit(-1);
00050 }
00051 
00052 // Crash recovery signal handler
00053 static void crashHandler( int sigId )
00054 {
00055   setSignalHandler( SIG_DFL );
00056   fprintf( stderr, "*** libkabc got signal %d (Crashing)\n", sigId );
00057   // try to cleanup all lock files
00058   StdAddressBook::self()->cleanUp();
00059   // Return to DrKonqi.
00060 }
00061 
00062 static void setSignalHandler( void (*handler)(int) )
00063 {
00064   signal( SIGKILL, handler );
00065   signal( SIGTERM, handler );
00066   signal( SIGHUP,  handler );
00067   KCrash::setEmergencySaveFunction( crashHandler );
00068 }
00069 
00070 }
00071 
00072 AddressBook *StdAddressBook::mSelf = 0;
00073 bool StdAddressBook::mAutomaticSave = true;
00074 
00075 QString StdAddressBook::fileName()
00076 {
00077   return locateLocal( "data", "kabc/std.vcf" );
00078 }
00079 
00080 QString StdAddressBook::directoryName()
00081 {
00082   return locateLocal( "data", "kabc/stdvcf" );
00083 }
00084 
00085 AddressBook *StdAddressBook::self()
00086 {
00087   kdDebug(5700) << "StdAddressBook::self()" << endl;
00088 
00089   if ( !mSelf )
00090     mSelf = new StdAddressBook;
00091 
00092   return mSelf;
00093 }
00094 
00095 AddressBook *StdAddressBook::self( bool onlyFastResources )
00096 {
00097   kdDebug(5700) << "StdAddressBook::self()" << endl;
00098 
00099   if ( !mSelf )
00100     mSelf = new StdAddressBook( onlyFastResources );
00101 
00102   return mSelf;
00103 }
00104 
00105 bool StdAddressBook::save()
00106 {
00107   kdDebug(5700) << "StdAddressBook::save()" << endl;
00108 
00109   bool ok = true;
00110   Resource *resource = 0;
00111 
00112   AddressBook *ab = self();
00113 
00114   ab->deleteRemovedAddressees();
00115 
00116   QPtrList<Resource> list = ab->resources();
00117   for ( uint i = 0; i < list.count(); ++i ) {
00118     resource = list.at( i );
00119     if ( !resource->readOnly() ) {
00120       Ticket *ticket = ab->requestSaveTicket( resource );
00121       if ( !ticket ) {
00122         ab->error( i18n( "Unable to save to standard addressbook. It is locked." ) );
00123         return false;
00124       }
00125 
00126       if ( !ab->save( ticket ) )
00127         ok = false;
00128     }
00129   }
00130 
00131   return ok;
00132 }
00133 
00134 StdAddressBook::StdAddressBook()
00135 {
00136   kdDebug(5700) << "StdAddressBook::StdAddressBook()" << endl;
00137 
00138   init( false );
00139 }
00140 
00141 StdAddressBook::StdAddressBook( bool onlyFastResources )
00142 {
00143   kdDebug(5700) << "StdAddressBook::StdAddressBook( bool )" << endl;
00144 
00145   init( onlyFastResources );
00146 }
00147 
00148 StdAddressBook::~StdAddressBook()
00149 {
00150   if ( mAutomaticSave )
00151     save();
00152 }
00153 
00154 void StdAddressBook::init( bool onlyFastResources )
00155 {
00156   KSimpleConfig config( "kabcrc", true );
00157   ResourceFactory *factory = ResourceFactory::self();
00158   config.setGroup( "General" );
00159 
00160   QStringList keys = config.readListEntry( "ResourceKeys" );
00161   QString stdKey = config.readEntry( "Standard" );
00162   for ( QStringList::Iterator it = keys.begin(); it != keys.end(); ++it ) {
00163     config.setGroup( "Resource_" + (*it) );
00164     QString type = config.readEntry( "ResourceType" );
00165 
00166 
00167     if ( onlyFastResources && !config.readBoolEntry( "ResourceIsFast" ) )
00168         continue;
00169 
00170     Resource *resource = factory->resource( type, this, &config );
00171 
00172     if ( !resource ) continue;
00173 
00174     resource->setReadOnly( config.readBoolEntry( "ResourceIsReadOnly" ) );
00175     resource->setFastResource( config.readBoolEntry( "ResourceIsFast" ) );
00176     resource->setName( config.readEntry( "ResourceName" ).latin1() );
00177 
00178     if ( !addResource( resource ) ) {
00179       delete resource;
00180       continue;
00181     }
00182 
00183     if ( stdKey == (*it) )
00184       setStandardResource( resource );
00185   }
00186 
00187   QPtrList<Resource> list = resources();
00188   if ( list.count() == 0 ) {  // default resource
00189     kdDebug(5700) << "StdAddressBook(): using default resource" << endl;
00190 
00191     Resource *resource = new ResourceFile( this, fileName(),
00192                                            new VCardFormatPlugin );
00193     resource->setReadOnly( false );
00194     resource->setFastResource( true );
00195 
00196     if ( !addResource( resource ) ) delete resource;
00197 
00198     setStandardResource( resource );
00199   }
00200 
00201   load();
00202 
00203   setSignalHandler( signalHandler );
00204 }
00205 
00206 void StdAddressBook::close()
00207 {
00208   delete mSelf;
00209   mSelf = 0;
00210 }
00211 
00212 void StdAddressBook::setAutomaticSave( bool enable )
00213 {
00214   mAutomaticSave = enable;
00215 }
00216 
00217 bool StdAddressBook::automaticSave()
00218 {
00219   return mAutomaticSave;
00220 }
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.0.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Oct 8 12:22:08 2003 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2001