summaryrefslogtreecommitdiffabout
path: root/kabc/addressbook.cpp
Unidiff
Diffstat (limited to 'kabc/addressbook.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--kabc/addressbook.cpp35
1 files changed, 23 insertions, 12 deletions
diff --git a/kabc/addressbook.cpp b/kabc/addressbook.cpp
index 8882259..592d78d 100644
--- a/kabc/addressbook.cpp
+++ b/kabc/addressbook.cpp
@@ -187,519 +187,530 @@ const Addressee* AddressBook::ConstIterator::operator->() const
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, false );
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
379void AddressBook::export2File( QString fileName ) 380void AddressBook::export2File( QString fileName )
380{ 381{
381 382
382 QFile outFile( fileName ); 383 QFile outFile( fileName );
383 if ( !outFile.open( IO_WriteOnly ) ) { 384 if ( !outFile.open( IO_WriteOnly ) ) {
384 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>" );
385 KMessageBox::error( 0, text.arg( fileName ) ); 386 KMessageBox::error( 0, text.arg( fileName ) );
386 return ; 387 return ;
387 } 388 }
388 QTextStream t( &outFile ); 389 QTextStream t( &outFile );
389 t.setEncoding( QTextStream::UnicodeUTF8 ); 390 t.setEncoding( QTextStream::UnicodeUTF8 );
390 Iterator it; 391 Iterator it;
391 KABC::VCardConverter::Version version; 392 KABC::VCardConverter::Version version;
392 version = KABC::VCardConverter::v3_0; 393 version = KABC::VCardConverter::v3_0;
393 for ( it = begin(); it != end(); ++it ) { 394 for ( it = begin(); it != end(); ++it ) {
394 if ( !(*it).IDStr().isEmpty() ) { 395 if ( (*it).resource() && (*it).resource()->includeInSync() ) {
395 (*it).insertCustom( "KADDRESSBOOK", "X-ExternalID", (*it).IDStr() ); 396 if ( !(*it).IDStr().isEmpty() ) {
397 (*it).insertCustom( "KADDRESSBOOK", "X-ExternalID", (*it).IDStr() );
398 }
399 KABC::VCardConverter converter;
400 QString vcard;
401 //Resource *resource() const;
402 converter.addresseeToVCard( *it, vcard, version );
403 t << vcard << "\r\n";
396 } 404 }
397 KABC::VCardConverter converter;
398 QString vcard;
399 //Resource *resource() const;
400 converter.addresseeToVCard( *it, vcard, version );
401 t << vcard << "\r\n";
402 } 405 }
403 t << "\r\n\r\n"; 406 t << "\r\n\r\n";
404 outFile.close(); 407 outFile.close();
405} 408}
406// if QStringList uids is empty, all are exported 409// if QStringList uids is empty, all are exported
407bool AddressBook::export2PhoneFormat( QStringList uids ,QString fileName ) 410bool AddressBook::export2PhoneFormat( QStringList uids ,QString fileName )
408{ 411{
409 KABC::VCardConverter converter; 412 KABC::VCardConverter converter;
410 QString datastream; 413 QString datastream;
411 Iterator it; 414 Iterator it;
412 bool all = uids.isEmpty(); 415 bool all = uids.isEmpty();
413 for ( it = begin(); it != end(); ++it ) { 416 for ( it = begin(); it != end(); ++it ) {
414 // for( QStringList::ConstIterator it = uids.begin(); it != uids.end(); ++it ) { 417 // for( QStringList::ConstIterator it = uids.begin(); it != uids.end(); ++it ) {
415 if ( ! all ) { 418 if ( ! all ) {
416 if ( ! ( uids.contains((*it).uid() ) )) 419 if ( ! ( uids.contains((*it).uid() ) ))
417 continue; 420 continue;
418 } 421 }
419 KABC::Addressee a = ( *it ); 422 KABC::Addressee a = ( *it );
420 if ( a.isEmpty() ) 423 if ( a.isEmpty() )
421 continue; 424 continue;
425 if ( all && a.resource() && !a.resource()->includeInSync() )
426 continue;
422 a.simplifyEmails(); 427 a.simplifyEmails();
423 a.simplifyPhoneNumbers(); 428 a.simplifyPhoneNumbers();
424 a.simplifyPhoneNumberTypes(); 429 a.simplifyPhoneNumberTypes();
425 a.simplifyAddresses(); 430 a.simplifyAddresses();
426 431
427 QString vcard; 432 QString vcard;
428 QString vcardnew; 433 QString vcardnew;
429 converter.addresseeToVCard( a, vcard ); 434 converter.addresseeToVCard( a, vcard );
430 int start = 0; 435 int start = 0;
431 int next; 436 int next;
432 while ( (next = vcard.find("TYPE=", start) )>= 0 ) { 437 while ( (next = vcard.find("TYPE=", start) )>= 0 ) {
433 int semi = vcard.find(";", next); 438 int semi = vcard.find(";", next);
434 int dopp = vcard.find(":", next); 439 int dopp = vcard.find(":", next);
435 int sep; 440 int sep;
436 if ( semi < dopp && semi >= 0 ) 441 if ( semi < dopp && semi >= 0 )
437 sep = semi ; 442 sep = semi ;
438 else 443 else
439 sep = dopp; 444 sep = dopp;
440 vcardnew +=vcard.mid( start, next - start); 445 vcardnew +=vcard.mid( start, next - start);
441 vcardnew +=vcard.mid( next+5,sep -next -5 ).upper(); 446 vcardnew +=vcard.mid( next+5,sep -next -5 ).upper();
442 start = sep; 447 start = sep;
443 } 448 }
444 vcardnew += vcard.mid( start,vcard.length() ); 449 vcardnew += vcard.mid( start,vcard.length() );
445 vcard = ""; 450 vcard = "";
446 start = 0; 451 start = 0;
447 while ( (next = vcardnew.find("ADR", start) )>= 0 ) { 452 while ( (next = vcardnew.find("ADR", start) )>= 0 ) {
448 int sep = vcardnew.find(":", next); 453 int sep = vcardnew.find(":", next);
449 vcard +=vcardnew.mid( start, next - start+3); 454 vcard +=vcardnew.mid( start, next - start+3);
450 start = sep; 455 start = sep;
451 } 456 }
452 vcard += vcardnew.mid( start,vcardnew.length() ); 457 vcard += vcardnew.mid( start,vcardnew.length() );
453 vcard.replace ( QRegExp(";;;") , "" ); 458 vcard.replace ( QRegExp(";;;") , "" );
454 vcard.replace ( QRegExp(";;") , "" ); 459 vcard.replace ( QRegExp(";;") , "" );
455 datastream += vcard; 460 datastream += vcard;
456 461
457 } 462 }
458 463
459 QFile outFile(fileName); 464 QFile outFile(fileName);
460 if ( outFile.open(IO_WriteOnly) ) { 465 if ( outFile.open(IO_WriteOnly) ) {
461 datastream.replace ( QRegExp("VERSION:3.0") , "VERSION:2.1" ); 466 datastream.replace ( QRegExp("VERSION:3.0") , "VERSION:2.1" );
462 QTextStream t( &outFile ); // use a text stream 467 QTextStream t( &outFile ); // use a text stream
463 t.setEncoding( QTextStream::UnicodeUTF8 ); 468 t.setEncoding( QTextStream::UnicodeUTF8 );
464 t <<datastream; 469 t <<datastream;
465 t << "\r\n\r\n"; 470 t << "\r\n\r\n";
466 outFile.close(); 471 outFile.close();
467 472
468 } else { 473 } else {
469 qDebug("Error open temp file "); 474 qDebug("Error open temp file ");
470 return false; 475 return false;
471 } 476 }
472 return true; 477 return true;
473 478
474} 479}
475void AddressBook::importFromFile( QString fileName, bool replaceLabel, bool removeOld ) 480void AddressBook::importFromFile( QString fileName, bool replaceLabel, bool removeOld )
476{ 481{
477 482
478 if ( removeOld ) 483 if ( removeOld )
479 setUntagged(); 484 setUntagged( true );
480 KABC::Addressee::List list; 485 KABC::Addressee::List list;
481 QFile file( fileName ); 486 QFile file( fileName );
482 file.open( IO_ReadOnly ); 487 file.open( IO_ReadOnly );
483 QByteArray rawData = file.readAll(); 488 QByteArray rawData = file.readAll();
484 file.close(); 489 file.close();
485 QString data; 490 QString data;
486 if ( replaceLabel ) { 491 if ( replaceLabel ) {
487 data = QString::fromLatin1( rawData.data(), rawData.size() + 1 ); 492 data = QString::fromLatin1( rawData.data(), rawData.size() + 1 );
488 data.replace ( QRegExp("LABEL") , "ADR" ); 493 data.replace ( QRegExp("LABEL") , "ADR" );
489 data.replace ( QRegExp("CHARSET=ISO-8859-1") , "" ); 494 data.replace ( QRegExp("CHARSET=ISO-8859-1") , "" );
490 } else 495 } else
491 data = QString::fromUtf8( rawData.data(), rawData.size() + 1 ); 496 data = QString::fromUtf8( rawData.data(), rawData.size() + 1 );
492 KABC::VCardTool tool; 497 KABC::VCardTool tool;
493 list = tool.parseVCards( data ); 498 list = tool.parseVCards( data );
494 KABC::Addressee::List::Iterator it; 499 KABC::Addressee::List::Iterator it;
495 for ( it = list.begin(); it != list.end(); ++it ) { 500 for ( it = list.begin(); it != list.end(); ++it ) {
496 QString id = (*it).custom( "KADDRESSBOOK", "X-ExternalID" ); 501 QString id = (*it).custom( "KADDRESSBOOK", "X-ExternalID" );
497 if ( !id.isEmpty() ) 502 if ( !id.isEmpty() )
498 (*it).setIDStr(id ); 503 (*it).setIDStr(id );
499 (*it).setResource( 0 ); 504 (*it).setResource( 0 );
500 if ( replaceLabel ) 505 if ( replaceLabel )
501 (*it).removeVoice(); 506 (*it).removeVoice();
502 if ( removeOld ) 507 if ( removeOld )
503 (*it).setTagged( true ); 508 (*it).setTagged( true );
504 insertAddressee( (*it), false, true ); 509 insertAddressee( (*it), false, true );
505 } 510 }
506 if ( removeOld ) 511 if ( removeOld )
507 removeUntagged(); 512 removeUntagged();
508} 513}
509void AddressBook::setUntagged() 514void AddressBook::setUntagged(bool setNonSyncTagged) // = false)
510{ 515{
511 Iterator ait; 516 Iterator ait;
512 for ( ait = begin(); ait != end(); ++ait ) { 517 for ( ait = begin(); ait != end(); ++ait ) {
513 (*ait).setTagged( false ); 518 if ( setNonSyncTagged ) {
519 if ( (*ait).resource() && ! (*ait).resource()->includeInSync() ) {
520 (*ait).setTagged( true );
521 } else
522 (*ait).setTagged( false );
523 } else
524 (*ait).setTagged( false );
514 } 525 }
515} 526}
516void AddressBook::removeUntagged() 527void AddressBook::removeUntagged()
517{ 528{
518 Iterator ait; 529 Iterator ait;
519 bool todelete = false; 530 bool todelete = false;
520 Iterator todel; 531 Iterator todel;
521 for ( ait = begin(); ait != end(); ++ait ) { 532 for ( ait = begin(); ait != end(); ++ait ) {
522 if ( todelete ) 533 if ( todelete )
523 removeAddressee( todel ); 534 removeAddressee( todel );
524 if (!(*ait).tagged()) { 535 if (!(*ait).tagged()) {
525 todelete = true; 536 todelete = true;
526 todel = ait; 537 todel = ait;
527 } else 538 } else
528 todelete = false; 539 todelete = false;
529 } 540 }
530 if ( todelete ) 541 if ( todelete )
531 removeAddressee( todel ); 542 removeAddressee( todel );
532 deleteRemovedAddressees(); 543 deleteRemovedAddressees();
533} 544}
534void AddressBook::smplifyAddressees() 545void AddressBook::smplifyAddressees()
535{ 546{
536 Iterator ait; 547 Iterator ait;
537 for ( ait = begin(); ait != end(); ++ait ) { 548 for ( ait = begin(); ait != end(); ++ait ) {
538 (*ait).simplifyEmails(); 549 (*ait).simplifyEmails();
539 (*ait).simplifyPhoneNumbers(); 550 (*ait).simplifyPhoneNumbers();
540 (*ait).simplifyPhoneNumberTypes(); 551 (*ait).simplifyPhoneNumberTypes();
541 (*ait).simplifyAddresses(); 552 (*ait).simplifyAddresses();
542 } 553 }
543} 554}
544void AddressBook::preparePhoneSync( QString currentSyncDevice, bool isPreSync ) 555void AddressBook::preparePhoneSync( QString currentSyncDevice, bool isPreSync )
545{ 556{
546 Iterator ait; 557 Iterator ait;
547 for ( ait = begin(); ait != end(); ++ait ) { 558 for ( ait = begin(); ait != end(); ++ait ) {
548 QString id = (*ait).IDStr(); 559 QString id = (*ait).IDStr();
549 (*ait).setIDStr( ":"); 560 (*ait).setIDStr( ":");
550 (*ait).setExternalUID( id ); 561 (*ait).setExternalUID( id );
551 (*ait).setOriginalExternalUID( id ); 562 (*ait).setOriginalExternalUID( id );
552 if ( isPreSync ) 563 if ( isPreSync )
553 (*ait).setTempSyncStat( SYNC_TEMPSTATE_NEW_EXTERNAL ); 564 (*ait).setTempSyncStat( SYNC_TEMPSTATE_NEW_EXTERNAL );
554 else { 565 else {
555 (*ait).setTempSyncStat( SYNC_TEMPSTATE_NEW_ID ); 566 (*ait).setTempSyncStat( SYNC_TEMPSTATE_NEW_ID );
556 (*ait).setID( currentSyncDevice,id ); 567 (*ait).setID( currentSyncDevice,id );
557 568
558 } 569 }
559 } 570 }
560} 571}
561void AddressBook::findNewExtIds( QString fileName, QString currentSyncDevice ) 572void AddressBook::findNewExtIds( QString fileName, QString currentSyncDevice )
562{ 573{
563 574
564 setUntagged(); 575 setUntagged();
565 KABC::Addressee::List list; 576 KABC::Addressee::List list;
566 QFile file( fileName ); 577 QFile file( fileName );
567 file.open( IO_ReadOnly ); 578 file.open( IO_ReadOnly );
568 QByteArray rawData = file.readAll(); 579 QByteArray rawData = file.readAll();
569 file.close(); 580 file.close();
570 QString data; 581 QString data;
571 582
572 data = QString::fromUtf8( rawData.data(), rawData.size() + 1 ); 583 data = QString::fromUtf8( rawData.data(), rawData.size() + 1 );
573 KABC::VCardTool tool; 584 KABC::VCardTool tool;
574 list = tool.parseVCards( data ); 585 list = tool.parseVCards( data );
575 KABC::Addressee::List::Iterator it; 586 KABC::Addressee::List::Iterator it;
576 for ( it = list.begin(); it != list.end(); ++it ) { 587 for ( it = list.begin(); it != list.end(); ++it ) {
577 Iterator ait; 588 Iterator ait;
578 for ( ait = begin(); ait != end(); ++ait ) { 589 for ( ait = begin(); ait != end(); ++ait ) {
579 if ( !(*ait).tagged() ) { 590 if ( !(*ait).tagged() ) {
580 if ( (*ait).containsAdr(*it)) { 591 if ( (*ait).containsAdr(*it)) {
581 (*ait).setTagged(true); 592 (*ait).setTagged(true);
582 QString id = (*it).custom( "KADDRESSBOOK", "X-ExternalID" ); 593 QString id = (*it).custom( "KADDRESSBOOK", "X-ExternalID" );
583 (*it).setIDStr( ":"); 594 (*it).setIDStr( ":");
584 (*it).setID( currentSyncDevice,id ); 595 (*it).setID( currentSyncDevice,id );
585 (*it).setExternalUID( id ); 596 (*it).setExternalUID( id );
586 (*it).setTempSyncStat( SYNC_TEMPSTATE_NEW_ID ); 597 (*it).setTempSyncStat( SYNC_TEMPSTATE_NEW_ID );
587 (*it).setUid( ( (*ait).uid() )); 598 (*it).setUid( ( (*ait).uid() ));
588 break; 599 break;
589 } 600 }
590 } 601 }
591 602
592 } 603 }
593 if ( ait == end() ) 604 if ( ait == end() )
594 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());
595 } 606 }
596 clear(); 607 clear();
597 for ( it = list.begin(); it != list.end(); ++it ) { 608 for ( it = list.begin(); it != list.end(); ++it ) {
598 insertAddressee( (*it) ); 609 insertAddressee( (*it) );
599 } 610 }
600} 611}
601 612
602bool AddressBook::saveABphone( QString fileName ) 613bool AddressBook::saveABphone( QString fileName )
603{ 614{
604 //smplifyAddressees(); 615 //smplifyAddressees();
605 qDebug("saveABphone:: saving AB... "); 616 qDebug("saveABphone:: saving AB... ");
606 if ( ! export2PhoneFormat( QStringList() ,fileName ) ) 617 if ( ! export2PhoneFormat( QStringList() ,fileName ) )
607 return false; 618 return false;
608 qDebug("saveABphone:: writing to phone... "); 619 qDebug("saveABphone:: writing to phone... ");
609 if ( !PhoneAccess::writeToPhone( fileName) ) { 620 if ( !PhoneAccess::writeToPhone( fileName) ) {
610 return false; 621 return false;
611 } 622 }
612 qDebug("saveABphone:: re-reading from phone... "); 623 qDebug("saveABphone:: re-reading from phone... ");
613 if ( !PhoneAccess::readFromPhone( fileName) ) { 624 if ( !PhoneAccess::readFromPhone( fileName) ) {
614 return false; 625 return false;
615 } 626 }
616 return true; 627 return true;
617} 628}
618bool AddressBook::saveAB() 629bool AddressBook::saveAB()
619{ 630{
620 bool ok = true; 631 bool ok = true;
621 632
622 deleteRemovedAddressees(); 633 deleteRemovedAddressees();
623 Iterator ait; 634 Iterator ait;
624 for ( ait = begin(); ait != end(); ++ait ) { 635 for ( ait = begin(); ait != end(); ++ait ) {
625 if ( !(*ait).IDStr().isEmpty() ) { 636 if ( !(*ait).IDStr().isEmpty() ) {
626 (*ait).insertCustom( "KADDRESSBOOK", "X-ExternalID", (*ait).IDStr() ); 637 (*ait).insertCustom( "KADDRESSBOOK", "X-ExternalID", (*ait).IDStr() );
627 } 638 }
628 } 639 }
629 KRES::Manager<Resource>::ActiveIterator it; 640 KRES::Manager<Resource>::ActiveIterator it;
630 KRES::Manager<Resource> *manager = d->mManager; 641 KRES::Manager<Resource> *manager = d->mManager;
631 qDebug("SaveAB::saving..." ); 642 qDebug("SaveAB::saving..." );
632 for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) { 643 for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) {
633 qDebug("SaveAB::checking resource..." ); 644 qDebug("SaveAB::checking resource..." );
634 if ( (*it)->readOnly() ) 645 if ( (*it)->readOnly() )
635 qDebug("resource is readonly." ); 646 qDebug("resource is readonly." );
636 if ( (*it)->isOpen() ) 647 if ( (*it)->isOpen() )
637 qDebug("resource is open" ); 648 qDebug("resource is open" );
638 649
639 if ( !(*it)->readOnly() && (*it)->isOpen() ) { 650 if ( !(*it)->readOnly() && (*it)->isOpen() ) {
640 Ticket *ticket = requestSaveTicket( *it ); 651 Ticket *ticket = requestSaveTicket( *it );
641 qDebug("StdAddressBook::save '%s'", (*it)->resourceName().latin1() ); 652 qDebug("StdAddressBook::save '%s'", (*it)->resourceName().latin1() );
642 if ( !ticket ) { 653 if ( !ticket ) {
643 qDebug( i18n( "Unable to save to resource '%1'. It is locked." ) 654 qDebug( i18n( "Unable to save to resource '%1'. It is locked." )
644 .arg( (*it)->resourceName() ) ); 655 .arg( (*it)->resourceName() ) );
645 return false; 656 return false;
646 } 657 }
647 658
648 //if ( !save( ticket ) ) 659 //if ( !save( ticket ) )
649 if ( ticket->resource() ) { 660 if ( ticket->resource() ) {
650 QString name = ticket->resource()->resourceName(); 661 QString name = ticket->resource()->resourceName();
651 if ( ! ticket->resource()->save( ticket ) ) 662 if ( ! ticket->resource()->save( ticket ) )
652 ok = false; 663 ok = false;
653 else 664 else
654 qDebug("StdAddressBook::resource saved '%s'", name.latin1() ); 665 qDebug("StdAddressBook::resource saved '%s'", name.latin1() );
655 666
656 } else 667 } else
657 ok = false; 668 ok = false;
658 669
659 } 670 }
660 } 671 }
661 return ok; 672 return ok;
662} 673}
663 674
664AddressBook::Iterator AddressBook::begin() 675AddressBook::Iterator AddressBook::begin()
665{ 676{
666 Iterator it = Iterator(); 677 Iterator it = Iterator();
667 it.d->mIt = d->mAddressees.begin(); 678 it.d->mIt = d->mAddressees.begin();
668 return it; 679 return it;
669} 680}
670 681
671AddressBook::ConstIterator AddressBook::begin() const 682AddressBook::ConstIterator AddressBook::begin() const
672{ 683{
673 ConstIterator it = ConstIterator(); 684 ConstIterator it = ConstIterator();
674 it.d->mIt = d->mAddressees.begin(); 685 it.d->mIt = d->mAddressees.begin();
675 return it; 686 return it;
676} 687}
677 688
678AddressBook::Iterator AddressBook::end() 689AddressBook::Iterator AddressBook::end()
679{ 690{
680 Iterator it = Iterator(); 691 Iterator it = Iterator();
681 it.d->mIt = d->mAddressees.end(); 692 it.d->mIt = d->mAddressees.end();
682 return it; 693 return it;
683} 694}
684 695
685AddressBook::ConstIterator AddressBook::end() const 696AddressBook::ConstIterator AddressBook::end() const
686{ 697{
687 ConstIterator it = ConstIterator(); 698 ConstIterator it = ConstIterator();
688 it.d->mIt = d->mAddressees.end(); 699 it.d->mIt = d->mAddressees.end();
689 return it; 700 return it;
690} 701}
691 702
692void AddressBook::clear() 703void AddressBook::clear()
693{ 704{
694 d->mAddressees.clear(); 705 d->mAddressees.clear();
695} 706}
696 707
697Ticket *AddressBook::requestSaveTicket( Resource *resource ) 708Ticket *AddressBook::requestSaveTicket( Resource *resource )
698{ 709{
699 kdDebug(5700) << "AddressBook::requestSaveTicket()" << endl; 710 kdDebug(5700) << "AddressBook::requestSaveTicket()" << endl;
700 711
701 if ( !resource ) 712 if ( !resource )
702 { 713 {
703 qDebug("AddressBook::requestSaveTicket no resource" ); 714 qDebug("AddressBook::requestSaveTicket no resource" );
704 resource = standardResource(); 715 resource = standardResource();
705 } 716 }