Merge pull request #4808 from arthomnix/feature-create-shortcut

NOISSUE (Re-)implement the ability to create instance shortcuts
This commit is contained in:
Petr Mrázek 2022-07-19 17:25:35 +02:00 committed by GitHub
commit bec8293f28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 613 additions and 1 deletions

View File

@ -778,7 +778,8 @@ SET(LAUNCHER_SOURCES
ui/dialogs/VersionSelectDialog.h
ui/dialogs/SkinUploadDialog.cpp
ui/dialogs/SkinUploadDialog.h
ui/dialogs/CreateShortcutDialog.cpp
ui/dialogs/CreateShortcutDialog.h
# GUI - widgets
ui/widgets/Common.cpp
@ -876,6 +877,7 @@ qt5_wrap_ui(LAUNCHER_UI
ui/dialogs/AboutDialog.ui
ui/dialogs/LoginDialog.ui
ui/dialogs/EditAccountDialog.ui
ui/dialogs/CreateShortcutDialog.ui
)
qt5_add_resources(LAUNCHER_RESOURCES

View File

@ -83,6 +83,7 @@
#include "ui/dialogs/UpdateDialog.h"
#include "ui/dialogs/EditAccountDialog.h"
#include "ui/dialogs/NotificationDialog.h"
#include "ui/dialogs/CreateShortcutDialog.h"
#include "ui/dialogs/ExportInstanceDialog.h"
#include "UpdateController.h"
@ -222,6 +223,7 @@ public:
TranslatedAction actionLaunchInstanceOffline;
TranslatedAction actionScreenshots;
TranslatedAction actionExportInstance;
TranslatedAction actionCreateShortcut;
QVector<TranslatedAction *> all_actions;
LabeledToolButton *renameButton = nullptr;
@ -594,6 +596,13 @@ public:
instanceToolBar->addSeparator();
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);
actionExportInstance = TranslatedAction(MainWindow);
actionExportInstance->setObjectName(QStringLiteral("actionExportInstance"));
actionExportInstance.setTextId(QT_TRANSLATE_NOOP("MainWindow", "Export Instance"));
@ -1844,6 +1853,13 @@ void MainWindow::on_actionLaunchInstance_triggered()
}
}
void MainWindow::on_actionCreateShortcut_triggered() {
if (m_selectedInstance)
{
CreateShortcutDialog(this, m_selectedInstance).exec();
}
}
void MainWindow::activateInstance(InstancePtr instance)
{
APPLICATION->launch(instance);

View File

@ -141,6 +141,8 @@ private slots:
void on_actionScreenshots_triggered();
void on_actionCreateShortcut_triggered();
void taskEnd();
/**

View File

@ -0,0 +1,248 @@
/*
* Copyright 2022 arthomnix
*
* This source is subject to the Microsoft Public License (MS-PL).
* Please see the COPYING.md file for more information.
*/
#include <QFileDialog>
#include <BuildConfig.h>
#include <QMessageBox>
#include "CreateShortcutDialog.h"
#include "ui_CreateShortcutDialog.h"
#include "Application.h"
#include "minecraft/auth/AccountList.h"
#include "minecraft/MinecraftInstance.h"
#include "minecraft/PackProfile.h"
#include "icons/IconList.h"
#ifdef Q_OS_WIN
#include <shobjidl.h>
#include <shlguid.h>
#endif
CreateShortcutDialog::CreateShortcutDialog(QWidget *parent, InstancePtr instance)
:QDialog(parent), ui(new Ui::CreateShortcutDialog), m_instance(instance)
{
ui->setupUi(this);
QStringList accountNameList;
auto accounts = APPLICATION->accounts();
for (int i = 0; i < accounts->count(); i++)
{
accountNameList.append(accounts->at(i)->profileName());
}
ui->profileComboBox->addItems(accountNameList);
if (accounts->defaultAccount())
{
ui->profileComboBox->setCurrentText(accounts->defaultAccount()->profileName());
}
// check if instance is affected by a bug causing it to crash when trying to join a server on launch
// TODO: implement this check in meta
if (m_instance->typeName() == "Minecraft")
{
try
{
MinecraftInstancePtr minecraftInstance = qobject_pointer_cast<MinecraftInstance>(m_instance);
minecraftInstance->getPackProfile()->reload(Net::Mode::Online);
QDateTime versionDate = minecraftInstance->getPackProfile()->getComponent("net.minecraft")->getReleaseDateTime();
bool enableJoinServer = (versionDate < MC_145102_START)
|| (versionDate >= MC_145102_END && versionDate < MC_228828_START)
|| (versionDate >= MC_228828_END);
ui->joinServerCheckBox->setEnabled(enableJoinServer);
ui->joinServer->setEnabled(enableJoinServer);
}
catch (const Exception &e)
{
QMessageBox::critical(this, tr("Error"), e.cause());
}
catch (...)
{
QMessageBox::critical(this, tr("Error"), tr("Failed to load pack profile to check version!"));
}
}
// Macs don't have any concept of a desktop shortcut, so force-enable the option to generate a shell script instead
#if defined(Q_OS_UNIX) && !defined(Q_OS_LINUX)
ui->createScriptCheckBox->setEnabled(false);
ui->createScriptCheckBox->setChecked(true);
#endif
updateDialogState();
}
CreateShortcutDialog::~CreateShortcutDialog()
{
delete ui;
}
void CreateShortcutDialog::on_shortcutPathBrowse_clicked()
{
QString linkExtension;
#ifdef Q_OS_UNIX
linkExtension = ui->createScriptCheckBox->isChecked() ? "sh" : "desktop";
#endif
#ifdef Q_OS_WIN
linkExtension = ui->createScriptCheckBox->isChecked() ? "bat" : "lnk";
#endif
QFileDialog fileDialog(this, tr("Select shortcut path"), QStandardPaths::writableLocation(QStandardPaths::DesktopLocation));
fileDialog.setDefaultSuffix(linkExtension);
fileDialog.setAcceptMode(QFileDialog::AcceptSave);
fileDialog.setFileMode(QFileDialog::AnyFile);
fileDialog.selectFile(m_instance->name() + " - " + BuildConfig.LAUNCHER_DISPLAYNAME + "." + linkExtension);
if (fileDialog.exec())
{
ui->shortcutPath->setText(fileDialog.selectedFiles().at(0));
}
updateDialogState();
}
void CreateShortcutDialog::accept()
{
createShortcut();
QDialog::accept();
}
void CreateShortcutDialog::updateDialogState()
{
ui->buttonBox->button(QDialogButtonBox::StandardButton::Ok)->setEnabled(
!ui->shortcutPath->text().isEmpty()
&& (!ui->joinServerCheckBox->isChecked() || !ui->joinServer->text().isEmpty())
&& (!ui->offlineUsernameCheckBox->isChecked() || !ui->offlineUsername->text().isEmpty())
&& (!ui->useProfileCheckBox->isChecked() || !ui->profileComboBox->currentText().isEmpty())
);
ui->joinServer->setEnabled(ui->joinServerCheckBox->isChecked());
ui->profileComboBox->setEnabled(ui->useProfileCheckBox->isChecked());
ui->offlineUsernameCheckBox->setEnabled(ui->launchOfflineCheckBox->isChecked());
ui->offlineUsername->setEnabled(ui->launchOfflineCheckBox->isChecked() && ui->offlineUsernameCheckBox->isChecked());
if (!ui->launchOfflineCheckBox->isChecked())
{
ui->offlineUsernameCheckBox->setChecked(false);
}
}
QString CreateShortcutDialog::getLaunchCommand()
{
return "\"" + QDir::toNativeSeparators(QCoreApplication::applicationFilePath()) + "\""
+ getLaunchArgs();
}
QString CreateShortcutDialog::getLaunchArgs()
{
return " -l " + m_instance->id()
+ (ui->joinServerCheckBox->isChecked() ? " -s " + ui->joinServer->text() : "")
+ (ui->useProfileCheckBox->isChecked() ? " -a " + ui->profileComboBox->currentText() : "")
+ (ui->launchOfflineCheckBox->isChecked() ? " -o" : "")
+ (ui->offlineUsernameCheckBox->isChecked() ? " -n " + ui->offlineUsername->text() : "");
}
void CreateShortcutDialog::createShortcut()
{
#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
{
#endif
QString shortcutText;
#ifdef Q_OS_UNIX
// Unix shell script
if (ui->createScriptCheckBox->isChecked())
{
shortcutText = "#!/bin/sh\n"
"cd \"" + QCoreApplication::applicationDirPath() + "\"\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() + "/icons/shortcut-icon.png"))
{
QPixmap iconPixmap = QIcon(":/logo.svg").pixmap(64, 64);
iconPixmap.save(QCoreApplication::applicationDirPath() + "/icons/shortcut-icon.png");
}
shortcutText = "[Desktop Entry]\n"
"Type=Application\n"
"Name=" + m_instance->name() + " - " + BuildConfig.LAUNCHER_DISPLAYNAME + "\n"
+ "Exec=" + getLaunchCommand() + "\n"
+ "Path=" + QCoreApplication::applicationDirPath() + "\n"
+ "Icon=" + QCoreApplication::applicationDirPath() + "/icons/shortcut-icon.png\n";
}
#endif
#ifdef Q_OS_WIN
// Windows batch script implementation
shortcutText = "@ECHO OFF\r\n"
"CD \"" + QDir::toNativeSeparators(QCoreApplication::applicationDirPath()) + "\"\r\n"
"START /B " + getLaunchCommand() + "\r\n";
#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
}
else
{
if (!QFileInfo::exists(QCoreApplication::applicationDirPath() + "/icons/shortcut-icon.ico"))
{
QPixmap iconPixmap = QIcon(":/logo.svg").pixmap(64, 64);
iconPixmap.save(QCoreApplication::applicationDirPath() + "/icons/shortcut-icon.ico");
}
createWindowsLink(QDir::toNativeSeparators(QCoreApplication::applicationFilePath()).toStdString().c_str(),
QDir::toNativeSeparators(QCoreApplication::applicationDirPath()).toStdString().c_str(),
getLaunchArgs().toStdString().c_str(),
ui->shortcutPath->text().toStdString().c_str(),
(m_instance->name() + " - " + BuildConfig.LAUNCHER_DISPLAYNAME).toStdString().c_str(),
QDir::toNativeSeparators(QCoreApplication::applicationDirPath() + "/icons/shortcut-icon.ico").toStdString().c_str()
);
}
#endif
}
#ifdef Q_OS_WIN
void CreateShortcutDialog::createWindowsLink(LPCSTR target, LPCSTR workingDir, LPCSTR args, LPCSTR filename,
LPCSTR desc, LPCSTR iconPath)
{
HRESULT result;
IShellLink *link;
CoInitialize(nullptr);
result = CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID *) &link);
if (SUCCEEDED(result))
{
IPersistFile *file;
link->SetPath(target);
link->SetWorkingDirectory(workingDir);
link->SetArguments(args);
link->SetDescription(desc);
link->SetIconLocation(iconPath, 0);
result = link->QueryInterface(IID_IPersistFile, (LPVOID *) &file);
if (SUCCEEDED(result))
{
WCHAR path[MAX_PATH];
MultiByteToWideChar(CP_ACP, 0, filename, -1, path, MAX_PATH);
file->Save(path, TRUE);
file->Release();
}
link->Release();
}
CoUninitialize();
}
#endif

View File

@ -0,0 +1,56 @@
/*
* Copyright 2022 arthomnix
*
* This source is subject to the Microsoft Public License (MS-PL).
* Please see the COPYING.md file for more information.
*/
#pragma once
#include <QDialog>
#include "minecraft/auth/MinecraftAccount.h"
#include "BaseInstance.h"
#include "minecraft/ParseUtils.h"
#ifdef Q_OS_WIN
#include <windows.h>
#endif
// Dates when the game was affected by bugs that crashed the game when using the option to join a server on startup
const QDateTime MC_145102_START = timeFromS3Time("2019-02-20T14:56:58+00:00");
const QDateTime MC_145102_END = timeFromS3Time("2020-05-14T08:16:26+00:00");
const QDateTime MC_228828_START = timeFromS3Time("2021-03-10T15:24:38+00:00");
const QDateTime MC_228828_END = timeFromS3Time("2021-06-18T12:24:40+00:00");
namespace Ui
{
class CreateShortcutDialog;
}
class CreateShortcutDialog : public QDialog
{
Q_OBJECT
public:
explicit CreateShortcutDialog(QWidget *parent = nullptr, InstancePtr instance = nullptr);
~CreateShortcutDialog() override;
private
slots:
void on_shortcutPathBrowse_clicked();
void updateDialogState();
void accept() override;
private:
Ui::CreateShortcutDialog *ui;
InstancePtr m_instance;
QString getLaunchCommand();
QString getLaunchArgs();
void createShortcut();
#ifdef Q_OS_WIN
void createWindowsLink(LPCSTR target, LPCSTR workingDir, LPCSTR args, LPCSTR filename, LPCSTR desc, LPCSTR iconPath);
#endif
};

View File

@ -0,0 +1,288 @@
<?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">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>796</width>
<height>232</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>796</width>
<height>230</height>
</size>
</property>
<property name="windowTitle">
<string>Create Shortcut</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QGridLayout" name="gridLayout">
<property name="sizeConstraint">
<enum>QLayout::SetDefaultConstraint</enum>
</property>
<item row="3" column="0">
<widget class="QCheckBox" name="launchOfflineCheckBox">
<property name="text">
<string>Launch in offline mode</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="shortcutPathLabel">
<property name="text">
<string>Shortcut path:</string>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QPushButton" name="shortcutPathBrowse">
<property name="text">
<string>Browse</string>
</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">
<string>Use specific profile:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="joinServer"/>
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
</spacer>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="bottomMargin">
<number>0</number>
</property>
<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>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>CreateShortcutDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>397</x>
<y>207</y>
</hint>
<hint type="destinationlabel">
<x>397</x>
<y>114</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>CreateShortcutDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>397</x>
<y>207</y>
</hint>
<hint type="destinationlabel">
<x>397</x>
<y>114</y>
</hint>
</hints>
</connection>
<connection>
<sender>joinServer</sender>
<signal>textChanged(QString)</signal>
<receiver>CreateShortcutDialog</receiver>
<slot>updateDialogState()</slot>
<hints>
<hint type="sourcelabel">
<x>471</x>
<y>61</y>
</hint>
<hint type="destinationlabel">
<x>397</x>
<y>114</y>
</hint>
</hints>
</connection>
<connection>
<sender>joinServerCheckBox</sender>
<signal>stateChanged(int)</signal>
<receiver>CreateShortcutDialog</receiver>
<slot>updateDialogState()</slot>
<hints>
<hint type="sourcelabel">
<x>122</x>
<y>61</y>
</hint>
<hint type="destinationlabel">
<x>397</x>
<y>114</y>
</hint>
</hints>
</connection>
<connection>
<sender>launchOfflineCheckBox</sender>
<signal>stateChanged(int)</signal>
<receiver>CreateShortcutDialog</receiver>
<slot>updateDialogState()</slot>
<hints>
<hint type="sourcelabel">
<x>122</x>
<y>130</y>
</hint>
<hint type="destinationlabel">
<x>397</x>
<y>114</y>
</hint>
</hints>
</connection>
<connection>
<sender>offlineUsername</sender>
<signal>textChanged(QString)</signal>
<receiver>CreateShortcutDialog</receiver>
<slot>updateDialogState()</slot>
<hints>
<hint type="sourcelabel">
<x>471</x>
<y>162</y>
</hint>
<hint type="destinationlabel">
<x>397</x>
<y>114</y>
</hint>
</hints>
</connection>
<connection>
<sender>offlineUsernameCheckBox</sender>
<signal>stateChanged(int)</signal>
<receiver>CreateShortcutDialog</receiver>
<slot>updateDialogState()</slot>
<hints>
<hint type="sourcelabel">
<x>122</x>
<y>162</y>
</hint>
<hint type="destinationlabel">
<x>397</x>
<y>114</y>
</hint>
</hints>
</connection>
<connection>
<sender>profileComboBox</sender>
<signal>currentTextChanged(QString)</signal>
<receiver>CreateShortcutDialog</receiver>
<slot>updateDialogState()</slot>
<hints>
<hint type="sourcelabel">
<x>471</x>
<y>98</y>
</hint>
<hint type="destinationlabel">
<x>397</x>
<y>114</y>
</hint>
</hints>
</connection>
<connection>
<sender>shortcutPath</sender>
<signal>textChanged(QString)</signal>
<receiver>CreateShortcutDialog</receiver>
<slot>updateDialogState()</slot>
<hints>
<hint type="sourcelabel">
<x>471</x>
<y>23</y>
</hint>
<hint type="destinationlabel">
<x>397</x>
<y>114</y>
</hint>
</hints>
</connection>
<connection>
<sender>useProfileCheckBox</sender>
<signal>stateChanged(int)</signal>
<receiver>CreateShortcutDialog</receiver>
<slot>updateDialogState()</slot>
<hints>
<hint type="sourcelabel">
<x>122</x>
<y>98</y>
</hint>
<hint type="destinationlabel">
<x>397</x>
<y>114</y>
</hint>
</hints>
</connection>
</connections>
</ui>