• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.9.1 API Reference
  • KDE Home
  • Contact Us
 

kabc

  • kabc
resourcecached.cpp
1 /*
2  This file is part of kabc.
3  Copyright (c) 2004 Tobias Koenig <tokoe@kde.org>
4 
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.
9 
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.
14 
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., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #include "resourcecached.h"
22 
23 #include <kabc/vcardconverter.h>
24 
25 #include <kdebug.h>
26 #include <klocale.h>
27 #include <kstandarddirs.h>
28 
29 #include <QtCore/QFile>
30 
31 using namespace KABC;
32 
33 class ResourceCached::Private
34 {
35  public:
36  Private( ResourceCached *parent )
37  : mParent( parent ), mIdMapper( QLatin1String( "kabc/uidmaps/" ) )
38  {
39  }
40 
41  void loadChangesCache( QMap<QString, KABC::Addressee> &map, const QString &type );
42  void saveChangesCache( const QMap<QString, KABC::Addressee> &map, const QString &type );
43 
44  ResourceCached *mParent;
45  KRES::IdMapper mIdMapper;
46 
47  QMap<QString, KABC::Addressee> mAddedAddressees;
48  QMap<QString, KABC::Addressee> mChangedAddressees;
49  QMap<QString, KABC::Addressee> mDeletedAddressees;
50 };
51 
52 void ResourceCached::Private::saveChangesCache( const QMap<QString, KABC::Addressee> &map,
53  const QString &type )
54 {
55  QFile file( mParent->changesCacheFile( type ) );
56 
57  const KABC::Addressee::List list = map.values();
58  if ( list.isEmpty() ) {
59  file.remove();
60  } else {
61  if ( !file.open( QIODevice::WriteOnly ) ) {
62  kError(5700) << "Can't open changes cache file '" << file.fileName() << "' for saving.";
63  return;
64  }
65 
66  KABC::VCardConverter converter;
67  const QByteArray vCards = converter.createVCards( list );
68  file.write( vCards );
69  }
70 }
71 
72 void ResourceCached::Private::loadChangesCache( QMap<QString, KABC::Addressee> &map,
73  const QString &type )
74 {
75  QFile file( mParent->changesCacheFile( type ) );
76  if ( !file.open( QIODevice::ReadOnly ) ) {
77  return;
78  }
79 
80  KABC::VCardConverter converter;
81 
82  const KABC::Addressee::List list = converter.parseVCards( file.readAll() );
83  KABC::Addressee::List::ConstIterator it;
84  for ( it = list.begin(); it != list.end(); ++it ) {
85  map.insert( (*it).uid(), *it );
86  }
87 
88  file.close();
89 }
90 
91 ResourceCached::ResourceCached()
92  : KABC::Resource(), d( new Private( this ) )
93 {
94 }
95 
96 ResourceCached::ResourceCached( const KConfigGroup &group )
97  : KABC::Resource( group ), d( new Private( this ) )
98 {
99 }
100 
101 ResourceCached::~ResourceCached()
102 {
103  delete d;
104 }
105 
106 void ResourceCached::writeConfig( KConfigGroup &group )
107 {
108  KABC::Resource::writeConfig( group );
109 }
110 
111 void ResourceCached::insertAddressee( const Addressee &addr )
112 {
113  if ( !mAddrMap.contains( addr.uid() ) ) { // new contact
114  if ( d->mDeletedAddressees.contains( addr.uid() ) ) {
115  // it was first removed, then added, so it's an update...
116  d->mDeletedAddressees.remove( addr.uid() );
117 
118  mAddrMap.insert( addr.uid(), addr );
119  d->mChangedAddressees.insert( addr.uid(), addr );
120  return;
121  }
122 
123  mAddrMap.insert( addr.uid(), addr );
124  d->mAddedAddressees.insert( addr.uid(), addr );
125  } else {
126  KABC::Addressee oldAddressee = mAddrMap.find( addr.uid() ).value();
127  if ( oldAddressee != addr ) {
128  mAddrMap.remove( addr.uid() );
129  mAddrMap.insert( addr.uid(), addr );
130  d->mChangedAddressees.insert( addr.uid(), addr );
131  }
132  }
133 }
134 
135 void ResourceCached::removeAddressee( const Addressee &addr )
136 {
137  if ( d->mAddedAddressees.contains( addr.uid() ) ) {
138  d->mAddedAddressees.remove( addr.uid() );
139  return;
140  }
141 
142  if ( d->mDeletedAddressees.find( addr.uid() ) == d->mDeletedAddressees.end() ) {
143  d->mDeletedAddressees.insert( addr.uid(), addr );
144  }
145 
146  mAddrMap.remove( addr.uid() );
147 }
148 
149 bool ResourceCached::loadFromCache()
150 {
151  mAddrMap.clear();
152 
153  setIdMapperIdentifier();
154  d->mIdMapper.load();
155 
156  // load cache
157  QFile file( cacheFile() );
158  if ( !file.open( QIODevice::ReadOnly ) ) {
159  return false;
160  }
161 
162  KABC::VCardConverter converter;
163  KABC::Addressee::List list = converter.parseVCards( file.readAll() );
164  KABC::Addressee::List::Iterator it;
165 
166  for ( it = list.begin(); it != list.end(); ++it ) {
167  (*it).setResource( this );
168  (*it).setChanged( false );
169  mAddrMap.insert( (*it).uid(), *it );
170  }
171 
172  file.close();
173  return true;
174 }
175 
176 void ResourceCached::saveToCache()
177 {
178  setIdMapperIdentifier();
179  d->mIdMapper.save();
180 
181  // save cache
182  QFile file( cacheFile() );
183  if ( !file.open( QIODevice::WriteOnly ) ) {
184  return;
185  }
186 
187  KABC::Addressee::List list = mAddrMap.values();
188 
189  KABC::VCardConverter converter;
190  QByteArray vCard = converter.createVCards( list );
191  file.write( vCard );
192  file.close();
193 }
194 
195 void ResourceCached::cleanUpCache( const KABC::Addressee::List &addrList )
196 {
197  // load cache
198  QFile file( cacheFile() );
199  if ( !file.open( QIODevice::ReadOnly ) ) {
200  return;
201  }
202 
203  KABC::VCardConverter converter;
204  KABC::Addressee::List list = converter.parseVCards( file.readAll() );
205  KABC::Addressee::List::Iterator cacheIt;
206  KABC::Addressee::List::ConstIterator it;
207 
208  for ( cacheIt = list.begin(); cacheIt != list.end(); ++cacheIt ) {
209  bool found = false;
210  for ( it = addrList.begin(); it != addrList.end(); ++it ) {
211  if ( (*it).uid() == (*cacheIt).uid() ) {
212  found = true;
213  }
214  }
215 
216  if ( !found ) {
217  d->mIdMapper.removeRemoteId( d->mIdMapper.remoteId( (*cacheIt).uid() ) );
218  mAddrMap.remove( (*cacheIt).uid() );
219  }
220  }
221 
222  file.close();
223 }
224 
225 KRES::IdMapper &ResourceCached::idMapper()
226 {
227  return d->mIdMapper;
228 }
229 
230 bool ResourceCached::hasChanges() const
231 {
232  return !( d->mAddedAddressees.isEmpty() &&
233  d->mChangedAddressees.isEmpty() &&
234  d->mDeletedAddressees.isEmpty() );
235 }
236 
237 void ResourceCached::clearChanges()
238 {
239  d->mAddedAddressees.clear();
240  d->mChangedAddressees.clear();
241  d->mDeletedAddressees.clear();
242 }
243 
244 void ResourceCached::clearChange( const KABC::Addressee &addr )
245 {
246  d->mAddedAddressees.remove( addr.uid() );
247  d->mChangedAddressees.remove( addr.uid() );
248  d->mDeletedAddressees.remove( addr.uid() );
249 }
250 
251 void ResourceCached::clearChange( const QString &uid )
252 {
253  d->mAddedAddressees.remove( uid );
254  d->mChangedAddressees.remove( uid );
255  d->mDeletedAddressees.remove( uid );
256 }
257 
258 KABC::Addressee::List ResourceCached::addedAddressees() const
259 {
260  return d->mAddedAddressees.values();
261 }
262 
263 KABC::Addressee::List ResourceCached::changedAddressees() const
264 {
265  return d->mChangedAddressees.values();
266 }
267 
268 KABC::Addressee::List ResourceCached::deletedAddressees() const
269 {
270  return d->mDeletedAddressees.values();
271 }
272 
273 QString ResourceCached::cacheFile() const
274 {
275  return KStandardDirs::locateLocal( "cache", QLatin1String( "kabc/kresources/" ) + identifier() );
276 }
277 
278 QString ResourceCached::changesCacheFile( const QString &type ) const
279 {
280  return KStandardDirs::locateLocal( "cache", QLatin1String( "kabc/changescache/" ) + identifier() +
281  QLatin1Char( '_' ) + type );
282 }
283 
284 void ResourceCached::saveChangesCache()
285 {
286  d->saveChangesCache( d->mAddedAddressees, QLatin1String( "added" ) );
287  d->saveChangesCache( d->mDeletedAddressees, QLatin1String( "deleted" ) );
288  d->saveChangesCache( d->mChangedAddressees, QLatin1String( "changed" ) );
289 }
290 
291 void ResourceCached::loadChangesCache()
292 {
293  d->loadChangesCache( d->mAddedAddressees, QLatin1String( "added" ) );
294  d->loadChangesCache( d->mDeletedAddressees, QLatin1String( "deleted" ) );
295  d->loadChangesCache( d->mChangedAddressees, QLatin1String( "changed" ) );
296 }
297 
298 void ResourceCached::setIdMapperIdentifier()
299 {
300  d->mIdMapper.setIdentifier( type() + QLatin1Char( '_' ) + identifier() );
301 }
302 
303 #include "resourcecached.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Sep 24 2012 09:09:51 by doxygen 1.8.1.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kabc

Skip menu "kabc"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs-4.9.1 API Reference

Skip menu "kdepimlibs-4.9.1 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal