00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "resourcecached.h"
00022
00023 #include <kabc/vcardconverter.h>
00024
00025 #include <kdebug.h>
00026 #include <klocale.h>
00027 #include <kstandarddirs.h>
00028
00029 #include <QtCore/QFile>
00030
00031 using namespace KABC;
00032
00033 class ResourceCached::Private
00034 {
00035 public:
00036 Private( ResourceCached *parent )
00037 : mParent( parent ), mIdMapper( QLatin1String( "kabc/uidmaps/" ) )
00038 {
00039 }
00040
00041 void loadChangesCache( QMap<QString, KABC::Addressee> &map, const QString &type );
00042 void saveChangesCache( const QMap<QString, KABC::Addressee> &map, const QString &type );
00043
00044 ResourceCached *mParent;
00045 KRES::IdMapper mIdMapper;
00046
00047 QMap<QString, KABC::Addressee> mAddedAddressees;
00048 QMap<QString, KABC::Addressee> mChangedAddressees;
00049 QMap<QString, KABC::Addressee> mDeletedAddressees;
00050 };
00051
00052 void ResourceCached::Private::saveChangesCache( const QMap<QString, KABC::Addressee> &map,
00053 const QString &type )
00054 {
00055 QFile file( mParent->changesCacheFile( type ) );
00056
00057 const KABC::Addressee::List list = map.values();
00058 if ( list.isEmpty() ) {
00059 file.remove();
00060 } else {
00061 if ( !file.open( QIODevice::WriteOnly ) ) {
00062 kError(5700) << "Can't open changes cache file '" << file.fileName() << "' for saving.";
00063 return;
00064 }
00065
00066 KABC::VCardConverter converter;
00067 const QByteArray vCards = converter.createVCards( list );
00068 file.write( vCards );
00069 }
00070 }
00071
00072 void ResourceCached::Private::loadChangesCache( QMap<QString, KABC::Addressee> &map,
00073 const QString &type )
00074 {
00075 QFile file( mParent->changesCacheFile( type ) );
00076 if ( !file.open( QIODevice::ReadOnly ) ) {
00077 return;
00078 }
00079
00080 KABC::VCardConverter converter;
00081
00082 const KABC::Addressee::List list = converter.parseVCards( file.readAll() );
00083 KABC::Addressee::List::ConstIterator it;
00084 for ( it = list.begin(); it != list.end(); ++it ) {
00085 map.insert( (*it).uid(), *it );
00086 }
00087
00088 file.close();
00089 }
00090
00091 ResourceCached::ResourceCached()
00092 : KABC::Resource(), d( new Private( this ) )
00093 {
00094 }
00095
00096 ResourceCached::ResourceCached( const KConfigGroup &group )
00097 : KABC::Resource( group ), d( new Private( this ) )
00098 {
00099 }
00100
00101 ResourceCached::~ResourceCached()
00102 {
00103 delete d;
00104 }
00105
00106 void ResourceCached::writeConfig( KConfigGroup &group )
00107 {
00108 KABC::Resource::writeConfig( group );
00109 }
00110
00111 void ResourceCached::insertAddressee( const Addressee &addr )
00112 {
00113 if ( !mAddrMap.contains( addr.uid() ) ) {
00114 if ( d->mDeletedAddressees.contains( addr.uid() ) ) {
00115
00116 d->mDeletedAddressees.remove( addr.uid() );
00117
00118 mAddrMap.insert( addr.uid(), addr );
00119 d->mChangedAddressees.insert( addr.uid(), addr );
00120 return;
00121 }
00122
00123 mAddrMap.insert( addr.uid(), addr );
00124 d->mAddedAddressees.insert( addr.uid(), addr );
00125 } else {
00126 KABC::Addressee oldAddressee = mAddrMap.find( addr.uid() ).value();
00127 if ( oldAddressee != addr ) {
00128 mAddrMap.remove( addr.uid() );
00129 mAddrMap.insert( addr.uid(), addr );
00130 d->mChangedAddressees.insert( addr.uid(), addr );
00131 }
00132 }
00133 }
00134
00135 void ResourceCached::removeAddressee( const Addressee &addr )
00136 {
00137 if ( d->mAddedAddressees.contains( addr.uid() ) ) {
00138 d->mAddedAddressees.remove( addr.uid() );
00139 return;
00140 }
00141
00142 if ( d->mDeletedAddressees.find( addr.uid() ) == d->mDeletedAddressees.end() ) {
00143 d->mDeletedAddressees.insert( addr.uid(), addr );
00144 }
00145
00146 mAddrMap.remove( addr.uid() );
00147 }
00148
00149 bool ResourceCached::loadFromCache()
00150 {
00151 mAddrMap.clear();
00152
00153 setIdMapperIdentifier();
00154 d->mIdMapper.load();
00155
00156
00157 QFile file( cacheFile() );
00158 if ( !file.open( QIODevice::ReadOnly ) ) {
00159 return false;
00160 }
00161
00162 KABC::VCardConverter converter;
00163 KABC::Addressee::List list = converter.parseVCards( file.readAll() );
00164 KABC::Addressee::List::Iterator it;
00165
00166 for ( it = list.begin(); it != list.end(); ++it ) {
00167 (*it).setResource( this );
00168 (*it).setChanged( false );
00169 mAddrMap.insert( (*it).uid(), *it );
00170 }
00171
00172 file.close();
00173 return true;
00174 }
00175
00176 void ResourceCached::saveToCache()
00177 {
00178 setIdMapperIdentifier();
00179 d->mIdMapper.save();
00180
00181
00182 QFile file( cacheFile() );
00183 if ( !file.open( QIODevice::WriteOnly ) ) {
00184 return;
00185 }
00186
00187 KABC::Addressee::List list = mAddrMap.values();
00188
00189 KABC::VCardConverter converter;
00190 QByteArray vCard = converter.createVCards( list );
00191 file.write( vCard );
00192 file.close();
00193 }
00194
00195 void ResourceCached::cleanUpCache( const KABC::Addressee::List &addrList )
00196 {
00197
00198 QFile file( cacheFile() );
00199 if ( !file.open( QIODevice::ReadOnly ) ) {
00200 return;
00201 }
00202
00203 KABC::VCardConverter converter;
00204 KABC::Addressee::List list = converter.parseVCards( file.readAll() );
00205 KABC::Addressee::List::Iterator cacheIt;
00206 KABC::Addressee::List::ConstIterator it;
00207
00208 for ( cacheIt = list.begin(); cacheIt != list.end(); ++cacheIt ) {
00209 bool found = false;
00210 for ( it = addrList.begin(); it != addrList.end(); ++it ) {
00211 if ( (*it).uid() == (*cacheIt).uid() ) {
00212 found = true;
00213 }
00214 }
00215
00216 if ( !found ) {
00217 d->mIdMapper.removeRemoteId( d->mIdMapper.remoteId( (*cacheIt).uid() ) );
00218 mAddrMap.remove( (*cacheIt).uid() );
00219 }
00220 }
00221
00222 file.close();
00223 }
00224
00225 KRES::IdMapper &ResourceCached::idMapper()
00226 {
00227 return d->mIdMapper;
00228 }
00229
00230 bool ResourceCached::hasChanges() const
00231 {
00232 return !( d->mAddedAddressees.isEmpty() &&
00233 d->mChangedAddressees.isEmpty() &&
00234 d->mDeletedAddressees.isEmpty() );
00235 }
00236
00237 void ResourceCached::clearChanges()
00238 {
00239 d->mAddedAddressees.clear();
00240 d->mChangedAddressees.clear();
00241 d->mDeletedAddressees.clear();
00242 }
00243
00244 void ResourceCached::clearChange( const KABC::Addressee &addr )
00245 {
00246 d->mAddedAddressees.remove( addr.uid() );
00247 d->mChangedAddressees.remove( addr.uid() );
00248 d->mDeletedAddressees.remove( addr.uid() );
00249 }
00250
00251 void ResourceCached::clearChange( const QString &uid )
00252 {
00253 d->mAddedAddressees.remove( uid );
00254 d->mChangedAddressees.remove( uid );
00255 d->mDeletedAddressees.remove( uid );
00256 }
00257
00258 KABC::Addressee::List ResourceCached::addedAddressees() const
00259 {
00260 return d->mAddedAddressees.values();
00261 }
00262
00263 KABC::Addressee::List ResourceCached::changedAddressees() const
00264 {
00265 return d->mChangedAddressees.values();
00266 }
00267
00268 KABC::Addressee::List ResourceCached::deletedAddressees() const
00269 {
00270 return d->mDeletedAddressees.values();
00271 }
00272
00273 QString ResourceCached::cacheFile() const
00274 {
00275 return KStandardDirs::locateLocal( "cache", QLatin1String( "kabc/kresources/" ) + identifier() );
00276 }
00277
00278 QString ResourceCached::changesCacheFile( const QString &type ) const
00279 {
00280 return KStandardDirs::locateLocal( "cache", QLatin1String( "kabc/changescache/" ) + identifier() +
00281 QLatin1Char( '_' ) + type );
00282 }
00283
00284 void ResourceCached::saveChangesCache()
00285 {
00286 d->saveChangesCache( d->mAddedAddressees, QLatin1String( "added" ) );
00287 d->saveChangesCache( d->mDeletedAddressees, QLatin1String( "deleted" ) );
00288 d->saveChangesCache( d->mChangedAddressees, QLatin1String( "changed" ) );
00289 }
00290
00291 void ResourceCached::loadChangesCache()
00292 {
00293 d->loadChangesCache( d->mAddedAddressees, QLatin1String( "added" ) );
00294 d->loadChangesCache( d->mDeletedAddressees, QLatin1String( "deleted" ) );
00295 d->loadChangesCache( d->mChangedAddressees, QLatin1String( "changed" ) );
00296 }
00297
00298 void ResourceCached::setIdMapperIdentifier()
00299 {
00300 d->mIdMapper.setIdentifier( type() + QLatin1Char( '_' ) + identifier() );
00301 }
00302
00303 #include "resourcecached.moc"