Skip to content

Latest commit

 

History

History
365 lines (297 loc) · 11 KB

aboutsettings.cpp

File metadata and controls

365 lines (297 loc) · 11 KB
 
May 11, 2013
May 11, 2013
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
/*
* Copyright (C) 2013 Jolla Ltd. <pekka.vuorela@jollamobile.com>
*
* You may use this file under the terms of the BSD license as follows:
*
* "Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Nemo Mobile nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
*/
Aug 20, 2013
Aug 20, 2013
32
#include "aboutsettings.h"
Oct 14, 2019
Oct 14, 2019
33
#include "aboutsettings_p.h"
Aug 20, 2013
Aug 20, 2013
34
May 11, 2013
May 11, 2013
35
36
#include <QDebug>
#include <QStringList>
Oct 14, 2019
Oct 14, 2019
37
Aug 20, 2013
Aug 20, 2013
38
#include <QNetworkInfo>
Oct 14, 2019
Oct 14, 2019
39
Aug 20, 2013
Aug 20, 2013
40
#include <QDeviceInfo>
Oct 25, 2013
Oct 25, 2013
41
42
#include <QFile>
#include <QByteArray>
Nov 4, 2013
Nov 4, 2013
43
#include <QRegularExpression>
Mar 20, 2015
Mar 20, 2015
44
45
#include <QMap>
#include <QTextStream>
Jun 4, 2015
Jun 4, 2015
46
#include <QVariant>
May 26, 2016
May 26, 2016
47
#include <QSettings>
Mar 8, 2019
Mar 8, 2019
48
#include <QTimer>
Jun 4, 2015
Jun 4, 2015
49
Oct 2, 2015
Oct 2, 2015
50
51
52
namespace
{
Jan 25, 2016
Jan 25, 2016
53
void parseReleaseFile(const QString &filename, QMap<QString, QString> *result)
Mar 20, 2015
Mar 20, 2015
54
{
Jan 25, 2016
Jan 25, 2016
55
56
57
if (!result->isEmpty()) {
return;
}
Mar 20, 2015
Mar 20, 2015
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
// Specification of the format:
// http://www.freedesktop.org/software/systemd/man/os-release.html
QFile release(filename);
if (release.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&release);
// "All strings should be in UTF-8 format, and non-printable characters
// should not be used."
in.setCodec("UTF-8");
while (!in.atEnd()) {
QString line = in.readLine();
// "Lines beginning with "#" shall be ignored as comments."
if (line.startsWith('#')) {
continue;
}
QString key = line.section('=', 0, 0);
QString value = line.section('=', 1);
// Remove trailing whitespace in value
value = value.trimmed();
// POSIX.1-2001 says uppercase, digits and underscores.
//
// Bash uses "[a-zA-Z_]+[a-zA-Z0-9_]*", so we'll use that too,
// as we can safely assume that "shell-compatible variable
// assignments" means it should be compatible with bash.
//
// see http://stackoverflow.com/a/2821183
// and http://stackoverflow.com/a/2821201
if (!QRegExp("[a-zA-Z_]+[a-zA-Z0-9_]*").exactMatch(key)) {
qWarning("Invalid key in input line: '%s'", qPrintable(line));
continue;
}
// "Variable assignment values should be enclosed in double or
// single quotes if they include spaces, semicolons or other
// special characters outside of A-Z, a-z, 0-9."
if (((value.at(0) == '\'') || (value.at(0) == '"'))) {
if (value.at(0) != value.at(value.size() - 1)) {
qWarning("Quoting error in input line: '%s'", qPrintable(line));
continue;
}
// Remove the quotes
value = value.mid(1, value.size() - 2);
}
// "If double or single quotes or backslashes are to be used within
// variable assignments, they should be escaped with backslashes,
// following shell style."
value = value.replace(QRegularExpression("\\\\(.)"), "\\1");
Jan 25, 2016
Jan 25, 2016
115
(*result)[key] = value;
Mar 20, 2015
Mar 20, 2015
116
117
118
119
120
121
}
release.close();
}
}
Sep 19, 2019
Sep 19, 2019
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
void parseLocalizationFile(const QString &filename, QMap<QString, QString> *result)
{
if (!result->isEmpty()) {
return;
}
if (!QFile(filename).exists()) {
return;
}
QSettings localizations(filename, QSettings::IniFormat);
localizations.setIniCodec("UTF-8");
QStringList languages = QLocale::system().uiLanguages();
QStringList availableLanguages;
for (auto it = languages.crbegin(); it != languages.crend(); ++it) {
const auto &lang = *it;
if (localizations.childGroups().contains(lang)) {
availableLanguages.append(lang);
}
}
// Gradually load localizations, overridding least preferred with most preferred ones
for (const auto &lang : availableLanguages) {
localizations.beginGroup(lang);
for (const auto &key : localizations.childKeys()) {
result->insert(key, localizations.value(key).toString());
}
localizations.endGroup();
}
}
Oct 2, 2015
Oct 2, 2015
155
}
May 11, 2013
May 11, 2013
156
Oct 14, 2019
Oct 14, 2019
157
158
159
160
161
162
163
164
165
166
167
168
169
AboutSettingsPrivate::AboutSettingsPrivate(QObject *parent)
: QObject(parent)
{
}
AboutSettingsPrivate::~AboutSettingsPrivate()
{
}
May 11, 2013
May 11, 2013
170
AboutSettings::AboutSettings(QObject *parent)
Oct 14, 2019
Oct 14, 2019
171
172
: QObject(parent)
, d_ptr(new AboutSettingsPrivate(this))
May 11, 2013
May 11, 2013
173
{
Oct 14, 2019
Oct 14, 2019
174
Q_D(AboutSettings);
May 26, 2016
May 26, 2016
175
QSettings settings(QStringLiteral("/mnt/vendor_data/vendor-data.ini"), QSettings::IniFormat);
Oct 14, 2019
Oct 14, 2019
176
177
d->vendorName = settings.value(QStringLiteral("Name")).toString();
d->vendorVersion = settings.value(QStringLiteral("Version")).toString();
May 26, 2016
May 26, 2016
178
Oct 2, 2015
Oct 2, 2015
179
refreshStorageModels();
Mar 8, 2019
Mar 8, 2019
180
Oct 14, 2019
Oct 14, 2019
181
connect(&d->partitionManager, &PartitionManager::partitionAdded,
Mar 8, 2019
Mar 8, 2019
182
this, &AboutSettings::partitionCountChanged);
Oct 14, 2019
Oct 14, 2019
183
connect(&d->partitionManager, &PartitionManager::partitionRemoved,
Mar 8, 2019
Mar 8, 2019
184
this, &AboutSettings::partitionCountChanged);
May 11, 2013
May 11, 2013
185
186
187
188
189
190
191
192
}
AboutSettings::~AboutSettings()
{
}
qlonglong AboutSettings::totalDiskSpace() const
{
Oct 14, 2019
Oct 14, 2019
193
194
Q_D(const AboutSettings);
return d->partitionManager.root().bytesTotal();
May 11, 2013
May 11, 2013
195
196
197
198
}
qlonglong AboutSettings::availableDiskSpace() const
{
Oct 14, 2019
Oct 14, 2019
199
200
Q_D(const AboutSettings);
return d->partitionManager.root().bytesAvailable();
May 11, 2013
May 11, 2013
201
202
}
Jun 4, 2015
Jun 4, 2015
203
204
QVariant AboutSettings::diskUsageModel() const
{
Oct 14, 2019
Oct 14, 2019
205
206
Q_D(const AboutSettings);
return d->internalStorage;
Oct 2, 2015
Oct 2, 2015
207
}
Jun 4, 2015
Jun 4, 2015
208
Oct 25, 2013
Oct 25, 2013
209
QString AboutSettings::wlanMacAddress() const
May 11, 2013
May 11, 2013
210
{
Oct 14, 2019
Oct 14, 2019
211
212
Q_D(const AboutSettings);
return d->networkInfo.macAddress(QNetworkInfo::WlanMode, 0);
May 11, 2013
May 11, 2013
213
214
}
Oct 25, 2013
Oct 25, 2013
215
QString AboutSettings::imei() const
May 11, 2013
May 11, 2013
216
{
Oct 14, 2019
Oct 14, 2019
217
218
Q_D(const AboutSettings);
return d->deviceInfo.imei(0);
May 11, 2013
May 11, 2013
219
220
}
May 5, 2015
May 5, 2015
221
222
QString AboutSettings::serial() const
{
Mar 22, 2018
Mar 22, 2018
223
224
225
226
227
// TODO: eventually we should use QDeviceInfo's uniqueDeviceID()
QStringList serialFiles;
serialFiles
Oct 19, 2018
Oct 19, 2018
228
229
// Old location for serial number that was used by e.g.
// Jolla Tablet, that should not be used anymore.
Mar 22, 2018
Mar 22, 2018
230
<< "/config/serial/serial.txt"
Oct 19, 2018
Oct 19, 2018
231
232
233
234
// Location for serialnumber file that should be preferred if no /sys
// node or something for it. The means how the serialnumber ends to
// this file are device specific.
<< "/run/config/serial"
Mar 22, 2018
Mar 22, 2018
235
236
237
238
239
240
241
// Some devices have serialno in this path.
<< "/sys/firmware/devicetree/base/firmware/android/serialno";
for (const QString &serialFile : serialFiles) {
QFile serialTxt(serialFile);
if (serialTxt.exists() && serialTxt.open(QIODevice::ReadOnly))
return QString::fromUtf8(serialTxt.readAll()).trimmed();
May 5, 2015
May 5, 2015
242
}
Mar 22, 2018
Mar 22, 2018
243
244
return QString();
May 5, 2015
May 5, 2015
245
246
}
Sep 19, 2019
Sep 19, 2019
247
248
QString AboutSettings::localizedOperatingSystemName() const
{
Oct 14, 2019
Oct 14, 2019
249
250
Q_D(const AboutSettings);
parseLocalizationFile(QStringLiteral("/etc/os-release-l10n"), &d->osReleaseLocalization);
Sep 19, 2019
Sep 19, 2019
251
Oct 14, 2019
Oct 14, 2019
252
return d->osReleaseLocalization.value("NAME", operatingSystemName());
Sep 19, 2019
Sep 19, 2019
253
254
}
Jul 2, 2019
Jul 2, 2019
255
256
257
258
259
260
261
262
263
QString AboutSettings::baseOperatingSystemName() const
{
QString osName = operatingSystemName();
if (osName.endsWith(QStringLiteral(" OS"))) {
osName.chop(3);
}
return osName;
}
Jan 24, 2018
Jan 24, 2018
264
265
QString AboutSettings::operatingSystemName() const
{
Oct 14, 2019
Oct 14, 2019
266
267
Q_D(const AboutSettings);
parseReleaseFile(QStringLiteral("/etc/os-release"), &d->osRelease);
Jan 24, 2018
Jan 24, 2018
268
Oct 14, 2019
Oct 14, 2019
269
return d->osRelease["NAME"];
Jan 24, 2018
Jan 24, 2018
270
271
}
Oct 18, 2019
Oct 18, 2019
272
273
274
275
276
277
278
279
QString AboutSettings::localizedSoftwareVersion() const
{
Q_D(const AboutSettings);
parseLocalizationFile(QStringLiteral("/etc/os-release-l10n"), &d->osReleaseLocalization);
return d->osReleaseLocalization.value("VERSION", softwareVersion());
}
Oct 25, 2013
Oct 25, 2013
280
281
QString AboutSettings::softwareVersion() const
{
Oct 14, 2019
Oct 14, 2019
282
283
Q_D(const AboutSettings);
parseReleaseFile(QStringLiteral("/etc/os-release"), &d->osRelease);
Jan 25, 2016
Jan 25, 2016
284
Oct 14, 2019
Oct 14, 2019
285
return d->osRelease["VERSION"];
Jan 25, 2016
Jan 25, 2016
286
287
288
289
}
QString AboutSettings::softwareVersionId() const
{
Oct 14, 2019
Oct 14, 2019
290
291
Q_D(const AboutSettings);
parseReleaseFile(QStringLiteral("/etc/os-release"), &d->osRelease);
Jan 25, 2016
Jan 25, 2016
292
Oct 14, 2019
Oct 14, 2019
293
return d->osRelease["VERSION_ID"];
Mar 20, 2015
Mar 20, 2015
294
}
Oct 25, 2013
Oct 25, 2013
295
Mar 20, 2015
Mar 20, 2015
296
297
QString AboutSettings::adaptationVersion() const
{
Oct 14, 2019
Oct 14, 2019
298
299
Q_D(const AboutSettings);
parseReleaseFile(QStringLiteral("/etc/hw-release"), &d->hardwareRelease);
Jan 25, 2016
Jan 25, 2016
300
Oct 14, 2019
Oct 14, 2019
301
return d->hardwareRelease["VERSION_ID"];
Oct 25, 2013
Oct 25, 2013
302
}
Oct 2, 2015
Oct 2, 2015
303
May 26, 2016
May 26, 2016
304
305
QString AboutSettings::vendorName() const
{
Oct 14, 2019
Oct 14, 2019
306
307
Q_D(const AboutSettings);
return d->vendorName;
May 26, 2016
May 26, 2016
308
309
310
311
}
QString AboutSettings::vendorVersion() const
{
Oct 14, 2019
Oct 14, 2019
312
313
Q_D(const AboutSettings);
return d->vendorVersion;
May 26, 2016
May 26, 2016
314
315
}
Oct 2, 2015
Oct 2, 2015
316
void AboutSettings::refreshStorageModels()
Mar 8, 2019
Mar 8, 2019
317
{
Oct 14, 2019
Oct 14, 2019
318
319
Q_D(AboutSettings);
d->partitionManager.refresh();
Mar 8, 2019
Mar 8, 2019
320
321
322
323
324
325
326
327
328
329
330
331
partitionCountChanged();
}
void AboutSettings::partitionCountChanged()
{
// Queue the method invocation in case several list changes are made consecutively, so that
// the list is only reloaded once.
QTimer::singleShot(0, this, &AboutSettings::reloadStorageLists);
}
void AboutSettings::reloadStorageLists()
Oct 2, 2015
Oct 2, 2015
332
{
Oct 14, 2019
Oct 14, 2019
333
334
Q_D(AboutSettings);
d->internalStorage.clear();
Oct 2, 2015
Oct 2, 2015
335
Oct 14, 2019
Oct 14, 2019
336
for (auto partition : d->partitionManager.partitions()) {
Mar 23, 2016
Mar 23, 2016
337
QVariantMap row;
Jul 5, 2016
Jul 5, 2016
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
row[QStringLiteral("mounted")] = partition.status() == Partition::Mounted;
row[QStringLiteral("path")] = partition.mountPath();
row[QStringLiteral("available")] = partition.bytesAvailable();
row[QStringLiteral("total")] = partition.bytesTotal();
row[QStringLiteral("filesystem")] = partition.filesystemType();
row[QStringLiteral("devicePath")] = partition.devicePath();
row[QStringLiteral("storageType")] = [&partition]() {
switch (partition.storageType()) {
case Partition::System:
return QStringLiteral("system");
case Partition::User:
return QStringLiteral("user");
case Partition::Mass:
return QStringLiteral("mass");
case Partition::External:
return QStringLiteral("card");
default:
return QString();
}
}();
Oct 2, 2015
Oct 2, 2015
358
Aug 22, 2019
Aug 22, 2019
359
if (partition.storageType() != Partition::External) {
Oct 14, 2019
Oct 14, 2019
360
d->internalStorage << QVariant(row);
Oct 2, 2015
Oct 2, 2015
361
362
363
364
365
}
}
emit storageChanged();
}