Skip to content

Commit

Permalink
[aboutsettings] Expose adaptationVersion (Contributes to JB#13494)
Browse files Browse the repository at this point in the history
  • Loading branch information
thp committed Mar 20, 2015
1 parent d38eab0 commit 887b027
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 22 deletions.
100 changes: 78 additions & 22 deletions src/aboutsettings.cpp
Expand Up @@ -39,6 +39,79 @@
#include <QFile>
#include <QByteArray>
#include <QRegularExpression>
#include <QMap>
#include <QTextStream>


static QMap<QString, QString> parseReleaseFile(const QString &filename)
{
QMap<QString, QString> result;

// 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");

result[key] = value;
}

release.close();
}

return result;
}


AboutSettings::AboutSettings(QObject *parent)
: QObject(parent),
Expand Down Expand Up @@ -80,27 +153,10 @@ QString AboutSettings::imei() const

QString AboutSettings::softwareVersion() const
{
QFile releaseFile("/etc/os-release");
if (!releaseFile.open(QIODevice::ReadOnly | QIODevice::Text))
return QString();

QString version;
QByteArray versionTag("VERSION=");

while (!releaseFile.atEnd()) {
QByteArray line = releaseFile.readLine();

if (line.startsWith(versionTag)) {
version = line.mid(versionTag.length()).simplified();
// remove start and end quotes if exist
if (version.length() > 0 && (version.at(0) == '"' || version.at(0) == '\'')) {
version = version.mid(1, version.length() - 2);
}
// unescape rest
version.replace(QRegularExpression("\\\\(.)"), "\\1");
break;
}
}
return parseReleaseFile("/etc/os-release")["VERSION"];
}

return version;
QString AboutSettings::adaptationVersion() const
{
return parseReleaseFile("/etc/hw-release")["VERSION_ID"];
}
2 changes: 2 additions & 0 deletions src/aboutsettings.h
Expand Up @@ -45,6 +45,7 @@ class AboutSettings: public QObject
Q_PROPERTY(QString wlanMacAddress READ wlanMacAddress CONSTANT)
Q_PROPERTY(QString imei READ imei CONSTANT)
Q_PROPERTY(QString softwareVersion READ softwareVersion CONSTANT)
Q_PROPERTY(QString adaptationVersion READ adaptationVersion CONSTANT)

public:
explicit AboutSettings(QObject *parent = 0);
Expand All @@ -57,6 +58,7 @@ class AboutSettings: public QObject
QString wlanMacAddress() const;
QString imei() const;
QString softwareVersion() const;
QString adaptationVersion() const;

private:
QStorageInfo *m_sysinfo;
Expand Down

0 comments on commit 887b027

Please sign in to comment.