aboutsettings.cpp 11 KB
Newer Older
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."
 */

32
#include "aboutsettings.h"
33
#include "aboutsettings_p.h"
34

35 36
#include <QDebug>
#include <QStringList>
37

38
#include <QNetworkInfo>
39

40
#include <QDeviceInfo>
41 42
#include <QFile>
#include <QByteArray>
43
#include <QRegularExpression>
44 45
#include <QMap>
#include <QTextStream>
46
#include <QVariant>
47
#include <QSettings>
48
#include <QTimer>
49

50 51 52
namespace
{

53
void parseReleaseFile(const QString &filename, QMap<QString, QString> *result)
54
{
55 56 57
    if (!result->isEmpty()) {
        return;
    }
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");

115
            (*result)[key] = value;
116 117 118 119 120 121
        }

        release.close();
    }
}

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();
    }
}

155
}
156

157 158 159 160 161 162 163 164 165 166 167 168 169

AboutSettingsPrivate::AboutSettingsPrivate(QObject *parent)
    : QObject(parent)
{

}

AboutSettingsPrivate::~AboutSettingsPrivate()
{

}


170
AboutSettings::AboutSettings(QObject *parent)
171 172
    : QObject(parent)
    , d_ptr(new AboutSettingsPrivate(this))
173
{
174
    Q_D(AboutSettings);
175
    QSettings settings(QStringLiteral("/mnt/vendor_data/vendor-data.ini"), QSettings::IniFormat);
176 177
    d->vendorName = settings.value(QStringLiteral("Name")).toString();
    d->vendorVersion = settings.value(QStringLiteral("Version")).toString();
178

179
    refreshStorageModels();
180

181
    connect(&d->partitionManager, &PartitionManager::partitionAdded,
182
            this, &AboutSettings::partitionCountChanged);
183
    connect(&d->partitionManager, &PartitionManager::partitionRemoved,
184
            this, &AboutSettings::partitionCountChanged);
185 186 187 188 189 190 191 192
}

AboutSettings::~AboutSettings()
{
}

qlonglong AboutSettings::totalDiskSpace() const
{
193 194
    Q_D(const AboutSettings);
    return d->partitionManager.root().bytesTotal();
195 196 197 198
}

qlonglong AboutSettings::availableDiskSpace() const
{
199 200
    Q_D(const AboutSettings);
    return d->partitionManager.root().bytesAvailable();
201 202
}

203 204
QVariant AboutSettings::diskUsageModel() const
{
205 206
    Q_D(const AboutSettings);
    return d->internalStorage;
207
}
208

209
QString AboutSettings::wlanMacAddress() const
210
{
211 212
    Q_D(const AboutSettings);
    return d->networkInfo.macAddress(QNetworkInfo::WlanMode, 0);
213 214
}

215
QString AboutSettings::imei() const
216
{
217 218
    Q_D(const AboutSettings);
    return d->deviceInfo.imei(0);
219 220
}

221 222
QString AboutSettings::serial() const
{
223 224 225 226 227
    // TODO: eventually we should use QDeviceInfo's uniqueDeviceID()

    QStringList serialFiles;

    serialFiles
228 229
        // Old location for serial number that was used by e.g. 
        // Jolla Tablet, that should not be used anymore.
230
        << "/config/serial/serial.txt"
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"
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();
242
    }
243 244

    return QString();
245 246
}

247 248
QString AboutSettings::localizedOperatingSystemName() const
{
249 250
    Q_D(const AboutSettings);
    parseLocalizationFile(QStringLiteral("/etc/os-release-l10n"), &d->osReleaseLocalization);
251

252
    return d->osReleaseLocalization.value("NAME", operatingSystemName());
253 254
}

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;
}

264 265
QString AboutSettings::operatingSystemName() const
{
266 267
    Q_D(const AboutSettings);
    parseReleaseFile(QStringLiteral("/etc/os-release"), &d->osRelease);
268

269
    return d->osRelease["NAME"];
270 271
}

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());
}

280 281
QString AboutSettings::softwareVersion() const
{
282 283
    Q_D(const AboutSettings);
    parseReleaseFile(QStringLiteral("/etc/os-release"), &d->osRelease);
284

285
    return d->osRelease["VERSION"];
286 287 288 289
}

QString AboutSettings::softwareVersionId() const
{
290 291
    Q_D(const AboutSettings);
    parseReleaseFile(QStringLiteral("/etc/os-release"), &d->osRelease);
292

293
    return d->osRelease["VERSION_ID"];
294
}
295

296 297
QString AboutSettings::adaptationVersion() const
{
298 299
    Q_D(const AboutSettings);
    parseReleaseFile(QStringLiteral("/etc/hw-release"), &d->hardwareRelease);
300

301
    return d->hardwareRelease["VERSION_ID"];
302
}
303

304 305
QString AboutSettings::vendorName() const
{
306 307
    Q_D(const AboutSettings);
    return d->vendorName;
308 309 310 311
}

QString AboutSettings::vendorVersion() const
{
312 313
    Q_D(const AboutSettings);
    return d->vendorVersion;
314 315
}

316
void AboutSettings::refreshStorageModels()
317
{
318 319
    Q_D(AboutSettings);
    d->partitionManager.refresh();
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()
332
{
333 334
    Q_D(AboutSettings);
    d->internalStorage.clear();
335

336
    for (auto partition : d->partitionManager.partitions()) {
337
        QVariantMap row;
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();
            }
        }();
358

359
        if (partition.storageType() != Partition::External) {
360
            d->internalStorage << QVariant(row);
361 362 363 364 365
        }
    }

    emit storageChanged();
}