• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tderesources
 

tderesources

  • tderesources
manager.h
1 /*
2  This file is part of libtderesources.
3 
4  Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>
5  Copyright (c) 2002 Jan-Pascal van Best <janpascal@vanbest.org>
6  Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>
7 
8  This library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Library General Public
10  License as published by the Free Software Foundation; either
11  version 2 of the License, or (at your option) any later version.
12 
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Library General Public License for more details.
17 
18  You should have received a copy of the GNU Library General Public License
19  along with this library; see the file COPYING.LIB. If not, write to
20  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  Boston, MA 02110-1301, USA.
22 */
23 
24 #ifndef TDERESOURCES_MANAGER_H
25 #define TDERESOURCES_MANAGER_H
26 
27 #include <tqdict.h>
28 #include <tqstringlist.h>
29 
30 #include <kdebug.h>
31 #include <tderesources/factory.h>
32 #include <tderesources/managerimpl.h>
33 
34 namespace KRES {
35 
36 class Resource;
37 
46 template<class T>
47 class ManagerObserver
48 {
49  public:
50  virtual void resourceAdded( T *resource ) = 0;
51  virtual void resourceModified( T *resource ) = 0;
52  virtual void resourceDeleted( T *resource ) = 0;
53 };
54 
58 class ManagerNotifier
59 {
60  public:
61  virtual void notifyResourceAdded( Resource *resource ) = 0;
62  virtual void notifyResourceModified( Resource *resource ) = 0;
63  virtual void notifyResourceDeleted( Resource *resource ) = 0;
64 };
65 
78 template<class T>
79 class Manager : private ManagerNotifier
80 {
81  public:
85  class Iterator
86  {
87  friend class Manager;
88  public:
89  Iterator() {};
90  Iterator( const Iterator &it ) { mIt = it.mIt; }
91 
92  T *operator*() { return static_cast<T *>( *mIt ); }
93  Iterator &operator++() { mIt++; return *this; }
94  Iterator &operator++( int ) { mIt++; return *this; }
95  Iterator &operator--() { mIt--; return *this; }
96  Iterator &operator--( int ) { mIt--; return *this; }
97  bool operator==( const Iterator &it ) { return mIt == it.mIt; }
98  bool operator!=( const Iterator &it ) { return mIt != it.mIt; }
99 
100  private:
101  Resource::List::Iterator mIt;
102  };
103 
107  Iterator begin()
108  {
109  Iterator it;
110  it.mIt = mImpl->resourceList()->begin();
111  return it;
112  }
113 
117  Iterator end()
118  {
119  Iterator it;
120  it.mIt = mImpl->resourceList()->end();
121  return it;
122  }
123 
127  class ActiveIterator
128  {
129  friend class Manager;
130  public:
131  ActiveIterator() : mList( 0 ) {};
132  ActiveIterator( const ActiveIterator &it )
133  {
134  mIt = it.mIt;
135  mList = it.mList;
136  }
137 
138  T *operator*() { return static_cast<T *>( *mIt ); }
139  ActiveIterator &operator++()
140  {
141  do { mIt++; } while ( checkActive() );
142  return *this;
143  }
144  ActiveIterator &operator++( int )
145  {
146  do { mIt++; } while ( checkActive() );
147  return *this;
148  }
149  ActiveIterator &operator--()
150  {
151  do { mIt--; } while ( checkActive() );
152  return *this;
153  }
154  ActiveIterator &operator--( int )
155  {
156  do { mIt--; } while ( checkActive() );
157  return *this;
158  }
159  bool operator==( const ActiveIterator &it ) { return mIt == it.mIt; }
160  bool operator!=( const ActiveIterator &it ) { return mIt != it.mIt; }
161 
162  private:
166  bool checkActive()
167  {
168  if ( !mList || mIt == mList->end() ) return false;
169  return !(*mIt)->isActive();
170  }
171 
172  Resource::List::Iterator mIt;
173  Resource::List *mList;
174  };
175 
180  ActiveIterator activeBegin()
181  {
182  ActiveIterator it;
183  it.mIt = mImpl->resourceList()->begin();
184  it.mList = mImpl->resourceList();
185  if ( it.mIt != mImpl->resourceList()->end() ) {
186  if ( !(*it)->isActive() ) it++;
187  }
188  return it;
189  }
190 
194  ActiveIterator activeEnd()
195  {
196  ActiveIterator it;
197  it.mIt = mImpl->resourceList()->end();
198  it.mList = mImpl->resourceList();
199  return it;
200  }
201 
206  bool isEmpty() const { return mImpl->resourceList()->isEmpty(); }
207 
212  Manager( const TQString &family )
213  {
214  mFactory = Factory::self( family );
215  // The managerimpl will use the same Factory object as the manager
216  // because of the Factory::self() pattern
217  mImpl = new ManagerImpl( this, family );
218  mObservers.setAutoDelete( false );
219  }
220 
221  virtual ~Manager()
222  {
223  delete mImpl;
224  }
225 
230  void readConfig( TDEConfig *cfg = 0 )
231  {
232  mImpl->readConfig( cfg );
233  }
234 
239  void writeConfig( TDEConfig *cfg = 0 )
240  {
241  mImpl->writeConfig( cfg );
242  }
243 
248  void add( Resource *resource )
249  {
250  if ( resource ) mImpl->add( resource );
251  }
252 
256  void remove( Resource *resource )
257  {
258  if ( resource ) mImpl->remove( resource );
259  }
260 
265  void change( T *resource )
266  {
267  mImpl->change( resource );
268  }
269 
273  T *standardResource()
274  {
275  return static_cast<T *>( mImpl->standardResource() );
276  }
277 
281  void setStandardResource( T *resource )
282  {
283  if ( resource ) mImpl->setStandardResource( resource );
284  }
285 
289  void setActive( Resource *resource, bool active )
290  {
291  if ( resource ) mImpl->setActive( resource, active );
292  }
293 
298  TQStringList resourceNames() const
299  {
300  return mImpl->resourceNames();
301  }
302 
313  T *createResource( const TQString& type )
314  {
315  return dynamic_cast<T *>( mFactory->resource( type, 0 ) );
316  }
317 
321  TQStringList resourceTypeNames() const
322  {
323  return mFactory->typeNames();
324  }
325 
329  TQStringList resourceTypeDescriptions() const
330  {
331  TQStringList typeDescs;
332  TQStringList types = mFactory->typeNames();
333 
334  for ( TQStringList::ConstIterator it = types.begin(); it != types.end();
335  ++it ) {
336  TQString desc = mFactory->typeName( *it );
337  if ( !mFactory->typeDescription( *it ).isEmpty() )
338  desc += TQString::fromLatin1(" (") + mFactory->typeDescription( *it ) + TQString::fromLatin1(")");
339 
340  typeDescs.append( desc );
341  }
342 
343  return typeDescs;
344  }
345 
350  void addObserver( ManagerObserver<T> *observer )
351  {
352  mObservers.append( observer );
353  }
354 
359  void removeObserver( ManagerObserver<T> *observer )
360  {
361  mObservers.remove( observer );
362  }
363 
364  private:
368  void notifyResourceAdded( Resource *res )
369  {
370  kdDebug(5650) << "Manager::resourceAdded " << res->resourceName() << endl;
371  T *resource = dynamic_cast<T *>( res );
372  if ( resource ) {
373  ManagerObserver<T> *observer;
374  for ( observer = mObservers.first(); observer;
375  observer = mObservers.next() )
376  observer->resourceAdded( resource );
377  }
378  }
379 
383  void notifyResourceModified( Resource *res )
384  {
385  kdDebug(5650) << "Manager::resourceModified " << res->resourceName()
386  << endl;
387  T *resource = dynamic_cast<T *>( res );
388  if ( resource ) {
389  ManagerObserver<T> *observer;
390  for ( observer = mObservers.first(); observer;
391  observer = mObservers.next() )
392  observer->resourceModified( resource );
393  }
394  }
395 
399  void notifyResourceDeleted( Resource *res )
400  {
401  kdDebug(5650) << "Manager::resourceDeleted " << res->resourceName()
402  << endl;
403  T *resource = dynamic_cast<T *>( res );
404  if ( resource ) {
405  ManagerObserver<T> *observer;
406  for ( observer = mObservers.first(); observer;
407  observer = mObservers.next() ) {
408  kdDebug(5650) << "Notifying a observer to Manager..." << endl;
409  observer->resourceDeleted( resource );
410  }
411  }
412  }
413 
414  private:
415  ManagerImpl *mImpl;
416  Factory *mFactory;
417  TQPtrList<ManagerObserver<T> > mObservers;
418 };
419 
420 }
421 
422 #endif
KRES::Manager::Manager
Manager(const TQString &family)
Create manager for given resource family.
Definition: manager.h:212
KRES::Resource::resourceName
virtual TQString resourceName() const
Returns the name of resource.
Definition: resource.cpp:158
KRES::Manager::setActive
void setActive(Resource *resource, bool active)
Set active state of resource.
Definition: manager.h:289
KRES::Manager::standardResource
T * standardResource()
Return standard resource.
Definition: manager.h:273
KRES::Factory::typeName
TQString typeName(const TQString &type) const
Returns the name for a special type.
Definition: factory.cpp:110
KRES::Manager::resourceNames
TQStringList resourceNames() const
Returns a list of the names of the resources managed by the Manager for this family.
Definition: manager.h:298
KRES::Factory::typeNames
TQStringList typeNames() const
Returns a list of all available resource types.
Definition: factory.cpp:75
KRES::Manager::end
Iterator end()
Return Iterator indicating end of resource list.
Definition: manager.h:117
KRES::Manager::resourceTypeNames
TQStringList resourceTypeNames() const
Returns a list of the names of all available resource types.
Definition: manager.h:321
KRES::Manager::removeObserver
void removeObserver(ManagerObserver< T > *observer)
Remove Observer for resource changes from manager.
Definition: manager.h:359
KRES::Manager::activeEnd
ActiveIterator activeEnd()
Return Iterator indicating end of active resource list.
Definition: manager.h:194
KRES::Manager::readConfig
void readConfig(TDEConfig *cfg=0)
Recreate Resource objects from configuration file.
Definition: manager.h:230
KRES::Manager::createResource
T * createResource(const TQString &type)
Creates a new resource of type type with default settings.
Definition: manager.h:313
KRES::Manager::begin
Iterator begin()
Return Iterator on first resource.
Definition: manager.h:107
KRES::ManagerObserver
Observer class for Manager class.
Definition: manager.h:47
KRES
Definition: configdialog.h:31
KRES::Manager::change
void change(T *resource)
Call this to notify manager about changes of the configuration of the given resource.
Definition: manager.h:265
KRES::Manager::setStandardResource
void setStandardResource(T *resource)
Set standard resource.
Definition: manager.h:281
KRES::Manager::Iterator
Iterator for iterations over all resources managed by a manager.
Definition: manager.h:85
KRES::Manager::ActiveIterator
Iterator for iterations over only active resources managed by a manager.
Definition: manager.h:127
KRES::Resource
This class provides a resource which is managed in a general way.
Definition: resource.h:255
KRES::Factory::self
static Factory * self(const TQString &resourceFamily)
Returns the global resource factory.
Definition: factory.cpp:40
KRES::Manager::add
void add(Resource *resource)
Add resource to manager.
Definition: manager.h:248
KRES::Manager::resourceTypeDescriptions
TQStringList resourceTypeDescriptions() const
Return list of descriptions of all available resource types.
Definition: manager.h:329
KRES::Factory::resource
Resource * resource(const TQString &type, const TDEConfig *config)
Returns a pointer to a resource object or a null pointer if resource type doesn't exist...
Definition: factory.cpp:128
KRES::Manager::writeConfig
void writeConfig(TDEConfig *cfg=0)
Write configuration of Resource objects to configuration file.
Definition: manager.h:239
KRES::Factory::typeDescription
TQString typeDescription(const TQString &type) const
Returns the description for a special type.
Definition: factory.cpp:119
KRES::Manager::activeBegin
ActiveIterator activeBegin()
Return Iterator on first active resource.
Definition: manager.h:180
KRES::Manager::addObserver
void addObserver(ManagerObserver< T > *observer)
Add observer for resource changes to manager.
Definition: manager.h:350
KRES::Manager
This class provides a manager for resources of a specified family.
Definition: manager.h:79
KRES::Manager::isEmpty
bool isEmpty() const
Return true, if manager doesn't hold any resources.
Definition: manager.h:206

tderesources

Skip menu "tderesources"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members

tderesources

Skip menu "tderesources"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  •     tdecore
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  • tdeioslave
  •   http
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tderesources by doxygen 1.8.8
This website is maintained by Timothy Pearson.