From ea49e0a48c2aba1c45920a06d8ef90ae9ce23891 Mon Sep 17 00:00:00 2001 From: Dmitry Gerasimov Date: Tue, 10 Sep 2019 17:41:08 +0300 Subject: [PATCH] [aboutsettings] Add property with localized OS name. Contributes to JB#47268 Localization is loaded from /etc/os-release-l10n, which has ini syntax: [en-US] NAME = Value If there is no localization, value from /etc/os-release used as fallback. --- src/aboutsettings.cpp | 40 +++++++++++++++++++++++++++++++++++++ src/aboutsettings.h | 3 +++ src/plugin/plugins.qmltypes | 1 + 3 files changed, 44 insertions(+) diff --git a/src/aboutsettings.cpp b/src/aboutsettings.cpp index 8f07cfc..4caeb7b 100644 --- a/src/aboutsettings.cpp +++ b/src/aboutsettings.cpp @@ -116,6 +116,39 @@ void parseReleaseFile(const QString &filename, QMap *result) } } +void parseLocalizationFile(const QString &filename, QMap *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(); + } +} + } AboutSettings::AboutSettings(QObject *parent) @@ -189,6 +222,13 @@ QString AboutSettings::serial() const return QString(); } +QString AboutSettings::localizedOperatingSystemName() const +{ + parseLocalizationFile(QStringLiteral("/etc/os-release-l10n"), &m_osReleaseLocalization); + + return m_osReleaseLocalization.value("NAME", operatingSystemName()); +} + QString AboutSettings::baseOperatingSystemName() const { QString osName = operatingSystemName(); diff --git a/src/aboutsettings.h b/src/aboutsettings.h index 384f8d0..6752135 100644 --- a/src/aboutsettings.h +++ b/src/aboutsettings.h @@ -47,6 +47,7 @@ class SYSTEMSETTINGS_EXPORT AboutSettings: public QObject Q_PROPERTY(QString wlanMacAddress READ wlanMacAddress CONSTANT) Q_PROPERTY(QString imei READ imei CONSTANT) Q_PROPERTY(QString serial READ serial CONSTANT) + Q_PROPERTY(QString localizedOperatingSystemName READ localizedOperatingSystemName CONSTANT) Q_PROPERTY(QString baseOperatingSystemName READ baseOperatingSystemName CONSTANT) Q_PROPERTY(QString operatingSystemName READ operatingSystemName CONSTANT) Q_PROPERTY(QString softwareVersion READ softwareVersion CONSTANT) @@ -77,6 +78,7 @@ class SYSTEMSETTINGS_EXPORT AboutSettings: public QObject QString wlanMacAddress() const; QString imei() const; QString serial() const; + QString localizedOperatingSystemName() const; QString baseOperatingSystemName() const; QString operatingSystemName() const; QString softwareVersion() const; @@ -100,6 +102,7 @@ class SYSTEMSETTINGS_EXPORT AboutSettings: public QObject PartitionManager m_partitionManager; mutable QMap m_osRelease; + mutable QMap m_osReleaseLocalization; mutable QMap m_hardwareRelease; QString m_vendorName; diff --git a/src/plugin/plugins.qmltypes b/src/plugin/plugins.qmltypes index 09e8acc..5e95ba1 100644 --- a/src/plugin/plugins.qmltypes +++ b/src/plugin/plugins.qmltypes @@ -16,6 +16,7 @@ Module { Property { name: "wlanMacAddress"; type: "string"; isReadonly: true } Property { name: "imei"; type: "string"; isReadonly: true } Property { name: "serial"; type: "string"; isReadonly: true } + Property { name: "localizedOperatingSystemName"; type: "string"; isReadonly: true } Property { name: "baseOperatingSystemName"; type: "string"; isReadonly: true } Property { name: "operatingSystemName"; type: "string"; isReadonly: true } Property { name: "softwareVersion"; type: "string"; isReadonly: true }