home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / kresources / manager.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-11-08  |  11.7 KB  |  423 lines

  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 <qdict.h>
  28. #include <qstringlist.h>
  29.  
  30. #include <kdebug.h>
  31. #include <kresources/factory.h>
  32. #include <kresources/managerimpl.h>
  33.  
  34. namespace KRES {
  35.  
  36. class Resource;
  37.  
  38. /**
  39.   Observer class for Manager class. For getting notified about changes of
  40.   Resources managed by a Manager object implement this Observer interface and
  41.   add an object of this implementation to the Manager using addObserver(). The
  42.   resourceAdded(), resourceModified() and resourceDeleted() functions of your
  43.   implementation will be called whenever resources managed by the Manager object
  44.   are added, modified or deleted.
  45. */
  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.  
  55. /**
  56.   @internal
  57. */
  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.  
  66. /**
  67.   This class provides a manager for resources of a specified family. It takes
  68.   care of loading and saving resource configurations and provides access to the
  69.   resources and their attributes. External changes in the resource configuration
  70.   are notified by the ManagerObserver interface. If your application needs to be
  71.   notified about resource configuration changes, you have to subclass
  72.   ManagerObserver and add it to the Manager by addObserver().
  73.  
  74.   Since KDE 3.4 it's required to supply your application with a *.desktop
  75.   file for each resource family you introduce. The ServiceType should be of
  76.   KResources/Manager.
  77. */
  78. template<class T>
  79. class Manager : private ManagerNotifier
  80. {
  81.   public:
  82.     /**
  83.       Iterator for iterations over all resources managed by a manager.
  84.     */
  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.  
  104.     /**
  105.       Return Iterator on first resource. If there is no resource returns end().
  106.     */
  107.     Iterator begin()
  108.     {
  109.       Iterator it;
  110.       it.mIt = mImpl->resourceList()->begin();
  111.       return it;
  112.     }
  113.  
  114.     /**
  115.       Return Iterator indicating end of resource list.
  116.     */
  117.     Iterator end()
  118.     {
  119.       Iterator it;
  120.       it.mIt = mImpl->resourceList()->end();
  121.       return it;
  122.     }
  123.  
  124.     /**
  125.       Iterator for iterations over only active resources managed by a manager.
  126.     */
  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:
  163.         /**
  164.           Check if iterator needs to be advanced once more.
  165.         */
  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.  
  176.     /**
  177.       Return Iterator on first active resource. If there is no active resource
  178.       returns end().
  179.     */
  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.  
  191.     /**
  192.       Return Iterator indicating end of active resource list.
  193.     */
  194.     ActiveIterator activeEnd()
  195.     {
  196.       ActiveIterator it;
  197.       it.mIt = mImpl->resourceList()->end();
  198.       it.mList = mImpl->resourceList();
  199.       return it;
  200.     }
  201.  
  202.     /**
  203.       Return true, if manager doesn't hold any resources. If there are resources
  204.       return false.
  205.     */
  206.     bool isEmpty() const { return mImpl->resourceList()->isEmpty(); }
  207.  
  208.     /**
  209.       Create manager for given resource family. The family argument is used as
  210.       identifier for loading and saving resource configurations.
  211.     */
  212.     Manager( const QString &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.  
  226.     /**
  227.       Recreate Resource objects from configuration file. If cfg is 0, read
  228.       standard configuration file determined by family name.
  229.     */
  230.     void readConfig( KConfig *cfg = 0 )
  231.     {
  232.       mImpl->readConfig( cfg );
  233.     }
  234.  
  235.     /**
  236.       Write configuration of Resource objects to configuration file. If cfg is
  237.       0, write to standard configuration file determined by family name.
  238.     */
  239.     void writeConfig( KConfig *cfg = 0 )
  240.     {
  241.       mImpl->writeConfig( cfg );
  242.     }
  243.  
  244.     /**
  245.       Add resource to manager. This passes ownership of the Resource object
  246.       to the manager.
  247.     */
  248.     void add( Resource *resource )
  249.     {
  250.       if ( resource ) mImpl->add( resource );
  251.     }
  252.  
  253.     /**
  254.       Remove resource from manager. This deletes the Resource object.
  255.     */
  256.     void remove( Resource *resource )
  257.     {
  258.       if ( resource ) mImpl->remove( resource );
  259.     }
  260.  
  261.     /**
  262.       Call this to notify manager about changes of the configuration of the
  263.       given resource.
  264.     */
  265.     void change( T *resource )
  266.     {
  267.       mImpl->change( resource );
  268.     }
  269.  
  270.     /**
  271.       Return standard resource.
  272.     */
  273.     T *standardResource()
  274.     {
  275.       return static_cast<T *>( mImpl->standardResource() );
  276.     }
  277.  
  278.     /**
  279.       Set standard resource.
  280.     */
  281.     void setStandardResource( T *resource )
  282.     {
  283.       if ( resource ) mImpl->setStandardResource( resource );
  284.     }
  285.  
  286.     /**
  287.       Set active state of resource.
  288.     */
  289.     void setActive( Resource *resource, bool active )
  290.     {
  291.       if ( resource ) mImpl->setActive( resource, active );
  292.     }
  293.  
  294.     /**
  295.       Returns a list of the names of the resources managed by the
  296.       Manager for this family.
  297.     */
  298.     QStringList resourceNames() const
  299.     {
  300.       return mImpl->resourceNames();
  301.     }
  302.  
  303.     /**
  304.       Creates a new resource of type @p type with default
  305.       settings. The resource is
  306.       not added to the manager, the application has to do that.
  307.       Returns a pointer to a resource object or a null pointer
  308.       if resource type doesn't exist.
  309.  
  310.       @param type   The type of the resource, one of those returned
  311.                     by resourceTypeNames()
  312.     */
  313.     T *createResource( const QString& type )
  314.     {
  315.       return dynamic_cast<T *>( mFactory->resource( type, 0 ) );
  316.     }
  317.  
  318.     /**
  319.       Returns a list of the names of all available resource types.
  320.     */
  321.     QStringList resourceTypeNames() const
  322.     {
  323.       return mFactory->typeNames();
  324.     }
  325.  
  326.     /**
  327.       Return list of descriptions of all available resource types.
  328.     */
  329.     QStringList resourceTypeDescriptions() const
  330.     {
  331.       QStringList typeDescs;
  332.       QStringList types = mFactory->typeNames();
  333.  
  334.       for ( QStringList::ConstIterator it = types.begin(); it != types.end();
  335.             ++it ) {
  336.         QString desc = mFactory->typeName( *it );
  337.         if ( !mFactory->typeDescription( *it ).isEmpty() )
  338.           desc += QString::fromLatin1(" (") + mFactory->typeDescription( *it ) + QString::fromLatin1(")");
  339.  
  340.         typeDescs.append( desc );
  341.       }
  342.  
  343.       return typeDescs;
  344.     }
  345.  
  346.     /**
  347.       Add observer for resource changes to manager. See ManagerObserver. The
  348.       Manager does not take ownership of the Observer object.
  349.     */
  350.     void addObserver( ManagerObserver<T> *observer )
  351.     {
  352.       mObservers.append( observer );
  353.     }
  354.  
  355.     /**
  356.       Remove Observer for resource changes from manager. See ManagerObserver.
  357.       The Observer is not deleted by the Manager after being removed.
  358.     */
  359.     void removeObserver( ManagerObserver<T> *observer )
  360.     {
  361.       mObservers.remove( observer );
  362.     }
  363.  
  364.   private:
  365.     /**
  366.       Implementation of the ManagerNotifier interface.
  367.     */
  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.  
  380.     /**
  381.       Implementation of the ManagerNotifier interface.
  382.     */
  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.  
  396.     /**
  397.       Implementation of the ManagerNotifier interface.
  398.     */
  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.     QPtrList<ManagerObserver<T> > mObservers;
  418. };
  419.  
  420. }
  421.  
  422. #endif
  423.