kresources
manager.h
00001
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 <QtCore/QList>
00028 #include <QtCore/QStringList>
00029
00030 #include <kdebug.h>
00031
00032 #include "factory.h"
00033 #include "managerimpl.h"
00034
00035 namespace KRES {
00036
00037 class Resource;
00038
00047 template<class T>
00048 class ManagerObserver
00049 {
00050 public:
00051 virtual ~ManagerObserver(){}
00052 virtual void resourceAdded( T *resource ) = 0;
00053 virtual void resourceModified( T *resource ) = 0;
00054 virtual void resourceDeleted( T *resource ) = 0;
00055 };
00056
00060 class ManagerNotifier
00061 {
00062 public:
00063 virtual ~ManagerNotifier(){}
00064 virtual void notifyResourceAdded( Resource *resource ) = 0;
00065 virtual void notifyResourceModified( Resource *resource ) = 0;
00066 virtual void notifyResourceDeleted( Resource *resource ) = 0;
00067 };
00068
00081 template<class T>
00082 class Manager : private ManagerNotifier
00083 {
00084 public:
00088 class Iterator
00089 {
00090 friend class Manager;
00091 public:
00092 Iterator() {}
00093 Iterator( const Iterator &it ) { mIt = it.mIt; }
00094
00095 T *operator*() { return static_cast<T *>( *mIt ); }
00096 Iterator &operator++()
00097 {
00098 mIt++;
00099 return *this;
00100 }
00101 Iterator &operator++( int )
00102 {
00103 mIt++;
00104 return *this;
00105 }
00106 Iterator &operator--()
00107 {
00108 mIt--;
00109 return *this;
00110 }
00111 Iterator &operator--( int )
00112 {
00113 mIt--;
00114 return *this;
00115 }
00116 bool operator==( const Iterator &it ) const
00117 {
00118 return mIt == it.mIt;
00119 }
00120 bool operator!=( const Iterator &it ) const
00121 {
00122 return mIt != it.mIt;
00123 }
00124
00125 private:
00126 Resource::List::Iterator mIt;
00127 };
00128
00132 typedef Iterator iterator;
00133
00137 Iterator begin()
00138 {
00139 Iterator it;
00140 it.mIt = mImpl->resourceList()->begin();
00141 return it;
00142 }
00143
00147 Iterator end()
00148 {
00149 Iterator it;
00150 it.mIt = mImpl->resourceList()->end();
00151 return it;
00152 }
00153
00157 class ActiveIterator
00158 {
00159 friend class Manager;
00160 public:
00161 ActiveIterator() : mList( 0 ) {}
00162 ActiveIterator( const ActiveIterator &it )
00163 {
00164 mIt = it.mIt;
00165 mList = it.mList;
00166 }
00167
00168 T *operator*() { return static_cast<T *>( *mIt ); }
00169 ActiveIterator &operator++()
00170 {
00171 do { mIt++; } while ( checkActive() );
00172 return *this;
00173 }
00174 ActiveIterator &operator++( int )
00175 {
00176 do { mIt++; } while ( checkActive() );
00177 return *this;
00178 }
00179 ActiveIterator &operator--()
00180 {
00181 do { mIt--; } while ( checkActive() );
00182 return *this;
00183 }
00184 ActiveIterator &operator--( int )
00185 {
00186 do { mIt--; } while ( checkActive() );
00187 return *this;
00188 }
00189 bool operator==( const ActiveIterator &it ) const { return mIt == it.mIt; }
00190 bool operator!=( const ActiveIterator &it ) const { return mIt != it.mIt; }
00191
00192 private:
00196 bool checkActive()
00197 {
00198 if ( !mList || mIt == mList->end() ) {
00199 return false;
00200 }
00201 return !(*mIt)->isActive();
00202 }
00203
00204 Resource::List::Iterator mIt;
00205 Resource::List *mList;
00206 };
00207
00212 ActiveIterator activeBegin()
00213 {
00214 ActiveIterator it;
00215 it.mIt = mImpl->resourceList()->begin();
00216 it.mList = mImpl->resourceList();
00217 if ( it.mIt != mImpl->resourceList()->end() ) {
00218 if ( !(*it)->isActive() ) {
00219 it++;
00220 }
00221 }
00222 return it;
00223 }
00224
00228 ActiveIterator activeEnd()
00229 {
00230 ActiveIterator it;
00231 it.mIt = mImpl->resourceList()->end();
00232 it.mList = mImpl->resourceList();
00233 return it;
00234 }
00235
00240 bool isEmpty() const { return mImpl->resourceList()->isEmpty(); }
00241
00246 Manager( const QString &family )
00247 {
00248 mFactory = Factory::self( family );
00249
00250
00251 mImpl = new ManagerImpl( this, family );
00252 }
00253
00254 virtual ~Manager()
00255 {
00256 delete mImpl;
00257 }
00258
00263 void readConfig( KConfig *cfg = 0 )
00264 {
00265 mImpl->readConfig( cfg );
00266 }
00267
00272 void writeConfig( KConfig *cfg = 0 )
00273 {
00274 mImpl->writeConfig( cfg );
00275 }
00276
00281 void add( Resource *resource )
00282 {
00283 if ( resource ) {
00284 mImpl->add( resource );
00285 }
00286 }
00287
00291 void remove( Resource *resource )
00292 {
00293 if ( resource ) {
00294 mImpl->remove( resource );
00295 }
00296 }
00297
00302 void change( T *resource )
00303 {
00304 mImpl->change( resource );
00305 }
00306
00310 T *standardResource()
00311 {
00312 return static_cast<T *>( mImpl->standardResource() );
00313 }
00314
00318 void setStandardResource( T *resource )
00319 {
00320 if ( resource ) {
00321 mImpl->setStandardResource( resource );
00322 }
00323 }
00324
00328 void setActive( Resource *resource, bool active )
00329 {
00330 if ( resource ) {
00331 mImpl->setActive( resource, active );
00332 }
00333 }
00334
00339 QStringList resourceNames() const
00340 {
00341 return mImpl->resourceNames();
00342 }
00343
00354 T *createResource( const QString& type )
00355 {
00356 return dynamic_cast<T *>( mFactory->resource( type ) );
00357 }
00358
00362 QStringList resourceTypeNames() const
00363 {
00364 return mFactory->typeNames();
00365 }
00366
00370 QStringList resourceTypeDescriptions() const
00371 {
00372 QStringList typeDescs;
00373 QStringList types = mFactory->typeNames();
00374
00375 for ( QStringList::ConstIterator it = types.begin(); it != types.end(); ++it ) {
00376 QString desc = mFactory->typeName( *it );
00377 if ( !mFactory->typeDescription( *it ).isEmpty() ) {
00378 desc += QLatin1String( " (" ) + mFactory->typeDescription( *it ) + QLatin1Char( ')' );
00379 }
00380
00381 typeDescs.append( desc );
00382 }
00383
00384 return typeDescs;
00385 }
00386
00391 void addObserver( ManagerObserver<T> *observer )
00392 {
00393 mObservers.append( observer );
00394 }
00395
00400 void removeObserver( ManagerObserver<T> *observer )
00401 {
00402 mObservers.removeAll( observer );
00403 }
00404
00405 private:
00409 void notifyResourceAdded( Resource *res )
00410 {
00411 kDebug(5650) << "Manager::resourceAdded" << res->resourceName();
00412 T *resource = dynamic_cast<T *>( res );
00413 if ( resource ) {
00414 for ( int i = 0; i < mObservers.size(); ++i ) {
00415 mObservers.at(i)->resourceAdded( resource );
00416 }
00417 }
00418 }
00419
00423 void notifyResourceModified( Resource *res )
00424 {
00425 kDebug(5650) << "Manager::resourceModified" << res->resourceName();
00426 T *resource = dynamic_cast<T *>( res );
00427 if ( resource ) {
00428 for ( int i = 0; i < mObservers.size(); ++i ) {
00429 mObservers.at(i)->resourceAdded( resource );
00430 }
00431 }
00432 }
00433
00437 void notifyResourceDeleted( Resource *res )
00438 {
00439 kDebug(5650) << "Manager::resourceDeleted" << res->resourceName();
00440 T *resource = dynamic_cast<T *>( res );
00441 if ( resource ) {
00442 for ( int i = 0; i < mObservers.size(); ++i ) {
00443 mObservers.at(i)->resourceDeleted( resource );
00444 }
00445 }
00446 }
00447
00448 private:
00449 ManagerImpl *mImpl;
00450 Factory *mFactory;
00451 QList<ManagerObserver<T> *> mObservers;
00452 };
00453
00454 }
00455
00456 #endif