game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
lutrissearchdialog.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// qt/lutrissearchdialog.cpp
4// ==============================================================================
5// Lutris search dialog implementation
6// ==============================================================================
7
9
10#include <QApplication>
11#include <QGroupBox>
12#include <QHBoxLayout>
13#include <QHeaderView>
14#include <QMessageBox>
15#include <QSplitter>
16#include <QVBoxLayout>
17
18// ==============================================================================
19// LutrisSearchWorker Implementation
20// ==============================================================================
21
22LutrisSearchWorker::LutrisSearchWorker(QObject* parent) : QObject(parent) {}
23
24void LutrisSearchWorker::setQuery(const QString& query)
25{
26 query_ = query;
27}
28
30{
31 if (query_.isEmpty())
32 {
33 emit searchComplete({});
34 return;
35 }
36
37 try
38 {
39 auto results = LutrisAPIService::searchGames(query_.toStdString());
40 emit searchComplete(results);
41 }
42 catch (const std::exception& e)
43 {
44 emit searchError(QString("Search failed: %1").arg(e.what()));
45 }
46}
47
48void LutrisSearchWorker::loadInstallers(const QString& gameSlug)
49{
50 try
51 {
52 auto installers = LutrisAPIService::getInstallers(gameSlug.toStdString());
53 emit installersLoaded(gameSlug, installers);
54 }
55 catch (const std::exception& e)
56 {
57 emit searchError(QString("Failed to load installers: %1").arg(e.what()));
58 }
59}
60
61void LutrisSearchWorker::loadInstallerScript(const QString& gameSlug, const QString& installerSlug)
62{
63 try
64 {
65 auto script = LutrisAPIService::getInstallerScript(gameSlug.toStdString(),
66 installerSlug.toStdString());
67 emit installerScriptLoaded(installerSlug, script);
68 }
69 catch (const std::exception& e)
70 {
71 emit installerScriptError(installerSlug,
72 QString("Failed to load script: %1").arg(e.what()));
73 }
74}
75
76// ==============================================================================
77// LutrisSearchDialog Implementation
78// ==============================================================================
79
81 QWidget* parent)
82 : QDialog(parent), libraryController_(libraryController)
83{
84 setupUI();
85 setWindowTitle(tr("Install from Lutris"));
86 resize(900, 600);
87
88 // Setup worker thread
89 workerThread_ = new QThread(this);
90 worker_ = new LutrisSearchWorker();
91 worker_->moveToThread(workerThread_);
92
93 connect(worker_, &LutrisSearchWorker::searchComplete, this,
94 &LutrisSearchDialog::onSearchComplete);
95 connect(worker_, &LutrisSearchWorker::searchError, this, &LutrisSearchDialog::onSearchError);
96 connect(worker_, &LutrisSearchWorker::installersLoaded, this,
97 &LutrisSearchDialog::onInstallersLoaded);
98 connect(worker_, &LutrisSearchWorker::installerScriptLoaded, this,
99 &LutrisSearchDialog::onInstallerScriptLoaded);
100 connect(worker_, &LutrisSearchWorker::installerScriptError, this,
101 &LutrisSearchDialog::onInstallerScriptError);
102
103 connect(workerThread_, &QThread::finished, worker_, &QObject::deleteLater);
104
105 workerThread_->start();
106
107 // Setup search debounce timer
108 searchDebounce_ = new QTimer(this);
109 searchDebounce_->setSingleShot(true);
110 searchDebounce_->setInterval(300); // 300ms debounce
111 connect(searchDebounce_, &QTimer::timeout, this, &LutrisSearchDialog::onSearchTriggered);
112}
113
115{
116 if (workerThread_)
117 {
118 workerThread_->quit();
119 workerThread_->wait();
120 }
121}
122
123void LutrisSearchDialog::setupUI()
124{
125 QVBoxLayout* mainLayout = new QVBoxLayout(this);
126
127 // Search bar
128 QHBoxLayout* searchLayout = new QHBoxLayout();
129
130 searchEdit_ = new QLineEdit(this);
131 searchEdit_->setPlaceholderText(tr("Search for games..."));
132 searchEdit_->setClearButtonEnabled(true);
133 connect(searchEdit_, &QLineEdit::textChanged, this, &LutrisSearchDialog::onSearchTextChanged);
134 connect(searchEdit_, &QLineEdit::returnPressed, this, &LutrisSearchDialog::onSearchTriggered);
135 searchLayout->addWidget(searchEdit_);
136
137 searchBtn_ = new QPushButton("Search", this);
138 connect(searchBtn_, &QPushButton::clicked, this, &LutrisSearchDialog::onSearchTriggered);
139 searchLayout->addWidget(searchBtn_);
140
141 mainLayout->addLayout(searchLayout);
142
143 // Status and progress
144 QHBoxLayout* statusLayout = new QHBoxLayout();
145 statusLabel_ = new QLabel("Enter a game name to search", this);
146 statusLayout->addWidget(statusLabel_);
147 statusLayout->addStretch();
148 mainLayout->addLayout(statusLayout);
149
150 progressBar_ = new QProgressBar(this);
151 progressBar_->setRange(0, 0); // Indeterminate
152 progressBar_->setVisible(false);
153 mainLayout->addWidget(progressBar_);
154
155 // Splitter for games and installers
156 QSplitter* splitter = new QSplitter(Qt::Horizontal, this);
157
158 // Games list
159 QWidget* gamesWidget = new QWidget(splitter);
160 QVBoxLayout* gamesLayout = new QVBoxLayout(gamesWidget);
161 gamesLayout->setContentsMargins(0, 0, 0, 0);
162
163 QLabel* gamesLabel = new QLabel("Games", gamesWidget);
164 gamesLabel->setStyleSheet("font-weight: bold;");
165 gamesLayout->addWidget(gamesLabel);
166
167 gameTree_ = new QTreeWidget(gamesWidget);
168 gameTree_->setHeaderLabels({"Name", "Year", "Installers"});
169 gameTree_->setRootIsDecorated(false);
170 gameTree_->setSelectionMode(QAbstractItemView::SingleSelection);
171 gameTree_->header()->setStretchLastSection(false);
172 gameTree_->header()->setSectionResizeMode(0, QHeaderView::Stretch);
173 gameTree_->header()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
174 gameTree_->header()->setSectionResizeMode(2, QHeaderView::ResizeToContents);
175 connect(gameTree_, &QTreeWidget::itemSelectionChanged, this,
176 &LutrisSearchDialog::onGameSelectionChanged);
177 gamesLayout->addWidget(gameTree_);
178
179 splitter->addWidget(gamesWidget);
180
181 // Installers list
182 QWidget* installersWidget = new QWidget(splitter);
183 QVBoxLayout* installersLayout = new QVBoxLayout(installersWidget);
184 installersLayout->setContentsMargins(0, 0, 0, 0);
185
186 installerLabel_ = new QLabel("Installers", installersWidget);
187 installerLabel_->setStyleSheet("font-weight: bold;");
188 installersLayout->addWidget(installerLabel_);
189
190 installerTree_ = new QTreeWidget(installersWidget);
191 installerTree_->setHeaderLabels({"Installer", "Runner", "Version"});
192 installerTree_->setRootIsDecorated(false);
193 installerTree_->setSelectionMode(QAbstractItemView::SingleSelection);
194 installerTree_->header()->setStretchLastSection(false);
195 installerTree_->header()->setSectionResizeMode(0, QHeaderView::Stretch);
196 installerTree_->header()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
197 installerTree_->header()->setSectionResizeMode(2, QHeaderView::ResizeToContents);
198 connect(installerTree_, &QTreeWidget::itemClicked, this,
199 &LutrisSearchDialog::onInstallerSelected);
200 connect(installerTree_, &QTreeWidget::itemDoubleClicked, this,
201 [this](QTreeWidgetItem*, int) { onInstallClicked(); });
202 installersLayout->addWidget(installerTree_);
203
204 splitter->addWidget(installersWidget);
205
206 // Set initial sizes (60/40 split)
207 splitter->setSizes({540, 360});
208 mainLayout->addWidget(splitter, 1);
209
210 // Dialog buttons
211 QHBoxLayout* buttonLayout = new QHBoxLayout();
212 buttonLayout->addStretch();
213
214 installBtn_ = new QPushButton("Install...", this);
215 installBtn_->setEnabled(false);
216 connect(installBtn_, &QPushButton::clicked, this, &LutrisSearchDialog::onInstallClicked);
217 buttonLayout->addWidget(installBtn_);
218
219 closeBtn_ = new QPushButton("Close", this);
220 connect(closeBtn_, &QPushButton::clicked, this, &QDialog::reject);
221 buttonLayout->addWidget(closeBtn_);
222
223 mainLayout->addLayout(buttonLayout);
224}
225
226void LutrisSearchDialog::onSearchTextChanged(const QString& text)
227{
228 Q_UNUSED(text);
229 // Debounce search
230 searchDebounce_->start();
231}
232
233void LutrisSearchDialog::onSearchTriggered()
234{
235 QString query = searchEdit_->text().trimmed();
236 if (query.isEmpty())
237 {
238 statusLabel_->setText(tr("Enter a game name to search"));
239 gameTree_->clear();
240 clearInstallers();
241 return;
242 }
243
244 if (query.length() < 2)
245 {
246 statusLabel_->setText(tr("Please enter at least 2 characters"));
247 return;
248 }
249
250 startSearch(query);
251}
252
253void LutrisSearchDialog::startSearch(const QString& query)
254{
255 searching_ = true;
256 progressBar_->setVisible(true);
257 statusLabel_->setText(tr("Searching..."));
258 gameTree_->clear();
259 clearInstallers();
260
261 worker_->setQuery(query);
262 QMetaObject::invokeMethod(worker_, "doSearch", Qt::QueuedConnection);
263}
264
265void LutrisSearchDialog::onSearchComplete(const std::vector<LutrisSearchResult>& results)
266{
267 searching_ = false;
268 progressBar_->setVisible(false);
269 searchResults_ = results;
270
271 gameTree_->clear();
272
273 if (results.empty())
274 {
275 statusLabel_->setText(tr("No games found"));
276 return;
277 }
278
279 for (const auto& result : results)
280 {
281 QTreeWidgetItem* item = new QTreeWidgetItem();
282 item->setText(0, QString::fromStdString(result.name));
283 item->setText(1, result.year > 0 ? QString::number(result.year) : "-");
284 item->setText(2, QString::number(result.installerCount));
285 item->setData(0, Qt::UserRole, QString::fromStdString(result.slug));
286
287 gameTree_->addTopLevelItem(item);
288 }
289
290 statusLabel_->setText(QString("Found %1 game(s)").arg(results.size()));
291}
292
293void LutrisSearchDialog::onSearchError(const QString& error)
294{
295 searching_ = false;
296 progressBar_->setVisible(false);
297 statusLabel_->setText(error);
298 QMessageBox::warning(this, tr("Search Error"), error);
299}
300
301void LutrisSearchDialog::onGameSelectionChanged()
302{
303 QList<QTreeWidgetItem*> selected = gameTree_->selectedItems();
304 if (selected.isEmpty())
305 {
306 clearInstallers();
307 return;
308 }
309
310 QTreeWidgetItem* item = selected.first();
311 QString gameSlug = item->data(0, Qt::UserRole).toString();
312
313 if (gameSlug == selectedGameSlug_)
314 {
315 return; // Already loaded
316 }
317
318 selectedGameSlug_ = gameSlug;
319 clearInstallers();
320
321 installerLabel_->setText(QString("Installers for %1 - Loading...").arg(item->text(0)));
322 progressBar_->setVisible(true);
323
324 QMetaObject::invokeMethod(worker_, "loadInstallers", Qt::QueuedConnection,
325 Q_ARG(QString, gameSlug));
326}
327
328void LutrisSearchDialog::onInstallersLoaded(const QString& gameSlug,
329 const std::vector<LutrisInstallerSummary>& installers)
330{
331 progressBar_->setVisible(false);
332
333 if (gameSlug != selectedGameSlug_)
334 {
335 return; // Selection changed while loading
336 }
337
338 currentInstallers_ = installers;
339 installerTree_->clear();
340
341 if (installers.empty())
342 {
343 installerLabel_->setText(tr("No installers available"));
344 return;
345 }
346
347 // Find game name for label
348 QString gameName;
349 for (const auto& result : searchResults_)
350 {
351 if (result.slug == gameSlug.toStdString())
352 {
353 gameName = QString::fromStdString(result.name);
354 break;
355 }
356 }
357
358 installerLabel_->setText(
359 QString("Installers for %1 (%2 available)").arg(gameName).arg(installers.size()));
360
361 for (const auto& installer : installers)
362 {
363 QTreeWidgetItem* item = new QTreeWidgetItem();
364
365 // Format installer name nicely
366 QString name = QString::fromStdString(installer.name);
367 if (name.isEmpty())
368 {
369 name = QString::fromStdString(installer.slug);
370 }
371 item->setText(0, name);
372
373 // Runner (wine, linux, steam, etc.)
374 QString runner = QString::fromStdString(installer.runner);
375 if (runner == "wine")
376 runner = "Wine/Proton";
377 else if (runner == "linux")
378 runner = "Native Linux";
379 else if (runner == "steam")
380 runner = "Steam";
381 item->setText(1, runner);
382
383 // Version
384 item->setText(2, QString::fromStdString(installer.version));
385
386 item->setData(0, Qt::UserRole, QString::fromStdString(installer.slug));
387
388 // Add tooltip with description
389 if (!installer.description.empty())
390 {
391 item->setToolTip(0, QString::fromStdString(installer.description));
392 }
393
394 installerTree_->addTopLevelItem(item);
395 }
396
397 // Auto-select first installer
398 if (installerTree_->topLevelItemCount() > 0)
399 {
400 installerTree_->setCurrentItem(installerTree_->topLevelItem(0));
401 onInstallerSelected(installerTree_->topLevelItem(0), 0);
402 }
403}
404
405void LutrisSearchDialog::onInstallerSelected(QTreeWidgetItem* item, int column)
406{
407 Q_UNUSED(column);
408
409 if (!item)
410 {
411 selectedInstallerSlug_.clear();
412 hasSelectedScript_ = false;
413 updateInstallButton();
414 return;
415 }
416
417 QString installerSlug = item->data(0, Qt::UserRole).toString();
418
419 if (installerSlug == selectedInstallerSlug_ && hasSelectedScript_)
420 {
421 return; // Already loaded
422 }
423
424 selectedInstallerSlug_ = installerSlug;
425 hasSelectedScript_ = false;
426 updateInstallButton();
427
428 // Load the installer script in background
429 progressBar_->setVisible(true);
430 statusLabel_->setText(tr("Loading installer details..."));
431
432 QMetaObject::invokeMethod(worker_, "loadInstallerScript", Qt::QueuedConnection,
433 Q_ARG(QString, selectedGameSlug_), Q_ARG(QString, installerSlug));
434}
435
436void LutrisSearchDialog::onInstallerScriptLoaded(const QString& installerSlug,
437 const LutrisInstallerScript& script)
438{
439 progressBar_->setVisible(false);
440
441 if (installerSlug != selectedInstallerSlug_)
442 {
443 return; // Selection changed
444 }
445
446 selectedScript_ = script;
447 hasSelectedScript_ = true;
448
449 // Update status with script details
450 QString status = QString("Ready to install: %1 files, %2 steps")
451 .arg(script.files.size())
452 .arg(script.steps.size());
453 statusLabel_->setText(status);
454
455 updateInstallButton();
456}
457
458void LutrisSearchDialog::onInstallerScriptError(const QString& installerSlug, const QString& error)
459{
460 progressBar_->setVisible(false);
461
462 if (installerSlug != selectedInstallerSlug_)
463 {
464 return;
465 }
466
467 hasSelectedScript_ = false;
468 statusLabel_->setText(error);
469 updateInstallButton();
470}
471
472void LutrisSearchDialog::onInstallClicked()
473{
474 if (!hasSelectedScript_)
475 {
476 QMessageBox::warning(this, tr("No Installer Selected"),
477 tr("Please select an installer first."));
478 return;
479 }
480
481 // Emit signal with the selected script for the preview dialog to handle
482 emit installRequested(selectedScript_);
483
484 // Accept dialog to close it
485 accept();
486}
487
488void LutrisSearchDialog::clearInstallers()
489{
490 installerTree_->clear();
491 currentInstallers_.clear();
492 selectedInstallerSlug_.clear();
493 hasSelectedScript_ = false;
494 installerLabel_->setText(tr("Installers"));
495 updateInstallButton();
496}
497
498void LutrisSearchDialog::updateInstallButton()
499{
500 installBtn_->setEnabled(hasSelectedScript_);
501
502 if (hasSelectedScript_)
503 {
504 installBtn_->setText(
505 QString("Install %1...").arg(QString::fromStdString(selectedScript_.gameName)));
506 }
507 else
508 {
509 installBtn_->setText(tr("Install..."));
510 }
511}
static std::vector< LutrisInstallerSummary > getInstallers(const std::string &gameSlug)
Get available installers for a game.
static std::vector< LutrisSearchResult > searchGames(const std::string &query, int maxResults=20)
Search for games by name.
static LutrisInstallerScript getInstallerScript(const std::string &gameSlug, const std::string &installerSlug)
Get full installer script with all details.
void installRequested(const LutrisInstallerScript &script)
LutrisSearchDialog(core::GameLibraryController *libraryController, QWidget *parent=nullptr)
void installerScriptLoaded(const QString &installerSlug, const LutrisInstallerScript &script)
void loadInstallers(const QString &gameSlug)
void searchError(const QString &error)
void installerScriptError(const QString &installerSlug, const QString &error)
void installersLoaded(const QString &gameSlug, const std::vector< LutrisInstallerSummary > &installers)
LutrisSearchWorker(QObject *parent=nullptr)
void searchComplete(const std::vector< LutrisSearchResult > &results)
void setQuery(const QString &query)
void loadInstallerScript(const QString &gameSlug, const QString &installerSlug)
std::vector< LutrisInstallerFile > files
std::vector< LutrisInstallStep > steps