addressbook.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <qfile.h>
00022 #include <qregexp.h>
00023 #include <qtimer.h>
00024
00025 #include <kapplication.h>
00026 #include <kdebug.h>
00027 #include <kglobal.h>
00028 #include <kinstance.h>
00029 #include <klocale.h>
00030 #include <kstandarddirs.h>
00031
00032 #include "errorhandler.h"
00033 #include "resource.h"
00034
00035 #include "addressbook.h"
00036 #include "addressbook.moc"
00037
00038 using namespace KABC;
00039
00040 struct AddressBook::AddressBookData
00041 {
00042 Addressee::List mAddressees;
00043 Addressee::List mRemovedAddressees;
00044 Field::List mAllFields;
00045 QPtrList<Resource> mResources;
00046 ErrorHandler *mErrorHandler;
00047 Resource *mStandardResource;
00048 };
00049
00050 struct AddressBook::Iterator::IteratorData
00051 {
00052 Addressee::List::Iterator mIt;
00053 };
00054
00055 struct AddressBook::ConstIterator::ConstIteratorData
00056 {
00057 Addressee::List::ConstIterator mIt;
00058 };
00059
00060 AddressBook::Iterator::Iterator()
00061 {
00062 d = new IteratorData;
00063 }
00064
00065 AddressBook::Iterator::Iterator( const AddressBook::Iterator &i )
00066 {
00067 d = new IteratorData;
00068 d->mIt = i.d->mIt;
00069 }
00070
00071 AddressBook::Iterator &AddressBook::Iterator::operator=( const AddressBook::Iterator &i )
00072 {
00073 d = new IteratorData;
00074 d->mIt = i.d->mIt;
00075 return *this;
00076 }
00077
00078 AddressBook::Iterator::~Iterator()
00079 {
00080 delete d;
00081 }
00082
00083 const Addressee &AddressBook::Iterator::operator*() const
00084 {
00085 return *(d->mIt);
00086 }
00087
00088 Addressee &AddressBook::Iterator::operator*()
00089 {
00090 return *(d->mIt);
00091 }
00092
00093 Addressee *AddressBook::Iterator::operator->()
00094 {
00095 return &(*(d->mIt));
00096 }
00097
00098 AddressBook::Iterator &AddressBook::Iterator::operator++()
00099 {
00100 (d->mIt)++;
00101 return *this;
00102 }
00103
00104 AddressBook::Iterator &AddressBook::Iterator::operator++(int)
00105 {
00106 (d->mIt)++;
00107 return *this;
00108 }
00109
00110 AddressBook::Iterator &AddressBook::Iterator::operator--()
00111 {
00112 (d->mIt)--;
00113 return *this;
00114 }
00115
00116 AddressBook::Iterator &AddressBook::Iterator::operator--(int)
00117 {
00118 (d->mIt)--;
00119 return *this;
00120 }
00121
00122 bool AddressBook::Iterator::operator==( const Iterator &it )
00123 {
00124 return ( d->mIt == it.d->mIt );
00125 }
00126
00127 bool AddressBook::Iterator::operator!=( const Iterator &it )
00128 {
00129 return ( d->mIt != it.d->mIt );
00130 }
00131
00132
00133 AddressBook::ConstIterator::ConstIterator()
00134 {
00135 d = new ConstIteratorData;
00136 }
00137
00138 AddressBook::ConstIterator::ConstIterator( const AddressBook::ConstIterator &i )
00139 {
00140 d = new ConstIteratorData;
00141 d->mIt = i.d->mIt;
00142 }
00143
00144 AddressBook::ConstIterator &AddressBook::ConstIterator::operator=( const AddressBook::ConstIterator &i )
00145 {
00146 d = new ConstIteratorData;
00147 d->mIt = i.d->mIt;
00148 return *this;
00149 }
00150
00151 AddressBook::ConstIterator::~ConstIterator()
00152 {
00153 delete d;
00154 }
00155
00156 const Addressee &AddressBook::ConstIterator::operator*() const
00157 {
00158 return *(d->mIt);
00159 }
00160
00161 const Addressee* AddressBook::ConstIterator::operator->() const
00162 {
00163 return &(*(d->mIt));
00164 }
00165
00166 AddressBook::ConstIterator &AddressBook::ConstIterator::operator++()
00167 {
00168 (d->mIt)++;
00169 return *this;
00170 }
00171
00172 AddressBook::ConstIterator &AddressBook::ConstIterator::operator++(int)
00173 {
00174 (d->mIt)++;
00175 return *this;
00176 }
00177
00178 AddressBook::ConstIterator &AddressBook::ConstIterator::operator--()
00179 {
00180 (d->mIt)--;
00181 return *this;
00182 }
00183
00184 AddressBook::ConstIterator &AddressBook::ConstIterator::operator--(int)
00185 {
00186 (d->mIt)--;
00187 return *this;
00188 }
00189
00190 bool AddressBook::ConstIterator::operator==( const ConstIterator &it )
00191 {
00192 return ( d->mIt == it.d->mIt );
00193 }
00194
00195 bool AddressBook::ConstIterator::operator!=( const ConstIterator &it )
00196 {
00197 return ( d->mIt != it.d->mIt );
00198 }
00199
00200
00201 AddressBook::AddressBook()
00202 {
00203 d = new AddressBookData;
00204 d->mResources.setAutoDelete( true );
00205 d->mErrorHandler = 0;
00206 d->mStandardResource = 0;
00207 }
00208
00209 AddressBook::~AddressBook()
00210 {
00211 d->mResources.clear();
00212 d->mStandardResource = 0;
00213 delete d->mErrorHandler;
00214 delete d;
00215 }
00216
00217 bool AddressBook::load()
00218 {
00219 kdDebug(5700) << "AddressBook::load()" << endl;
00220
00221 clear();
00222
00223 Resource *r;
00224 bool ok = true;
00225 for( r = d->mResources.first(); r; r = d->mResources.next() )
00226 if ( !r->load() ) {
00227 error( i18n("Unable to load resource '%1'").arg( r->name() ) );
00228 ok = false;
00229 }
00230
00231
00232 Addressee::List::Iterator it;
00233 for ( it = d->mAddressees.begin(); it != d->mAddressees.end(); ++it )
00234 (*it).setChanged( false );
00235
00236 return ok;
00237 }
00238
00239 bool AddressBook::save( Ticket *ticket )
00240 {
00241 kdDebug(5700) << "AddressBook::save()"<< endl;
00242
00243 if ( ticket->resource() ) {
00244 deleteRemovedAddressees();
00245 return ticket->resource()->save( ticket );
00246 }
00247
00248 return false;
00249 }
00250
00251 AddressBook::Iterator AddressBook::begin()
00252 {
00253 Iterator it = Iterator();
00254 it.d->mIt = d->mAddressees.begin();
00255 return it;
00256 }
00257
00258 AddressBook::ConstIterator AddressBook::begin() const
00259 {
00260 ConstIterator it = ConstIterator();
00261 it.d->mIt = d->mAddressees.begin();
00262 return it;
00263 }
00264
00265 AddressBook::Iterator AddressBook::end()
00266 {
00267 Iterator it = Iterator();
00268 it.d->mIt = d->mAddressees.end();
00269 return it;
00270 }
00271
00272 AddressBook::ConstIterator AddressBook::end() const
00273 {
00274 ConstIterator it = ConstIterator();
00275 it.d->mIt = d->mAddressees.end();
00276 return it;
00277 }
00278
00279 void AddressBook::clear()
00280 {
00281 d->mAddressees.clear();
00282 }
00283
00284 Ticket *AddressBook::requestSaveTicket( Resource *resource )
00285 {
00286 kdDebug(5700) << "AddressBook::requestSaveTicket()" << endl;
00287
00288 if ( !resource )
00289 resource = standardResource();
00290
00291 if ( d->mResources.find( resource ) < 0 ) {
00292 return 0;
00293 } else {
00294 if ( resource->readOnly() )
00295 return 0;
00296 else
00297 return resource->requestSaveTicket();
00298 }
00299 }
00300
00301 void AddressBook::insertAddressee( const Addressee &a )
00302 {
00303 Addressee::List::Iterator it;
00304 for ( it = d->mAddressees.begin(); it != d->mAddressees.end(); ++it ) {
00305 if ( a.uid() == (*it).uid() ) {
00306 bool changed = false;
00307 Addressee addr = a;
00308 if ( addr != (*it) )
00309 changed = true;
00310
00311 (*it) = a;
00312 if ( (*it).resource() == 0 )
00313 (*it).setResource( standardResource() );
00314
00315 if ( changed ) {
00316 (*it).setRevision( QDateTime::currentDateTime() );
00317 (*it).setChanged( true );
00318 }
00319 return;
00320 }
00321 }
00322 d->mAddressees.append( a );
00323 Addressee& addr = d->mAddressees.last();
00324 if ( addr.resource() == 0 )
00325 addr.setResource( standardResource() );
00326 addr.setChanged( true );
00327 }
00328
00329 void AddressBook::removeAddressee( const Addressee &a )
00330 {
00331 Iterator it;
00332 for ( it = begin(); it != end(); ++it ) {
00333 if ( a.uid() == (*it).uid() ) {
00334 removeAddressee( it );
00335 return;
00336 }
00337 }
00338 }
00339
00340 void AddressBook::removeAddressee( const Iterator &it )
00341 {
00342 d->mRemovedAddressees.append( (*it) );
00343 d->mAddressees.remove( it.d->mIt );
00344 }
00345
00346 AddressBook::Iterator AddressBook::find( const Addressee &a )
00347 {
00348 Iterator it;
00349 for ( it = begin(); it != end(); ++it ) {
00350 if ( a.uid() == (*it).uid() ) {
00351 return it;
00352 }
00353 }
00354 return end();
00355 }
00356
00357 Addressee AddressBook::findByUid( const QString &uid )
00358 {
00359 Iterator it;
00360 for ( it = begin(); it != end(); ++it ) {
00361 if ( uid == (*it).uid() ) {
00362 return *it;
00363 }
00364 }
00365 return Addressee();
00366 }
00367
00368 Addressee::List AddressBook::findByName( const QString &name )
00369 {
00370 Addressee::List results;
00371
00372 Iterator it;
00373 for ( it = begin(); it != end(); ++it ) {
00374 if ( name == (*it).name() ) {
00375 results.append( *it );
00376 }
00377 }
00378
00379 return results;
00380 }
00381
00382 Addressee::List AddressBook::findByEmail( const QString &email )
00383 {
00384 Addressee::List results;
00385 QStringList mailList;
00386
00387 Iterator it;
00388 for ( it = begin(); it != end(); ++it ) {
00389 mailList = (*it).emails();
00390 for ( QStringList::Iterator ite = mailList.begin(); ite != mailList.end(); ++ite ) {
00391 if ( email == (*ite) ) {
00392 results.append( *it );
00393 }
00394 }
00395 }
00396
00397 return results;
00398 }
00399
00400 Addressee::List AddressBook::findByCategory( const QString &category )
00401 {
00402 Addressee::List results;
00403
00404 Iterator it;
00405 for ( it = begin(); it != end(); ++it ) {
00406 if ( (*it).hasCategory( category) ) {
00407 results.append( *it );
00408 }
00409 }
00410
00411 return results;
00412 }
00413
00414 void AddressBook::dump() const
00415 {
00416 kdDebug(5700) << "AddressBook::dump() --- begin ---" << endl;
00417
00418 ConstIterator it;
00419 for( it = begin(); it != end(); ++it ) {
00420 (*it).dump();
00421 }
00422
00423 kdDebug(5700) << "AddressBook::dump() --- end ---" << endl;
00424 }
00425
00426 QString AddressBook::identifier()
00427 {
00428 QString identifier;
00429
00430 for ( uint i = 0; i < d->mResources.count(); ++i ) {
00431 Resource *resource = d->mResources.at( i );
00432 identifier += ( i == 0 ? "" : ":" ) + resource->identifier();
00433 }
00434
00435 return identifier;
00436 }
00437
00438 Field::List AddressBook::fields( int category )
00439 {
00440 if ( d->mAllFields.isEmpty() ) {
00441 d->mAllFields = Field::allFields();
00442 }
00443
00444 if ( category == Field::All ) return d->mAllFields;
00445
00446 Field::List result;
00447 Field::List::ConstIterator it;
00448 for( it = d->mAllFields.begin(); it != d->mAllFields.end(); ++it ) {
00449 if ( (*it)->category() & category ) result.append( *it );
00450 }
00451
00452 return result;
00453 }
00454
00455 bool AddressBook::addCustomField( const QString &label, int category,
00456 const QString &key, const QString &app )
00457 {
00458 if ( d->mAllFields.isEmpty() ) {
00459 d->mAllFields = Field::allFields();
00460 }
00461
00462 QString a = app.isNull() ? KGlobal::instance()->instanceName() : app;
00463 QString k = key.isNull() ? label : key;
00464
00465 Field *field = Field::createCustomField( label, category, k, a );
00466
00467 if ( !field ) return false;
00468
00469 d->mAllFields.append( field );
00470
00471 return true;
00472 }
00473
00474 QDataStream &KABC::operator<<( QDataStream &s, const AddressBook &ab )
00475 {
00476 if (!ab.d) return s;
00477
00478 return s << ab.d->mAddressees;
00479 }
00480
00481 QDataStream &KABC::operator>>( QDataStream &s, AddressBook &ab )
00482 {
00483 if (!ab.d) return s;
00484
00485 s >> ab.d->mAddressees;
00486
00487 return s;
00488 }
00489
00490 bool AddressBook::addResource( Resource *resource )
00491 {
00492 if ( !resource->open() ) {
00493 kdDebug(5700) << "AddressBook::addResource(): can't add resource" << endl;
00494 return false;
00495 }
00496
00497 d->mResources.append( resource );
00498 return true;
00499 }
00500
00501 bool AddressBook::removeResource( Resource *resource )
00502 {
00503 if ( resource == standardResource() )
00504 setStandardResource( 0 );
00505
00506 return d->mResources.remove( resource );
00507 }
00508
00509 QPtrList<Resource> AddressBook::resources()
00510 {
00511 return d->mResources;
00512 }
00513
00514 void AddressBook::setErrorHandler( ErrorHandler *handler )
00515 {
00516 delete d->mErrorHandler;
00517 d->mErrorHandler = handler;
00518 }
00519
00520 void AddressBook::error( const QString& msg )
00521 {
00522 if ( !d->mErrorHandler )
00523 d->mErrorHandler = new ConsoleErrorHandler;
00524
00525 if ( d->mErrorHandler )
00526 d->mErrorHandler->error( msg );
00527 else
00528 kdError(5700) << "no error handler defined" << endl;
00529 }
00530
00531 void AddressBook::deleteRemovedAddressees()
00532 {
00533 Addressee::List::Iterator it;
00534 for ( it = d->mRemovedAddressees.begin(); it != d->mRemovedAddressees.end(); ++it ) {
00535 Resource *resource = (*it).resource();
00536 if ( resource && !resource->readOnly() )
00537 resource->removeAddressee( *it );
00538 }
00539
00540 d->mRemovedAddressees.clear();
00541 }
00542
00543 void AddressBook::setStandardResource( Resource *resource )
00544 {
00545 d->mStandardResource = resource;
00546 }
00547
00548 Resource *AddressBook::standardResource()
00549 {
00550 return d->mStandardResource;
00551 }
00552
00553 void AddressBook::cleanUp()
00554 {
00555 for ( uint i = 0; i < d->mResources.count(); ++i ) {
00556 Resource *resource = d->mResources.at( i );
00557 if ( !resource->readOnly() )
00558 resource->cleanUp();
00559 }
00560 }
This file is part of the documentation for kdelibs Version 3.1.0.