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 actionScreenshots;
TranslatedAction actionExportInstance;
#if defined(Q_OS_LINUX) // currently only implemented for linux; TODO: other OS implementations
TranslatedAction actionCreateShortcut;
#endif
QVector<TranslatedAction *> 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)
{

View File

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

View File

@ -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
}

View File

@ -1,10 +1,4 @@
<?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">
<class>CreateShortcutDialog</class>
<widget class="QDialog" name="CreateShortcutDialog">
@ -13,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>796</width>
<height>230</height>
<height>232</height>
</rect>
</property>
<property name="minimumSize">
@ -31,10 +25,10 @@
<property name="sizeConstraint">
<enum>QLayout::SetDefaultConstraint</enum>
</property>
<item row="1" column="0">
<widget class="QCheckBox" name="joinServerCheckBox">
<item row="3" column="0">
<widget class="QCheckBox" name="launchOfflineCheckBox">
<property name="text">
<string>Join server on launch:</string>
<string>Launch in offline mode</string>
</property>
</widget>
</item>
@ -45,22 +39,6 @@
</property>
</widget>
</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">
<widget class="QPushButton" name="shortcutPathBrowse">
<property name="text">
@ -68,6 +46,29 @@
</property>
</widget>
</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">
<widget class="QCheckBox" name="useProfileCheckBox">
<property name="text">
@ -75,15 +76,8 @@
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QComboBox" name="profileComboBox"/>
</item>
<item row="4" column="0">
<widget class="QCheckBox" name="offlineUsernameCheckBox">
<property name="text">
<string>Set offline mode username:</string>
</property>
</widget>
<item row="1" column="1">
<widget class="QLineEdit" name="joinServer"/>
</item>
</layout>
</item>
@ -101,11 +95,25 @@
</spacer>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="bottomMargin">
<number>0</number>
</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>
</layout>
</widget>