manager.h00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
00078 template<class T>
00079 class Manager : private ManagerNotifier
00080 {
00081 public:
00085 class Iterator
00086 {
00087 friend class Manager;
00088 public:
00089 Iterator() {};
00090 Iterator( const Iterator &it ) { mIt = it.mIt; }
00091
00092 T *operator*() { return static_cast<T *>( *mIt ); }
00093 Iterator &operator++() { mIt++; return *this; }
00094 Iterator &operator++( int ) { mIt++; return *this; }
00095 Iterator &operator--() { mIt--; return *this; }
00096 Iterator &operator--( int ) { mIt--; return *this; }
00097 bool operator==( const Iterator &it ) { return mIt == it.mIt; }
00098 bool operator!=( const Iterator &it ) { return mIt != it.mIt; }
00099
00100 private:
00101 Resource::List::Iterator mIt;
00102 };
00103
00107 Iterator begin()
00108 {
00109 Iterator it;
00110 it.mIt = mImpl->resourceList()->begin();
00111 return it;
00112 }
00113
00117 Iterator end()
00118 {
00119 Iterator it;
00120 it.mIt = mImpl->resourceList()->end();
00121 return it;
00122 }
00123
00127 class ActiveIterator
00128 {
00129 friend class Manager;
00130 public:
00131 ActiveIterator() : mList( 0 ) {};
00132 ActiveIterator( const ActiveIterator &it )
00133 {
00134 mIt = it.mIt;
00135 mList = it.mList;
00136 }
00137
00138 T *operator*() { return static_cast<T *>( *mIt ); }
00139 ActiveIterator &operator++()
00140 {
00141 do { mIt++; } while ( checkActive() );
00142 return *this;
00143 }
00144 ActiveIterator &operator++( int )
00145 {
00146 do { mIt++; } while ( checkActive() );
00147 return *this;
00148 }
00149 ActiveIterator &operator--()
00150 {
00151 do { mIt--; } while ( checkActive() );
00152 return *this;
00153 }
00154 ActiveIterator &operator--( int )
00155 {
00156 do { mIt--; } while ( checkActive() );
00157 return *this;
00158 }
00159 bool operator==( const ActiveIterator &it ) { return mIt == it.mIt; }
00160 bool operator!=( const ActiveIterator &it ) { return mIt != it.mIt; }
00161
00162 private:
00166 bool checkActive()
00167 {
00168 if ( !mList || mIt == mList->end() ) return false;
00169 return !(*mIt)->isActive();
00170 }
00171
00172 Resource::List::Iterator mIt;
00173 Resource::List *mList;
00174 };
00175
00180 ActiveIterator activeBegin()
00181 {
00182 ActiveIterator it;
00183 it.mIt = mImpl->resourceList()->begin();
00184 it.mList = mImpl->resourceList();
00185 if ( it.mIt != mImpl->resourceList()->end() ) {
00186 if ( !(*it)->isActive() ) it++;
00187 }
00188 return it;
00189 }
00190
00194 ActiveIterator activeEnd()
00195 {
00196 ActiveIterator it;
00197 it.mIt = mImpl->resourceList()->end();
00198 it.mList = mImpl->resourceList();
00199 return it;
00200 }
00201
00206 bool isEmpty() const { return mImpl->resourceList()->isEmpty(); }
00207
00212 Manager( const QString &family )
00213 {
00214 mFactory = Factory::self( family );
00215
00216
00217 mImpl = new ManagerImpl( this, family );
00218 mObservers.setAutoDelete( false );
00219 }
00220
00221 virtual ~Manager()
00222 {
00223 delete mImpl;
00224 }
00225
00230 void readConfig( KConfig *cfg = 0 )
00231 {
00232 mImpl->readConfig( cfg );
00233 }
00234
00239 void writeConfig( KConfig *cfg = 0 )
00240 {
00241 mImpl->writeConfig( cfg );
00242 }
00243
00248 void add( Resource *resource )
00249 {
00250 if ( resource ) mImpl->add( resource );
00251 }
00252
00256 void remove( Resource *resource )
00257 {
00258 if ( resource ) mImpl->remove( resource );
00259 }
00260
00265 void change( T *resource )
00266 {
00267 mImpl->change( resource );
00268 }
00269
00273 T *standardResource()
00274 {
00275 return static_cast<T *>( mImpl->standardResource() );
00276 }
00277
00281 void setStandardResource( T *resource )
00282 {
00283 if ( resource ) mImpl->setStandardResource( resource );
00284 }
00285
00289 void setActive( Resource *resource, bool active )
00290 {
00291 if ( resource ) mImpl->setActive( resource, active );
00292 }
00293
00298 QStringList resourceNames() const
00299 {
00300 return mImpl->resourceNames();
00301 }
00302
00313 T *createResource( const QString& type )
00314 {
00315 return dynamic_cast<T *>( mFactory->resource( type, 0 ) );
00316 }
00317
00321 QStringList resourceTypeNames() const
00322 {
00323 return mFactory->typeNames();
00324 }
00325
00329 QStringList resourceTypeDescriptions() const
00330 {
00331 QStringList typeDescs;
00332 QStringList types = mFactory->typeNames();
00333
00334 for ( QStringList::ConstIterator it = types.begin(); it != types.end();
00335 ++it ) {
00336 QString desc = mFactory->typeName( *it );
00337 if ( !mFactory->typeDescription( *it ).isEmpty() )
00338 desc += QString::fromLatin1(" (") + mFactory->typeDescription( *it ) + QString::fromLatin1(")");
00339
00340 typeDescs.append( desc );
00341 }
00342
00343 return typeDescs;
00344 }
00345
00350 void addObserver( ManagerObserver<T> *observer )
00351 {
00352 mObservers.append( observer );
00353 }
00354
00359 void removeObserver( ManagerObserver<T> *observer )
00360 {
00361 mObservers.remove( observer );
00362 }
00363
00364 private:
00368 void notifyResourceAdded( Resource *res )
00369 {
00370 kdDebug(5650) << "Manager::resourceAdded " << res->resourceName() << endl;
00371 T *resource = dynamic_cast<T *>( res );
00372 if ( resource ) {
00373 ManagerObserver<T> *observer;
00374 for ( observer = mObservers.first(); observer;
00375 observer = mObservers.next() )
00376 observer->resourceAdded( resource );
00377 }
00378 }
00379
00383 void notifyResourceModified( Resource *res )
00384 {
00385 kdDebug(5650) << "Manager::resourceModified " << res->resourceName()
00386 << endl;
00387 T *resource = dynamic_cast<T *>( res );
00388 if ( resource ) {
00389 ManagerObserver<T> *observer;
00390 for ( observer = mObservers.first(); observer;
00391 observer = mObservers.next() )
00392 observer->resourceModified( resource );
00393 }
00394 }
00395
00399 void notifyResourceDeleted( Resource *res )
00400 {
00401 kdDebug(5650) << "Manager::resourceDeleted " << res->resourceName()
00402 << endl;
00403 T *resource = dynamic_cast<T *>( res );
00404 if ( resource ) {
00405 ManagerObserver<T> *observer;
00406 for ( observer = mObservers.first(); observer;
00407 observer = mObservers.next() ) {
00408 kdDebug(5650) << "Notifying a observer to Manager..." << endl;
00409 observer->resourceDeleted( resource );
00410 }
00411 }
00412 }
00413
00414 private:
00415 ManagerImpl *mImpl;
00416 Factory *mFactory;
00417 QPtrList<ManagerObserver<T> > mObservers;
00418 };
00419
00420 }
00421
00422 #endif
|