From d3a03837bad99f1a58062231c3c42df3e9fb1f30 Mon Sep 17 00:00:00 2001 From: xSlendiX Date: Fri, 25 Nov 2022 17:15:26 +0200 Subject: [PATCH] Add "Create desktop shortcut" feature. This patch adds a way of adding a shortcut to directly start an instance. How this works is it uses the already existing "-l" command line option. This, however, has not been implemented for Mac, which is something that can definetly be improved in the future if anyone wants to work on it :^) Signed-off-by: xSlendiX --- launcher/ui/MainWindow.cpp | 76 ++++++++++++++++++++++++++++++++++++++ launcher/ui/MainWindow.h | 3 ++ 2 files changed, 79 insertions(+) diff --git a/launcher/ui/MainWindow.cpp b/launcher/ui/MainWindow.cpp index 75b72baf68..768ced42dc 100644 --- a/launcher/ui/MainWindow.cpp +++ b/launcher/ui/MainWindow.cpp @@ -115,6 +115,41 @@ #include "MMCTime.h" +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) +// For .lnk creation +#include +#include +#include +#include +#include +#include +#include +#include + +// https://stackoverflow.com/a/63443879 +HRESULT createLink(LPCWSTR target, LPCWSTR shortcut_path, LPCWSTR description, LPCWSTR arguments) { + HRESULT hres; + IShellLinkW* psl; + + hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&psl)); + if (SUCCEEDED(hres)) { + IPersistFile* ppf; + psl->SetPath(target); + psl->SetArguments(arguments); + psl->SetDescription(description); + + hres = psl->QueryInterface(IID_PPV_ARGS(&ppf)); + if (SUCCEEDED(hres)) { + hres = ppf->Save(shortcut_path, TRUE); + ppf->Release(); + } + psl->Release(); + } + return hres; +} + +#endif + namespace { QString profileInUseFilter(const QString & profile, bool used) { @@ -230,6 +265,7 @@ class MainWindow::Ui TranslatedAction actionRenameInstance; TranslatedAction actionChangeInstGroup; TranslatedAction actionChangeInstIcon; + TranslatedAction actionCreateShortcut; TranslatedAction actionEditInstNotes; TranslatedAction actionEditInstance; TranslatedAction actionWorlds; @@ -723,6 +759,15 @@ class MainWindow::Ui actionChangeInstGroup.setTooltipId(QT_TRANSLATE_NOOP("MainWindow", "Change the selected instance's group.")); actionChangeInstGroup->setShortcut(QKeySequence(tr("Ctrl+G"))); all_actions.append(&actionChangeInstGroup); + + // FIXME: Add a way to create shortcuts on Mac. +#ifndef __APPLE__ + actionCreateShortcut = TranslatedAction(MainWindow); + actionCreateShortcut->setObjectName(QStringLiteral("actionCreateShortcut")); + actionCreateShortcut.setTextId(QT_TRANSLATE_NOOP("MainWindow", "Create desktop shortcut")); + actionCreateShortcut.setTooltipId(QT_TRANSLATE_NOOP("MainWindow", "Create a desktop shortcut to launch the instance.")); + all_actions.append(&actionCreateShortcut); +#endif actionViewSelectedMCFolder = TranslatedAction(MainWindow); actionViewSelectedMCFolder->setObjectName(QStringLiteral("actionViewSelectedMCFolder")); @@ -803,6 +848,7 @@ class MainWindow::Ui instanceToolBar->addAction(actionWorlds); instanceToolBar->addAction(actionScreenshots); instanceToolBar->addAction(actionChangeInstGroup); + instanceToolBar->addAction(actionCreateShortcut); instanceToolBar->addSeparator(); @@ -1861,6 +1907,36 @@ void MainWindow::on_actionChangeInstGroup_triggered() } } +void MainWindow::on_actionCreateShortcut_triggered() +{ + if (!m_selectedInstance) + return; + + auto desktop = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation); + auto executable_path = APPLICATION->applicationFilePath(); + auto instId = m_selectedInstance->id(); + auto icon = APPLICATION->windowIcon(); + QString name(APPLICATION->instances()->getInstanceGroup(instId)); +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) + createLink( + QString("%1\\%2.lnk").arg(desktop, name).toLocal8Bit().data(), + executable_path.toLocal8Bit().data(), + QString("Launch instance %1").arg(name).toLocal8Bit().data(), + QString("-l %2").arg(instId).toLocal8Bit().data() + ); +#else + QFile shortcut_file(QString("%1/%2.desktop").arg(desktop, instId)); + if (shortcut_file.open(QFile::WriteOnly | QFile::Truncate)) { + QTextStream out(&shortcut_file); + out << "[Desktop Entry]" << endl + << "Type=Application" << endl + << "Exec=" << executable_path << " -l " << instId << " %U" << endl + << "Terminal=false" << endl; + } + shortcut_file.close(); +#endif +} + void MainWindow::deleteGroup() { QObject* obj = sender(); diff --git a/launcher/ui/MainWindow.h b/launcher/ui/MainWindow.h index 858c54770e..636996d7d8 100644 --- a/launcher/ui/MainWindow.h +++ b/launcher/ui/MainWindow.h @@ -105,6 +105,9 @@ private slots: void on_actionChangeInstGroup_triggered(); void on_actionChangeInstIcon_triggered(); + + void on_actionCreateShortcut_triggered(); + void on_changeIconButton_clicked(bool) { on_actionChangeInstIcon_triggered();