diff options
8 files changed, 41 insertions, 167 deletions
diff --git a/libcontextsubscriber/src/contextpropertyinfo.cpp b/libcontextsubscriber/src/contextpropertyinfo.cpp index 78f68b6b..5e334b56 100644 --- a/libcontextsubscriber/src/contextpropertyinfo.cpp +++ b/libcontextsubscriber/src/contextpropertyinfo.cpp @@ -196,12 +196,13 @@ ContextPropertyInfo::ContextPropertyInfo(const QString &key, QObject *parent) keyName = key; if (key != "") { - sconnect(InfoBackend::instance(), SIGNAL(keyDataChanged(QString)), + InfoBackend* infoBackend = InfoBackend::instance(); + sconnect(infoBackend, SIGNAL(keyDataChanged(QString)), this, SLOT(onKeyDataChanged(QString))); - cachedType = InfoBackend::instance()->typeForKey(keyName); - cachedProvider = InfoBackend::instance()->providerForKey(keyName); - cachedProviderDBusType = InfoBackend::instance()->providerDBusTypeForKey(keyName); + cachedType = infoBackend->typeForKey(keyName); + cachedProvider = infoBackend->providerForKey(keyName); + cachedProviderDBusType = infoBackend->providerDBusTypeForKey(keyName); } } diff --git a/libcontextsubscriber/src/contextregistryinfo.cpp b/libcontextsubscriber/src/contextregistryinfo.cpp index e0f8599d..ec902bcc 100644 --- a/libcontextsubscriber/src/contextregistryinfo.cpp +++ b/libcontextsubscriber/src/contextregistryinfo.cpp @@ -23,6 +23,8 @@ #include "infobackend.h" #include "sconnect.h" #include <QMutex> +#include <QMutexLocker> +#include <QCoreApplication> /*! \class ContextRegistryInfo @@ -43,30 +45,27 @@ ContextRegistryInfo* ContextRegistryInfo::registryInstance = NULL; /// is constructed automaticall on first access. /// \param backendName the optional name of the backend to use (force). -// FIXME: we don't yet 100% sure if this double locking pattern implementation is safe -// FIXME: the instance should always be created in the main thread ContextRegistryInfo* ContextRegistryInfo::instance(const QString &backendName) { static QMutex mutex; - if (!registryInstance) - { - mutex.lock(); + QMutexLocker locker(&mutex); + if (! registryInstance) { + InfoBackend::instance(backendName); + registryInstance = new ContextRegistryInfo; - if (! registryInstance) { - InfoBackend::instance(backendName); - registryInstance = new ContextRegistryInfo; + InfoBackend* infoBackend = InfoBackend::instance(); - sconnect(InfoBackend::instance(), SIGNAL(keysChanged(QStringList)), - registryInstance, SLOT(onKeysChanged(QStringList))); + sconnect(infoBackend, SIGNAL(keysChanged(QStringList)), + registryInstance, SLOT(onKeysChanged(QStringList))); - sconnect(InfoBackend::instance(), SIGNAL(keysAdded(QStringList)), - registryInstance, SLOT(onKeysAdded(QStringList))); + sconnect(infoBackend, SIGNAL(keysAdded(QStringList)), + registryInstance, SLOT(onKeysAdded(QStringList))); - sconnect(InfoBackend::instance(), SIGNAL(keysRemoved(QStringList)), - registryInstance, SLOT(onKeysRemoved(QStringList))); - } + sconnect(infoBackend, SIGNAL(keysRemoved(QStringList)), + registryInstance, SLOT(onKeysRemoved(QStringList))); - mutex.unlock(); + // Move the backend to the main thread + registryInstance->moveToThread(QCoreApplication::instance()->thread()); } return registryInstance; diff --git a/libcontextsubscriber/src/infobackend.cpp b/libcontextsubscriber/src/infobackend.cpp index 922fa637..444285b6 100644 --- a/libcontextsubscriber/src/infobackend.cpp +++ b/libcontextsubscriber/src/infobackend.cpp @@ -25,6 +25,8 @@ #include <QMutex> #include <QDebug> #include <QCoreApplication> +#include <QMutexLocker> + /*! \class InfoBackend @@ -52,26 +54,25 @@ InfoBackend::InfoBackend(QObject *parent) : QObject(parent) InfoBackend* InfoBackend::instance(const QString &backendName) { static QMutex mutex; + QMutexLocker locker(&mutex); if (!backendInstance) { - mutex.lock(); - - if (! backendInstance) { - if (backendName == "xml") - backendInstance = new InfoXmlBackend; - else if (backendName == "cdb") + // Backend instance doesn't exist -> create it + if (backendName == "xml") + backendInstance = new InfoXmlBackend; + else if (backendName == "cdb") + backendInstance = new InfoCdbBackend; + else { + if (InfoCdbBackend::databaseExists()) backendInstance = new InfoCdbBackend; - else { - if (InfoCdbBackend::databaseExists()) - backendInstance = new InfoCdbBackend; - else - backendInstance = new InfoXmlBackend; - } - - qAddPostRoutine(destroyInstance); + else + backendInstance = new InfoXmlBackend; } - mutex.unlock(); + // Move the backend to the main thread + backendInstance->moveToThread(QCoreApplication::instance()->thread()); + + qAddPostRoutine(destroyInstance); } return backendInstance; diff --git a/libcontextsubscriber/src/propertyhandle.cpp b/libcontextsubscriber/src/propertyhandle.cpp index 16d59ad6..b1c328e2 100644 --- a/libcontextsubscriber/src/propertyhandle.cpp +++ b/libcontextsubscriber/src/propertyhandle.cpp @@ -81,20 +81,15 @@ PropertyHandle::PropertyHandle(const QString& key) sconnect(commanderListener, SIGNAL(nameDisappeared()), this, SLOT(updateProvider())); - // Move the PropertyHandle (and all children) to main thread. - moveToThread(QCoreApplication::instance()->thread()); - - queueOnce("init"); -} - -void PropertyHandle::init() -{ myInfo = new ContextPropertyInfo(myKey, this); updateProvider(); // Start listening to changes in property registry (e.g., new keys, keys removed) sconnect(ContextRegistryInfo::instance(), SIGNAL(keysChanged(const QStringList&)), this, SLOT(updateProvider())); + + // Move the PropertyHandle (and all children) to main thread. + moveToThread(QCoreApplication::instance()->thread()); } void PropertyHandle::ignoreCommander() diff --git a/libcontextsubscriber/src/propertyhandle.h b/libcontextsubscriber/src/propertyhandle.h index 5d11ad7a..21d21c39 100644 --- a/libcontextsubscriber/src/propertyhandle.h +++ b/libcontextsubscriber/src/propertyhandle.h @@ -28,7 +28,6 @@ #include <QSet> #include <QReadWriteLock> #include <QMutex> -#include "queuedinvoker.h" class ContextPropertyInfo; @@ -37,7 +36,7 @@ namespace ContextSubscriber { class PropertyProvider; class DBusNameListener; -class PropertyHandle : public QueuedInvoker +class PropertyHandle : public QObject { Q_OBJECT @@ -65,7 +64,6 @@ private slots: private: PropertyHandle(const QString& key); - Q_INVOKABLE void init(); PropertyProvider *myProvider; ///< Provider of this property ContextPropertyInfo *myInfo; ///< Metadata for this property diff --git a/libcontextsubscriber/unit-tests/propertyhandle/Makefile.am b/libcontextsubscriber/unit-tests/propertyhandle/Makefile.am index 4a0ee5f7..277cef3e 100644 --- a/libcontextsubscriber/unit-tests/propertyhandle/Makefile.am +++ b/libcontextsubscriber/unit-tests/propertyhandle/Makefile.am @@ -3,7 +3,7 @@ check_PROGRAMS = propertyhandle-unit-tests # your test's sources propertyhandle_unit_tests_SOURCES = testpropertyhandle.cpp \ testpropertyhandle.h propertyprovider.h dbusnamelistener.h \ - contextpropertyinfo.h contextregistryinfo.h queuedinvoker.h + contextpropertyinfo.h contextregistryinfo.h # only include these files in the coverage COVERAGE_FILES = propertyhandle.cpp diff --git a/libcontextsubscriber/unit-tests/propertyhandle/queuedinvoker.h b/libcontextsubscriber/unit-tests/propertyhandle/queuedinvoker.h deleted file mode 100644 index dc238956..00000000 --- a/libcontextsubscriber/unit-tests/propertyhandle/queuedinvoker.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (C) 2008, 2009 Nokia Corporation. - * - * Contact: Marius Vollmer <marius.vollmer@nokia.com> - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License - * version 2.1 as published by the Free Software Foundation. - * - * 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 - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA - * 02110-1301 USA - * - */ - -#ifndef QUEUEDINVOKER_H -#define QUEUEDINVOKER_H - -// This is a mock implementation - -#include <QObject> -#include <QString> -#include <QStringList> - -namespace ContextSubscriber { - -class QueuedInvoker : public QObject -{ - Q_OBJECT - -public: - QueuedInvoker(); - - // For tests - void callAllMethodsInQueue(); - -protected: - void queueOnce(const char *method); - -private: - QStringList methodsToCall; -}; - -} // namespace -#endif diff --git a/libcontextsubscriber/unit-tests/propertyhandle/testpropertyhandle.cpp b/libcontextsubscriber/unit-tests/propertyhandle/testpropertyhandle.cpp index 3d008057..d97f76bb 100644 --- a/libcontextsubscriber/unit-tests/propertyhandle/testpropertyhandle.cpp +++ b/libcontextsubscriber/unit-tests/propertyhandle/testpropertyhandle.cpp @@ -173,33 +173,6 @@ bool DBusNameListener::isServicePresent() const return servicePresent; } -// Mock implementation of QueuedInvoker - -QueuedInvoker::QueuedInvoker() -{ -} - -void QueuedInvoker::queueOnce(const char *method) -{ - qDebug() << "queueonce" << QString(method); - if (!methodsToCall.contains(QString(method))) { - methodsToCall.push_back(method); - } -} - -void QueuedInvoker::callAllMethodsInQueue() -{ - while (!methodsToCall.empty()) { - QString method = methodsToCall.front(); - methodsToCall.pop_front(); - if (!QMetaObject::invokeMethod(this, method.toStdString().c_str(), Qt::DirectConnection)) { - qFatal(" *****************\n" - "Erroneous usage of queueOnce\n" - " *****************\n"); - } - } -} - // // Definition of testcases // @@ -255,9 +228,6 @@ void PropertyHandleUnitTests::initializing() QString key = "Property." + QString(__FUNCTION__); propertyHandle = PropertyHandle::instance(key); - // Let the provider process the deferred events - propertyHandle->callAllMethodsInQueue(); - // Expected results: // The PropertyProvider with the correct DBusName and DBusType was created. QCOMPARE(PropertyProvider::instanceCount, 1); @@ -274,9 +244,6 @@ void PropertyHandleUnitTests::key() QString key = "Property." + QString(__FUNCTION__); propertyHandle = PropertyHandle::instance(key); - // Let the provider process the deferred events - propertyHandle->callAllMethodsInQueue(); - // Test and expected results: // The key() function returns the correct key QCOMPARE(propertyHandle->key(), key); @@ -291,9 +258,6 @@ void PropertyHandleUnitTests::info() QString key = "Property." + QString(__FUNCTION__); propertyHandle = PropertyHandle::instance(key); - // Let the provider process the deferred events - propertyHandle->callAllMethodsInQueue(); - // Test and expected results: // The info() function returns the correct ContextPropertyInfo QCOMPARE(propertyHandle->info(), mockContextPropertyInfo); @@ -308,9 +272,6 @@ void PropertyHandleUnitTests::subscribe() QString key = "Property." + QString(__FUNCTION__); propertyHandle = PropertyHandle::instance(key); - // Let the provider process the deferred events - propertyHandle->callAllMethodsInQueue(); - // Test: // Command the PropertyHandle to subscribe propertyHandle->subscribe(); @@ -331,9 +292,6 @@ void PropertyHandleUnitTests::subscribeAndUnsubscribe() QString key = "Property." + QString(__FUNCTION__); propertyHandle = PropertyHandle::instance(key); - // Let the provider process the deferred events - propertyHandle->callAllMethodsInQueue(); - // Test: // Command the PropertyHandle to subscribe propertyHandle->subscribe(); @@ -357,9 +315,6 @@ void PropertyHandleUnitTests::subscribeTwice() QString key = "Property." + QString(__FUNCTION__); propertyHandle = PropertyHandle::instance(key); - // Let the provider process the deferred events - propertyHandle->callAllMethodsInQueue(); - // Test: // Command the PropertyHandle to subscribe propertyHandle->subscribe(); @@ -383,9 +338,6 @@ void PropertyHandleUnitTests::subscriptionPendingAndFinished() QString key = "Property." + QString(__FUNCTION__); propertyHandle = PropertyHandle::instance(key); - // Let the provider process the deferred events - propertyHandle->callAllMethodsInQueue(); - // Test: // Command the PropertyHandle to subscribe propertyHandle->subscribe(); @@ -426,9 +378,6 @@ void PropertyHandleUnitTests::subscribeTwiceAndUnsubscribe() QString key = "Property." + QString(__FUNCTION__); propertyHandle = PropertyHandle::instance(key); - // Let the provider process the deferred events - propertyHandle->callAllMethodsInQueue(); - // Test: // Command the PropertyHandle to subscribe propertyHandle->subscribe(); @@ -455,9 +404,6 @@ void PropertyHandleUnitTests::subscribeTwiceAndUnsubscribeTwice() QString key = "Property." + QString(__FUNCTION__); propertyHandle = PropertyHandle::instance(key); - // Let the provider process the deferred events - propertyHandle->callAllMethodsInQueue(); - // Test: // Command the PropertyHandle to subscribe propertyHandle->subscribe(); @@ -486,9 +432,6 @@ void PropertyHandleUnitTests::setValueWithoutTypeCheck() QString key = "Property." + QString(__FUNCTION__); propertyHandle = PropertyHandle::instance(key); - // Let the provider process the deferred events - propertyHandle->callAllMethodsInQueue(); - // Start listening to the valueChanged signal QSignalSpy spy(propertyHandle, SIGNAL(valueChanged())); @@ -612,9 +555,6 @@ void PropertyHandleUnitTests::setValueWithTypeCheckAndCorrectTypes() QString key = "Property." + QString(__FUNCTION__); propertyHandle = PropertyHandle::instance(key); - // Let the provider process the deferred events - propertyHandle->callAllMethodsInQueue(); - // Enable the type checks PropertyHandle::setTypeCheck(true); @@ -755,9 +695,6 @@ void PropertyHandleUnitTests::setValueWithTypeCheckAndIncorrectTypes() QString key = "Property." + QString(__FUNCTION__); propertyHandle = PropertyHandle::instance(key); - // Let the provider process the deferred events - propertyHandle->callAllMethodsInQueue(); - // Enable the type checks PropertyHandle::setTypeCheck(true); @@ -854,9 +791,6 @@ void PropertyHandleUnitTests::commanderAppearsAndDisappears() QString key = "Property." + QString(__FUNCTION__); propertyHandle = PropertyHandle::instance(key); - // Let the provider process the deferred events - propertyHandle->callAllMethodsInQueue(); - // Subscribe to the PropertyHandle propertyHandle->subscribe(); @@ -907,9 +841,6 @@ void PropertyHandleUnitTests::commandingDisabled() QString key = "Property." + QString(__FUNCTION__); propertyHandle = PropertyHandle::instance(key); - // Let the provider process the deferred events - propertyHandle->callAllMethodsInQueue(); - // Subscribe to the handle propertyHandle->subscribe(); |