initial support for add to PATH action

Signed-off-by: Ryan Cao <70191398+ryanccn@users.noreply.github.com>
This commit is contained in:
Ryan Cao 2022-10-19 21:53:57 +08:00
parent 2999afe781
commit 7b1e68bfa8
No known key found for this signature in database
4 changed files with 49 additions and 0 deletions

View File

@ -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;

View File

@ -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\
*/

View File

@ -61,6 +61,7 @@
#include <QMenu>
#include <QMenuBar>
#include <QMessageBox>
#include <QFileDialog>
#include <QInputDialog>
#include <QLabel>
#include <QToolButton>
@ -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("")));

View File

@ -128,6 +128,8 @@ private slots:
void on_actionClearMetadata_triggered();
void on_actionAddToPATH_triggered();
void on_actionOpenWiki_triggered();
void on_actionMoreNews_triggered();