kabc
resource.cpp
00001 /* 00002 This file is part of libkabc. 00003 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to 00017 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 Boston, MA 02110-1301, USA. 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 #ifndef QT_STRICT_ITERATORS 00147 Resource::ConstIterator::ConstIterator( const Resource::Iterator &other ) 00148 : d( new Private ) 00149 { 00150 d->mIt = other.d->mIt; 00151 } 00152 #endif 00153 00154 Resource::ConstIterator &Resource::ConstIterator::operator=( const Resource::ConstIterator &other ) 00155 { 00156 if ( this != &other ) { 00157 d->mIt = other.d->mIt; 00158 } 00159 00160 return *this; 00161 } 00162 00163 Resource::ConstIterator::~ConstIterator() 00164 { 00165 delete d; 00166 } 00167 00168 const Addressee &Resource::ConstIterator::operator*() const 00169 { 00170 return *(d->mIt); 00171 } 00172 00173 Resource::ConstIterator &Resource::ConstIterator::operator++() 00174 { 00175 (d->mIt)++; 00176 return *this; 00177 } 00178 00179 Resource::ConstIterator &Resource::ConstIterator::operator++( int ) 00180 { 00181 (d->mIt)++; 00182 return *this; 00183 } 00184 00185 Resource::ConstIterator &Resource::ConstIterator::operator--() 00186 { 00187 --(d->mIt); 00188 return *this; 00189 } 00190 00191 Resource::ConstIterator &Resource::ConstIterator::operator--( int ) 00192 { 00193 --(d->mIt); 00194 return *this; 00195 } 00196 00197 bool Resource::ConstIterator::operator==( const ConstIterator &it ) const 00198 { 00199 return d->mIt == it.d->mIt; 00200 } 00201 00202 bool Resource::ConstIterator::operator!=( const ConstIterator &it ) const 00203 { 00204 return d->mIt != it.d->mIt; 00205 } 00206 00207 class Resource::Private 00208 { 00209 public: 00210 Private() 00211 : mAddressBook( 0 ) 00212 { 00213 } 00214 00215 AddressBook *mAddressBook; 00216 }; 00217 00218 Resource::Resource() 00219 : KRES::Resource(), d( new Private ) 00220 { 00221 } 00222 00223 Resource::Resource( const KConfigGroup &group ) 00224 : KRES::Resource( group ), d( new Private ) 00225 { 00226 } 00227 00228 Resource::~Resource() 00229 { 00230 clear(); 00231 delete d; 00232 } 00233 00234 Resource::Iterator Resource::begin() 00235 { 00236 Iterator it; 00237 it.d->mIt = mAddrMap.begin(); 00238 00239 return it; 00240 } 00241 00242 Resource::ConstIterator Resource::begin() const 00243 { 00244 ConstIterator it; 00245 it.d->mIt = mAddrMap.constBegin(); 00246 return it; 00247 } 00248 00249 Resource::Iterator Resource::end() 00250 { 00251 Iterator it; 00252 it.d->mIt = mAddrMap.end(); 00253 00254 return it; 00255 } 00256 00257 Resource::ConstIterator Resource::end() const 00258 { 00259 ConstIterator it; 00260 it.d->mIt = mAddrMap.constEnd(); 00261 return it; 00262 } 00263 00264 void Resource::writeConfig( KConfigGroup &group ) 00265 { 00266 KRES::Resource::writeConfig( group ); 00267 } 00268 00269 void Resource::setAddressBook( AddressBook *ab ) 00270 { 00271 d->mAddressBook = ab; 00272 } 00273 00274 AddressBook *Resource::addressBook() 00275 { 00276 return d->mAddressBook; 00277 } 00278 00279 Ticket *Resource::createTicket( Resource *resource ) 00280 { 00281 return new Ticket( resource ); 00282 } 00283 00284 void Resource::insertAddressee( const Addressee &addr ) 00285 { 00286 mAddrMap.insert( addr.uid(), addr ); 00287 } 00288 00289 void Resource::removeAddressee( const Addressee &addr ) 00290 { 00291 mAddrMap.remove( addr.uid() ); 00292 } 00293 00294 Addressee Resource::findByUid( const QString &uid ) 00295 { 00296 Addressee::Map::ConstIterator it = mAddrMap.constFind( uid ); 00297 00298 if ( it != mAddrMap.constEnd() ) { 00299 return it.value(); 00300 } 00301 00302 return Addressee(); 00303 } 00304 00305 Addressee::List Resource::findByName( const QString &name ) // TODO: const 00306 { 00307 Addressee::List results; 00308 00309 ConstIterator it; 00310 for ( it = constBegin(); it != constEnd(); ++it ) { 00311 if ( name == (*it).name() ) { 00312 results.append( *it ); 00313 } 00314 } 00315 00316 return results; 00317 } 00318 00319 Addressee::List Resource::findByEmail( const QString &email ) // TODO: const 00320 { 00321 Addressee::List results; 00322 const QString lowerEmail = email.toLower(); 00323 00324 ConstIterator it; 00325 for ( it = constBegin(); it != constEnd(); ++it ) { 00326 const QStringList mailList = (*it).emails(); 00327 for ( QStringList::ConstIterator ite = mailList.begin(); ite != mailList.end(); ++ite ) { 00328 if ( lowerEmail == (*ite).toLower() ) { 00329 results.append( *it ); 00330 } 00331 } 00332 } 00333 00334 return results; 00335 } 00336 00337 Addressee::List Resource::findByCategory( const QString &category ) // TODO: const 00338 { 00339 Addressee::List results; 00340 00341 ConstIterator it; 00342 for ( it = constBegin(); it != constEnd(); ++it ) { 00343 if ( (*it).hasCategory( category ) ) { 00344 results.append( *it ); 00345 } 00346 } 00347 00348 return results; 00349 } 00350 00351 void Resource::clear() 00352 { 00353 mAddrMap.clear(); 00354 00355 // take a copy of mDistListMap, then clear it and finally qDeleteAll 00356 // the copy to avoid problems with removeDistributionList() called by 00357 // ~DistributionList(). 00358 DistributionListMap tempDistListMap( mDistListMap ); 00359 mDistListMap.clear(); 00360 qDeleteAll( tempDistListMap ); 00361 } 00362 00363 void Resource::insertDistributionList( DistributionList *list ) 00364 { 00365 Q_ASSERT( list ); 00366 00367 mDistListMap.insert( list->identifier(), list ); 00368 } 00369 00370 void Resource::removeDistributionList( DistributionList *list ) 00371 { 00372 Q_ASSERT( list ); 00373 00374 DistributionListMap::iterator it = mDistListMap.find( list->identifier() ); 00375 if ( it != mDistListMap.end() ) { 00376 if ( it.value() == list ) { 00377 mDistListMap.erase( it ); 00378 } 00379 } 00380 } 00381 00382 DistributionList *Resource::findDistributionListByIdentifier( const QString &identifier ) 00383 { 00384 return mDistListMap.value( identifier ); 00385 } 00386 00387 DistributionList *Resource::findDistributionListByName( const QString &name, 00388 Qt::CaseSensitivity caseSensitivity ) 00389 { 00390 QString searchName = name; 00391 if ( caseSensitivity == Qt::CaseInsensitive ) { 00392 searchName = name.toLower(); 00393 } 00394 00395 DistributionListMap::const_iterator it = mDistListMap.constBegin(); 00396 DistributionListMap::const_iterator endIt = mDistListMap.constEnd(); 00397 for ( ; it != endIt; ++it ) { 00398 if ( caseSensitivity == Qt::CaseSensitive ) { 00399 if ( searchName == it.value()->name() ) { 00400 return it.value(); 00401 } 00402 } else { 00403 if ( searchName == it.value()->name().toLower() ) { 00404 return it.value(); 00405 } 00406 } 00407 } 00408 00409 return 0; 00410 } 00411 00412 QList<DistributionList*> Resource::allDistributionLists() 00413 { 00414 return mDistListMap.values(); 00415 } 00416 00417 QStringList Resource::allDistributionListNames() const 00418 { 00419 QStringList results; 00420 00421 DistributionListMap::const_iterator it = mDistListMap.constBegin(); 00422 DistributionListMap::const_iterator endIt = mDistListMap.constEnd(); 00423 for ( ; it != endIt; ++it ) { 00424 results += it.value()->name(); 00425 } 00426 00427 return results; 00428 } 00429 00430 bool Resource::asyncLoad() 00431 { 00432 bool ok = load(); 00433 if ( !ok ) { 00434 emit loadingError( this, i18n( "Loading resource '%1' failed.", resourceName() ) ); 00435 } else { 00436 emit loadingFinished( this ); 00437 } 00438 00439 return ok; 00440 } 00441 00442 bool Resource::asyncSave( Ticket *ticket ) 00443 { 00444 bool ok = save( ticket ); 00445 if ( !ok ) { 00446 emit savingError( this, i18n( "Saving resource '%1' failed.", resourceName() ) ); 00447 } else { 00448 emit savingFinished( this ); 00449 } 00450 00451 return ok; 00452 } 00453 00454 #include "resource.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu Aug 2 2012 15:26:07 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu Aug 2 2012 15:26:07 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.