on xenial help sign in
add support for loading the deviceconfig from an external config file
1d243ddMichael Zanetti
plugins/Utils/CMakeLists.txt+1
@@ -23,6 +23,7 @@ set(QMLPLUGIN_SRC
23 windowstatestorage.cpp
23 windowstatestorage.cpp
24 timezoneFormatter.cpp
24 timezoneFormatter.cpp
25 inputeventgenerator.cpp
25 inputeventgenerator.cpp
26+ deviceconfigparser.cpp
26 plugin.cpp
27 plugin.cpp
27 )
28 )
28
29
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 @@
37#include "timezoneFormatter.h"
37#include "timezoneFormatter.h"
38#include "applicationsfiltermodel.h"
38#include "applicationsfiltermodel.h"
39#include "inputeventgenerator.h"
39#include "inputeventgenerator.h"
40+#include "deviceconfigparser.h"
40
41
41static QObject *createWindowStateStorage(QQmlEngine *engine, QJSEngine *scriptEngine)
42static QObject *createWindowStateStorage(QQmlEngine *engine, QJSEngine *scriptEngine)
42{
43{
@@ -70,6 +71,7 @@ void UtilsPlugin::registerTypes(const char *uri)
70 qmlRegisterType<ActiveFocusLogger>(uri, 0, 1, "ActiveFocusLogger");
71 qmlRegisterType<ActiveFocusLogger>(uri, 0, 1, "ActiveFocusLogger");
71 qmlRegisterType<ApplicationsFilterModel>(uri, 0, 1, "ApplicationsFilterModel");
72 qmlRegisterType<ApplicationsFilterModel>(uri, 0, 1, "ApplicationsFilterModel");
72 qmlRegisterType<InputEventGenerator>(uri, 0, 1, "InputEventGenerator");
73 qmlRegisterType<InputEventGenerator>(uri, 0, 1, "InputEventGenerator");
74+ qmlRegisterType<DeviceConfigParser>(uri, 0, 1, "DeviceConfigParser");
73}
75}
74
76
75void UtilsPlugin::initializeEngine(QQmlEngine *engine, const char *uri)
77void UtilsPlugin::initializeEngine(QQmlEngine *engine, const char *uri)
qml/DeviceConfiguration.qml+12−9
@@ -15,6 +15,7 @@
15 */
15 */
16
16
17import QtQuick 2.4
17import QtQuick 2.4
18+import Utils 0.1
18
19
19QtObject {
20QtObject {
20 id: root
21 id: root
@@ -34,20 +35,22 @@ QtObject {
34
35
35 readonly property alias category: priv.category
36 readonly property alias category: priv.category
36
37
38+ readonly property var deviceConfigParser: DeviceConfigParser {
39+ name: root.name
40+ }
41+
37 readonly property var priv: StateGroup {
42 readonly property var priv: StateGroup {
38 id: priv
43 id: priv
39
44
40− property int primaryOrientation: root.useNativeOrientation
45+ property int primaryOrientation: deviceConfigParser.primaryOrientation == Qt.PrimaryOrientation ?
46+ root.useNativeOrientation : deviceConfigParser.primaryOrientation
41
47
42− property int supportedOrientations: Qt.PortraitOrientation
48+ property int supportedOrientations: deviceConfigParser.supportedOrientations
43− | Qt.InvertedPortraitOrientation
44− | Qt.LandscapeOrientation
45− | Qt.InvertedLandscapeOrientation
46
49
47− property int landscapeOrientation: Qt.LandscapeOrientation
50+ property int landscapeOrientation: deviceConfigParser.landscapeOrientation
48− property int invertedLandscapeOrientation: Qt.InvertedLandscapeOrientation
51+ property int invertedLandscapeOrientation: deviceConfigParser.invertedLandscapeOrientation
49− property int portraitOrientation: Qt.PortraitOrientation
52+ property int portraitOrientation: deviceConfigParser.portraitOrientation
50− property int invertedPortraitOrientation: Qt.InvertedPortraitOrientation
53+ property int invertedPortraitOrientation: deviceConfigParser.invertedPortraitOrientation
51
54
52 // Supported values so far:
55 // Supported values so far:
53 // "phone", "tablet" or "desktop"
56 // "phone", "tablet" or "desktop"
tests/mocks/Utils/CMakeLists.txt+1
@@ -18,6 +18,7 @@ set(QMLPLUGIN_SRC
18 ${CMAKE_SOURCE_DIR}/plugins/Utils/inputwatcher.cpp
18 ${CMAKE_SOURCE_DIR}/plugins/Utils/inputwatcher.cpp
19 ${CMAKE_SOURCE_DIR}/plugins/Utils/applicationsfiltermodel.cpp
19 ${CMAKE_SOURCE_DIR}/plugins/Utils/applicationsfiltermodel.cpp
20 ${CMAKE_SOURCE_DIR}/plugins/Utils/inputeventgenerator.cpp
20 ${CMAKE_SOURCE_DIR}/plugins/Utils/inputeventgenerator.cpp
21+ ${CMAKE_SOURCE_DIR}/plugins/Utils/deviceconfigparser.cpp
21 ${APPLICATION_API_INCLUDEDIR}/unity/shell/application/ApplicationManagerInterface.h
22 ${APPLICATION_API_INCLUDEDIR}/unity/shell/application/ApplicationManagerInterface.h
22 constants.cpp
23 constants.cpp
23 plugin.cpp
24 plugin.cpp
tests/mocks/Utils/plugin.cpp+2
@@ -37,6 +37,7 @@
37#include <easingcurve.h>
37#include <easingcurve.h>
38#include <applicationsfiltermodel.h>
38#include <applicationsfiltermodel.h>
39#include <inputeventgenerator.h>
39#include <inputeventgenerator.h>
40+#include <deviceconfigparser.h>
40
41
41static QObject *createWindowStateStorage(QQmlEngine *engine, QJSEngine *scriptEngine)
42static QObject *createWindowStateStorage(QQmlEngine *engine, QJSEngine *scriptEngine)
42{
43{
@@ -67,6 +68,7 @@ void FakeUtilsPlugin::registerTypes(const char *uri)
67 qmlRegisterType<ActiveFocusLogger>(uri, 0, 1, "ActiveFocusLogger");
68 qmlRegisterType<ActiveFocusLogger>(uri, 0, 1, "ActiveFocusLogger");
68 qmlRegisterType<ApplicationsFilterModel>(uri, 0, 1, "ApplicationsFilterModel");
69 qmlRegisterType<ApplicationsFilterModel>(uri, 0, 1, "ApplicationsFilterModel");
69 qmlRegisterType<InputEventGenerator>(uri, 0, 1, "InputEventGenerator");
70 qmlRegisterType<InputEventGenerator>(uri, 0, 1, "InputEventGenerator");
71+ qmlRegisterType<DeviceConfigParser>(uri, 0, 1, "DeviceConfigParser");
70}
72}
71
73
72void FakeUtilsPlugin::initializeEngine(QQmlEngine *engine, const char *uri)
74void FakeUtilsPlugin::initializeEngine(QQmlEngine *engine, const char *uri)