summaryrefslogtreecommitdiffabout
path: root/kabc/addressbook.cpp
Unidiff
Diffstat (limited to 'kabc/addressbook.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--kabc/addressbook.cpp1
1 files changed, 0 insertions, 1 deletions
diff --git a/kabc/addressbook.cpp b/kabc/addressbook.cpp
index bf6d053..1a06956 100644
--- a/kabc/addressbook.cpp
+++ b/kabc/addressbook.cpp
@@ -30,769 +30,768 @@ $Id$
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 43
44#include <kglobal.h> 44#include <kglobal.h>
45#include <klocale.h> 45#include <klocale.h>
46#include <kmessagebox.h> 46#include <kmessagebox.h>
47#include <kdebug.h> 47#include <kdebug.h>
48#include <libkcal/syncdefines.h> 48#include <libkcal/syncdefines.h>
49#include "addressbook.h" 49#include "addressbook.h"
50#include "resource.h" 50#include "resource.h"
51#include "vcardconverter.h" 51#include "vcardconverter.h"
52#include "vcardparser/vcardtool.h" 52#include "vcardparser/vcardtool.h"
53 53
54//US #include "addressbook.moc" 54//US #include "addressbook.moc"
55 55
56using namespace KABC; 56using namespace KABC;
57 57
58struct AddressBook::AddressBookData 58struct AddressBook::AddressBookData
59{ 59{
60 Addressee::List mAddressees; 60 Addressee::List mAddressees;
61 Addressee::List mRemovedAddressees; 61 Addressee::List mRemovedAddressees;
62 Field::List mAllFields; 62 Field::List mAllFields;
63 KConfig *mConfig; 63 KConfig *mConfig;
64 KRES::Manager<Resource> *mManager; 64 KRES::Manager<Resource> *mManager;
65//US ErrorHandler *mErrorHandler; 65//US ErrorHandler *mErrorHandler;
66}; 66};
67 67
68struct AddressBook::Iterator::IteratorData 68struct AddressBook::Iterator::IteratorData
69{ 69{
70 Addressee::List::Iterator mIt; 70 Addressee::List::Iterator mIt;
71}; 71};
72 72
73struct AddressBook::ConstIterator::ConstIteratorData 73struct AddressBook::ConstIterator::ConstIteratorData
74{ 74{
75 Addressee::List::ConstIterator mIt; 75 Addressee::List::ConstIterator mIt;
76}; 76};
77 77
78AddressBook::Iterator::Iterator() 78AddressBook::Iterator::Iterator()
79{ 79{
80 d = new IteratorData; 80 d = new IteratorData;
81} 81}
82 82
83AddressBook::Iterator::Iterator( const AddressBook::Iterator &i ) 83AddressBook::Iterator::Iterator( const AddressBook::Iterator &i )
84{ 84{
85 d = new IteratorData; 85 d = new IteratorData;
86 d->mIt = i.d->mIt; 86 d->mIt = i.d->mIt;
87} 87}
88 88
89AddressBook::Iterator &AddressBook::Iterator::operator=( const AddressBook::Iterator &i ) 89AddressBook::Iterator &AddressBook::Iterator::operator=( const AddressBook::Iterator &i )
90{ 90{
91 if( this == &i ) return *this; // guard against self assignment 91 if( this == &i ) return *this; // guard against self assignment
92 delete d; // delete the old data the Iterator was completely constructed before 92 delete d; // delete the old data the Iterator was completely constructed before
93 d = new IteratorData; 93 d = new IteratorData;
94 d->mIt = i.d->mIt; 94 d->mIt = i.d->mIt;
95 return *this; 95 return *this;
96} 96}
97 97
98AddressBook::Iterator::~Iterator() 98AddressBook::Iterator::~Iterator()
99{ 99{
100 delete d; 100 delete d;
101} 101}
102 102
103const Addressee &AddressBook::Iterator::operator*() const 103const Addressee &AddressBook::Iterator::operator*() const
104{ 104{
105 return *(d->mIt); 105 return *(d->mIt);
106} 106}
107 107
108Addressee &AddressBook::Iterator::operator*() 108Addressee &AddressBook::Iterator::operator*()
109{ 109{
110 return *(d->mIt); 110 return *(d->mIt);
111} 111}
112 112
113Addressee *AddressBook::Iterator::operator->() 113Addressee *AddressBook::Iterator::operator->()
114{ 114{
115 return &(*(d->mIt)); 115 return &(*(d->mIt));
116} 116}
117 117
118AddressBook::Iterator &AddressBook::Iterator::operator++() 118AddressBook::Iterator &AddressBook::Iterator::operator++()
119{ 119{
120 (d->mIt)++; 120 (d->mIt)++;
121 return *this; 121 return *this;
122} 122}
123 123
124AddressBook::Iterator &AddressBook::Iterator::operator++(int) 124AddressBook::Iterator &AddressBook::Iterator::operator++(int)
125{ 125{
126 (d->mIt)++; 126 (d->mIt)++;
127 return *this; 127 return *this;
128} 128}
129 129
130AddressBook::Iterator &AddressBook::Iterator::operator--() 130AddressBook::Iterator &AddressBook::Iterator::operator--()
131{ 131{
132 (d->mIt)--; 132 (d->mIt)--;
133 return *this; 133 return *this;
134} 134}
135 135
136AddressBook::Iterator &AddressBook::Iterator::operator--(int) 136AddressBook::Iterator &AddressBook::Iterator::operator--(int)
137{ 137{
138 (d->mIt)--; 138 (d->mIt)--;
139 return *this; 139 return *this;
140} 140}
141 141
142bool AddressBook::Iterator::operator==( const Iterator &it ) 142bool AddressBook::Iterator::operator==( const Iterator &it )
143{ 143{
144 return ( d->mIt == it.d->mIt ); 144 return ( d->mIt == it.d->mIt );
145} 145}
146 146
147bool AddressBook::Iterator::operator!=( const Iterator &it ) 147bool AddressBook::Iterator::operator!=( const Iterator &it )
148{ 148{
149 return ( d->mIt != it.d->mIt ); 149 return ( d->mIt != it.d->mIt );
150} 150}
151 151
152 152
153AddressBook::ConstIterator::ConstIterator() 153AddressBook::ConstIterator::ConstIterator()
154{ 154{
155 d = new ConstIteratorData; 155 d = new ConstIteratorData;
156} 156}
157 157
158AddressBook::ConstIterator::ConstIterator( const AddressBook::ConstIterator &i ) 158AddressBook::ConstIterator::ConstIterator( const AddressBook::ConstIterator &i )
159{ 159{
160 d = new ConstIteratorData; 160 d = new ConstIteratorData;
161 d->mIt = i.d->mIt; 161 d->mIt = i.d->mIt;
162} 162}
163 163
164AddressBook::ConstIterator &AddressBook::ConstIterator::operator=( const AddressBook::ConstIterator &i ) 164AddressBook::ConstIterator &AddressBook::ConstIterator::operator=( const AddressBook::ConstIterator &i )
165{ 165{
166 if( this == &i ) return *this; // guard for self assignment 166 if( this == &i ) return *this; // guard for self assignment
167 delete d; // delete the old data because the Iterator was really constructed before 167 delete d; // delete the old data because the Iterator was really constructed before
168 d = new ConstIteratorData; 168 d = new ConstIteratorData;
169 d->mIt = i.d->mIt; 169 d->mIt = i.d->mIt;
170 return *this; 170 return *this;
171} 171}
172 172
173AddressBook::ConstIterator::~ConstIterator() 173AddressBook::ConstIterator::~ConstIterator()
174{ 174{
175 delete d; 175 delete d;
176} 176}
177 177
178const Addressee &AddressBook::ConstIterator::operator*() const 178const Addressee &AddressBook::ConstIterator::operator*() const
179{ 179{
180 return *(d->mIt); 180 return *(d->mIt);
181} 181}
182 182
183const Addressee* AddressBook::ConstIterator::operator->() const 183const Addressee* AddressBook::ConstIterator::operator->() const
184{ 184{
185 return &(*(d->mIt)); 185 return &(*(d->mIt));
186} 186}
187 187
188AddressBook::ConstIterator &AddressBook::ConstIterator::operator++() 188AddressBook::ConstIterator &AddressBook::ConstIterator::operator++()
189{ 189{
190 (d->mIt)++; 190 (d->mIt)++;
191 return *this; 191 return *this;
192} 192}
193 193
194AddressBook::ConstIterator &AddressBook::ConstIterator::operator++(int) 194AddressBook::ConstIterator &AddressBook::ConstIterator::operator++(int)
195{ 195{
196 (d->mIt)++; 196 (d->mIt)++;
197 return *this; 197 return *this;
198} 198}
199 199
200AddressBook::ConstIterator &AddressBook::ConstIterator::operator--() 200AddressBook::ConstIterator &AddressBook::ConstIterator::operator--()
201{ 201{
202 (d->mIt)--; 202 (d->mIt)--;
203 return *this; 203 return *this;
204} 204}
205 205
206AddressBook::ConstIterator &AddressBook::ConstIterator::operator--(int) 206AddressBook::ConstIterator &AddressBook::ConstIterator::operator--(int)
207{ 207{
208 (d->mIt)--; 208 (d->mIt)--;
209 return *this; 209 return *this;
210} 210}
211 211
212bool AddressBook::ConstIterator::operator==( const ConstIterator &it ) 212bool AddressBook::ConstIterator::operator==( const ConstIterator &it )
213{ 213{
214 return ( d->mIt == it.d->mIt ); 214 return ( d->mIt == it.d->mIt );
215} 215}
216 216
217bool AddressBook::ConstIterator::operator!=( const ConstIterator &it ) 217bool AddressBook::ConstIterator::operator!=( const ConstIterator &it )
218{ 218{
219 return ( d->mIt != it.d->mIt ); 219 return ( d->mIt != it.d->mIt );
220} 220}
221 221
222 222
223AddressBook::AddressBook() 223AddressBook::AddressBook()
224{ 224{
225 init(0, "contact"); 225 init(0, "contact");
226} 226}
227 227
228AddressBook::AddressBook( const QString &config ) 228AddressBook::AddressBook( const QString &config )
229{ 229{
230 init(config, "contact"); 230 init(config, "contact");
231} 231}
232 232
233AddressBook::AddressBook( const QString &config, const QString &family ) 233AddressBook::AddressBook( const QString &config, const QString &family )
234{ 234{
235 init(config, family); 235 init(config, family);
236 236
237} 237}
238 238
239// the default family is "contact" 239// the default family is "contact"
240void AddressBook::init(const QString &config, const QString &family ) 240void AddressBook::init(const QString &config, const QString &family )
241{ 241{
242 blockLSEchange = false; 242 blockLSEchange = false;
243 d = new AddressBookData; 243 d = new AddressBookData;
244 QString fami = family; 244 QString fami = family;
245 if (config != 0) { 245 if (config != 0) {
246 if ( family == "syncContact" ) { 246 if ( family == "syncContact" ) {
247 qDebug("creating sync config "); 247 qDebug("creating sync config ");
248 fami = "contact"; 248 fami = "contact";
249 KConfig* con = new KConfig( locateLocal("config", "syncContactrc") ); 249 KConfig* con = new KConfig( locateLocal("config", "syncContactrc") );
250 con->setGroup( "General" ); 250 con->setGroup( "General" );
251 con->writeEntry( "ResourceKeys", QString("sync") ); 251 con->writeEntry( "ResourceKeys", QString("sync") );
252 con->writeEntry( "Standard", QString("sync") ); 252 con->writeEntry( "Standard", QString("sync") );
253 con->setGroup( "Resource_sync" ); 253 con->setGroup( "Resource_sync" );
254 con->writeEntry( "FileName", config ); 254 con->writeEntry( "FileName", config );
255 con->writeEntry( "FileFormat", QString("vcard") ); 255 con->writeEntry( "FileFormat", QString("vcard") );
256 con->writeEntry( "ResourceIdentifier", QString("sync") ); 256 con->writeEntry( "ResourceIdentifier", QString("sync") );
257 con->writeEntry( "ResourceName", QString("sync_res") ); 257 con->writeEntry( "ResourceName", QString("sync_res") );
258 if ( config.right(4) == ".xml" ) 258 if ( config.right(4) == ".xml" )
259 con->writeEntry( "ResourceType", QString("qtopia") ); 259 con->writeEntry( "ResourceType", QString("qtopia") );
260 else if ( config == "sharp" ) { 260 else if ( config == "sharp" ) {
261 con->writeEntry( "ResourceType", QString("sharp") ); 261 con->writeEntry( "ResourceType", QString("sharp") );
262 } else { 262 } else {
263 con->writeEntry( "ResourceType", QString("file") ); 263 con->writeEntry( "ResourceType", QString("file") );
264 } 264 }
265 //con->sync(); 265 //con->sync();
266 d->mConfig = con; 266 d->mConfig = con;
267 } 267 }
268 else 268 else
269 d->mConfig = new KConfig( locateLocal("config", config) ); 269 d->mConfig = new KConfig( locateLocal("config", config) );
270// qDebug("AddressBook::init 1 config=%s",config.latin1() ); 270// qDebug("AddressBook::init 1 config=%s",config.latin1() );
271 } 271 }
272 else { 272 else {
273 d->mConfig = 0; 273 d->mConfig = 0;
274// qDebug("AddressBook::init 1 config=0"); 274// qDebug("AddressBook::init 1 config=0");
275 } 275 }
276 276
277//US d->mErrorHandler = 0; 277//US d->mErrorHandler = 0;
278 d->mManager = new KRES::Manager<Resource>( fami, false ); 278 d->mManager = new KRES::Manager<Resource>( fami, false );
279 d->mManager->readConfig( d->mConfig ); 279 d->mManager->readConfig( d->mConfig );
280 if ( family == "syncContact" ) { 280 if ( family == "syncContact" ) {
281 KRES::Manager<Resource> *manager = d->mManager; 281 KRES::Manager<Resource> *manager = d->mManager;
282 KRES::Manager<Resource>::ActiveIterator it; 282 KRES::Manager<Resource>::ActiveIterator it;
283 for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) { 283 for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) {
284 (*it)->setAddressBook( this ); 284 (*it)->setAddressBook( this );
285 if ( !(*it)->open() ) 285 if ( !(*it)->open() )
286 error( QString( "Unable to open resource '%1'!" ).arg( (*it)->resourceName() ) ); 286 error( QString( "Unable to open resource '%1'!" ).arg( (*it)->resourceName() ) );
287 } 287 }
288 Resource *res = standardResource(); 288 Resource *res = standardResource();
289 if ( !res ) { 289 if ( !res ) {
290 qDebug("ERROR: no standard resource"); 290 qDebug("ERROR: no standard resource");
291 res = manager->createResource( "file" ); 291 res = manager->createResource( "file" );
292 if ( res ) 292 if ( res )
293 { 293 {
294 addResource( res ); 294 addResource( res );
295 } 295 }
296 else 296 else
297 qDebug(" No resource available!!!"); 297 qDebug(" No resource available!!!");
298 } 298 }
299 setStandardResource( res ); 299 setStandardResource( res );
300 manager->writeConfig(); 300 manager->writeConfig();
301 } 301 }
302 addCustomField( i18n( "Department" ), KABC::Field::Organization, 302 addCustomField( i18n( "Department" ), KABC::Field::Organization,
303 "X-Department", "KADDRESSBOOK" ); 303 "X-Department", "KADDRESSBOOK" );
304 addCustomField( i18n( "Profession" ), KABC::Field::Organization, 304 addCustomField( i18n( "Profession" ), KABC::Field::Organization,
305 "X-Profession", "KADDRESSBOOK" ); 305 "X-Profession", "KADDRESSBOOK" );
306 addCustomField( i18n( "Assistant's Name" ), KABC::Field::Organization, 306 addCustomField( i18n( "Assistant's Name" ), KABC::Field::Organization,
307 "X-AssistantsName", "KADDRESSBOOK" ); 307 "X-AssistantsName", "KADDRESSBOOK" );
308 addCustomField( i18n( "Manager's Name" ), KABC::Field::Organization, 308 addCustomField( i18n( "Manager's Name" ), KABC::Field::Organization,
309 "X-ManagersName", "KADDRESSBOOK" ); 309 "X-ManagersName", "KADDRESSBOOK" );
310 addCustomField( i18n( "Spouse's Name" ), KABC::Field::Personal, 310 addCustomField( i18n( "Spouse's Name" ), KABC::Field::Personal,
311 "X-SpousesName", "KADDRESSBOOK" ); 311 "X-SpousesName", "KADDRESSBOOK" );
312 addCustomField( i18n( "Office" ), KABC::Field::Personal, 312 addCustomField( i18n( "Office" ), KABC::Field::Personal,
313 "X-Office", "KADDRESSBOOK" ); 313 "X-Office", "KADDRESSBOOK" );
314 addCustomField( i18n( "IM Address" ), KABC::Field::Personal, 314 addCustomField( i18n( "IM Address" ), KABC::Field::Personal,
315 "X-IMAddress", "KADDRESSBOOK" ); 315 "X-IMAddress", "KADDRESSBOOK" );
316 addCustomField( i18n( "Anniversary" ), KABC::Field::Personal, 316 addCustomField( i18n( "Anniversary" ), KABC::Field::Personal,
317 "X-Anniversary", "KADDRESSBOOK" ); 317 "X-Anniversary", "KADDRESSBOOK" );
318 318
319 //US added this field to become compatible with Opie/qtopia addressbook 319 //US added this field to become compatible with Opie/qtopia addressbook
320 // values can be "female" or "male" or "". An empty field represents undefined. 320 // values can be "female" or "male" or "". An empty field represents undefined.
321 addCustomField( i18n( "Gender" ), KABC::Field::Personal, 321 addCustomField( i18n( "Gender" ), KABC::Field::Personal,
322 "X-Gender", "KADDRESSBOOK" ); 322 "X-Gender", "KADDRESSBOOK" );
323 addCustomField( i18n( "Children" ), KABC::Field::Personal, 323 addCustomField( i18n( "Children" ), KABC::Field::Personal,
324 "X-Children", "KADDRESSBOOK" ); 324 "X-Children", "KADDRESSBOOK" );
325 addCustomField( i18n( "FreeBusyUrl" ), KABC::Field::Personal, 325 addCustomField( i18n( "FreeBusyUrl" ), KABC::Field::Personal,
326 "X-FreeBusyUrl", "KADDRESSBOOK" ); 326 "X-FreeBusyUrl", "KADDRESSBOOK" );
327 addCustomField( i18n( "ExternalID" ), KABC::Field::Personal, 327 addCustomField( i18n( "ExternalID" ), KABC::Field::Personal,
328 "X-ExternalID", "KADDRESSBOOK" ); 328 "X-ExternalID", "KADDRESSBOOK" );
329} 329}
330 330
331AddressBook::~AddressBook() 331AddressBook::~AddressBook()
332{ 332{
333 delete d->mConfig; d->mConfig = 0; 333 delete d->mConfig; d->mConfig = 0;
334 delete d->mManager; d->mManager = 0; 334 delete d->mManager; d->mManager = 0;
335//US delete d->mErrorHandler; d->mErrorHandler = 0; 335//US delete d->mErrorHandler; d->mErrorHandler = 0;
336 delete d; d = 0; 336 delete d; d = 0;
337} 337}
338 338
339bool AddressBook::load() 339bool AddressBook::load()
340{ 340{
341 341
342 342
343 clear(); 343 clear();
344 344
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 error( i18n("Unable to load resource '%1'").arg( (*it)->resourceName() ) ); 349 error( i18n("Unable to load resource '%1'").arg( (*it)->resourceName() ) );
350 ok = false; 350 ok = false;
351 } 351 }
352 352
353 // mark all addressees as unchanged 353 // mark all addressees as unchanged
354 Addressee::List::Iterator addrIt; 354 Addressee::List::Iterator addrIt;
355 for ( addrIt = d->mAddressees.begin(); addrIt != d->mAddressees.end(); ++addrIt ) { 355 for ( addrIt = d->mAddressees.begin(); addrIt != d->mAddressees.end(); ++addrIt ) {
356 (*addrIt).setChanged( false ); 356 (*addrIt).setChanged( false );
357 QString id = (*addrIt).custom( "KADDRESSBOOK", "X-ExternalID" ); 357 QString id = (*addrIt).custom( "KADDRESSBOOK", "X-ExternalID" );
358 if ( !id.isEmpty() ) { 358 if ( !id.isEmpty() ) {
359 //qDebug("setId aa %s ", id.latin1()); 359 //qDebug("setId aa %s ", id.latin1());
360 (*addrIt).setIDStr(id ); 360 (*addrIt).setIDStr(id );
361 } 361 }
362 } 362 }
363 blockLSEchange = true; 363 blockLSEchange = true;
364 return ok; 364 return ok;
365} 365}
366 366
367bool AddressBook::save( Ticket *ticket ) 367bool AddressBook::save( Ticket *ticket )
368{ 368{
369 kdDebug(5700) << "AddressBook::save()"<< endl; 369 kdDebug(5700) << "AddressBook::save()"<< endl;
370 370
371 if ( ticket->resource() ) { 371 if ( ticket->resource() ) {
372 deleteRemovedAddressees(); 372 deleteRemovedAddressees();
373 return ticket->resource()->save( ticket ); 373 return ticket->resource()->save( ticket );
374 } 374 }
375 375
376 return false; 376 return false;
377} 377}
378void AddressBook::export2File( QString fileName ) 378void AddressBook::export2File( QString fileName )
379{ 379{
380 380
381 QFile outFile( fileName ); 381 QFile outFile( fileName );
382 if ( !outFile.open( IO_WriteOnly ) ) { 382 if ( !outFile.open( IO_WriteOnly ) ) {
383 QString text = i18n( "<qt>Unable to open file <b>%1</b> for export.</qt>" ); 383 QString text = i18n( "<qt>Unable to open file <b>%1</b> for export.</qt>" );
384 KMessageBox::error( 0, text.arg( fileName ) ); 384 KMessageBox::error( 0, text.arg( fileName ) );
385 return ; 385 return ;
386 } 386 }
387 QTextStream t( &outFile ); 387 QTextStream t( &outFile );
388 t.setEncoding( QTextStream::UnicodeUTF8 ); 388 t.setEncoding( QTextStream::UnicodeUTF8 );
389 Iterator it; 389 Iterator it;
390 KABC::VCardConverter::Version version; 390 KABC::VCardConverter::Version version;
391 version = KABC::VCardConverter::v3_0; 391 version = KABC::VCardConverter::v3_0;
392 for ( it = begin(); it != end(); ++it ) { 392 for ( it = begin(); it != end(); ++it ) {
393 if ( !(*it).IDStr().isEmpty() ) { 393 if ( !(*it).IDStr().isEmpty() ) {
394 (*it).insertCustom( "KADDRESSBOOK", "X-ExternalID", (*it).IDStr() ); 394 (*it).insertCustom( "KADDRESSBOOK", "X-ExternalID", (*it).IDStr() );
395 } 395 }
396 KABC::VCardConverter converter; 396 KABC::VCardConverter converter;
397 QString vcard; 397 QString vcard;
398 //Resource *resource() const; 398 //Resource *resource() const;
399 converter.addresseeToVCard( *it, vcard, version ); 399 converter.addresseeToVCard( *it, vcard, version );
400 t << vcard << "\r\n"; 400 t << vcard << "\r\n";
401 } 401 }
402 t << "\r\n\r\n"; 402 t << "\r\n\r\n";
403 outFile.close(); 403 outFile.close();
404} 404}
405void AddressBook::importFromFile( QString fileName, bool replaceLabel ) 405void AddressBook::importFromFile( QString fileName, bool replaceLabel )
406{ 406{
407 407
408 KABC::Addressee::List list; 408 KABC::Addressee::List list;
409 QFile file( fileName ); 409 QFile file( fileName );
410 410
411 file.open( IO_ReadOnly ); 411 file.open( IO_ReadOnly );
412 QByteArray rawData = file.readAll(); 412 QByteArray rawData = file.readAll();
413 file.close(); 413 file.close();
414 qDebug("AddressBook::importFromFile ");
415 QString data; 414 QString data;
416 if ( replaceLabel ) { 415 if ( replaceLabel ) {
417 data = QString::fromLatin1( rawData.data(), rawData.size() + 1 ); 416 data = QString::fromLatin1( rawData.data(), rawData.size() + 1 );
418 data.replace ( QRegExp("LABEL") , "ADR" ); 417 data.replace ( QRegExp("LABEL") , "ADR" );
419 data.replace ( QRegExp("CHARSET=ISO-8859-1") , "" ); 418 data.replace ( QRegExp("CHARSET=ISO-8859-1") , "" );
420 } else 419 } else
421 data = QString::fromUtf8( rawData.data(), rawData.size() + 1 ); 420 data = QString::fromUtf8( rawData.data(), rawData.size() + 1 );
422 KABC::VCardTool tool; 421 KABC::VCardTool tool;
423 list = tool.parseVCards( data ); 422 list = tool.parseVCards( data );
424 KABC::Addressee::List::Iterator it; 423 KABC::Addressee::List::Iterator it;
425 for ( it = list.begin(); it != list.end(); ++it ) { 424 for ( it = list.begin(); it != list.end(); ++it ) {
426 (*it).setResource( 0 ); 425 (*it).setResource( 0 );
427 if ( replaceLabel ) 426 if ( replaceLabel )
428 (*it).removeVoice(); 427 (*it).removeVoice();
429 insertAddressee( (*it), false, true ); 428 insertAddressee( (*it), false, true );
430 } 429 }
431 430
432} 431}
433 432
434bool AddressBook::saveAB() 433bool AddressBook::saveAB()
435{ 434{
436 bool ok = true; 435 bool ok = true;
437 436
438 deleteRemovedAddressees(); 437 deleteRemovedAddressees();
439 Iterator ait; 438 Iterator ait;
440 for ( ait = begin(); ait != end(); ++ait ) { 439 for ( ait = begin(); ait != end(); ++ait ) {
441 if ( !(*ait).IDStr().isEmpty() ) { 440 if ( !(*ait).IDStr().isEmpty() ) {
442 (*ait).insertCustom( "KADDRESSBOOK", "X-ExternalID", (*ait).IDStr() ); 441 (*ait).insertCustom( "KADDRESSBOOK", "X-ExternalID", (*ait).IDStr() );
443 } 442 }
444 } 443 }
445 KRES::Manager<Resource>::ActiveIterator it; 444 KRES::Manager<Resource>::ActiveIterator it;
446 KRES::Manager<Resource> *manager = d->mManager; 445 KRES::Manager<Resource> *manager = d->mManager;
447 for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) { 446 for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) {
448 if ( !(*it)->readOnly() && (*it)->isOpen() ) { 447 if ( !(*it)->readOnly() && (*it)->isOpen() ) {
449 Ticket *ticket = requestSaveTicket( *it ); 448 Ticket *ticket = requestSaveTicket( *it );
450// qDebug("StdAddressBook::save '%s'", (*it)->resourceName().latin1() ); 449// qDebug("StdAddressBook::save '%s'", (*it)->resourceName().latin1() );
451 if ( !ticket ) { 450 if ( !ticket ) {
452 error( i18n( "Unable to save to resource '%1'. It is locked." ) 451 error( i18n( "Unable to save to resource '%1'. It is locked." )
453 .arg( (*it)->resourceName() ) ); 452 .arg( (*it)->resourceName() ) );
454 return false; 453 return false;
455 } 454 }
456 455
457 //if ( !save( ticket ) ) 456 //if ( !save( ticket ) )
458 if ( ticket->resource() ) { 457 if ( ticket->resource() ) {
459 if ( ! ticket->resource()->save( ticket ) ) 458 if ( ! ticket->resource()->save( ticket ) )
460 ok = false; 459 ok = false;
461 } else 460 } else
462 ok = false; 461 ok = false;
463 462
464 } 463 }
465 } 464 }
466 return ok; 465 return ok;
467} 466}
468 467
469AddressBook::Iterator AddressBook::begin() 468AddressBook::Iterator AddressBook::begin()
470{ 469{
471 Iterator it = Iterator(); 470 Iterator it = Iterator();
472 it.d->mIt = d->mAddressees.begin(); 471 it.d->mIt = d->mAddressees.begin();
473 return it; 472 return it;
474} 473}
475 474
476AddressBook::ConstIterator AddressBook::begin() const 475AddressBook::ConstIterator AddressBook::begin() const
477{ 476{
478 ConstIterator it = ConstIterator(); 477 ConstIterator it = ConstIterator();
479 it.d->mIt = d->mAddressees.begin(); 478 it.d->mIt = d->mAddressees.begin();
480 return it; 479 return it;
481} 480}
482 481
483AddressBook::Iterator AddressBook::end() 482AddressBook::Iterator AddressBook::end()
484{ 483{
485 Iterator it = Iterator(); 484 Iterator it = Iterator();
486 it.d->mIt = d->mAddressees.end(); 485 it.d->mIt = d->mAddressees.end();
487 return it; 486 return it;
488} 487}
489 488
490AddressBook::ConstIterator AddressBook::end() const 489AddressBook::ConstIterator AddressBook::end() const
491{ 490{
492 ConstIterator it = ConstIterator(); 491 ConstIterator it = ConstIterator();
493 it.d->mIt = d->mAddressees.end(); 492 it.d->mIt = d->mAddressees.end();
494 return it; 493 return it;
495} 494}
496 495
497void AddressBook::clear() 496void AddressBook::clear()
498{ 497{
499 d->mAddressees.clear(); 498 d->mAddressees.clear();
500} 499}
501 500
502Ticket *AddressBook::requestSaveTicket( Resource *resource ) 501Ticket *AddressBook::requestSaveTicket( Resource *resource )
503{ 502{
504 kdDebug(5700) << "AddressBook::requestSaveTicket()" << endl; 503 kdDebug(5700) << "AddressBook::requestSaveTicket()" << endl;
505 504
506 if ( !resource ) 505 if ( !resource )
507 { 506 {
508 qDebug("AddressBook::requestSaveTicket no resource" ); 507 qDebug("AddressBook::requestSaveTicket no resource" );
509 resource = standardResource(); 508 resource = standardResource();
510 } 509 }
511 510
512 KRES::Manager<Resource>::ActiveIterator it; 511 KRES::Manager<Resource>::ActiveIterator it;
513 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) { 512 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
514 if ( (*it) == resource ) { 513 if ( (*it) == resource ) {
515 if ( (*it)->readOnly() || !(*it)->isOpen() ) 514 if ( (*it)->readOnly() || !(*it)->isOpen() )
516 return 0; 515 return 0;
517 else 516 else
518 return (*it)->requestSaveTicket(); 517 return (*it)->requestSaveTicket();
519 } 518 }
520 } 519 }
521 520
522 return 0; 521 return 0;
523} 522}
524 523
525void AddressBook::insertAddressee( const Addressee &a, bool setRev, bool takeResource ) 524void AddressBook::insertAddressee( const Addressee &a, bool setRev, bool takeResource )
526{ 525{
527 if ( blockLSEchange && setRev && a.uid().left( 19 ) == QString("last-syncAddressee-") ) { 526 if ( blockLSEchange && setRev && a.uid().left( 19 ) == QString("last-syncAddressee-") ) {
528 //qDebug("block insert "); 527 //qDebug("block insert ");
529 return; 528 return;
530 } 529 }
531 //qDebug("inserting.... %s ",a.uid().latin1() ); 530 //qDebug("inserting.... %s ",a.uid().latin1() );
532 bool found = false; 531 bool found = false;
533 Addressee::List::Iterator it; 532 Addressee::List::Iterator it;
534 for ( it = d->mAddressees.begin(); it != d->mAddressees.end(); ++it ) { 533 for ( it = d->mAddressees.begin(); it != d->mAddressees.end(); ++it ) {
535 if ( a.uid() == (*it).uid() ) { 534 if ( a.uid() == (*it).uid() ) {
536 535
537 bool changed = false; 536 bool changed = false;
538 Addressee addr = a; 537 Addressee addr = a;
539 if ( addr != (*it) ) 538 if ( addr != (*it) )
540 changed = true; 539 changed = true;
541 540
542 if ( takeResource ) { 541 if ( takeResource ) {
543 Resource * res = (*it).resource(); 542 Resource * res = (*it).resource();
544 (*it) = a; 543 (*it) = a;
545 (*it).setResource( res ); 544 (*it).setResource( res );
546 } else { 545 } else {
547 (*it) = a; 546 (*it) = a;
548 if ( (*it).resource() == 0 ) 547 if ( (*it).resource() == 0 )
549 (*it).setResource( standardResource() ); 548 (*it).setResource( standardResource() );
550 } 549 }
551 if ( changed ) { 550 if ( changed ) {
552 if ( setRev ) { 551 if ( setRev ) {
553 552
554 // get rid of micro seconds 553 // get rid of micro seconds
555 QDateTime dt = QDateTime::currentDateTime(); 554 QDateTime dt = QDateTime::currentDateTime();
556 QTime t = dt.time(); 555 QTime t = dt.time();
557 dt.setTime( QTime (t.hour (), t.minute (), t.second () ) ); 556 dt.setTime( QTime (t.hour (), t.minute (), t.second () ) );
558 (*it).setRevision( dt ); 557 (*it).setRevision( dt );
559 } 558 }
560 (*it).setChanged( true ); 559 (*it).setChanged( true );
561 } 560 }
562 561
563 found = true; 562 found = true;
564 } else { 563 } else {
565 if ( (*it).uid().left( 19 ) == QString("last-syncAddressee-") ) { 564 if ( (*it).uid().left( 19 ) == QString("last-syncAddressee-") ) {
566 QString name = (*it).uid().mid( 19 ); 565 QString name = (*it).uid().mid( 19 );
567 Addressee b = a; 566 Addressee b = a;
568 QString id = b.getID( name ); 567 QString id = b.getID( name );
569 if ( ! id.isEmpty() ) { 568 if ( ! id.isEmpty() ) {
570 QString des = (*it).note(); 569 QString des = (*it).note();
571 int startN; 570 int startN;
572 if( (startN = des.find( id ) ) >= 0 ) { 571 if( (startN = des.find( id ) ) >= 0 ) {
573 int endN = des.find( ",", startN+1 ); 572 int endN = des.find( ",", startN+1 );
574 des = des.left( startN ) + des.mid( endN+1 ); 573 des = des.left( startN ) + des.mid( endN+1 );
575 (*it).setNote( des ); 574 (*it).setNote( des );
576 } 575 }
577 } 576 }
578 } 577 }
579 } 578 }
580 } 579 }
581 if ( found ) 580 if ( found )
582 return; 581 return;
583 d->mAddressees.append( a ); 582 d->mAddressees.append( a );
584 Addressee& addr = d->mAddressees.last(); 583 Addressee& addr = d->mAddressees.last();
585 if ( addr.resource() == 0 ) 584 if ( addr.resource() == 0 )
586 addr.setResource( standardResource() ); 585 addr.setResource( standardResource() );
587 586
588 addr.setChanged( true ); 587 addr.setChanged( true );
589} 588}
590 589
591void AddressBook::removeAddressee( const Addressee &a ) 590void AddressBook::removeAddressee( const Addressee &a )
592{ 591{
593 Iterator it; 592 Iterator it;
594 Iterator it2; 593 Iterator it2;
595 bool found = false; 594 bool found = false;
596 for ( it = begin(); it != end(); ++it ) { 595 for ( it = begin(); it != end(); ++it ) {
597 if ( a.uid() == (*it).uid() ) { 596 if ( a.uid() == (*it).uid() ) {
598 found = true; 597 found = true;
599 it2 = it; 598 it2 = it;
600 } else { 599 } else {
601 if ( (*it).uid().left( 19 ) == QString("last-syncAddressee-") ) { 600 if ( (*it).uid().left( 19 ) == QString("last-syncAddressee-") ) {
602 QString name = (*it).uid().mid( 19 ); 601 QString name = (*it).uid().mid( 19 );
603 Addressee b = a; 602 Addressee b = a;
604 QString id = b.getID( name ); 603 QString id = b.getID( name );
605 if ( ! id.isEmpty() ) { 604 if ( ! id.isEmpty() ) {
606 QString des = (*it).note(); 605 QString des = (*it).note();
607 if( des.find( id ) < 0 ) { 606 if( des.find( id ) < 0 ) {
608 des += id + ","; 607 des += id + ",";
609 (*it).setNote( des ); 608 (*it).setNote( des );
610 } 609 }
611 } 610 }
612 } 611 }
613 612
614 } 613 }
615 } 614 }
616 615
617 if ( found ) 616 if ( found )
618 removeAddressee( it2 ); 617 removeAddressee( it2 );
619 618
620} 619}
621 620
622void AddressBook::removeSyncAddressees( bool removeDeleted ) 621void AddressBook::removeSyncAddressees( bool removeDeleted )
623{ 622{
624 Iterator it = begin(); 623 Iterator it = begin();
625 Iterator it2 ; 624 Iterator it2 ;
626 QDateTime dt ( QDate( 2004,1,1) ); 625 QDateTime dt ( QDate( 2004,1,1) );
627 while ( it != end() ) { 626 while ( it != end() ) {
628 (*it).setRevision( dt ); 627 (*it).setRevision( dt );
629 (*it).removeCustom( "KADDRESSBOOK", "X-ExternalID" ); 628 (*it).removeCustom( "KADDRESSBOOK", "X-ExternalID" );
630 (*it).setIDStr(""); 629 (*it).setIDStr("");
631 if ( ( (*it).tempSyncStat() == SYNC_TEMPSTATE_DELETE && removeDeleted )|| (*it).uid().left( 19 ) == QString("last-syncAddressee-")) { 630 if ( ( (*it).tempSyncStat() == SYNC_TEMPSTATE_DELETE && removeDeleted )|| (*it).uid().left( 19 ) == QString("last-syncAddressee-")) {
632 it2 = it; 631 it2 = it;
633 //qDebug("removing %s ",(*it).uid().latin1() ); 632 //qDebug("removing %s ",(*it).uid().latin1() );
634 ++it; 633 ++it;
635 removeAddressee( it2 ); 634 removeAddressee( it2 );
636 } else { 635 } else {
637 //qDebug("skipping %s ",(*it).uid().latin1() ); 636 //qDebug("skipping %s ",(*it).uid().latin1() );
638 ++it; 637 ++it;
639 } 638 }
640 } 639 }
641 deleteRemovedAddressees(); 640 deleteRemovedAddressees();
642} 641}
643 642
644void AddressBook::removeAddressee( const Iterator &it ) 643void AddressBook::removeAddressee( const Iterator &it )
645{ 644{
646 d->mRemovedAddressees.append( (*it) ); 645 d->mRemovedAddressees.append( (*it) );
647 d->mAddressees.remove( it.d->mIt ); 646 d->mAddressees.remove( it.d->mIt );
648} 647}
649 648
650AddressBook::Iterator AddressBook::find( const Addressee &a ) 649AddressBook::Iterator AddressBook::find( const Addressee &a )
651{ 650{
652 Iterator it; 651 Iterator it;
653 for ( it = begin(); it != end(); ++it ) { 652 for ( it = begin(); it != end(); ++it ) {
654 if ( a.uid() == (*it).uid() ) { 653 if ( a.uid() == (*it).uid() ) {
655 return it; 654 return it;
656 } 655 }
657 } 656 }
658 return end(); 657 return end();
659} 658}
660 659
661Addressee AddressBook::findByUid( const QString &uid ) 660Addressee AddressBook::findByUid( const QString &uid )
662{ 661{
663 Iterator it; 662 Iterator it;
664 for ( it = begin(); it != end(); ++it ) { 663 for ( it = begin(); it != end(); ++it ) {
665 if ( uid == (*it).uid() ) { 664 if ( uid == (*it).uid() ) {
666 return *it; 665 return *it;
667 } 666 }
668 } 667 }
669 return Addressee(); 668 return Addressee();
670} 669}
671void AddressBook::preExternSync( AddressBook* aBook, const QString& csd ) 670void AddressBook::preExternSync( AddressBook* aBook, const QString& csd )
672{ 671{
673 //qDebug("AddressBook::preExternSync "); 672 //qDebug("AddressBook::preExternSync ");
674 AddressBook::Iterator it; 673 AddressBook::Iterator it;
675 for ( it = begin(); it != end(); ++it ) { 674 for ( it = begin(); it != end(); ++it ) {
676 (*it).setID( csd, (*it).externalUID() ); 675 (*it).setID( csd, (*it).externalUID() );
677 (*it).computeCsum( csd ); 676 (*it).computeCsum( csd );
678 } 677 }
679 mergeAB( aBook ,csd ); 678 mergeAB( aBook ,csd );
680} 679}
681void AddressBook::postExternSync( AddressBook* aBook , const QString& csd) 680void AddressBook::postExternSync( AddressBook* aBook , const QString& csd)
682{ 681{
683 //qDebug("AddressBook::postExternSync "); 682 //qDebug("AddressBook::postExternSync ");
684 AddressBook::Iterator it; 683 AddressBook::Iterator it;
685 for ( it = begin(); it != end(); ++it ) { 684 for ( it = begin(); it != end(); ++it ) {
686 // qDebug("check uid %s ", (*it).uid().latin1() ); 685 // qDebug("check uid %s ", (*it).uid().latin1() );
687 if ( (*it).tempSyncStat() == SYNC_TEMPSTATE_NEW_ID || 686 if ( (*it).tempSyncStat() == SYNC_TEMPSTATE_NEW_ID ||
688 (*it).tempSyncStat() == SYNC_TEMPSTATE_NEW_CSUM ) { 687 (*it).tempSyncStat() == SYNC_TEMPSTATE_NEW_CSUM ) {
689 Addressee ad = aBook->findByUid( ( (*it).uid() )); 688 Addressee ad = aBook->findByUid( ( (*it).uid() ));
690 if ( ad.isEmpty() ) { 689 if ( ad.isEmpty() ) {
691 qDebug("postExternSync:ERROR addressee is empty: %s ", (*it).uid().latin1()); 690 qDebug("postExternSync:ERROR addressee is empty: %s ", (*it).uid().latin1());
692 } else { 691 } else {
693 (*it).computeCsum( csd ); 692 (*it).computeCsum( csd );
694 if ( (*it).tempSyncStat() == SYNC_TEMPSTATE_NEW_ID ) 693 if ( (*it).tempSyncStat() == SYNC_TEMPSTATE_NEW_ID )
695 ad.setID( csd, (*it).externalUID() ); 694 ad.setID( csd, (*it).externalUID() );
696 ad.setCsum( csd, (*it).getCsum( csd ) ); 695 ad.setCsum( csd, (*it).getCsum( csd ) );
697 aBook->insertAddressee( ad ); 696 aBook->insertAddressee( ad );
698 } 697 }
699 } 698 }
700 } 699 }
701} 700}
702 701
703bool AddressBook::containsExternalUid( const QString& uid ) 702bool AddressBook::containsExternalUid( const QString& uid )
704{ 703{
705 Iterator it; 704 Iterator it;
706 for ( it = begin(); it != end(); ++it ) { 705 for ( it = begin(); it != end(); ++it ) {
707 if ( uid == (*it).externalUID( ) ) 706 if ( uid == (*it).externalUID( ) )
708 return true; 707 return true;
709 } 708 }
710 return false; 709 return false;
711} 710}
712Addressee AddressBook::findByExternUid( const QString& uid , const QString& profile ) 711Addressee AddressBook::findByExternUid( const QString& uid , const QString& profile )
713{ 712{
714 Iterator it; 713 Iterator it;
715 for ( it = begin(); it != end(); ++it ) { 714 for ( it = begin(); it != end(); ++it ) {
716 if ( uid == (*it).getID( profile ) ) 715 if ( uid == (*it).getID( profile ) )
717 return (*it); 716 return (*it);
718 } 717 }
719 return Addressee(); 718 return Addressee();
720} 719}
721void AddressBook::mergeAB( AddressBook *aBook, const QString& profile ) 720void AddressBook::mergeAB( AddressBook *aBook, const QString& profile )
722{ 721{
723 Iterator it; 722 Iterator it;
724 Addressee ad; 723 Addressee ad;
725 for ( it = begin(); it != end(); ++it ) { 724 for ( it = begin(); it != end(); ++it ) {
726 ad = aBook->findByExternUid( (*it).externalUID(), profile ); 725 ad = aBook->findByExternUid( (*it).externalUID(), profile );
727 if ( !ad.isEmpty() ) { 726 if ( !ad.isEmpty() ) {
728 (*it).mergeContact( ad ); 727 (*it).mergeContact( ad );
729 } 728 }
730 } 729 }
731#if 0 730#if 0
732 // test only 731 // test only
733 for ( it = begin(); it != end(); ++it ) { 732 for ( it = begin(); it != end(); ++it ) {
734 733
735 qDebug("uid %s ", (*it).uid().latin1()); 734 qDebug("uid %s ", (*it).uid().latin1());
736 } 735 }
737#endif 736#endif
738} 737}
739 738
740#if 0 739#if 0
741Addressee::List AddressBook::getExternLastSyncAddressees() 740Addressee::List AddressBook::getExternLastSyncAddressees()
742{ 741{
743 Addressee::List results; 742 Addressee::List results;
744 743
745 Iterator it; 744 Iterator it;
746 for ( it = begin(); it != end(); ++it ) { 745 for ( it = begin(); it != end(); ++it ) {
747 if ( (*it).uid().left( 19 ) == "last-syncAddressee-" ) { 746 if ( (*it).uid().left( 19 ) == "last-syncAddressee-" ) {
748 if ( (*it).familyName().left(4) == "!E: " ) 747 if ( (*it).familyName().left(4) == "!E: " )
749 results.append( *it ); 748 results.append( *it );
750 } 749 }
751 } 750 }
752 751
753 return results; 752 return results;
754} 753}
755#endif 754#endif
756void AddressBook::resetTempSyncStat() 755void AddressBook::resetTempSyncStat()
757{ 756{
758 Iterator it; 757 Iterator it;
759 for ( it = begin(); it != end(); ++it ) { 758 for ( it = begin(); it != end(); ++it ) {
760 (*it).setTempSyncStat ( SYNC_TEMPSTATE_INITIAL ); 759 (*it).setTempSyncStat ( SYNC_TEMPSTATE_INITIAL );
761 } 760 }
762 761
763} 762}
764 763
765QStringList AddressBook:: uidList() 764QStringList AddressBook:: uidList()
766{ 765{
767 QStringList results; 766 QStringList results;
768 Iterator it; 767 Iterator it;
769 for ( it = begin(); it != end(); ++it ) { 768 for ( it = begin(); it != end(); ++it ) {
770 results.append( (*it).uid() ); 769 results.append( (*it).uid() );
771 } 770 }
772 return results; 771 return results;
773} 772}
774 773
775 774
776Addressee::List AddressBook::allAddressees() 775Addressee::List AddressBook::allAddressees()
777{ 776{
778 return d->mAddressees; 777 return d->mAddressees;
779 778
780} 779}
781 780
782Addressee::List AddressBook::findByName( const QString &name ) 781Addressee::List AddressBook::findByName( const QString &name )
783{ 782{
784 Addressee::List results; 783 Addressee::List results;
785 784
786 Iterator it; 785 Iterator it;
787 for ( it = begin(); it != end(); ++it ) { 786 for ( it = begin(); it != end(); ++it ) {
788 if ( name == (*it).realName() ) { 787 if ( name == (*it).realName() ) {
789 results.append( *it ); 788 results.append( *it );
790 } 789 }
791 } 790 }
792 791
793 return results; 792 return results;
794} 793}
795 794
796Addressee::List AddressBook::findByEmail( const QString &email ) 795Addressee::List AddressBook::findByEmail( const QString &email )
797{ 796{
798 Addressee::List results; 797 Addressee::List results;