summaryrefslogtreecommitdiffabout
path: root/kabc/addressbook.cpp
Unidiff
Diffstat (limited to 'kabc/addressbook.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--kabc/addressbook.cpp2
1 files changed, 1 insertions, 1 deletions
diff --git a/kabc/addressbook.cpp b/kabc/addressbook.cpp
index 592d78d..c584c35 100644
--- a/kabc/addressbook.cpp
+++ b/kabc/addressbook.cpp
@@ -1,1048 +1,1048 @@
1/* 1/*
2 This file is part of libkabc. 2 This file is part of libkabc.
3 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> 3 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
4 4
5 This library is free software; you can redistribute it and/or 5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public 6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either 7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version. 8 version 2 of the License, or (at your option) any later version.
9 9
10 This library is distributed in the hope that it will be useful, 10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details. 13 Library General Public License for more details.
14 14
15 You should have received a copy of the GNU Library General Public License 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 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, 17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. 18 Boston, MA 02111-1307, USA.
19*/ 19*/
20 20
21/* 21/*
22Enhanced Version of the file for platform independent KDE tools. 22Enhanced Version of the file for platform independent KDE tools.
23Copyright (c) 2004 Ulf Schenk 23Copyright (c) 2004 Ulf Schenk
24 24
25$Id$ 25$Id$
26*/ 26*/
27 27
28/*US 28/*US
29 29
30#include <qfile.h> 30#include <qfile.h>
31#include <qregexp.h> 31#include <qregexp.h>
32#include <qtimer.h> 32#include <qtimer.h>
33 33
34#include <kapplication.h> 34#include <kapplication.h>
35#include <kinstance.h> 35#include <kinstance.h>
36#include <kstandarddirs.h> 36#include <kstandarddirs.h>
37 37
38#include "errorhandler.h" 38#include "errorhandler.h"
39*/ 39*/
40#include <qptrlist.h> 40#include <qptrlist.h>
41#include <qtextstream.h> 41#include <qtextstream.h>
42#include <qfile.h> 42#include <qfile.h>
43#include <qregexp.h> 43#include <qregexp.h>
44 44
45#include <kglobal.h> 45#include <kglobal.h>
46#include <klocale.h> 46#include <klocale.h>
47#include <kmessagebox.h> 47#include <kmessagebox.h>
48#include <kdebug.h> 48#include <kdebug.h>
49#include <libkcal/syncdefines.h> 49#include <libkcal/syncdefines.h>
50#include <libkdepim/phoneaccess.h> 50#include <libkdepim/phoneaccess.h>
51#include "addressbook.h" 51#include "addressbook.h"
52#include "resource.h" 52#include "resource.h"
53#include "vcardconverter.h" 53#include "vcardconverter.h"
54#include "vcardparser/vcardtool.h" 54#include "vcardparser/vcardtool.h"
55 55
56//US #include "addressbook.moc" 56//US #include "addressbook.moc"
57 57
58using namespace KABC; 58using namespace KABC;
59 59
60struct AddressBook::AddressBookData 60struct AddressBook::AddressBookData
61{ 61{
62 Addressee::List mAddressees; 62 Addressee::List mAddressees;
63 Addressee::List mRemovedAddressees; 63 Addressee::List mRemovedAddressees;
64 Field::List mAllFields; 64 Field::List mAllFields;
65 KConfig *mConfig; 65 KConfig *mConfig;
66 KRES::Manager<Resource> *mManager; 66 KRES::Manager<Resource> *mManager;
67//US ErrorHandler *mErrorHandler; 67//US ErrorHandler *mErrorHandler;
68}; 68};
69 69
70struct AddressBook::Iterator::IteratorData 70struct AddressBook::Iterator::IteratorData
71{ 71{
72 Addressee::List::Iterator mIt; 72 Addressee::List::Iterator mIt;
73}; 73};
74 74
75struct AddressBook::ConstIterator::ConstIteratorData 75struct AddressBook::ConstIterator::ConstIteratorData
76{ 76{
77 Addressee::List::ConstIterator mIt; 77 Addressee::List::ConstIterator mIt;
78}; 78};
79 79
80AddressBook::Iterator::Iterator() 80AddressBook::Iterator::Iterator()
81{ 81{
82 d = new IteratorData; 82 d = new IteratorData;
83} 83}
84 84
85AddressBook::Iterator::Iterator( const AddressBook::Iterator &i ) 85AddressBook::Iterator::Iterator( const AddressBook::Iterator &i )
86{ 86{
87 d = new IteratorData; 87 d = new IteratorData;
88 d->mIt = i.d->mIt; 88 d->mIt = i.d->mIt;
89} 89}
90 90
91AddressBook::Iterator &AddressBook::Iterator::operator=( const AddressBook::Iterator &i ) 91AddressBook::Iterator &AddressBook::Iterator::operator=( const AddressBook::Iterator &i )
92{ 92{
93 if( this == &i ) return *this; // guard against self assignment 93 if( this == &i ) return *this; // guard against self assignment
94 delete d; // delete the old data the Iterator was completely constructed before 94 delete d; // delete the old data the Iterator was completely constructed before
95 d = new IteratorData; 95 d = new IteratorData;
96 d->mIt = i.d->mIt; 96 d->mIt = i.d->mIt;
97 return *this; 97 return *this;
98} 98}
99 99
100AddressBook::Iterator::~Iterator() 100AddressBook::Iterator::~Iterator()
101{ 101{
102 delete d; 102 delete d;
103} 103}
104 104
105const Addressee &AddressBook::Iterator::operator*() const 105const Addressee &AddressBook::Iterator::operator*() const
106{ 106{
107 return *(d->mIt); 107 return *(d->mIt);
108} 108}
109 109
110Addressee &AddressBook::Iterator::operator*() 110Addressee &AddressBook::Iterator::operator*()
111{ 111{
112 return *(d->mIt); 112 return *(d->mIt);
113} 113}
114 114
115Addressee *AddressBook::Iterator::operator->() 115Addressee *AddressBook::Iterator::operator->()
116{ 116{
117 return &(*(d->mIt)); 117 return &(*(d->mIt));
118} 118}
119 119
120AddressBook::Iterator &AddressBook::Iterator::operator++() 120AddressBook::Iterator &AddressBook::Iterator::operator++()
121{ 121{
122 (d->mIt)++; 122 (d->mIt)++;
123 return *this; 123 return *this;
124} 124}
125 125
126AddressBook::Iterator &AddressBook::Iterator::operator++(int) 126AddressBook::Iterator &AddressBook::Iterator::operator++(int)
127{ 127{
128 (d->mIt)++; 128 (d->mIt)++;
129 return *this; 129 return *this;
130} 130}
131 131
132AddressBook::Iterator &AddressBook::Iterator::operator--() 132AddressBook::Iterator &AddressBook::Iterator::operator--()
133{ 133{
134 (d->mIt)--; 134 (d->mIt)--;
135 return *this; 135 return *this;
136} 136}
137 137
138AddressBook::Iterator &AddressBook::Iterator::operator--(int) 138AddressBook::Iterator &AddressBook::Iterator::operator--(int)
139{ 139{
140 (d->mIt)--; 140 (d->mIt)--;
141 return *this; 141 return *this;
142} 142}
143 143
144bool AddressBook::Iterator::operator==( const Iterator &it ) 144bool AddressBook::Iterator::operator==( const Iterator &it )
145{ 145{
146 return ( d->mIt == it.d->mIt ); 146 return ( d->mIt == it.d->mIt );
147} 147}
148 148
149bool AddressBook::Iterator::operator!=( const Iterator &it ) 149bool AddressBook::Iterator::operator!=( const Iterator &it )
150{ 150{
151 return ( d->mIt != it.d->mIt ); 151 return ( d->mIt != it.d->mIt );
152} 152}
153 153
154 154
155AddressBook::ConstIterator::ConstIterator() 155AddressBook::ConstIterator::ConstIterator()
156{ 156{
157 d = new ConstIteratorData; 157 d = new ConstIteratorData;
158} 158}
159 159
160AddressBook::ConstIterator::ConstIterator( const AddressBook::ConstIterator &i ) 160AddressBook::ConstIterator::ConstIterator( const AddressBook::ConstIterator &i )
161{ 161{
162 d = new ConstIteratorData; 162 d = new ConstIteratorData;
163 d->mIt = i.d->mIt; 163 d->mIt = i.d->mIt;
164} 164}
165 165
166AddressBook::ConstIterator &AddressBook::ConstIterator::operator=( const AddressBook::ConstIterator &i ) 166AddressBook::ConstIterator &AddressBook::ConstIterator::operator=( const AddressBook::ConstIterator &i )
167{ 167{
168 if( this == &i ) return *this; // guard for self assignment 168 if( this == &i ) return *this; // guard for self assignment
169 delete d; // delete the old data because the Iterator was really constructed before 169 delete d; // delete the old data because the Iterator was really constructed before
170 d = new ConstIteratorData; 170 d = new ConstIteratorData;
171 d->mIt = i.d->mIt; 171 d->mIt = i.d->mIt;
172 return *this; 172 return *this;
173} 173}
174 174
175AddressBook::ConstIterator::~ConstIterator() 175AddressBook::ConstIterator::~ConstIterator()
176{ 176{
177 delete d; 177 delete d;
178} 178}
179 179
180const Addressee &AddressBook::ConstIterator::operator*() const 180const Addressee &AddressBook::ConstIterator::operator*() const
181{ 181{
182 return *(d->mIt); 182 return *(d->mIt);
183} 183}
184 184
185const Addressee* AddressBook::ConstIterator::operator->() const 185const Addressee* AddressBook::ConstIterator::operator->() const
186{ 186{
187 return &(*(d->mIt)); 187 return &(*(d->mIt));
188} 188}
189 189
190AddressBook::ConstIterator &AddressBook::ConstIterator::operator++() 190AddressBook::ConstIterator &AddressBook::ConstIterator::operator++()
191{ 191{
192 (d->mIt)++; 192 (d->mIt)++;
193 return *this; 193 return *this;
194} 194}
195 195
196AddressBook::ConstIterator &AddressBook::ConstIterator::operator++(int) 196AddressBook::ConstIterator &AddressBook::ConstIterator::operator++(int)
197{ 197{
198 (d->mIt)++; 198 (d->mIt)++;
199 return *this; 199 return *this;
200} 200}
201 201
202AddressBook::ConstIterator &AddressBook::ConstIterator::operator--() 202AddressBook::ConstIterator &AddressBook::ConstIterator::operator--()
203{ 203{
204 (d->mIt)--; 204 (d->mIt)--;
205 return *this; 205 return *this;
206} 206}
207 207
208AddressBook::ConstIterator &AddressBook::ConstIterator::operator--(int) 208AddressBook::ConstIterator &AddressBook::ConstIterator::operator--(int)
209{ 209{
210 (d->mIt)--; 210 (d->mIt)--;
211 return *this; 211 return *this;
212} 212}
213 213
214bool AddressBook::ConstIterator::operator==( const ConstIterator &it ) 214bool AddressBook::ConstIterator::operator==( const ConstIterator &it )
215{ 215{
216 return ( d->mIt == it.d->mIt ); 216 return ( d->mIt == it.d->mIt );
217} 217}
218 218
219bool AddressBook::ConstIterator::operator!=( const ConstIterator &it ) 219bool AddressBook::ConstIterator::operator!=( const ConstIterator &it )
220{ 220{
221 return ( d->mIt != it.d->mIt ); 221 return ( d->mIt != it.d->mIt );
222} 222}
223 223
224 224
225AddressBook::AddressBook() 225AddressBook::AddressBook()
226{ 226{
227 init(0, "contact"); 227 init(0, "contact");
228} 228}
229 229
230AddressBook::AddressBook( const QString &config ) 230AddressBook::AddressBook( const QString &config )
231{ 231{
232 init(config, "contact"); 232 init(config, "contact");
233} 233}
234 234
235AddressBook::AddressBook( const QString &config, const QString &family ) 235AddressBook::AddressBook( const QString &config, const QString &family )
236{ 236{
237 init(config, family); 237 init(config, family);
238 238
239} 239}
240 240
241// the default family is "contact" 241// the default family is "contact"
242void AddressBook::init(const QString &config, const QString &family ) 242void AddressBook::init(const QString &config, const QString &family )
243{ 243{
244 blockLSEchange = false; 244 blockLSEchange = false;
245 d = new AddressBookData; 245 d = new AddressBookData;
246 QString fami = family; 246 QString fami = family;
247 if (config != 0) { 247 if (config != 0) {
248 if ( family == "syncContact" ) { 248 if ( family == "syncContact" ) {
249 qDebug("creating sync config "); 249 qDebug("creating sync config ");
250 fami = "contact"; 250 fami = "contact";
251 KConfig* con = new KConfig( locateLocal("config", "syncContactrc") ); 251 KConfig* con = new KConfig( locateLocal("config", "syncContactrc") );
252 con->setGroup( "General" ); 252 con->setGroup( "General" );
253 con->writeEntry( "ResourceKeys", QString("sync") ); 253 con->writeEntry( "ResourceKeys", QString("sync") );
254 con->writeEntry( "Standard", QString("sync") ); 254 con->writeEntry( "Standard", QString("sync") );
255 con->setGroup( "Resource_sync" ); 255 con->setGroup( "Resource_sync" );
256 con->writeEntry( "FileName", config ); 256 con->writeEntry( "FileName", config );
257 con->writeEntry( "FileFormat", QString("vcard") ); 257 con->writeEntry( "FileFormat", QString("vcard") );
258 con->writeEntry( "ResourceIdentifier", QString("sync") ); 258 con->writeEntry( "ResourceIdentifier", QString("sync") );
259 con->writeEntry( "ResourceName", QString("sync_res") ); 259 con->writeEntry( "ResourceName", QString("sync_res") );
260 if ( config.right(4) == ".xml" ) 260 if ( config.right(4) == ".xml" )
261 con->writeEntry( "ResourceType", QString("qtopia") ); 261 con->writeEntry( "ResourceType", QString("qtopia") );
262 else if ( config == "sharp" ) { 262 else if ( config == "sharp" ) {
263 con->writeEntry( "ResourceType", QString("sharp") ); 263 con->writeEntry( "ResourceType", QString("sharp") );
264 } else { 264 } else {
265 con->writeEntry( "ResourceType", QString("file") ); 265 con->writeEntry( "ResourceType", QString("file") );
266 } 266 }
267 //con->sync(); 267 //con->sync();
268 d->mConfig = con; 268 d->mConfig = con;
269 } 269 }
270 else 270 else
271 d->mConfig = new KConfig( locateLocal("config", config) ); 271 d->mConfig = new KConfig( locateLocal("config", config) );
272// qDebug("AddressBook::init 1 config=%s",config.latin1() ); 272// qDebug("AddressBook::init 1 config=%s",config.latin1() );
273 } 273 }
274 else { 274 else {
275 d->mConfig = 0; 275 d->mConfig = 0;
276// qDebug("AddressBook::init 1 config=0"); 276// qDebug("AddressBook::init 1 config=0");
277 } 277 }
278 278
279//US d->mErrorHandler = 0; 279//US d->mErrorHandler = 0;
280 d->mManager = new KRES::Manager<Resource>( fami, false ); 280 d->mManager = new KRES::Manager<Resource>( fami );
281 d->mManager->readConfig( d->mConfig ); 281 d->mManager->readConfig( d->mConfig );
282 if ( family == "syncContact" ) { 282 if ( family == "syncContact" ) {
283 KRES::Manager<Resource> *manager = d->mManager; 283 KRES::Manager<Resource> *manager = d->mManager;
284 KRES::Manager<Resource>::ActiveIterator it; 284 KRES::Manager<Resource>::ActiveIterator it;
285 for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) { 285 for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) {
286 (*it)->setAddressBook( this ); 286 (*it)->setAddressBook( this );
287 if ( !(*it)->open() ) 287 if ( !(*it)->open() )
288 error( QString( "Unable to open resource '%1'!" ).arg( (*it)->resourceName() ) ); 288 error( QString( "Unable to open resource '%1'!" ).arg( (*it)->resourceName() ) );
289 } 289 }
290 Resource *res = standardResource(); 290 Resource *res = standardResource();
291 if ( !res ) { 291 if ( !res ) {
292 qDebug("ERROR: no standard resource"); 292 qDebug("ERROR: no standard resource");
293 res = manager->createResource( "file" ); 293 res = manager->createResource( "file" );
294 if ( res ) 294 if ( res )
295 { 295 {
296 addResource( res ); 296 addResource( res );
297 } 297 }
298 else 298 else
299 qDebug(" No resource available!!!"); 299 qDebug(" No resource available!!!");
300 } 300 }
301 setStandardResource( res ); 301 setStandardResource( res );
302 manager->writeConfig(); 302 manager->writeConfig();
303 } 303 }
304 addCustomField( i18n( "Department" ), KABC::Field::Organization, 304 addCustomField( i18n( "Department" ), KABC::Field::Organization,
305 "X-Department", "KADDRESSBOOK" ); 305 "X-Department", "KADDRESSBOOK" );
306 addCustomField( i18n( "Profession" ), KABC::Field::Organization, 306 addCustomField( i18n( "Profession" ), KABC::Field::Organization,
307 "X-Profession", "KADDRESSBOOK" ); 307 "X-Profession", "KADDRESSBOOK" );
308 addCustomField( i18n( "Assistant's Name" ), KABC::Field::Organization, 308 addCustomField( i18n( "Assistant's Name" ), KABC::Field::Organization,
309 "X-AssistantsName", "KADDRESSBOOK" ); 309 "X-AssistantsName", "KADDRESSBOOK" );
310 addCustomField( i18n( "Manager's Name" ), KABC::Field::Organization, 310 addCustomField( i18n( "Manager's Name" ), KABC::Field::Organization,
311 "X-ManagersName", "KADDRESSBOOK" ); 311 "X-ManagersName", "KADDRESSBOOK" );
312 addCustomField( i18n( "Spouse's Name" ), KABC::Field::Personal, 312 addCustomField( i18n( "Spouse's Name" ), KABC::Field::Personal,
313 "X-SpousesName", "KADDRESSBOOK" ); 313 "X-SpousesName", "KADDRESSBOOK" );
314 addCustomField( i18n( "Office" ), KABC::Field::Personal, 314 addCustomField( i18n( "Office" ), KABC::Field::Personal,
315 "X-Office", "KADDRESSBOOK" ); 315 "X-Office", "KADDRESSBOOK" );
316 addCustomField( i18n( "IM Address" ), KABC::Field::Personal, 316 addCustomField( i18n( "IM Address" ), KABC::Field::Personal,
317 "X-IMAddress", "KADDRESSBOOK" ); 317 "X-IMAddress", "KADDRESSBOOK" );
318 addCustomField( i18n( "Anniversary" ), KABC::Field::Personal, 318 addCustomField( i18n( "Anniversary" ), KABC::Field::Personal,
319 "X-Anniversary", "KADDRESSBOOK" ); 319 "X-Anniversary", "KADDRESSBOOK" );
320 320
321 //US added this field to become compatible with Opie/qtopia addressbook 321 //US added this field to become compatible with Opie/qtopia addressbook
322 // values can be "female" or "male" or "". An empty field represents undefined. 322 // values can be "female" or "male" or "". An empty field represents undefined.
323 addCustomField( i18n( "Gender" ), KABC::Field::Personal, 323 addCustomField( i18n( "Gender" ), KABC::Field::Personal,
324 "X-Gender", "KADDRESSBOOK" ); 324 "X-Gender", "KADDRESSBOOK" );
325 addCustomField( i18n( "Children" ), KABC::Field::Personal, 325 addCustomField( i18n( "Children" ), KABC::Field::Personal,
326 "X-Children", "KADDRESSBOOK" ); 326 "X-Children", "KADDRESSBOOK" );
327 addCustomField( i18n( "FreeBusyUrl" ), KABC::Field::Personal, 327 addCustomField( i18n( "FreeBusyUrl" ), KABC::Field::Personal,
328 "X-FreeBusyUrl", "KADDRESSBOOK" ); 328 "X-FreeBusyUrl", "KADDRESSBOOK" );
329 addCustomField( i18n( "ExternalID" ), KABC::Field::Personal, 329 addCustomField( i18n( "ExternalID" ), KABC::Field::Personal,
330 "X-ExternalID", "KADDRESSBOOK" ); 330 "X-ExternalID", "KADDRESSBOOK" );
331} 331}
332 332
333AddressBook::~AddressBook() 333AddressBook::~AddressBook()
334{ 334{
335 delete d->mConfig; d->mConfig = 0; 335 delete d->mConfig; d->mConfig = 0;
336 delete d->mManager; d->mManager = 0; 336 delete d->mManager; d->mManager = 0;
337//US delete d->mErrorHandler; d->mErrorHandler = 0; 337//US delete d->mErrorHandler; d->mErrorHandler = 0;
338 delete d; d = 0; 338 delete d; d = 0;
339} 339}
340 340
341bool AddressBook::load() 341bool AddressBook::load()
342{ 342{
343 343
344 clear(); 344 clear();
345 KRES::Manager<Resource>::ActiveIterator it; 345 KRES::Manager<Resource>::ActiveIterator it;
346 bool ok = true; 346 bool ok = true;
347 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) 347 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it )
348 if ( !(*it)->load() ) { 348 if ( !(*it)->load() ) {
349 qDebug( i18n("Unable to load resource '%1'").arg( (*it)->resourceName() ) ); 349 qDebug( i18n("Unable to load resource '%1'").arg( (*it)->resourceName() ) );
350 ok = false; 350 ok = false;
351 } else { 351 } else {
352 qDebug( i18n("Resource loaded: '%1'").arg( (*it)->resourceName() ) ); 352 qDebug( i18n("Resource loaded: '%1'").arg( (*it)->resourceName() ) );
353 } 353 }
354 // mark all addressees as unchanged 354 // mark all addressees as unchanged
355 Addressee::List::Iterator addrIt; 355 Addressee::List::Iterator addrIt;
356 for ( addrIt = d->mAddressees.begin(); addrIt != d->mAddressees.end(); ++addrIt ) { 356 for ( addrIt = d->mAddressees.begin(); addrIt != d->mAddressees.end(); ++addrIt ) {
357 (*addrIt).setChanged( false ); 357 (*addrIt).setChanged( false );
358 QString id = (*addrIt).custom( "KADDRESSBOOK", "X-ExternalID" ); 358 QString id = (*addrIt).custom( "KADDRESSBOOK", "X-ExternalID" );
359 if ( !id.isEmpty() ) { 359 if ( !id.isEmpty() ) {
360 //qDebug("setId aa %s ", id.latin1()); 360 //qDebug("setId aa %s ", id.latin1());
361 (*addrIt).setIDStr(id ); 361 (*addrIt).setIDStr(id );
362 } 362 }
363 } 363 }
364 blockLSEchange = true; 364 blockLSEchange = true;
365 return ok; 365 return ok;
366} 366}
367 367
368bool AddressBook::save( Ticket *ticket ) 368bool AddressBook::save( Ticket *ticket )
369{ 369{
370 kdDebug(5700) << "AddressBook::save()"<< endl; 370 kdDebug(5700) << "AddressBook::save()"<< endl;
371 371
372 if ( ticket->resource() ) { 372 if ( ticket->resource() ) {
373 deleteRemovedAddressees(); 373 deleteRemovedAddressees();
374 return ticket->resource()->save( ticket ); 374 return ticket->resource()->save( ticket );
375 } 375 }
376 376
377 return false; 377 return false;
378} 378}
379// exports all Addressees, which are syncable 379// exports all Addressees, which are syncable
380void AddressBook::export2File( QString fileName ) 380void AddressBook::export2File( QString fileName )
381{ 381{
382 382
383 QFile outFile( fileName ); 383 QFile outFile( fileName );
384 if ( !outFile.open( IO_WriteOnly ) ) { 384 if ( !outFile.open( IO_WriteOnly ) ) {
385 QString text = i18n( "<qt>Unable to open file <b>%1</b> for export.</qt>" ); 385 QString text = i18n( "<qt>Unable to open file <b>%1</b> for export.</qt>" );
386 KMessageBox::error( 0, text.arg( fileName ) ); 386 KMessageBox::error( 0, text.arg( fileName ) );
387 return ; 387 return ;
388 } 388 }
389 QTextStream t( &outFile ); 389 QTextStream t( &outFile );
390 t.setEncoding( QTextStream::UnicodeUTF8 ); 390 t.setEncoding( QTextStream::UnicodeUTF8 );
391 Iterator it; 391 Iterator it;
392 KABC::VCardConverter::Version version; 392 KABC::VCardConverter::Version version;
393 version = KABC::VCardConverter::v3_0; 393 version = KABC::VCardConverter::v3_0;
394 for ( it = begin(); it != end(); ++it ) { 394 for ( it = begin(); it != end(); ++it ) {
395 if ( (*it).resource() && (*it).resource()->includeInSync() ) { 395 if ( (*it).resource() && (*it).resource()->includeInSync() ) {
396 if ( !(*it).IDStr().isEmpty() ) { 396 if ( !(*it).IDStr().isEmpty() ) {
397 (*it).insertCustom( "KADDRESSBOOK", "X-ExternalID", (*it).IDStr() ); 397 (*it).insertCustom( "KADDRESSBOOK", "X-ExternalID", (*it).IDStr() );
398 } 398 }
399 KABC::VCardConverter converter; 399 KABC::VCardConverter converter;
400 QString vcard; 400 QString vcard;
401 //Resource *resource() const; 401 //Resource *resource() const;
402 converter.addresseeToVCard( *it, vcard, version ); 402 converter.addresseeToVCard( *it, vcard, version );
403 t << vcard << "\r\n"; 403 t << vcard << "\r\n";
404 } 404 }
405 } 405 }
406 t << "\r\n\r\n"; 406 t << "\r\n\r\n";
407 outFile.close(); 407 outFile.close();
408} 408}
409// if QStringList uids is empty, all are exported 409// if QStringList uids is empty, all are exported
410bool AddressBook::export2PhoneFormat( QStringList uids ,QString fileName ) 410bool AddressBook::export2PhoneFormat( QStringList uids ,QString fileName )
411{ 411{
412 KABC::VCardConverter converter; 412 KABC::VCardConverter converter;
413 QString datastream; 413 QString datastream;
414 Iterator it; 414 Iterator it;
415 bool all = uids.isEmpty(); 415 bool all = uids.isEmpty();
416 for ( it = begin(); it != end(); ++it ) { 416 for ( it = begin(); it != end(); ++it ) {
417 // for( QStringList::ConstIterator it = uids.begin(); it != uids.end(); ++it ) { 417 // for( QStringList::ConstIterator it = uids.begin(); it != uids.end(); ++it ) {
418 if ( ! all ) { 418 if ( ! all ) {
419 if ( ! ( uids.contains((*it).uid() ) )) 419 if ( ! ( uids.contains((*it).uid() ) ))
420 continue; 420 continue;
421 } 421 }
422 KABC::Addressee a = ( *it ); 422 KABC::Addressee a = ( *it );
423 if ( a.isEmpty() ) 423 if ( a.isEmpty() )
424 continue; 424 continue;
425 if ( all && a.resource() && !a.resource()->includeInSync() ) 425 if ( all && a.resource() && !a.resource()->includeInSync() )
426 continue; 426 continue;
427 a.simplifyEmails(); 427 a.simplifyEmails();
428 a.simplifyPhoneNumbers(); 428 a.simplifyPhoneNumbers();
429 a.simplifyPhoneNumberTypes(); 429 a.simplifyPhoneNumberTypes();
430 a.simplifyAddresses(); 430 a.simplifyAddresses();
431 431
432 QString vcard; 432 QString vcard;
433 QString vcardnew; 433 QString vcardnew;
434 converter.addresseeToVCard( a, vcard ); 434 converter.addresseeToVCard( a, vcard );
435 int start = 0; 435 int start = 0;
436 int next; 436 int next;
437 while ( (next = vcard.find("TYPE=", start) )>= 0 ) { 437 while ( (next = vcard.find("TYPE=", start) )>= 0 ) {
438 int semi = vcard.find(";", next); 438 int semi = vcard.find(";", next);
439 int dopp = vcard.find(":", next); 439 int dopp = vcard.find(":", next);
440 int sep; 440 int sep;
441 if ( semi < dopp && semi >= 0 ) 441 if ( semi < dopp && semi >= 0 )
442 sep = semi ; 442 sep = semi ;
443 else 443 else
444 sep = dopp; 444 sep = dopp;
445 vcardnew +=vcard.mid( start, next - start); 445 vcardnew +=vcard.mid( start, next - start);
446 vcardnew +=vcard.mid( next+5,sep -next -5 ).upper(); 446 vcardnew +=vcard.mid( next+5,sep -next -5 ).upper();
447 start = sep; 447 start = sep;
448 } 448 }
449 vcardnew += vcard.mid( start,vcard.length() ); 449 vcardnew += vcard.mid( start,vcard.length() );
450 vcard = ""; 450 vcard = "";
451 start = 0; 451 start = 0;
452 while ( (next = vcardnew.find("ADR", start) )>= 0 ) { 452 while ( (next = vcardnew.find("ADR", start) )>= 0 ) {
453 int sep = vcardnew.find(":", next); 453 int sep = vcardnew.find(":", next);
454 vcard +=vcardnew.mid( start, next - start+3); 454 vcard +=vcardnew.mid( start, next - start+3);
455 start = sep; 455 start = sep;
456 } 456 }
457 vcard += vcardnew.mid( start,vcardnew.length() ); 457 vcard += vcardnew.mid( start,vcardnew.length() );
458 vcard.replace ( QRegExp(";;;") , "" ); 458 vcard.replace ( QRegExp(";;;") , "" );
459 vcard.replace ( QRegExp(";;") , "" ); 459 vcard.replace ( QRegExp(";;") , "" );
460 datastream += vcard; 460 datastream += vcard;
461 461
462 } 462 }
463 463
464 QFile outFile(fileName); 464 QFile outFile(fileName);
465 if ( outFile.open(IO_WriteOnly) ) { 465 if ( outFile.open(IO_WriteOnly) ) {
466 datastream.replace ( QRegExp("VERSION:3.0") , "VERSION:2.1" ); 466 datastream.replace ( QRegExp("VERSION:3.0") , "VERSION:2.1" );
467 QTextStream t( &outFile ); // use a text stream 467 QTextStream t( &outFile ); // use a text stream
468 t.setEncoding( QTextStream::UnicodeUTF8 ); 468 t.setEncoding( QTextStream::UnicodeUTF8 );
469 t <<datastream; 469 t <<datastream;
470 t << "\r\n\r\n"; 470 t << "\r\n\r\n";
471 outFile.close(); 471 outFile.close();
472 472
473 } else { 473 } else {
474 qDebug("Error open temp file "); 474 qDebug("Error open temp file ");
475 return false; 475 return false;
476 } 476 }
477 return true; 477 return true;
478 478
479} 479}
480void AddressBook::importFromFile( QString fileName, bool replaceLabel, bool removeOld ) 480void AddressBook::importFromFile( QString fileName, bool replaceLabel, bool removeOld )
481{ 481{
482 482
483 if ( removeOld ) 483 if ( removeOld )
484 setUntagged( true ); 484 setUntagged( true );
485 KABC::Addressee::List list; 485 KABC::Addressee::List list;
486 QFile file( fileName ); 486 QFile file( fileName );
487 file.open( IO_ReadOnly ); 487 file.open( IO_ReadOnly );
488 QByteArray rawData = file.readAll(); 488 QByteArray rawData = file.readAll();
489 file.close(); 489 file.close();
490 QString data; 490 QString data;
491 if ( replaceLabel ) { 491 if ( replaceLabel ) {
492 data = QString::fromLatin1( rawData.data(), rawData.size() + 1 ); 492 data = QString::fromLatin1( rawData.data(), rawData.size() + 1 );
493 data.replace ( QRegExp("LABEL") , "ADR" ); 493 data.replace ( QRegExp("LABEL") , "ADR" );
494 data.replace ( QRegExp("CHARSET=ISO-8859-1") , "" ); 494 data.replace ( QRegExp("CHARSET=ISO-8859-1") , "" );
495 } else 495 } else
496 data = QString::fromUtf8( rawData.data(), rawData.size() + 1 ); 496 data = QString::fromUtf8( rawData.data(), rawData.size() + 1 );
497 KABC::VCardTool tool; 497 KABC::VCardTool tool;
498 list = tool.parseVCards( data ); 498 list = tool.parseVCards( data );
499 KABC::Addressee::List::Iterator it; 499 KABC::Addressee::List::Iterator it;
500 for ( it = list.begin(); it != list.end(); ++it ) { 500 for ( it = list.begin(); it != list.end(); ++it ) {
501 QString id = (*it).custom( "KADDRESSBOOK", "X-ExternalID" ); 501 QString id = (*it).custom( "KADDRESSBOOK", "X-ExternalID" );
502 if ( !id.isEmpty() ) 502 if ( !id.isEmpty() )
503 (*it).setIDStr(id ); 503 (*it).setIDStr(id );
504 (*it).setResource( 0 ); 504 (*it).setResource( 0 );
505 if ( replaceLabel ) 505 if ( replaceLabel )
506 (*it).removeVoice(); 506 (*it).removeVoice();
507 if ( removeOld ) 507 if ( removeOld )
508 (*it).setTagged( true ); 508 (*it).setTagged( true );
509 insertAddressee( (*it), false, true ); 509 insertAddressee( (*it), false, true );
510 } 510 }
511 if ( removeOld ) 511 if ( removeOld )
512 removeUntagged(); 512 removeUntagged();
513} 513}
514void AddressBook::setUntagged(bool setNonSyncTagged) // = false) 514void AddressBook::setUntagged(bool setNonSyncTagged) // = false)
515{ 515{
516 Iterator ait; 516 Iterator ait;
517 for ( ait = begin(); ait != end(); ++ait ) { 517 for ( ait = begin(); ait != end(); ++ait ) {
518 if ( setNonSyncTagged ) { 518 if ( setNonSyncTagged ) {
519 if ( (*ait).resource() && ! (*ait).resource()->includeInSync() ) { 519 if ( (*ait).resource() && ! (*ait).resource()->includeInSync() ) {
520 (*ait).setTagged( true ); 520 (*ait).setTagged( true );
521 } else 521 } else
522 (*ait).setTagged( false ); 522 (*ait).setTagged( false );
523 } else 523 } else
524 (*ait).setTagged( false ); 524 (*ait).setTagged( false );
525 } 525 }
526} 526}
527void AddressBook::removeUntagged() 527void AddressBook::removeUntagged()
528{ 528{
529 Iterator ait; 529 Iterator ait;
530 bool todelete = false; 530 bool todelete = false;
531 Iterator todel; 531 Iterator todel;
532 for ( ait = begin(); ait != end(); ++ait ) { 532 for ( ait = begin(); ait != end(); ++ait ) {
533 if ( todelete ) 533 if ( todelete )
534 removeAddressee( todel ); 534 removeAddressee( todel );
535 if (!(*ait).tagged()) { 535 if (!(*ait).tagged()) {
536 todelete = true; 536 todelete = true;
537 todel = ait; 537 todel = ait;
538 } else 538 } else
539 todelete = false; 539 todelete = false;
540 } 540 }
541 if ( todelete ) 541 if ( todelete )
542 removeAddressee( todel ); 542 removeAddressee( todel );
543 deleteRemovedAddressees(); 543 deleteRemovedAddressees();
544} 544}
545void AddressBook::smplifyAddressees() 545void AddressBook::smplifyAddressees()
546{ 546{
547 Iterator ait; 547 Iterator ait;
548 for ( ait = begin(); ait != end(); ++ait ) { 548 for ( ait = begin(); ait != end(); ++ait ) {
549 (*ait).simplifyEmails(); 549 (*ait).simplifyEmails();
550 (*ait).simplifyPhoneNumbers(); 550 (*ait).simplifyPhoneNumbers();
551 (*ait).simplifyPhoneNumberTypes(); 551 (*ait).simplifyPhoneNumberTypes();
552 (*ait).simplifyAddresses(); 552 (*ait).simplifyAddresses();
553 } 553 }
554} 554}
555void AddressBook::preparePhoneSync( QString currentSyncDevice, bool isPreSync ) 555void AddressBook::preparePhoneSync( QString currentSyncDevice, bool isPreSync )
556{ 556{
557 Iterator ait; 557 Iterator ait;
558 for ( ait = begin(); ait != end(); ++ait ) { 558 for ( ait = begin(); ait != end(); ++ait ) {
559 QString id = (*ait).IDStr(); 559 QString id = (*ait).IDStr();
560 (*ait).setIDStr( ":"); 560 (*ait).setIDStr( ":");
561 (*ait).setExternalUID( id ); 561 (*ait).setExternalUID( id );
562 (*ait).setOriginalExternalUID( id ); 562 (*ait).setOriginalExternalUID( id );
563 if ( isPreSync ) 563 if ( isPreSync )
564 (*ait).setTempSyncStat( SYNC_TEMPSTATE_NEW_EXTERNAL ); 564 (*ait).setTempSyncStat( SYNC_TEMPSTATE_NEW_EXTERNAL );
565 else { 565 else {
566 (*ait).setTempSyncStat( SYNC_TEMPSTATE_NEW_ID ); 566 (*ait).setTempSyncStat( SYNC_TEMPSTATE_NEW_ID );
567 (*ait).setID( currentSyncDevice,id ); 567 (*ait).setID( currentSyncDevice,id );
568 568
569 } 569 }
570 } 570 }
571} 571}
572void AddressBook::findNewExtIds( QString fileName, QString currentSyncDevice ) 572void AddressBook::findNewExtIds( QString fileName, QString currentSyncDevice )
573{ 573{
574 574
575 setUntagged(); 575 setUntagged();
576 KABC::Addressee::List list; 576 KABC::Addressee::List list;
577 QFile file( fileName ); 577 QFile file( fileName );
578 file.open( IO_ReadOnly ); 578 file.open( IO_ReadOnly );
579 QByteArray rawData = file.readAll(); 579 QByteArray rawData = file.readAll();
580 file.close(); 580 file.close();
581 QString data; 581 QString data;
582 582
583 data = QString::fromUtf8( rawData.data(), rawData.size() + 1 ); 583 data = QString::fromUtf8( rawData.data(), rawData.size() + 1 );
584 KABC::VCardTool tool; 584 KABC::VCardTool tool;
585 list = tool.parseVCards( data ); 585 list = tool.parseVCards( data );
586 KABC::Addressee::List::Iterator it; 586 KABC::Addressee::List::Iterator it;
587 for ( it = list.begin(); it != list.end(); ++it ) { 587 for ( it = list.begin(); it != list.end(); ++it ) {
588 Iterator ait; 588 Iterator ait;
589 for ( ait = begin(); ait != end(); ++ait ) { 589 for ( ait = begin(); ait != end(); ++ait ) {
590 if ( !(*ait).tagged() ) { 590 if ( !(*ait).tagged() ) {
591 if ( (*ait).containsAdr(*it)) { 591 if ( (*ait).containsAdr(*it)) {
592 (*ait).setTagged(true); 592 (*ait).setTagged(true);
593 QString id = (*it).custom( "KADDRESSBOOK", "X-ExternalID" ); 593 QString id = (*it).custom( "KADDRESSBOOK", "X-ExternalID" );
594 (*it).setIDStr( ":"); 594 (*it).setIDStr( ":");
595 (*it).setID( currentSyncDevice,id ); 595 (*it).setID( currentSyncDevice,id );
596 (*it).setExternalUID( id ); 596 (*it).setExternalUID( id );
597 (*it).setTempSyncStat( SYNC_TEMPSTATE_NEW_ID ); 597 (*it).setTempSyncStat( SYNC_TEMPSTATE_NEW_ID );
598 (*it).setUid( ( (*ait).uid() )); 598 (*it).setUid( ( (*ait).uid() ));
599 break; 599 break;
600 } 600 }
601 } 601 }
602 602
603 } 603 }
604 if ( ait == end() ) 604 if ( ait == end() )
605 qDebug("ERROR:: no ext ID found for uid: %s", (*it).uid().latin1()); 605 qDebug("ERROR:: no ext ID found for uid: %s", (*it).uid().latin1());
606 } 606 }
607 clear(); 607 clear();
608 for ( it = list.begin(); it != list.end(); ++it ) { 608 for ( it = list.begin(); it != list.end(); ++it ) {
609 insertAddressee( (*it) ); 609 insertAddressee( (*it) );
610 } 610 }
611} 611}
612 612
613bool AddressBook::saveABphone( QString fileName ) 613bool AddressBook::saveABphone( QString fileName )
614{ 614{
615 //smplifyAddressees(); 615 //smplifyAddressees();
616 qDebug("saveABphone:: saving AB... "); 616 qDebug("saveABphone:: saving AB... ");
617 if ( ! export2PhoneFormat( QStringList() ,fileName ) ) 617 if ( ! export2PhoneFormat( QStringList() ,fileName ) )
618 return false; 618 return false;
619 qDebug("saveABphone:: writing to phone... "); 619 qDebug("saveABphone:: writing to phone... ");
620 if ( !PhoneAccess::writeToPhone( fileName) ) { 620 if ( !PhoneAccess::writeToPhone( fileName) ) {
621 return false; 621 return false;
622 } 622 }
623 qDebug("saveABphone:: re-reading from phone... "); 623 qDebug("saveABphone:: re-reading from phone... ");
624 if ( !PhoneAccess::readFromPhone( fileName) ) { 624 if ( !PhoneAccess::readFromPhone( fileName) ) {
625 return false; 625 return false;
626 } 626 }
627 return true; 627 return true;
628} 628}
629bool AddressBook::saveAB() 629bool AddressBook::saveAB()
630{ 630{
631 bool ok = true; 631 bool ok = true;
632 632
633 deleteRemovedAddressees(); 633 deleteRemovedAddressees();
634 Iterator ait; 634 Iterator ait;
635 for ( ait = begin(); ait != end(); ++ait ) { 635 for ( ait = begin(); ait != end(); ++ait ) {
636 if ( !(*ait).IDStr().isEmpty() ) { 636 if ( !(*ait).IDStr().isEmpty() ) {
637 (*ait).insertCustom( "KADDRESSBOOK", "X-ExternalID", (*ait).IDStr() ); 637 (*ait).insertCustom( "KADDRESSBOOK", "X-ExternalID", (*ait).IDStr() );
638 } 638 }
639 } 639 }
640 KRES::Manager<Resource>::ActiveIterator it; 640 KRES::Manager<Resource>::ActiveIterator it;
641 KRES::Manager<Resource> *manager = d->mManager; 641 KRES::Manager<Resource> *manager = d->mManager;
642 qDebug("SaveAB::saving..." ); 642 qDebug("SaveAB::saving..." );
643 for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) { 643 for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) {
644 qDebug("SaveAB::checking resource..." ); 644 qDebug("SaveAB::checking resource..." );
645 if ( (*it)->readOnly() ) 645 if ( (*it)->readOnly() )
646 qDebug("resource is readonly." ); 646 qDebug("resource is readonly." );
647 if ( (*it)->isOpen() ) 647 if ( (*it)->isOpen() )
648 qDebug("resource is open" ); 648 qDebug("resource is open" );
649 649
650 if ( !(*it)->readOnly() && (*it)->isOpen() ) { 650 if ( !(*it)->readOnly() && (*it)->isOpen() ) {
651 Ticket *ticket = requestSaveTicket( *it ); 651 Ticket *ticket = requestSaveTicket( *it );
652 qDebug("StdAddressBook::save '%s'", (*it)->resourceName().latin1() ); 652 qDebug("StdAddressBook::save '%s'", (*it)->resourceName().latin1() );
653 if ( !ticket ) { 653 if ( !ticket ) {
654 qDebug( i18n( "Unable to save to resource '%1'. It is locked." ) 654 qDebug( i18n( "Unable to save to resource '%1'. It is locked." )
655 .arg( (*it)->resourceName() ) ); 655 .arg( (*it)->resourceName() ) );
656 return false; 656 return false;
657 } 657 }
658 658
659 //if ( !save( ticket ) ) 659 //if ( !save( ticket ) )
660 if ( ticket->resource() ) { 660 if ( ticket->resource() ) {
661 QString name = ticket->resource()->resourceName(); 661 QString name = ticket->resource()->resourceName();
662 if ( ! ticket->resource()->save( ticket ) ) 662 if ( ! ticket->resource()->save( ticket ) )
663 ok = false; 663 ok = false;
664 else 664 else
665 qDebug("StdAddressBook::resource saved '%s'", name.latin1() ); 665 qDebug("StdAddressBook::resource saved '%s'", name.latin1() );
666 666
667 } else 667 } else
668 ok = false; 668 ok = false;
669 669
670 } 670 }
671 } 671 }
672 return ok; 672 return ok;
673} 673}
674 674
675AddressBook::Iterator AddressBook::begin() 675AddressBook::Iterator AddressBook::begin()
676{ 676{
677 Iterator it = Iterator(); 677 Iterator it = Iterator();
678 it.d->mIt = d->mAddressees.begin(); 678 it.d->mIt = d->mAddressees.begin();
679 return it; 679 return it;
680} 680}
681 681
682AddressBook::ConstIterator AddressBook::begin() const 682AddressBook::ConstIterator AddressBook::begin() const
683{ 683{
684 ConstIterator it = ConstIterator(); 684 ConstIterator it = ConstIterator();
685 it.d->mIt = d->mAddressees.begin(); 685 it.d->mIt = d->mAddressees.begin();
686 return it; 686 return it;
687} 687}
688 688
689AddressBook::Iterator AddressBook::end() 689AddressBook::Iterator AddressBook::end()
690{ 690{
691 Iterator it = Iterator(); 691 Iterator it = Iterator();
692 it.d->mIt = d->mAddressees.end(); 692 it.d->mIt = d->mAddressees.end();
693 return it; 693 return it;
694} 694}
695 695
696AddressBook::ConstIterator AddressBook::end() const 696AddressBook::ConstIterator AddressBook::end() const
697{ 697{
698 ConstIterator it = ConstIterator(); 698 ConstIterator it = ConstIterator();
699 it.d->mIt = d->mAddressees.end(); 699 it.d->mIt = d->mAddressees.end();
700 return it; 700 return it;
701} 701}
702 702
703void AddressBook::clear() 703void AddressBook::clear()
704{ 704{
705 d->mAddressees.clear(); 705 d->mAddressees.clear();
706} 706}
707 707
708Ticket *AddressBook::requestSaveTicket( Resource *resource ) 708Ticket *AddressBook::requestSaveTicket( Resource *resource )
709{ 709{
710 kdDebug(5700) << "AddressBook::requestSaveTicket()" << endl; 710 kdDebug(5700) << "AddressBook::requestSaveTicket()" << endl;
711 711
712 if ( !resource ) 712 if ( !resource )
713 { 713 {
714 qDebug("AddressBook::requestSaveTicket no resource" ); 714 qDebug("AddressBook::requestSaveTicket no resource" );
715 resource = standardResource(); 715 resource = standardResource();
716 } 716 }
717 717
718 KRES::Manager<Resource>::ActiveIterator it; 718 KRES::Manager<Resource>::ActiveIterator it;
719 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) { 719 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
720 if ( (*it) == resource ) { 720 if ( (*it) == resource ) {
721 if ( (*it)->readOnly() || !(*it)->isOpen() ) 721 if ( (*it)->readOnly() || !(*it)->isOpen() )
722 return 0; 722 return 0;
723 else 723 else
724 return (*it)->requestSaveTicket(); 724 return (*it)->requestSaveTicket();
725 } 725 }
726 } 726 }
727 727
728 return 0; 728 return 0;
729} 729}
730//void insertAddressee( const Addressee &, bool setRev = true, bool takeResource = false); 730//void insertAddressee( const Addressee &, bool setRev = true, bool takeResource = false);
731void AddressBook::insertAddressee( const Addressee &a, bool setRev, bool takeResource ) 731void AddressBook::insertAddressee( const Addressee &a, bool setRev, bool takeResource )
732{ 732{
733 if ( blockLSEchange && setRev && a.uid().left( 19 ) == QString("last-syncAddressee-") ) { 733 if ( blockLSEchange && setRev && a.uid().left( 19 ) == QString("last-syncAddressee-") ) {
734 //qDebug("block insert "); 734 //qDebug("block insert ");
735 return; 735 return;
736 } 736 }
737 //qDebug("inserting.... %s ",a.uid().latin1() ); 737 //qDebug("inserting.... %s ",a.uid().latin1() );
738 bool found = false; 738 bool found = false;
739 Addressee::List::Iterator it; 739 Addressee::List::Iterator it;
740 for ( it = d->mAddressees.begin(); it != d->mAddressees.end(); ++it ) { 740 for ( it = d->mAddressees.begin(); it != d->mAddressees.end(); ++it ) {
741 if ( a.uid() == (*it).uid() ) { 741 if ( a.uid() == (*it).uid() ) {
742 742
743 bool changed = false; 743 bool changed = false;
744 Addressee addr = a; 744 Addressee addr = a;
745 if ( addr != (*it) ) 745 if ( addr != (*it) )
746 changed = true; 746 changed = true;
747 747
748 if ( takeResource ) { 748 if ( takeResource ) {
749 Resource * res = (*it).resource(); 749 Resource * res = (*it).resource();
750 (*it) = a; 750 (*it) = a;
751 (*it).setResource( res ); 751 (*it).setResource( res );
752 } else { 752 } else {
753 (*it) = a; 753 (*it) = a;
754 if ( (*it).resource() == 0 ) 754 if ( (*it).resource() == 0 )
755 (*it).setResource( standardResource() ); 755 (*it).setResource( standardResource() );
756 } 756 }
757 if ( changed ) { 757 if ( changed ) {
758 if ( setRev ) { 758 if ( setRev ) {
759 759
760 // get rid of micro seconds 760 // get rid of micro seconds
761 QDateTime dt = QDateTime::currentDateTime(); 761 QDateTime dt = QDateTime::currentDateTime();
762 QTime t = dt.time(); 762 QTime t = dt.time();
763 dt.setTime( QTime (t.hour (), t.minute (), t.second () ) ); 763 dt.setTime( QTime (t.hour (), t.minute (), t.second () ) );
764 (*it).setRevision( dt ); 764 (*it).setRevision( dt );
765 } 765 }
766 (*it).setChanged( true ); 766 (*it).setChanged( true );
767 } 767 }
768 768
769 found = true; 769 found = true;
770 } else { 770 } else {
771 if ( (*it).uid().left( 19 ) == QString("last-syncAddressee-") ) { 771 if ( (*it).uid().left( 19 ) == QString("last-syncAddressee-") ) {
772 QString name = (*it).uid().mid( 19 ); 772 QString name = (*it).uid().mid( 19 );
773 Addressee b = a; 773 Addressee b = a;
774 QString id = b.getID( name ); 774 QString id = b.getID( name );
775 if ( ! id.isEmpty() ) { 775 if ( ! id.isEmpty() ) {
776 QString des = (*it).note(); 776 QString des = (*it).note();
777 int startN; 777 int startN;
778 if( (startN = des.find( id ) ) >= 0 ) { 778 if( (startN = des.find( id ) ) >= 0 ) {
779 int endN = des.find( ",", startN+1 ); 779 int endN = des.find( ",", startN+1 );
780 des = des.left( startN ) + des.mid( endN+1 ); 780 des = des.left( startN ) + des.mid( endN+1 );
781 (*it).setNote( des ); 781 (*it).setNote( des );
782 } 782 }
783 } 783 }
784 } 784 }
785 } 785 }
786 } 786 }
787 if ( found ) 787 if ( found )
788 return; 788 return;
789 d->mAddressees.append( a ); 789 d->mAddressees.append( a );
790 Addressee& addr = d->mAddressees.last(); 790 Addressee& addr = d->mAddressees.last();
791 if ( addr.resource() == 0 ) 791 if ( addr.resource() == 0 )
792 addr.setResource( standardResource() ); 792 addr.setResource( standardResource() );
793 793
794 addr.setChanged( true ); 794 addr.setChanged( true );
795} 795}
796 796
797void AddressBook::removeAddressee( const Addressee &a ) 797void AddressBook::removeAddressee( const Addressee &a )
798{ 798{
799 Iterator it; 799 Iterator it;
800 Iterator it2; 800 Iterator it2;
801 bool found = false; 801 bool found = false;
802 for ( it = begin(); it != end(); ++it ) { 802 for ( it = begin(); it != end(); ++it ) {
803 if ( a.uid() == (*it).uid() ) { 803 if ( a.uid() == (*it).uid() ) {
804 found = true; 804 found = true;
805 it2 = it; 805 it2 = it;
806 } else { 806 } else {
807 if ( (*it).uid().left( 19 ) == QString("last-syncAddressee-") ) { 807 if ( (*it).uid().left( 19 ) == QString("last-syncAddressee-") ) {
808 QString name = (*it).uid().mid( 19 ); 808 QString name = (*it).uid().mid( 19 );
809 Addressee b = a; 809 Addressee b = a;
810 QString id = b.getID( name ); 810 QString id = b.getID( name );
811 if ( ! id.isEmpty() ) { 811 if ( ! id.isEmpty() ) {
812 QString des = (*it).note(); 812 QString des = (*it).note();
813 if( des.find( id ) < 0 ) { 813 if( des.find( id ) < 0 ) {
814 des += id + ","; 814 des += id + ",";
815 (*it).setNote( des ); 815 (*it).setNote( des );
816 } 816 }
817 } 817 }
818 } 818 }
819 819
820 } 820 }
821 } 821 }
822 822
823 if ( found ) 823 if ( found )
824 removeAddressee( it2 ); 824 removeAddressee( it2 );
825 825
826} 826}
827 827
828void AddressBook::removeSyncAddressees( bool removeDeleted ) 828void AddressBook::removeSyncAddressees( bool removeDeleted )
829{ 829{
830 Iterator it = begin(); 830 Iterator it = begin();
831 Iterator it2 ; 831 Iterator it2 ;
832 QDateTime dt ( QDate( 2004,1,1) ); 832 QDateTime dt ( QDate( 2004,1,1) );
833 while ( it != end() ) { 833 while ( it != end() ) {
834 (*it).setRevision( dt ); 834 (*it).setRevision( dt );
835 (*it).removeCustom( "KADDRESSBOOK", "X-ExternalID" ); 835 (*it).removeCustom( "KADDRESSBOOK", "X-ExternalID" );
836 (*it).setIDStr(""); 836 (*it).setIDStr("");
837 if ( ( (*it).tempSyncStat() == SYNC_TEMPSTATE_DELETE && removeDeleted )|| (*it).uid().left( 19 ) == QString("last-syncAddressee-")) { 837 if ( ( (*it).tempSyncStat() == SYNC_TEMPSTATE_DELETE && removeDeleted )|| (*it).uid().left( 19 ) == QString("last-syncAddressee-")) {
838 it2 = it; 838 it2 = it;
839 //qDebug("removing %s ",(*it).uid().latin1() ); 839 //qDebug("removing %s ",(*it).uid().latin1() );
840 ++it; 840 ++it;
841 removeAddressee( it2 ); 841 removeAddressee( it2 );
842 } else { 842 } else {
843 //qDebug("skipping %s ",(*it).uid().latin1() ); 843 //qDebug("skipping %s ",(*it).uid().latin1() );
844 ++it; 844 ++it;
845 } 845 }
846 } 846 }
847 deleteRemovedAddressees(); 847 deleteRemovedAddressees();
848} 848}
849 849
850void AddressBook::removeAddressee( const Iterator &it ) 850void AddressBook::removeAddressee( const Iterator &it )
851{ 851{
852 d->mRemovedAddressees.append( (*it) ); 852 d->mRemovedAddressees.append( (*it) );
853 d->mAddressees.remove( it.d->mIt ); 853 d->mAddressees.remove( it.d->mIt );
854} 854}
855 855
856AddressBook::Iterator AddressBook::find( const Addressee &a ) 856AddressBook::Iterator AddressBook::find( const Addressee &a )
857{ 857{
858 Iterator it; 858 Iterator it;
859 for ( it = begin(); it != end(); ++it ) { 859 for ( it = begin(); it != end(); ++it ) {
860 if ( a.uid() == (*it).uid() ) { 860 if ( a.uid() == (*it).uid() ) {
861 return it; 861 return it;
862 } 862 }
863 } 863 }
864 return end(); 864 return end();
865} 865}
866 866
867Addressee AddressBook::findByUid( const QString &uid ) 867Addressee AddressBook::findByUid( const QString &uid )
868{ 868{
869 Iterator it; 869 Iterator it;
870 for ( it = begin(); it != end(); ++it ) { 870 for ( it = begin(); it != end(); ++it ) {
871 if ( uid == (*it).uid() ) { 871 if ( uid == (*it).uid() ) {
872 return *it; 872 return *it;
873 } 873 }
874 } 874 }
875 return Addressee(); 875 return Addressee();
876} 876}
877void AddressBook::preExternSync( AddressBook* aBook, const QString& csd , bool isSubset ) 877void AddressBook::preExternSync( AddressBook* aBook, const QString& csd , bool isSubset )
878{ 878{
879 //qDebug("AddressBook::preExternSync "); 879 //qDebug("AddressBook::preExternSync ");
880 AddressBook::Iterator it; 880 AddressBook::Iterator it;
881 for ( it = begin(); it != end(); ++it ) { 881 for ( it = begin(); it != end(); ++it ) {
882 (*it).setID( csd, (*it).externalUID() ); 882 (*it).setID( csd, (*it).externalUID() );
883 (*it).computeCsum( csd ); 883 (*it).computeCsum( csd );
884 } 884 }
885 mergeAB( aBook ,csd, isSubset ); 885 mergeAB( aBook ,csd, isSubset );
886} 886}
887void AddressBook::postExternSync( AddressBook* aBook , const QString& csd) 887void AddressBook::postExternSync( AddressBook* aBook , const QString& csd)
888{ 888{
889 //qDebug("AddressBook::postExternSync "); 889 //qDebug("AddressBook::postExternSync ");
890 AddressBook::Iterator it; 890 AddressBook::Iterator it;
891 for ( it = begin(); it != end(); ++it ) { 891 for ( it = begin(); it != end(); ++it ) {
892 // qDebug("check uid %s ", (*it).uid().latin1() ); 892 // qDebug("check uid %s ", (*it).uid().latin1() );
893 if ( (*it).tempSyncStat() == SYNC_TEMPSTATE_NEW_ID || 893 if ( (*it).tempSyncStat() == SYNC_TEMPSTATE_NEW_ID ||
894 (*it).tempSyncStat() == SYNC_TEMPSTATE_NEW_CSUM ) { 894 (*it).tempSyncStat() == SYNC_TEMPSTATE_NEW_CSUM ) {
895 Addressee ad = aBook->findByUid( ( (*it).uid() )); 895 Addressee ad = aBook->findByUid( ( (*it).uid() ));
896 if ( ad.isEmpty() ) { 896 if ( ad.isEmpty() ) {
897 qDebug("postExternSync:ERROR addressee is empty: %s ", (*it).uid().latin1()); 897 qDebug("postExternSync:ERROR addressee is empty: %s ", (*it).uid().latin1());
898 } else { 898 } else {
899 (*it).computeCsum( csd ); 899 (*it).computeCsum( csd );
900 if ( (*it).tempSyncStat() == SYNC_TEMPSTATE_NEW_ID ) 900 if ( (*it).tempSyncStat() == SYNC_TEMPSTATE_NEW_ID )
901 ad.setID( csd, (*it).externalUID() ); 901 ad.setID( csd, (*it).externalUID() );
902 ad.setCsum( csd, (*it).getCsum( csd ) ); 902 ad.setCsum( csd, (*it).getCsum( csd ) );
903 aBook->insertAddressee( ad ); 903 aBook->insertAddressee( ad );
904 } 904 }
905 } 905 }
906 } 906 }
907} 907}
908 908
909bool AddressBook::containsExternalUid( const QString& uid ) 909bool AddressBook::containsExternalUid( const QString& uid )
910{ 910{
911 Iterator it; 911 Iterator it;
912 for ( it = begin(); it != end(); ++it ) { 912 for ( it = begin(); it != end(); ++it ) {
913 if ( uid == (*it).externalUID( ) ) 913 if ( uid == (*it).externalUID( ) )
914 return true; 914 return true;
915 } 915 }
916 return false; 916 return false;
917} 917}
918Addressee AddressBook::findByExternUid( const QString& uid , const QString& profile ) 918Addressee AddressBook::findByExternUid( const QString& uid , const QString& profile )
919{ 919{
920 Iterator it; 920 Iterator it;
921 for ( it = begin(); it != end(); ++it ) { 921 for ( it = begin(); it != end(); ++it ) {
922 if ( uid == (*it).getID( profile ) ) 922 if ( uid == (*it).getID( profile ) )
923 return (*it); 923 return (*it);
924 } 924 }
925 return Addressee(); 925 return Addressee();
926} 926}
927void AddressBook::mergeAB( AddressBook *aBook, const QString& profile , bool isSubset ) 927void AddressBook::mergeAB( AddressBook *aBook, const QString& profile , bool isSubset )
928{ 928{
929 Iterator it; 929 Iterator it;
930 Addressee ad; 930 Addressee ad;
931 for ( it = begin(); it != end(); ++it ) { 931 for ( it = begin(); it != end(); ++it ) {
932 ad = aBook->findByExternUid( (*it).externalUID(), profile ); 932 ad = aBook->findByExternUid( (*it).externalUID(), profile );
933 if ( !ad.isEmpty() ) { 933 if ( !ad.isEmpty() ) {
934 (*it).mergeContact( ad ,isSubset); 934 (*it).mergeContact( ad ,isSubset);
935 } 935 }
936 } 936 }
937#if 0 937#if 0
938 // test only 938 // test only
939 for ( it = begin(); it != end(); ++it ) { 939 for ( it = begin(); it != end(); ++it ) {
940 940
941 qDebug("uid %s ", (*it).uid().latin1()); 941 qDebug("uid %s ", (*it).uid().latin1());
942 } 942 }
943#endif 943#endif
944} 944}
945 945
946#if 0 946#if 0
947Addressee::List AddressBook::getExternLastSyncAddressees() 947Addressee::List AddressBook::getExternLastSyncAddressees()
948{ 948{
949 Addressee::List results; 949 Addressee::List results;
950 950
951 Iterator it; 951 Iterator it;
952 for ( it = begin(); it != end(); ++it ) { 952 for ( it = begin(); it != end(); ++it ) {
953 if ( (*it).uid().left( 19 ) == "last-syncAddressee-" ) { 953 if ( (*it).uid().left( 19 ) == "last-syncAddressee-" ) {
954 if ( (*it).familyName().left(4) == "!E: " ) 954 if ( (*it).familyName().left(4) == "!E: " )
955 results.append( *it ); 955 results.append( *it );
956 } 956 }
957 } 957 }
958 958
959 return results; 959 return results;
960} 960}
961#endif 961#endif
962void AddressBook::resetTempSyncStat() 962void AddressBook::resetTempSyncStat()
963{ 963{
964 Iterator it; 964 Iterator it;
965 for ( it = begin(); it != end(); ++it ) { 965 for ( it = begin(); it != end(); ++it ) {
966 (*it).setTempSyncStat ( SYNC_TEMPSTATE_INITIAL ); 966 (*it).setTempSyncStat ( SYNC_TEMPSTATE_INITIAL );
967 } 967 }
968 968
969} 969}
970 970
971QStringList AddressBook:: uidList() 971QStringList AddressBook:: uidList()
972{ 972{
973 QStringList results; 973 QStringList results;
974 Iterator it; 974 Iterator it;
975 for ( it = begin(); it != end(); ++it ) { 975 for ( it = begin(); it != end(); ++it ) {
976 results.append( (*it).uid() ); 976 results.append( (*it).uid() );
977 } 977 }
978 return results; 978 return results;
979} 979}
980 980
981 981
982Addressee::List AddressBook::allAddressees() 982Addressee::List AddressBook::allAddressees()
983{ 983{
984 return d->mAddressees; 984 return d->mAddressees;
985 985
986} 986}
987 987
988Addressee::List AddressBook::findByName( const QString &name ) 988Addressee::List AddressBook::findByName( const QString &name )
989{ 989{
990 Addressee::List results; 990 Addressee::List results;
991 991
992 Iterator it; 992 Iterator it;
993 for ( it = begin(); it != end(); ++it ) { 993 for ( it = begin(); it != end(); ++it ) {
994 if ( name == (*it).realName() ) { 994 if ( name == (*it).realName() ) {
995 results.append( *it ); 995 results.append( *it );
996 } 996 }
997 } 997 }
998 998
999 return results; 999 return results;
1000} 1000}
1001 1001
1002Addressee::List AddressBook::findByEmail( const QString &email ) 1002Addressee::List AddressBook::findByEmail( const QString &email )
1003{ 1003{
1004 Addressee::List results; 1004 Addressee::List results;
1005 QStringList mailList; 1005 QStringList mailList;
1006 1006
1007 Iterator it; 1007 Iterator it;
1008 for ( it = begin(); it != end(); ++it ) { 1008 for ( it = begin(); it != end(); ++it ) {
1009 mailList = (*it).emails(); 1009 mailList = (*it).emails();
1010 for ( QStringList::Iterator ite = mailList.begin(); ite != mailList.end(); ++ite ) { 1010 for ( QStringList::Iterator ite = mailList.begin(); ite != mailList.end(); ++ite ) {
1011 if ( email == (*ite) ) { 1011 if ( email == (*ite) ) {
1012 results.append( *it ); 1012 results.append( *it );
1013 } 1013 }
1014 } 1014 }
1015 } 1015 }
1016 1016
1017 return results; 1017 return results;
1018} 1018}
1019 1019
1020Addressee::List AddressBook::findByCategory( const QString &category ) 1020Addressee::List AddressBook::findByCategory( const QString &category )
1021{ 1021{
1022 Addressee::List results; 1022 Addressee::List results;
1023 1023
1024 Iterator it; 1024 Iterator it;
1025 for ( it = begin(); it != end(); ++it ) { 1025 for ( it = begin(); it != end(); ++it ) {
1026 if ( (*it).hasCategory( category) ) { 1026 if ( (*it).hasCategory( category) ) {
1027 results.append( *it ); 1027 results.append( *it );
1028 } 1028 }
1029 } 1029 }
1030 1030
1031 return results; 1031 return results;
1032} 1032}
1033 1033
1034void AddressBook::dump() const 1034void AddressBook::dump() const
1035{ 1035{
1036 kdDebug(5700) << "AddressBook::dump() --- begin ---" << endl; 1036 kdDebug(5700) << "AddressBook::dump() --- begin ---" << endl;
1037 1037
1038 ConstIterator it; 1038 ConstIterator it;
1039 for( it = begin(); it != end(); ++it ) { 1039 for( it = begin(); it != end(); ++it ) {
1040 (*it).dump(); 1040 (*it).dump();
1041 } 1041 }
1042 1042
1043 kdDebug(5700) << "AddressBook::dump() --- end ---" << endl; 1043 kdDebug(5700) << "AddressBook::dump() --- end ---" << endl;
1044} 1044}
1045 1045
1046QString AddressBook::identifier() 1046QString AddressBook::identifier()
1047{ 1047{
1048 QStringList identifier; 1048 QStringList identifier;