Build, if that was not necessary blame cartman who told me "sure" :-D
[kdepim.git] / libkdepim / kabcresourcecached.cpp
blobc7edae1e29759758039f76bba46d805668f756b3
1 /*
2 This file is part of libkdepim.
3 Copyright (c) 2004 Tobias Koenig <tokoe@kde.org>
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 #include <qfile.h>
23 #include <kabc/vcardconverter.h>
24 #include <kdebug.h>
25 #include <klocale.h>
26 #include <kstandarddirs.h>
28 #include "kabcresourcecached.h"
30 using namespace KABC;
32 ResourceCached::ResourceCached( const KConfig *config )
33 : KABC::Resource( config ), mIdMapper( "kabc/uidmaps/" )
37 ResourceCached::~ResourceCached()
41 void ResourceCached::writeConfig( KConfig *config )
43 KABC::Resource::writeConfig( config );
46 void ResourceCached::insertAddressee( const Addressee &addr )
48 if ( !mAddrMap.contains( addr.uid() ) ) { // new contact
49 if ( mDeletedAddressees.contains( addr.uid() ) ) {
50 // it was first removed, then added, so it's an update...
51 mDeletedAddressees.remove( addr.uid() );
53 mAddrMap.insert( addr.uid(), addr );
54 mChangedAddressees.insert( addr.uid(), addr );
55 return;
58 mAddrMap.insert( addr.uid(), addr );
59 mAddedAddressees.insert( addr.uid(), addr );
60 } else {
61 KABC::Addressee oldAddressee = mAddrMap.find( addr.uid() ).data();
62 if ( oldAddressee != addr ) {
63 mAddrMap.remove( addr.uid() );
64 mAddrMap.insert( addr.uid(), addr );
65 mChangedAddressees.insert( addr.uid(), addr );
70 void ResourceCached::removeAddressee( const Addressee &addr )
72 if ( mAddedAddressees.contains( addr.uid() ) ) {
73 mAddedAddressees.remove( addr.uid() );
74 return;
77 if ( mDeletedAddressees.find( addr.uid() ) == mDeletedAddressees.end() )
78 mDeletedAddressees.insert( addr.uid(), addr );
80 mAddrMap.remove( addr.uid() );
83 void ResourceCached::loadCache()
85 mAddrMap.clear();
87 setIdMapperIdentifier();
88 mIdMapper.load();
90 // load cache
91 QFile file( cacheFile() );
92 if ( !file.open( IO_ReadOnly ) )
93 return;
96 KABC::VCardConverter converter;
97 KABC::Addressee::List list = converter.parseVCards( QString::fromUtf8( file.readAll() ) );
98 KABC::Addressee::List::Iterator it;
100 for ( it = list.begin(); it != list.end(); ++it ) {
101 (*it).setResource( this );
102 (*it).setChanged( false );
103 mAddrMap.insert( (*it).uid(), *it );
106 file.close();
109 void ResourceCached::saveCache()
111 setIdMapperIdentifier();
112 mIdMapper.save();
114 // save cache
115 QFile file( cacheFile() );
116 if ( !file.open( IO_WriteOnly ) )
117 return;
119 KABC::Addressee::List list = mAddrMap.values();
121 KABC::VCardConverter converter;
122 QString vCard = converter.createVCards( list );
123 file.writeBlock( vCard.utf8(), vCard.utf8().length() );
124 file.close();
127 void ResourceCached::cleanUpCache( const KABC::Addressee::List &addrList )
129 // load cache
130 QFile file( cacheFile() );
131 if ( !file.open( IO_ReadOnly ) )
132 return;
135 KABC::VCardConverter converter;
136 KABC::Addressee::List list = converter.parseVCards( QString::fromUtf8( file.readAll() ) );
137 KABC::Addressee::List::Iterator cacheIt;
138 KABC::Addressee::List::ConstIterator it;
140 for ( cacheIt = list.begin(); cacheIt != list.end(); ++cacheIt ) {
141 bool found = false;
142 for ( it = addrList.begin(); it != addrList.end(); ++it ) {
143 if ( (*it).uid() == (*cacheIt).uid() )
144 found = true;
147 if ( !found ) {
148 mIdMapper.removeRemoteId( mIdMapper.remoteId( (*cacheIt).uid() ) );
149 mAddrMap.remove( (*cacheIt).uid() );
153 file.close();
156 KPIM::IdMapper& ResourceCached::idMapper()
158 return mIdMapper;
161 bool ResourceCached::hasChanges() const
163 return !( mAddedAddressees.isEmpty() &&
164 mChangedAddressees.isEmpty() &&
165 mDeletedAddressees.isEmpty() );
168 void ResourceCached::clearChanges()
170 mAddedAddressees.clear();
171 mChangedAddressees.clear();
172 mDeletedAddressees.clear();
175 void ResourceCached::clearChange( const KABC::Addressee &addr )
177 mAddedAddressees.remove( addr.uid() );
178 mChangedAddressees.remove( addr.uid() );
179 mDeletedAddressees.remove( addr.uid() );
182 void ResourceCached::clearChange( const QString &uid )
184 mAddedAddressees.remove( uid );
185 mChangedAddressees.remove( uid );
186 mDeletedAddressees.remove( uid );
189 KABC::Addressee::List ResourceCached::addedAddressees() const
191 return mAddedAddressees.values();
194 KABC::Addressee::List ResourceCached::changedAddressees() const
196 return mChangedAddressees.values();
199 KABC::Addressee::List ResourceCached::deletedAddressees() const
201 return mDeletedAddressees.values();
204 QString ResourceCached::cacheFile() const
206 return locateLocal( "cache", "kabc/kresources/" + identifier() );
209 void ResourceCached::setIdMapperIdentifier()
211 mIdMapper.setIdentifier( type() + "_" + identifier() );
214 #include "kabcresourcecached.moc"