fix: escape quetes in shorcut creation arguments

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
Trial97 2025-06-25 13:13:42 +03:00
parent e0c569365f
commit cdf8ad2c94
No known key found for this signature in database
GPG key ID: 55EF5DA53DB36318

View file

@ -897,6 +897,29 @@ QString getApplicationsDir()
return QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation); return QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation);
} }
QString quoteArgs(const QStringList& args, const QString& wrap, const QString& escapeChar, bool wrapOnlyIfNeeded = false)
{
QString result;
auto size = args.size();
for (int i = 0; i < size; ++i) {
QString arg = args[i];
arg.replace(wrap, escapeChar);
bool needsWrapping = !wrapOnlyIfNeeded || arg.contains(' ') || arg.contains('\t') || arg.contains(wrap);
if (needsWrapping)
result += wrap + arg + wrap;
else
result += arg;
if (i < size - 1)
result += ' ';
}
return result;
}
// Cross-platform Shortcut creation // Cross-platform Shortcut creation
QString createShortcut(QString destination, QString target, QStringList args, QString name, QString icon) QString createShortcut(QString destination, QString target, QStringList args, QString name, QString icon)
{ {
@ -940,9 +963,7 @@ QString createShortcut(QString destination, QString target, QStringList args, QS
f.open(QIODevice::WriteOnly | QIODevice::Text); f.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream stream(&f); QTextStream stream(&f);
QString argstring; auto argstring = quoteArgs(args, "\"", "\\\"");
if (!args.empty())
argstring = " \"" + args.join("\" \"") + "\"";
stream << "#!/bin/bash" << "\n"; stream << "#!/bin/bash" << "\n";
stream << "\"" << target << "\" " << argstring << "\n"; stream << "\"" << target << "\" " << argstring << "\n";
@ -984,14 +1005,12 @@ QString createShortcut(QString destination, QString target, QStringList args, QS
f.open(QIODevice::WriteOnly | QIODevice::Text); f.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream stream(&f); QTextStream stream(&f);
QString argstring; auto argstring = quoteArgs(args, "'", "'\\''");
if (!args.empty())
argstring = " '" + args.join("' '") + "'";
stream << "[Desktop Entry]" << "\n"; stream << "[Desktop Entry]" << "\n";
stream << "Type=Application" << "\n"; stream << "Type=Application" << "\n";
stream << "Categories=Game;ActionGame;AdventureGame;Simulation" << "\n"; stream << "Categories=Game;ActionGame;AdventureGame;Simulation" << "\n";
stream << "Exec=\"" << target.toLocal8Bit() << "\"" << argstring.toLocal8Bit() << "\n"; stream << "Exec=\"" << target.toLocal8Bit() << "\" " << argstring.toLocal8Bit() << "\n";
stream << "Name=" << name.toLocal8Bit() << "\n"; stream << "Name=" << name.toLocal8Bit() << "\n";
if (!icon.isEmpty()) { if (!icon.isEmpty()) {
stream << "Icon=" << icon.toLocal8Bit() << "\n"; stream << "Icon=" << icon.toLocal8Bit() << "\n";
@ -1030,20 +1049,7 @@ QString createShortcut(QString destination, QString target, QStringList args, QS
return QString(); return QString();
} }
QString argStr; auto argStr = quoteArgs(args, "\"", "\\\"", true);
int argCount = args.count();
for (int i = 0; i < argCount; i++) {
if (args[i].contains(' ')) {
argStr.append('"').append(args[i]).append('"');
} else {
argStr.append(args[i]);
}
if (i < argCount - 1) {
argStr.append(" ");
}
}
if (argStr.length() >= MAX_PATH) { if (argStr.length() >= MAX_PATH) {
qWarning() << "Arguments string is too long!"; qWarning() << "Arguments string is too long!";
return QString(); return QString();