Better device name in Sailfish OS

This commit is contained in:
Richard Liebscher 2020-08-02 20:57:41 +02:00 committed by Nicolas Fella
parent f3656d962c
commit f1b3c0e0ca

View file

@ -58,6 +58,29 @@ struct KdeConnectConfigPrivate {
#endif #endif
}; };
static QString getDefaultDeviceName() {
#ifdef SAILFISHOS
const QString hwReleaseFile = QStringLiteral("/etc/hw-release");
// QSettings will crash if the file does not exist or can be created, like in this case by us in /etc.
// E.g. in the SFOS SDK Emulator there is no such file, so check before to protect against the crash.
if (QFile::exists(hwReleaseFile)) {
QSettings hwRelease(hwReleaseFile, QSettings::IniFormat);
auto hwName = hwRelease.value(QStringLiteral("NAME")).toString();
if (!hwName.isEmpty()) {
return hwName;
}
}
#endif
#ifdef Q_OS_WIN
QString username = QString::fromLatin1(qgetenv("USERNAME"));
#else
QString username = QString::fromLatin1(qgetenv("USER"));
#endif
return username + QStringLiteral("@") + QHostInfo::localHostName();
}
KdeConnectConfig& KdeConnectConfig::instance() KdeConnectConfig& KdeConnectConfig::instance()
{ {
static KdeConnectConfig kcc; static KdeConnectConfig kcc;
@ -86,19 +109,15 @@ KdeConnectConfig::KdeConnectConfig()
loadPrivateKey(); loadPrivateKey();
loadCertificate(); loadCertificate();
if (name().isEmpty()) {
setName(getDefaultDeviceName());
}
} }
QString KdeConnectConfig::name() QString KdeConnectConfig::name()
{ {
QString username; return d->m_config->value(QStringLiteral("name")).toString();
#ifdef Q_OS_WIN
username = QString::fromLatin1(qgetenv("USERNAME"));
#else
username = QString::fromLatin1(qgetenv("USER"));
#endif
QString defaultName = username + QStringLiteral("@") + QHostInfo::localHostName();
QString name = d->m_config->value(QStringLiteral("name"), defaultName).toString();
return name;
} }
void KdeConnectConfig::setName(const QString& name) void KdeConnectConfig::setName(const QString& name)