NOISSUE WIP implementation of the ability to create instance shortcuts

Currently Linux-only and lacking some features
This commit is contained in:
arthomnix 2022-07-03 07:54:20 +01:00
parent bf80bd1143
commit 5d14dede50
6 changed files with 464 additions and 4 deletions

View File

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

View File

@ -83,14 +83,15 @@
#include "ui/dialogs/UpdateDialog.h" #include "ui/dialogs/UpdateDialog.h"
#include "ui/dialogs/EditAccountDialog.h" #include "ui/dialogs/EditAccountDialog.h"
#include "ui/dialogs/NotificationDialog.h" #include "ui/dialogs/NotificationDialog.h"
#include "ui/dialogs/CreateShortcutDialog.h"
#include "ui/dialogs/ExportInstanceDialog.h" #include "ui/dialogs/ExportInstanceDialog.h"
#include "UpdateController.h" #include "UpdateController.h"
#include "KonamiCode.h" #include "KonamiCode.h"
#include "InstanceImportTask.h" #include "InstanceImportTask.h"
#include "InstanceCopyTask.h"
#include "InstanceCopyTask.h"
#include "MMCTime.h" #include "MMCTime.h"
namespace { namespace {
@ -222,6 +223,9 @@ 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;
#endif
QVector<TranslatedAction *> all_actions; QVector<TranslatedAction *> all_actions;
LabeledToolButton *renameButton = nullptr; LabeledToolButton *renameButton = nullptr;
@ -594,6 +598,15 @@ public:
instanceToolBar->addSeparator(); 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 = TranslatedAction(MainWindow);
actionExportInstance->setObjectName(QStringLiteral("actionExportInstance")); actionExportInstance->setObjectName(QStringLiteral("actionExportInstance"));
actionExportInstance.setTextId(QT_TRANSLATE_NOOP("MainWindow", "Export Instance")); actionExportInstance.setTextId(QT_TRANSLATE_NOOP("MainWindow", "Export Instance"));
@ -1844,6 +1857,15 @@ 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) void MainWindow::activateInstance(InstancePtr instance)
{ {
APPLICATION->launch(instance); APPLICATION->launch(instance);

View File

@ -141,6 +141,10 @@ private slots:
void on_actionScreenshots_triggered(); void on_actionScreenshots_triggered();
#if defined(Q_OS_LINUX)
void on_actionCreateShortcut_triggered();
#endif
void taskEnd(); void taskEnd();
/** /**

View File

@ -0,0 +1,117 @@
/*
* Copyright 2022 arthomnix
*
* This source is subject to the Microsoft Public License (MS-PL).
* Please see the COPYING.md file for more information.
*/
#include <fstream>
#include <iostream>
#include <QFileDialog>
#include <BuildConfig.h>
#include "CreateShortcutDialog.h"
#include "ui_CreateShortcutDialog.h"
#include "Application.h"
#include "minecraft/auth/AccountList.h"
#include "icons/IconList.h"
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());
}
updateDialogState();
}
CreateShortcutDialog::~CreateShortcutDialog()
{
delete ui;
}
void CreateShortcutDialog::on_shortcutPathBrowse_clicked()
{
QString linkExtension;
#ifdef Q_OS_LINUX
linkExtension = "desktop";
#endif
#ifdef Q_OS_WIN
linkExtension = "lnk";
#endif
QFileDialog fileDialog(this, tr("Select shortcut path"), QStandardPaths::writableLocation(QStandardPaths::DesktopLocation));
fileDialog.setDefaultSuffix(linkExtension);
fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
fileDialog.setFileMode(QFileDialog::AnyFile);
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());
}
QString CreateShortcutDialog::getLaunchCommand()
{
return QCoreApplication::applicationFilePath()
+ " -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()
{
// Linux implementation using .desktop file
#ifdef Q_OS_LINUX
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;
desktopFile.setPermissions(QFile::ReadOwner | QFile::ReadGroup | QFile::ReadOther
| QFile::WriteOwner | QFile::ExeOwner | QFile::ExeGroup);
desktopFile.close();
}
#endif
// TODO: implementations for other operating systems
}

View File

@ -0,0 +1,40 @@
/*
* 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"
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();
void createShortcut();
};

View File

@ -0,0 +1,275 @@
<?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>230</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="1" column="0">
<widget class="QCheckBox" name="joinServerCheckBox">
<property name="text">
<string>Join server on launch:</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="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">
<string>Browse</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="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>
</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>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</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>