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 AddressBook *StdAddressBook::mSelf = 0;
00039 bool StdAddressBook::mAutomaticSave = true;
00040 
00041 QString StdAddressBook::fileName()
00042 {
00043   return locateLocal( "data", "kabc/std.vcf" );
00044 }
00045 
00046 QString StdAddressBook::directoryName()
00047 {
00048   return locateLocal( "data", "kabc/stdvcf" );
00049 }
00050 
00051 void StdAddressBook::handleCrash()
00052 {
00053   StdAddressBook::self()->cleanUp();
00054 }
00055 
00056 AddressBook *StdAddressBook::self()
00057 {
00058   kdDebug(5700) << "StdAddressBook::self()" << endl;
00059 
00060   if ( !mSelf )
00061     mSelf = new StdAddressBook;
00062 
00063   return mSelf;
00064 }
00065 
00066 AddressBook *StdAddressBook::self( bool onlyFastResources )
00067 {
00068   kdDebug(5700) << "StdAddressBook::self()" << endl;
00069 
00070   if ( !mSelf )
00071     mSelf = new StdAddressBook( onlyFastResources );
00072 
00073   return mSelf;
00074 }
00075 
00076 bool StdAddressBook::save()
00077 {
00078   kdDebug(5700) << "StdAddressBook::save()" << endl;
00079 
00080   bool ok = true;
00081   Resource *resource = 0;
00082 
00083   AddressBook *ab = self();
00084 
00085   ab->deleteRemovedAddressees();
00086 
00087   QPtrList<Resource> list = ab->resources();
00088   for ( uint i = 0; i < list.count(); ++i ) {
00089     resource = list.at( i );
00090     if ( !resource->readOnly() ) {
00091       Ticket *ticket = ab->requestSaveTicket( resource );
00092       if ( !ticket ) {
00093         ab->error( i18n( "Unable to save to standard addressbook. It is locked." ) );
00094         return false;
00095       }
00096 
00097       if ( !ab->save( ticket ) )
00098         ok = false;
00099     }
00100   }
00101 
00102   return ok;
00103 }
00104 
00105 StdAddressBook::StdAddressBook()
00106 {
00107   kdDebug(5700) << "StdAddressBook::StdAddressBook()" << endl;
00108 
00109   init( false );
00110 }
00111 
00112 StdAddressBook::StdAddressBook( bool onlyFastResources )
00113 {
00114   kdDebug(5700) << "StdAddressBook::StdAddressBook( bool )" << endl;
00115 
00116   init( onlyFastResources );
00117 }
00118 
00119 StdAddressBook::~StdAddressBook()
00120 {
00121   if ( mAutomaticSave )
00122     save();
00123 }
00124 
00125 void StdAddressBook::init( bool onlyFastResources )
00126 {
00127   KSimpleConfig config( "kabcrc", true );
00128   ResourceFactory *factory = ResourceFactory::self();
00129   config.setGroup( "General" );
00130 
00131   QStringList keys = config.readListEntry( "ResourceKeys" );
00132   QString stdKey = config.readEntry( "Standard" );
00133   for ( QStringList::Iterator it = keys.begin(); it != keys.end(); ++it ) {
00134     config.setGroup( "Resource_" + (*it) );
00135     QString type = config.readEntry( "ResourceType" );
00136 
00137 
00138     if ( onlyFastResources && !config.readBoolEntry( "ResourceIsFast" ) )
00139         continue;
00140 
00141     Resource *resource = factory->resource( type, this, &config );
00142 
00143     if ( !resource ) continue;
00144 
00145     resource->setReadOnly( config.readBoolEntry( "ResourceIsReadOnly" ) );
00146     resource->setFastResource( config.readBoolEntry( "ResourceIsFast" ) );
00147     resource->setName( config.readEntry( "ResourceName" ).latin1() );
00148 
00149     if ( !addResource( resource ) ) {
00150       delete resource;
00151       continue;
00152     }
00153 
00154     if ( stdKey == (*it) )
00155       setStandardResource( resource );
00156   }
00157 
00158   QPtrList<Resource> list = resources();
00159   if ( list.count() == 0 ) {  // default resource
00160     kdDebug(5700) << "StdAddressBook(): using default resource" << endl;
00161 
00162     Resource *resource = new ResourceFile( this, fileName(),
00163                                            new VCardFormatPlugin );
00164     resource->setReadOnly( false );
00165     resource->setFastResource( true );
00166 
00167     if ( !addResource( resource ) ) delete resource;
00168 
00169     setStandardResource( resource );
00170   }
00171 
00172   load();
00173 }
00174 
00175 void StdAddressBook::close()
00176 {
00177   delete mSelf;
00178   mSelf = 0;
00179 }
00180 
00181 void StdAddressBook::setAutomaticSave( bool enable )
00182 {
00183   mAutomaticSave = enable;
00184 }
00185 
00186 bool StdAddressBook::automaticSave()
00187 {
00188   return mAutomaticSave;
00189 }
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Feb 27 22:16:08 2005 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001