libkdepim

kabcresourcecached.cpp

00001 /*
00002     This file is part of libkdepim.
00003     Copyright (c) 2004 Tobias Koenig <tokoe@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 <qfile.h>
00022 
00023 #include <kabc/vcardconverter.h>
00024 #include <kdebug.h>
00025 #include <klocale.h>
00026 #include <kstandarddirs.h>
00027 
00028 #include "kabcresourcecached.h"
00029 
00030 using namespace KABC;
00031 
00032 ResourceCached::ResourceCached( const KConfig *config )
00033   : KABC::Resource( config ), mIdMapper( "kabc/uidmaps/" )
00034 {
00035 }
00036 
00037 ResourceCached::~ResourceCached()
00038 {
00039 }
00040 
00041 void ResourceCached::writeConfig( KConfig *config )
00042 {
00043   KABC::Resource::writeConfig( config );
00044 }
00045 
00046 void ResourceCached::insertAddressee( const Addressee &addr )
00047 {
00048   if ( !mAddrMap.contains( addr.uid() ) ) { // new contact
00049     if ( mDeletedAddressees.contains( addr.uid() ) ) {
00050       // it was first removed, then added, so it's an update...
00051       mDeletedAddressees.remove( addr.uid() );
00052 
00053       mAddrMap.insert( addr.uid(), addr );
00054       mChangedAddressees.insert( addr.uid(), addr );
00055       return;
00056     }
00057 
00058     mAddrMap.insert( addr.uid(), addr );
00059     mAddedAddressees.insert( addr.uid(), addr );
00060   } else {
00061     KABC::Addressee oldAddressee = mAddrMap.find( addr.uid() ).data();
00062     if ( oldAddressee != addr ) {
00063       mAddrMap.remove( addr.uid() );
00064       mAddrMap.insert( addr.uid(), addr );
00065       mChangedAddressees.insert( addr.uid(), addr );
00066     }
00067   }
00068 }
00069 
00070 void ResourceCached::removeAddressee( const Addressee &addr )
00071 {
00072   if ( mAddedAddressees.contains( addr.uid() ) ) {
00073     mAddedAddressees.remove( addr.uid() );
00074     return;
00075   }
00076 
00077   if ( mDeletedAddressees.find( addr.uid() ) == mDeletedAddressees.end() )
00078     mDeletedAddressees.insert( addr.uid(), addr );
00079 
00080   mAddrMap.remove( addr.uid() );
00081 }
00082 
00083 void ResourceCached::loadCache()
00084 {
00085   mAddrMap.clear();
00086 
00087   setIdMapperIdentifier();
00088   mIdMapper.load();
00089 
00090   // load cache
00091   QFile file( cacheFile() );
00092   if ( !file.open( IO_ReadOnly ) )
00093     return;
00094 
00095 
00096   KABC::VCardConverter converter;
00097 #if defined(KABC_VCARD_ENCODING_FIX)
00098   KABC::Addressee::List list = converter.parseVCardsRaw( file.readAll().data() );
00099 #else
00100   KABC::Addressee::List list = converter.parseVCards( QString::fromUtf8( file.readAll() ) );
00101 #endif
00102   KABC::Addressee::List::Iterator it;
00103 
00104   for ( it = list.begin(); it != list.end(); ++it ) {
00105     (*it).setResource( this );
00106     (*it).setChanged( false );
00107     mAddrMap.insert( (*it).uid(), *it );
00108   }
00109 
00110   file.close();
00111 }
00112 
00113 void ResourceCached::saveCache()
00114 {
00115   setIdMapperIdentifier();
00116   mIdMapper.save();
00117 
00118   // save cache
00119   QFile file( cacheFile() );
00120   if ( !file.open( IO_WriteOnly ) )
00121     return;
00122 
00123   KABC::Addressee::List list = mAddrMap.values();
00124 
00125   KABC::VCardConverter converter;
00126 #if defined(KABC_VCARD_ENCODING_FIX)
00127   QCString vCard = converter.createVCardsRaw( list );
00128   file.writeBlock( vCard, vCard.length() );
00129 #else
00130   QString vCard = converter.createVCards( list );
00131   file.writeBlock( vCard.utf8(), vCard.utf8().length() );
00132 #endif
00133   file.close();
00134 }
00135 
00136 void ResourceCached::cleanUpCache( const KABC::Addressee::List &addrList )
00137 {
00138   // load cache
00139   QFile file( cacheFile() );
00140   if ( !file.open( IO_ReadOnly ) )
00141     return;
00142 
00143 
00144   KABC::VCardConverter converter;
00145 #if defined(KABC_VCARD_ENCODING_FIX)
00146   KABC::Addressee::List list = converter.parseVCardsRaw( file.readAll().data() );
00147 #else
00148   KABC::Addressee::List list = converter.parseVCards( QString::fromUtf8( file.readAll() ) );
00149 #endif
00150   KABC::Addressee::List::Iterator cacheIt;
00151   KABC::Addressee::List::ConstIterator it;
00152 
00153   for ( cacheIt = list.begin(); cacheIt != list.end(); ++cacheIt ) {
00154     bool found = false;
00155     for ( it = addrList.begin(); it != addrList.end(); ++it ) {
00156       if ( (*it).uid() == (*cacheIt).uid() )
00157         found = true;
00158     }
00159 
00160     if ( !found ) {
00161       mIdMapper.removeRemoteId( mIdMapper.remoteId( (*cacheIt).uid() ) );
00162       mAddrMap.remove( (*cacheIt).uid() );
00163     }
00164   }
00165 
00166   file.close();
00167 }
00168 
00169 KPIM::IdMapper& ResourceCached::idMapper()
00170 {
00171   return mIdMapper;
00172 }
00173 
00174 bool ResourceCached::hasChanges() const
00175 {
00176   return !( mAddedAddressees.isEmpty() &&
00177             mChangedAddressees.isEmpty() &&
00178             mDeletedAddressees.isEmpty() );
00179 }
00180 
00181 void ResourceCached::clearChanges()
00182 {
00183   mAddedAddressees.clear();
00184   mChangedAddressees.clear();
00185   mDeletedAddressees.clear();
00186 }
00187 
00188 void ResourceCached::clearChange( const KABC::Addressee &addr )
00189 {
00190   mAddedAddressees.remove( addr.uid() );
00191   mChangedAddressees.remove( addr.uid() );
00192   mDeletedAddressees.remove( addr.uid() );
00193 }
00194 
00195 void ResourceCached::clearChange( const QString &uid )
00196 {
00197   mAddedAddressees.remove( uid );
00198   mChangedAddressees.remove( uid );
00199   mDeletedAddressees.remove( uid );
00200 }
00201 
00202 KABC::Addressee::List ResourceCached::addedAddressees() const
00203 {
00204   return mAddedAddressees.values();
00205 }
00206 
00207 KABC::Addressee::List ResourceCached::changedAddressees() const
00208 {
00209   return mChangedAddressees.values();
00210 }
00211 
00212 KABC::Addressee::List ResourceCached::deletedAddressees() const
00213 {
00214   return mDeletedAddressees.values();
00215 }
00216 
00217 QString ResourceCached::cacheFile() const
00218 {
00219   return locateLocal( "cache", "kabc/kresources/" + identifier() );
00220 }
00221 
00222 QString ResourceCached::changesCacheFile( const QString &type ) const
00223 {
00224   return locateLocal( "cache", "kabc/changescache/" + identifier() + "_" + type );
00225 }
00226 
00227 void ResourceCached::saveChangesCache( const QMap<QString, KABC::Addressee> &map, const QString &type )
00228 {
00229   QFile file( changesCacheFile( type ) );
00230 
00231   const KABC::Addressee::List list = map.values();
00232   if ( list.isEmpty() ) {
00233     file.remove();
00234   } else {
00235     if ( !file.open( IO_WriteOnly ) ) {
00236       kdError() << "Can't open changes cache file '" << file.name() << "' for saving." << endl;
00237       return;
00238     }
00239 
00240     KABC::VCardConverter converter;
00241 #if defined(KABC_VCARD_ENCODING_FIX)
00242     const QCString vCards = converter.createVCardsRaw( list );
00243     file.writeBlock( vCards, vCards.length() );
00244 #else
00245     const QString vCards = converter.createVCards( list );
00246     QCString content = vCards.utf8();
00247     file.writeBlock( content, content.length() );
00248 #endif
00249   }
00250 }
00251 
00252 void ResourceCached::saveChangesCache()
00253 {
00254   saveChangesCache( mAddedAddressees, "added" );
00255   saveChangesCache( mDeletedAddressees, "deleted" );
00256   saveChangesCache( mChangedAddressees, "changed" );
00257 }
00258 
00259 void ResourceCached::loadChangesCache( QMap<QString, KABC::Addressee> &map, const QString &type )
00260 {
00261   QFile file( changesCacheFile( type ) );
00262   if ( !file.open( IO_ReadOnly ) )
00263     return;
00264 
00265   KABC::VCardConverter converter;
00266 
00267 #if defined(KABC_VCARD_ENCODING_FIX)
00268   const KABC::Addressee::List list = converter.parseVCardsRaw( file.readAll().data() );
00269 #else
00270   const KABC::Addressee::List list = converter.parseVCards( QString::fromUtf8( file.readAll() ) );
00271 #endif
00272   KABC::Addressee::List::ConstIterator it;
00273   for ( it = list.begin(); it != list.end(); ++it )
00274     map.insert( (*it).uid(), *it );
00275 
00276   file.close();
00277 }
00278 
00279 void ResourceCached::loadChangesCache()
00280 {
00281   loadChangesCache( mAddedAddressees, "added" );
00282   loadChangesCache( mDeletedAddressees, "deleted" );
00283   loadChangesCache( mChangedAddressees, "changed" );
00284 }
00285 
00286 void ResourceCached::setIdMapperIdentifier()
00287 {
00288   mIdMapper.setIdentifier( type() + "_" + identifier() );
00289 }
00290 
00291 #include "kabcresourcecached.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys