diff options
author | Wolf Bergenheim <ext-wolf.2.bergenheim@nokia.com> | 2010-01-29 17:20:56 +0200 |
---|---|---|
committer | Wolf Bergenheim <ext-wolf.2.bergenheim@nokia.com> | 2010-01-29 17:20:56 +0200 |
commit | 0746bbd252fe13ce3c4ae523fc28b0cf27314c05 (patch) | |
tree | 323b3406c6f893f21c7c7820532c0df2a61881cc /include | |
parent | 27c45e867533ad43bc2b2e64fe577a503b2d05a8 (diff) |
Refactored Resource into multiple classes
Diffstat (limited to 'include')
-rw-r--r-- | include/resource-factory.h | 38 | ||||
-rw-r--r-- | include/resource-set.h | 290 | ||||
-rw-r--r-- | include/resource.h | 170 |
3 files changed, 217 insertions, 281 deletions
diff --git a/include/resource-factory.h b/include/resource-factory.h deleted file mode 100644 index 22de00a..0000000 --- a/include/resource-factory.h +++ /dev/null @@ -1,38 +0,0 @@ -#ifndef RESOURCEFACTORY_H -#define RESOURCEFACTORY_H - -#include <QObject> -#include <QDBusConnection> -#include "resource-types.h" - -class Resource; - -/** This class is a helper class which should be used to create Resources. Note - * that it will own all resources, and so should not be deleted before all - * resources are ready to be released. The best way to acchieve this is by - * makeing the ResourceHandler class own the factory. - */ -class ResourceFactory: public QObject -{ - Q_OBJECT - Q_DISABLE_COPY( ResourceFactory ); -private: - //! \internal - QDBusConnection sessionBusConnection; -public: - ResourceFactory(QObject *parent = 0); - ~ResourceFactory(); - /** This method creates a new \ref Resource object. - * @param applicationClass The application class to tell the Resource Policy. - * @param requestedResources A bit mask of the resources tha this application - * wats as a set. If more than one resource is set in this bit mask, then - * that set is treated as a whole, and cannot be separated. If the application - * needs at times say only audio and at times audio and video, then several - * \ref Resource objects need to be created. - * @return Returns a pointer to a new Resource, or NULL if there is an error. - */ - Resource * createResource(enum ResourceTypes::ResourceClass applicationClass, - quint16 requestedResources=ResourceTypes::AudioResource); -}; - -#endif diff --git a/include/resource-set.h b/include/resource-set.h index 8d94a44..1f515ee 100644 --- a/include/resource-set.h +++ b/include/resource-set.h @@ -1,7 +1,7 @@ #ifndef RESOURCE_SET_H #define RESOURCE_SET_H -#include "resource.h" +#include "resources.h" #include <QString> #include <QObject> #include <QVector> @@ -22,15 +22,15 @@ * \ref Resource objects like this (given as an example of what a media player * might want/need): * \code - * ResourcePolicy::Resource audioResource(AudioPlaybackResource); - * ResourcePolicy::Resource videoResource(VideoPlaybackResource); - * videoResource.setOptional(); + * ResourcePolicy::AudioResource *audioResource = new ResourcePolicy::AudioResource(); + * ResourcePolicy::VideoResource *audioResource = new ResourcePolicy::AudioResource(); + * videoResource->setOptional(); * \endcode * Then you need to create a \ref ResourcePolicy::ResourceSet like this: * \code * ResourcePolicy::ResourceSet *resources = new ResourcePolicy::ResourceSet("player"); - * resources.addResource(audioResource); - * resources.addResource(videoResource); + * resources->addResource(audioResource); + * resources->addResource(videoResource); * resources->initialize(); * QObject::connect(resources, SIGNAL(connectedToManager), this, SLOT(connectedHandler())); * resources->connectToManager(); @@ -50,147 +50,157 @@ */ namespace ResourcePolicy { - /** - * The resourceSet repesents a set of attributes. Each set can only contain - * a single Resource of a given type. That is one AudioPlaybackResource, etc. - * - * Internally the set is stored as a QVector of \ref Resource objects. - */ - class ResourceSet: public QObject - { - Q_OBJECT - Q_DISABLE_COPY( ResourceSet ) - public: - /** - * The constructor. - * \param applicationClass This parameter defines the application class. - * The application class is used to determine the priority order of the - * application. - * \param parent The optional parent of of this class. - */ - ResourceSet(const QString &applicationClass, QObject *parent=NULL); - /** - * The destructor - */ - ~ResourceSet(); - /** - * Initializes the underlaying communications library. - * \return true if initialization is successful. - */ - bool initialize(); - - /** - * This method adds a resource to the set. A set contains only a single - * instance of a given resource. If the ResourceSet already contains a - * resource of the given type it will be overridden. - * \param resource The resource to add to the set. - */ - void addResource(const Resource &resource); - /** - * This method adds (and replaces) the list of resource to the set. - * \param resources The list of resources to add to the set. - */ - void addResources(const QList<Resource> &resources); - /** - * This method clears the old resources and replaces the set with the - * resources from the given list. - * \param resources The list of resources that the set should contain. - */ - void setResources(const QList<Resource> &resources); + /** + * The resourceSet repesents a set of attributes. Each set can only contain + * a single Resource of a given type. That is one AudioPlaybackResource, etc. + * + * Internally the set is stored as a QVector of \ref Resource objects. + */ + class ResourceSet: public QObject + { + Q_OBJECT + Q_DISABLE_COPY( ResourceSet ) + public: + /** + * The constructor. + * \param applicationClass This parameter defines the application class. + * The application class is used to determine the priority order of the + * application. + * \param parent The optional parent of of this class. + */ + ResourceSet(const QString &applicationClass, QObject *parent=NULL); + /** + * The destructor + */ + ~ResourceSet(); + /** + * Initializes the underlaying communications library. + * \return true if initialization is successful. + */ + bool initialize(); - /** - * This method returns a list of all resource in the set. - * \return a QList of all resources in the set. - */ - QList<Resource> resources(); + /** + * This method adds a resource to the set. A set contains only a single + * instance of a given resource. If the ResourceSet already contains a + * resource of the given type it will be overridden. + * \param resource The resource to add to the set. A copy of this object + * is stored in the Set. + */ + void addResource(const Resource *resource); + /** + * This method adds all resources in the list to the set. + * A set contains only a single instance of a given resource. If the + * ResourceSet already contains a resource of the given type it will be + * overridden. + * \param resources The list of resources to add to the set. These will + * be copied. + */ + void addResources(const QList<Resource *>resources); + /** + * This method removes the resource of the given type + * \param type The type of the resource to remove from the set. + */ + void delResource(ResourceType type); - /** - * Checks if the \ref ResourceSet contains the given \ref Resource - * \param resource The Resource to look for - * \return true if the \ref Resource is defined in this \ref ResourceSet - */ - bool contains(const Resource &resource) const; + /** + * This method returns a list of all resource in the set. + * \return a QList of all resources in the set. + */ + QList<Resource *> resources(); + /** + * This method returns a const pointer to a resource of a specific type. + * \type The type of resource we are interested in. + * \return a pointer to the Resource if it is defined NULL otherwise. + */ + Resource * resource(ResourceType type) const; + /** + * Checks if the \ref ResourceSet contains the given \ref Resource + * \param type The Resource to look for + * \return true if the \ref Resource is defined in this \ref ResourceSet + */ + bool contains(ResourceType type) const; - /** - * Checks if the \ref ResourceSet contains all given resources. - * \param resources A list of resources to check for - * \return true if \b all given resources are defined in the ResourceSet. - */ - bool contains(const QList<Resource> &resources) const; + /** + * Checks if the \ref ResourceSet contains all given resources. + * \param types A list of resources to check for + * \return true if \b all given resources are defined in the ResourceSet. + */ + bool contains(const QList<ResourceType> &types) const; - quint32 id() const; + quint32 id() const; - /** - * Connects to the Resource Policy Manager. The connected() signal is sent - * when the connection has been established. Acquiring the ResourceSet can - * be attempted at any time after this, but not before. - * \return true if the connection request was successfully sent. - * \param reconnectOnDisconnect. Set to true to automatically reconnect on - * lost connection to the Policy Resource Manager. This optional parameter - * defaults to true. - */ - bool connectToManager(bool reconnectOnDisconnect); - /** - * Disconnects from the Resource Policy Manager. Further attempts at acquiring - * the \ref ResourceSet will fail. - */ - void disconnectFromManager(); - /** - * Checks whether the ResourceSet is connected or not. - */ - bool isConnectedToManager(); - /** - * Attempt to acquire the ResourceSet. The response is returned as the - * resourcesAcquired() signal. - */ - bool acquire(); - /** - * Release the acquired resources. - */ - bool release(); - /** - * Commit changes to the \ref ResourceSet to the Manager. - */ - bool update(); + /** + * Connects to the Resource Policy Manager. The connected() signal is sent + * when the connection has been established. Acquiring the ResourceSet can + * be attempted at any time after this, but not before. + * \return true if the connection request was successfully sent. + * \param reconnectOnDisconnect. Set to true to automatically reconnect on + * lost connection to the Policy Resource Manager. This optional parameter + * defaults to true. + */ + bool connectToManager(bool reconnectOnDisconnect); + /** + * Disconnects from the Resource Policy Manager. Further attempts at acquiring + * the \ref ResourceSet will fail. + */ + void disconnectFromManager(); + /** + * Checks whether the ResourceSet is connected or not. + */ + bool isConnectedToManager(); + /** + * Attempt to acquire the ResourceSet. The response is returned as the + * resourcesAcquired() signal. + */ + bool acquire(); + /** + * Release the acquired resources. + */ + bool release(); + /** + * Commit changes to the \ref ResourceSet to the Manager. + */ + bool update(); - signals: - /** - * This signal is emited when the Resource Policy Manager notifies that - * the given resources have become available. - * \param availableResources A list of available resources. - */ - void resourcesBecameAvailable(QList<Resource> availableResources); - /** - * This signal is emited as a response to the acquire() request. - * \param grantedResources The list of granted resources. - */ - void resourcesAcquired(QList<Resource> grantedResources); - /** - * This signal is emited as a response to the acquire() request, in the - * case where we are not granted any requests. - */ - void resourcesDenied(); - /** - * This signal is emited when some other program with a higher priority - * superseeds us, and as a result we loose our resources. - */ - void lostResources(); - /** - * This signal is emited when we have successfully connected to the manager. - */ - void connectedToManager(); - /** - * This signal is emited when we loose the connection to the manager. - * A reconnect is automatically attempted. - */ - void disconnectedFromManager(); - + signals: + /** + * This signal is emited when the Resource Policy Manager notifies that + * the given resources have become available. + * \param availableResources A list of available resources. + */ + void resourcesBecameAvailable(QList<Resource *> availableResources); + /** + * This signal is emited as a response to the acquire() request. + * \param grantedResources The list of granted optional resources. + */ + void resourcesGranted(QList<Resource *> grantedOptionalResources); + /** + * This signal is emited as a response to the acquire() request, in the + * case where we are not granted any requests. + */ + void resourcesDenied(); + /** + * This signal is emited when some other program with a higher priority + * superseeds us, and as a result we loose our resources. + */ + void lostResources(); + /** + * This signal is emited when we have successfully connected to the manager. + */ + void connectedToManager(); + /** + * This signal is emited when we loose the connection to the manager. + * A reconnect is automatically attempted. + */ + void disconnectedFromManager(); - private: - quint32 identifier; - const QString applicationClass; - QVector<Resource> resourceSet; - }; + + private: + quint32 identifier; + const QString applicationClass; + Resource* resourceSet[NumberOfTypes]; + }; } #endif + diff --git a/include/resource.h b/include/resource.h index ef3290c..13265d5 100644 --- a/include/resource.h +++ b/include/resource.h @@ -3,114 +3,78 @@ #include <QtCore> - namespace ResourcePolicy { - /** - * This enum defines the different resources that an application can use. - */ - enum ResourceType { - InvalidResource = 0, - AudioPlaybackResource, - VideoPlaybackResource, - AudioRecorderResource, - VideoRecorderResource, - ResourceGuard - }; + enum ResourceType { + AudioPlaybackType = 0, + VideoPlaybackType, + AudioRecorderType, + VideoRecorderType, + VibraType, + LedsType, + BacklightType, + SystemButtonType, + LockButtonType, + ScaleButtonType, + SnapButtonType, + LensCoverType, + NumberOfTypes + }; - /** - * This class represents a resource which can be acquired. The main use of - * Resources is to use then in a ResourceSet. - * - * Other Resource types can be added later, these reflect the resources found - * in the Policy configuration. - * - * \b Examples: - * This example shows how to make a resource which represents an optional - * audio requirement. - * \code - * Resource audioResource(AudioPlaybackResource); - * audioResource->setOptional(); - * \endcode - * This example shows how to create the resources used by a video player - * \code - * Resource audioResource(AudioPlaybackResource); - * Resource videoResource(VideoPlaybackResource); - * \endcode + /** + * This class is the super class for all resources. It represents a generic + * \ref Resource. The type specific resource classes should be used. */ - class Resource - { - public: - /** - * Constructor. Creates a new identifier. The new identifier defaults to - * not optional, and not shared. The default type is undefined and - * \ref setType() needs to be called to set the type. Optionally a - * resource type may be given to the constructor, to avoid having to - * call the setType() method. - */ - Resource(enum ResourceType type=InvalidResource); - /** - * Copy constructor. Copies also the identifier. - * \param other The source to copy from. - */ - Resource(const Resource &other); - /** - * Assignement operator. Works like the copy constructor. - * \param other The source to assign from. - */ - Resource &operator=(const Resource &other); - //! Destructor. - ~Resource(); + class Resource + { + public: + /** + * Whether or not this resource is optional, in that it doesn't need to + * be available for the set to be acquired. + * \return true when this resource is optional. + */ + bool isOptional() const; + /** + * Set the resource to be optional or mandatory. + * \param resourceIsOptional This optional parameter defaults to true. + * The default, true, results in the resource becoming optional. When it + * is set to false the resource becomes mandatory. + */ + void setOptional(bool resourceIsOptional=true); + /** + * Whether or not the resource to be shared. If it is shared then other + * programs are allowed to share this resource. + * \return true when this resource is shared. + */ + bool isShared() const; + /** + * Sets the resource to be shared/private. + * \param resourceIsShared This optional parameter defaults to true. + * When it is set to true (de default) the resource is shared with + * other programs. Setting it to false makes this resource exclusive. + */ + void setShared(bool resourceIsShared=true); + /** + * Whether or not this resource has been granted. + * \return true if this resource has been granted. + */ + bool isGranted() const; + void setGranted(); + void unsetGranted(); - /** - * Returns the type of the resource. - * \return the type of the resource. - */ - ResourceType type() const; - /** - * Sets the resource type. - * \param type The type to set to. - * \return true if the type is valid. - */ - bool setType(enum ResourceType type); - /** - * Whether or not this resource is optional, in that it doesn't need to - * be available for the set to be acquired. - * \return true when this resource is optional. - */ - bool isOptional() const; - /** - * Set the resource to be optional or mandatory. - * \param resourceIsOptional This optional parameter defaults to true. - * The default, true, results in the resource becoming optional. When it - * is set to false the resource becomes mandatory. - */ - void setOptional(bool resourceIsOptional=true); - /** - * Whether or not the resource to be shared. If it is shared then other - * programs are allowed to share this resource. - * \return true when this resource is shared. - */ - bool isShared() const; - /** - * Sets the resource to be shared/private. - * \param resourceIsShared This optional parameter defaults to true. - * When it is set to true (de default) the resource is shared with - * other programs. Setting it to false makes this resource exclusive. - */ - void setShared(bool resourceIsShared=true); - /** - * Compares two resources and returns true if both are of the same \b type. - * \param other The resrouce to compare to. - * \return true if the types of both resources are of the same type. - */ - bool operator==(const Resource &other) const; - private: - ResourceType resourceType; - bool optional; - bool shared; - quint32 identifier; - }; + virtual ResourceType type() const = 0; + virtual Resource * clone() const = 0; + virtual ~Resource(); + protected: + Resource(); + Resource(const Resource &other); + ResourceType resourceType; + bool optional; + bool shared; + quint32 identifier; + bool granted; + }; } #endif + |