on xenial help sign in
add support for loading the deviceconfig from an external config file
commit1d243ddc8641e1806bb3765095a1dc98d14cfe8a authorMichael Zanetti <michael.zanetti@canonical.com> dateThu 11 Feb 2016, 17:59 (11 years ago) parent53f4944
7 files changed+168−9
plugins/Utils/CMakeLists.txt+1 plugins/Utils/deviceconfigparser.cpp+93 plugins/Utils/deviceconfigparser.h+57 plugins/Utils/plugin.cpp+2 qml/DeviceConfiguration.qml+12−9 tests/mocks/Utils/CMakeLists.txt+1 tests/mocks/Utils/plugin.cpp+2
plugins/Utils/CMakeLists.txt+1
@@ -23,6 +23,7 @@ set(QMLPLUGIN_SRC
2323 windowstatestorage.cpp
2424 timezoneFormatter.cpp
2525 inputeventgenerator.cpp
26+ deviceconfigparser.cpp
2627 plugin.cpp
2728 )
2829
plugins/Utils/deviceconfigparser.cppnew file+93
@@ -0,0 +1,93 @@
1+#include "deviceconfigparser.h"
2+
3+#include <QSettings>
4+#include <QSettings>
5+
6+DeviceConfigParser::DeviceConfigParser(QObject *parent): QObject(parent)
7+{
8+
9+}
10+
11+QString DeviceConfigParser::name() const
12+{
13+ return m_name;
14+}
15+
16+void DeviceConfigParser::setName(const QString &name)
17+{
18+ if (m_name == name) {
19+ return;
20+ }
21+ m_name = name;
22+ Q_EMIT changed();
23+}
24+
25+Qt::ScreenOrientation DeviceConfigParser::primaryOrientation() const
26+{
27+ return stringToOrientation(readOrientationStringFromConfig("PrimaryOrientation"), Qt::PrimaryOrientation);
28+}
29+
30+Qt::ScreenOrientations DeviceConfigParser::supportedOrientations() const
31+{
32+ QString values = readOrientationStringFromConfig("SupportedOrientations");
33+ if (values.isEmpty()) {
34+ return Qt::PortraitOrientation
35+ | Qt::InvertedPortraitOrientation
36+ | Qt::LandscapeOrientation
37+ | Qt::InvertedLandscapeOrientation;
38+ }
39+
40+ Qt::ScreenOrientations ret = Qt::PrimaryOrientation;
41+ Q_FOREACH(const QString &orientationString, values.split(',')) {
42+ ret |= stringToOrientation(orientationString, Qt::PrimaryOrientation);
43+ }
44+ return ret;
45+}
46+
47+Qt::ScreenOrientation DeviceConfigParser::landscapeOrientation() const
48+{
49+ return stringToOrientation(readOrientationStringFromConfig("LandscapeOrientation"), Qt::LandscapeOrientation);
50+}
51+
52+Qt::ScreenOrientation DeviceConfigParser::invertedLandscapeOrientation() const
53+{
54+ return stringToOrientation(readOrientationStringFromConfig("InvertedLandscapeOrientation"), Qt::InvertedLandscapeOrientation);
55+}
56+
57+Qt::ScreenOrientation DeviceConfigParser::portraitOrientation() const
58+{
59+ return stringToOrientation(readOrientationStringFromConfig("PortraitOrientation"), Qt::PortraitOrientation);
60+}
61+
62+Qt::ScreenOrientation DeviceConfigParser::invertedPortraitOrientation() const
63+{
64+ return stringToOrientation(readOrientationStringFromConfig("InvertedPortraitOrientation"), Qt::InvertedPortraitOrientation);
65+}
66+
67+QString DeviceConfigParser::readOrientationStringFromConfig(const QString &key) const
68+{
69+ QSettings config("/etc/unity8/devices.conf", QSettings::IniFormat);
70+ config.beginGroup(m_name);
71+
72+ if (config.contains(key)) {
73+ return config.value(key).toString();
74+ }
75+ return QString();
76+}
77+
78+Qt::ScreenOrientation DeviceConfigParser::stringToOrientation(const QString &orientationString, Qt::ScreenOrientation defaultValue) const
79+{
80+ if (orientationString == "Landscape") {
81+ return Qt::LandscapeOrientation;
82+ }
83+ if (orientationString == "InvertedLandscape") {
84+ return Qt::InvertedLandscapeOrientation;
85+ }
86+ if (orientationString == "Portrait") {
87+ return Qt::PortraitOrientation;
88+ }
89+ if (orientationString == "InvertedPortrait") {
90+ return Qt::InvertedPortraitOrientation;
91+ }
92+ return defaultValue;
93+}
plugins/Utils/deviceconfigparser.hnew file+57
@@ -0,0 +1,57 @@
1+/*
2+ * Copyright 2016 Canonical Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU Lesser General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU Lesser General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU Lesser General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ */
16+
17+#ifndef DEVICECONFIGPARSER_H
18+#define DEVICECONFIGPARSER_H
19+
20+#include <QObject>
21+
22+class DeviceConfigParser: public QObject
23+{
24+ Q_OBJECT
25+ Q_PROPERTY(QString name READ name WRITE setName NOTIFY changed)
26+
27+ Q_PROPERTY(Qt::ScreenOrientation primaryOrientation READ primaryOrientation NOTIFY changed)
28+ Q_PROPERTY(Qt::ScreenOrientations supportedOrientations READ supportedOrientations NOTIFY changed)
29+ Q_PROPERTY(Qt::ScreenOrientation landscapeOrientation READ landscapeOrientation NOTIFY changed)
30+ Q_PROPERTY(Qt::ScreenOrientation invertedLandscapeOrientation READ invertedLandscapeOrientation NOTIFY changed)
31+ Q_PROPERTY(Qt::ScreenOrientation portraitOrientation READ portraitOrientation NOTIFY changed)
32+ Q_PROPERTY(Qt::ScreenOrientation invertedPortraitOrientation READ invertedPortraitOrientation NOTIFY changed)
33+
34+public:
35+ DeviceConfigParser(QObject *parent = nullptr);
36+
37+ QString name() const;
38+ void setName(const QString &name);
39+
40+ Qt::ScreenOrientation primaryOrientation() const;
41+ Qt::ScreenOrientations supportedOrientations() const;
42+ Qt::ScreenOrientation landscapeOrientation() const;
43+ Qt::ScreenOrientation invertedLandscapeOrientation() const;
44+ Qt::ScreenOrientation portraitOrientation() const;
45+ Qt::ScreenOrientation invertedPortraitOrientation() const;
46+
47+Q_SIGNALS:
48+ void changed();
49+
50+private:
51+ QString m_name;
52+
53+ QString readOrientationStringFromConfig(const QString &key) const;
54+ Qt::ScreenOrientation stringToOrientation(const QString &orientationString, Qt::ScreenOrientation defaultValue) const;
55+};
56+
57+#endif
plugins/Utils/plugin.cpp+2
@@ -37,6 +37,7 @@
3737#include "timezoneFormatter.h"
3838#include "applicationsfiltermodel.h"
3939#include "inputeventgenerator.h"
40+#include "deviceconfigparser.h"
4041
4142static QObject *createWindowStateStorage(QQmlEngine *engine, QJSEngine *scriptEngine)
4243{
@@ -70,6 +71,7 @@ void UtilsPlugin::registerTypes(const char *uri)
7071 qmlRegisterType<ActiveFocusLogger>(uri, 0, 1, "ActiveFocusLogger");
7172 qmlRegisterType<ApplicationsFilterModel>(uri, 0, 1, "ApplicationsFilterModel");
7273 qmlRegisterType<InputEventGenerator>(uri, 0, 1, "InputEventGenerator");
74+ qmlRegisterType<DeviceConfigParser>(uri, 0, 1, "DeviceConfigParser");
7375}
7476
7577void UtilsPlugin::initializeEngine(QQmlEngine *engine, const char *uri)
qml/DeviceConfiguration.qml+12−9
@@ -15,6 +15,7 @@
1515 */
1616
1717import QtQuick 2.4
18+import Utils 0.1
1819
1920QtObject {
2021 id: root
@@ -34,20 +35,22 @@ QtObject {
3435
3536 readonly property alias category: priv.category
3637
38+ readonly property var deviceConfigParser: DeviceConfigParser {
39+ name: root.name
40+ }
41+
3742 readonly property var priv: StateGroup {
3843 id: priv
3944
40− property int primaryOrientation: root.useNativeOrientation
45+ property int primaryOrientation: deviceConfigParser.primaryOrientation == Qt.PrimaryOrientation ?
46+ root.useNativeOrientation : deviceConfigParser.primaryOrientation
4147
42− property int supportedOrientations: Qt.PortraitOrientation
43− | Qt.InvertedPortraitOrientation
44− | Qt.LandscapeOrientation
45− | Qt.InvertedLandscapeOrientation
48+ property int supportedOrientations: deviceConfigParser.supportedOrientations
4649
47− property int landscapeOrientation: Qt.LandscapeOrientation
48− property int invertedLandscapeOrientation: Qt.InvertedLandscapeOrientation
49− property int portraitOrientation: Qt.PortraitOrientation
50− property int invertedPortraitOrientation: Qt.InvertedPortraitOrientation
50+ property int landscapeOrientation: deviceConfigParser.landscapeOrientation
51+ property int invertedLandscapeOrientation: deviceConfigParser.invertedLandscapeOrientation
52+ property int portraitOrientation: deviceConfigParser.portraitOrientation
53+ property int invertedPortraitOrientation: deviceConfigParser.invertedPortraitOrientation
5154
5255 // Supported values so far:
5356 // "phone", "tablet" or "desktop"
tests/mocks/Utils/CMakeLists.txt+1
@@ -18,6 +18,7 @@ set(QMLPLUGIN_SRC
1818 ${CMAKE_SOURCE_DIR}/plugins/Utils/inputwatcher.cpp
1919 ${CMAKE_SOURCE_DIR}/plugins/Utils/applicationsfiltermodel.cpp
2020 ${CMAKE_SOURCE_DIR}/plugins/Utils/inputeventgenerator.cpp
21+ ${CMAKE_SOURCE_DIR}/plugins/Utils/deviceconfigparser.cpp
2122 ${APPLICATION_API_INCLUDEDIR}/unity/shell/application/ApplicationManagerInterface.h
2223 constants.cpp
2324 plugin.cpp
tests/mocks/Utils/plugin.cpp+2
@@ -37,6 +37,7 @@
3737#include <easingcurve.h>
3838#include <applicationsfiltermodel.h>
3939#include <inputeventgenerator.h>
40+#include <deviceconfigparser.h>
4041
4142static QObject *createWindowStateStorage(QQmlEngine *engine, QJSEngine *scriptEngine)
4243{
@@ -67,6 +68,7 @@ void FakeUtilsPlugin::registerTypes(const char *uri)
6768 qmlRegisterType<ActiveFocusLogger>(uri, 0, 1, "ActiveFocusLogger");
6869 qmlRegisterType<ApplicationsFilterModel>(uri, 0, 1, "ApplicationsFilterModel");
6970 qmlRegisterType<InputEventGenerator>(uri, 0, 1, "InputEventGenerator");
71+ qmlRegisterType<DeviceConfigParser>(uri, 0, 1, "DeviceConfigParser");
7072}
7173
7274void FakeUtilsPlugin::initializeEngine(QQmlEngine *engine, const char *uri)