on 7fd5fcd help sign in
unity8/plugins/Utils/deviceconfigparser.cpp
143 lines 4.7 kB history raw
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 */
17#include "deviceconfigparser.h"
19#include <QSettings>
20#include <QFileInfo>
21#include <QDebug>
23DeviceConfigParser::DeviceConfigParser(QObject *parent): QObject(parent)
25 QString path;
26 if (QFileInfo("./devices.conf").exists()) {
27 path = "./devices.conf";
28 } else {
29 path = "/etc/ubuntu/devices.conf";
30 }
31 m_config = new QSettings(path, QSettings::IniFormat, this);
34QString DeviceConfigParser::name() const
36 return m_name;
39void DeviceConfigParser::setName(const QString &name)
41 if (m_name == name) {
42 return;
43 }
44 m_name = name;
45 Q_EMIT changed();
48Qt::ScreenOrientation DeviceConfigParser::primaryOrientation() const
50 return stringToOrientation(readOrientationFromConfig("PrimaryOrientation"), Qt::PrimaryOrientation);
53Qt::ScreenOrientations DeviceConfigParser::supportedOrientations() const
55 QStringList values = readOrientationsFromConfig("SupportedOrientations");
56 if (values.isEmpty()) {
57 return Qt::PortraitOrientation
58 | Qt::InvertedPortraitOrientation
59 | Qt::LandscapeOrientation
60 | Qt::InvertedLandscapeOrientation;
61 }
63 Qt::ScreenOrientations ret = Qt::PrimaryOrientation;
64 Q_FOREACH(const QString &orientationString, values) {
65 ret |= stringToOrientation(orientationString, Qt::PrimaryOrientation);
66 }
67 return ret;
70Qt::ScreenOrientation DeviceConfigParser::landscapeOrientation() const
72 return stringToOrientation(readOrientationFromConfig("LandscapeOrientation"), Qt::LandscapeOrientation);
75Qt::ScreenOrientation DeviceConfigParser::invertedLandscapeOrientation() const
77 return stringToOrientation(readOrientationFromConfig("InvertedLandscapeOrientation"), Qt::InvertedLandscapeOrientation);
80Qt::ScreenOrientation DeviceConfigParser::portraitOrientation() const
82 return stringToOrientation(readOrientationFromConfig("PortraitOrientation"), Qt::PortraitOrientation);
85Qt::ScreenOrientation DeviceConfigParser::invertedPortraitOrientation() const
87 return stringToOrientation(readOrientationFromConfig("InvertedPortraitOrientation"), Qt::InvertedPortraitOrientation);
90QString DeviceConfigParser::category() const
92 QStringList supportedValues = {"phone", "tablet", "desktop"};
93 m_config->beginGroup(m_name);
94 QString value = m_config->value("Category", "phone").toString();
95 if (!supportedValues.contains(value)) {
96 qDebug().nospace().noquote() << "Unknown option \"" << value << "\" in " << m_config->fileName()
97 << ". Supported options are: " << supportedValues.join(", ") << ".";
98 return "phone";
99 }
100 m_config->endGroup();
101 return value;
104QStringList DeviceConfigParser::readOrientationsFromConfig(const QString &key) const
106 m_config->beginGroup(m_name);
108 QStringList ret;
109 if (m_config->contains(key)) {
110 ret = m_config->value(key).toStringList();
111 }
113 m_config->endGroup();
114 return ret;
117QString DeviceConfigParser::readOrientationFromConfig(const QString &key) const
119 QStringList ret = readOrientationsFromConfig(key);
120 return ret.count() > 0 ? ret.first() : QString();
123Qt::ScreenOrientation DeviceConfigParser::stringToOrientation(const QString &orientationString, Qt::ScreenOrientation defaultValue) const
125 if (orientationString == "Landscape") {
126 return Qt::LandscapeOrientation;
127 }
128 if (orientationString == "InvertedLandscape") {
129 return Qt::InvertedLandscapeOrientation;
130 }
131 if (orientationString == "Portrait") {
132 return Qt::PortraitOrientation;
133 }
134 if (orientationString == "InvertedPortrait") {
135 return Qt::InvertedPortraitOrientation;
136 }
137 if (!orientationString.isEmpty()) {
138 // Some option we don't know. Give some hint on what went wrong.
139 qWarning().nospace().noquote() << "Unknown option \"" << orientationString << "\" in " << m_config->fileName()
140 << ". Supported options are: Landscape, InvertedLandscape, Portrait and InvertedPortrait.";
141 }
142 return defaultValue;