|
| 1 | +#include "systeminfoprovider.h" |
| 2 | + |
| 3 | +#include "commandrunner.h" |
| 4 | + |
| 5 | +#include <QFile> |
| 6 | +#include <QRegularExpression> |
| 7 | +#include <QSysInfo> |
| 8 | +#include <QTextStream> |
| 9 | + |
| 10 | +#if defined(Q_OS_UNIX) |
| 11 | +#include <sys/utsname.h> |
| 12 | +#endif |
| 13 | + |
| 14 | +namespace { |
| 15 | + |
| 16 | +QString valueFromOsRelease(const QString &key) { |
| 17 | + QFile file(QStringLiteral("/etc/os-release")); |
| 18 | + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { |
| 19 | + return {}; |
| 20 | + } |
| 21 | + |
| 22 | + QTextStream stream(&file); |
| 23 | + while (!stream.atEnd()) { |
| 24 | + const QString line = stream.readLine().trimmed(); |
| 25 | + if (!line.startsWith(key + QLatin1Char('='))) { |
| 26 | + continue; |
| 27 | + } |
| 28 | + |
| 29 | + QString value = line.mid(key.size() + 1).trimmed(); |
| 30 | + if (value.startsWith(QLatin1Char('"')) && value.endsWith(QLatin1Char('"')) && |
| 31 | + value.size() >= 2) { |
| 32 | + value = value.mid(1, value.size() - 2); |
| 33 | + } |
| 34 | + return value; |
| 35 | + } |
| 36 | + |
| 37 | + return {}; |
| 38 | +} |
| 39 | + |
| 40 | +} // namespace |
| 41 | + |
| 42 | +SystemInfoProvider::SystemInfoProvider(QObject *parent) : QObject(parent) { |
| 43 | + refresh(); |
| 44 | +} |
| 45 | + |
| 46 | +void SystemInfoProvider::refresh() { |
| 47 | + const QString nextOsName = detectOsName(); |
| 48 | + const QString nextDesktopEnvironment = detectDesktopEnvironment(); |
| 49 | + const QString nextKernelVersion = detectKernelVersion(); |
| 50 | + const QString nextCpuModel = detectCpuModel(); |
| 51 | + |
| 52 | + if (m_osName == nextOsName && |
| 53 | + m_desktopEnvironment == nextDesktopEnvironment && |
| 54 | + m_kernelVersion == nextKernelVersion && m_cpuModel == nextCpuModel) { |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + m_osName = nextOsName; |
| 59 | + m_desktopEnvironment = nextDesktopEnvironment; |
| 60 | + m_kernelVersion = nextKernelVersion; |
| 61 | + m_cpuModel = nextCpuModel; |
| 62 | + emit infoChanged(); |
| 63 | +} |
| 64 | + |
| 65 | +QString SystemInfoProvider::detectOsName() const { |
| 66 | + const QString prettyName = valueFromOsRelease(QStringLiteral("PRETTY_NAME")); |
| 67 | + if (!prettyName.isEmpty()) { |
| 68 | + return prettyName; |
| 69 | + } |
| 70 | + |
| 71 | + const QString productName = QSysInfo::prettyProductName(); |
| 72 | + if (!productName.isEmpty()) { |
| 73 | + return productName; |
| 74 | + } |
| 75 | + |
| 76 | + return QSysInfo::productType(); |
| 77 | +} |
| 78 | + |
| 79 | +QString SystemInfoProvider::detectKernelVersion() const { |
| 80 | +#if defined(Q_OS_UNIX) |
| 81 | + utsname name {}; |
| 82 | + if (uname(&name) == 0) { |
| 83 | + return QString::fromLocal8Bit(name.release); |
| 84 | + } |
| 85 | +#endif |
| 86 | + return QSysInfo::kernelVersion(); |
| 87 | +} |
| 88 | + |
| 89 | +QString SystemInfoProvider::detectCpuModel() const { |
| 90 | +#if defined(Q_OS_LINUX) |
| 91 | + QFile cpuInfo(QStringLiteral("/proc/cpuinfo")); |
| 92 | + if (cpuInfo.open(QIODevice::ReadOnly | QIODevice::Text)) { |
| 93 | + QTextStream stream(&cpuInfo); |
| 94 | + while (!stream.atEnd()) { |
| 95 | + const QString line = stream.readLine(); |
| 96 | + if (line.startsWith(QStringLiteral("model name"))) { |
| 97 | + const int separatorIndex = line.indexOf(QLatin1Char(':')); |
| 98 | + if (separatorIndex >= 0) { |
| 99 | + return line.mid(separatorIndex + 1).trimmed(); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | +#elif defined(Q_OS_MACOS) |
| 105 | + CommandRunner runner; |
| 106 | + const auto result = |
| 107 | + runner.run(QStringLiteral("sysctl"), |
| 108 | + {QStringLiteral("-n"), QStringLiteral("machdep.cpu.brand_string")}); |
| 109 | + if (result.success()) { |
| 110 | + const QString value = result.stdout.trimmed(); |
| 111 | + if (!value.isEmpty()) { |
| 112 | + return value; |
| 113 | + } |
| 114 | + } |
| 115 | +#endif |
| 116 | + |
| 117 | + const QString architecture = QSysInfo::currentCpuArchitecture(); |
| 118 | + return architecture.isEmpty() ? QStringLiteral("Unknown CPU") : architecture; |
| 119 | +} |
| 120 | + |
| 121 | +QString SystemInfoProvider::detectDesktopEnvironment() const { |
| 122 | + QString desktop = qEnvironmentVariable("XDG_CURRENT_DESKTOP").trimmed(); |
| 123 | + if (desktop.isEmpty()) { |
| 124 | + desktop = qEnvironmentVariable("DESKTOP_SESSION").trimmed(); |
| 125 | + } |
| 126 | + |
| 127 | + if (desktop.isEmpty()) { |
| 128 | + return {}; |
| 129 | + } |
| 130 | + |
| 131 | + desktop.replace(QLatin1Char(':'), QLatin1String(" / ")); |
| 132 | + const QStringList parts = desktop.split(QLatin1Char('/'), Qt::SkipEmptyParts); |
| 133 | + QStringList normalizedParts; |
| 134 | + for (const QString &part : parts) { |
| 135 | + const QString trimmed = part.trimmed(); |
| 136 | + if (trimmed.isEmpty()) { |
| 137 | + continue; |
| 138 | + } |
| 139 | + |
| 140 | + if (trimmed.compare(QStringLiteral("KDE"), Qt::CaseInsensitive) == 0) { |
| 141 | + normalizedParts << QStringLiteral("KDE Plasma"); |
| 142 | + } else if (trimmed.compare(QStringLiteral("GNOME"), Qt::CaseInsensitive) == 0) { |
| 143 | + normalizedParts << QStringLiteral("GNOME"); |
| 144 | + } else { |
| 145 | + normalizedParts << trimmed; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + return normalizedParts.join(QStringLiteral(" / ")); |
| 150 | +} |
0 commit comments