@@ -0,0 +1,150 @@
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+#include "deviceconfigparser.h"
18+
19+#include <QSettings>
20+#include <QFileInfo>
21+#include <QDebug>
22+#include <QStandardPaths>
23+
24+DeviceConfigParser::DeviceConfigParser(QObject *parent): QObject(parent)
25+{
26+ QString path;
27+ Q_FOREACH (const QString &standardPath, QStandardPaths::standardLocations(QStandardPaths::GenericConfigLocation)) {
28+ if (QFileInfo(standardPath + "/devices.conf").exists()) {
29+ path = standardPath + "/devices.conf";
30+ break;
31+ }
32+ }
33+
34+ if (path.isEmpty()) {
35+ path = "/etc/ubuntu/devices.conf";
36+ }
37+ qDebug() << "Using" << path << "as device configuration file";
38+ m_config = new QSettings(path, QSettings::IniFormat, this);
39+}
40+
41+QString DeviceConfigParser::name() const
42+{
43+ return m_name;
44+}
45+
46+void DeviceConfigParser::setName(const QString &name)
47+{
48+ if (m_name == name) {
49+ return;
50+ }
51+ m_name = name;
52+ Q_EMIT changed();
53+}
54+
55+Qt::ScreenOrientation DeviceConfigParser::primaryOrientation() const
56+{
57+ return stringToOrientation(readOrientationFromConfig("PrimaryOrientation"), Qt::PrimaryOrientation);
58+}
59+
60+Qt::ScreenOrientations DeviceConfigParser::supportedOrientations() const
61+{
62+ QStringList values = readOrientationsFromConfig("SupportedOrientations");
63+ if (values.isEmpty()) {
64+ return Qt::PortraitOrientation
65+ | Qt::InvertedPortraitOrientation
66+ | Qt::LandscapeOrientation
67+ | Qt::InvertedLandscapeOrientation;
68+ }
69+
70+ Qt::ScreenOrientations ret = Qt::PrimaryOrientation;
71+ Q_FOREACH(const QString &orientationString, values) {
72+ ret |= stringToOrientation(orientationString, Qt::PrimaryOrientation);
73+ }
74+ return ret;
75+}
76+
77+Qt::ScreenOrientation DeviceConfigParser::landscapeOrientation() const
78+{
79+ return stringToOrientation(readOrientationFromConfig("LandscapeOrientation"), Qt::LandscapeOrientation);
80+}
81+
82+Qt::ScreenOrientation DeviceConfigParser::invertedLandscapeOrientation() const
83+{
84+ return stringToOrientation(readOrientationFromConfig("InvertedLandscapeOrientation"), Qt::InvertedLandscapeOrientation);
85+}
86+
87+Qt::ScreenOrientation DeviceConfigParser::portraitOrientation() const
88+{
89+ return stringToOrientation(readOrientationFromConfig("PortraitOrientation"), Qt::PortraitOrientation);
90+}
91+
92+Qt::ScreenOrientation DeviceConfigParser::invertedPortraitOrientation() const
93+{
94+ return stringToOrientation(readOrientationFromConfig("InvertedPortraitOrientation"), Qt::InvertedPortraitOrientation);
95+}
96+
97+QString DeviceConfigParser::category() const
98+{
99+ QStringList supportedValues = {"phone", "tablet", "desktop"};
100+ m_config->beginGroup(m_name);
101+ QString value = m_config->value("Category", "phone").toString();
102+ if (!supportedValues.contains(value)) {
103+ qWarning().nospace().noquote() << "Unknown option \"" << value << "\" in " << m_config->fileName()
104+ << ". Supported options are: " << supportedValues.join(", ") << ".";
105+ return "phone";
106+ }
107+ m_config->endGroup();
108+ return value;
109+}
110+
111+QStringList DeviceConfigParser::readOrientationsFromConfig(const QString &key) const
112+{
113+ m_config->beginGroup(m_name);
114+
115+ QStringList ret;
116+ if (m_config->contains(key)) {
117+ ret = m_config->value(key).toStringList();
118+ }
119+
120+ m_config->endGroup();
121+ return ret;
122+}
123+
124+QString DeviceConfigParser::readOrientationFromConfig(const QString &key) const
125+{
126+ QStringList ret = readOrientationsFromConfig(key);
127+ return ret.count() > 0 ? ret.first() : QString();
128+}
129+
130+Qt::ScreenOrientation DeviceConfigParser::stringToOrientation(const QString &orientationString, Qt::ScreenOrientation defaultValue) const
131+{
132+ if (orientationString == "Landscape") {
133+ return Qt::LandscapeOrientation;
134+ }
135+ if (orientationString == "InvertedLandscape") {
136+ return Qt::InvertedLandscapeOrientation;
137+ }
138+ if (orientationString == "Portrait") {
139+ return Qt::PortraitOrientation;
140+ }
141+ if (orientationString == "InvertedPortrait") {
142+ return Qt::InvertedPortraitOrientation;
143+ }
144+ if (!orientationString.isEmpty()) {
145+ // Some option we don't know. Give some hint on what went wrong.
146+ qWarning().nospace().noquote() << "Unknown option \"" << orientationString << "\" in " << m_config->fileName()
147+ << ". Supported options are: Landscape, InvertedLandscape, Portrait and InvertedPortrait.";
148+ }
149+ return defaultValue;
150+}