summaryrefslogtreecommitdiffabout
path: root/kabc
Unidiff
Diffstat (limited to 'kabc') (more/less context) (ignore whitespace changes)
-rw-r--r--kabc/addressbook.cpp70
-rw-r--r--kabc/addressbook.h5
2 files changed, 67 insertions, 8 deletions
diff --git a/kabc/addressbook.cpp b/kabc/addressbook.cpp
index 5fb49eb..295cf03 100644
--- a/kabc/addressbook.cpp
+++ b/kabc/addressbook.cpp
@@ -9,71 +9,76 @@
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>
42#include <qfile.h>
41 43
42#include <kglobal.h> 44#include <kglobal.h>
43#include <klocale.h> 45#include <klocale.h>>
46#include <kmessagebox.h>
44#include <kdebug.h> 47#include <kdebug.h>
45#include <libkcal/syncdefines.h> 48#include <libkcal/syncdefines.h>
46#include "addressbook.h" 49#include "addressbook.h"
47#include "resource.h" 50#include "resource.h"
51#include "vcardconverter.h"
52#include "vcardparser/vcardtool.h"
48 53
49//US #include "addressbook.moc" 54//US #include "addressbook.moc"
50 55
51using namespace KABC; 56using namespace KABC;
52 57
53struct AddressBook::AddressBookData 58struct AddressBook::AddressBookData
54{ 59{
55 Addressee::List mAddressees; 60 Addressee::List mAddressees;
56 Addressee::List mRemovedAddressees; 61 Addressee::List mRemovedAddressees;
57 Field::List mAllFields; 62 Field::List mAllFields;
58 KConfig *mConfig; 63 KConfig *mConfig;
59 KRES::Manager<Resource> *mManager; 64 KRES::Manager<Resource> *mManager;
60//US ErrorHandler *mErrorHandler; 65//US ErrorHandler *mErrorHandler;
61}; 66};
62 67
63struct AddressBook::Iterator::IteratorData 68struct AddressBook::Iterator::IteratorData
64{ 69{
65 Addressee::List::Iterator mIt; 70 Addressee::List::Iterator mIt;
66}; 71};
67 72
68struct AddressBook::ConstIterator::ConstIteratorData 73struct AddressBook::ConstIterator::ConstIteratorData
69{ 74{
70 Addressee::List::ConstIterator mIt; 75 Addressee::List::ConstIterator mIt;
71}; 76};
72 77
73AddressBook::Iterator::Iterator() 78AddressBook::Iterator::Iterator()
74{ 79{
75 d = new IteratorData; 80 d = new IteratorData;
76} 81}
77 82
78AddressBook::Iterator::Iterator( const AddressBook::Iterator &i ) 83AddressBook::Iterator::Iterator( const AddressBook::Iterator &i )
79{ 84{
@@ -341,64 +346,112 @@ bool AddressBook::load()
341 bool ok = true; 346 bool ok = true;
342 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) 347 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it )
343 if ( !(*it)->load() ) { 348 if ( !(*it)->load() ) {
344 error( i18n("Unable to load resource '%1'").arg( (*it)->resourceName() ) ); 349 error( i18n("Unable to load resource '%1'").arg( (*it)->resourceName() ) );
345 ok = false; 350 ok = false;
346 } 351 }
347 352
348 // mark all addressees as unchanged 353 // mark all addressees as unchanged
349 Addressee::List::Iterator addrIt; 354 Addressee::List::Iterator addrIt;
350 for ( addrIt = d->mAddressees.begin(); addrIt != d->mAddressees.end(); ++addrIt ) { 355 for ( addrIt = d->mAddressees.begin(); addrIt != d->mAddressees.end(); ++addrIt ) {
351 (*addrIt).setChanged( false ); 356 (*addrIt).setChanged( false );
352 QString id = (*addrIt).custom( "KADDRESSBOOK", "X-ExternalID" ); 357 QString id = (*addrIt).custom( "KADDRESSBOOK", "X-ExternalID" );
353 if ( !id.isEmpty() ) { 358 if ( !id.isEmpty() ) {
354 //qDebug("setId aa %s ", id.latin1()); 359 //qDebug("setId aa %s ", id.latin1());
355 (*addrIt).setIDStr(id ); 360 (*addrIt).setIDStr(id );
356 } 361 }
357 } 362 }
358 blockLSEchange = true; 363 blockLSEchange = true;
359 return ok; 364 return ok;
360} 365}
361 366
362bool AddressBook::save( Ticket *ticket ) 367bool AddressBook::save( Ticket *ticket )
363{ 368{
364 kdDebug(5700) << "AddressBook::save()"<< endl; 369 kdDebug(5700) << "AddressBook::save()"<< endl;
365 370
366 if ( ticket->resource() ) { 371 if ( ticket->resource() ) {
367 deleteRemovedAddressees(); 372 deleteRemovedAddressees();
368 return ticket->resource()->save( ticket ); 373 return ticket->resource()->save( ticket );
369 } 374 }
370 375
371 return false; 376 return false;
372} 377}
378void AddressBook::export2File( QString fileName )
379{
380
381 QFile outFile( fileName );
382 if ( !outFile.open( IO_WriteOnly ) ) {
383 QString text = i18n( "<qt>Unable to open file <b>%1</b> for export.</qt>" );
384 KMessageBox::error( 0, text.arg( fileName ) );
385 return ;
386 }
387 QTextStream t( &outFile );
388 t.setEncoding( QTextStream::UnicodeUTF8 );
389 Iterator it;
390 KABC::VCardConverter::Version version;
391 version = KABC::VCardConverter::v3_0;
392 for ( it = begin(); it != end(); ++it ) {
393 if ( !(*it).IDStr().isEmpty() ) {
394 (*it).insertCustom( "KADDRESSBOOK", "X-ExternalID", (*it).IDStr() );
395 }
396 KABC::VCardConverter converter;
397 QString vcard;
398 //Resource *resource() const;
399 converter.addresseeToVCard( *it, vcard, version );
400 t << vcard << "\r\n";
401 }
402 outFile.close();
403}
404void AddressBook::importFromFile( QString fileName )
405{
406
407 KABC::Addressee::List list;
408 QFile file( fileName );
409
410 file.open( IO_ReadOnly );
411 QByteArray rawData = file.readAll();
412 file.close();
413
414 QString data = QString::fromUtf8( rawData.data(), rawData.size() + 1 );
415 KABC::VCardTool tool;
416 list = tool.parseVCards( data );
417
418 KABC::Addressee::List::Iterator it;
419 for ( it = list.begin(); it != list.end(); ++it ) {
420 (*it).setResource( 0 );
421 insertAddressee( (*it), false, true );
422 }
423
424}
425
373bool AddressBook::saveAB() 426bool AddressBook::saveAB()
374{ 427{
375 bool ok = true; 428 bool ok = true;
376 429
377 deleteRemovedAddressees(); 430 deleteRemovedAddressees();
378 Iterator ait; 431 Iterator ait;
379 for ( ait = begin(); ait != end(); ++ait ) { 432 for ( ait = begin(); ait != end(); ++ait ) {
380 if ( !(*ait).IDStr().isEmpty() ) { 433 if ( !(*ait).IDStr().isEmpty() ) {
381 (*ait).insertCustom( "KADDRESSBOOK", "X-ExternalID", (*ait).IDStr() ); 434 (*ait).insertCustom( "KADDRESSBOOK", "X-ExternalID", (*ait).IDStr() );
382 } 435 }
383 } 436 }
384 KRES::Manager<Resource>::ActiveIterator it; 437 KRES::Manager<Resource>::ActiveIterator it;
385 KRES::Manager<Resource> *manager = d->mManager; 438 KRES::Manager<Resource> *manager = d->mManager;
386 for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) { 439 for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) {
387 if ( !(*it)->readOnly() && (*it)->isOpen() ) { 440 if ( !(*it)->readOnly() && (*it)->isOpen() ) {
388 Ticket *ticket = requestSaveTicket( *it ); 441 Ticket *ticket = requestSaveTicket( *it );
389// qDebug("StdAddressBook::save '%s'", (*it)->resourceName().latin1() ); 442// qDebug("StdAddressBook::save '%s'", (*it)->resourceName().latin1() );
390 if ( !ticket ) { 443 if ( !ticket ) {
391 error( i18n( "Unable to save to resource '%1'. It is locked." ) 444 error( i18n( "Unable to save to resource '%1'. It is locked." )
392 .arg( (*it)->resourceName() ) ); 445 .arg( (*it)->resourceName() ) );
393 return false; 446 return false;
394 } 447 }
395 448
396 //if ( !save( ticket ) ) 449 //if ( !save( ticket ) )
397 if ( ticket->resource() ) { 450 if ( ticket->resource() ) {
398 if ( ! ticket->resource()->save( ticket ) ) 451 if ( ! ticket->resource()->save( ticket ) )
399 ok = false; 452 ok = false;
400 } else 453 } else
401 ok = false; 454 ok = false;
402 455
403 } 456 }
404 } 457 }
@@ -432,85 +485,90 @@ AddressBook::ConstIterator AddressBook::end() const
432 it.d->mIt = d->mAddressees.end(); 485 it.d->mIt = d->mAddressees.end();
433 return it; 486 return it;
434} 487}
435 488
436void AddressBook::clear() 489void AddressBook::clear()
437{ 490{
438 d->mAddressees.clear(); 491 d->mAddressees.clear();
439} 492}
440 493
441Ticket *AddressBook::requestSaveTicket( Resource *resource ) 494Ticket *AddressBook::requestSaveTicket( Resource *resource )
442{ 495{
443 kdDebug(5700) << "AddressBook::requestSaveTicket()" << endl; 496 kdDebug(5700) << "AddressBook::requestSaveTicket()" << endl;
444 497
445 if ( !resource ) 498 if ( !resource )
446 { 499 {
447 qDebug("AddressBook::requestSaveTicket no resource" ); 500 qDebug("AddressBook::requestSaveTicket no resource" );
448 resource = standardResource(); 501 resource = standardResource();
449 } 502 }
450 503
451 KRES::Manager<Resource>::ActiveIterator it; 504 KRES::Manager<Resource>::ActiveIterator it;
452 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) { 505 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
453 if ( (*it) == resource ) { 506 if ( (*it) == resource ) {
454 if ( (*it)->readOnly() || !(*it)->isOpen() ) 507 if ( (*it)->readOnly() || !(*it)->isOpen() )
455 return 0; 508 return 0;
456 else 509 else
457 return (*it)->requestSaveTicket(); 510 return (*it)->requestSaveTicket();
458 } 511 }
459 } 512 }
460 513
461 return 0; 514 return 0;
462} 515}
463 516
464void AddressBook::insertAddressee( const Addressee &a, bool setRev ) 517void AddressBook::insertAddressee( const Addressee &a, bool setRev, bool takeResource )
465{ 518{
466 if ( blockLSEchange && setRev && a.uid().left( 19 ) == QString("last-syncAddressee-") ) { 519 if ( blockLSEchange && setRev && a.uid().left( 19 ) == QString("last-syncAddressee-") ) {
467 //qDebug("block insert "); 520 //qDebug("block insert ");
468 return; 521 return;
469 } 522 }
470 //qDebug("inserting.... %s ",a.uid().latin1() ); 523 //qDebug("inserting.... %s ",a.uid().latin1() );
471 bool found = false; 524 bool found = false;
472 Addressee::List::Iterator it; 525 Addressee::List::Iterator it;
473 for ( it = d->mAddressees.begin(); it != d->mAddressees.end(); ++it ) { 526 for ( it = d->mAddressees.begin(); it != d->mAddressees.end(); ++it ) {
474 if ( a.uid() == (*it).uid() ) { 527 if ( a.uid() == (*it).uid() ) {
475 528
476 bool changed = false; 529 bool changed = false;
477 Addressee addr = a; 530 Addressee addr = a;
478 if ( addr != (*it) ) 531 if ( addr != (*it) )
479 changed = true; 532 changed = true;
480 533
481 (*it) = a; 534 if ( takeResource ) {
482 if ( (*it).resource() == 0 ) 535 Resource * res = (*it).resource();
483 (*it).setResource( standardResource() ); 536 (*it) = a;
484 537 (*it).setResource( res );
538 } else {
539 (*it) = a;
540 if ( (*it).resource() == 0 )
541 (*it).setResource( standardResource() );
542 }
485 if ( changed ) { 543 if ( changed ) {
486 if ( setRev ) { 544 if ( setRev ) {
487 545
488 // get rid of micro seconds 546 // get rid of micro seconds
489 QDateTime dt = QDateTime::currentDateTime(); 547 QDateTime dt = QDateTime::currentDateTime();
490 QTime t = dt.time(); 548 QTime t = dt.time();
491 dt.setTime( QTime (t.hour (), t.minute (), t.second () ) ); 549 dt.setTime( QTime (t.hour (), t.minute (), t.second () ) );
492 (*it).setRevision( dt ); 550 (*it).setRevision( dt );
493 } 551 }
494 (*it).setChanged( true ); 552 (*it).setChanged( true );
495 } 553 }
496 554
497 found = true; 555 found = true;
498 } else { 556 } else {
499 if ( (*it).uid().left( 19 ) == QString("last-syncAddressee-") ) { 557 if ( (*it).uid().left( 19 ) == QString("last-syncAddressee-") ) {
500 QString name = (*it).uid().mid( 19 ); 558 QString name = (*it).uid().mid( 19 );
501 Addressee b = a; 559 Addressee b = a;
502 QString id = b.getID( name ); 560 QString id = b.getID( name );
503 if ( ! id.isEmpty() ) { 561 if ( ! id.isEmpty() ) {
504 QString des = (*it).note(); 562 QString des = (*it).note();
505 int startN; 563 int startN;
506 if( (startN = des.find( id ) ) >= 0 ) { 564 if( (startN = des.find( id ) ) >= 0 ) {
507 int endN = des.find( ",", startN+1 ); 565 int endN = des.find( ",", startN+1 );
508 des = des.left( startN ) + des.mid( endN+1 ); 566 des = des.left( startN ) + des.mid( endN+1 );
509 (*it).setNote( des ); 567 (*it).setNote( des );
510 } 568 }
511 } 569 }
512 } 570 }
513 } 571 }
514 } 572 }
515 if ( found ) 573 if ( found )
516 return; 574 return;
diff --git a/kabc/addressbook.h b/kabc/addressbook.h
index 8f62f0d..3603ec1 100644
--- a/kabc/addressbook.h
+++ b/kabc/addressbook.h
@@ -113,96 +113,97 @@ class AddressBook : public QObject
113 Constructs a address book object. 113 Constructs a address book object.
114 114
115 @param format File format class. 115 @param format File format class.
116 */ 116 */
117 AddressBook(); 117 AddressBook();
118 AddressBook( const QString &config ); 118 AddressBook( const QString &config );
119 AddressBook( const QString &config, const QString &family ); 119 AddressBook( const QString &config, const QString &family );
120 virtual ~AddressBook(); 120 virtual ~AddressBook();
121 121
122 /** 122 /**
123 Requests a ticket for saving the addressbook. Calling this function locks 123 Requests a ticket for saving the addressbook. Calling this function locks
124 the addressbook for all other processes. If the address book is already 124 the addressbook for all other processes. If the address book is already
125 locked the function returns 0. You need the returned @ref Ticket object 125 locked the function returns 0. You need the returned @ref Ticket object
126 for calling the @ref save() function. 126 for calling the @ref save() function.
127 127
128 @see save() 128 @see save()
129 */ 129 */
130 Ticket *requestSaveTicket( Resource *resource=0 ); 130 Ticket *requestSaveTicket( Resource *resource=0 );
131 131
132 /** 132 /**
133 Load address book from file. 133 Load address book from file.
134 */ 134 */
135 bool load(); 135 bool load();
136 136
137 /** 137 /**
138 Save address book. The address book is saved to the file, the Ticket 138 Save address book. The address book is saved to the file, the Ticket
139 object has been requested for by @ref requestSaveTicket(). 139 object has been requested for by @ref requestSaveTicket().
140 140
141 @param ticket a ticket object returned by @ref requestSaveTicket() 141 @param ticket a ticket object returned by @ref requestSaveTicket()
142 */ 142 */
143 bool save( Ticket *ticket ); 143 bool save( Ticket *ticket );
144 bool saveAB( ); 144 bool saveAB( );
145 145 void export2File( QString fileName );
146 void importFromFile( QString fileName );
146 /** 147 /**
147 Returns a iterator for first entry of address book. 148 Returns a iterator for first entry of address book.
148 */ 149 */
149 Iterator begin(); 150 Iterator begin();
150 151
151 /** 152 /**
152 Returns a const iterator for first entry of address book. 153 Returns a const iterator for first entry of address book.
153 */ 154 */
154 ConstIterator begin() const; 155 ConstIterator begin() const;
155 156
156 /** 157 /**
157 Returns a iterator for first entry of address book. 158 Returns a iterator for first entry of address book.
158 */ 159 */
159 Iterator end(); 160 Iterator end();
160 161
161 /** 162 /**
162 Returns a const iterator for first entry of address book. 163 Returns a const iterator for first entry of address book.
163 */ 164 */
164 ConstIterator end() const; 165 ConstIterator end() const;
165 166
166 /** 167 /**
167 Removes all entries from address book. 168 Removes all entries from address book.
168 */ 169 */
169 void clear(); 170 void clear();
170 171
171 /** 172 /**
172 Insert an Addressee object into address book. If an object with the same 173 Insert an Addressee object into address book. If an object with the same
173 unique id already exists in the address book it it replaced by the new 174 unique id already exists in the address book it it replaced by the new
174 one. If not the new object is appended to the address book. 175 one. If not the new object is appended to the address book.
175 */ 176 */
176 void insertAddressee( const Addressee &, bool setRev = true ); 177 void insertAddressee( const Addressee &, bool setRev = true, bool takeResource = false);
177 178
178 /** 179 /**
179 Removes entry from the address book. 180 Removes entry from the address book.
180 */ 181 */
181 void removeAddressee( const Addressee & ); 182 void removeAddressee( const Addressee & );
182 183
183 /** 184 /**
184 This is like @ref removeAddressee() just above, with the difference that 185 This is like @ref removeAddressee() just above, with the difference that
185 the first element is a iterator, returned by @ref begin(). 186 the first element is a iterator, returned by @ref begin().
186 */ 187 */
187 void removeAddressee( const Iterator & ); 188 void removeAddressee( const Iterator & );
188 189
189 /** 190 /**
190 Find the specified entry in address book. Returns end(), if the entry 191 Find the specified entry in address book. Returns end(), if the entry
191 couldn't be found. 192 couldn't be found.
192 */ 193 */
193 Iterator find( const Addressee & ); 194 Iterator find( const Addressee & );
194 195
195 /** 196 /**
196 Find the entry specified by an unique id. Returns an empty Addressee 197 Find the entry specified by an unique id. Returns an empty Addressee
197 object, if the address book does not contain an entry with this id. 198 object, if the address book does not contain an entry with this id.
198 */ 199 */
199 Addressee findByUid( const QString & ); 200 Addressee findByUid( const QString & );
200 201
201 202
202 /** 203 /**
203 Returns a list of all addressees in the address book. This list can 204 Returns a list of all addressees in the address book. This list can
204 be sorted with @ref KABC::AddresseeList for example. 205 be sorted with @ref KABC::AddresseeList for example.
205 */ 206 */
206 Addressee::List allAddressees(); 207 Addressee::List allAddressees();
207 208
208 /** 209 /**