23 #include <tdeabc/vcardconverter.h>
25 #include <tdelocale.h>
26 #include <kstandarddirs.h>
28 #include "tdeabcresourcecached.h"
30 using namespace TDEABC;
32 ResourceCached::ResourceCached(
const TDEConfig *config )
33 : TDEABC::Resource( config ), mIdMapper(
"tdeabc/uidmaps/" ),
34 mReloadPolicy( ReloadInterval ), mReloadInterval( 10 ),
35 mKABCReloadTimer( 0,
"mKABCReloadTimer" ), mReloaded( false ),
36 mSavePolicy( SaveDelayed ), mSaveInterval( 10 ),
37 mKABCSaveTimer( 0,
"mKABCSaveTimer" )
39 connect( &mKABCReloadTimer, TQ_SIGNAL( timeout() ), TQ_SLOT( slotKABCReload() ) );
40 connect( &mKABCSaveTimer, TQ_SIGNAL( timeout() ), TQ_SLOT( slotKABCSave() ) );
43 this->readConfig(
const_cast<TDEConfig *
>(config));
46 ResourceCached::~ResourceCached()
50 void ResourceCached::setReloadPolicy(
int i )
57 int ResourceCached::reloadPolicy()
const
62 void ResourceCached::setReloadInterval(
int minutes )
64 mReloadInterval = minutes;
67 int ResourceCached::reloadInterval()
const
69 return mReloadInterval;
72 void ResourceCached::setSavePolicy(
int i )
79 int ResourceCached::savePolicy()
const
84 void ResourceCached::setSaveInterval(
int minutes )
86 mSaveInterval = minutes;
89 int ResourceCached::saveInterval()
const
94 void ResourceCached::writeConfig( TDEConfig *config )
96 config->writeEntry(
"ReloadPolicy", mReloadPolicy );
97 config->writeEntry(
"ReloadInterval", mReloadInterval );
99 config->writeEntry(
"SavePolicy", mSavePolicy );
100 config->writeEntry(
"SaveInterval", mSaveInterval );
102 config->writeEntry(
"LastLoad", mLastLoad );
103 config->writeEntry(
"LastSave", mLastSave );
105 TDEABC::Resource::writeConfig( config );
108 void ResourceCached::readConfig( TDEConfig *config )
110 mReloadPolicy = config->readNumEntry(
"ReloadPolicy", ReloadNever );
111 mReloadInterval = config->readNumEntry(
"ReloadInterval", 10 );
113 mSaveInterval = config->readNumEntry(
"SaveInterval", 10 );
114 mSavePolicy = config->readNumEntry(
"SavePolicy", SaveNever );
116 mLastLoad = config->readDateTimeEntry(
"LastLoad" );
117 mLastSave = config->readDateTimeEntry(
"LastSave" );
123 void ResourceCached::setupSaveTimer()
125 if ( mSavePolicy == SaveInterval ) {
126 kdDebug(5800) <<
"ResourceCached::setSavePolicy(): start save timer (interval "
127 << mSaveInterval <<
" minutes)." << endl;
128 mKABCSaveTimer.start( mSaveInterval * 60 * 1000 );
130 mKABCSaveTimer.stop();
134 void ResourceCached::setupReloadTimer()
136 if ( mReloadPolicy == ReloadInterval ) {
137 kdDebug(5800) <<
"ResourceCached::setSavePolicy(): start reload timer "
138 "(interval " << mReloadInterval <<
" minutes)" << endl;
139 mKABCReloadTimer.start( mReloadInterval * 60 * 1000 );
141 mKABCReloadTimer.stop();
145 void ResourceCached::slotKABCReload()
147 if ( !isActive() )
return;
149 kdDebug(5800) <<
"ResourceCached::slotKABCReload()" << endl;
154 void ResourceCached::slotKABCSave()
156 if ( !isActive() )
return;
158 kdDebug(5800) <<
"ResourceCached::slotKABCSave()" << endl;
160 TDEABC::Ticket *ticket = requestSaveTicket();
166 void ResourceCached::insertAddressee(
const Addressee &addr )
168 if ( !mAddrMap.contains( addr.uid() ) ) {
169 if ( mDeletedAddressees.contains( addr.uid() ) ) {
171 mDeletedAddressees.remove( addr.uid() );
173 mAddrMap.insert( addr.uid(), addr );
174 mChangedAddressees.insert( addr.uid(), addr );
178 mAddrMap.insert( addr.uid(), addr );
179 mAddedAddressees.insert( addr.uid(), addr );
181 TDEABC::Addressee oldAddressee = mAddrMap.find( addr.uid() ).data();
182 if ( oldAddressee != addr ) {
183 mAddrMap.remove( addr.uid() );
184 mAddrMap.insert( addr.uid(), addr );
185 mChangedAddressees.insert( addr.uid(), addr );
190 void ResourceCached::removeAddressee(
const Addressee &addr )
192 if ( mAddedAddressees.contains( addr.uid() ) ) {
193 mAddedAddressees.remove( addr.uid() );
197 if ( mDeletedAddressees.find( addr.uid() ) == mDeletedAddressees.end() )
198 mDeletedAddressees.insert( addr.uid(), addr );
200 mAddrMap.remove( addr.uid() );
203 void ResourceCached::loadCache()
207 setIdMapperIdentifier();
211 TQFile file( cacheFile() );
212 if ( !file.open( IO_ReadOnly ) )
216 TDEABC::VCardConverter converter;
217 #if defined(KABC_VCARD_ENCODING_FIX)
218 TDEABC::Addressee::List list = converter.parseVCardsRaw( file.readAll().data() );
220 TQTextStream s( &file );
221 s.setEncoding( TQTextStream::UnicodeUTF8 );
222 TDEABC::Addressee::List list = converter.parseVCards( s.read() );
224 TDEABC::Addressee::List::Iterator it;
226 for ( it = list.begin(); it != list.end(); ++it ) {
227 (*it).setResource(
this );
228 (*it).setChanged(
false );
229 mAddrMap.insert( (*it).uid(), *it );
235 void ResourceCached::saveCache()
237 setIdMapperIdentifier();
241 TQFile file( cacheFile() );
242 if ( !file.open( IO_WriteOnly ) )
245 TDEABC::Addressee::List list = mAddrMap.values();
247 TDEABC::VCardConverter converter;
248 #if defined(KABC_VCARD_ENCODING_FIX)
249 TQCString vCard = converter.createVCardsRaw( list );
250 file.writeBlock( vCard, vCard.length() );
252 TQString vCard = converter.createVCards( list );
253 file.writeBlock( vCard.utf8(), vCard.utf8().length() );
258 void ResourceCached::clearCache()
264 void ResourceCached::cleanUpCache(
const TDEABC::Addressee::List &addrList )
267 TQFile file( cacheFile() );
268 if ( !file.open( IO_ReadOnly ) )
272 TDEABC::VCardConverter converter;
273 #if defined(KABC_VCARD_ENCODING_FIX)
274 TDEABC::Addressee::List list = converter.parseVCardsRaw( file.readAll().data() );
276 TQTextStream s( &file );
277 s.setEncoding( TQTextStream::UnicodeUTF8 );
278 TDEABC::Addressee::List list = converter.parseVCards( s.read() );
280 TDEABC::Addressee::List::Iterator cacheIt;
281 TDEABC::Addressee::List::ConstIterator it;
283 for ( cacheIt = list.begin(); cacheIt != list.end(); ++cacheIt ) {
285 for ( it = addrList.begin(); it != addrList.end(); ++it ) {
286 if ( (*it).uid() == (*cacheIt).uid() )
291 mIdMapper.removeRemoteId( mIdMapper.remoteId( (*cacheIt).uid() ) );
292 mAddrMap.remove( (*cacheIt).uid() );
299 KPIM::IdMapper& ResourceCached::idMapper()
304 bool ResourceCached::hasChanges()
const
306 return !( mAddedAddressees.isEmpty() &&
307 mChangedAddressees.isEmpty() &&
308 mDeletedAddressees.isEmpty() );
311 void ResourceCached::clearChanges()
313 mAddedAddressees.clear();
314 mChangedAddressees.clear();
315 mDeletedAddressees.clear();
318 void ResourceCached::clearChange(
const TDEABC::Addressee &addr )
320 mAddedAddressees.remove( addr.uid() );
321 mChangedAddressees.remove( addr.uid() );
322 mDeletedAddressees.remove( addr.uid() );
325 void ResourceCached::clearChange(
const TQString &uid )
327 mAddedAddressees.remove( uid );
328 mChangedAddressees.remove( uid );
329 mDeletedAddressees.remove( uid );
332 TDEABC::Addressee::List ResourceCached::addedAddressees()
const
334 return mAddedAddressees.values();
337 TDEABC::Addressee::List ResourceCached::changedAddressees()
const
339 return mChangedAddressees.values();
342 TDEABC::Addressee::List ResourceCached::deletedAddressees()
const
344 return mDeletedAddressees.values();
347 TQString ResourceCached::cacheFile()
const
349 return locateLocal(
"cache",
"tdeabc/tderesources/" + identifier() );
352 TQString ResourceCached::changesCacheFile(
const TQString &type )
const
354 return locateLocal(
"cache",
"tdeabc/changescache/" + identifier() +
"_" + type );
357 void ResourceCached::saveChangesCache(
const TQMap<TQString, TDEABC::Addressee> &map,
const TQString &type )
359 TQFile file( changesCacheFile( type ) );
361 const TDEABC::Addressee::List list = map.values();
362 if ( list.isEmpty() ) {
365 if ( !file.open( IO_WriteOnly ) ) {
366 kdError() <<
"Can't open changes cache file '" << file.name() <<
"' for saving." << endl;
370 TDEABC::VCardConverter converter;
371 #if defined(KABC_VCARD_ENCODING_FIX)
372 const TQCString vCards = converter.createVCardsRaw( list );
373 file.writeBlock( vCards, vCards.length() );
375 const TQString vCards = converter.createVCards( list );
376 TQCString content = vCards.utf8();
377 file.writeBlock( content, content.length() );
382 void ResourceCached::saveChangesCache()
384 saveChangesCache( mAddedAddressees,
"added" );
385 saveChangesCache( mDeletedAddressees,
"deleted" );
386 saveChangesCache( mChangedAddressees,
"changed" );
389 void ResourceCached::loadChangesCache( TQMap<TQString, TDEABC::Addressee> &map,
const TQString &type )
391 TQFile file( changesCacheFile( type ) );
392 if ( !file.open( IO_ReadOnly ) )
395 TDEABC::VCardConverter converter;
397 #if defined(KABC_VCARD_ENCODING_FIX)
398 const TDEABC::Addressee::List list = converter.parseVCardsRaw( file.readAll().data() );
400 TQTextStream s( &file );
401 s.setEncoding( TQTextStream::UnicodeUTF8 );
402 const TDEABC::Addressee::List list = converter.parseVCards( s.read() );
404 TDEABC::Addressee::List::ConstIterator it;
405 for ( it = list.begin(); it != list.end(); ++it )
406 map.insert( (*it).uid(), *it );
411 void ResourceCached::loadChangesCache()
413 loadChangesCache( mAddedAddressees,
"added" );
414 loadChangesCache( mDeletedAddressees,
"deleted" );
415 loadChangesCache( mChangedAddressees,
"changed" );
418 void ResourceCached::setIdMapperIdentifier()
420 mIdMapper.setIdentifier( type() +
"_" + identifier() );
423 #include "tdeabcresourcecached.moc"