summaryrefslogtreecommitdiffabout
path: root/kabc/addressbook.cpp
Unidiff
Diffstat (limited to 'kabc/addressbook.cpp') (more/less context) (show whitespace changes)
-rw-r--r--kabc/addressbook.cpp30
1 files changed, 15 insertions, 15 deletions
diff --git a/kabc/addressbook.cpp b/kabc/addressbook.cpp
index fe59fcb..c700897 100644
--- a/kabc/addressbook.cpp
+++ b/kabc/addressbook.cpp
@@ -16,50 +16,50 @@
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 <q3ptrlist.h>
41#include <qtextstream.h> 41#include <q3textstream.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;
@@ -223,49 +223,49 @@ bool AddressBook::ConstIterator::operator!=( const ConstIterator &it )
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.isEmpty()) {
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) );
@@ -360,55 +360,55 @@ bool AddressBook::load()
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, QString resourceName ) 380void AddressBook::export2File( QString fileName, QString resourceName )
381{ 381{
382 382
383 QFile outFile( fileName ); 383 QFile outFile( fileName );
384 if ( !outFile.open( IO_WriteOnly ) ) { 384 if ( !outFile.open( QIODevice::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 Q3TextStream t( &outFile );
390 t.setEncoding( QTextStream::UnicodeUTF8 ); 390 t.setEncoding( Q3TextStream::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() ) { 395 if ( (*it).resource() ) {
396 bool include = (*it).resource()->includeInSync(); 396 bool include = (*it).resource()->includeInSync();
397 if ( !resourceName.isEmpty() ) 397 if ( !resourceName.isEmpty() )
398 include = (resourceName == (*it).resource()->resourceName() ); 398 include = (resourceName == (*it).resource()->resourceName() );
399 if ( include ) { 399 if ( include ) {
400 //qDebug(QString ("Exporting resource %1 to file %2").arg( (*it).resource()->resourceName() ).arg( fileName ) ); 400 //qDebug(QString ("Exporting resource %1 to file %2").arg( (*it).resource()->resourceName() ).arg( fileName ) );
401 if ( !(*it).IDStr().isEmpty() ) { 401 if ( !(*it).IDStr().isEmpty() ) {
402 (*it).insertCustom( "KADDRESSBOOK", "X-ExternalID", (*it).IDStr() ); 402 (*it).insertCustom( "KADDRESSBOOK", "X-ExternalID", (*it).IDStr() );
403 } 403 }
404 KABC::VCardConverter converter; 404 KABC::VCardConverter converter;
405 QString vcard; 405 QString vcard;
406 //Resource *resource() const; 406 //Resource *resource() const;
407 converter.addresseeToVCard( *it, vcard, version ); 407 converter.addresseeToVCard( *it, vcard, version );
408 t << vcard << "\r\n"; 408 t << vcard << "\r\n";
409 } 409 }
410 } 410 }
411 } 411 }
412 t << "\r\n\r\n"; 412 t << "\r\n\r\n";
413 outFile.close(); 413 outFile.close();
414} 414}
@@ -447,71 +447,71 @@ bool AddressBook::export2PhoneFormat( QStringList uids ,QString fileName )
447 if ( semi < dopp && semi >= 0 ) 447 if ( semi < dopp && semi >= 0 )
448 sep = semi ; 448 sep = semi ;
449 else 449 else
450 sep = dopp; 450 sep = dopp;
451 vcardnew +=vcard.mid( start, next - start); 451 vcardnew +=vcard.mid( start, next - start);
452 vcardnew +=vcard.mid( next+5,sep -next -5 ).upper(); 452 vcardnew +=vcard.mid( next+5,sep -next -5 ).upper();
453 start = sep; 453 start = sep;
454 } 454 }
455 vcardnew += vcard.mid( start,vcard.length() ); 455 vcardnew += vcard.mid( start,vcard.length() );
456 vcard = ""; 456 vcard = "";
457 start = 0; 457 start = 0;
458 while ( (next = vcardnew.find("ADR", start) )>= 0 ) { 458 while ( (next = vcardnew.find("ADR", start) )>= 0 ) {
459 int sep = vcardnew.find(":", next); 459 int sep = vcardnew.find(":", next);
460 vcard +=vcardnew.mid( start, next - start+3); 460 vcard +=vcardnew.mid( start, next - start+3);
461 start = sep; 461 start = sep;
462 } 462 }
463 vcard += vcardnew.mid( start,vcardnew.length() ); 463 vcard += vcardnew.mid( start,vcardnew.length() );
464 vcard.replace ( QRegExp(";;;") , "" ); 464 vcard.replace ( QRegExp(";;;") , "" );
465 vcard.replace ( QRegExp(";;") , "" ); 465 vcard.replace ( QRegExp(";;") , "" );
466 datastream += vcard; 466 datastream += vcard;
467 467
468 } 468 }
469 469
470 QFile outFile(fileName); 470 QFile outFile(fileName);
471 if ( outFile.open(IO_WriteOnly) ) { 471 if ( outFile.open(QIODevice::WriteOnly) ) {
472 datastream.replace ( QRegExp("VERSION:3.0") , "VERSION:2.1" ); 472 datastream.replace ( QRegExp("VERSION:3.0") , "VERSION:2.1" );
473 QTextStream t( &outFile ); // use a text stream 473 Q3TextStream t( &outFile ); // use a text stream
474 t.setEncoding( QTextStream::UnicodeUTF8 ); 474 t.setEncoding( Q3TextStream::UnicodeUTF8 );
475 t <<datastream; 475 t <<datastream;
476 t << "\r\n\r\n"; 476 t << "\r\n\r\n";
477 outFile.close(); 477 outFile.close();
478 478
479 } else { 479 } else {
480 qDebug("Error open temp file "); 480 qDebug("Error open temp file ");
481 return false; 481 return false;
482 } 482 }
483 return true; 483 return true;
484 484
485} 485}
486int AddressBook::importFromFile( QString fileName, bool replaceLabel, bool removeOld, QString resource ) 486int AddressBook::importFromFile( QString fileName, bool replaceLabel, bool removeOld, QString resource )
487{ 487{
488 488
489 if ( removeOld ) 489 if ( removeOld )
490 setUntagged( true, resource ); 490 setUntagged( true, resource );
491 KABC::Addressee::List list; 491 KABC::Addressee::List list;
492 QFile file( fileName ); 492 QFile file( fileName );
493 file.open( IO_ReadOnly ); 493 file.open( QIODevice::ReadOnly );
494 QByteArray rawData = file.readAll(); 494 QByteArray rawData = file.readAll();
495 file.close(); 495 file.close();
496 QString data; 496 QString data;
497 if ( replaceLabel ) { 497 if ( replaceLabel ) {
498 data = QString::fromLatin1( rawData.data(), rawData.size() + 1 ); 498 data = QString::fromLatin1( rawData.data(), rawData.size() + 1 );
499 data.replace ( QRegExp("LABEL") , "ADR" ); 499 data.replace ( QRegExp("LABEL") , "ADR" );
500 data.replace ( QRegExp("CHARSET=ISO-8859-1") , "" ); 500 data.replace ( QRegExp("CHARSET=ISO-8859-1") , "" );
501 } else 501 } else
502 data = QString::fromUtf8( rawData.data(), rawData.size() + 1 ); 502 data = QString::fromUtf8( rawData.data(), rawData.size() + 1 );
503 KABC::VCardTool tool; 503 KABC::VCardTool tool;
504 list = tool.parseVCards( data ); 504 list = tool.parseVCards( data );
505 KABC::Addressee::List::Iterator it; 505 KABC::Addressee::List::Iterator it;
506 506
507 Resource * setRes = 0; 507 Resource * setRes = 0;
508 if ( !resource.isEmpty() ) { 508 if ( !resource.isEmpty() ) {
509 KRES::Manager<Resource>::ActiveIterator it; 509 KRES::Manager<Resource>::ActiveIterator it;
510 KRES::Manager<Resource> *manager = d->mManager; 510 KRES::Manager<Resource> *manager = d->mManager;
511 for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) { 511 for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) {
512 //qDebug("SaveAB::checking resource..." ); 512 //qDebug("SaveAB::checking resource..." );
513 if ( (*it)->resourceName() == resource ) { 513 if ( (*it)->resourceName() == resource ) {
514 setRes = (*it); 514 setRes = (*it);
515 qDebug("KA: AB: Inserting imported contacs to resource %s", resource.latin1()); 515 qDebug("KA: AB: Inserting imported contacs to resource %s", resource.latin1());
516 break; 516 break;
517 } 517 }
@@ -613,49 +613,49 @@ void AddressBook::removeSyncInfo( QString syncProfile)
613} 613}
614void AddressBook::preparePhoneSync( QString currentSyncDevice, bool isPreSync ) 614void AddressBook::preparePhoneSync( QString currentSyncDevice, bool isPreSync )
615{ 615{
616 Iterator ait; 616 Iterator ait;
617 for ( ait = begin(); ait != end(); ++ait ) { 617 for ( ait = begin(); ait != end(); ++ait ) {
618 QString id = (*ait).IDStr(); 618 QString id = (*ait).IDStr();
619 (*ait).setIDStr( ":"); 619 (*ait).setIDStr( ":");
620 (*ait).setExternalUID( id ); 620 (*ait).setExternalUID( id );
621 (*ait).setOriginalExternalUID( id ); 621 (*ait).setOriginalExternalUID( id );
622 if ( isPreSync ) 622 if ( isPreSync )
623 (*ait).setTempSyncStat( SYNC_TEMPSTATE_NEW_EXTERNAL ); 623 (*ait).setTempSyncStat( SYNC_TEMPSTATE_NEW_EXTERNAL );
624 else { 624 else {
625 (*ait).setTempSyncStat( SYNC_TEMPSTATE_NEW_ID ); 625 (*ait).setTempSyncStat( SYNC_TEMPSTATE_NEW_ID );
626 (*ait).setID( currentSyncDevice,id ); 626 (*ait).setID( currentSyncDevice,id );
627 627
628 } 628 }
629 } 629 }
630} 630}
631void AddressBook::findNewExtIds( QString fileName, QString currentSyncDevice ) 631void AddressBook::findNewExtIds( QString fileName, QString currentSyncDevice )
632{ 632{
633 633
634 setUntagged(); 634 setUntagged();
635 KABC::Addressee::List list; 635 KABC::Addressee::List list;
636 QFile file( fileName ); 636 QFile file( fileName );
637 file.open( IO_ReadOnly ); 637 file.open( QIODevice::ReadOnly );
638 QByteArray rawData = file.readAll(); 638 QByteArray rawData = file.readAll();
639 file.close(); 639 file.close();
640 QString data; 640 QString data;
641 641
642 data = QString::fromUtf8( rawData.data(), rawData.size() + 1 ); 642 data = QString::fromUtf8( rawData.data(), rawData.size() + 1 );
643 KABC::VCardTool tool; 643 KABC::VCardTool tool;
644 list = tool.parseVCards( data ); 644 list = tool.parseVCards( data );
645 KABC::Addressee::List::Iterator it; 645 KABC::Addressee::List::Iterator it;
646 for ( it = list.begin(); it != list.end(); ++it ) { 646 for ( it = list.begin(); it != list.end(); ++it ) {
647 Iterator ait; 647 Iterator ait;
648 for ( ait = begin(); ait != end(); ++ait ) { 648 for ( ait = begin(); ait != end(); ++ait ) {
649 if ( !(*ait).tagged() ) { 649 if ( !(*ait).tagged() ) {
650 if ( (*ait).containsAdr(*it)) { 650 if ( (*ait).containsAdr(*it)) {
651 (*ait).setTagged(true); 651 (*ait).setTagged(true);
652 QString id = (*it).custom( "KADDRESSBOOK", "X-ExternalID" ); 652 QString id = (*it).custom( "KADDRESSBOOK", "X-ExternalID" );
653 (*it).setIDStr( ":"); 653 (*it).setIDStr( ":");
654 (*it).setID( currentSyncDevice,id ); 654 (*it).setID( currentSyncDevice,id );
655 (*it).setExternalUID( id ); 655 (*it).setExternalUID( id );
656 (*it).setTempSyncStat( SYNC_TEMPSTATE_NEW_ID ); 656 (*it).setTempSyncStat( SYNC_TEMPSTATE_NEW_ID );
657 (*it).setUid( ( (*ait).uid() )); 657 (*it).setUid( ( (*ait).uid() ));
658 break; 658 break;
659 } 659 }
660 } 660 }
661 661
@@ -1197,73 +1197,73 @@ QDataStream &KABC::operator<<( QDataStream &s, const AddressBook &ab )
1197QDataStream &KABC::operator>>( QDataStream &s, AddressBook &ab ) 1197QDataStream &KABC::operator>>( QDataStream &s, AddressBook &ab )
1198{ 1198{
1199 if (!ab.d) return s; 1199 if (!ab.d) return s;
1200 1200
1201 s >> ab.d->mAddressees; 1201 s >> ab.d->mAddressees;
1202 1202
1203 return s; 1203 return s;
1204} 1204}
1205 1205
1206bool AddressBook::addResource( Resource *resource ) 1206bool AddressBook::addResource( Resource *resource )
1207{ 1207{
1208 if ( !resource->open() ) { 1208 if ( !resource->open() ) {
1209 kdDebug(5700) << "AddressBook::addResource(): can't add resource" << endl; 1209 kdDebug(5700) << "AddressBook::addResource(): can't add resource" << endl;
1210 return false; 1210 return false;
1211 } 1211 }
1212 1212
1213 resource->setAddressBook( this ); 1213 resource->setAddressBook( this );
1214 1214
1215 d->mManager->add( resource ); 1215 d->mManager->add( resource );
1216 return true; 1216 return true;
1217} 1217}
1218void AddressBook::removeResources() 1218void AddressBook::removeResources()
1219{ 1219{
1220 //remove all possible resources. This should cleanup the configfile. 1220 //remove all possible resources. This should cleanup the configfile.
1221 QPtrList<KABC::Resource> mResources = resources(); 1221 Q3PtrList<KABC::Resource> mResources = resources();
1222 1222
1223 QPtrListIterator<KABC::Resource> it(mResources); 1223 Q3PtrListIterator<KABC::Resource> it(mResources);
1224 for ( ; it.current(); ++it ) { 1224 for ( ; it.current(); ++it ) {
1225 KABC::Resource *res = it.current(); 1225 KABC::Resource *res = it.current();
1226 removeResource(res); 1226 removeResource(res);
1227 } 1227 }
1228} 1228}
1229 1229
1230bool AddressBook::removeResource( Resource *resource ) 1230bool AddressBook::removeResource( Resource *resource )
1231{ 1231{
1232 resource->close(); 1232 resource->close();
1233 1233
1234 if ( resource == standardResource() ) 1234 if ( resource == standardResource() )
1235 d->mManager->setStandardResource( 0 ); 1235 d->mManager->setStandardResource( 0 );
1236 1236
1237 resource->setAddressBook( 0 ); 1237 resource->setAddressBook( 0 );
1238 1238
1239 d->mManager->remove( resource ); 1239 d->mManager->remove( resource );
1240 return true; 1240 return true;
1241} 1241}
1242 1242
1243QPtrList<Resource> AddressBook::resources() 1243Q3PtrList<Resource> AddressBook::resources()
1244{ 1244{
1245 QPtrList<Resource> list; 1245 Q3PtrList<Resource> list;
1246 1246
1247// qDebug("AddressBook::resources() 1"); 1247// qDebug("AddressBook::resources() 1");
1248 1248
1249 KRES::Manager<Resource>::ActiveIterator it; 1249 KRES::Manager<Resource>::ActiveIterator it;
1250 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) 1250 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it )
1251 list.append( *it ); 1251 list.append( *it );
1252 1252
1253 return list; 1253 return list;
1254} 1254}
1255 1255
1256/*US 1256/*US
1257void AddressBook::setErrorHandler( ErrorHandler *handler ) 1257void AddressBook::setErrorHandler( ErrorHandler *handler )
1258{ 1258{
1259 delete d->mErrorHandler; 1259 delete d->mErrorHandler;
1260 d->mErrorHandler = handler; 1260 d->mErrorHandler = handler;
1261} 1261}
1262*/ 1262*/
1263 1263
1264void AddressBook::error( const QString& msg ) 1264void AddressBook::error( const QString& msg )
1265{ 1265{
1266/*US 1266/*US
1267 if ( !d->mErrorHandler ) // create default error handler 1267 if ( !d->mErrorHandler ) // create default error handler
1268 d->mErrorHandler = new ConsoleErrorHandler; 1268 d->mErrorHandler = new ConsoleErrorHandler;
1269 1269