This repository was archived by the owner on Nov 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathdeviceconfigparser.cpp
194 lines (165 loc) · 5.99 KB
/
deviceconfigparser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* Copyright 2016 Canonical Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "deviceconfigparser.h"
#include <QSettings>
#include <QFileInfo>
#include <QDebug>
#include <QStandardPaths>
DeviceConfigParser::DeviceConfigParser(QObject *parent): QObject(parent)
{
// Local files have highest priority
QString path;
Q_FOREACH (const QString &standardPath, QStandardPaths::standardLocations(QStandardPaths::GenericConfigLocation)) {
if (QFileInfo::exists(standardPath + "/devices.conf")) {
path = standardPath + "/devices.conf";
break;
}
}
// Check if there is an override in the device tarball (/system/etc/)
if (path.isEmpty() && QFileInfo::exists("/system/etc/ubuntu/devices.conf")) {
path = "/system/etc/ubuntu/devices.conf";
}
// No higher priority files found. Use standard of /etc/
if (path.isEmpty()) {
path = "/etc/ubuntu/devices.conf";
}
qDebug() << "Using" << path << "as device configuration file";
m_config = new QSettings(path, QSettings::IniFormat, this);
}
QString DeviceConfigParser::name() const
{
return m_name;
}
void DeviceConfigParser::setName(const QString &name)
{
if (m_name == name) {
return;
}
m_name = name;
Q_EMIT changed();
}
Qt::ScreenOrientation DeviceConfigParser::primaryOrientation() const
{
return stringToOrientation(readOrientationFromConfig("PrimaryOrientation"), Qt::PrimaryOrientation);
}
Qt::ScreenOrientations DeviceConfigParser::supportedOrientations() const
{
QStringList values = readOrientationsFromConfig("SupportedOrientations");
if (values.isEmpty()) {
return Qt::PortraitOrientation
| Qt::InvertedPortraitOrientation
| Qt::LandscapeOrientation
| Qt::InvertedLandscapeOrientation;
}
Qt::ScreenOrientations ret = Qt::PrimaryOrientation;
Q_FOREACH(const QString &orientationString, values) {
ret |= stringToOrientation(orientationString, Qt::PrimaryOrientation);
}
return ret;
}
Qt::ScreenOrientation DeviceConfigParser::landscapeOrientation() const
{
return stringToOrientation(readOrientationFromConfig("LandscapeOrientation"), Qt::LandscapeOrientation);
}
Qt::ScreenOrientation DeviceConfigParser::invertedLandscapeOrientation() const
{
return stringToOrientation(readOrientationFromConfig("InvertedLandscapeOrientation"), Qt::InvertedLandscapeOrientation);
}
Qt::ScreenOrientation DeviceConfigParser::portraitOrientation() const
{
return stringToOrientation(readOrientationFromConfig("PortraitOrientation"), Qt::PortraitOrientation);
}
Qt::ScreenOrientation DeviceConfigParser::invertedPortraitOrientation() const
{
return stringToOrientation(readOrientationFromConfig("InvertedPortraitOrientation"), Qt::InvertedPortraitOrientation);
}
QString DeviceConfigParser::category() const
{
QStringList supportedValues = {"phone", "tablet", "desktop"};
m_config->beginGroup(m_name);
QString value = m_config->value("Category", "phone").toString();
if (!supportedValues.contains(value)) {
qWarning().nospace().noquote() << "Unknown option \"" << value << "\" in " << m_config->fileName()
<< ". Supported options are: " << supportedValues.join(", ") << ".";
return "phone";
}
m_config->endGroup();
return value;
}
bool DeviceConfigParser::supportsMultiColorLed() const
{
return readBoolFromConfig("SupportsMultiColorLed", true);
}
bool DeviceConfigParser::readBoolFromConfig(const QString &key, bool defaultValue) const
{
m_config->beginGroup(m_name);
bool ret = defaultValue;
if (m_config->contains(key)) {
ret = m_config->value(key).toBool();
}
m_config->endGroup();
return ret;
}
int DeviceConfigParser::topMargin() const
{
return readIntegerFromConfig("TopMargin", 0);
}
int DeviceConfigParser::readIntegerFromConfig(const QString &key, int defaultValue) const
{
m_config->beginGroup(m_name);
int ret = defaultValue;
if (m_config->contains(key)) {
ret = m_config->value(key).toInt();
}
m_config->endGroup();
return ret;
}
QStringList DeviceConfigParser::readOrientationsFromConfig(const QString &key) const
{
m_config->beginGroup(m_name);
QStringList ret;
if (m_config->contains(key)) {
ret = m_config->value(key).toStringList();
}
m_config->endGroup();
return ret;
}
QString DeviceConfigParser::readOrientationFromConfig(const QString &key) const
{
QStringList ret = readOrientationsFromConfig(key);
return ret.count() > 0 ? ret.first() : QString();
}
Qt::ScreenOrientation DeviceConfigParser::stringToOrientation(const QString &orientationString, Qt::ScreenOrientation defaultValue) const
{
if (orientationString == "Landscape") {
return Qt::LandscapeOrientation;
}
if (orientationString == "InvertedLandscape") {
return Qt::InvertedLandscapeOrientation;
}
if (orientationString == "Portrait") {
return Qt::PortraitOrientation;
}
if (orientationString == "InvertedPortrait") {
return Qt::InvertedPortraitOrientation;
}
if (!orientationString.isEmpty()) {
// Some option we don't know. Give some hint on what went wrong.
qWarning().nospace().noquote() << "Unknown option \"" << orientationString << "\" in " << m_config->fileName()
<< ". Supported options are: Landscape, InvertedLandscape, Portrait and InvertedPortrait.";
}
return defaultValue;
}