diff options
-rw-r--r-- | doc/src/qtsensors-backend.qdoc | 3 | ||||
-rw-r--r-- | src/sensors/qsensormanager.cpp | 66 | ||||
-rw-r--r-- | src/sensors/qsensormanager.h | 2 | ||||
-rw-r--r-- | tests/auto/qsensor/tst_qsensor.cpp | 25 |
4 files changed, 63 insertions, 33 deletions
diff --git a/doc/src/qtsensors-backend.qdoc b/doc/src/qtsensors-backend.qdoc index 8e79100..aec028a 100644 --- a/doc/src/qtsensors-backend.qdoc +++ b/doc/src/qtsensors-backend.qdoc @@ -114,8 +114,7 @@ file to determine the default instead. \section1 Sensors.conf The config file that determines the default sensor for a type is called Sensors.conf. If present, -it is located in /etc/xdg/Nokia. It is read using QSettings so it has the standard formatting -of a QSettings .conf file. +it is located in /etc/xdg/Nokia. It has the standard formatting of an ini file. The settings live in the Default group and the general format is: \code diff --git a/src/sensors/qsensormanager.cpp b/src/sensors/qsensormanager.cpp index 6b8d534..053e62e 100644 --- a/src/sensors/qsensormanager.cpp +++ b/src/sensors/qsensormanager.cpp @@ -43,9 +43,10 @@ #include <QDebug> #include "qsensorpluginloader_p.h" #include "qsensorplugin.h" -#include <QSettings> +#include <QStandardPaths> #include "sensorlog_p.h" #include <QTimer> +#include <QFile> QT_BEGIN_NAMESPACE @@ -67,6 +68,7 @@ public: }; QSensorManagerPrivate() : pluginLoadingState(NotLoaded) + , defaultIdentifierForTypeLoaded(false) , sensorsChanged(false) { } @@ -79,6 +81,10 @@ public: // Holds a mapping from type to available identifiers (and from there to the factory) BackendIdentifiersForTypeMap backendsByType; + // Holds the default identifier + QHash<QByteArray, QByteArray> defaultIdentifierForType; + bool defaultIdentifierForTypeLoaded; + // Holds the first identifier for each type QHash<QByteArray, QByteArray> firstIdentifierForType; @@ -127,7 +133,6 @@ Q_GLOBAL_STATIC(QSensorManagerPrivate, sensorManagerPrivate) // The unit test needs to change the behaviour of the library. It does this // through an exported but undocumented function. static void initPlugin(QObject *plugin); -static QSettings::Scope settings_scope = QSettings::SystemScope; static bool load_external_plugins = true; Q_SENSORS_EXPORT void sensors_unit_test_hook(int index) { @@ -136,7 +141,6 @@ Q_SENSORS_EXPORT void sensors_unit_test_hook(int index) switch (index) { case 0: Q_ASSERT(d->pluginLoadingState == QSensorManagerPrivate::NotLoaded); - settings_scope = QSettings::UserScope; load_external_plugins = false; break; case 1: @@ -387,6 +391,16 @@ bool QSensorManager::isBackendRegistered(const QByteArray &type, const QByteArra return true; } +/*! + Sets or overwrite the sensor \a type with the backend \a identifier. +*/ +void QSensorManager::setDefaultBackend(const QByteArray &type, const QByteArray &identifier) +{ + QSensorManagerPrivate *d = sensorManagerPrivate(); + d->defaultIdentifierForType.insert(type, identifier); +} + + // ===================================================================== /*! @@ -438,13 +452,45 @@ QByteArray QSensor::defaultSensorForType(const QByteArray &type) if (!d->backendsByType.contains(type)) return QByteArray(); - // The unit test needs to modify Sensors.conf but it can't access the system directory - QSettings settings(settings_scope, QLatin1String("Nokia"), QLatin1String("Sensors")); - QVariant value = settings.value(QString(QLatin1String("Default/%1")).arg(QString::fromLatin1(type))); - if (!value.isNull()) { - QByteArray defaultIdentifier = value.toByteArray(); - if (d->backendsByType[type].contains(defaultIdentifier)) // Don't return a value that we can't use! - return defaultIdentifier; + //check if we need to read the config setting file + if (!d->defaultIdentifierForTypeLoaded) { + d->defaultIdentifierForTypeLoaded = true; + QStringList abspath = QStandardPaths::standardLocations(QStandardPaths::ConfigLocation); + QString cfgpath = ""; + //first in the list is user specific, so ignore it and take the last + if (abspath.length() > 0) { + cfgpath = abspath[abspath.count() - 1]; + cfgpath += QString::fromLatin1("/Nokia/Sensors.conf"); + if (QFile::exists(cfgpath)){ + QFile cfgfile(cfgpath); + if (cfgfile.open(QFile::ReadOnly)){ + //Read the sensor default setting file + QTextStream stream(&cfgfile); + QString line = ""; + bool isconfig = false; + while (!stream.atEnd()) { + line = stream.readLine(); + if (!isconfig && line == QString::fromLatin1("[Default]")) + isconfig = true; + else { + if (isconfig) { + //read out setting line + line.remove(' '); + QStringList pair = line.split('='); + if (pair.count() == 2) + d->defaultIdentifierForType.insert(pair[0].toLatin1(), pair[1].toLatin1()); + } + } + } + } + } + } + } + + QHash<QByteArray, QByteArray>::const_iterator i = d->defaultIdentifierForType.find(type); + if (i != d->defaultIdentifierForType.end() && i.key() == type) { + if (d->backendsByType[type].contains(i.value())) // Don't return a value that we can't use! + return i.value(); } // This is our fallback diff --git a/src/sensors/qsensormanager.h b/src/sensors/qsensormanager.h index 0cd151d..48a2ed4 100644 --- a/src/sensors/qsensormanager.h +++ b/src/sensors/qsensormanager.h @@ -68,6 +68,8 @@ public: // For static plugins static void registerStaticPlugin(CreatePluginFunc func); + + static void setDefaultBackend(const QByteArray &type, const QByteArray &identifier); }; class Q_SENSORS_EXPORT QSensorBackendFactory diff --git a/tests/auto/qsensor/tst_qsensor.cpp b/tests/auto/qsensor/tst_qsensor.cpp index 2c1dd9a..45b2ab3 100644 --- a/tests/auto/qsensor/tst_qsensor.cpp +++ b/tests/auto/qsensor/tst_qsensor.cpp @@ -44,9 +44,9 @@ #include <QObject> #include <QTest> #include <QDebug> -#include <QSettings> #include <QFile> #include <QSignalSpy> +#include <QSensorManager> #include "qsensor.h" #include "test_sensor.h" @@ -123,14 +123,10 @@ public: private slots: void initTestCase() { - QSettings settings(QLatin1String("Nokia"), QLatin1String("Sensors")); - settings.clear(); } void cleanupTestCase() { - QSettings settings(QLatin1String("Nokia"), QLatin1String("Sensors")); - settings.clear(); #ifdef WAIT_AT_END QFile _stdin; @@ -181,10 +177,7 @@ private slots: void testBadDefaultFromConfig() { - QSettings settings(QLatin1String("Nokia"), QLatin1String("Sensors")); - settings.setValue(QString(QLatin1String("Default/%1")).arg(QString::fromLatin1(TestSensor::type)), QByteArray("bogus id")); - settings.sync(); - + QSensorManager::setDefaultBackend(QByteArray(TestSensor::type), QByteArray("bogus id")); QByteArray expected = testsensorimpl::id; QByteArray actual = QSensor::defaultSensorForType(TestSensor::type); QCOMPARE(actual, expected); @@ -192,15 +185,10 @@ private slots: void testGoodDefaultFromConfig() { - QSettings settings(QLatin1String("Nokia"), QLatin1String("Sensors")); - settings.setValue(QString(QLatin1String("Default/%1")).arg(QString::fromLatin1(TestSensor::type)), QByteArray(testsensorimpl::id)); - settings.sync(); - + QSensorManager::setDefaultBackend(QByteArray(TestSensor::type), QByteArray(testsensorimpl::id)); QByteArray expected = testsensorimpl::id; QByteArray actual = QSensor::defaultSensorForType(TestSensor::type); QCOMPARE(actual, expected); - - settings.clear(); } void testNoSensorsForType() @@ -245,18 +233,13 @@ private slots: void testBadDefaultCreation() { - QSettings settings(QLatin1String("Nokia"), QLatin1String("Sensors")); - settings.setValue(QString(QLatin1String("Default/%1")).arg(QString::fromLatin1(TestSensor::type)), QByteArray("test sensor 2")); - settings.sync(); - + QSensorManager::setDefaultBackend(QByteArray(TestSensor::type), QByteArray("test sensor 2")); TestSensor sensor; QTest::ignoreMessage(QtWarningMsg, "Can't create backend \"test sensor 2\" "); sensor.connectToBackend(); QByteArray expected = testsensorimpl::id; QByteArray actual = sensor.identifier(); QCOMPARE(actual, expected); - - settings.clear(); } void testBadCreation() |