mirror of
https://invent.kde.org/marcoa/shift-shell.git
synced 2026-07-29 07:44:45 +00:00
Extract common launch logic into launchEntry
Both launch() and launchByStorageId() duplicated the process spawn, signal emission, and timestamp bookkeeping. Move it to a single private launchEntry(GameEntry&) method.
This commit is contained in:
parent
a1044567cd
commit
daa8fc7d8a
2 changed files with 29 additions and 57 deletions
|
|
@ -112,14 +112,31 @@ void GameLauncherProvider::launch(int index)
|
||||||
if (index < 0 || index >= m_games.size()) {
|
if (index < 0 || index >= m_games.size()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const auto &g = m_games.at(index);
|
// Find the matching entry in m_allGames so the timestamp update persists
|
||||||
|
const QString &sid = m_games.at(index).storageId;
|
||||||
|
for (auto &entry : m_allGames) {
|
||||||
|
if (entry.storageId == sid) {
|
||||||
|
launchEntry(entry);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (g.source == QLatin1String("desktop")) {
|
void GameLauncherProvider::launchByStorageId(const QString &storageId)
|
||||||
// Launch via KService for proper activation tracking
|
{
|
||||||
auto service = KService::serviceByStorageId(g.storageId);
|
for (auto &entry : m_allGames) {
|
||||||
|
if (entry.storageId == storageId) {
|
||||||
|
launchEntry(entry);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void GameLauncherProvider::launchEntry(GameEntry &entry)
|
||||||
|
{
|
||||||
|
if (entry.source == QLatin1String("desktop")) {
|
||||||
|
auto service = KService::serviceByStorageId(entry.storageId);
|
||||||
if (service) {
|
if (service) {
|
||||||
// Use QProcess to launch the exec line — KIO::ApplicationLauncherJob
|
|
||||||
// would be better but requires KIOWidgets which is heavy for a plugin.
|
|
||||||
QStringList args = KShell::splitArgs(service->exec());
|
QStringList args = KShell::splitArgs(service->exec());
|
||||||
if (!args.isEmpty()) {
|
if (!args.isEmpty()) {
|
||||||
QString program = args.takeFirst();
|
QString program = args.takeFirst();
|
||||||
|
|
@ -127,63 +144,17 @@ void GameLauncherProvider::launch(int index)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Steam, Flatpak, etc. — run the launch command directly
|
QStringList parts = entry.launchCommand.split(QLatin1Char(' '));
|
||||||
QStringList parts = g.launchCommand.split(QLatin1Char(' '));
|
|
||||||
if (!parts.isEmpty()) {
|
if (!parts.isEmpty()) {
|
||||||
QString program = parts.takeFirst();
|
QString program = parts.takeFirst();
|
||||||
QProcess::startDetached(program, parts);
|
QProcess::startDetached(program, parts);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Q_EMIT gameLaunched(g.name);
|
Q_EMIT gameLaunched(entry.name);
|
||||||
|
const auto now = QDateTime::currentDateTime();
|
||||||
// Record timestamp for "recently played"
|
saveRecentTimestamp(entry.storageId, now);
|
||||||
saveRecentTimestamp(g.storageId, QDateTime::currentDateTime());
|
entry.lastPlayed = now;
|
||||||
|
|
||||||
// Update the in-memory entry so recentGames() picks it up immediately
|
|
||||||
for (auto &entry : m_allGames) {
|
|
||||||
if (entry.storageId == g.storageId) {
|
|
||||||
entry.lastPlayed = QDateTime::currentDateTime();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void GameLauncherProvider::launchByStorageId(const QString &storageId)
|
|
||||||
{
|
|
||||||
for (int i = 0; i < m_allGames.size(); ++i) {
|
|
||||||
if (m_allGames.at(i).storageId == storageId) {
|
|
||||||
// Find the index in the filtered model, or launch from allGames directly
|
|
||||||
for (int j = 0; j < m_games.size(); ++j) {
|
|
||||||
if (m_games.at(j).storageId == storageId) {
|
|
||||||
launch(j);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Not in filtered view — launch directly from allGames
|
|
||||||
const auto &g = m_allGames.at(i);
|
|
||||||
if (g.source == QLatin1String("desktop")) {
|
|
||||||
auto service = KService::serviceByStorageId(g.storageId);
|
|
||||||
if (service) {
|
|
||||||
QStringList args = KShell::splitArgs(service->exec());
|
|
||||||
if (!args.isEmpty()) {
|
|
||||||
QString program = args.takeFirst();
|
|
||||||
QProcess::startDetached(program, args);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
QStringList parts = g.launchCommand.split(QLatin1Char(' '));
|
|
||||||
if (!parts.isEmpty()) {
|
|
||||||
QString program = parts.takeFirst();
|
|
||||||
QProcess::startDetached(program, parts);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Q_EMIT gameLaunched(g.name);
|
|
||||||
saveRecentTimestamp(g.storageId, QDateTime::currentDateTime());
|
|
||||||
m_allGames[i].lastPlayed = QDateTime::currentDateTime();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameLauncherProvider::deduplicateGames()
|
void GameLauncherProvider::deduplicateGames()
|
||||||
|
|
|
||||||
|
|
@ -78,6 +78,7 @@ private:
|
||||||
void loadRecentTimestamps();
|
void loadRecentTimestamps();
|
||||||
void saveRecentTimestamp(const QString &storageId, const QDateTime &when);
|
void saveRecentTimestamp(const QString &storageId, const QDateTime &when);
|
||||||
void applyFilter();
|
void applyFilter();
|
||||||
|
void launchEntry(GameEntry &entry);
|
||||||
|
|
||||||
QList<GameEntry> m_allGames;
|
QList<GameEntry> m_allGames;
|
||||||
QList<GameEntry> m_games; // filtered view
|
QList<GameEntry> m_games; // filtered view
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue