From b0269e6dfc685cd002340e95e80942410f1b1fc5 Mon Sep 17 00:00:00 2001 From: ADudeCalledLeo <7997354+Leo40Git@users.noreply.github.com> Date: Sun, 13 Nov 2022 17:51:29 +0200 Subject: [PATCH] Linux: fixes - Fix shortcut icons - Possibly fix shortcut creation on AppImages - Fix shortcut not working if path to launcher contains spaces Signed-off-by: ADudeCalledLeo <7997354+Leo40Git@users.noreply.github.com> --- launcher/FileSystem.cpp | 4 ++-- launcher/ui/MainWindow.cpp | 48 +++++++++++++++++++++++++++++++++----- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/launcher/FileSystem.cpp b/launcher/FileSystem.cpp index 80715498..e1059ca9 100644 --- a/launcher/FileSystem.cpp +++ b/launcher/FileSystem.cpp @@ -382,8 +382,8 @@ bool createShortcut(QString destination, QString target, QStringList args, QStri << "\n"; stream << "Type=Application" << "\n"; - stream << "TryExec=" << target.toLocal8Bit() << "\n"; - stream << "Exec=" << target.toLocal8Bit() << argstring.toLocal8Bit() << "\n"; + stream << "TryExec=\"" << target.toLocal8Bit() << "\"\n"; + stream << "Exec=\"" << target.toLocal8Bit() << "\"" << argstring.toLocal8Bit() << "\n"; stream << "Name=" << name.toLocal8Bit() << "\n"; if (!icon.isEmpty()) { diff --git a/launcher/ui/MainWindow.cpp b/launcher/ui/MainWindow.cpp index c7de46c7..4dbac967 100644 --- a/launcher/ui/MainWindow.cpp +++ b/launcher/ui/MainWindow.cpp @@ -2101,13 +2101,14 @@ void MainWindow::on_actionCreateInstanceShortcut_triggered() #if defined(Q_OS_MACOS) QString appPath = QApplication::applicationFilePath(); - if (appPath.startsWith("/private/var")) { + if (appPath.startsWith("/private/var/")) { QMessageBox::critical(this, tr("Create instance shortcut"), tr("The launcher is in the folder it was extracted from, therefore it cannot create shortcuts.")); return; } if (FS::createShortcut(FS::PathCombine(desktopPath, m_selectedInstance->name()), - appPath, { "--launch", m_selectedInstance->id() }, m_selectedInstance->name(), "")) { + appPath, { "--launch", m_selectedInstance->id() }, + m_selectedInstance->name(), "")) { QMessageBox::information(this, tr("Create instance shortcut"), tr("Created a shortcut to this instance on your desktop!")); } else @@ -2115,14 +2116,48 @@ void MainWindow::on_actionCreateInstanceShortcut_triggered() QMessageBox::critical(this, tr("Create instance shortcut"), tr("Failed to create instance shortcut!")); } #elif defined(Q_OS_LINUX) || defined(Q_OS_FREEBSD) || defined(Q_OS_OPENBSD) + QString appPath = QApplication::applicationFilePath(); + if (appPath.startsWith("/tmp/.mount_")) { + // AppImage! + appPath = QProcessEnvironment::systemEnvironment().value(QStringLiteral("APPIMAGE")); + if (appPath.isEmpty()) + { + QMessageBox::critical(this, tr("Create instance shortcut"), tr("Launcher is running as misconfigured AppImage? ($APPIMAGE environment variable is missing)")); + } + else if (appPath.endsWith("/")) + { + appPath.chop(1); + } + } + auto icon = APPLICATION->icons()->icon(m_selectedInstance->iconKey()); + QString iconPath = FS::PathCombine(m_selectedInstance->instanceRoot(), "icon.png"); + + QFile iconFile(iconPath); + if (!iconFile.open(QFile::WriteOnly)) + { + QMessageBox::critical(this, tr("Create instance shortcut"), tr("Failed to create icon for shortcut.")); + return; + } + bool success = icon->icon().pixmap(64, 64).save(&iconFile, "PNG"); + iconFile.close(); + + if (!success) + { + iconFile.remove(); + QMessageBox::critical(this, tr("Create instance shortcut"), tr("Failed to create icon for shortcut.")); + return; + } + if (FS::createShortcut(FS::PathCombine(desktopPath, m_selectedInstance->name()), - QApplication::applicationFilePath(), { "--launch", m_selectedInstance->id() }, m_selectedInstance->name(), icon->getFilePath())) { + appPath, { "--launch", m_selectedInstance->id() }, + m_selectedInstance->name(), iconPath)) { QMessageBox::information(this, tr("Create instance shortcut"), tr("Created a shortcut to this instance on your desktop!")); } else { + iconFile.remove(); QMessageBox::critical(this, tr("Create instance shortcut"), tr("Failed to create instance shortcut!")); } #elif defined(Q_OS_WIN) @@ -2137,7 +2172,7 @@ void MainWindow::on_actionCreateInstanceShortcut_triggered() QFile iconFile(iconPath); if (!iconFile.open(QFile::WriteOnly)) { - QMessageBox::critical(this, tr("Create instance shortcut"), tr("Failed to create instance shortcut!")); + QMessageBox::critical(this, tr("Create instance shortcut"), tr("Failed to create icon for shortcut.")); return; } bool success = icon->icon().pixmap(64, 64).save(&iconFile, "ICO"); @@ -2149,12 +2184,13 @@ void MainWindow::on_actionCreateInstanceShortcut_triggered() if (!success) { iconFile.remove(); - QMessageBox::critical(this, tr("Create instance shortcut"), tr("Failed to create instance shortcut!")); + QMessageBox::critical(this, tr("Create instance shortcut"), tr("Failed to create icon for shortcut.")); return; } if (FS::createShortcut(FS::PathCombine(desktopPath, m_selectedInstance->name()), - QApplication::applicationFilePath(), { "--launch", m_selectedInstance->id() }, m_selectedInstance->name(), iconPath)) { + QApplication::applicationFilePath(), { "--launch", m_selectedInstance->id() }, + m_selectedInstance->name(), iconPath)) { QMessageBox::information(this, tr("Create instance shortcut"), tr("Created a shortcut to this instance on your desktop!")); } else