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.
23 #include <kabc/vcardconverter.h>
26 #include <kstandarddirs.h>
28 #include "kabcresourcecached.h"
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
);
58 mAddrMap
.insert( addr
.uid(), addr
);
59 mAddedAddressees
.insert( addr
.uid(), addr
);
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() );
77 if ( mDeletedAddressees
.find( addr
.uid() ) == mDeletedAddressees
.end() )
78 mDeletedAddressees
.insert( addr
.uid(), addr
);
80 mAddrMap
.remove( addr
.uid() );
83 void ResourceCached::loadCache()
87 setIdMapperIdentifier();
91 QFile
file( cacheFile() );
92 if ( !file
.open( IO_ReadOnly
) )
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
);
109 void ResourceCached::saveCache()
111 setIdMapperIdentifier();
115 QFile
file( cacheFile() );
116 if ( !file
.open( IO_WriteOnly
) )
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() );
127 void ResourceCached::cleanUpCache( const KABC::Addressee::List
&addrList
)
130 QFile
file( cacheFile() );
131 if ( !file
.open( IO_ReadOnly
) )
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
) {
142 for ( it
= addrList
.begin(); it
!= addrList
.end(); ++it
) {
143 if ( (*it
).uid() == (*cacheIt
).uid() )
148 mIdMapper
.removeRemoteId( mIdMapper
.remoteId( (*cacheIt
).uid() ) );
149 mAddrMap
.remove( (*cacheIt
).uid() );
156 KPIM::IdMapper
& ResourceCached::idMapper()
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"