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 <kdebug.h>
00022 #include <klocale.h>
00023 
00024 #include "resource.h"
00025 
00026 using namespace KABC;
00027 
00028 Ticket::Ticket( Resource *resource )
00029   : mResource( resource )
00030 {
00031 }
00032 
00033 Ticket::~Ticket()
00034 {
00035 /* FIXME: avoid cycle deletion
00036   if ( mResource )
00037     mResource->releaseSaveTicket( this );
00038 */
00039 }
00040 
00041 Resource *Ticket::resource()
00042 {
00043   return mResource;
00044 }
00045 
00046 struct Resource::Iterator::IteratorData
00047 {
00048   Addressee::Map::Iterator mIt;
00049 };
00050 
00051 struct Resource::ConstIterator::ConstIteratorData
00052 {
00053   Addressee::Map::ConstIterator mIt;
00054 };
00055 
00056 Resource::Iterator::Iterator()
00057 {
00058   d = new IteratorData;
00059 }
00060 
00061 Resource::Iterator::Iterator( const Resource::Iterator &i )
00062 {
00063   d = new IteratorData;
00064   d->mIt = i.d->mIt;
00065 }
00066 
00067 Resource::Iterator &Resource::Iterator::operator=( const Resource::Iterator &i )
00068 {
00069   if ( this == &i )
00070     return *this;
00071   delete d;
00072 
00073   d = new IteratorData;
00074   d->mIt = i.d->mIt;
00075   return *this;
00076 }
00077 
00078 Resource::Iterator::~Iterator()
00079 {
00080   delete d;
00081 }
00082 
00083 const Addressee &Resource::Iterator::operator*() const
00084 {
00085   return d->mIt.data();
00086 }
00087 
00088 Addressee &Resource::Iterator::operator*()
00089 {
00090   return d->mIt.data();
00091 }
00092 
00093 Resource::Iterator &Resource::Iterator::operator++()
00094 {
00095   (d->mIt)++;
00096   return *this;
00097 }
00098 
00099 Resource::Iterator &Resource::Iterator::operator++( int )
00100 {
00101   (d->mIt)++;
00102   return *this;
00103 }
00104 
00105 Resource::Iterator &Resource::Iterator::operator--()
00106 {
00107   (d->mIt)--;
00108   return *this;
00109 }
00110 
00111 Resource::Iterator &Resource::Iterator::operator--( int )
00112 {
00113   (d->mIt)--;
00114   return *this;
00115 }
00116 
00117 bool Resource::Iterator::operator==( const Iterator &it )
00118 {
00119   return ( d->mIt == it.d->mIt );
00120 }
00121 
00122 bool Resource::Iterator::operator!=( const Iterator &it )
00123 {
00124   return ( d->mIt != it.d->mIt );
00125 }
00126 
00127 Resource::ConstIterator::ConstIterator()
00128 {
00129   d = new ConstIteratorData;
00130 }
00131 
00132 Resource::ConstIterator::ConstIterator( const Resource::ConstIterator &i )
00133 {
00134   d = new ConstIteratorData;
00135   d->mIt = i.d->mIt;
00136 }
00137 
00138 Resource::ConstIterator::ConstIterator( const Resource::Iterator &i )
00139 {
00140   d = new ConstIteratorData;
00141   d->mIt = i.d->mIt;
00142 }
00143 
00144 Resource::ConstIterator &Resource::ConstIterator::operator=( const Resource::ConstIterator &i )
00145 {
00146   if ( this  == &i )
00147     return *this;
00148   delete d;
00149 
00150   d = new ConstIteratorData;
00151   d->mIt = i.d->mIt;
00152   return *this;
00153 }
00154 
00155 Resource::ConstIterator::~ConstIterator()
00156 {
00157   delete d;
00158 }
00159 
00160 const Addressee &Resource::ConstIterator::operator*() const
00161 {
00162   return *(d->mIt);
00163 }
00164 
00165 Resource::ConstIterator &Resource::ConstIterator::operator++()
00166 {
00167   (d->mIt)++;
00168   return *this;
00169 }
00170 
00171 Resource::ConstIterator &Resource::ConstIterator::operator++( int )
00172 {
00173   (d->mIt)++;
00174   return *this;
00175 }
00176 
00177 Resource::ConstIterator &Resource::ConstIterator::operator--()
00178 {
00179   (d->mIt)--;
00180   return *this;
00181 }
00182 
00183 Resource::ConstIterator &Resource::ConstIterator::operator--( int )
00184 {
00185   (d->mIt)--;
00186   return *this;
00187 }
00188 
00189 bool Resource::ConstIterator::operator==( const ConstIterator &it )
00190 {
00191   return ( d->mIt == it.d->mIt );
00192 }
00193 
00194 bool Resource::ConstIterator::operator!=( const ConstIterator &it )
00195 {
00196   return ( d->mIt != it.d->mIt );
00197 }
00198 
00199 
00200 Resource::Resource( const KConfig *config )
00201   : KRES::Resource( config ), mAddressBook( 0 )
00202 {
00203 }
00204 
00205 Resource::~Resource()
00206 {
00207 }
00208 
00209 Resource::Iterator Resource::begin()
00210 {
00211   Iterator it;
00212   it.d->mIt = mAddrMap.begin();
00213 
00214   return it;
00215 }
00216 
00217 Resource::ConstIterator Resource::begin() const
00218 {
00219   ConstIterator it;
00220   it.d->mIt = mAddrMap.constBegin();
00221   return it;
00222 }
00223 
00224 Resource::Iterator Resource::end()
00225 {
00226   Iterator it;
00227   it.d->mIt = mAddrMap.end();
00228 
00229   return it;
00230 }
00231 
00232 Resource::ConstIterator Resource::end() const
00233 {
00234   ConstIterator it;
00235   it.d->mIt = mAddrMap.constEnd();
00236   return it;
00237 }
00238 
00239 void Resource::writeConfig( KConfig *config )
00240 {
00241   KRES::Resource::writeConfig( config );
00242 }
00243 
00244 void Resource::setAddressBook( AddressBook *ab )
00245 {
00246   mAddressBook = ab;
00247 }
00248 
00249 AddressBook *Resource::addressBook()
00250 {
00251   return mAddressBook;
00252 }
00253 
00254 Ticket *Resource::createTicket( Resource *resource )
00255 {
00256   return new Ticket( resource );
00257 }
00258 
00259 void Resource::insertAddressee( const Addressee &addr )
00260 {
00261   mAddrMap.insert( addr.uid(), addr );
00262 }
00263 
00264 void Resource::removeAddressee( const Addressee &addr )
00265 {
00266   mAddrMap.erase( addr.uid() );
00267 }
00268 
00269 Addressee Resource::findByUid( const QString &uid )
00270 {
00271   Addressee::Map::ConstIterator it = mAddrMap.find( uid );
00272 
00273   if ( it != mAddrMap.end() )
00274     return it.data();
00275 
00276   return Addressee();
00277 }
00278 
00279 Addressee::List Resource::findByName( const QString &name )
00280 {
00281   Addressee::List results;
00282 
00283   ConstIterator it;
00284   for ( it = begin(); it != end(); ++it ) {
00285     if ( name == (*it).name() )
00286       results.append( *it );
00287   }
00288 
00289   return results;
00290 }
00291 
00292 Addressee::List Resource::findByEmail( const QString &email )
00293 {
00294   Addressee::List results;
00295   const QString lowerEmail = email.lower();
00296 
00297   ConstIterator it;
00298   for ( it = begin(); it != end(); ++it ) {
00299     const QStringList mailList = (*it).emails();
00300     for ( QStringList::ConstIterator ite = mailList.begin(); ite != mailList.end(); ++ite ) {
00301       if ( lowerEmail == (*ite).lower() )
00302         results.append( *it );
00303     }
00304   }
00305 
00306   return results;
00307 }
00308 
00309 Addressee::List Resource::findByCategory( const QString &category )
00310 {
00311   Addressee::List results;
00312 
00313   ConstIterator it;
00314   for ( it = begin(); it != end(); ++it ) {
00315     if ( (*it).hasCategory( category) ) {
00316       results.append( *it );
00317     }
00318   }
00319 
00320   return results;
00321 }
00322 
00323 void Resource::clear()
00324 {
00325   mAddrMap.clear();
00326 }
00327 
00328 bool Resource::asyncLoad()
00329 {
00330   bool ok = load();
00331   if ( !ok )
00332     emit loadingError( this, i18n( "Loading resource '%1' failed!" )
00333                        .arg( resourceName() ) );
00334   else
00335     emit loadingFinished( this );
00336 
00337   return ok;
00338 }
00339 
00340 bool Resource::asyncSave( Ticket *ticket ) {
00341   bool ok = save( ticket );
00342   if ( !ok )
00343     emit savingError( this, i18n( "Saving resource '%1' failed!" )
00344                       .arg( resourceName() ) );
00345   else
00346     emit savingFinished( this );
00347 
00348   return ok;
00349 }
00350 
00351 #include "resource.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys