From 7b1e68bfa8cf9d46e0c810cfa0f077427cfc707a Mon Sep 17 00:00:00 2001 From: Ryan Cao <70191398+ryanccn@users.noreply.github.com> Date: Wed, 19 Oct 2022 21:53:57 +0800 Subject: [PATCH] initial support for add to PATH action Signed-off-by: Ryan Cao <70191398+ryanccn@users.noreply.github.com> --- launcher/FileSystem.cpp | 13 +++++++++++++ launcher/FileSystem.h | 5 +++++ launcher/ui/MainWindow.cpp | 29 +++++++++++++++++++++++++++++ launcher/ui/MainWindow.h | 2 ++ 4 files changed, 49 insertions(+) diff --git a/launcher/FileSystem.cpp b/launcher/FileSystem.cpp index 4026d6c1..13bb95d9 100644 --- a/launcher/FileSystem.cpp +++ b/launcher/FileSystem.cpp @@ -163,6 +163,19 @@ bool ensureFolderPathExists(QString foldernamepath) return success; } +bool symlink(const QString& source, const QString& target) +{ + std::error_code err; + + fs::create_symlink(toStdString(source), toStdString(target)); + + if (err) { + qWarning() << "Failed to symlink files:" << QString::fromStdString(err.message()); + } + + return err.value() == 0; +} + bool copy::operator()(const QString& offset) { using copy_opts = fs::copy_options; diff --git a/launcher/FileSystem.h b/launcher/FileSystem.h index b46f3281..2d4dfb56 100644 --- a/launcher/FileSystem.h +++ b/launcher/FileSystem.h @@ -53,6 +53,11 @@ class FileSystemException : public ::Exception { */ void write(const QString& filename, const QByteArray& data); +/** + * create a symlink + */ +bool symlink(const QString& target, const QString& link); + /** * read data from a file safely\ */ diff --git a/launcher/ui/MainWindow.cpp b/launcher/ui/MainWindow.cpp index afbc505e..80d212e9 100644 --- a/launcher/ui/MainWindow.cpp +++ b/launcher/ui/MainWindow.cpp @@ -61,6 +61,7 @@ #include #include #include +#include #include #include #include @@ -253,6 +254,7 @@ public: QMenu * helpMenu = nullptr; TranslatedToolButton helpMenuButton; TranslatedAction actionClearMetadata; + TranslatedAction actionAddToPATH; TranslatedAction actionReportBug; TranslatedAction actionDISCORD; TranslatedAction actionMATRIX; @@ -348,6 +350,14 @@ public: actionClearMetadata.setTooltipId(QT_TRANSLATE_NOOP("MainWindow", "Clear cached metadata")); all_actions.append(&actionClearMetadata); + #ifdef Q_OS_MAC + actionAddToPATH = TranslatedAction(MainWindow); + actionAddToPATH->setObjectName(QStringLiteral("actionAddToPATH")); + actionAddToPATH.setTextId(QT_TRANSLATE_NOOP("MainWindow", "Add to &PATH")); + actionAddToPATH.setTooltipId(QT_TRANSLATE_NOOP("MainWindow", "Add the prism binary to PATH.")); + all_actions.append(&actionAddToPATH); + #endif + if (!BuildConfig.BUG_TRACKER_URL.isEmpty()) { actionReportBug = TranslatedAction(MainWindow); actionReportBug->setObjectName(QStringLiteral("actionReportBug")); @@ -448,6 +458,10 @@ public: helpMenu->addAction(actionClearMetadata); + #ifdef Q_OS_MAC + helpMenu->addAction(actionAddToPATH); + #endif + if (!BuildConfig.BUG_TRACKER_URL.isEmpty()) { helpMenu->addAction(actionReportBug); } @@ -533,6 +547,9 @@ public: helpMenu = menuBar->addMenu(tr("&Help")); helpMenu->setSeparatorsCollapsible(false); helpMenu->addAction(actionClearMetadata); + #ifdef Q_OS_MAC + helpMenu->addAction(actionAddToPATH); + #endif helpMenu->addSeparator(); helpMenu->addAction(actionAbout); helpMenu->addAction(actionOpenWiki); @@ -1902,6 +1919,18 @@ void MainWindow::on_actionClearMetadata_triggered() APPLICATION->metacache()->evictAll(); } +void MainWindow::on_actionAddToPATH_triggered() { + auto binaryPath = APPLICATION->arguments().first(); + + auto outcome = FS::symlink(binaryPath, "/usr/local/bin/prism"); + + if (!outcome) { + QMessageBox::critical(this, tr("Failed to add Prism to PATH"), tr("")); + } else { + QMessageBox::information(this, tr("Added Prism to PATH"), tr("Prism was successfully added to your PATH.")); + } +} + void MainWindow::on_actionOpenWiki_triggered() { DesktopServices::openUrl(QUrl(BuildConfig.HELP_URL.arg(""))); diff --git a/launcher/ui/MainWindow.h b/launcher/ui/MainWindow.h index cb8cb4aa..097b0983 100644 --- a/launcher/ui/MainWindow.h +++ b/launcher/ui/MainWindow.h @@ -128,6 +128,8 @@ private slots: void on_actionClearMetadata_triggered(); + void on_actionAddToPATH_triggered(); + void on_actionOpenWiki_triggered(); void on_actionMoreNews_triggered();