fix: escape quetes in shorcut creation arguments
Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
parent
e0c569365f
commit
cdf8ad2c94
1 changed files with 27 additions and 21 deletions
|
@ -897,6 +897,29 @@ QString getApplicationsDir()
|
|||
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
|
||||
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);
|
||||
QTextStream stream(&f);
|
||||
|
||||
QString argstring;
|
||||
if (!args.empty())
|
||||
argstring = " \"" + args.join("\" \"") + "\"";
|
||||
auto argstring = quoteArgs(args, "\"", "\\\"");
|
||||
|
||||
stream << "#!/bin/bash" << "\n";
|
||||
stream << "\"" << target << "\" " << argstring << "\n";
|
||||
|
@ -984,9 +1005,7 @@ QString createShortcut(QString destination, QString target, QStringList args, QS
|
|||
f.open(QIODevice::WriteOnly | QIODevice::Text);
|
||||
QTextStream stream(&f);
|
||||
|
||||
QString argstring;
|
||||
if (!args.empty())
|
||||
argstring = " '" + args.join("' '") + "'";
|
||||
auto argstring = quoteArgs(args, "'", "'\\''");
|
||||
|
||||
stream << "[Desktop Entry]" << "\n";
|
||||
stream << "Type=Application" << "\n";
|
||||
|
@ -1030,20 +1049,7 @@ QString createShortcut(QString destination, QString target, QStringList args, QS
|
|||
return QString();
|
||||
}
|
||||
|
||||
QString argStr;
|
||||
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(" ");
|
||||
}
|
||||
}
|
||||
|
||||
auto argStr = quoteArgs(args, "\"", "\\\"", true);
|
||||
if (argStr.length() >= MAX_PATH) {
|
||||
qWarning() << "Arguments string is too long!";
|
||||
return QString();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue