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   KABC::Addressee::List list = converter.parseVCards( QString::fromUtf8( file.readAll() ) );
00098   KABC::Addressee::List::Iterator it;
00099 
00100   for ( it = list.begin(); it != list.end(); ++it ) {
00101     (*it).setResource( this );
00102     (*it).setChanged( false );
00103     mAddrMap.insert( (*it).uid(), *it );
00104   }
00105 
00106   file.close();
00107 }
00108 
00109 void ResourceCached::saveCache()
00110 {
00111   setIdMapperIdentifier();
00112   mIdMapper.save();
00113 
00114   // save cache
00115   QFile file( cacheFile() );
00116   if ( !file.open( IO_WriteOnly ) )
00117     return;
00118 
00119   KABC::Addressee::List list = mAddrMap.values();
00120 
00121   KABC::VCardConverter converter;
00122   QString vCard = converter.createVCards( list );
00123   file.writeBlock( vCard.utf8(), vCard.utf8().length() );
00124   file.close();
00125 }
00126 
00127 void ResourceCached::cleanUpCache( const KABC::Addressee::List &addrList )
00128 {
00129   // load cache
00130   QFile file( cacheFile() );
00131   if ( !file.open( IO_ReadOnly ) )
00132     return;
00133 
00134 
00135   KABC::VCardConverter converter;
00136   KABC::Addressee::List list = converter.parseVCards( QString::fromUtf8( file.readAll() ) );
00137   KABC::Addressee::List::Iterator cacheIt;
00138   KABC::Addressee::List::ConstIterator it;
00139 
00140   for ( cacheIt = list.begin(); cacheIt != list.end(); ++cacheIt ) {
00141     bool found = false;
00142     for ( it = addrList.begin(); it != addrList.end(); ++it ) {
00143       if ( (*it).uid() == (*cacheIt).uid() )
00144         found = true;
00145     }
00146 
00147     if ( !found ) {
00148       mIdMapper.removeRemoteId( mIdMapper.remoteId( (*cacheIt).uid() ) );
00149       mAddrMap.remove( (*cacheIt).uid() );
00150     }
00151   }
00152 
00153   file.close();
00154 }
00155 
00156 KPIM::IdMapper& ResourceCached::idMapper()
00157 {
00158   return mIdMapper;
00159 }
00160 
00161 bool ResourceCached::hasChanges() const
00162 {
00163   return !( mAddedAddressees.isEmpty() &&
00164             mChangedAddressees.isEmpty() &&
00165             mDeletedAddressees.isEmpty() );
00166 }
00167 
00168 void ResourceCached::clearChanges()
00169 {
00170   mAddedAddressees.clear();
00171   mChangedAddressees.clear();
00172   mDeletedAddressees.clear();
00173 }
00174 
00175 void ResourceCached::clearChange( const KABC::Addressee &addr )
00176 {
00177   mAddedAddressees.remove( addr.uid() );
00178   mChangedAddressees.remove( addr.uid() );
00179   mDeletedAddressees.remove( addr.uid() );
00180 }
00181 
00182 void ResourceCached::clearChange( const QString &uid )
00183 {
00184   mAddedAddressees.remove( uid );
00185   mChangedAddressees.remove( uid );
00186   mDeletedAddressees.remove( uid );
00187 }
00188 
00189 KABC::Addressee::List ResourceCached::addedAddressees() const
00190 {
00191   return mAddedAddressees.values();
00192 }
00193 
00194 KABC::Addressee::List ResourceCached::changedAddressees() const
00195 {
00196   return mChangedAddressees.values();
00197 }
00198 
00199 KABC::Addressee::List ResourceCached::deletedAddressees() const
00200 {
00201   return mDeletedAddressees.values();
00202 }
00203 
00204 QString ResourceCached::cacheFile() const
00205 {
00206   return locateLocal( "cache", "kabc/kresources/" + identifier() );
00207 }
00208 
00209 QString ResourceCached::changesCacheFile( const QString &type ) const
00210 {
00211   return locateLocal( "cache", "kabc/changescache/" + identifier() + "_" + type );
00212 }
00213 
00214 void ResourceCached::saveChangesCache( const QMap<QString, KABC::Addressee> &map, const QString &type )
00215 {
00216   QFile file( changesCacheFile( type ) );
00217 
00218   const KABC::Addressee::List list = map.values();
00219   if ( list.isEmpty() ) {
00220     file.remove();
00221   } else {
00222     if ( !file.open( IO_WriteOnly ) ) {
00223       kdError() << "Can't open changes cache file '" << file.name() << "' for saving." << endl;
00224       return;
00225     }
00226 
00227     KABC::VCardConverter converter;
00228     const QString vCards = converter.createVCards( list );
00229     QCString content = vCards.utf8();
00230     file.writeBlock( content, content.length() );
00231   }
00232 }
00233 
00234 void ResourceCached::saveChangesCache()
00235 {
00236   saveChangesCache( mAddedAddressees, "added" );
00237   saveChangesCache( mDeletedAddressees, "deleted" );
00238   saveChangesCache( mChangedAddressees, "changed" );
00239 }
00240 
00241 void ResourceCached::loadChangesCache( QMap<QString, KABC::Addressee> &map, const QString &type )
00242 {
00243   QFile file( changesCacheFile( type ) );
00244   if ( !file.open( IO_ReadOnly ) )
00245     return;
00246 
00247   KABC::VCardConverter converter;
00248 
00249   const KABC::Addressee::List list = converter.parseVCards( QString::fromUtf8( file.readAll() ) );
00250   KABC::Addressee::List::ConstIterator it;
00251   for ( it = list.begin(); it != list.end(); ++it )
00252     map.insert( (*it).uid(), *it );
00253 
00254   file.close();
00255 }
00256 
00257 void ResourceCached::loadChangesCache()
00258 {
00259   loadChangesCache( mAddedAddressees, "added" );
00260   loadChangesCache( mDeletedAddressees, "deleted" );
00261   loadChangesCache( mChangedAddressees, "changed" );
00262 }
00263 
00264 void ResourceCached::setIdMapperIdentifier()
00265 {
00266   mIdMapper.setIdentifier( type() + "_" + identifier() );
00267 }
00268 
00269 #include "kabcresourcecached.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys