manager.h
/*
This file is part of libkresources.
Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>
Copyright (c) 2002 Jan-Pascal van Best <janpascal@vanbest.org>
Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#ifndef KRESOURCES_MANAGER_H
#define KRESOURCES_MANAGER_H
#include <qdict.h>
#include <qstringlist.h>
#include <kdebug.h>
#include <kresources/factory.h>
#include <kresources/managerimpl.h>
namespace KRES {
class Resource;
template<class T>
class ManagerObserver
{
public:
virtual void resourceAdded( T *resource ) = 0;
virtual void resourceModified( T *resource ) = 0;
virtual void resourceDeleted( T *resource ) = 0;
};
class ManagerNotifier
{
public:
virtual void notifyResourceAdded( Resource *resource ) = 0;
virtual void notifyResourceModified( Resource *resource ) = 0;
virtual void notifyResourceDeleted( Resource *resource ) = 0;
};
template<class T>
class Manager : private ManagerNotifier
{
public:
class Iterator
{
friend class Manager;
public:
Iterator() {};
Iterator( const Iterator &it ) { mIt = it.mIt; }
T *operator*() { return static_cast<T *>( *mIt ); }
Iterator &operator++() { mIt++; return *this; }
Iterator &operator++( int ) { mIt++; return *this; }
Iterator &operator--() { mIt--; return *this; }
Iterator &operator--( int ) { mIt--; return *this; }
bool operator==( const Iterator &it ) { return mIt == it.mIt; }
bool operator!=( const Iterator &it ) { return mIt != it.mIt; }
private:
Resource::List::Iterator mIt;
};
Iterator begin()
{
Iterator it;
it.mIt = mImpl->resourceList()->begin();
return it;
}
Iterator end()
{
Iterator it;
it.mIt = mImpl->resourceList()->end();
return it;
}
class ActiveIterator
{
friend class Manager;
public:
ActiveIterator() : mList( 0 ) {};
ActiveIterator( const ActiveIterator &it )
{
mIt = it.mIt;
mList = it.mList;
}
T *operator*() { return static_cast<T *>( *mIt ); }
ActiveIterator &operator++()
{
do { mIt++; } while ( checkActive() );
return *this;
}
ActiveIterator &operator++( int )
{
do { mIt++; } while ( checkActive() );
return *this;
}
ActiveIterator &operator--()
{
do { mIt--; } while ( checkActive() );
return *this;
}
ActiveIterator &operator--( int )
{
do { mIt--; } while ( checkActive() );
return *this;
}
bool operator==( const ActiveIterator &it ) { return mIt == it.mIt; }
bool operator!=( const ActiveIterator &it ) { return mIt != it.mIt; }
private:
bool checkActive()
{
if ( !mList || mIt == mList->end() ) return false;
return !(*mIt)->isActive();
}
Resource::List::Iterator mIt;
Resource::List *mList;
};
ActiveIterator activeBegin()
{
ActiveIterator it;
it.mIt = mImpl->resourceList()->begin();
it.mList = mImpl->resourceList();
if ( it.mIt != mImpl->resourceList()->end() ) {
if ( !(*it)->isActive() ) it++;
}
return it;
}
ActiveIterator activeEnd()
{
ActiveIterator it;
it.mIt = mImpl->resourceList()->end();
it.mList = mImpl->resourceList();
return it;
}
bool isEmpty() const { return mImpl->resourceList()->isEmpty(); }
Manager( const QString &family )
{
mFactory = Factory::self( family );
// The managerimpl will use the same Factory object as the manager
// because of the Factory::self() pattern
mImpl = new ManagerImpl( this, family );
mObservers.setAutoDelete( false );
}
virtual ~Manager()
{
delete mImpl;
}
void readConfig( KConfig *cfg = 0 )
{
mImpl->readConfig( cfg );
}
void writeConfig( KConfig *cfg = 0 )
{
mImpl->writeConfig( cfg );
}
void add( Resource *resource )
{
if ( resource ) mImpl->add( resource );
}
void remove( Resource *resource )
{
if ( resource ) mImpl->remove( resource );
}
void change( T *resource )
{
mImpl->change( resource );
}
T *standardResource()
{
return static_cast<T *>( mImpl->standardResource() );
}
void setStandardResource( T *resource )
{
if ( resource ) mImpl->setStandardResource( resource );
}
void setActive( Resource *resource, bool active )
{
if ( resource ) mImpl->setActive( resource, active );
}
QStringList resourceNames() const
{
return mImpl->resourceNames();
}
T *createResource( const QString& type )
{
return dynamic_cast<T *>( mFactory->resource( type, 0 ) );
}
QStringList resourceTypeNames() const
{
return mFactory->typeNames();
}
QStringList resourceTypeDescriptions() const
{
QStringList typeDescs;
QStringList types = mFactory->typeNames();
for ( QStringList::ConstIterator it = types.begin(); it != types.end();
++it ) {
QString desc = mFactory->typeName( *it );
if ( !mFactory->typeDescription( *it ).isEmpty() )
desc += QString::fromLatin1(" (") + mFactory->typeDescription( *it ) + QString::fromLatin1(")");
typeDescs.append( desc );
}
return typeDescs;
}
void addObserver( ManagerObserver<T> *observer )
{
mObservers.append( observer );
}
void removeObserver( ManagerObserver<T> *observer )
{
mObservers.remove( observer );
}
private:
void notifyResourceAdded( Resource *res )
{
kdDebug(5650) << "Manager::resourceAdded " << res->resourceName() << endl;
T *resource = dynamic_cast<T *>( res );
if ( resource ) {
ManagerObserver<T> *observer;
for ( observer = mObservers.first(); observer;
observer = mObservers.next() )
observer->resourceAdded( resource );
}
}
void notifyResourceModified( Resource *res )
{
kdDebug(5650) << "Manager::resourceModified " << res->resourceName()
<< endl;
T *resource = dynamic_cast<T *>( res );
if ( resource ) {
ManagerObserver<T> *observer;
for ( observer = mObservers.first(); observer;
observer = mObservers.next() )
observer->resourceModified( resource );
}
}
void notifyResourceDeleted( Resource *res )
{
kdDebug(5650) << "Manager::resourceDeleted " << res->resourceName()
<< endl;
T *resource = dynamic_cast<T *>( res );
if ( resource ) {
ManagerObserver<T> *observer;
for ( observer = mObservers.first(); observer;
observer = mObservers.next() ) {
kdDebug(5650) << "Notifying a observer to Manager..." << endl;
observer->resourceDeleted( resource );
}
}
}
private:
ManagerImpl *mImpl;
Factory *mFactory;
QPtrList<ManagerObserver<T> > mObservers;
};
}
#endif
KRES::Factory::self
static Factory * self(const QString &resourceFamily)
Returns the global resource factory.
Definition: factory.cpp:40
KRES::Manager::resourceTypeNames
QStringList resourceTypeNames() const
Returns a list of the names of all available resource types.
Definition: manager.h:321
KRES::Factory::typeNames
QStringList typeNames() const
Returns a list of all available resource types.
Definition: factory.cpp:75
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::Manager::resourceTypeDescriptions
QStringList resourceTypeDescriptions() const
Return list of descriptions of all available resource types.
Definition: manager.h:329
KRES::Manager::end
Iterator end()
Return Iterator indicating end of resource list.
Definition: manager.h:117
KRES::Manager::Manager
Manager(const QString &family)
Create manager for given resource family.
Definition: manager.h:212
KRES::Manager::removeObserver
void removeObserver(ManagerObserver< T > *observer)
Remove Observer for resource changes from manager.
Definition: manager.h:359
KRES::Manager::createResource
T * createResource(const QString &type)
Creates a new resource of type type with default settings.
Definition: manager.h:313
KRES::Manager::activeEnd
ActiveIterator activeEnd()
Return Iterator indicating end of active resource list.
Definition: manager.h:194
KRES::Factory::resource
Resource * resource(const QString &type, const KConfig *config)
Returns a pointer to a resource object or a null pointer if resource type doesn't exist...
Definition: factory.cpp:128
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::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::readConfig
void readConfig(KConfig *cfg=0)
Recreate Resource objects from configuration file.
Definition: manager.h:230
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::Manager::writeConfig
void writeConfig(KConfig *cfg=0)
Write configuration of Resource objects to configuration file.
Definition: manager.h:239
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:298
KRES::Resource::resourceName
virtual QString resourceName() const
Returns the name of resource.
Definition: resource.cpp:158
KRES::Factory::typeName
QString typeName(const QString &type) const
Returns the name for a special type.
Definition: factory.cpp:110
KRES::Manager::add
void add(Resource *resource)
Add resource to manager.
Definition: manager.h:248
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::Factory::typeDescription
QString typeDescription(const QString &type) const
Returns the description for a special type.
Definition: factory.cpp:119
KRES::Manager::isEmpty
bool isEmpty() const
Return true, if manager doesn't hold any resources.
Definition: manager.h:206

KDE Home | KDE Accessibility Home | Description of Access Keys