kabc
resource.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "resource.h"
00022
00023 #include <kdebug.h>
00024 #include <klocale.h>
00025
00026 using namespace KABC;
00027
00028 class Ticket::Private
00029 {
00030 public:
00031 Private( Resource *resource )
00032 : mResource( resource )
00033 {
00034 }
00035
00036 Resource *mResource;
00037 };
00038
00039 Ticket::Ticket( Resource *resource )
00040 : d( new Private( resource ) )
00041 {
00042 }
00043
00044 Ticket::~Ticket()
00045 {
00046 delete d;
00047 }
00048
00049 Resource *Ticket::resource()
00050 {
00051 return d->mResource;
00052 }
00053
00054 class Resource::Iterator::Private
00055 {
00056 public:
00057 Addressee::Map::Iterator mIt;
00058 };
00059
00060 class Resource::ConstIterator::Private
00061 {
00062 public:
00063 Addressee::Map::ConstIterator mIt;
00064 };
00065
00066 Resource::Iterator::Iterator()
00067 : d( new Private )
00068 {
00069 }
00070
00071 Resource::Iterator::Iterator( const Resource::Iterator &other )
00072 : d( new Private )
00073 {
00074 d->mIt = other.d->mIt;
00075 }
00076
00077 Resource::Iterator &Resource::Iterator::operator=( const Resource::Iterator &other )
00078 {
00079 if ( this != &other ) {
00080 d->mIt = other.d->mIt;
00081 }
00082
00083 return *this;
00084 }
00085
00086 Resource::Iterator::~Iterator()
00087 {
00088 delete d;
00089 }
00090
00091 const Addressee &Resource::Iterator::operator*() const
00092 {
00093 return d->mIt.value();
00094 }
00095
00096 Addressee &Resource::Iterator::operator*()
00097 {
00098 return d->mIt.value();
00099 }
00100
00101 Resource::Iterator &Resource::Iterator::operator++()
00102 {
00103 (d->mIt)++;
00104 return *this;
00105 }
00106
00107 Resource::Iterator &Resource::Iterator::operator++( int )
00108 {
00109 (d->mIt)++;
00110 return *this;
00111 }
00112
00113 Resource::Iterator &Resource::Iterator::operator--()
00114 {
00115 (d->mIt)--;
00116 return *this;
00117 }
00118
00119 Resource::Iterator &Resource::Iterator::operator--( int )
00120 {
00121 (d->mIt)--;
00122 return *this;
00123 }
00124
00125 bool Resource::Iterator::operator==( const Iterator &it ) const
00126 {
00127 return d->mIt == it.d->mIt;
00128 }
00129
00130 bool Resource::Iterator::operator!=( const Iterator &it ) const
00131 {
00132 return d->mIt != it.d->mIt;
00133 }
00134
00135 Resource::ConstIterator::ConstIterator()
00136 : d( new Private )
00137 {
00138 }
00139
00140 Resource::ConstIterator::ConstIterator( const Resource::ConstIterator &other )
00141 : d( new Private )
00142 {
00143 d->mIt = other.d->mIt;
00144 }
00145
00146 Resource::ConstIterator::ConstIterator( const Resource::Iterator &other )
00147 : d( new Private )
00148 {
00149 d->mIt = other.d->mIt;
00150 }
00151
00152 Resource::ConstIterator &Resource::ConstIterator::operator=( const Resource::ConstIterator &other )
00153 {
00154 if ( this != &other ) {
00155 d->mIt = other.d->mIt;
00156 }
00157
00158 return *this;
00159 }
00160
00161 Resource::ConstIterator::~ConstIterator()
00162 {
00163 delete d;
00164 }
00165
00166 const Addressee &Resource::ConstIterator::operator*() const
00167 {
00168 return *(d->mIt);
00169 }
00170
00171 Resource::ConstIterator &Resource::ConstIterator::operator++()
00172 {
00173 (d->mIt)++;
00174 return *this;
00175 }
00176
00177 Resource::ConstIterator &Resource::ConstIterator::operator++( int )
00178 {
00179 (d->mIt)++;
00180 return *this;
00181 }
00182
00183 Resource::ConstIterator &Resource::ConstIterator::operator--()
00184 {
00185 --(d->mIt);
00186 return *this;
00187 }
00188
00189 Resource::ConstIterator &Resource::ConstIterator::operator--( int )
00190 {
00191 --(d->mIt);
00192 return *this;
00193 }
00194
00195 bool Resource::ConstIterator::operator==( const ConstIterator &it ) const
00196 {
00197 return d->mIt == it.d->mIt;
00198 }
00199
00200 bool Resource::ConstIterator::operator!=( const ConstIterator &it ) const
00201 {
00202 return d->mIt != it.d->mIt;
00203 }
00204
00205 class Resource::Private
00206 {
00207 public:
00208 Private()
00209 : mAddressBook( 0 )
00210 {
00211 }
00212
00213 AddressBook *mAddressBook;
00214 };
00215
00216 Resource::Resource()
00217 : KRES::Resource(), d( new Private )
00218 {
00219 }
00220
00221 Resource::Resource( const KConfigGroup &group )
00222 : KRES::Resource( group ), d( new Private )
00223 {
00224 }
00225
00226 Resource::~Resource()
00227 {
00228 delete d;
00229 qDeleteAll( mDistListMap );
00230 }
00231
00232 Resource::Iterator Resource::begin()
00233 {
00234 Iterator it;
00235 it.d->mIt = mAddrMap.begin();
00236
00237 return it;
00238 }
00239
00240 Resource::ConstIterator Resource::begin() const
00241 {
00242 ConstIterator it;
00243 it.d->mIt = mAddrMap.constBegin();
00244 return it;
00245 }
00246
00247 Resource::Iterator Resource::end()
00248 {
00249 Iterator it;
00250 it.d->mIt = mAddrMap.end();
00251
00252 return it;
00253 }
00254
00255 Resource::ConstIterator Resource::end() const
00256 {
00257 ConstIterator it;
00258 it.d->mIt = mAddrMap.constEnd();
00259 return it;
00260 }
00261
00262 void Resource::writeConfig( KConfigGroup &group )
00263 {
00264 KRES::Resource::writeConfig( group );
00265 }
00266
00267 void Resource::setAddressBook( AddressBook *ab )
00268 {
00269 d->mAddressBook = ab;
00270 }
00271
00272 AddressBook *Resource::addressBook()
00273 {
00274 return d->mAddressBook;
00275 }
00276
00277 Ticket *Resource::createTicket( Resource *resource )
00278 {
00279 return new Ticket( resource );
00280 }
00281
00282 void Resource::insertAddressee( const Addressee &addr )
00283 {
00284 mAddrMap.insert( addr.uid(), addr );
00285 }
00286
00287 void Resource::removeAddressee( const Addressee &addr )
00288 {
00289 mAddrMap.remove( addr.uid() );
00290 }
00291
00292 Addressee Resource::findByUid( const QString &uid )
00293 {
00294 Addressee::Map::ConstIterator it = mAddrMap.find( uid );
00295
00296 if ( it != mAddrMap.end() ) {
00297 return it.value();
00298 }
00299
00300 return Addressee();
00301 }
00302
00303 Addressee::List Resource::findByName( const QString &name )
00304 {
00305 Addressee::List results;
00306
00307 ConstIterator it;
00308 for ( it = begin(); it != end(); ++it ) {
00309 if ( name == (*it).name() ) {
00310 results.append( *it );
00311 }
00312 }
00313
00314 return results;
00315 }
00316
00317 Addressee::List Resource::findByEmail( const QString &email )
00318 {
00319 Addressee::List results;
00320 const QString lowerEmail = email.toLower();
00321
00322 ConstIterator it;
00323 for ( it = begin(); it != end(); ++it ) {
00324 const QStringList mailList = (*it).emails();
00325 for ( QStringList::ConstIterator ite = mailList.begin(); ite != mailList.end(); ++ite ) {
00326 if ( lowerEmail == (*ite).toLower() ) {
00327 results.append( *it );
00328 }
00329 }
00330 }
00331
00332 return results;
00333 }
00334
00335 Addressee::List Resource::findByCategory( const QString &category )
00336 {
00337 Addressee::List results;
00338
00339 ConstIterator it;
00340 for ( it = begin(); it != end(); ++it ) {
00341 if ( (*it).hasCategory( category ) ) {
00342 results.append( *it );
00343 }
00344 }
00345
00346 return results;
00347 }
00348
00349 void Resource::clear()
00350 {
00351 mAddrMap.clear();
00352
00353 qDeleteAll( mDistListMap );
00354 }
00355
00356 void Resource::insertDistributionList( DistributionList *list )
00357 {
00358 Q_ASSERT( list );
00359
00360 mDistListMap.insert( list->identifier(), list );
00361 }
00362
00363 void Resource::removeDistributionList( DistributionList *list )
00364 {
00365 Q_ASSERT( list );
00366
00367 DistributionListMap::iterator it = mDistListMap.find( list->identifier() );
00368 if ( it != mDistListMap.end() ) {
00369 if ( it.value() == list ) {
00370 mDistListMap.erase( it );
00371 }
00372 }
00373 }
00374
00375 DistributionList* Resource::findDistributionListByIdentifier( const QString& identifier )
00376 {
00377 DistributionListMap::const_iterator it = mDistListMap.find( identifier );
00378 if ( it != mDistListMap.end() )
00379 return it.value();
00380
00381 return 0;
00382 }
00383
00384 DistributionList* Resource::findDistributionListByName( const QString &name, Qt::CaseSensitivity caseSensitivity )
00385 {
00386 QString searchName = name;
00387 if ( caseSensitivity == Qt::CaseInsensitive )
00388 searchName = name.toLower();
00389
00390 DistributionListMap::const_iterator it = mDistListMap.begin();
00391 DistributionListMap::const_iterator endIt = mDistListMap.end();
00392 for ( ; it != endIt; ++it ) {
00393 if ( caseSensitivity == Qt::CaseSensitive ) {
00394 if ( searchName == it.value()->name() )
00395 return it.value();
00396 } else {
00397 if ( searchName == it.value()->name().toLower() )
00398 return it.value();
00399 }
00400 }
00401
00402 return 0;
00403 }
00404
00405 QList<DistributionList*> Resource::allDistributionLists()
00406 {
00407 QList<DistributionList*> results;
00408
00409 DistributionListMap::const_iterator it = mDistListMap.begin();
00410 DistributionListMap::const_iterator endIt = mDistListMap.end();
00411 for ( ; it != endIt; ++it ) {
00412 results += it.value();
00413 }
00414
00415 return results;
00416 }
00417
00418 QStringList Resource::allDistributionListNames() const
00419 {
00420 QStringList results;
00421
00422 DistributionListMap::const_iterator it = mDistListMap.begin();
00423 DistributionListMap::const_iterator endIt = mDistListMap.end();
00424 for ( ; it != endIt; ++it ) {
00425 results += it.value()->name();
00426 }
00427
00428 return results;
00429 }
00430
00431 bool Resource::asyncLoad()
00432 {
00433 bool ok = load();
00434 if ( !ok ) {
00435 emit loadingError( this, i18n( "Loading resource '%1' failed.", resourceName() ) );
00436 } else {
00437 emit loadingFinished( this );
00438 }
00439
00440 return ok;
00441 }
00442
00443 bool Resource::asyncSave( Ticket *ticket )
00444 {
00445 bool ok = save( ticket );
00446 if ( !ok ) {
00447 emit savingError( this, i18n( "Saving resource '%1' failed.", resourceName() ) );
00448 } else {
00449 emit savingFinished( this );
00450 }
00451
00452 return ok;
00453 }
00454
00455 #include "resource.moc"