Add src/launcher.cpp

This commit is contained in:
milonekrone 2026-01-14 16:08:10 +01:00
parent 1140696700
commit 9fdc093774

75
src/launcher.cpp Normal file
View File

@ -0,0 +1,75 @@
#include "launcher.h"
#include <QDesktopServices>
#include <QProcess>
#include <QRegularExpression>
#include <QUrl>
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;
}