From 9fdc093774ab6277e723e052d0e033665ac37209 Mon Sep 17 00:00:00 2001 From: milonekrone Date: Wed, 14 Jan 2026 16:08:10 +0100 Subject: [PATCH] Add src/launcher.cpp --- src/launcher.cpp | 75 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/launcher.cpp diff --git a/src/launcher.cpp b/src/launcher.cpp new file mode 100644 index 0000000..2031252 --- /dev/null +++ b/src/launcher.cpp @@ -0,0 +1,75 @@ +#include "launcher.h" +#include +#include +#include +#include + +Launcher::Launcher(QObject* parent) : QObject(parent) {} + +QString Launcher::sanitizeDesktopExec(QString exec) const { + // Entfernt Desktop-Placeholders wie %U %u %f etc. + exec.replace(QRegularExpression("\\s%[fFuUdDnNickvm]"), ""); + exec.replace(QRegularExpression("\\s%\\w"), ""); + return exec.trimmed(); +} + +bool Launcher::launchUrl(const QString& url) { + if (url.isEmpty()) return false; + return QDesktopServices::openUrl(QUrl(url)); +} + +bool Launcher::launchCommand(const QString& exec, const QStringList& args) { + if (exec.isEmpty()) return false; + return QProcess::startDetached(exec, args); +} + +bool Launcher::launchDesktop(const QVariantMap& e) { + // e["exec"] kommt aus .desktop + const QString execRaw = e.value("exec").toString(); + if (execRaw.isEmpty()) return false; + + const QString cleaned = sanitizeDesktopExec(execRaw); + + // Splitting: sehr simpel (für robuste Exec parsing später -> KShell / xdg spec parser) + QString program = cleaned; + QStringList args; + + const int sp = cleaned.indexOf(' '); + if (sp > 0) { + program = cleaned.left(sp); + args = cleaned.mid(sp + 1).split(' ', Qt::SkipEmptyParts); + } + + return launchCommand(program, args); +} + +bool Launcher::launch(const QVariantMap& entry) { + const int type = entry.value("type").toInt(); + const QString url = entry.value("url").toString(); + const QString gameId = entry.value("gameId").toString(); + const QString exec = entry.value("exec").toString(); + const QStringList args = entry.value("args").toStringList(); + + // 1) Steam GameId (steam://rungameid/XXXX) + if (!gameId.isEmpty()) { + return launchUrl(QString("steam://rungameid/%1").arg(gameId)); + } + + // 2) URL (steam://, http(s)://) + if (!url.isEmpty()) { + return launchUrl(url); + } + + // 3) Exec/Args (Custom Action) + if (!exec.isEmpty()) { + // Spezialfälle: mpv playback (Videos), KDE Desktop, Gaming Mode, etc. + return launchCommand(exec, args); + } + + // 4) Desktop Entry + if (type == 0 /*Desktop*/) { + return launchDesktop(entry); + } + + return false; +}