summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWolf Bergenheim <ext-wolf.2.bergenheim@nokia.com>2009-12-30 16:00:09 +0200
committerWolf Bergenheim <ext-wolf.2.bergenheim@nokia.com>2009-12-30 16:00:09 +0200
commit9504f0418dd0f3a9e6dff6034ffe36196c473a06 (patch)
treeb91b56455e0541b03f018df99c9ea95865621f9a
downloadlibresourceqt-9504f0418dd0f3a9e6dff6034ffe36196c473a06.tar.gz
Initial commit
-rw-r--r--include/resource-factory.h22
-rw-r--r--include/resource-types.h24
-rw-r--r--include/resource.h50
-rw-r--r--libresourceqt.pro34
-rw-r--r--src/libplayback-wrapper.cpp96
-rw-r--r--src/libplayback-wrapper.h26
-rw-r--r--src/resource-factory.cpp26
-rw-r--r--src/resource-library.h11
-rw-r--r--src/resource.cpp42
-rw-r--r--tests/mock-resource-library.cpp20
-rw-r--r--tests/mock-resource-library.h19
-rw-r--r--tests/test-resource.cpp44
-rw-r--r--tests/test-resource.h25
-rw-r--r--tests/test-resource.pro21
14 files changed, 460 insertions, 0 deletions
diff --git a/include/resource-factory.h b/include/resource-factory.h
new file mode 100644
index 0000000..90131c9
--- /dev/null
+++ b/include/resource-factory.h
@@ -0,0 +1,22 @@
+#ifndef RESOURCEFACTORY_H
+#define RESOURCEFACTORY_H
+
+#include <QObject>
+#include <QDBusConnection>
+#include "resource-types.h"
+
+class ResourceLibrary;
+class Resource;
+
+class ResourceFactory: public QObject
+{
+ Q_OBJECT
+private:
+ QDBusConnection sessionBusConnection;
+public:
+ ResourceFactory(QObject *parent = 0);
+ ~ResourceFactory();
+ Resource * createResource(enum ResourceClass type, quint16 usageFlags=RP_FLAGS_AUDIO);
+};
+
+#endif
diff --git a/include/resource-types.h b/include/resource-types.h
new file mode 100644
index 0000000..5732251
--- /dev/null
+++ b/include/resource-types.h
@@ -0,0 +1,24 @@
+#ifndef RESOURCE_TYPES_H
+#define RESOURCE_TYPES_H
+enum ResourceClass {
+ InvalidClass = 0,
+ VoIPClass,
+ CallClass = VoIPClass,
+ MediaClass,
+ BackgroundClass,
+ RingtoneClass,
+ VoiceUIClass,
+ CameraClass,
+ GameClass,
+ AlarmClass,
+ FlashClass,
+ SystemClass,
+ InputClass
+};
+
+#define RP_FLAGS_AUDIO 0x01
+#define RP_FLAGS_VIDEO 0x02
+#define RP_FLAGS_AUDIO_RECORDING 0x04
+#define RP_FLAGS_VIDEO_RECORDING 0x08
+
+#endif
diff --git a/include/resource.h b/include/resource.h
new file mode 100644
index 0000000..1cec065
--- /dev/null
+++ b/include/resource.h
@@ -0,0 +1,50 @@
+#ifndef RESOURCE_H
+#define RESOURCE_H
+
+#include <QObject>
+#include <QPointer>
+#include "resource-library.h"
+#include "resource-types.h"
+
+class Resource: public QObject
+{
+ Q_OBJECT
+ friend class ResourceFactory;
+ friend class TestResource;
+private:
+ enum ResourceClass resourceClass;
+ quint16 flags;
+ ResourceLibrary *resourceLibrary;
+
+ bool initialize(ResourceLibrary *library);
+ Resource(enum ResourceClass type, quint16 resourceFlags, QObject *parent=0);
+public:
+ virtual ~Resource();
+
+ enum ResourceClass getResourceClass();
+ quint16 getResourceFlags();
+
+ virtual bool requestExclusiveAccess();
+ virtual bool releaseExclusiveAccess();
+ virtual bool getExclusiveAccessState();
+/*
+ virtual bool setMute();
+ virtual bool unsetMute();
+ virtual bool requestMute();
+
+ virtual bool setPrivacyOverride();
+ virtual bool unsetPrivacyOverride();
+ virtual bool requestPrivacyOverride();
+
+ virtual bool setBluetoothOverride();
+ virtual bool unsetBluetoothOverride();
+ virtual bool requestBluetoothOverride();
+
+ virtual bool setPID(pid_t pid);
+ virtual bool setStreamName(const QString & name);
+*/
+ // completed and discarded from libplayback? what are those?
+};
+
+#endif
+
diff --git a/libresourceqt.pro b/libresourceqt.pro
new file mode 100644
index 0000000..6467262
--- /dev/null
+++ b/libresourceqt.pro
@@ -0,0 +1,34 @@
+TEMPLATE = lib
+VERSION = 1.0.0
+TARGET = resourceqt
+DESTDIR = build
+DEPENDPATH += include src
+INCLUDEPATH += src include
+
+# Input
+PUBLIC_HEADERS = include/resource.h \
+ include/resource-factory.h \
+ include/resource-types.h
+HEADERS += $$PUBLIC_HEADERS \
+ src/libplayback-wrapper.h \
+ src/resource-library.h
+
+SOURCES += src/libplayback-wrapper.cpp \
+ src/resource.cpp \
+ src/resource-factory.cpp
+
+
+OBJECTS_DIR = build
+MOC_DIR = build
+
+CONFIG += qt link_pkgconfig
+QT = core dbus
+PKGCONFIG += dbus-1 libplayback-1
+
+# Install directives
+headers.files = $$PUBLIC_HEADERS
+INSTALLBASE = /usr
+target.path = $$INSTALLBASE/lib
+headers.path = $$INSTALLBASE/include/resource/qt4
+
+INSTALLS = target headers
diff --git a/src/libplayback-wrapper.cpp b/src/libplayback-wrapper.cpp
new file mode 100644
index 0000000..a1cfbab
--- /dev/null
+++ b/src/libplayback-wrapper.cpp
@@ -0,0 +1,96 @@
+#include "libplayback-wrapper.h"
+
+LibPlaybackWrapper::LibPlaybackWrapper(Resource *res)
+ : QObject(res), dbusConnection(NULL), libPlaybackHandle(NULL)
+{
+ resource = res;
+}
+
+LibPlaybackWrapper::~LibPlaybackWrapper()
+{
+ pb_playback_destroy(libPlaybackHandle);
+}
+
+bool LibPlaybackWrapper::initialize()
+{
+ DBusError dbusError;
+ dbusConnection = dbus_bus_get(DBUS_BUS_SESSION, &dbusError);
+ if(dbusConnection == NULL) {
+ return false;
+ }
+
+ enum pb_class_e libPlaybackClass = resourceClassToLibPlaybackClass(resource->getResourceClass());
+ quint16 libPlaybackFlags = resourceFlagToLibPlaybackFlags(resource->getResourceFlags());
+ libPlaybackHandle = pb_playback_new_2(dbusConnection, libPlaybackClass, libPlaybackFlags,
+ PB_STATE_STOP, libPlaybackStateHandler, this);
+ if(libPlaybackHandle == NULL) {
+ return false;
+ }
+ return true;
+}
+
+enum pb_class_e resourceClassToLibPlaybackClass(enum ResourceClass resourceClass)
+{
+ switch(resourceClass) {
+ case InvalidClass:
+ return PB_CLASS_NONE;
+ case CallClass:
+ return PB_CLASS_CALL;
+ case MediaClass:
+ return PB_CLASS_MEDIA;
+ case BackgroundClass:
+ return PB_CLASS_BACKGROUND;
+ case RingtoneClass:
+ return PB_CLASS_RINGTONE;
+ case VoiceUIClass:
+ return PB_CLASS_VOICEUI;
+ case CameraClass:
+ return PB_CLASS_CAMERA;
+ case GameClass:
+ return PB_CLASS_GAME;
+ case AlarmClass:
+ return PB_CLASS_ALARM;
+ case FlashClass:
+ return PB_CLASS_FLASH;
+ case SystemClass:
+ return PB_CLASS_SYSTEM;
+ case InputClass:
+ return PB_CLASS_INPUT;
+ default:
+ return PB_CLASS_NONE;
+ }
+}
+
+quint16 resourceFlagToLibPlaybackFlags(quint16 resourceFlags)
+{
+ quint16 libPlaybackFlags = 0;
+ for (int flag=RP_FLAGS_AUDIO;flag<RP_FLAGS_VIDEO_RECORDING;flag=flag<<1) {
+ if((resourceFlags & flag) == flag) {
+ switch(flag) {
+ case RP_FLAGS_AUDIO:
+ libPlaybackFlags += PB_FLAG_AUDIO;
+ break;
+ case RP_FLAGS_VIDEO:
+ libPlaybackFlags += PB_FLAG_VIDEO;
+ break;
+ case RP_FLAGS_AUDIO_RECORDING:
+ libPlaybackFlags += PB_FLAG_AUDIO_RECORDING;
+ break;
+ case RP_FLAGS_VIDEO_RECORDING:
+ libPlaybackFlags += PB_FLAG_VIDEO_RECORDING;
+ break;
+ }
+ }
+ }
+ return libPlaybackFlags;
+}
+
+void libPlaybackStateHandler(pb_playback_t *libPlaybackHandle, enum pb_state_e requestedState,
+ pb_req_t* playbackRequest, void *data)
+{
+// LibPlaybackWrapper *libPlaybackWrapper = (LibPlaybackWrapper*)data;
+
+// libPlaybackWrapper->handleNewState(requestedState);
+
+ pb_playback_req_completed(libPlaybackHandle, playbackRequest);
+}
diff --git a/src/libplayback-wrapper.h b/src/libplayback-wrapper.h
new file mode 100644
index 0000000..83baf28
--- /dev/null
+++ b/src/libplayback-wrapper.h
@@ -0,0 +1,26 @@
+#include "resource-library.h"
+#include "resource.h"
+#include <libplayback/playback.h>
+
+#ifndef LIBPLAYBACKWRAPPER_H
+#define LIBPLAYBACKWRAPPER_H
+
+class LibPlaybackWrapper: public QObject, public ResourceLibrary
+{
+ Q_OBJECT
+private:
+ DBusConnection *dbusConnection;
+ pb_playback_t *libPlaybackHandle;
+ Resource *resource;
+public:
+ LibPlaybackWrapper(Resource *resource);
+ ~LibPlaybackWrapper();
+ bool initialize();
+};
+
+void libPlaybackStateHandler(pb_playback_t *libPlaybackHandle, enum pb_state_e requestedState,
+ pb_req_t* playbackRequest, void *data);
+
+inline enum pb_class_e resourceClassToLibPlaybackClass(enum ResourceClass resourceClass);
+inline quint16 resourceFlagToLibPlaybackFlags(quint16 resourceFlag);
+#endif
diff --git a/src/resource-factory.cpp b/src/resource-factory.cpp
new file mode 100644
index 0000000..03566e2
--- /dev/null
+++ b/src/resource-factory.cpp
@@ -0,0 +1,26 @@
+#include "resource-factory.h"
+#include "libplayback-wrapper.h"
+
+ResourceFactory::ResourceFactory(QObject *parent)
+ : QObject(parent), sessionBusConnection(QDBusConnection::sessionBus())
+{
+}
+
+ResourceFactory::~ResourceFactory()
+{
+}
+
+Resource * ResourceFactory::createResource(enum ResourceClass type, quint16 usageFlags)
+{
+ Resource *resource = new Resource(type, usageFlags, this);
+ ResourceLibrary *libPlayback = new LibPlaybackWrapper(resource);
+
+ bool resourceProperlyInitialized = resource->initialize(libPlayback);
+ if(resourceProperlyInitialized) {
+ return resource;
+ }
+ else {
+ delete resource;
+ return NULL;
+ }
+}
diff --git a/src/resource-library.h b/src/resource-library.h
new file mode 100644
index 0000000..f9e178b
--- /dev/null
+++ b/src/resource-library.h
@@ -0,0 +1,11 @@
+#ifndef RESOURCELIBRARY_H
+#define RESOURCELIBRARY_H
+
+class ResourceLibrary
+{
+public:
+ virtual ~ResourceLibrary() {}
+ virtual bool initialize() = 0;
+};
+
+#endif
diff --git a/src/resource.cpp b/src/resource.cpp
new file mode 100644
index 0000000..3bbe0a4
--- /dev/null
+++ b/src/resource.cpp
@@ -0,0 +1,42 @@
+#include "resource.h"
+
+Resource::Resource(enum ResourceClass type, quint16 resourceFlags, QObject *parent)
+ : QObject(parent), resourceClass(type), flags(resourceFlags)
+{
+}
+
+Resource::~Resource()
+{
+}
+
+bool Resource::initialize(ResourceLibrary *library)
+{
+ resourceLibrary = library;
+ return resourceLibrary->initialize();
+}
+
+
+quint16 Resource::getResourceFlags()
+{
+ return flags;
+}
+
+enum ResourceClass Resource::getResourceClass()
+{
+ return resourceClass;
+}
+
+bool Resource::requestExclusiveAccess()
+{
+ return false;
+}
+
+bool Resource::releaseExclusiveAccess()
+{
+ return false;
+}
+
+bool Resource::getExclusiveAccessState()
+{
+ return false;
+}
diff --git a/tests/mock-resource-library.cpp b/tests/mock-resource-library.cpp
new file mode 100644
index 0000000..4a4fdb4
--- /dev/null
+++ b/tests/mock-resource-library.cpp
@@ -0,0 +1,20 @@
+#include "mock-resource-library.h"
+
+MockResourceLibrary::MockResourceLibrary(QObject *parent, bool makeInitFail)
+ : QObject(parent), initializeFails(makeInitFail)
+{
+}
+
+MockResourceLibrary::~MockResourceLibrary()
+{
+}
+
+void MockResourceLibrary::makeInitializeFail()
+{
+ initializeFails=true;
+}
+
+bool MockResourceLibrary::initialize()
+{
+ return !initializeFails;
+}
diff --git a/tests/mock-resource-library.h b/tests/mock-resource-library.h
new file mode 100644
index 0000000..df6fd97
--- /dev/null
+++ b/tests/mock-resource-library.h
@@ -0,0 +1,19 @@
+#ifndef MOCK_RESOURCE_LIBRARY_H
+#define MOCK_RESOURCE_LIBRARY_H
+
+#include <QObject>
+#include "resource-library.h"
+
+class MockResourceLibrary: public QObject, public ResourceLibrary
+{
+ Q_OBJECT
+private:
+ bool initializeFails;
+public:
+ MockResourceLibrary(QObject *parent=0,bool makeInitFail=false);
+ virtual ~MockResourceLibrary();
+ void makeInitializeFail();
+ bool initialize();
+};
+
+#endif
diff --git a/tests/test-resource.cpp b/tests/test-resource.cpp
new file mode 100644
index 0000000..aae4dd5
--- /dev/null
+++ b/tests/test-resource.cpp
@@ -0,0 +1,44 @@
+#include "test-resource.h"
+TestResource::TestResource()
+{
+}
+
+TestResource::~TestResource()
+{
+}
+
+void TestResource::initTestCase()
+{
+ resource = new Resource(MediaClass, RP_FLAGS_AUDIO|RP_FLAGS_VIDEO, this);
+ resourceLibrary = new MockResourceLibrary(resource, false);
+}
+
+void TestResource::testConstructor()
+{
+ QVERIFY(resource != NULL);
+ QVERIFY(resource->resourceClass == MediaClass);
+ QVERIFY(resource->flags == (RP_FLAGS_AUDIO|RP_FLAGS_VIDEO));
+}
+
+void TestResource::testInitializeSucceeds()
+{
+ bool initializeSucceeded = resource->initialize(resourceLibrary);
+
+ QVERIFY(resource->resourceLibrary == resourceLibrary);
+ QVERIFY(initializeSucceeded);
+}
+
+void TestResource::testInitializeFails()
+{
+ MockResourceLibrary *mockResourceLibrary =
+ static_cast<MockResourceLibrary *>(resourceLibrary);
+ mockResourceLibrary->makeInitializeFail();
+
+ bool initializeSucceeded = resource->initialize(resourceLibrary);
+
+ QEXPECT_FAIL("", "Expecting resourceLibrary->initialize() to fail", Continue);
+ QVERIFY(initializeSucceeded);
+ delete resource;
+}
+
+QTEST_MAIN(TestResource)
diff --git a/tests/test-resource.h b/tests/test-resource.h
new file mode 100644
index 0000000..7d0bd7f
--- /dev/null
+++ b/tests/test-resource.h
@@ -0,0 +1,25 @@
+#ifndef TEST_RESOURCE_H
+#define TEST_RESOURCE_H
+
+#include <QtTest/QTest>
+#include "resource.h"
+#include "mock-resource-library.h"
+
+class TestResource: public QObject
+{
+ Q_OBJECT
+private:
+ ResourceLibrary *resourceLibrary;
+ Resource *resource;
+public:
+ TestResource();
+ ~TestResource();
+private slots:
+ void initTestCase();
+
+ void testConstructor();
+ void testInitializeSucceeds();
+ void testInitializeFails();
+};
+
+#endif
diff --git a/tests/test-resource.pro b/tests/test-resource.pro
new file mode 100644
index 0000000..3271864
--- /dev/null
+++ b/tests/test-resource.pro
@@ -0,0 +1,21 @@
+BASE = ..
+TEMPLATE = app
+TARGET = test-resource
+DESTDIR = build
+DEPENDPATH += $${BASE}/include $${BASE}/src .
+INCLUDEPATH += $${BASE}/src $${BASE}/include
+
+# Input
+HEADERS += $${BASE}/include/resource.h test-resource.h mock-resource-library.h
+SOURCES += $${BASE}/src/resource.cpp test-resource.cpp mock-resource-library.cpp
+
+OBJECTS_DIR = build
+MOC_DIR = build
+
+CONFIG += qt qtestlib debug warn_on
+QT -= gui
+
+# Install directives
+INSTALLBASE = /usr
+target.path = $$INSTALLBASE/share/libresourceqt/tests
+INSTALLS = target