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.
This commit is contained in:
arthomnix 2022-07-03 09:52:56 +01:00
parent 645bc3f445
commit 6a3ff58c8c
4 changed files with 104 additions and 71 deletions

View File

@ -223,9 +223,7 @@ public:
TranslatedAction actionLaunchInstanceOffline; TranslatedAction actionLaunchInstanceOffline;
TranslatedAction actionScreenshots; TranslatedAction actionScreenshots;
TranslatedAction actionExportInstance; TranslatedAction actionExportInstance;
#if defined(Q_OS_LINUX) // currently only implemented for linux; TODO: other OS implementations
TranslatedAction actionCreateShortcut; TranslatedAction actionCreateShortcut;
#endif
QVector<TranslatedAction *> all_actions; QVector<TranslatedAction *> all_actions;
LabeledToolButton *renameButton = nullptr; LabeledToolButton *renameButton = nullptr;
@ -598,14 +596,12 @@ public:
instanceToolBar->addSeparator(); instanceToolBar->addSeparator();
#if defined(Q_OS_LINUX)
actionCreateShortcut = TranslatedAction(MainWindow); actionCreateShortcut = TranslatedAction(MainWindow);
actionCreateShortcut->setObjectName(QStringLiteral("actionCreateShortcut")); actionCreateShortcut->setObjectName(QStringLiteral("actionCreateShortcut"));
actionCreateShortcut.setTextId(QT_TRANSLATE_NOOP("MainWindow", "Create Shortcut")); actionCreateShortcut.setTextId(QT_TRANSLATE_NOOP("MainWindow", "Create Shortcut"));
actionCreateShortcut.setTooltipId(QT_TRANSLATE_NOOP("MainWindow", "Create a shortcut that launches the selected instance")); actionCreateShortcut.setTooltipId(QT_TRANSLATE_NOOP("MainWindow", "Create a shortcut that launches the selected instance"));
all_actions.append(&actionCreateShortcut); all_actions.append(&actionCreateShortcut);
instanceToolBar->addAction(actionCreateShortcut); instanceToolBar->addAction(actionCreateShortcut);
#endif
actionExportInstance = TranslatedAction(MainWindow); actionExportInstance = TranslatedAction(MainWindow);
actionExportInstance->setObjectName(QStringLiteral("actionExportInstance")); actionExportInstance->setObjectName(QStringLiteral("actionExportInstance"));
@ -1857,14 +1853,12 @@ void MainWindow::on_actionLaunchInstance_triggered()
} }
} }
#if defined(Q_OS_LINUX)
void MainWindow::on_actionCreateShortcut_triggered() { void MainWindow::on_actionCreateShortcut_triggered() {
if (m_selectedInstance) if (m_selectedInstance)
{ {
CreateShortcutDialog(this, m_selectedInstance).exec(); CreateShortcutDialog(this, m_selectedInstance).exec();
} }
} }
#endif
void MainWindow::activateInstance(InstancePtr instance) void MainWindow::activateInstance(InstancePtr instance)
{ {

View File

@ -141,9 +141,7 @@ private slots:
void on_actionScreenshots_triggered(); void on_actionScreenshots_triggered();
#if defined(Q_OS_LINUX)
void on_actionCreateShortcut_triggered(); void on_actionCreateShortcut_triggered();
#endif
void taskEnd(); void taskEnd();

View File

@ -35,6 +35,11 @@ CreateShortcutDialog::CreateShortcutDialog(QWidget *parent, InstancePtr instance
ui->profileComboBox->setCurrentText(accounts->defaultAccount()->profileName()); 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(); updateDialogState();
} }
@ -46,11 +51,11 @@ CreateShortcutDialog::~CreateShortcutDialog()
void CreateShortcutDialog::on_shortcutPathBrowse_clicked() void CreateShortcutDialog::on_shortcutPathBrowse_clicked()
{ {
QString linkExtension; QString linkExtension;
#ifdef Q_OS_LINUX #ifdef Q_OS_UNIX
linkExtension = "desktop"; linkExtension = ui->createScriptCheckBox->isChecked() ? "sh" : "desktop";
#endif #endif
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
linkExtension = "lnk"; linkExtension = ui->createScriptCheckBox->isChecked() ? "bat" : "lnk";
#endif #endif
QFileDialog fileDialog(this, tr("Select shortcut path"), QStandardPaths::writableLocation(QStandardPaths::DesktopLocation)); QFileDialog fileDialog(this, tr("Select shortcut path"), QStandardPaths::writableLocation(QStandardPaths::DesktopLocation));
fileDialog.setDefaultSuffix(linkExtension); fileDialog.setDefaultSuffix(linkExtension);
@ -98,29 +103,57 @@ QString CreateShortcutDialog::getLaunchCommand()
void CreateShortcutDialog::createShortcut() void CreateShortcutDialog::createShortcut()
{ {
// Linux implementation using .desktop file #ifdef Q_OS_WIN
#ifdef Q_OS_LINUX if (ui->createScriptCheckBox->isChecked()) // on windows, creating .lnk shortcuts requires specific win32 api stuff
// save the launcher icon to a file so we can use it in the shortcut // rather than just writing a text file
if (!QFileInfo::exists(QCoreApplication::applicationDirPath() + "/shortcut-icon.png"))
{ {
QPixmap iconPixmap = QIcon(":/logo.svg").pixmap(64, 64); #endif
iconPixmap.save(QCoreApplication::applicationDirPath() + "/shortcut-icon.png"); 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()); shortcutText = "[Desktop Entry]\n"
if (desktopFile.open(QIODevice::WriteOnly)) "Type=Application\n"
{ "Name=" + m_instance->name() + " - " + BuildConfig.LAUNCHER_DISPLAYNAME + "\n"
QTextStream stream(&desktopFile); + "Exec=" + getLaunchCommand() + "\n"
qDebug() << m_instance->iconKey(); + "Icon=" + QCoreApplication::applicationDirPath() + "/shortcut-icon.png\n";
stream << "[Desktop Entry]" << endl
<< "Type=Application" << endl }
<< "Name=" << m_instance->name() << " - " << BuildConfig.LAUNCHER_DISPLAYNAME << endl #endif
<< "Exec=" << getLaunchCommand() << endl #ifdef Q_OS_WIN
<< "Icon=" << QCoreApplication::applicationDirPath() << "/shortcut-icon.png" << endl; // Windows batch script implementation
desktopFile.setPermissions(QFile::ReadOwner | QFile::ReadGroup | QFile::ReadOther if (ui->createScriptCheckBox->isChecked())
| QFile::WriteOwner | QFile::ExeOwner | QFile::ExeGroup); {
desktopFile.close(); 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 #endif
// TODO: implementations for other operating systems
} }

View File

@ -1,10 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2022 arthomnix
This source is subject to the Microsoft Public License (MS-PL).
Please see the COPYING.md file for more information.
-->
<ui version="4.0"> <ui version="4.0">
<class>CreateShortcutDialog</class> <class>CreateShortcutDialog</class>
<widget class="QDialog" name="CreateShortcutDialog"> <widget class="QDialog" name="CreateShortcutDialog">
@ -13,7 +7,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>796</width> <width>796</width>
<height>230</height> <height>232</height>
</rect> </rect>
</property> </property>
<property name="minimumSize"> <property name="minimumSize">
@ -31,10 +25,10 @@
<property name="sizeConstraint"> <property name="sizeConstraint">
<enum>QLayout::SetDefaultConstraint</enum> <enum>QLayout::SetDefaultConstraint</enum>
</property> </property>
<item row="1" column="0"> <item row="3" column="0">
<widget class="QCheckBox" name="joinServerCheckBox"> <widget class="QCheckBox" name="launchOfflineCheckBox">
<property name="text"> <property name="text">
<string>Join server on launch:</string> <string>Launch in offline mode</string>
</property> </property>
</widget> </widget>
</item> </item>
@ -45,22 +39,6 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="1" column="1">
<widget class="QLineEdit" name="joinServer"/>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="shortcutPath"/>
</item>
<item row="3" column="0">
<widget class="QCheckBox" name="launchOfflineCheckBox">
<property name="text">
<string>Launch in offline mode</string>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QLineEdit" name="offlineUsername"/>
</item>
<item row="0" column="2"> <item row="0" column="2">
<widget class="QPushButton" name="shortcutPathBrowse"> <widget class="QPushButton" name="shortcutPathBrowse">
<property name="text"> <property name="text">
@ -68,6 +46,29 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="0" column="1">
<widget class="QLineEdit" name="shortcutPath"/>
</item>
<item row="2" column="1">
<widget class="QComboBox" name="profileComboBox"/>
</item>
<item row="4" column="1">
<widget class="QLineEdit" name="offlineUsername"/>
</item>
<item row="4" column="0">
<widget class="QCheckBox" name="offlineUsernameCheckBox">
<property name="text">
<string>Set offline mode username:</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QCheckBox" name="joinServerCheckBox">
<property name="text">
<string>Join server on launch:</string>
</property>
</widget>
</item>
<item row="2" column="0"> <item row="2" column="0">
<widget class="QCheckBox" name="useProfileCheckBox"> <widget class="QCheckBox" name="useProfileCheckBox">
<property name="text"> <property name="text">
@ -75,15 +76,8 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="2" column="1"> <item row="1" column="1">
<widget class="QComboBox" name="profileComboBox"/> <widget class="QLineEdit" name="joinServer"/>
</item>
<item row="4" column="0">
<widget class="QCheckBox" name="offlineUsernameCheckBox">
<property name="text">
<string>Set offline mode username:</string>
</property>
</widget>
</item> </item>
</layout> </layout>
</item> </item>
@ -101,11 +95,25 @@
</spacer> </spacer>
</item> </item>
<item> <item>
<widget class="QDialogButtonBox" name="buttonBox"> <layout class="QHBoxLayout" name="horizontalLayout">
<property name="standardButtons"> <property name="bottomMargin">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set> <number>0</number>
</property> </property>
</widget> <item>
<widget class="QCheckBox" name="createScriptCheckBox">
<property name="text">
<string>Create script instead of shortcut</string>
</property>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</item> </item>
</layout> </layout>
</widget> </widget>