• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepimlibs API Reference
  • KDE Home
  • Contact Us
 

kresources

  • sources
  • kde-4.12
  • kdepimlibs
  • kresources
manager.h
1 /*
2  This file is part of libkresources.
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 KRESOURCES_MANAGER_H
25 #define KRESOURCES_MANAGER_H
26 
27 #include "managerimpl.h"
28 #include "factory.h"
29 
30 #include <kdebug.h>
31 
32 #include <QtCore/QList>
33 #include <QtCore/QStringList>
34 
35 namespace KRES {
36 
37 class Resource;
38 
47 template<class T>
48 class ManagerObserver
49 {
50  public:
51  virtual ~ManagerObserver(){}
52  virtual void resourceAdded( T *resource ) = 0;
53  virtual void resourceModified( T *resource ) = 0;
54  virtual void resourceDeleted( T *resource ) = 0;
55 };
56 
60 class ManagerNotifier
61 {
62  public:
63  virtual ~ManagerNotifier(){}
64  virtual void notifyResourceAdded( Resource *resource ) = 0;
65  virtual void notifyResourceModified( Resource *resource ) = 0;
66  virtual void notifyResourceDeleted( Resource *resource ) = 0;
67 };
68 
81 template<class T>
82 class Manager : private ManagerNotifier
83 {
84  public:
88  class Iterator
89  {
90  friend class Manager;
91  public:
92  Iterator() {}
93  Iterator( const Iterator &it ) { mIt = it.mIt; }
94 
95  T *operator*() { return static_cast<T *>( *mIt ); }
96  Iterator &operator++()
97  {
98  mIt++;
99  return *this;
100  }
101  Iterator &operator++( int )
102  {
103  mIt++;
104  return *this;
105  }
106  Iterator &operator--()
107  {
108  mIt--;
109  return *this;
110  }
111  Iterator &operator--( int )
112  {
113  mIt--;
114  return *this;
115  }
116  bool operator==( const Iterator &it ) const
117  {
118  return mIt == it.mIt;
119  }
120  bool operator!=( const Iterator &it ) const
121  {
122  return mIt != it.mIt;
123  }
124 
125  private:
126  Resource::List::Iterator mIt;
127  };
128 
132  typedef Iterator iterator;
133 
137  Iterator begin()
138  {
139  Iterator it;
140  it.mIt = mImpl->resourceList()->begin();
141  return it;
142  }
143 
147  Iterator end()
148  {
149  Iterator it;
150  it.mIt = mImpl->resourceList()->end();
151  return it;
152  }
153 
157  class ActiveIterator
158  {
159  friend class Manager;
160  public:
161  ActiveIterator() : mList( 0 ) {}
162  ActiveIterator( const ActiveIterator &it )
163  {
164  mIt = it.mIt;
165  mList = it.mList;
166  }
167 
168  T *operator*() { return static_cast<T *>( *mIt ); }
169  ActiveIterator &operator++()
170  {
171  do { mIt++; } while ( checkActive() );
172  return *this;
173  }
174  ActiveIterator &operator++( int )
175  {
176  do { mIt++; } while ( checkActive() );
177  return *this;
178  }
179  ActiveIterator &operator--()
180  {
181  do { mIt--; } while ( checkActive() );
182  return *this;
183  }
184  ActiveIterator &operator--( int )
185  {
186  do { mIt--; } while ( checkActive() );
187  return *this;
188  }
189  bool operator==( const ActiveIterator &it ) const { return mIt == it.mIt; }
190  bool operator!=( const ActiveIterator &it ) const { return mIt != it.mIt; }
191 
192  private:
196  bool checkActive()
197  {
198  if ( !mList || mIt == mList->end() ) {
199  return false;
200  }
201  return !( *mIt )->isActive();
202  }
203 
204  Resource::List::Iterator mIt;
205  Resource::List *mList;
206  };
207 
212  ActiveIterator activeBegin()
213  {
214  ActiveIterator it;
215  it.mIt = mImpl->resourceList()->begin();
216  it.mList = mImpl->resourceList();
217  if ( it.mIt != mImpl->resourceList()->end() ) {
218  if ( !( *it )->isActive() ) {
219  it++;
220  }
221  }
222  return it;
223  }
224 
228  ActiveIterator activeEnd()
229  {
230  ActiveIterator it;
231  it.mIt = mImpl->resourceList()->end();
232  it.mList = mImpl->resourceList();
233  return it;
234  }
235 
240  bool isEmpty() const { return mImpl->resourceList()->isEmpty(); }
241 
246  Manager( const QString &family )
247  {
248  mFactory = Factory::self( family );
249  // The managerimpl will use the same Factory object as the manager
250  // because of the Factory::self() pattern
251  mImpl = new ManagerImpl( this, family );
252  }
253 
254  virtual ~Manager()
255  {
256  delete mImpl;
257  }
258 
263  void readConfig( KConfig *cfg = 0 )
264  {
265  mImpl->readConfig( cfg );
266  }
267 
272  void writeConfig( KConfig *cfg = 0 )
273  {
274  mImpl->writeConfig( cfg );
275  }
276 
281  void add( Resource *resource )
282  {
283  if ( resource ) {
284  mImpl->add( resource );
285  }
286  }
287 
291  void remove( Resource *resource )
292  {
293  if ( resource ) {
294  mImpl->remove( resource );
295  }
296  }
297 
302  void change( T *resource )
303  {
304  mImpl->change( resource );
305  }
306 
310  T *standardResource()
311  {
312  return static_cast<T *>( mImpl->standardResource() );
313  }
314 
318  void setStandardResource( T *resource )
319  {
320  if ( resource ) {
321  mImpl->setStandardResource( resource );
322  }
323  }
324 
328  void setActive( Resource *resource, bool active )
329  {
330  if ( resource ) {
331  mImpl->setActive( resource, active );
332  }
333  }
334 
339  QStringList resourceNames() const
340  {
341  return mImpl->resourceNames();
342  }
343 
354  T *createResource( const QString &type )
355  {
356  return dynamic_cast<T *>( mFactory->resource( type ) );
357  }
358 
362  QStringList resourceTypeNames() const
363  {
364  return mFactory->typeNames();
365  }
366 
370  QStringList resourceTypeDescriptions() const
371  {
372  QStringList typeDescs;
373  const QStringList types = mFactory->typeNames();
374 
375  for ( QStringList::ConstIterator it = types.constBegin(); it != types.constEnd(); ++it ) {
376  QString desc = mFactory->typeName( *it );
377  if ( !mFactory->typeDescription( *it ).isEmpty() ) {
378  desc += QLatin1String( " (" ) + mFactory->typeDescription( *it ) + QLatin1Char( ')' );
379  }
380 
381  typeDescs.append( desc );
382  }
383 
384  return typeDescs;
385  }
386 
391  void addObserver( ManagerObserver<T> *observer )
392  {
393  mObservers.append( observer );
394  }
395 
400  void removeObserver( ManagerObserver<T> *observer )
401  {
402  mObservers.removeAll( observer );
403  }
404 
405  private:
409  void notifyResourceAdded( Resource *res )
410  {
411  kDebug() << res->resourceName();
412  T *resource = dynamic_cast<T *>( res );
413  if ( resource ) {
414  for ( int i = 0; i < mObservers.size(); ++i ) {
415  mObservers.at( i )->resourceAdded( resource );
416  }
417  }
418  }
419 
423  void notifyResourceModified( Resource *res )
424  {
425  kDebug() << res->resourceName();
426  T *resource = dynamic_cast<T *>( res );
427  if ( resource ) {
428  for ( int i = 0; i < mObservers.size(); ++i ) {
429  mObservers.at( i )->resourceAdded( resource );
430  }
431  }
432  }
433 
437  void notifyResourceDeleted( Resource *res )
438  {
439  kDebug() << res->resourceName();
440  T *resource = dynamic_cast<T *>( res );
441  if ( resource ) {
442  for ( int i = 0; i < mObservers.size(); ++i ) {
443  mObservers.at( i )->resourceDeleted( resource );
444  }
445  }
446  }
447 
448  private:
449  ManagerImpl *mImpl;
450  Factory *mFactory;
451  QList<ManagerObserver<T> *> mObservers;
452 };
453 
454 }
455 
456 #endif
KRES::Factory::self
static Factory * self(const QString &resourceFamily)
Returns the global resource factory.
Definition: factory.cpp:68
KRES::Manager::resourceTypeNames
QStringList resourceTypeNames() const
Returns a list of the names of all available resource types.
Definition: manager.h:362
KRES::Factory::typeNames
QStringList typeNames() const
Returns a list of all available resource types.
Definition: factory.cpp:148
KRES::Manager::setActive
void setActive(Resource *resource, bool active)
Set active state of resource.
Definition: manager.h:328
KRES::Manager::standardResource
T * standardResource()
Return standard resource.
Definition: manager.h:310
KRES::Manager::resourceTypeDescriptions
QStringList resourceTypeDescriptions() const
Return list of descriptions of all available resource types.
Definition: manager.h:370
KRES::Manager::end
Iterator end()
Return Iterator indicating end of resource list.
Definition: manager.h:147
KRES::Manager::Manager
Manager(const QString &family)
Create manager for given resource family.
Definition: manager.h:246
KRES::Manager::removeObserver
void removeObserver(ManagerObserver< T > *observer)
Remove Observer for resource changes from manager.
Definition: manager.h:400
KRES::Manager::createResource
T * createResource(const QString &type)
Creates a new resource of type type with default settings.
Definition: manager.h:354
KRES::Manager::activeEnd
ActiveIterator activeEnd()
Return Iterator indicating end of active resource list.
Definition: manager.h:228
KRES::Manager::begin
Iterator begin()
Return Iterator on first resource.
Definition: manager.h:137
KRES::ManagerObserver
Observer class for Manager class.
Definition: manager.h:48
KRES::Manager::change
void change(T *resource)
Call this to notify manager about changes of the configuration of the given resource.
Definition: manager.h:302
KRES::Manager::setStandardResource
void setStandardResource(T *resource)
Set standard resource.
Definition: manager.h:318
KRES::Manager::Iterator
Iterator for iterations over all resources managed by a manager.
Definition: manager.h:88
KRES::Manager::readConfig
void readConfig(KConfig *cfg=0)
Recreate Resource objects from configuration file.
Definition: manager.h:263
KRES::Manager::ActiveIterator
Iterator for iterations over only active resources managed by a manager.
Definition: manager.h:157
KRES::Factory::resource
Resource * resource(const QString &type, const KConfigGroup &group)
Returns a pointer to a resource object or a null pointer if resource type doesn't exist...
Definition: factory.cpp:245
KRES::Resource
This class provides a resource which is managed in a general way.
Definition: resource.h:75
factory.h
This file is part of the KDE resource framework and defines the Factory class.
KRES::Manager::writeConfig
void writeConfig(KConfig *cfg=0)
Write configuration of Resource objects to configuration file.
Definition: manager.h:272
KRES::Manager::resourceNames
QStringList resourceNames() const
Returns a list of the names of the resources managed by the Manager for this family.
Definition: manager.h:339
KRES::Resource::resourceName
virtual QString resourceName() const
Returns the name of resource.
Definition: resource.cpp:189
KRES::Factory::typeName
QString typeName(const QString &type) const
Returns the name for a special type.
Definition: factory.cpp:183
KRES::Manager::add
void add(Resource *resource)
Add resource to manager.
Definition: manager.h:281
KRES::Manager::activeBegin
ActiveIterator activeBegin()
Return Iterator on first active resource.
Definition: manager.h:212
KRES::ManagerImpl
Definition: managerimpl.h:41
KRES::Manager::addObserver
void addObserver(ManagerObserver< T > *observer)
Add observer for resource changes to manager.
Definition: manager.h:391
KRES::Manager
This class provides a manager for resources of a specified family.
Definition: manager.h:82
KRES::Factory::typeDescription
QString typeDescription(const QString &type) const
Returns the description for a special type.
Definition: factory.cpp:193
KRES::ManagerNotifier
Definition: manager.h:60
KRES::Manager::isEmpty
bool isEmpty() const
Return true, if manager doesn't hold any resources.
Definition: manager.h:240
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:00:19 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kresources

Skip menu "kresources"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal