From 6a3ff58c8c641a232697b93b387be657e2fa2d07 Mon Sep 17 00:00:00 2001 From: arthomnix Date: Sun, 3 Jul 2022 09:52:56 +0100 Subject: [PATCH] NOISSUE shortcut creation: add option to create launch scripts This allows shortcuts to be created on Macs (which don't have a concept of desktop shortcuts) as well as Linux systems that don't support the desktop file specification. Also included a windows batch file implementation. --- launcher/ui/MainWindow.cpp | 6 -- launcher/ui/MainWindow.h | 2 - launcher/ui/dialogs/CreateShortcutDialog.cpp | 81 ++++++++++++------ launcher/ui/dialogs/CreateShortcutDialog.ui | 86 +++++++++++--------- 4 files changed, 104 insertions(+), 71 deletions(-) diff --git a/launcher/ui/MainWindow.cpp b/launcher/ui/MainWindow.cpp index 343d35e1..139ca780 100644 --- a/launcher/ui/MainWindow.cpp +++ b/launcher/ui/MainWindow.cpp @@ -223,9 +223,7 @@ public: TranslatedAction actionLaunchInstanceOffline; TranslatedAction actionScreenshots; TranslatedAction actionExportInstance; -#if defined(Q_OS_LINUX) // currently only implemented for linux; TODO: other OS implementations TranslatedAction actionCreateShortcut; -#endif QVector all_actions; LabeledToolButton *renameButton = nullptr; @@ -598,14 +596,12 @@ public: instanceToolBar->addSeparator(); -#if defined(Q_OS_LINUX) actionCreateShortcut = TranslatedAction(MainWindow); actionCreateShortcut->setObjectName(QStringLiteral("actionCreateShortcut")); actionCreateShortcut.setTextId(QT_TRANSLATE_NOOP("MainWindow", "Create Shortcut")); actionCreateShortcut.setTooltipId(QT_TRANSLATE_NOOP("MainWindow", "Create a shortcut that launches the selected instance")); all_actions.append(&actionCreateShortcut); instanceToolBar->addAction(actionCreateShortcut); -#endif actionExportInstance = TranslatedAction(MainWindow); actionExportInstance->setObjectName(QStringLiteral("actionExportInstance")); @@ -1857,14 +1853,12 @@ void MainWindow::on_actionLaunchInstance_triggered() } } -#if defined(Q_OS_LINUX) void MainWindow::on_actionCreateShortcut_triggered() { if (m_selectedInstance) { CreateShortcutDialog(this, m_selectedInstance).exec(); } } -#endif void MainWindow::activateInstance(InstancePtr instance) { diff --git a/launcher/ui/MainWindow.h b/launcher/ui/MainWindow.h index 24d9fee0..685adba9 100644 --- a/launcher/ui/MainWindow.h +++ b/launcher/ui/MainWindow.h @@ -141,9 +141,7 @@ private slots: void on_actionScreenshots_triggered(); -#if defined(Q_OS_LINUX) void on_actionCreateShortcut_triggered(); -#endif void taskEnd(); diff --git a/launcher/ui/dialogs/CreateShortcutDialog.cpp b/launcher/ui/dialogs/CreateShortcutDialog.cpp index 011b67fd..2c260514 100644 --- a/launcher/ui/dialogs/CreateShortcutDialog.cpp +++ b/launcher/ui/dialogs/CreateShortcutDialog.cpp @@ -35,6 +35,11 @@ CreateShortcutDialog::CreateShortcutDialog(QWidget *parent, InstancePtr instance ui->profileComboBox->setCurrentText(accounts->defaultAccount()->profileName()); } +#if defined(Q_OS_WIN) || (defined(Q_OS_UNIX) && !defined(Q_OS_LINUX)) + ui->createScriptCheckBox->setEnabled(false); + ui->createScriptCheckBox->setChecked(true); +#endif + updateDialogState(); } @@ -46,11 +51,11 @@ CreateShortcutDialog::~CreateShortcutDialog() void CreateShortcutDialog::on_shortcutPathBrowse_clicked() { QString linkExtension; -#ifdef Q_OS_LINUX - linkExtension = "desktop"; +#ifdef Q_OS_UNIX + linkExtension = ui->createScriptCheckBox->isChecked() ? "sh" : "desktop"; #endif #ifdef Q_OS_WIN - linkExtension = "lnk"; + linkExtension = ui->createScriptCheckBox->isChecked() ? "bat" : "lnk"; #endif QFileDialog fileDialog(this, tr("Select shortcut path"), QStandardPaths::writableLocation(QStandardPaths::DesktopLocation)); fileDialog.setDefaultSuffix(linkExtension); @@ -98,29 +103,57 @@ QString CreateShortcutDialog::getLaunchCommand() void CreateShortcutDialog::createShortcut() { - // Linux implementation using .desktop file -#ifdef Q_OS_LINUX - // save the launcher icon to a file so we can use it in the shortcut - if (!QFileInfo::exists(QCoreApplication::applicationDirPath() + "/shortcut-icon.png")) +#ifdef Q_OS_WIN + if (ui->createScriptCheckBox->isChecked()) // on windows, creating .lnk shortcuts requires specific win32 api stuff + // rather than just writing a text file { - QPixmap iconPixmap = QIcon(":/logo.svg").pixmap(64, 64); - iconPixmap.save(QCoreApplication::applicationDirPath() + "/shortcut-icon.png"); - } +#endif + QString shortcutText; +#ifdef Q_OS_UNIX + // Unix shell script + if (ui->createScriptCheckBox->isChecked()) + { + shortcutText = "#!/bin/sh\n" + getLaunchCommand() + " &\n"; + } else + // freedesktop.org desktop entry + { + // save the launcher icon to a file so we can use it in the shortcut + if (!QFileInfo::exists(QCoreApplication::applicationDirPath() + "/shortcut-icon.png")) + { + QPixmap iconPixmap = QIcon(":/logo.svg").pixmap(64, 64); + iconPixmap.save(QCoreApplication::applicationDirPath() + "/shortcut-icon.png"); + } - QFile desktopFile(ui->shortcutPath->text()); - if (desktopFile.open(QIODevice::WriteOnly)) - { - QTextStream stream(&desktopFile); - qDebug() << m_instance->iconKey(); - stream << "[Desktop Entry]" << endl - << "Type=Application" << endl - << "Name=" << m_instance->name() << " - " << BuildConfig.LAUNCHER_DISPLAYNAME << endl - << "Exec=" << getLaunchCommand() << endl - << "Icon=" << QCoreApplication::applicationDirPath() << "/shortcut-icon.png" << endl; - desktopFile.setPermissions(QFile::ReadOwner | QFile::ReadGroup | QFile::ReadOther - | QFile::WriteOwner | QFile::ExeOwner | QFile::ExeGroup); - desktopFile.close(); + shortcutText = "[Desktop Entry]\n" + "Type=Application\n" + "Name=" + m_instance->name() + " - " + BuildConfig.LAUNCHER_DISPLAYNAME + "\n" + + "Exec=" + getLaunchCommand() + "\n" + + "Icon=" + QCoreApplication::applicationDirPath() + "/shortcut-icon.png\n"; + + } +#endif +#ifdef Q_OS_WIN + // Windows batch script implementation + if (ui->createScriptCheckBox->isChecked()) + { + shortcutText = "@ECHO OFF\r\n" + "START /B " + getLaunchCommand() + "\r\n"; + } + else + { + // TODO: windows .lnk implementation + } +#endif + QFile shortcutFile(ui->shortcutPath->text()); + if (shortcutFile.open(QIODevice::WriteOnly)) + { + QTextStream stream(&shortcutFile); + stream << shortcutText; + shortcutFile.setPermissions(QFile::ReadOwner | QFile::ReadGroup | QFile::ReadOther + | QFile::WriteOwner | QFile::ExeOwner | QFile::ExeGroup); + shortcutFile.close(); + } +#ifdef Q_OS_WIN } #endif - // TODO: implementations for other operating systems } \ No newline at end of file diff --git a/launcher/ui/dialogs/CreateShortcutDialog.ui b/launcher/ui/dialogs/CreateShortcutDialog.ui index b3e17d12..c8d1cc87 100644 --- a/launcher/ui/dialogs/CreateShortcutDialog.ui +++ b/launcher/ui/dialogs/CreateShortcutDialog.ui @@ -1,10 +1,4 @@ - CreateShortcutDialog @@ -13,7 +7,7 @@ 0 0 796 - 230 + 232 @@ -31,10 +25,10 @@ QLayout::SetDefaultConstraint - - + + - Join server on launch: + Launch in offline mode @@ -45,22 +39,6 @@ - - - - - - - - - - Launch in offline mode - - - - - - @@ -68,6 +46,29 @@ + + + + + + + + + + + + + Set offline mode username: + + + + + + + Join server on launch: + + + @@ -75,15 +76,8 @@ - - - - - - - Set offline mode username: - - + + @@ -101,11 +95,25 @@ - - - QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + 0 - + + + + Create script instead of shortcut + + + + + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + +