kresources
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., 51 Franklin Street, Fifth Floor, 00021 Boston, MA 02110-1301, USA. 00022 */ 00023 00024 #ifndef KRESOURCES_MANAGER_H 00025 #define KRESOURCES_MANAGER_H 00026 00027 #include "managerimpl.h" 00028 #include "factory.h" 00029 00030 #include <kdebug.h> 00031 00032 #include <QtCore/QList> 00033 #include <QtCore/QStringList> 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 // The managerimpl will use the same Factory object as the manager 00250 // because of the Factory::self() pattern 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 const QStringList types = mFactory->typeNames(); 00374 00375 for ( QStringList::ConstIterator it = types.constBegin(); it != types.constEnd(); ++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() << 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() << 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() << 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
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu May 10 2012 22:18:10 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu May 10 2012 22:18:10 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.