21 #include "resourcecached.h"
23 #include <kabc/vcardconverter.h>
26 #include <klocalizedstring.h>
27 #include <kstandarddirs.h>
29 #include <QtCore/QFile>
33 class ResourceCached::Private
36 Private( ResourceCached *parent )
37 : mParent( parent ), mIdMapper(
QLatin1String(
"kabc/uidmaps/" ) )
44 ResourceCached *mParent;
55 QFile file( mParent->changesCacheFile( type ) );
58 if ( list.isEmpty() ) {
61 if ( !file.open( QIODevice::WriteOnly ) ) {
62 kError( 5700 ) <<
"Can't open changes cache file '" << file.fileName() <<
"' for saving.";
75 QFile file( mParent->changesCacheFile( type ) );
76 if ( !file.open( QIODevice::ReadOnly ) ) {
85 for ( it = list.
begin(); it != end; ++it ) {
86 map.
insert( ( *it ).uid(), *it );
92 ResourceCached::ResourceCached()
93 : KABC::
Resource(), d( new Private( this ) )
97 ResourceCached::ResourceCached(
const KConfigGroup &group )
98 : KABC::
Resource( group ), d( new Private( this ) )
102 ResourceCached::~ResourceCached()
107 void ResourceCached::writeConfig( KConfigGroup &group )
112 void ResourceCached::insertAddressee(
const Addressee &addr )
114 if ( !mAddrMap.contains( addr.
uid() ) ) {
115 if ( d->mDeletedAddressees.contains( addr.
uid() ) ) {
117 d->mDeletedAddressees.remove( addr.
uid() );
119 mAddrMap.insert( addr.
uid(), addr );
120 d->mChangedAddressees.insert( addr.
uid(), addr );
124 mAddrMap.insert( addr.
uid(), addr );
125 d->mAddedAddressees.insert( addr.
uid(), addr );
128 if ( oldAddressee != addr ) {
129 mAddrMap.remove( addr.
uid() );
130 mAddrMap.insert( addr.
uid(), addr );
131 d->mChangedAddressees.insert( addr.
uid(), addr );
136 void ResourceCached::removeAddressee(
const Addressee &addr )
138 if ( d->mAddedAddressees.contains( addr.
uid() ) ) {
139 d->mAddedAddressees.remove( addr.
uid() );
143 if ( d->mDeletedAddressees.find( addr.
uid() ) == d->mDeletedAddressees.end() ) {
144 d->mDeletedAddressees.insert( addr.
uid(), addr );
147 mAddrMap.remove( addr.
uid() );
150 bool ResourceCached::loadFromCache()
154 setIdMapperIdentifier();
158 QFile file( cacheFile() );
159 if ( !file.open( QIODevice::ReadOnly ) ) {
167 for ( it = list.
begin(); it != list.
end(); ++it ) {
168 ( *it ).setResource(
this );
169 ( *it ).setChanged(
false );
170 mAddrMap.insert( ( *it ).uid(), *it );
177 void ResourceCached::saveToCache()
179 setIdMapperIdentifier();
183 QFile file( cacheFile() );
184 if ( !file.open( QIODevice::WriteOnly ) ) {
199 QFile file( cacheFile() );
200 if ( !file.open( QIODevice::ReadOnly ) ) {
209 for ( cacheIt = list.
begin(); cacheIt != list.
end(); ++cacheIt ) {
211 for ( it = addrList.
begin(); it != addrList.
end(); ++it ) {
212 if ( ( *it ).uid() == ( *cacheIt ).uid() ) {
218 d->mIdMapper.removeRemoteId( d->mIdMapper.remoteId( ( *cacheIt ).uid() ) );
219 mAddrMap.remove( ( *cacheIt ).uid() );
231 bool ResourceCached::hasChanges()
const
233 return !( d->mAddedAddressees.isEmpty() &&
234 d->mChangedAddressees.isEmpty() &&
235 d->mDeletedAddressees.isEmpty() );
238 void ResourceCached::clearChanges()
240 d->mAddedAddressees.
clear();
241 d->mChangedAddressees.clear();
242 d->mDeletedAddressees.clear();
247 d->mAddedAddressees.remove( addr.
uid() );
248 d->mChangedAddressees.remove( addr.
uid() );
249 d->mDeletedAddressees.remove( addr.
uid() );
252 void ResourceCached::clearChange(
const QString &uid )
254 d->mAddedAddressees.remove( uid );
255 d->mChangedAddressees.remove( uid );
256 d->mDeletedAddressees.remove( uid );
261 return d->mAddedAddressees.values();
266 return d->mChangedAddressees.values();
271 return d->mDeletedAddressees.values();
274 QString ResourceCached::cacheFile()
const
276 return KStandardDirs::locateLocal(
"cache",
QLatin1String(
"kabc/kresources/" ) + identifier() );
279 QString ResourceCached::changesCacheFile(
const QString &type )
const
281 return KStandardDirs::locateLocal(
"cache",
QLatin1String(
"kabc/changescache/" ) + identifier() +
285 void ResourceCached::saveChangesCache()
287 d->saveChangesCache( d->mAddedAddressees,
QLatin1String(
"added" ) );
288 d->saveChangesCache( d->mDeletedAddressees,
QLatin1String(
"deleted" ) );
289 d->saveChangesCache( d->mChangedAddressees,
QLatin1String(
"changed" ) );
292 void ResourceCached::loadChangesCache()
294 d->loadChangesCache( d->mAddedAddressees,
QLatin1String(
"added" ) );
295 d->loadChangesCache( d->mDeletedAddressees,
QLatin1String(
"deleted" ) );
296 d->loadChangesCache( d->mChangedAddressees,
QLatin1String(
"changed" ) );
299 void ResourceCached::setIdMapperIdentifier()
301 d->mIdMapper.setIdentifier( type() +
QLatin1Char(
'_' ) + identifier() );
QList< T > values() const
a QValueList of Addressee, with sorting functionality
Addressee::List parseVCards(const QByteArray &vcard) const
Parses a string in vCard format and returns a list of contact objects.
Class to converting contact objects into vCard format and vice versa.
QByteArray createVCards(Addressee::List list, Version version=v3_0) const
Creates a string in vCard format which contains the given list of contact.
QString uid() const
Return unique identifier.
iterator insert(const Key &key, const T &value)
virtual void writeConfig(KConfigGroup &group)
Writes the resource specific config to file.