Skip to content

Commit

Permalink
Add "Create desktop shortcut" feature.
Browse files Browse the repository at this point in the history
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 <slendi@socopon.com>
  • Loading branch information
xslendix committed Nov 26, 2022
1 parent 211c423 commit 5c00c96
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
70 changes: 70 additions & 0 deletions launcher/ui/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,35 @@

#include "MMCTime.h"

#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
// For .lnk creation
#include <windows.h>
#include <shobjidl.h>

// 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)
{
Expand Down Expand Up @@ -230,6 +259,7 @@ class MainWindow::Ui
TranslatedAction actionRenameInstance;
TranslatedAction actionChangeInstGroup;
TranslatedAction actionChangeInstIcon;
TranslatedAction actionCreateShortcut;
TranslatedAction actionEditInstNotes;
TranslatedAction actionEditInstance;
TranslatedAction actionWorlds;
Expand Down Expand Up @@ -723,6 +753,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"));
Expand Down Expand Up @@ -803,6 +842,7 @@ class MainWindow::Ui
instanceToolBar->addAction(actionWorlds);
instanceToolBar->addAction(actionScreenshots);
instanceToolBar->addAction(actionChangeInstGroup);
instanceToolBar->addAction(actionCreateShortcut);

instanceToolBar->addSeparator();

Expand Down Expand Up @@ -1861,6 +1901,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(
const_cast<wchar_t *>(executable_path.toStdWString().c_str()),
const_cast<wchar_t *>(QString("%1\\Launch %2.lnk").arg(desktop, name).toStdWString().c_str()),
const_cast<wchar_t *>(QString("Launch instance %1").arg(name).toStdWString().c_str()),
const_cast<wchar_t *>(QString("-l %2").arg(instId).toStdWString().c_str())
);
#else
QFile shortcut_file(QString("%1/Launch %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();
Expand Down
3 changes: 3 additions & 0 deletions launcher/ui/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 5c00c96

Please sign in to comment.