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

View file

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