Skip to content

Commit ed21c75

Browse files
committed
Refactor: Centralize session type detection into a new SessionUtil module and introduce internationalization for updater messages.
1 parent ca069c1 commit ed21c75

10 files changed

Lines changed: 87 additions & 103 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Build directories
22
build/
33
build-*/
4+
src/build-*/
45
cmake-build-*/
56
out/
67

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ set(BACKEND_SOURCES
7070
src/backend/system/polkit.cpp
7171
src/backend/system/dnfmanager.cpp
7272
src/backend/system/commandrunner.cpp
73+
src/backend/system/sessionutil.cpp
7374
)
7475

7576
set(APP_SOURCES

src/backend/nvidia/detector.cpp

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "detector.h"
22

33
#include "system/commandrunner.h"
4+
#include "system/sessionutil.h"
45

56
#include <QFile>
67
#include <QRegularExpression>
@@ -18,7 +19,7 @@ NvidiaDetector::GpuInfo NvidiaDetector::detect() const {
1819
info.driverLoaded = isModuleLoaded(QStringLiteral("nvidia"));
1920
info.nouveauActive = isModuleLoaded(QStringLiteral("nouveau"));
2021
info.secureBootEnabled = detectSecureBoot(&info.secureBootKnown);
21-
info.sessionType = detectSessionType();
22+
info.sessionType = SessionUtil::detectSessionType();
2223

2324
return info;
2425
}
@@ -151,24 +152,4 @@ bool NvidiaDetector::detectSecureBoot(bool *known) const {
151152
return false;
152153
}
153154

154-
QString NvidiaDetector::detectSessionType() const {
155-
const QString envType =
156-
qEnvironmentVariable("XDG_SESSION_TYPE").trimmed().toLower();
157-
if (!envType.isEmpty())
158-
return envType;
159155

160-
CommandRunner runner;
161-
const auto loginctl =
162-
runner.run(QStringLiteral("loginctl"),
163-
{QStringLiteral("show-session"),
164-
qEnvironmentVariable("XDG_SESSION_ID"), QStringLiteral("-p"),
165-
QStringLiteral("Type"), QStringLiteral("--value")});
166-
167-
if (loginctl.success()) {
168-
const QString type = loginctl.stdout.trimmed().toLower();
169-
if (!type.isEmpty())
170-
return type;
171-
}
172-
173-
return QStringLiteral("unknown");
174-
}

src/backend/nvidia/detector.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ class NvidiaDetector : public QObject {
6565
QString detectDriverVersion() const;
6666
bool isModuleLoaded(const QString &moduleName) const;
6767
bool detectSecureBoot(bool *known = nullptr) const;
68-
QString detectSessionType() const;
6968

7069
GpuInfo m_info;
7170
};

src/backend/nvidia/installer.cpp

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "installer.h"
22

33
#include "system/commandrunner.h"
4+
#include "system/sessionutil.h"
45

56
#include <QMetaObject>
67
#include <QPointer>
@@ -212,7 +213,7 @@ void NvidiaInstaller::installProprietary(bool agreementAccepted) {
212213
Qt::QueuedConnection);
213214
runner.runAsRoot(QStringLiteral("akmods"), {QStringLiteral("--force")});
214215

215-
const QString sessionType = guard->detectSessionType();
216+
const QString sessionType = SessionUtil::detectSessionType();
216217
QString sessionError;
217218
if (!guard->applySessionSpecificSetup(runner, sessionType, &sessionError)) {
218219
QMetaObject::invokeMethod(
@@ -466,27 +467,7 @@ void NvidiaInstaller::deepClean() {
466467
});
467468
}
468469

469-
QString NvidiaInstaller::detectSessionType() const {
470-
const QString envType =
471-
qEnvironmentVariable("XDG_SESSION_TYPE").trimmed().toLower();
472-
if (!envType.isEmpty())
473-
return envType;
474470

475-
CommandRunner runner;
476-
const auto loginctl =
477-
runner.run(QStringLiteral("loginctl"),
478-
{QStringLiteral("show-session"),
479-
qEnvironmentVariable("XDG_SESSION_ID"), QStringLiteral("-p"),
480-
QStringLiteral("Type"), QStringLiteral("--value")});
481-
482-
if (loginctl.success()) {
483-
const QString type = loginctl.stdout.trimmed().toLower();
484-
if (!type.isEmpty())
485-
return type;
486-
}
487-
488-
return QStringLiteral("unknown");
489-
}
490471

491472
bool NvidiaInstaller::applySessionSpecificSetup(CommandRunner &runner,
492473
const QString &sessionType,

src/backend/nvidia/installer.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ class NvidiaInstaller : public QObject {
6161
void setBusy(bool busy);
6262
void runAsyncTask(const std::function<void()> &task);
6363
void setProprietaryAgreement(bool required, const QString &text);
64-
QString detectSessionType() const;
6564
bool applySessionSpecificSetup(CommandRunner &runner,
6665
const QString &sessionType,
6766
QString *errorMessage);

src/backend/nvidia/updater.cpp

Lines changed: 35 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "updater.h"
22
#include "detector.h"
33
#include "system/commandrunner.h"
4+
#include "system/sessionutil.h"
45
#include "versionparser.h"
56

67
#include <QMetaObject>
@@ -49,37 +50,15 @@ struct UpdateStatusSnapshot {
4950
QString message;
5051
};
5152

52-
QString detectSessionTypeImpl() {
53-
const QString envType =
54-
qEnvironmentVariable("XDG_SESSION_TYPE").trimmed().toLower();
55-
if (!envType.isEmpty()) {
56-
return envType;
57-
}
5853

59-
CommandRunner runner;
60-
const auto loginctl =
61-
runner.run(QStringLiteral("loginctl"),
62-
{QStringLiteral("show-session"),
63-
qEnvironmentVariable("XDG_SESSION_ID"), QStringLiteral("-p"),
64-
QStringLiteral("Type"), QStringLiteral("--value")});
65-
66-
if (loginctl.success()) {
67-
const QString type = loginctl.stdout.trimmed().toLower();
68-
if (!type.isEmpty()) {
69-
return type;
70-
}
71-
}
72-
73-
return QStringLiteral("unknown");
74-
}
7554

7655
UpdateStatusSnapshot collectUpdateStatus() {
7756
UpdateStatusSnapshot snapshot;
7857
NvidiaDetector detector;
7958
snapshot.currentVersion = detector.installedDriverVersion();
8059

8160
if (QStandardPaths::findExecutable(QStringLiteral("dnf")).isEmpty()) {
82-
snapshot.message = QStringLiteral("dnf bulunamadi.");
61+
snapshot.message = NvidiaUpdater::tr("dnf not found.");
8362
return snapshot;
8463
}
8564

@@ -96,7 +75,7 @@ UpdateStatusSnapshot collectUpdateStatus() {
9675
}
9776

9877
if (snapshot.currentVersion.isEmpty()) {
99-
snapshot.message = QStringLiteral("Kurulu NVIDIA surucusu bulunamadi.");
78+
snapshot.message = NvidiaUpdater::tr("No installed NVIDIA driver found.");
10079
return snapshot;
10180
}
10281

@@ -110,13 +89,13 @@ UpdateStatusSnapshot collectUpdateStatus() {
11089
snapshot.updateAvailable = true;
11190
snapshot.message =
11291
snapshot.latestVersion.isEmpty()
113-
? QStringLiteral("Guncelleme bulundu (surum ayrintisi alinamadi).")
114-
: QStringLiteral("Guncelleme bulundu: %1")
92+
? NvidiaUpdater::tr("Update found (version details unavailable).")
93+
: NvidiaUpdater::tr("Update found: %1")
11594
.arg(snapshot.latestVersion);
11695
} else if (checkResult.exitCode == 0) {
117-
snapshot.message = QStringLiteral("Surucu guncel. Yeni surum bulunamadi.");
96+
snapshot.message = NvidiaUpdater::tr("Driver is up to date. No new version found.");
11897
} else {
119-
snapshot.message = QStringLiteral("Guncelleme kontrolu basarisiz: %1")
98+
snapshot.message = NvidiaUpdater::tr("Update check failed: %1")
12099
.arg(checkResult.stderr.trimmed().isEmpty()
121100
? checkResult.stdout.trimmed()
122101
: checkResult.stderr.trimmed());
@@ -141,7 +120,7 @@ void NvidiaUpdater::setBusy(bool busy) {
141120
void NvidiaUpdater::runAsyncTask(const std::function<void()> &task) {
142121
if (m_busy) {
143122
emit progressMessage(
144-
QStringLiteral("Baska bir surucu islemi zaten calisiyor."));
123+
tr("Another driver operation is already running."));
145124
return;
146125
}
147126

@@ -174,7 +153,7 @@ void NvidiaUpdater::setAvailableVersions(const QStringList &versions) {
174153
}
175154

176155
QString NvidiaUpdater::detectSessionType() const {
177-
return detectSessionTypeImpl();
156+
return SessionUtil::detectSessionType();
178157
}
179158

180159
QStringList
@@ -196,23 +175,23 @@ bool NvidiaUpdater::finalizeDriverChange(CommandRunner &runner,
196175
runner.runAsRoot(QStringLiteral("akmods"), {QStringLiteral("--force")});
197176
if (!result.success()) {
198177
if (errorMessage != nullptr) {
199-
*errorMessage = QStringLiteral("Kernel modulu derlenemedi: ") +
200-
commandError(result, QStringLiteral("bilinmeyen hata"));
178+
*errorMessage = tr("Kernel module build failed: ") +
179+
commandError(result, tr("unknown error"));
201180
}
202181
return false;
203182
}
204183

205184
if (sessionType == QStringLiteral("wayland")) {
206-
emit progressMessage(QStringLiteral(
207-
"Wayland tespit edildi: nvidia-drm.modeset=1 ayari guncelleniyor..."));
185+
emit progressMessage(
186+
tr("Wayland detected: applying nvidia-drm.modeset=1..."));
208187
result = runner.runAsRoot(QStringLiteral("grubby"),
209188
{QStringLiteral("--update-kernel=ALL"),
210189
QStringLiteral("--args=nvidia-drm.modeset=1")});
211190
if (!result.success()) {
212191
if (errorMessage != nullptr) {
213192
*errorMessage =
214-
QStringLiteral("Wayland kernel parametresi guncellenemedi: ") +
215-
commandError(result, QStringLiteral("bilinmeyen hata"));
193+
tr("Failed to update the Wayland kernel parameter: ") +
194+
commandError(result, tr("unknown error"));
216195
}
217196
return false;
218197
}
@@ -239,8 +218,8 @@ void NvidiaUpdater::refreshAvailableVersions() {
239218
guard->setAvailableVersions(snapshot.availableVersions);
240219
emit guard->progressMessage(
241220
snapshot.availableVersions.isEmpty()
242-
? QStringLiteral("Kullanilabilir surum bulunamadi.")
243-
: QStringLiteral("Kullanilabilir surum sayisi: %1")
221+
? guard->tr("No available versions found.")
222+
: guard->tr("Available versions: %1")
244223
.arg(snapshot.availableVersions.size()));
245224
},
246225
Qt::QueuedConnection);
@@ -250,7 +229,7 @@ void NvidiaUpdater::refreshAvailableVersions() {
250229
void NvidiaUpdater::checkForUpdate() {
251230
// TR: Her kontrol denemesinde UI'ye gorunur bir baslangic mesaji gonder.
252231
// EN: Always emit a visible start message for each check request.
253-
emit progressMessage(QStringLiteral("Guncelleme kontrolu baslatildi..."));
232+
emit progressMessage(tr("Starting update check..."));
254233

255234
QPointer<NvidiaUpdater> guard(this);
256235
runAsyncTask([guard]() {
@@ -318,7 +297,7 @@ void NvidiaUpdater::applyVersion(const QString &version) {
318297
[guard]() {
319298
if (guard) {
320299
emit guard->updateFinished(false,
321-
QStringLiteral("dnf bulunamadi."));
300+
guard->tr("dnf not found."));
322301
}
323302
},
324303
Qt::QueuedConnection);
@@ -327,7 +306,7 @@ void NvidiaUpdater::applyVersion(const QString &version) {
327306

328307
NvidiaDetector detector;
329308
const QString installedVersion = detector.installedDriverVersion();
330-
const QString sessionType = detectSessionTypeImpl();
309+
const QString sessionType = SessionUtil::detectSessionType();
331310

332311
if (!trimmedVersion.isEmpty() && !knownVersions.contains(trimmedVersion)) {
333312
QMetaObject::invokeMethod(
@@ -336,7 +315,7 @@ void NvidiaUpdater::applyVersion(const QString &version) {
336315
if (guard) {
337316
emit guard->updateFinished(
338317
false,
339-
QStringLiteral("Secilen surum repo listesinde bulunamadi."));
318+
guard->tr("Selected version not found in the repository."));
340319
}
341320
},
342321
Qt::QueuedConnection);
@@ -352,10 +331,10 @@ void NvidiaUpdater::applyVersion(const QString &version) {
352331

353332
emit guard->progressMessage(
354333
trimmedVersion.isEmpty()
355-
? QStringLiteral(
356-
"NVIDIA surucusu en son surume guncelleniyor...")
357-
: QStringLiteral(
358-
"NVIDIA surucusu secilen surume geciriliyor: %1")
334+
? guard->tr(
335+
"Updating NVIDIA driver to the latest version...")
336+
: guard->tr(
337+
"Switching NVIDIA driver to selected version: %1")
359338
.arg(trimmedVersion));
360339
},
361340
Qt::QueuedConnection);
@@ -374,8 +353,8 @@ void NvidiaUpdater::applyVersion(const QString &version) {
374353
auto result = runner.runAsRoot(QStringLiteral("dnf"), args);
375354
if (!result.success()) {
376355
const QString error =
377-
QStringLiteral("Guncelleme basarisiz: ") +
378-
commandError(result, QStringLiteral("bilinmeyen hata"));
356+
guard->tr("Update failed: ") +
357+
commandError(result, guard->tr("unknown error"));
379358
QMetaObject::invokeMethod(
380359
guard,
381360
[guard, error]() {
@@ -392,7 +371,7 @@ void NvidiaUpdater::applyVersion(const QString &version) {
392371
[guard]() {
393372
if (guard) {
394373
emit guard->progressMessage(
395-
QStringLiteral("Kernel modulu yeniden derleniyor..."));
374+
guard->tr("Rebuilding kernel module..."));
396375
}
397376
},
398377
Qt::QueuedConnection);
@@ -414,13 +393,13 @@ void NvidiaUpdater::applyVersion(const QString &version) {
414393
const QString successMessage =
415394
trimmedVersion.isEmpty()
416395
? (installedVersion.isEmpty()
417-
? QStringLiteral("En son surum basariyla kuruldu. Lutfen "
418-
"sistemi yeniden baslatin.")
419-
: QStringLiteral("Surucu basariyla guncellendi. Lutfen "
420-
"sistemi yeniden baslatin."))
421-
: QStringLiteral(
422-
"Secilen surum basariyla uygulandi. Lutfen sistemi "
423-
"yeniden baslatin.");
396+
? guard->tr("Latest version installed successfully. "
397+
"Please restart the system.")
398+
: guard->tr("Driver updated successfully. "
399+
"Please restart the system."))
400+
: guard->tr(
401+
"Selected version applied successfully. "
402+
"Please restart the system.");
424403

425404
QMetaObject::invokeMethod(
426405
guard,

src/backend/system/sessionutil.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "sessionutil.h"
2+
3+
#include "commandrunner.h"
4+
5+
#include <QtGlobal>
6+
7+
namespace SessionUtil {
8+
9+
QString detectSessionType() {
10+
const QString envType =
11+
qEnvironmentVariable("XDG_SESSION_TYPE").trimmed().toLower();
12+
if (!envType.isEmpty())
13+
return envType;
14+
15+
CommandRunner runner;
16+
const auto loginctl =
17+
runner.run(QStringLiteral("loginctl"),
18+
{QStringLiteral("show-session"),
19+
qEnvironmentVariable("XDG_SESSION_ID"), QStringLiteral("-p"),
20+
QStringLiteral("Type"), QStringLiteral("--value")});
21+
22+
if (loginctl.success()) {
23+
const QString type = loginctl.stdout.trimmed().toLower();
24+
if (!type.isEmpty())
25+
return type;
26+
}
27+
28+
return QStringLiteral("unknown");
29+
}
30+
31+
} // namespace SessionUtil

src/backend/system/sessionutil.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
3+
#include <QString>
4+
5+
namespace SessionUtil {
6+
7+
// XDG_SESSION_TYPE ortam degiskeni veya loginctl
8+
// uzerinden geçerli oturum turunu tespit eder.
9+
// "wayland", "x11" veya "unknown" doner.
10+
QString detectSessionType();
11+
12+
} // namespace SessionUtil

src/qml/components/StatCard.qml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import QtQuick 2.15
2-
import QtQuick.Controls 2.15
3-
import QtQuick.Layouts 1.15
1+
import QtQuick
2+
import QtQuick.Controls
3+
import QtQuick.Layouts
44

55
Rectangle {
66
id: card

0 commit comments

Comments
 (0)