game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
lutrisinstallprogressdialog.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// qt/lutrisinstallprogressdialog.cpp
4// ==============================================================================
5// Lutris install progress dialog implementation
6// ==============================================================================
7
9#include "models/game.h"
10
11#include <QApplication>
12#include <QCloseEvent>
13#include <QDir>
14#include <QGroupBox>
15#include <QHBoxLayout>
16#include <QMessageBox>
17#include <QVBoxLayout>
18
19// ==============================================================================
20// LutrisInstallWorker Implementation
21// ==============================================================================
22
23LutrisInstallWorker::LutrisInstallWorker(QObject* parent) : QObject(parent) {}
24
26{
27 script_ = script;
28}
29
31{
32 config_ = config;
33}
34
36{
37 cancelled_ = false;
38
39 // Progress callback
40 auto progressCallback = [this](const InstallProgress& progress)
41 {
42 if (!cancelled_)
43 {
44 emit progressUpdate(progress);
45 }
46 };
47
48 // Execute the installation
49 InstallResult result = InstallationExecutor::install(script_, config_, progressCallback);
50
51 emit installComplete(result);
52}
53
55{
56 cancelled_ = true;
58}
59
60// ==============================================================================
61// LutrisInstallProgressDialog Implementation
62// ==============================================================================
63
65 const LutrisInstallerScript& script, const InstallConfig& config,
66 core::GameLibraryController* libraryController, QWidget* parent)
67 : QDialog(parent), script_(script), config_(config), libraryController_(libraryController)
68{
69 setupUI();
70 setWindowTitle(QString("Installing %1").arg(QString::fromStdString(script.gameName)));
71 resize(600, 500);
72 setModal(true);
73
74 // Disable close button while installing
75 setWindowFlags(windowFlags() & ~Qt::WindowCloseButtonHint);
76
77 startInstall();
78}
79
81{
82 if (workerThread_)
83 {
84 if (installing_)
85 {
86 worker_->cancel();
87 }
88 workerThread_->quit();
89 workerThread_->wait();
90 }
91}
92
93void LutrisInstallProgressDialog::setupUI()
94{
95 QVBoxLayout* mainLayout = new QVBoxLayout(this);
96
97 // Game info header
98 gameLabel_ = new QLabel(this);
99 gameLabel_->setText(QString("<h3>Installing %1</h3>")
100 .arg(QString::fromStdString(script_.gameName).toHtmlEscaped()));
101 mainLayout->addWidget(gameLabel_);
102
103 // Status
104 statusLabel_ = new QLabel("Preparing installation...", this);
105 statusLabel_->setStyleSheet("font-weight: bold;");
106 mainLayout->addWidget(statusLabel_);
107
108 // Current operation
109 currentOpLabel_ = new QLabel(this);
110 currentOpLabel_->setWordWrap(true);
111 mainLayout->addWidget(currentOpLabel_);
112
113 // Progress bars
114 QGroupBox* progressGroup = new QGroupBox("Progress", this);
115 QVBoxLayout* progressLayout = new QVBoxLayout(progressGroup);
116
117 QLabel* overallLabel = new QLabel("Overall Progress:", progressGroup);
118 progressLayout->addWidget(overallLabel);
119
120 overallProgress_ = new QProgressBar(progressGroup);
121 overallProgress_->setRange(0, 100);
122 overallProgress_->setValue(0);
123 overallProgress_->setTextVisible(true);
124 progressLayout->addWidget(overallProgress_);
125
126 stepInfoLabel_ = new QLabel("Step 0 of 0", progressGroup);
127 progressLayout->addWidget(stepInfoLabel_);
128
129 QLabel* currentLabel = new QLabel("Current Operation:", progressGroup);
130 progressLayout->addWidget(currentLabel);
131
132 currentProgress_ = new QProgressBar(progressGroup);
133 currentProgress_->setRange(0, 100);
134 currentProgress_->setValue(0);
135 currentProgress_->setTextVisible(true);
136 progressLayout->addWidget(currentProgress_);
137
138 downloadInfoLabel_ = new QLabel(progressGroup);
139 progressLayout->addWidget(downloadInfoLabel_);
140
141 mainLayout->addWidget(progressGroup);
142
143 // Log output
144 QGroupBox* logGroup = new QGroupBox("Installation Log", this);
145 QVBoxLayout* logLayout = new QVBoxLayout(logGroup);
146
147 logText_ = new QTextEdit(logGroup);
148 logText_->setReadOnly(true);
149 logText_->setFontFamily("monospace");
150 logText_->setMaximumHeight(150);
151 logLayout->addWidget(logText_);
152
153 mainLayout->addWidget(logGroup);
154
155 // Buttons
156 QHBoxLayout* buttonLayout = new QHBoxLayout();
157 buttonLayout->addStretch();
158
159 cancelBtn_ = new QPushButton("Cancel", this);
160 connect(cancelBtn_, &QPushButton::clicked, this, &LutrisInstallProgressDialog::onCancelClicked);
161 buttonLayout->addWidget(cancelBtn_);
162
163 closeBtn_ = new QPushButton("Close", this);
164 closeBtn_->setEnabled(false);
165 connect(closeBtn_, &QPushButton::clicked, this, &LutrisInstallProgressDialog::onCloseClicked);
166 buttonLayout->addWidget(closeBtn_);
167
168 mainLayout->addLayout(buttonLayout);
169}
170
171void LutrisInstallProgressDialog::startInstall()
172{
173 installing_ = true;
174 cancelled_ = false;
175
176 // Create worker thread
177 workerThread_ = new QThread(this);
178 worker_ = new LutrisInstallWorker();
179 worker_->setScript(script_);
180 worker_->setConfig(config_);
181 worker_->moveToThread(workerThread_);
182
183 // Explicit Qt::QueuedConnection on worker -> GUI signals so the intent is
184 // unambiguous regardless of thread-affinity reshuffling.
185 connect(workerThread_, &QThread::started, worker_, &LutrisInstallWorker::doInstall);
186 connect(worker_, &LutrisInstallWorker::progressUpdate, this,
187 &LutrisInstallProgressDialog::onProgressUpdate, Qt::QueuedConnection);
188 connect(worker_, &LutrisInstallWorker::logMessage, this,
189 &LutrisInstallProgressDialog::onLogMessage, Qt::QueuedConnection);
190 connect(worker_, &LutrisInstallWorker::installComplete, this,
191 &LutrisInstallProgressDialog::onInstallComplete, Qt::QueuedConnection);
192 connect(worker_, &LutrisInstallWorker::installComplete, workerThread_, &QThread::quit);
193 connect(workerThread_, &QThread::finished, worker_, &QObject::deleteLater);
194
195 workerThread_->start();
196}
197
198void LutrisInstallProgressDialog::onProgressUpdate(const InstallProgress& progress)
199{
200 // Update status based on state
201 QString stateText;
202 switch (progress.state)
203 {
205 stateText = "Idle";
206 break;
208 stateText = "Preparing...";
209 break;
211 stateText = "Downloading files...";
212 break;
214 stateText = "Extracting archives...";
215 break;
217 stateText = "Installing...";
218 break;
220 stateText = "Configuring Wine...";
221 break;
223 stateText = "Finalizing...";
224 break;
226 stateText = "Complete!";
227 break;
229 stateText = "Failed";
230 break;
232 stateText = "Cancelled";
233 break;
234 }
235 statusLabel_->setText(stateText);
236
237 // Current operation
238 currentOpLabel_->setText(QString::fromStdString(progress.currentOperation));
239
240 // Overall progress
241 overallProgress_->setValue(progress.overallPercent);
242
243 // Step info
244 stepInfoLabel_->setText(
245 QString("Step %1 of %2").arg(progress.currentStep).arg(progress.totalSteps));
246
247 // Current progress (for downloads)
248 if (progress.state == InstallState::DownloadingFiles && progress.totalBytes > 0)
249 {
250 int percent = static_cast<int>((progress.bytesDownloaded * 100) / progress.totalBytes);
251 currentProgress_->setValue(percent);
252
253 QString info = QString("%1 / %2")
254 .arg(formatSize(progress.bytesDownloaded))
255 .arg(formatSize(progress.totalBytes));
256
257 if (progress.downloadSpeedBytesPerSec > 0)
258 {
259 info += QString(" - %1/s").arg(formatSpeed(progress.downloadSpeedBytesPerSec));
260
261 // ETA calculation
262 if (progress.bytesDownloaded < progress.totalBytes)
263 {
264 uint64_t remaining = progress.totalBytes - progress.bytesDownloaded;
265 int eta = static_cast<int>(static_cast<double>(remaining) /
266 progress.downloadSpeedBytesPerSec);
267 info += QString(" - ETA: %1").arg(formatEta(eta));
268 }
269 }
270 downloadInfoLabel_->setText(info);
271 }
272 else
273 {
274 currentProgress_->setValue(progress.operationPercent);
275 downloadInfoLabel_->clear();
276 }
277}
278
279void LutrisInstallProgressDialog::onLogMessage(const QString& message)
280{
281 logText_->append(message);
282
283 // Auto-scroll to bottom
284 QTextCursor cursor = logText_->textCursor();
285 cursor.movePosition(QTextCursor::End);
286 logText_->setTextCursor(cursor);
287}
288
289void LutrisInstallProgressDialog::onInstallComplete(const InstallResult& result)
290{
291 installing_ = false;
292 result_ = result;
293
294 // Update UI
295 cancelBtn_->setEnabled(false);
296 closeBtn_->setEnabled(true);
297
298 // Re-enable close button
299 setWindowFlags(windowFlags() | Qt::WindowCloseButtonHint);
300 show();
301
302 if (result.success)
303 {
304 statusLabel_->setText(tr("Installation Complete!"));
305 statusLabel_->setStyleSheet("font-weight: bold; color: green;");
306 overallProgress_->setValue(100);
307 currentProgress_->setValue(100);
308
309 onLogMessage("Installation completed successfully!");
310
311 // Add game to library
312 if (libraryController_)
313 {
314 try
315 {
316 Game game;
317 game.name = script_.gameName;
319
320 // Set runner based on script runner
321 if (script_.runner == "wine")
323 else if (script_.runner == "linux")
325 else if (script_.runner == "steam")
327 else
329
330 // Sync deprecated platform field
332
333 // Set runner configuration
336 game.runnerConfig.arguments = result.arguments;
337
338 // Wine prefix for Wine games
339 if (!result.winePrefixPath.empty())
340 {
342 }
343
344 // Environment variables
345 for (const auto& [key, value] : result.environmentVariables)
346 {
347 game.runnerConfig.environmentVariables[key] = value;
348 }
349
350 libraryController_->addGame(game);
351 onLogMessage("Game added to library!");
352 }
353 catch (const std::exception& e)
354 {
355 onLogMessage(QString("Warning: Failed to add game to library: %1").arg(e.what()));
356 }
357 }
358
359 QMessageBox::information(this, tr("Installation Complete"),
360 QString("%1 has been installed successfully!")
361 .arg(QString::fromStdString(script_.gameName)));
362 }
363 else
364 {
365 statusLabel_->setText(tr("Installation Failed"));
366 statusLabel_->setStyleSheet("font-weight: bold; color: red;");
367
368 onLogMessage(
369 QString("Installation failed: %1").arg(QString::fromStdString(result.errorMessage)));
370
371 QMessageBox::critical(this, tr("Installation Failed"),
372 QString("Failed to install %1:\n\n%2")
373 .arg(QString::fromStdString(script_.gameName))
374 .arg(QString::fromStdString(result.errorMessage)));
375 }
376}
377
378void LutrisInstallProgressDialog::onCancelClicked()
379{
380 if (!installing_)
381 {
382 reject();
383 return;
384 }
385
386 int result = QMessageBox::question(this, tr("Cancel Installation"),
387 tr("Are you sure you want to cancel the installation?\n\n"
388 "Any downloaded files will be kept for later use."),
389 QMessageBox::Yes | QMessageBox::No);
390
391 if (result == QMessageBox::Yes)
392 {
393 cancelled_ = true;
394 statusLabel_->setText(tr("Cancelling..."));
395 cancelBtn_->setEnabled(false);
396
397 if (worker_)
398 {
399 worker_->cancel();
400 }
401 }
402}
403
404void LutrisInstallProgressDialog::onCloseClicked()
405{
406 if (result_.success)
407 {
408 accept();
409 }
410 else
411 {
412 reject();
413 }
414}
415
417{
418 if (installing_)
419 {
420 event->ignore();
421 onCancelClicked();
422 }
423 else
424 {
425 event->accept();
426 }
427}
428
429QString LutrisInstallProgressDialog::formatSpeed(double bytesPerSec) const
430{
431 if (bytesPerSec < 1024)
432 {
433 return QString("%1 B").arg(static_cast<int>(bytesPerSec));
434 }
435 else if (bytesPerSec < 1024 * 1024)
436 {
437 return QString("%1 KB").arg(bytesPerSec / 1024, 0, 'f', 1);
438 }
439 else
440 {
441 return QString("%1 MB").arg(bytesPerSec / (1024 * 1024), 0, 'f', 1);
442 }
443}
444
445QString LutrisInstallProgressDialog::formatEta(int seconds) const
446{
447 if (seconds < 60)
448 {
449 return QString("%1s").arg(seconds);
450 }
451 else if (seconds < 3600)
452 {
453 int min = seconds / 60;
454 int sec = seconds % 60;
455 return QString("%1m %2s").arg(min).arg(sec);
456 }
457 else
458 {
459 int hours = seconds / 3600;
460 int min = (seconds % 3600) / 60;
461 return QString("%1h %2m").arg(hours).arg(min);
462 }
463}
464
465QString LutrisInstallProgressDialog::formatSize(uint64_t bytes) const
466{
467 if (bytes == 0)
468 return "0 B";
469
470 const char* units[] = {"B", "KB", "MB", "GB", "TB"};
471 int unitIndex = 0;
472 double size = static_cast<double>(bytes);
473
474 while (size >= 1024.0 && unitIndex < 4)
475 {
476 size /= 1024.0;
477 unitIndex++;
478 }
479
480 if (unitIndex == 0)
481 {
482 return QString("%1 B").arg(static_cast<int>(size));
483 }
484 else
485 {
486 return QString("%1 %2").arg(size, 0, 'f', 1).arg(units[unitIndex]);
487 }
488}
std::string name
Display name shown in UI.
Definition game.h:774
void syncPlatformFromRunner()
Sync the deprecated platform field from the current runner and source.
Definition game.h:912
GameRunner runner
Runner/compatibility layer to use (Native, Proton, Wine, etc.).
Definition game.h:777
GameRunnerConfig runnerConfig
How to launch and run the game.
Definition game.h:784
GameSource source
Where the game was acquired from (Steam, GOG, Local, etc.).
Definition game.h:775
static void cancel()
Cancel current installation.
static InstallResult install(const LutrisInstallerScript &script, const InstallConfig &config, const ProgressCallback &progressCallback=nullptr)
Execute an installation (blocking).
LutrisInstallProgressDialog(const LutrisInstallerScript &script, const InstallConfig &config, core::GameLibraryController *libraryController, QWidget *parent=nullptr)
void closeEvent(QCloseEvent *event) override
void installComplete(const InstallResult &result)
void logMessage(const QString &message)
void progressUpdate(const InstallProgress &progress)
void setConfig(const InstallConfig &config)
LutrisInstallWorker(QObject *parent=nullptr)
void setScript(const LutrisInstallerScript &script)
@ Lutris
Lutris community scripts.
Definition game.h:45
@ Native
Native Linux executable (no compatibility layer).
Definition game.h:82
@ Wine
Wine compatibility layer.
Definition game.h:83
std::string executablePath
Full path to game executable.
Definition game.h:141
std::string workingDirectory
Working directory for game execution.
Definition game.h:142
std::string arguments
Command line arguments to pass to game.
Definition game.h:143
std::string winePrefix
WINEPREFIX path (Wine install location).
Definition game.h:167
std::map< std::string, std::string > environmentVariables
Environment variables to set before launching (KEY=VALUE pairs).
Definition game.h:146
std::map< std::string, std::string > environmentVariables
@ Game
Game-specific fixes.