kresources Library API Documentation

manager.h

00001 /*
00002     This file is part of libkresources.
00003 
00004     Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>
00005     Copyright (c) 2002 Jan-Pascal van Best <janpascal@vanbest.org>
00006     Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>
00007 
00008     This library is free software; you can redistribute it and/or
00009     modify it under the terms of the GNU Library General Public
00010     License as published by the Free Software Foundation; either
00011     version 2 of the License, or (at your option) any later version.
00012 
00013     This library is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016     Library General Public License for more details.
00017 
00018     You should have received a copy of the GNU Library General Public License
00019     along with this library; see the file COPYING.LIB.  If not, write to
00020     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00021     Boston, MA 02111-1307, USA.
00022 */
00023 
00024 #ifndef KRESOURCES_MANAGER_H
00025 #define KRESOURCES_MANAGER_H
00026 
00027 #include <qdict.h>
00028 #include <qstringlist.h>
00029 
00030 #include <kdebug.h>
00031 #include <kresources/factory.h>
00032 #include <kresources/managerimpl.h>
00033 
00034 namespace KRES {
00035 
00036 class Resource;
00037 
00046 template<class T>
00047 class ManagerObserver
00048 {
00049   public:
00050     virtual void resourceAdded( T *resource ) = 0;
00051     virtual void resourceModified( T *resource ) = 0;
00052     virtual void resourceDeleted( T *resource ) = 0;
00053 };
00054 
00058 class ManagerNotifier
00059 {
00060   public:
00061     virtual void notifyResourceAdded( Resource *resource ) = 0;
00062     virtual void notifyResourceModified( Resource *resource ) = 0;
00063     virtual void notifyResourceDeleted( Resource *resource ) = 0;
00064 };
00065 
00074 template<class T>
00075 class Manager : private ManagerNotifier
00076 {
00077   public:
00081     class Iterator
00082     {
00083         friend class Manager;
00084       public:
00085         Iterator() {};
00086         Iterator( const Iterator &it ) { mIt = it.mIt; }
00087 
00088         T *operator*() { return static_cast<T *>( *mIt ); }
00089         Iterator &operator++() { mIt++; return *this; }
00090         Iterator &operator++( int ) { mIt++; return *this; }
00091         Iterator &operator--() { mIt--; return *this; }
00092         Iterator &operator--( int ) { mIt--; return *this; }
00093         bool operator==( const Iterator &it ) { return mIt == it.mIt; }
00094         bool operator!=( const Iterator &it ) { return mIt != it.mIt; }
00095 
00096       private:       
00097         Resource::List::Iterator mIt;
00098     };
00099 
00103     Iterator begin()
00104     {
00105       Iterator it;
00106       it.mIt = mImpl->resourceList()->begin();
00107       return it;
00108     }
00109   
00113     Iterator end()
00114     {
00115       Iterator it;
00116       it.mIt = mImpl->resourceList()->end();
00117       return it;
00118     }
00119 
00123     class ActiveIterator
00124     {
00125         friend class Manager;
00126       public:
00127         ActiveIterator() : mList( 0 ) {};
00128         ActiveIterator( const ActiveIterator &it )
00129         {
00130           mIt = it.mIt;
00131           mList = it.mList;
00132         }
00133 
00134         T *operator*() { return static_cast<T *>( *mIt ); }
00135         ActiveIterator &operator++()
00136         {
00137           do { mIt++; } while ( checkActive() );
00138           return *this;
00139         }
00140         ActiveIterator &operator++( int )
00141         {
00142           do { mIt++; } while ( checkActive() );
00143           return *this;
00144         }
00145         ActiveIterator &operator--()
00146         {
00147           do { mIt--; } while ( checkActive() );
00148           return *this;
00149         }
00150         ActiveIterator &operator--( int )
00151         {
00152           do { mIt--; } while ( checkActive() );
00153           return *this;
00154         }
00155         bool operator==( const ActiveIterator &it ) { return mIt == it.mIt; }
00156         bool operator!=( const ActiveIterator &it ) { return mIt != it.mIt; }
00157 
00158       private:
00162         bool checkActive()
00163         {
00164           if ( !mList || mIt == mList->end() ) return false;
00165           return !(*mIt)->isActive();
00166         }
00167 
00168         Resource::List::Iterator mIt;
00169         Resource::List *mList;
00170     };
00171 
00176     ActiveIterator activeBegin()
00177     {
00178       ActiveIterator it;
00179       it.mIt = mImpl->resourceList()->begin();
00180       it.mList = mImpl->resourceList();
00181       if ( it.mIt != mImpl->resourceList()->end() ) {
00182         if ( !(*it)->isActive() ) it++;
00183       }
00184       return it;
00185     }
00186 
00190     ActiveIterator activeEnd()
00191     {
00192       ActiveIterator it;
00193       it.mIt = mImpl->resourceList()->end();
00194       it.mList = mImpl->resourceList();
00195       return it;
00196     }
00197 
00202     bool isEmpty() const { return mImpl->resourceList()->isEmpty(); }
00203 
00208     Manager( const QString &family )
00209     {
00210       mFactory = Factory::self( family );
00211       // The managerimpl will use the same Factory object as the manager
00212       // because of the Factory::self() pattern
00213       mImpl = new ManagerImpl( this, family );
00214       mObservers.setAutoDelete( false );
00215     }
00216 
00217     virtual ~Manager()
00218     { 
00219       delete mImpl;
00220     }
00221 
00226     void readConfig( KConfig *cfg = 0 )
00227     {
00228       mImpl->readConfig( cfg );
00229     }
00230 
00235     void writeConfig( KConfig *cfg = 0 )
00236     {
00237       mImpl->writeConfig( cfg );
00238     }
00239 
00244     void add( Resource *resource )
00245     {
00246       if ( resource ) mImpl->add( resource );
00247     }
00248 
00252     void remove( Resource *resource )
00253     {
00254       if ( resource ) mImpl->remove( resource );
00255     }
00256 
00261     void change( T *resource )
00262     {
00263       mImpl->change( resource ); 
00264     }
00265 
00269     T *standardResource()
00270     {
00271       return static_cast<T *>( mImpl->standardResource() );
00272     }
00273 
00277     void setStandardResource( T *resource )
00278     {
00279       if ( resource ) mImpl->setStandardResource( resource );
00280     }
00281 
00285     void setActive( Resource *resource, bool active )
00286     {
00287       if ( resource ) mImpl->setActive( resource, active );
00288     }
00289 
00294     QStringList resourceNames() const
00295     {
00296       return mImpl->resourceNames();
00297     }
00298 
00309     T *createResource( const QString& type )
00310     {
00311       return dynamic_cast<T *>( mFactory->resource( type, 0 ) );
00312     }
00313 
00317     QStringList resourceTypeNames() const
00318     {
00319       return mFactory->typeNames();
00320     }
00321 
00325     QStringList resourceTypeDescriptions() const
00326     {
00327       QStringList typeDescs;
00328       QStringList types = mFactory->typeNames();
00329 
00330       for ( QStringList::ConstIterator it = types.begin(); it != types.end();
00331             ++it ) {
00332         QString desc = mFactory->typeName( *it );
00333         if ( !mFactory->typeDescription( *it ).isEmpty() )
00334           desc += " (" + mFactory->typeDescription( *it ) + ")";
00335 
00336         typeDescs.append( desc );
00337       }
00338 
00339       return typeDescs;
00340     }
00341 
00346     void addObserver( ManagerObserver<T> *observer )
00347     {
00348       mObservers.append( observer );
00349     }
00350 
00355     void removeObserver( ManagerObserver<T> *observer )
00356     {
00357       mObservers.remove( observer );
00358     }
00359 
00360   private:
00364     void notifyResourceAdded( Resource *res )
00365     {
00366       kdDebug(5650) << "Manager::resourceAdded " << res->resourceName() << endl;
00367       T *resource = dynamic_cast<T *>( res );
00368       if ( resource ) {
00369         ManagerObserver<T> *observer;
00370         for ( observer = mObservers.first(); observer;
00371               observer = mObservers.next() )
00372           observer->resourceAdded( resource );
00373       }
00374     }
00375 
00379     void notifyResourceModified( Resource *res )
00380     {
00381       kdDebug(5650) << "Manager::resourceModified " << res->resourceName()
00382                     << endl;
00383       T *resource = dynamic_cast<T *>( res );
00384       if ( resource ) {
00385         ManagerObserver<T> *observer;
00386         for ( observer = mObservers.first(); observer;
00387               observer = mObservers.next() )
00388           observer->resourceModified( resource );
00389       }
00390     }
00391     
00395     void notifyResourceDeleted( Resource *res )
00396     {
00397       kdDebug(5650) << "Manager::resourceDeleted " << res->resourceName()
00398                     << endl;
00399       T *resource = dynamic_cast<T *>( res );
00400       if ( resource ) {
00401         ManagerObserver<T> *observer;
00402         for ( observer = mObservers.first(); observer;
00403               observer = mObservers.next() ) {
00404           kdDebug(5650) << "Notifying a observer to Manager..." << endl;
00405           observer->resourceDeleted( resource );
00406         }
00407       }
00408     }
00409 
00410   private:
00411     ManagerImpl *mImpl;
00412     Factory *mFactory;
00413     QPtrList<ManagerObserver<T> > mObservers;
00414 };
00415 
00416 }
00417 
00418 #endif
KDE Logo
This file is part of the documentation for kresources Library Version 3.2.3.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Aug 4 05:27:12 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2003