Fix escaping in scheme creation on Linux and set -workdir

This commit is contained in:
Ilya Fedin 2020-11-15 11:18:23 +04:00 committed by John Preston
parent e64f6f7266
commit c8ce5dfa8b
1 changed files with 27 additions and 31 deletions

View File

@ -124,10 +124,12 @@ void PortalAutostart(bool autostart, bool silent = false) {
QVariantMap options; QVariantMap options;
options["reason"] = tr::lng_settings_auto_start(tr::now); options["reason"] = tr::lng_settings_auto_start(tr::now);
options["autostart"] = autostart; options["autostart"] = autostart;
options["commandline"] = QStringList({ options["commandline"] = QStringList{
cExeName(), cExeName(),
qsl("-workdir"),
cWorkingDir(),
qsl("-autostart") qsl("-autostart")
}); };
options["dbus-activatable"] = false; options["dbus-activatable"] = false;
auto message = QDBusMessage::createMethodCall( auto message = QDBusMessage::createMethodCall(
@ -228,6 +230,10 @@ uint FileChooserPortalVersion() {
} }
#endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION #endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION
QString EscapeShellInLauncher(const QString &content) {
return EscapeShell(content.toUtf8()).replace('\\', "\\\\");
}
QString FlatpakID() { QString FlatpakID() {
static const auto Result = [] { static const auto Result = [] {
if (!qEnvironmentVariableIsEmpty("FLATPAK_ID")) { if (!qEnvironmentVariableIsEmpty("FLATPAK_ID")) {
@ -291,35 +297,26 @@ bool GenerateDesktopFile(
QFile target(targetFile); QFile target(targetFile);
if (target.open(QIODevice::WriteOnly)) { if (target.open(QIODevice::WriteOnly)) {
if (!Core::UpdaterDisabled()) {
fileText = fileText.replace( fileText = fileText.replace(
QRegularExpression( QRegularExpression(
qsl("^TryExec=.*$"), qsl("^TryExec=.*$"),
QRegularExpression::MultilineOption), QRegularExpression::MultilineOption),
qsl("TryExec=") qsl("TryExec=%1").arg(
+ QFile::encodeName(cExeDir() + cExeName()) QString(cExeDir() + cExeName()).replace('\\', "\\\\")));
.replace('\\', "\\\\"));
fileText = fileText.replace( fileText = fileText.replace(
QRegularExpression( QRegularExpression(
qsl("^Exec=.*$"), qsl("^Exec=.*$"),
QRegularExpression::MultilineOption), QRegularExpression::MultilineOption),
qsl("Exec=") qsl("Exec=%1 -workdir %2").arg(
+ EscapeShell(QFile::encodeName(cExeDir() + cExeName())) EscapeShellInLauncher(cExeDir() + cExeName()),
.replace('\\', "\\\\") EscapeShellInLauncher(cWorkingDir()))
+ (args.isEmpty() ? QString() : ' ' + args)); + (args.isEmpty() ? QString() : ' ' + args));
} else {
fileText = fileText.replace(
QRegularExpression(
qsl("^Exec=(.*) -- %u$"),
QRegularExpression::MultilineOption),
qsl("Exec=\\1")
+ (args.isEmpty() ? QString() : ' ' + args));
}
target.write(fileText.toUtf8()); target.write(fileText.toUtf8());
target.close(); target.close();
if (IsStaticBinary()) { if (!Core::UpdaterDisabled()) {
DEBUG_LOG(("App Info: removing old .desktop files")); DEBUG_LOG(("App Info: removing old .desktop files"));
QFile::remove(qsl("%1telegram.desktop").arg(targetPath)); QFile::remove(qsl("%1telegram.desktop").arg(targetPath));
QFile::remove(qsl("%1telegramdesktop.desktop").arg(targetPath)); QFile::remove(qsl("%1telegramdesktop.desktop").arg(targetPath));
@ -1217,10 +1214,9 @@ void RegisterCustomScheme(bool force) {
GError *error = nullptr; GError *error = nullptr;
const auto neededCommandlineBuilder = qsl("%1 --") const auto neededCommandlineBuilder = qsl("%1 -workdir %2 --").arg(
.arg(!Core::UpdaterDisabled() QString(EscapeShell(QFile::encodeName(cExeDir() + cExeName()))),
? cExeDir() + cExeName() QString(EscapeShell(QFile::encodeName(cWorkingDir()))));
: cExeName());
const auto neededCommandline = qsl("%1 %u") const auto neededCommandline = qsl("%1 %u")
.arg(neededCommandlineBuilder); .arg(neededCommandlineBuilder);