App: guard importAllGames task during shutdown

This commit is contained in:
Marco Allegretti 2026-02-13 18:30:00 +01:00
parent 058fe8c8e0
commit 4b7655bc6b
2 changed files with 420 additions and 272 deletions

View file

@ -12,14 +12,17 @@
#include "retroarchimporter.h"
#include "steamimporter.h"
#include <QCoreApplication>
#include <QDir>
#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonParseError>
#include <QPointer>
#include <QSaveFile>
#include <QStandardPaths>
#include <QThread>
#include <QtConcurrent>
App *App::s_instance = nullptr;
@ -34,6 +37,12 @@ App::App(QObject *parent)
, m_mediaManager(new MediaManager(this))
, m_config(new Config(this))
{
if (QCoreApplication::instance()) {
connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit, this, [this]() {
m_shuttingDown.store(true);
});
}
loadLibrary();
if (!m_config->importSteam()) {
@ -147,6 +156,11 @@ App::App(QObject *parent)
});
}
App::~App()
{
m_shuttingDown.store(true);
}
App *App::instance()
{
if (!s_instance) {
@ -225,7 +239,7 @@ void App::setImportStatus(const QString &status)
void App::importAllGames()
{
if (m_importing)
if (m_importing || m_shuttingDown.load())
return;
const bool anyEnabled = m_config->importSteam() || m_config->importLutris() || m_config->importHeroic() || m_config->importDesktop()
@ -256,29 +270,50 @@ void App::importAllGames()
setImporting(true);
setImportStatus(tr("Importing games..."));
[[maybe_unused]] auto future = QtConcurrent::run([this, doSteam, doLutris, doHeroic, doDesktop, doBottles, doFlatpak, doItch, doLegendary, doRetroArch]() {
[[maybe_unused]] auto future =
QtConcurrent::run([self = QPointer<App>(this), doSteam, doLutris, doHeroic, doDesktop, doBottles, doFlatpak, doItch, doLegendary, doRetroArch]() {
if (!self || self->m_shuttingDown.load()) {
return;
}
QThread *appThread = self->thread();
auto shouldAbort = [&self]() {
return !self || self->m_shuttingDown.load();
};
int totalCount = 0;
// Import from Steam
if (doSteam) {
QMetaObject::invokeMethod(
this,
[this]() {
setImportStatus(tr("Scanning Steam library..."));
self.data(),
[self]() {
if (self && !self->m_shuttingDown.load()) {
self->setImportStatus(self->tr("Scanning Steam library..."));
}
},
Qt::QueuedConnection);
SteamImporter steamImporter;
QList<Game *> steamGames = steamImporter.importGames();
if (shouldAbort()) {
for (Game *game : steamGames) {
game->moveToThread(this->thread());
delete game;
}
return;
}
for (Game *game : steamGames) {
if (game) {
game->moveToThread(appThread);
game->setParent(nullptr);
}
}
QMetaObject::invokeMethod(
this,
[this, steamGames]() {
if (!m_config->importSteam()) {
self.data(),
[self, steamGames]() {
if (!self || self->m_shuttingDown.load() || !self->m_config->importSteam()) {
for (Game *game : steamGames) {
if (game) {
game->deleteLater();
@ -287,7 +322,7 @@ void App::importAllGames()
return;
}
for (Game *game : steamGames) {
m_gameModel->addGame(game);
self->m_gameModel->addGame(game);
}
},
Qt::QueuedConnection);
@ -297,23 +332,34 @@ void App::importAllGames()
// Import from Lutris
if (doLutris) {
QMetaObject::invokeMethod(
this,
[this]() {
setImportStatus(tr("Scanning Lutris library..."));
self.data(),
[self]() {
if (self && !self->m_shuttingDown.load()) {
self->setImportStatus(self->tr("Scanning Lutris library..."));
}
},
Qt::QueuedConnection);
LutrisImporter lutrisImporter;
QList<Game *> lutrisGames = lutrisImporter.importGames();
if (shouldAbort()) {
for (Game *game : lutrisGames) {
game->moveToThread(this->thread());
delete game;
}
return;
}
for (Game *game : lutrisGames) {
if (game) {
game->moveToThread(appThread);
game->setParent(nullptr);
}
}
QMetaObject::invokeMethod(
this,
[this, lutrisGames]() {
if (!m_config->importLutris()) {
self.data(),
[self, lutrisGames]() {
if (!self || self->m_shuttingDown.load() || !self->m_config->importLutris()) {
for (Game *game : lutrisGames) {
if (game) {
game->deleteLater();
@ -322,7 +368,9 @@ void App::importAllGames()
return;
}
for (Game *game : lutrisGames) {
m_gameModel->addGame(game);
if (game) {
self->m_gameModel->addGame(game);
}
}
},
Qt::QueuedConnection);
@ -332,23 +380,34 @@ void App::importAllGames()
// Import from Heroic
if (doHeroic) {
QMetaObject::invokeMethod(
this,
[this]() {
setImportStatus(tr("Scanning Heroic library..."));
self.data(),
[self]() {
if (self && !self->m_shuttingDown.load()) {
self->setImportStatus(self->tr("Scanning Heroic library..."));
}
},
Qt::QueuedConnection);
HeroicImporter heroicImporter;
QList<Game *> heroicGames = heroicImporter.importGames();
if (shouldAbort()) {
for (Game *game : heroicGames) {
game->moveToThread(this->thread());
delete game;
}
return;
}
for (Game *game : heroicGames) {
if (game) {
game->moveToThread(appThread);
game->setParent(nullptr);
}
}
QMetaObject::invokeMethod(
this,
[this, heroicGames]() {
if (!m_config->importHeroic()) {
self.data(),
[self, heroicGames]() {
if (!self || self->m_shuttingDown.load() || !self->m_config->importHeroic()) {
for (Game *game : heroicGames) {
if (game) {
game->deleteLater();
@ -357,7 +416,9 @@ void App::importAllGames()
return;
}
for (Game *game : heroicGames) {
m_gameModel->addGame(game);
if (game) {
self->m_gameModel->addGame(game);
}
}
},
Qt::QueuedConnection);
@ -367,23 +428,34 @@ void App::importAllGames()
// Import from Desktop entries
if (doDesktop) {
QMetaObject::invokeMethod(
this,
[this]() {
setImportStatus(tr("Scanning desktop entries..."));
self.data(),
[self]() {
if (self && !self->m_shuttingDown.load()) {
self->setImportStatus(self->tr("Scanning desktop entries..."));
}
},
Qt::QueuedConnection);
DesktopImporter desktopImporter;
QList<Game *> desktopGames = desktopImporter.importGames();
if (shouldAbort()) {
for (Game *game : desktopGames) {
game->moveToThread(this->thread());
delete game;
}
return;
}
for (Game *game : desktopGames) {
if (game) {
game->moveToThread(appThread);
game->setParent(nullptr);
}
}
QMetaObject::invokeMethod(
this,
[this, desktopGames]() {
if (!m_config->importDesktop()) {
self.data(),
[self, desktopGames]() {
if (!self || self->m_shuttingDown.load() || !self->m_config->importDesktop()) {
for (Game *game : desktopGames) {
if (game) {
game->deleteLater();
@ -392,7 +464,9 @@ void App::importAllGames()
return;
}
for (Game *game : desktopGames) {
m_gameModel->addGame(game);
if (game) {
self->m_gameModel->addGame(game);
}
}
},
Qt::QueuedConnection);
@ -402,23 +476,34 @@ void App::importAllGames()
// Import from Bottles
if (doBottles) {
QMetaObject::invokeMethod(
this,
[this]() {
setImportStatus(tr("Scanning Bottles..."));
self.data(),
[self]() {
if (self && !self->m_shuttingDown.load()) {
self->setImportStatus(self->tr("Scanning Bottles..."));
}
},
Qt::QueuedConnection);
BottlesImporter bottlesImporter;
QList<Game *> bottlesGames = bottlesImporter.importGames();
if (shouldAbort()) {
for (Game *game : bottlesGames) {
game->moveToThread(this->thread());
delete game;
}
return;
}
for (Game *game : bottlesGames) {
if (game) {
game->moveToThread(appThread);
game->setParent(nullptr);
}
}
QMetaObject::invokeMethod(
this,
[this, bottlesGames]() {
if (!m_config->importBottles()) {
self.data(),
[self, bottlesGames]() {
if (!self || self->m_shuttingDown.load() || !self->m_config->importBottles()) {
for (Game *game : bottlesGames) {
if (game) {
game->deleteLater();
@ -427,7 +512,9 @@ void App::importAllGames()
return;
}
for (Game *game : bottlesGames) {
m_gameModel->addGame(game);
if (game) {
self->m_gameModel->addGame(game);
}
}
},
Qt::QueuedConnection);
@ -437,23 +524,34 @@ void App::importAllGames()
// Import from Flatpak
if (doFlatpak) {
QMetaObject::invokeMethod(
this,
[this]() {
setImportStatus(tr("Scanning Flatpak games..."));
self.data(),
[self]() {
if (self && !self->m_shuttingDown.load()) {
self->setImportStatus(self->tr("Scanning Flatpak games..."));
}
},
Qt::QueuedConnection);
FlatpakImporter flatpakImporter;
QList<Game *> flatpakGames = flatpakImporter.importGames();
if (shouldAbort()) {
for (Game *game : flatpakGames) {
game->moveToThread(this->thread());
delete game;
}
return;
}
for (Game *game : flatpakGames) {
if (game) {
game->moveToThread(appThread);
game->setParent(nullptr);
}
}
QMetaObject::invokeMethod(
this,
[this, flatpakGames]() {
if (!m_config->importFlatpak()) {
self.data(),
[self, flatpakGames]() {
if (!self || self->m_shuttingDown.load() || !self->m_config->importFlatpak()) {
for (Game *game : flatpakGames) {
if (game) {
game->deleteLater();
@ -462,7 +560,9 @@ void App::importAllGames()
return;
}
for (Game *game : flatpakGames) {
m_gameModel->addGame(game);
if (game) {
self->m_gameModel->addGame(game);
}
}
},
Qt::QueuedConnection);
@ -472,23 +572,34 @@ void App::importAllGames()
// Import from itch.io
if (doItch) {
QMetaObject::invokeMethod(
this,
[this]() {
setImportStatus(tr("Scanning itch.io library..."));
self.data(),
[self]() {
if (self && !self->m_shuttingDown.load()) {
self->setImportStatus(self->tr("Scanning itch.io library..."));
}
},
Qt::QueuedConnection);
ItchImporter itchImporter;
QList<Game *> itchGames = itchImporter.importGames();
if (shouldAbort()) {
for (Game *game : itchGames) {
game->moveToThread(this->thread());
delete game;
}
return;
}
for (Game *game : itchGames) {
if (game) {
game->moveToThread(appThread);
game->setParent(nullptr);
}
}
QMetaObject::invokeMethod(
this,
[this, itchGames]() {
if (!m_config->importItch()) {
self.data(),
[self, itchGames]() {
if (!self || self->m_shuttingDown.load() || !self->m_config->importItch()) {
for (Game *game : itchGames) {
if (game) {
game->deleteLater();
@ -497,7 +608,9 @@ void App::importAllGames()
return;
}
for (Game *game : itchGames) {
m_gameModel->addGame(game);
if (game) {
self->m_gameModel->addGame(game);
}
}
},
Qt::QueuedConnection);
@ -507,23 +620,34 @@ void App::importAllGames()
// Import from Legendary
if (doLegendary) {
QMetaObject::invokeMethod(
this,
[this]() {
setImportStatus(tr("Scanning Legendary library..."));
self.data(),
[self]() {
if (self && !self->m_shuttingDown.load()) {
self->setImportStatus(self->tr("Scanning Legendary library..."));
}
},
Qt::QueuedConnection);
LegendaryImporter legendaryImporter;
QList<Game *> legendaryGames = legendaryImporter.importGames();
if (shouldAbort()) {
for (Game *game : legendaryGames) {
game->moveToThread(this->thread());
delete game;
}
return;
}
for (Game *game : legendaryGames) {
if (game) {
game->moveToThread(appThread);
game->setParent(nullptr);
}
}
QMetaObject::invokeMethod(
this,
[this, legendaryGames]() {
if (!m_config->importLegendary()) {
self.data(),
[self, legendaryGames]() {
if (!self || self->m_shuttingDown.load() || !self->m_config->importLegendary()) {
for (Game *game : legendaryGames) {
if (game) {
game->deleteLater();
@ -532,7 +656,9 @@ void App::importAllGames()
return;
}
for (Game *game : legendaryGames) {
m_gameModel->addGame(game);
if (game) {
self->m_gameModel->addGame(game);
}
}
},
Qt::QueuedConnection);
@ -542,23 +668,34 @@ void App::importAllGames()
// Import from RetroArch
if (doRetroArch) {
QMetaObject::invokeMethod(
this,
[this]() {
setImportStatus(tr("Scanning RetroArch playlists..."));
self.data(),
[self]() {
if (self && !self->m_shuttingDown.load()) {
self->setImportStatus(self->tr("Scanning RetroArch playlists..."));
}
},
Qt::QueuedConnection);
RetroArchImporter retroArchImporter;
QList<Game *> retroArchGames = retroArchImporter.importGames();
if (shouldAbort()) {
for (Game *game : retroArchGames) {
game->moveToThread(this->thread());
delete game;
}
return;
}
for (Game *game : retroArchGames) {
if (game) {
game->moveToThread(appThread);
game->setParent(nullptr);
}
}
QMetaObject::invokeMethod(
this,
[this, retroArchGames]() {
if (!m_config->importRetroArch()) {
self.data(),
[self, retroArchGames]() {
if (!self || self->m_shuttingDown.load() || !self->m_config->importRetroArch()) {
for (Game *game : retroArchGames) {
if (game) {
game->deleteLater();
@ -567,7 +704,9 @@ void App::importAllGames()
return;
}
for (Game *game : retroArchGames) {
m_gameModel->addGame(game);
if (game) {
self->m_gameModel->addGame(game);
}
}
},
Qt::QueuedConnection);
@ -576,12 +715,15 @@ void App::importAllGames()
// Complete
QMetaObject::invokeMethod(
this,
[this, totalCount]() {
setImportStatus(tr("Import complete: %1 games found").arg(totalCount));
setImporting(false);
saveLibrary();
Q_EMIT importCompleted(totalCount);
self.data(),
[self, totalCount]() {
if (!self || self->m_shuttingDown.load()) {
return;
}
self->setImportStatus(self->tr("Import complete: %1 games found").arg(totalCount));
self->setImporting(false);
self->saveLibrary();
Q_EMIT self->importCompleted(totalCount);
},
Qt::QueuedConnection);
});

View file

@ -3,6 +3,8 @@
#pragma once
#include <atomic>
#include <QHash>
#include <QJsonObject>
#include <QObject>
@ -36,6 +38,8 @@ public:
static App *instance();
static App *create(QQmlEngine *engine, QJSEngine *scriptEngine);
~App() override;
GameModel *gameModel() const;
GameLauncher *launcher() const;
RunnerManagerClient *runnerManager() const;
@ -90,6 +94,8 @@ private:
QString m_importStatus;
QHash<QString, QJsonObject> m_removedGames;
std::atomic_bool m_shuttingDown{false};
void setImporting(bool importing);
void setImportStatus(const QString &status);
};