17 #include "deviceconfigparser.h" 22 #include <QStandardPaths> 24 DeviceConfigParser::DeviceConfigParser(QObject *parent): QObject(parent)
28 Q_FOREACH (
const QString &standardPath, QStandardPaths::standardLocations(QStandardPaths::GenericConfigLocation)) {
29 if (QFileInfo::exists(standardPath +
"/devices.conf")) {
30 path = standardPath +
"/devices.conf";
36 if (path.isEmpty() && QFileInfo::exists(
"/system/etc/ubuntu/devices.conf")) {
37 path =
"/system/etc/ubuntu/devices.conf";
42 path =
"/etc/ubuntu/devices.conf";
45 qDebug() <<
"Using" << path <<
"as device configuration file";
46 m_config =
new QSettings(path, QSettings::IniFormat,
this);
49 QString DeviceConfigParser::name()
const 54 void DeviceConfigParser::setName(
const QString &name)
63 Qt::ScreenOrientation DeviceConfigParser::primaryOrientation()
const 65 return stringToOrientation(readOrientationFromConfig(
"PrimaryOrientation"), Qt::PrimaryOrientation);
68 Qt::ScreenOrientations DeviceConfigParser::supportedOrientations()
const 70 QStringList values = readOrientationsFromConfig(
"SupportedOrientations");
71 if (values.isEmpty()) {
72 return Qt::PortraitOrientation
73 | Qt::InvertedPortraitOrientation
74 | Qt::LandscapeOrientation
75 | Qt::InvertedLandscapeOrientation;
78 Qt::ScreenOrientations ret = Qt::PrimaryOrientation;
79 Q_FOREACH(
const QString &orientationString, values) {
80 ret |= stringToOrientation(orientationString, Qt::PrimaryOrientation);
85 Qt::ScreenOrientation DeviceConfigParser::landscapeOrientation()
const 87 return stringToOrientation(readOrientationFromConfig(
"LandscapeOrientation"), Qt::LandscapeOrientation);
90 Qt::ScreenOrientation DeviceConfigParser::invertedLandscapeOrientation()
const 92 return stringToOrientation(readOrientationFromConfig(
"InvertedLandscapeOrientation"), Qt::InvertedLandscapeOrientation);
95 Qt::ScreenOrientation DeviceConfigParser::portraitOrientation()
const 97 return stringToOrientation(readOrientationFromConfig(
"PortraitOrientation"), Qt::PortraitOrientation);
100 Qt::ScreenOrientation DeviceConfigParser::invertedPortraitOrientation()
const 102 return stringToOrientation(readOrientationFromConfig(
"InvertedPortraitOrientation"), Qt::InvertedPortraitOrientation);
105 QString DeviceConfigParser::category()
const 107 QStringList supportedValues = {
"phone",
"tablet",
"desktop"};
108 m_config->beginGroup(m_name);
109 QString value = m_config->value(
"Category",
"phone").toString();
110 if (!supportedValues.contains(value)) {
111 qWarning().nospace().noquote() <<
"Unknown option \"" << value <<
"\" in " << m_config->fileName()
112 <<
". Supported options are: " << supportedValues.join(
", ") <<
".";
115 m_config->endGroup();
119 bool DeviceConfigParser::supportsMultiColorLed()
const 121 return readBoolFromConfig(
"SupportsMultiColorLed",
true);
124 bool DeviceConfigParser::readBoolFromConfig(
const QString &key,
bool defaultValue)
const 126 m_config->beginGroup(m_name);
128 bool ret = defaultValue;
129 if (m_config->contains(key)) {
130 ret = m_config->value(key).toBool();
133 m_config->endGroup();
137 QStringList DeviceConfigParser::readOrientationsFromConfig(
const QString &key)
const 139 m_config->beginGroup(m_name);
142 if (m_config->contains(key)) {
143 ret = m_config->value(key).toStringList();
146 m_config->endGroup();
150 QString DeviceConfigParser::readOrientationFromConfig(
const QString &key)
const 152 QStringList ret = readOrientationsFromConfig(key);
153 return ret.count() > 0 ? ret.first() : QString();
156 Qt::ScreenOrientation DeviceConfigParser::stringToOrientation(
const QString &orientationString, Qt::ScreenOrientation defaultValue)
const 158 if (orientationString ==
"Landscape") {
159 return Qt::LandscapeOrientation;
161 if (orientationString ==
"InvertedLandscape") {
162 return Qt::InvertedLandscapeOrientation;
164 if (orientationString ==
"Portrait") {
165 return Qt::PortraitOrientation;
167 if (orientationString ==
"InvertedPortrait") {
168 return Qt::InvertedPortraitOrientation;
170 if (!orientationString.isEmpty()) {
172 qWarning().nospace().noquote() <<
"Unknown option \"" << orientationString <<
"\" in " << m_config->fileName()
173 <<
". Supported options are: Landscape, InvertedLandscape, Portrait and InvertedPortrait.";