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 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 clear(); 00229 delete d; 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.constFind( uid ); 00295 00296 if ( it != mAddrMap.constEnd() ) { 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 // take a copy of mDistListMap, then clear it and finally qDeleteAll 00354 // the copy to avoid problems with removeDistributionList() called by 00355 // ~DistributionList(). 00356 DistributionListMap tempDistListMap( mDistListMap ); 00357 mDistListMap.clear(); 00358 qDeleteAll( tempDistListMap ); 00359 } 00360 00361 void Resource::insertDistributionList( DistributionList *list ) 00362 { 00363 Q_ASSERT( list ); 00364 00365 mDistListMap.insert( list->identifier(), list ); 00366 } 00367 00368 void Resource::removeDistributionList( DistributionList *list ) 00369 { 00370 Q_ASSERT( list ); 00371 00372 DistributionListMap::iterator it = mDistListMap.find( list->identifier() ); 00373 if ( it != mDistListMap.end() ) { 00374 if ( it.value() == list ) { 00375 mDistListMap.erase( it ); 00376 } 00377 } 00378 } 00379 00380 DistributionList *Resource::findDistributionListByIdentifier( const QString &identifier ) 00381 { 00382 return mDistListMap.value( identifier ); 00383 } 00384 00385 DistributionList *Resource::findDistributionListByName( const QString &name, 00386 Qt::CaseSensitivity caseSensitivity ) 00387 { 00388 QString searchName = name; 00389 if ( caseSensitivity == Qt::CaseInsensitive ) { 00390 searchName = name.toLower(); 00391 } 00392 00393 DistributionListMap::const_iterator it = mDistListMap.constBegin(); 00394 DistributionListMap::const_iterator endIt = mDistListMap.constEnd(); 00395 for ( ; it != endIt; ++it ) { 00396 if ( caseSensitivity == Qt::CaseSensitive ) { 00397 if ( searchName == it.value()->name() ) { 00398 return it.value(); 00399 } 00400 } else { 00401 if ( searchName == it.value()->name().toLower() ) { 00402 return it.value(); 00403 } 00404 } 00405 } 00406 00407 return 0; 00408 } 00409 00410 QList<DistributionList*> Resource::allDistributionLists() 00411 { 00412 return mDistListMap.values(); 00413 } 00414 00415 QStringList Resource::allDistributionListNames() const 00416 { 00417 QStringList results; 00418 00419 DistributionListMap::const_iterator it = mDistListMap.constBegin(); 00420 DistributionListMap::const_iterator endIt = mDistListMap.constEnd(); 00421 for ( ; it != endIt; ++it ) { 00422 results += it.value()->name(); 00423 } 00424 00425 return results; 00426 } 00427 00428 bool Resource::asyncLoad() 00429 { 00430 bool ok = load(); 00431 if ( !ok ) { 00432 emit loadingError( this, i18n( "Loading resource '%1' failed.", resourceName() ) ); 00433 } else { 00434 emit loadingFinished( this ); 00435 } 00436 00437 return ok; 00438 } 00439 00440 bool Resource::asyncSave( Ticket *ticket ) 00441 { 00442 bool ok = save( ticket ); 00443 if ( !ok ) { 00444 emit savingError( this, i18n( "Saving resource '%1' failed.", resourceName() ) ); 00445 } else { 00446 emit savingFinished( this ); 00447 } 00448 00449 return ok; 00450 } 00451 00452 #include "resource.moc"