on 30441cf help sign in
unity8/plugins/Utils/deviceconfigparser.cpp
127 lines 4.1 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)
28QString DeviceConfigParser::name() const
30 return m_name;
33void DeviceConfigParser::setName(const QString &name)
35 if (m_name == name) {
36 return;
37 }
38 m_name = name;
39 Q_EMIT changed();
42Qt::ScreenOrientation DeviceConfigParser::primaryOrientation() const
44 return stringToOrientation(readOrientationFromConfig("PrimaryOrientation"), Qt::PrimaryOrientation);
47Qt::ScreenOrientations DeviceConfigParser::supportedOrientations() const
49 QStringList values = readOrientationsFromConfig("SupportedOrientations");
50 if (values.isEmpty()) {
51 return Qt::PortraitOrientation
52 | Qt::InvertedPortraitOrientation
53 | Qt::LandscapeOrientation
54 | Qt::InvertedLandscapeOrientation;
55 }
57 Qt::ScreenOrientations ret = Qt::PrimaryOrientation;
58 Q_FOREACH(const QString &orientationString, values) {
59 ret |= stringToOrientation(orientationString, Qt::PrimaryOrientation);
60 }
61 return ret;
64Qt::ScreenOrientation DeviceConfigParser::landscapeOrientation() const
66 return stringToOrientation(readOrientationFromConfig("LandscapeOrientation"), Qt::LandscapeOrientation);
69Qt::ScreenOrientation DeviceConfigParser::invertedLandscapeOrientation() const
71 return stringToOrientation(readOrientationFromConfig("InvertedLandscapeOrientation"), Qt::InvertedLandscapeOrientation);
74Qt::ScreenOrientation DeviceConfigParser::portraitOrientation() const
76 return stringToOrientation(readOrientationFromConfig("PortraitOrientation"), Qt::PortraitOrientation);
79Qt::ScreenOrientation DeviceConfigParser::invertedPortraitOrientation() const
81 return stringToOrientation(readOrientationFromConfig("InvertedPortraitOrientation"), Qt::InvertedPortraitOrientation);
84QStringList DeviceConfigParser::readOrientationsFromConfig(const QString &key) const
86 QFileInfo fi("./devices.conf");
87 QString path;
88 if (fi.exists()) {
89 path = "./devices.conf";
90 } else {
91 path = "/etc/ubuntu/devices.conf";
92 }
93 QSettings config(path, QSettings::IniFormat);
94 config.beginGroup(m_name);
96 if (config.contains(key)) {
97 return config.value(key).toStringList();
98 }
99 return QStringList();
102QString DeviceConfigParser::readOrientationFromConfig(const QString &key) const
104 QStringList ret = readOrientationsFromConfig(key);
105 return ret.count() > 0 ? ret.first() : QString();
108Qt::ScreenOrientation DeviceConfigParser::stringToOrientation(const QString &orientationString, Qt::ScreenOrientation defaultValue) const
110 if (orientationString == "Landscape") {
111 return Qt::LandscapeOrientation;
112 }
113 if (orientationString == "InvertedLandscape") {
114 return Qt::InvertedLandscapeOrientation;
115 }
116 if (orientationString == "Portrait") {
117 return Qt::PortraitOrientation;
118 }
119 if (orientationString == "InvertedPortrait") {
120 return Qt::InvertedPortraitOrientation;
121 }
122 if (!orientationString.isEmpty()) {
123 // Some option we don't know. Give some hint on what went wrong.
124 qWarning().nospace().noquote() << "Unknown option \"" << orientationString << "\". Supported options are: Landscape, InvertedLandscape, Portrait and InvertedPortrait.";
125 }
126 return defaultValue;