diff --git a/application/GuiUtil.cpp b/application/GuiUtil.cpp index cc06d7116..f10095c14 100644 --- a/application/GuiUtil.cpp +++ b/application/GuiUtil.cpp @@ -1,7 +1,6 @@ #include "GuiUtil.h" #include -#include #include #include @@ -11,6 +10,7 @@ #include "MultiMC.h" #include +#include #include void GuiUtil::uploadPaste(const QString &text, QWidget *parentWidget) @@ -42,7 +42,7 @@ void GuiUtil::uploadPaste(const QString &text, QWidget *parentWidget) { const QString link = paste->pasteLink(); setClipboardText(link); - QDesktopServices::openUrl(link); + DesktopServices::openUrl(link); CustomMessageBox::selectable( parentWidget, QObject::tr("Upload finished"), QObject::tr("The link to the uploaded log has been opened in " diff --git a/application/MainWindow.cpp b/application/MainWindow.cpp index fa7566787..c977715be 100644 --- a/application/MainWindow.cpp +++ b/application/MainWindow.cpp @@ -26,7 +26,6 @@ #include #include -#include #include #include @@ -69,6 +68,7 @@ #include #include #include +#include #include "InstancePageProvider.h" #include "InstanceProxyModel.h" @@ -1197,7 +1197,7 @@ void MainWindow::on_actionAddInstance_triggered() void MainWindow::on_actionREDDIT_triggered() { - openWebPage(QUrl("https://www.reddit.com/r/MultiMC/")); + DesktopServices::openUrl(QUrl("https://www.reddit.com/r/MultiMC/")); } void MainWindow::on_actionCopyInstance_triggered() @@ -1312,7 +1312,7 @@ void MainWindow::on_actionChangeInstGroup_triggered() void MainWindow::on_actionViewInstanceFolder_triggered() { QString str = MMC->settings()->get("InstanceDir").toString(); - FS::openDirInDefaultProgram(str); + DesktopServices::openDirectory(str); } void MainWindow::on_actionRefresh_triggered() @@ -1322,7 +1322,7 @@ void MainWindow::on_actionRefresh_triggered() void MainWindow::on_actionViewCentralModsFolder_triggered() { - FS::openDirInDefaultProgram(MMC->settings()->get("CentralModsDir").toString(), true); + DesktopServices::openDirectory(MMC->settings()->get("CentralModsDir").toString(), true); } void MainWindow::on_actionConfig_Folder_triggered() @@ -1330,7 +1330,7 @@ void MainWindow::on_actionConfig_Folder_triggered() if (m_selectedInstance) { QString str = m_selectedInstance->instanceConfigFolder(); - FS::openDirInDefaultProgram(QDir(str).absolutePath()); + DesktopServices::openDirectory(QDir(str).absolutePath()); } } @@ -1384,26 +1384,30 @@ void MainWindow::on_actionManageAccounts_triggered() void MainWindow::on_actionReportBug_triggered() { - openWebPage(QUrl("https://github.com/MultiMC/MultiMC5/issues")); + DesktopServices::openUrl(QUrl("https://github.com/MultiMC/MultiMC5/issues")); } void MainWindow::on_actionPatreon_triggered() { - openWebPage(QUrl("http://www.patreon.com/multimc")); + DesktopServices::openUrl(QUrl("http://www.patreon.com/multimc")); } void MainWindow::on_actionMoreNews_triggered() { - openWebPage(QUrl("http://multimc.org/posts.html")); + DesktopServices::openUrl(QUrl("http://multimc.org/posts.html")); } void MainWindow::newsButtonClicked() { QList entries = m_newsChecker->getNewsEntries(); if (entries.count() > 0) - openWebPage(QUrl(entries[0]->link)); + { + DesktopServices::openUrl(QUrl(entries[0]->link)); + } else - openWebPage(QUrl("http://multimc.org/posts.html")); + { + DesktopServices::openUrl(QUrl("http://multimc.org/posts.html")); + } } void MainWindow::on_actionAbout_triggered() @@ -1469,7 +1473,7 @@ void MainWindow::on_actionViewSelectedInstFolder_triggered() if (m_selectedInstance) { QString str = m_selectedInstance->instanceRoot(); - FS::openDirInDefaultProgram(QDir(str).absolutePath()); + DesktopServices::openDirectory(QDir(str).absolutePath()); } } @@ -1538,12 +1542,6 @@ void MainWindow::startTask(Task *task) task->start(); } -// BrowserDialog -void MainWindow::openWebPage(QUrl url) -{ - QDesktopServices::openUrl(url); -} - void MainWindow::instanceChanged(const QModelIndex ¤t, const QModelIndex &previous) { if (!current.isValid()) diff --git a/application/MainWindow.h b/application/MainWindow.h index 0685c9f80..0a6167bc9 100644 --- a/application/MainWindow.h +++ b/application/MainWindow.h @@ -166,9 +166,6 @@ private: void finalizeInstance(InstancePtr inst); void launch(InstancePtr instance, bool online = true, BaseProfilerFactory *profiler = nullptr); - /// open a web page in the default browser - void openWebPage(QUrl url); - private: std::unique_ptr ui; diff --git a/application/MultiMC.cpp b/application/MultiMC.cpp index a2a145062..fb82510d8 100644 --- a/application/MultiMC.cpp +++ b/application/MultiMC.cpp @@ -17,7 +17,6 @@ #include #include #include -#include #include #include "InstanceList.h" @@ -54,6 +53,7 @@ #include #include +#include using namespace Commandline; @@ -932,12 +932,12 @@ bool MultiMC::openJsonEditor(const QString &filename) const QString file = QDir::current().absoluteFilePath(filename); if (m_settings->get("JsonEditor").toString().isEmpty()) { - return QDesktopServices::openUrl(QUrl::fromLocalFile(file)); + return DesktopServices::openUrl(QUrl::fromLocalFile(file)); } else { - return QProcess::startDetached(m_settings->get("JsonEditor").toString(), QStringList() - << file); + //return DesktopServices::openFile(m_settings->get("JsonEditor").toString(), file); + return DesktopServices::run(m_settings->get("JsonEditor").toString(), {}); } } diff --git a/application/dialogs/EditAccountDialog.cpp b/application/dialogs/EditAccountDialog.cpp index 7c2ff981e..f7e25f6f7 100644 --- a/application/dialogs/EditAccountDialog.cpp +++ b/application/dialogs/EditAccountDialog.cpp @@ -15,7 +15,7 @@ #include "EditAccountDialog.h" #include "ui_EditAccountDialog.h" -#include +#include #include EditAccountDialog::EditAccountDialog(const QString &text, QWidget *parent, int flags) @@ -37,7 +37,7 @@ EditAccountDialog::~EditAccountDialog() void EditAccountDialog::on_label_linkActivated(const QString &link) { - QDesktopServices::openUrl(QUrl(link)); + DesktopServices::openUrl(QUrl(link)); } void EditAccountDialog::setUsername(const QString & user) const diff --git a/application/dialogs/ModEditDialogCommon.cpp b/application/dialogs/ModEditDialogCommon.cpp index 4a2115d8c..4201bdad4 100644 --- a/application/dialogs/ModEditDialogCommon.cpp +++ b/application/dialogs/ModEditDialogCommon.cpp @@ -28,7 +28,7 @@ void showWebsiteForMod(QWidget *parentDlg, Mod &m) { url = "http://" + url; } - QDesktopServices::openUrl(url); + DesktopServices::openUrl(url); } else { diff --git a/application/dialogs/ModEditDialogCommon.h b/application/dialogs/ModEditDialogCommon.h index e2afb98a6..fc5e3c2b3 100644 --- a/application/dialogs/ModEditDialogCommon.h +++ b/application/dialogs/ModEditDialogCommon.h @@ -1,6 +1,6 @@ #pragma once #include -#include +#include #include #include diff --git a/application/pages/LegacyJarModPage.cpp b/application/pages/LegacyJarModPage.cpp index 2ff6b6de7..b329ba70f 100644 --- a/application/pages/LegacyJarModPage.cpp +++ b/application/pages/LegacyJarModPage.cpp @@ -25,7 +25,7 @@ #include "minecraft/ModList.h" #include "minecraft/LegacyInstance.h" #include "Env.h" -#include +#include #include "MultiMC.h" #include @@ -146,7 +146,7 @@ void LegacyJarModPage::on_rmJarBtn_clicked() void LegacyJarModPage::on_viewJarBtn_clicked() { - FS::openDirInDefaultProgram(m_inst->jarModsDir(), true); + DesktopServices::openDirectory(m_inst->jarModsDir(), true); } void LegacyJarModPage::jarCurrent(QModelIndex current, QModelIndex previous) diff --git a/application/pages/ModFolderPage.cpp b/application/pages/ModFolderPage.cpp index 6791a59ed..3c68b23ff 100644 --- a/application/pages/ModFolderPage.cpp +++ b/application/pages/ModFolderPage.cpp @@ -19,7 +19,6 @@ #include #include #include -#include #include #include "MultiMC.h" @@ -29,7 +28,7 @@ #include "minecraft/ModList.h" #include "minecraft/Mod.h" #include "minecraft/VersionFilterData.h" -#include +#include ModFolderPage::ModFolderPage(BaseInstance *inst, std::shared_ptr mods, QString id, QString iconName, QString displayName, QString helpPage, @@ -162,7 +161,7 @@ void ModFolderPage::on_rmModBtn_clicked() void ModFolderPage::on_viewModBtn_clicked() { - FS::openDirInDefaultProgram(m_mods->dir().absolutePath(), true); + DesktopServices::openDirectory(m_mods->dir().absolutePath(), true); } void ModFolderPage::modCurrent(const QModelIndex ¤t, const QModelIndex &previous) diff --git a/application/pages/ScreenshotsPage.cpp b/application/pages/ScreenshotsPage.cpp index 2b2fe31f5..5b406f3dc 100644 --- a/application/pages/ScreenshotsPage.cpp +++ b/application/pages/ScreenshotsPage.cpp @@ -12,7 +12,6 @@ #include #include #include -#include #include #include @@ -26,6 +25,7 @@ #include "RWStorage.h" #include +#include typedef RWStorage SharedIconCache; typedef std::shared_ptr SharedIconCachePtr; @@ -271,12 +271,12 @@ void ScreenshotsPage::onItemActivated(QModelIndex index) return; auto info = m_model->fileInfo(index); QString fileName = info.absoluteFilePath(); - FS::openFileInDefaultProgram(info.absoluteFilePath()); + DesktopServices::openFile(info.absoluteFilePath()); } void ScreenshotsPage::on_viewFolderBtn_clicked() { - FS::openDirInDefaultProgram(m_folder, true); + DesktopServices::openDirectory(m_folder, true); } void ScreenshotsPage::on_uploadBtn_clicked() @@ -312,7 +312,7 @@ void ScreenshotsPage::on_uploadBtn_clicked() auto link = QString("https://imgur.com/a/%1").arg(imgurAlbum->id()); QClipboard *clipboard = QApplication::clipboard(); clipboard->setText(link); - QDesktopServices::openUrl(link); + DesktopServices::openUrl(link); CustomMessageBox::selectable( this, tr("Upload finished"), tr("The link to the uploaded album has been opened in the " diff --git a/application/pages/WorldListPage.cpp b/application/pages/WorldListPage.cpp index 4e7eeedc0..befb6b5e8 100644 --- a/application/pages/WorldListPage.cpp +++ b/application/pages/WorldListPage.cpp @@ -16,7 +16,7 @@ #include "WorldListPage.h" #include "ui_WorldListPage.h" #include "minecraft/WorldList.h" -#include +#include #include "dialogs/ModEditDialogCommon.h" #include #include @@ -123,7 +123,7 @@ void WorldListPage::on_rmWorldBtn_clicked() void WorldListPage::on_viewFolderBtn_clicked() { - FS::openDirInDefaultProgram(m_worlds->dir().absolutePath(), true); + DesktopServices::openDirectory(m_worlds->dir().absolutePath(), true); } QModelIndex WorldListPage::getSelectedWorld() @@ -193,7 +193,8 @@ void WorldListPage::on_mcEditBtn_clicked() if(program.size()) { qint64 pid = 0; - QProcess::startDetached(program, QStringList() << fullPath, mceditPath, &pid); + + DesktopServices::openFile(program, fullPath, mceditPath, &pid); if(pid == 0) { QMessageBox::warning(this->parentWidget(), tr("MCEdit failed to start!"), tr("MCEdit failed to start.\nIt may be necessary to reinstall it.")); diff --git a/application/widgets/PageContainer.cpp b/application/widgets/PageContainer.cpp index 66d94e6c3..90aad26e1 100644 --- a/application/widgets/PageContainer.cpp +++ b/application/widgets/PageContainer.cpp @@ -25,13 +25,13 @@ #include #include #include -#include #include "MultiMC.h" #include "settings/SettingsObject.h" #include "widgets/IconLabel.h" #include "PageContainer_p.h" #include +#include class PageEntryFilterModel : public QSortFilterProxyModel { @@ -195,7 +195,7 @@ void PageContainer::help() QString pageId = m_currentPage->helpPage(); if (pageId.isEmpty()) return; - QDesktopServices::openUrl(QUrl("https://github.com/MultiMC/MultiMC5/wiki/" + pageId)); + DesktopServices::openUrl(QUrl("https://github.com/MultiMC/MultiMC5/wiki/" + pageId)); } } diff --git a/application/widgets/ServerStatus.cpp b/application/widgets/ServerStatus.cpp index f0a7f97a1..4602dde5c 100644 --- a/application/widgets/ServerStatus.cpp +++ b/application/widgets/ServerStatus.cpp @@ -2,6 +2,7 @@ #include "LineSeparator.h" #include "IconLabel.h" #include "status/StatusChecker.h" +#include #include "MultiMC.h" @@ -11,7 +12,6 @@ #include #include #include -#include class ClickableLabel : public QLabel { @@ -127,7 +127,7 @@ void ServerStatus::addStatus(QString key, QString name) void ServerStatus::clicked() { - QDesktopServices::openUrl(QUrl("https://help.mojang.com/")); + DesktopServices::openUrl(QUrl("https://help.mojang.com/")); } void ServerStatus::setStatus(QString key, int value) diff --git a/logic/CMakeLists.txt b/logic/CMakeLists.txt index 0398cffe0..9a87dcabb 100644 --- a/logic/CMakeLists.txt +++ b/logic/CMakeLists.txt @@ -32,8 +32,12 @@ set(LOGIC_SOURCES # JSON parsing helpers Json.h Json.cpp + FileSystem.h FileSystem.cpp + DesktopServices.h + DesktopServices.cpp + Exception.h # RW lock protected map diff --git a/logic/DesktopServices.cpp b/logic/DesktopServices.cpp new file mode 100644 index 000000000..3154ea013 --- /dev/null +++ b/logic/DesktopServices.cpp @@ -0,0 +1,149 @@ +#include "DesktopServices.h" +#include +#include +#include +#include + +/** + * This shouldn't exist, but until QTBUG-9328 and other unreported bugs are fixed, it needs to be a thing. + */ +#if defined(Q_OS_LINUX) + +#include +#include +#include +#include + +template +bool IndirectOpen(T callable, qint64 *pid_forked = nullptr) +{ + auto pid = fork(); + if(pid_forked) + { + if(pid > 0) + *pid_forked = pid; + else + *pid_forked = 0; + } + if(pid == -1) + { + qWarning() << "IndirectOpen failed to fork: " << errno; + return false; + } + // child - do the stuff + if(pid == 0) + { + // unset all this garbage so it doesn't get passed to the child process + qunsetenv("LD_PRELOAD"); + qunsetenv("LD_LIBRARY_PATH"); + qunsetenv("LD_DEBUG"); + qunsetenv("QT_PLUGIN_PATH"); + qunsetenv("QT_FONTPATH"); + + // open the URL + auto status = callable(); + + // detach from the parent process group. + setsid(); + + // die. now. do not clean up anything, it would just hang forever. + _exit(status ? 0 : 1); + } + else + { + //parent - assume it worked. + int status; + while (waitpid(pid, &status, 0)) + { + if(WIFEXITED(status)) + { + return WEXITSTATUS(status) == 0; + } + if(WIFSIGNALED(status)) + { + return false; + } + } + return true; + } +} +#endif + +namespace DesktopServices { +bool openDirectory(const QString &path, bool ensureExists) +{ + qDebug() << "Opening directory" << path; + QDir parentPath; + QDir dir(path); + if (!dir.exists()) + { + parentPath.mkpath(dir.absolutePath()); + } + auto f = [&]() + { + return QDesktopServices::openUrl(QUrl::fromLocalFile(dir.absolutePath())); + }; +#if defined(Q_OS_LINUX) + return IndirectOpen(f); +#else + return f(); +#endif +} + +bool openFile(const QString &path) +{ + qDebug() << "Opening file" << path; + auto f = [&]() + { + return QDesktopServices::openUrl(QUrl::fromLocalFile(path)); + }; +#if defined(Q_OS_LINUX) + return IndirectOpen(f); +#else + return f(); +#endif +} + +bool openFile(const QString &application, const QString &path, const QString &workingDirectory, qint64 *pid) +{ + qDebug() << "Opening file" << path << "using" << application; +#if defined(Q_OS_LINUX) + // FIXME: the pid here is fake. So if something depends on it, it will likely misbehave + return IndirectOpen([&]() + { + return QProcess::startDetached(application, QStringList() << path, workingDirectory); + }, pid); +#else + return QProcess::startDetached(application, QStringList() << path, workingDirectory, pid); +#endif +} + +bool run(const QString &application, const QStringList &args, const QString &workingDirectory, qint64 *pid) +{ + qDebug() << "Running" << application << "with args" << args.join(' '); +#if defined(Q_OS_LINUX) + // FIXME: the pid here is fake. So if something depends on it, it will likely misbehave + return IndirectOpen([&]() + { + return QProcess::startDetached(application, args, workingDirectory); + }, pid); +#else + return QProcess::startDetached(application, args, workingDirectory, pid); +#endif +} + +bool openUrl(const QUrl &url) +{ + qDebug() << "Opening URL" << url.toString(); + auto f = [&]() + { + return QDesktopServices::openUrl(url); + }; +#if defined(Q_OS_LINUX) + return IndirectOpen(f); +#else + return f(); +#endif +} + +} diff --git a/logic/DesktopServices.h b/logic/DesktopServices.h new file mode 100644 index 000000000..1e67e4cbd --- /dev/null +++ b/logic/DesktopServices.h @@ -0,0 +1,37 @@ +#pragma once + +#include +#include +#include "multimc_logic_export.h" + +/** + * This wraps around QDesktopServices and adds workarounds where needed + * Use this instead of QDesktopServices! + */ +namespace DesktopServices +{ + /** + * Open a file in whatever application is applicable + */ + MULTIMC_LOGIC_EXPORT bool openFile(const QString &path); + + /** + * Open a file in the specified application + */ + MULTIMC_LOGIC_EXPORT bool openFile(const QString &application, const QString &path, const QString & workingDirectory = QString(), qint64 *pid = 0); + + /** + * Run an application + */ + MULTIMC_LOGIC_EXPORT bool run(const QString &application,const QStringList &args, const QString & workingDirectory = QString(), qint64 *pid = 0); + + /** + * Open a directory + */ + MULTIMC_LOGIC_EXPORT bool openDirectory(const QString &path, bool ensureExists = false); + + /** + * Open the URL, most likely in a browser. Maybe. + */ + MULTIMC_LOGIC_EXPORT bool openUrl(const QUrl &url); +}; diff --git a/logic/FileSystem.cpp b/logic/FileSystem.cpp index 006356b39..049f1e387 100644 --- a/logic/FileSystem.cpp +++ b/logic/FileSystem.cpp @@ -6,8 +6,8 @@ #include #include #include -#include #include +#include namespace FS { @@ -304,22 +304,6 @@ QString DirNameFromString(QString string, QString inDir) return dirName; } -void openDirInDefaultProgram(QString path, bool ensureExists) -{ - QDir parentPath; - QDir dir(path); - if (!dir.exists()) - { - parentPath.mkpath(dir.absolutePath()); - } - QDesktopServices::openUrl(QUrl::fromLocalFile(dir.absolutePath())); -} - -void openFileInDefaultProgram(QString filename) -{ - QDesktopServices::openUrl(QUrl::fromLocalFile(filename)); -} - // Does the directory path contain any '!'? If yes, return true, otherwise false. // (This is a problem for Java) bool checkProblemticPathJava(QDir folder) @@ -449,4 +433,4 @@ bool createShortCut(QString location, QString dest, QStringList args, QString na return false; #endif } -} \ No newline at end of file +} diff --git a/logic/FileSystem.h b/logic/FileSystem.h index 5e21375b7..80637f90f 100644 --- a/logic/FileSystem.h +++ b/logic/FileSystem.h @@ -110,12 +110,6 @@ MULTIMC_LOGIC_EXPORT QString RemoveInvalidFilenameChars(QString string, QChar re MULTIMC_LOGIC_EXPORT QString DirNameFromString(QString string, QString inDir = "."); -/// Opens the given file in the default application. -MULTIMC_LOGIC_EXPORT void openFileInDefaultProgram(QString filename); - -/// Opens the given directory in the default application. -MULTIMC_LOGIC_EXPORT void openDirInDefaultProgram(QString dirpath, bool ensureExists = false); - /// Checks if the a given Path contains "!" MULTIMC_LOGIC_EXPORT bool checkProblemticPathJava(QDir folder); diff --git a/logic/tools/MCEditTool.cpp b/logic/tools/MCEditTool.cpp index db45797f4..f0f715c3c 100644 --- a/logic/tools/MCEditTool.cpp +++ b/logic/tools/MCEditTool.cpp @@ -2,7 +2,6 @@ #include #include -#include #include // FIXME: mixing logic and UI!!!! #include @@ -11,6 +10,7 @@ #include "settings/SettingsObject.h" #include "BaseInstance.h" #include "minecraft/MinecraftInstance.h" +#include MCEditTool::MCEditTool(SettingsObjectPtr settings, InstancePtr instance, QObject *parent) : BaseDetachedTool(settings, instance, parent) @@ -84,7 +84,7 @@ void MCEditTool::runImpl() #endif if(program.size()) { - QProcess::startDetached(program, QStringList() << save, mceditPath); + DesktopServices::openFile(program, save, mceditPath); } #endif }