Unity 8
deviceconfigparser.cpp
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  */
16 
17 #include "deviceconfigparser.h"
18 
19 #include <QSettings>
20 #include <QFileInfo>
21 #include <QDebug>
22 #include <QStandardPaths>
23 
24 DeviceConfigParser::DeviceConfigParser(QObject *parent): QObject(parent)
25 {
26  // Local files have highest priority
27  QString path;
28  Q_FOREACH (const QString &standardPath, QStandardPaths::standardLocations(QStandardPaths::GenericConfigLocation)) {
29  if (QFileInfo(standardPath + "/devices.conf").exists()) {
30  path = standardPath + "/devices.conf";
31  break;
32  }
33  }
34 
35  // Check if there is an override in the device tarball (/system/etc/)
36  if (path.isEmpty() && QFileInfo::exists("/system/etc/ubuntu/devices.conf")) {
37  path = "/system/etc/ubuntu/devices.conf";
38  }
39 
40  // No higher priority files found. Use standard of /etc/
41  if (path.isEmpty()) {
42  path = "/etc/ubuntu/devices.conf";
43  }
44 
45  qDebug() << "Using" << path << "as device configuration file";
46  m_config = new QSettings(path, QSettings::IniFormat, this);
47 }
48 
49 QString DeviceConfigParser::name() const
50 {
51  return m_name;
52 }
53 
54 void DeviceConfigParser::setName(const QString &name)
55 {
56  if (m_name == name) {
57  return;
58  }
59  m_name = name;
60  Q_EMIT changed();
61 }
62 
63 Qt::ScreenOrientation DeviceConfigParser::primaryOrientation() const
64 {
65  return stringToOrientation(readOrientationFromConfig("PrimaryOrientation"), Qt::PrimaryOrientation);
66 }
67 
68 Qt::ScreenOrientations DeviceConfigParser::supportedOrientations() const
69 {
70  QStringList values = readOrientationsFromConfig("SupportedOrientations");
71  if (values.isEmpty()) {
72  return Qt::PortraitOrientation
73  | Qt::InvertedPortraitOrientation
74  | Qt::LandscapeOrientation
75  | Qt::InvertedLandscapeOrientation;
76  }
77 
78  Qt::ScreenOrientations ret = Qt::PrimaryOrientation;
79  Q_FOREACH(const QString &orientationString, values) {
80  ret |= stringToOrientation(orientationString, Qt::PrimaryOrientation);
81  }
82  return ret;
83 }
84 
85 Qt::ScreenOrientation DeviceConfigParser::landscapeOrientation() const
86 {
87  return stringToOrientation(readOrientationFromConfig("LandscapeOrientation"), Qt::LandscapeOrientation);
88 }
89 
90 Qt::ScreenOrientation DeviceConfigParser::invertedLandscapeOrientation() const
91 {
92  return stringToOrientation(readOrientationFromConfig("InvertedLandscapeOrientation"), Qt::InvertedLandscapeOrientation);
93 }
94 
95 Qt::ScreenOrientation DeviceConfigParser::portraitOrientation() const
96 {
97  return stringToOrientation(readOrientationFromConfig("PortraitOrientation"), Qt::PortraitOrientation);
98 }
99 
100 Qt::ScreenOrientation DeviceConfigParser::invertedPortraitOrientation() const
101 {
102  return stringToOrientation(readOrientationFromConfig("InvertedPortraitOrientation"), Qt::InvertedPortraitOrientation);
103 }
104 
105 QString DeviceConfigParser::category() const
106 {
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(", ") << ".";
113  return "phone";
114  }
115  m_config->endGroup();
116  return value;
117 }
118 
119 QStringList DeviceConfigParser::readOrientationsFromConfig(const QString &key) const
120 {
121  m_config->beginGroup(m_name);
122 
123  QStringList ret;
124  if (m_config->contains(key)) {
125  ret = m_config->value(key).toStringList();
126  }
127 
128  m_config->endGroup();
129  return ret;
130 }
131 
132 QString DeviceConfigParser::readOrientationFromConfig(const QString &key) const
133 {
134  QStringList ret = readOrientationsFromConfig(key);
135  return ret.count() > 0 ? ret.first() : QString();
136 }
137 
138 Qt::ScreenOrientation DeviceConfigParser::stringToOrientation(const QString &orientationString, Qt::ScreenOrientation defaultValue) const
139 {
140  if (orientationString == "Landscape") {
141  return Qt::LandscapeOrientation;
142  }
143  if (orientationString == "InvertedLandscape") {
144  return Qt::InvertedLandscapeOrientation;
145  }
146  if (orientationString == "Portrait") {
147  return Qt::PortraitOrientation;
148  }
149  if (orientationString == "InvertedPortrait") {
150  return Qt::InvertedPortraitOrientation;
151  }
152  if (!orientationString.isEmpty()) {
153  // Some option we don't know. Give some hint on what went wrong.
154  qWarning().nospace().noquote() << "Unknown option \"" << orientationString << "\" in " << m_config->fileName()
155  << ". Supported options are: Landscape, InvertedLandscape, Portrait and InvertedPortrait.";
156  }
157  return defaultValue;
158 }