From 1a1de370e4806e85251779761ea40220d0ba9e82 Mon Sep 17 00:00:00 2001 From: bitscher Date: Sat, 3 Sep 2022 13:32:31 -0700 Subject: [PATCH] Portable way of opening URLs and directories (#156) Replaced every instance of ShellExecute with wxLaunchDefaultBrowser --- src/gui/ChecksumTool.cpp | 11 ++----- src/gui/MainWindow.cpp | 10 +------ src/gui/TitleManager.cpp | 10 +------ src/gui/components/wxGameList.cpp | 36 +++++------------------ src/gui/components/wxTitleManagerList.cpp | 15 +++------- 5 files changed, 15 insertions(+), 67 deletions(-) diff --git a/src/gui/ChecksumTool.cpp b/src/gui/ChecksumTool.cpp index 25e6c7f2..1c1e30a2 100644 --- a/src/gui/ChecksumTool.cpp +++ b/src/gui/ChecksumTool.cpp @@ -1,9 +1,5 @@ #include "gui/ChecksumTool.h" -#ifdef _WIN32 -#include -#endif - #include "Cafe/TitleList/GameInfo.h" #include "gui/helpers/wxCustomEvents.h" #include "util/helpers/helpers.h" @@ -521,11 +517,8 @@ void ChecksumTool::VerifyJsonEntry(const rapidjson::Document& doc) } file.flush(); file.close(); -#ifdef _WIN32 - ShellExecuteA(GetHWND(), "open", path.c_str(), nullptr, nullptr, SW_SHOWNORMAL); -#else - assert_dbg(); -#endif + + wxLaunchDefaultBrowser(fmt::format("file:{}", path)); } else wxMessageBox(_("Can't open file to write!"), _("Error"), wxOK | wxCENTRE | wxICON_ERROR, this); diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index a9ae6fe1..e26b31e3 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -37,10 +37,6 @@ #include "Cafe/GraphicPack/GraphicPack.h" #include "Cafe/TitleList/GameInfo.h" -#ifdef _WIN32 -#include -#endif - #include #include "util/helpers/SystemException.h" #include "gui/DownloadGraphicPacksWindow.h" @@ -1951,11 +1947,7 @@ public: "/*****************************************************************************/\r\n" ); delete fs; -#ifdef _WIN32 - ShellExecute(0, 0, tempPath.generic_wstring().c_str(), 0, 0, SW_SHOW); -#else - assert_dbg(); -#endif + wxLaunchDefaultBrowser(fmt::format("file:{}", _utf8Wrapper(tempPath))); }); lineSizer->Add(noticeLink, 0); lineSizer->Add(new wxStaticText(parent, -1, ")"), 0); diff --git a/src/gui/TitleManager.cpp b/src/gui/TitleManager.cpp index 7ae72131..47d7a6dd 100644 --- a/src/gui/TitleManager.cpp +++ b/src/gui/TitleManager.cpp @@ -1,9 +1,5 @@ #include "gui/TitleManager.h" -#ifdef _WIN32 -#include -#endif - #include "gui/helpers/wxCustomEvents.h" #include "gui/helpers/wxCustomData.h" #include "Cafe/TitleList/GameInfo.h" @@ -483,11 +479,7 @@ void TitleManager::OnSaveOpenDirectory(wxCommandEvent& event) if (!fs::exists(target) || !fs::is_directory(target)) return; -#ifdef _WIN32 - ShellExecuteW(GetHWND(), L"open", target.c_str(), nullptr, nullptr, SW_SHOWNORMAL); -#else - assert_dbg(); -#endif + wxLaunchDefaultBrowser(fmt::format("file:{}", _utf8Wrapper(target))); } void TitleManager::OnSaveDelete(wxCommandEvent& event) diff --git a/src/gui/components/wxGameList.cpp b/src/gui/components/wxGameList.cpp index 25ec35f4..b919e227 100644 --- a/src/gui/components/wxGameList.cpp +++ b/src/gui/components/wxGameList.cpp @@ -4,9 +4,6 @@ #include "util/helpers/helpers.h" #include "gui/GameProfileWindow.h" -#ifdef _WIN32 -#include -#endif #include #include @@ -473,6 +470,7 @@ void wxGameList::OnContextMenu(wxContextMenuEvent& event) menu.Append(kContextMenuStart, _("&Start")); bool isFavorite = false; + std::error_code ec; menu.AppendSeparator(); menu.AppendCheckItem(kContextMenuFavorite, _("&Favorite"))->Check(isFavorite); @@ -481,7 +479,7 @@ void wxGameList::OnContextMenu(wxContextMenuEvent& event) menu.AppendSeparator(); menu.Append(kWikiPage, _("&Wiki page")); menu.Append(kContextMenuGameFolder, _("&Game directory")); - menu.Append(kContextMenuSaveFolder, _("&Save directory"))->Enable(true); + menu.Append(kContextMenuSaveFolder, _("&Save directory"))->Enable(fs::is_directory(gameInfo.GetSaveFolder(), ec)); menu.Append(kContextMenuUpdateFolder, _("&Update directory"))->Enable(gameInfo.HasUpdate()); menu.Append(kContextMenuDLCFolder, _("&DLC directory"))->Enable(gameInfo.HasAOC()); @@ -552,13 +550,9 @@ void wxGameList::OnContextMenuSelected(wxCommandEvent& event) } case kContextMenuGameFolder: { -#ifdef _WIN32 fs::path path(gameInfo.GetBase().GetPath()); _stripPathFilename(path); - ShellExecuteW(GetHWND(), L"open", path.c_str(), nullptr, nullptr, SW_SHOWNORMAL); -#else - assert_dbg(); -#endif + wxLaunchDefaultBrowser(fmt::format("file:{}", _utf8Wrapper(path))); break; } case kWikiPage: @@ -572,44 +566,28 @@ void wxGameList::OnContextMenuSelected(wxCommandEvent& event) wxASSERT(!tokens.empty()); const std::string company_code = gameInfo.GetBase().GetMetaInfo()->GetCompanyCode(); wxASSERT(company_code.size() >= 2); -#ifdef _WIN32 - ShellExecuteA(GetHWND(), "open", fmt::format("https://wiki.cemu.info/wiki/{}{}", *tokens.rbegin(), company_code.substr(company_code.size() - 2)).c_str(), nullptr, nullptr, SW_SHOWNORMAL); -#else - assert_dbg(); -#endif + wxLaunchDefaultBrowser(fmt::format("https://wiki.cemu.info/wiki/{}{}", *tokens.rbegin(), company_code.substr(company_code.size() - 2).c_str())); } break; } case kContextMenuSaveFolder: { -#ifdef _WIN32 - ShellExecuteW(GetHWND(), L"open", gameInfo.GetSaveFolder().c_str(), nullptr, nullptr, SW_SHOWNORMAL); -#else - assert_dbg(); -#endif + wxLaunchDefaultBrowser(fmt::format("file:{}", _utf8Wrapper(gameInfo.GetSaveFolder()))); break; } case kContextMenuUpdateFolder: { -#ifdef _WIN32 fs::path path(gameInfo.GetUpdate().GetPath()); _stripPathFilename(path); - ShellExecuteW(GetHWND(), L"open", path.c_str(), nullptr, nullptr, SW_SHOWNORMAL); -#else - assert_dbg(); -#endif + wxLaunchDefaultBrowser(fmt::format("file:{}", _utf8Wrapper(path))); break; } case kContextMenuDLCFolder: { -#ifdef _WIN32 fs::path path(gameInfo.GetAOC().front().GetPath()); _stripPathFilename(path); - ShellExecuteW(GetHWND(), L"open", path.c_str(), nullptr, nullptr, SW_SHOWNORMAL); -#else - assert_dbg(); -#endif + wxLaunchDefaultBrowser(fmt::format("file:{}", _utf8Wrapper(path))); break; } case kContextMenuEditGraphicPacks: diff --git a/src/gui/components/wxTitleManagerList.cpp b/src/gui/components/wxTitleManagerList.cpp index 37025083..883472d1 100644 --- a/src/gui/components/wxTitleManagerList.cpp +++ b/src/gui/components/wxTitleManagerList.cpp @@ -20,9 +20,6 @@ #include "../wxHelper.h" #include -#ifdef _WIN32 -#include -#endif #include "config/ActiveSettings.h" #include "gui/ChecksumTool.h" @@ -853,14 +850,10 @@ void wxTitleManagerList::OnContextMenuSelected(wxCommandEvent& event) switch (event.GetId()) { case kContextMenuOpenDirectory: - { - const auto path = fs::is_directory(entry->path) ? entry->path : entry->path.parent_path(); -#ifdef _WIN32 - ShellExecuteW(GetHWND(), L"open", path.generic_wstring().c_str(), nullptr, nullptr, SW_SHOWNORMAL); -#else - assert_dbg(); -#endif - } + { + const auto path = fs::is_directory(entry->path) ? entry->path : entry->path.parent_path(); + wxLaunchDefaultBrowser(fmt::format("file:{}", _utf8Wrapper(path))); + } break; case kContextMenuDelete: m_context_worker = std::async(std::launch::async, &wxTitleManagerList::DeleteEntry, this, selection, entry.value());