17 #include "deviceconfigparser.h" 22 #include <QStandardPaths> 24 DeviceConfigParser::DeviceConfigParser(QObject *parent): QObject(parent)
27 Q_FOREACH (
const QString &standardPath, QStandardPaths::standardLocations(QStandardPaths::GenericConfigLocation)) {
28 if (QFileInfo(standardPath +
"/devices.conf").exists()) {
29 path = standardPath +
"/devices.conf";
35 path =
"/etc/ubuntu/devices.conf";
37 qDebug() <<
"Using" << path <<
"as device configuration file";
38 m_config =
new QSettings(path, QSettings::IniFormat,
this);
41 QString DeviceConfigParser::name()
const 46 void DeviceConfigParser::setName(
const QString &name)
55 Qt::ScreenOrientation DeviceConfigParser::primaryOrientation()
const 57 return stringToOrientation(readOrientationFromConfig(
"PrimaryOrientation"), Qt::PrimaryOrientation);
60 Qt::ScreenOrientations DeviceConfigParser::supportedOrientations()
const 62 QStringList values = readOrientationsFromConfig(
"SupportedOrientations");
63 if (values.isEmpty()) {
64 return Qt::PortraitOrientation
65 | Qt::InvertedPortraitOrientation
66 | Qt::LandscapeOrientation
67 | Qt::InvertedLandscapeOrientation;
70 Qt::ScreenOrientations ret = Qt::PrimaryOrientation;
71 Q_FOREACH(
const QString &orientationString, values) {
72 ret |= stringToOrientation(orientationString, Qt::PrimaryOrientation);
77 Qt::ScreenOrientation DeviceConfigParser::landscapeOrientation()
const 79 return stringToOrientation(readOrientationFromConfig(
"LandscapeOrientation"), Qt::LandscapeOrientation);
82 Qt::ScreenOrientation DeviceConfigParser::invertedLandscapeOrientation()
const 84 return stringToOrientation(readOrientationFromConfig(
"InvertedLandscapeOrientation"), Qt::InvertedLandscapeOrientation);
87 Qt::ScreenOrientation DeviceConfigParser::portraitOrientation()
const 89 return stringToOrientation(readOrientationFromConfig(
"PortraitOrientation"), Qt::PortraitOrientation);
92 Qt::ScreenOrientation DeviceConfigParser::invertedPortraitOrientation()
const 94 return stringToOrientation(readOrientationFromConfig(
"InvertedPortraitOrientation"), Qt::InvertedPortraitOrientation);
97 QString DeviceConfigParser::category()
const 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(
", ") <<
".";
107 m_config->endGroup();
111 QStringList DeviceConfigParser::readOrientationsFromConfig(
const QString &key)
const 113 m_config->beginGroup(m_name);
116 if (m_config->contains(key)) {
117 ret = m_config->value(key).toStringList();
120 m_config->endGroup();
124 QString DeviceConfigParser::readOrientationFromConfig(
const QString &key)
const 126 QStringList ret = readOrientationsFromConfig(key);
127 return ret.count() > 0 ? ret.first() : QString();
130 Qt::ScreenOrientation DeviceConfigParser::stringToOrientation(
const QString &orientationString, Qt::ScreenOrientation defaultValue)
const 132 if (orientationString ==
"Landscape") {
133 return Qt::LandscapeOrientation;
135 if (orientationString ==
"InvertedLandscape") {
136 return Qt::InvertedLandscapeOrientation;
138 if (orientationString ==
"Portrait") {
139 return Qt::PortraitOrientation;
141 if (orientationString ==
"InvertedPortrait") {
142 return Qt::InvertedPortraitOrientation;
144 if (!orientationString.isEmpty()) {
146 qWarning().nospace().noquote() <<
"Unknown option \"" << orientationString <<
"\" in " << m_config->fileName()
147 <<
". Supported options are: Landscape, InvertedLandscape, Portrait and InvertedPortrait.";