summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWolf Bergenheim <ext-wolf.2.bergenheim@nokia.com>2010-02-15 08:56:30 +0200
committerWolf Bergenheim <ext-wolf.2.bergenheim@nokia.com>2010-02-15 08:56:30 +0200
commit5748ff8e81efa6f2b07aac9a023dbda2ca2cdd69 (patch)
tree39794cc6a210a042a9dc5a183f8a3620009d107e
parentdade38119c9fdea35eb2f71cb4e5d43fd7c53a54 (diff)
downloadlibresourceqt-5748ff8e81efa6f2b07aac9a023dbda2ca2cdd69.tar.gz
Integrated Engine and Set and DBusQEventLoop together
-rw-r--r--libresourceqt/include/qt4/policy/resource-set.h37
-rw-r--r--libresourceqt/libresourceqt.pro5
-rw-r--r--libresourceqt/src/resource-engine.cpp8
-rw-r--r--libresourceqt/src/resource-engine.h3
-rw-r--r--libresourceqt/src/resource-set.cpp55
-rw-r--r--tests/test-resource-engine/test-resource-engine.pro4
-rw-r--r--tests/test-resource-set/test-resource-set.pro15
-rw-r--r--tests/test-resource/test-resource.pro2
8 files changed, 91 insertions, 38 deletions
diff --git a/libresourceqt/include/qt4/policy/resource-set.h b/libresourceqt/include/qt4/policy/resource-set.h
index be794d3..ea54b8a 100644
--- a/libresourceqt/include/qt4/policy/resource-set.h
+++ b/libresourceqt/include/qt4/policy/resource-set.h
@@ -31,18 +31,24 @@
* ResourcePolicy::ResourceSet *resources = new ResourcePolicy::ResourceSet("player");
* resources->addResource(audioResource);
* resources->addResource(videoResource);
- * resources->initialize();
- * QObject::connect(resources, SIGNAL(connectedToManager()), this, SLOT(connectedHandler()));
- * resources->connectToManager();
* \endcode
- * Then the when you want to acquire the \ref ResourcePolicy::ResourceSet you simply use the
- * \ref acquire() method of the \ref ResourceSet object, like this:
+ * The resource set now has control over the Resource object pointers. You can
+ * drop them, but should NOT delete them. Instead call the ResourcePolicy::ResourceSet::deleteResource()
+ * method. Then when you want to acquire the \ref ResourcePolicy::ResourceSet
+ * you simply use the \ref ResourcePolicy::ResourceSetacquire() method, like this:
* \code
- * QObject::connect(resources, SIGNAL(resourcesAcquired),
+ * QObject::connect(resources, SIGNAL(resourcesAcquired()),
* this, SLOT(acquireOkHandler(QList<ResourcePolicy::Resource>)));
- * QObject::connect(resources, SIGNAL(resourcesDenied), this, SLOT(acquireDeniedHandler()));
+ * QObject::connect(resources, SIGNAL(resourcesDenied()), this, SLOT(acquireDeniedHandler()));
* resources->acquire();
* \endcode
+ * You should also connect to the ResourcePolicy::ResourceSet::lostResources() signal like this:
+ * \code
+ * QObject::connect(resources, SIGNAL(lostResources()),
+ * this, SLOT(lostResources()));
+ * \endcode
+ * This signal tells you when you should stop using the resources you have asked for.
+ * So it is important that you connect to it.
*/
/**
@@ -50,6 +56,7 @@
*/
namespace ResourcePolicy
{
+class ResourceEngine;
/**
* The resourceSet repesents a set of attributes. Each set can only contain
* a single Resource of a given type. That is one AudioPlaybackResource, etc.
@@ -73,12 +80,6 @@ public:
* The destructor
*/
~ResourceSet();
- /**
- * Finalizes the ResourceSet. This method should be called after all
- * resources have been added to the set.
- * \return true if the finalization was successful.
- */
- bool finalize();
/**
* This method adds a resource to the set. A set contains only a single
@@ -168,6 +169,7 @@ public:
/**
* Sets that the resourcesGranted() signal is emited even if we already
* have the requested resources granted. By default this feature is off.
+ * This flag should be set once only before finalize is called, and cannot be unset.
*/
void setAlwaysReply();
@@ -193,16 +195,23 @@ signals:
/**
* This signal is emited when some other program with a higher priority
* superseeds us, and as a result we loose our resources.
+ * It is very important to connect to this signal as it is signaling when
+ * the acquired resources can't be used anymore.
*/
void lostResources();
private:
+
+ bool initialize();
+
quint32 identifier;
const QString resourceClass;
Resource* resourceSet[NumberOfTypes];
- bool connected;
+ ResourceEngine *resourceEngine;
bool autoRelease;
bool alwaysReply;
+ bool initialized;
+ bool pendingAcquire;
};
}
diff --git a/libresourceqt/libresourceqt.pro b/libresourceqt/libresourceqt.pro
index a80eac8..1fa80c2 100644
--- a/libresourceqt/libresourceqt.pro
+++ b/libresourceqt/libresourceqt.pro
@@ -3,7 +3,7 @@ TEMPLATE = lib
TARGET = resourceqt
DESTDIR = build
DEPENDPATH += $${POLICY} src
-INCLUDEPATH += $${LIBRESOURCEINC} src
+INCLUDEPATH += $${LIBRESOURCEINC} $${LIBDBUSQEVENTLOOP} src
# Input
PUBLIC_HEADERS = $${POLICY}/resource.h $${POLICY}/resource-set.h $${POLICY}/resources.h
@@ -15,6 +15,9 @@ SOURCES += src/resource.cpp \
src/resource-engine.cpp \
src/resources.cpp
+QMAKE_CXXFLAGS += -Wall
+LIBS += -L$${LIBDBUSQEVENTLOOP}/build -ldbus-qeventloop
+
OBJECTS_DIR = build
MOC_DIR = build
diff --git a/libresourceqt/src/resource-engine.cpp b/libresourceqt/src/resource-engine.cpp
index bc29de9..4d877e3 100644
--- a/libresourceqt/src/resource-engine.cpp
+++ b/libresourceqt/src/resource-engine.cpp
@@ -14,8 +14,8 @@ static void handleAdviceMessage(resmsg_t *msg, resset_t *rs, void *data);
ResourceEngine::ResourceEngine(ResourceSet *resourceSet)
: QObject(resourceSet), connected(false), resourceSet(resourceSet),
- libresourceConnection(NULL), libresourceSet(NULL), requestId(0),
- messageMap(), mode(0)
+ dbusEngine(NULL), libresourceConnection(NULL), libresourceSet(NULL),
+ requestId(0), messageMap(), mode(0)
{
}
@@ -27,6 +27,9 @@ bool ResourceEngine::initialize()
{
DBusError dbusError;
DBusConnection *dbusConnection;
+ dbusEngine = new DBUSConnectionEventLoop;
+ if(dbusEngine == NULL)
+ return false;
dbus_error_init(&dbusError);
dbusConnection = dbus_bus_get(DBUS_BUS_SYSTEM, &dbusError);
@@ -36,6 +39,7 @@ bool ResourceEngine::initialize()
return false;
}
dbus_error_free(&dbusError);
+ dbusEngine->addConnection(dbusConnection);
libresourceConnection = resproto_init(RESPROTO_ROLE_CLIENT, RESPROTO_TRANSPORT_DBUS,
connectionIsUp, dbusConnection);
if (libresourceConnection == NULL) {
diff --git a/libresourceqt/src/resource-engine.h b/libresourceqt/src/resource-engine.h
index 72b567d..a3693e9 100644
--- a/libresourceqt/src/resource-engine.h
+++ b/libresourceqt/src/resource-engine.h
@@ -7,6 +7,7 @@
#include <dbus/dbus.h>
#include <res-conn.h>
#include <policy/resource-set.h>
+#include <dbusconnectioneventloop.h>
namespace ResourcePolicy {
@@ -55,9 +56,11 @@ signals:
void disconnectedFromManager();
private:
+
bool connected;
ResourceSet *resourceSet;
DBusConnection *dbusConnection;
+ DBUSConnectionEventLoop *dbusEngine;
resconn_t *libresourceConnection;
resset_t *libresourceSet;
quint32 requestId;
diff --git a/libresourceqt/src/resource-set.cpp b/libresourceqt/src/resource-set.cpp
index dc58828..80974d2 100644
--- a/libresourceqt/src/resource-set.cpp
+++ b/libresourceqt/src/resource-set.cpp
@@ -1,11 +1,12 @@
#include <policy/resource-set.h>
+#include "resource-engine.h"
using namespace ResourcePolicy;
ResourceSet::ResourceSet(const QString &applicationClass, QObject * parent)
- : QObject(parent), resourceClass(applicationClass), autoRelease(false),
- alwaysReply(false)
-
+ : QObject(parent), resourceClass(applicationClass), resourceEngine(NULL),
+ autoRelease(false), alwaysReply(false), initialized(false),
+ pendingAcquire(false)
{
identifier = (quint32)this;
memset(resourceSet, 0, sizeof(QPointer<Resource *>)*NumberOfTypes);
@@ -18,9 +19,26 @@ ResourceSet::~ResourceSet()
}
}
-bool ResourceSet::finalize()
+bool ResourceSet::initialize()
{
- return false;
+ resourceEngine = new ResourceEngine(this);
+ if (resourceEngine == NULL) {
+ return false;
+ }
+ if (!resourceEngine->initialize()) {
+ return false;
+ }
+ if (!resourceEngine->connect()) {
+ return false;
+ }
+ QObject::connect(resourceEngine, SIGNAL(connectedToManager()),
+ this, SLOT(connectedHandler()));
+ QObject::connect(resourceEngine, SIGNAL(resourcesAcquired(quint32)),
+ this, SLOT(handleAcquire(quint32)));
+ QObject::connect(resourceEngine, SIGNAL(resourcesDenied()),
+ this, SLOT(handleDeny()));
+
+ return true;
}
void ResourceSet::addResource(const Resource *resource)
@@ -28,6 +46,7 @@ void ResourceSet::addResource(const Resource *resource)
if ((resource->type() == AudioPlaybackType) || (resource->type() == AudioRecorderType)) {
qDebug("audioResource...");
}
+ delete resourceSet[resource->type()];
resourceSet[resource->type()] = resource->clone();
}
@@ -40,10 +59,8 @@ void ResourceSet::addResources(const QList<Resource *>resources)
void ResourceSet::deleteResource(ResourceType type)
{
- if (contains(type)) {
- delete resourceSet[type];
- resourceSet[type] = NULL;
- }
+ delete resourceSet[type];
+ resourceSet[type] = NULL;
}
bool ResourceSet::contains(ResourceType type) const
@@ -76,8 +93,7 @@ QList<Resource *> ResourceSet::resources() const
QList<Resource *> listOfResources;
for (int i = 0; i < NumberOfTypes; i++) {
if (resourceSet[i] != NULL) {
- Resource *clone = resourceSet[i]->clone();
- listOfResources.append(clone);
+ listOfResources.append(resourceSet[i]);
}
}
return listOfResources;
@@ -85,15 +101,22 @@ QList<Resource *> ResourceSet::resources() const
Resource * ResourceSet::resource(ResourceType type) const
{
- if (contains(type))
- return resourceSet[type]->clone();
- else
- return NULL;
+ return resourceSet[type];
}
bool ResourceSet::acquire()
{
- return false;
+ if(!initialized) {
+ pendingAcquire = true;
+ return initialize();
+ }
+ else if (!resourceEngine->isConnected()) {
+ pendingAcquire = true;
+ resourceEngine->connect();
+ }
+ else {
+ return resourceEngine->acquireResources();
+ }
}
bool ResourceSet::release()
diff --git a/tests/test-resource-engine/test-resource-engine.pro b/tests/test-resource-engine/test-resource-engine.pro
index 2c0d4a7..d558bc2 100644
--- a/tests/test-resource-engine/test-resource-engine.pro
+++ b/tests/test-resource-engine/test-resource-engine.pro
@@ -3,7 +3,7 @@ TEMPLATE = app
TARGET = test-resource-engine
DESTDIR = build
DEPENDPATH += $${POLICY} $${LIBRESOURCEQT}/src .
-INCLUDEPATH += $${LIBRESOURCEQT}/src $${LIBRESOURCEINC} /usr/include/resource
+INCLUDEPATH += $${LIBRESOURCEQT}/src $${LIBRESOURCEINC} $${LIBDBUSQEVENTLOOP} /usr/include/resource
# Input
HEADERS += $${POLICY}/resource.h \
@@ -20,6 +20,8 @@ SOURCES += $${LIBRESOURCEQT}/src/resource.cpp \
OBJECTS_DIR = build
MOC_DIR = build
+QMAKE_CXXFLAGS += -Wall
+LIBS += -L$${LIBDBUSQEVENTLOOP}/build -ldbus-qeventloop
CONFIG += qt qtestlib debug warn_on link_pkgconfig
QT -= gui
diff --git a/tests/test-resource-set/test-resource-set.pro b/tests/test-resource-set/test-resource-set.pro
index aa3ebca..84bed87 100644
--- a/tests/test-resource-set/test-resource-set.pro
+++ b/tests/test-resource-set/test-resource-set.pro
@@ -3,18 +3,25 @@ TEMPLATE = app
TARGET = test-resource-set
DESTDIR = build
DEPENDPATH += $${POLICY} $${BASE}/src .
-INCLUDEPATH += $${LIBRESOURCEQT}/src $${LIBRESOURCEINC}
+INCLUDEPATH += $${LIBRESOURCEQT}/src $${LIBRESOURCEINC} $${LIBDBUSQEVENTLOOP}
# Input
-HEADERS += $${POLICY}/resources.h $${POLICY}/resource-set.h test-resource-set.h
+HEADERS += $${POLICY}/resources.h $${POLICY}/resource-set.h test-resource-set.h \
+ $${LIBRESOURCEQT}/src/resource-engine.h
+
SOURCES += $${LIBRESOURCEQT}/src/resource.cpp $${LIBRESOURCEQT}/src/resources.cpp \
- $${LIBRESOURCEQT}/src/resource-set.cpp test-resource-set.cpp
+ $${LIBRESOURCEQT}/src/resource-engine.cpp \
+ $${LIBRESOURCEQT}/src/resource-set.cpp test-resource-set.cpp
OBJECTS_DIR = build
MOC_DIR = build
-CONFIG += qt qtestlib debug warn_on
+QMAKE_CXXFLAGS += -Wall
+LIBS += -L$${LIBDBUSQEVENTLOOP}/build -ldbus-qeventloop
+
+CONFIG += qt qtestlib debug warn_on link_pkgconfig
QT -= gui
+PKGCONFIG += dbus-1 libresource0
# Install directives
INSTALLBASE = /usr
diff --git a/tests/test-resource/test-resource.pro b/tests/test-resource/test-resource.pro
index c6ec6ab..b103412 100644
--- a/tests/test-resource/test-resource.pro
+++ b/tests/test-resource/test-resource.pro
@@ -12,6 +12,8 @@ SOURCES += $${LIBRESOURCEQT}/src/resource.cpp $${LIBRESOURCEQT}/src/resources.c
OBJECTS_DIR = build
MOC_DIR = build
+QMAKE_CXXFLAGS += -Wall
+
CONFIG += qt qtestlib debug warn_on
QT -= gui