From fabb1596e5cf07e7ff95afd04df1980d433fbe6b Mon Sep 17 00:00:00 2001 From: Thomas Date: Sun, 26 Mar 2023 11:02:51 +0200 Subject: [PATCH] impr: Handle and show NFD errors (#995) This PR handles errors that NFD might encounter (both in Init() and the other method to open the dialog), and log them in the logs and in the GUI This (among other) fix the crash I had running ImHex as root and opening a file --- lib/external/nativefiledialog | 2 +- lib/libimhex/include/hex/helpers/fs.hpp | 2 +- lib/libimhex/source/helpers/fs.cpp | 17 +++++++++++++---- plugins/builtin/romfs/lang/en_US.json | 4 ++-- plugins/builtin/source/content/events.cpp | 6 +++--- 5 files changed, 20 insertions(+), 11 deletions(-) diff --git a/lib/external/nativefiledialog b/lib/external/nativefiledialog index 6efc82407..d1b80e3a6 160000 --- a/lib/external/nativefiledialog +++ b/lib/external/nativefiledialog @@ -1 +1 @@ -Subproject commit 6efc824070c79afd42a3a9e08da8f867d0ca2a97 +Subproject commit d1b80e3a60fcf30d0c886652790796630d09c90e diff --git a/lib/libimhex/include/hex/helpers/fs.hpp b/lib/libimhex/include/hex/helpers/fs.hpp index d970a5935..98522f291 100644 --- a/lib/libimhex/include/hex/helpers/fs.hpp +++ b/lib/libimhex/include/hex/helpers/fs.hpp @@ -20,7 +20,7 @@ namespace hex::fs { Folder }; - void setFileBrowserErrorCallback(const std::function &callback); + void setFileBrowserErrorCallback(const std::function &callback); bool openFileBrowser(DialogMode mode, const std::vector &validExtensions, const std::function &callback, const std::string &defaultPath = {}, bool multiple = false); enum class ImHexPath : u32 { diff --git a/lib/libimhex/source/helpers/fs.cpp b/lib/libimhex/source/helpers/fs.cpp index f48432513..ba446495a 100644 --- a/lib/libimhex/source/helpers/fs.cpp +++ b/lib/libimhex/source/helpers/fs.cpp @@ -2,6 +2,7 @@ #include #include +#include #include @@ -21,13 +22,20 @@ namespace hex::fs { - static std::function s_fileBrowserErrorCallback; - void setFileBrowserErrorCallback(const std::function &callback) { + static std::function s_fileBrowserErrorCallback; + void setFileBrowserErrorCallback(const std::function &callback) { s_fileBrowserErrorCallback = callback; } bool openFileBrowser(DialogMode mode, const std::vector &validExtensions, const std::function &callback, const std::string &defaultPath, bool multiple) { - NFD::Init(); + NFD::ClearError(); + + if (NFD::Init() != NFD_OKAY) { + log::error("NFD init returned an error: {}", NFD::GetError()); + if (s_fileBrowserErrorCallback != nullptr) + s_fileBrowserErrorCallback(NFD::GetError() ? NFD::GetError() : "No details"); + return false; + } NFD::UniquePathU8 outPath; NFD::UniquePathSet outPaths; @@ -64,8 +72,9 @@ namespace hex::fs { } } } else if (result == NFD_ERROR) { + log::error("Requested file dialog returned an error: {}", NFD::GetError()); if (s_fileBrowserErrorCallback != nullptr) - s_fileBrowserErrorCallback(); + s_fileBrowserErrorCallback(NFD::GetError() ? NFD::GetError() : "No details"); } NFD::Quit(); diff --git a/plugins/builtin/romfs/lang/en_US.json b/plugins/builtin/romfs/lang/en_US.json index 8559343a4..6ed42aa7c 100644 --- a/plugins/builtin/romfs/lang/en_US.json +++ b/plugins/builtin/romfs/lang/en_US.json @@ -362,8 +362,8 @@ "hex.builtin.popup.close_provider.desc": "There are unsaved changes made to this Provider\nthat haven't been saved to a Project yet.\n\nAre you sure you want to close it?", "hex.builtin.popup.close_provider.title": "Close Provider?", "hex.builtin.popup.error.create": "Failed to create new file!", - "hex.builtin.popup.error.file_dialog.common": "An error occurred while opening the file browser!", - "hex.builtin.popup.error.file_dialog.portal": "There was an error while opening the file browser.\nThis might be caused by your system not having a xdg-desktop-portal backend installed correctly.\n\nOn KDE, it's xdg-desktop-portal-kde.\nOn Gnome it's xdg-desktop-portal-gnome.\nOn wlroots it's xdg-desktop-portal-wlr.\nOtherwise, you can try to use xdg-desktop-portal-gtk.\n\nReboot your system after installing it.\n\nIf the file browser still doesn't work after this, submit an issue at https://github.com/WerWolv/ImHex/issues\n\nIn the meantime files can still be opened by dragging them onto the ImHex window!", + "hex.builtin.popup.error.file_dialog.common": "An error occurred while opening the file browser: {}", + "hex.builtin.popup.error.file_dialog.portal": "There was an error while opening the file browser: {}.\nThis might be caused by your system not having a xdg-desktop-portal backend installed correctly.\n\nOn KDE, it's xdg-desktop-portal-kde.\nOn Gnome it's xdg-desktop-portal-gnome.\nOn wlroots it's xdg-desktop-portal-wlr.\nOtherwise, you can try to use xdg-desktop-portal-gtk.\n\nReboot your system after installing it.\n\nIf the file browser still doesn't work after this, submit an issue at https://github.com/WerWolv/ImHex/issues\n\nIn the meantime files can still be opened by dragging them onto the ImHex window!", "hex.builtin.popup.error.open": "Failed to open file!", "hex.builtin.popup.error.project.load": "Failed to load project!", "hex.builtin.popup.error.project.save": "Failed to save project!", diff --git a/plugins/builtin/source/content/events.cpp b/plugins/builtin/source/content/events.cpp index e7fd24be1..efe068bad 100644 --- a/plugins/builtin/source/content/events.cpp +++ b/plugins/builtin/source/content/events.cpp @@ -123,11 +123,11 @@ namespace hex::plugin::builtin { ImHexApi::HexEditor::impl::setCurrentSelection(region); }); - fs::setFileBrowserErrorCallback([]{ + fs::setFileBrowserErrorCallback([](const std::string& errMsg){ #if defined(NFD_PORTAL) - View::showErrorPopup("hex.builtin.popup.error.file_dialog.portal"_lang); + View::showErrorPopup(hex::format("hex.builtin.popup.error.file_dialog.portal"_lang, errMsg)); #else - View::showErrorPopup("hex.builtin.popup.error.file_dialog.common"_lang); + View::showErrorPopup(hex::format("hex.builtin.popup.error.file_dialog.common"_lang, errMsg)); #endif });