From b4be27746f99aaca74d90a9e592aacbb2adc78bf Mon Sep 17 00:00:00 2001 From: EulalieCoevoet Date: Thu, 27 Nov 2025 22:59:38 +0100 Subject: [PATCH 1/3] [FileSystem] adds openFileWithDefaultApplication method --- .../src/sofa/helper/system/FileSystem.cpp | 31 +++++++++++++++++++ .../src/sofa/helper/system/FileSystem.h | 3 ++ 2 files changed, 34 insertions(+) diff --git a/Sofa/framework/Helper/src/sofa/helper/system/FileSystem.cpp b/Sofa/framework/Helper/src/sofa/helper/system/FileSystem.cpp index 8479dbf3d7d..e1678e86cb5 100644 --- a/Sofa/framework/Helper/src/sofa/helper/system/FileSystem.cpp +++ b/Sofa/framework/Helper/src/sofa/helper/system/FileSystem.cpp @@ -497,6 +497,37 @@ std::string FileSystem::append(const std::string_view& existingPath, const std:: return std::string(existingPath) + "/" + std::string(toAppend); } +void FileSystem::openFileWithDefaultApplication(const std::string& filename) +{ + if (!filename.empty()) + { + if (!FileSystem::exists(filename)) + { + msg_error("FileSystem::openFileWithDefaultApplication()") << "File does not exist: " << filename; + return; + } + + bool success = true; + +#ifdef WIN32 + // According to documentation, values greater than 32 indicate success + if (ShellExecuteA(nullptr, "start", filename.c_str(), nullptr, nullptr, SW_SHOWNORMAL) <= 32) + success = false; +#elif defined(__APPLE__) + const std::string command = "open \"" + filename + "\""; + if (std::system(command.c_str()) == -1) + success = false; +#else + const std::string command = "xdg-open \"" + filename + "\""; + if (std::system(command.c_str()) == -1) + success = false; +#endif + if (!success) + { + msg_error("FileSystem::openFileWithDefaultApplication()") << "Could not open file: " << filename; + } + } +} } // namespace system } // namespace helper diff --git a/Sofa/framework/Helper/src/sofa/helper/system/FileSystem.h b/Sofa/framework/Helper/src/sofa/helper/system/FileSystem.h index e42e7acb031..c3747ad3173 100644 --- a/Sofa/framework/Helper/src/sofa/helper/system/FileSystem.h +++ b/Sofa/framework/Helper/src/sofa/helper/system/FileSystem.h @@ -172,6 +172,9 @@ static std::string append(const std::string_view& existingPath, const std::strin return append(append(existingPath, toAppend), args...); } +/// Open a file with the default application associated to its type by the OS +static void openFileWithDefaultApplication(const std::string& filename); + }; From eba758fae230ab27fdadb948e51d253bb6db5bfc Mon Sep 17 00:00:00 2001 From: EulalieCoevoet Date: Fri, 28 Nov 2025 15:22:58 +0100 Subject: [PATCH 2/3] cleaning --- .../src/sofa/helper/system/FileSystem.cpp | 28 +++++++++---------- .../src/sofa/helper/system/FileSystem.h | 2 +- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/Sofa/framework/Helper/src/sofa/helper/system/FileSystem.cpp b/Sofa/framework/Helper/src/sofa/helper/system/FileSystem.cpp index e1678e86cb5..4ed69ca8d17 100644 --- a/Sofa/framework/Helper/src/sofa/helper/system/FileSystem.cpp +++ b/Sofa/framework/Helper/src/sofa/helper/system/FileSystem.cpp @@ -497,36 +497,34 @@ std::string FileSystem::append(const std::string_view& existingPath, const std:: return std::string(existingPath) + "/" + std::string(toAppend); } -void FileSystem::openFileWithDefaultApplication(const std::string& filename) +bool FileSystem::openFileWithDefaultApplication(const std::string& filename) { + bool success = false; + if (!filename.empty()) { if (!FileSystem::exists(filename)) { msg_error("FileSystem::openFileWithDefaultApplication()") << "File does not exist: " << filename; - return; + return success; } - bool success = true; - #ifdef WIN32 // According to documentation, values greater than 32 indicate success - if (ShellExecuteA(nullptr, "start", filename.c_str(), nullptr, nullptr, SW_SHOWNORMAL) <= 32) - success = false; + if (ShellExecuteA(nullptr, "start", filename.c_str(), nullptr, nullptr, SW_SHOWNORMAL) > 32) + success = true; #elif defined(__APPLE__) const std::string command = "open \"" + filename + "\""; - if (std::system(command.c_str()) == -1) - success = false; + if (std::system(command.c_str()) == 0) + success = true; #else - const std::string command = "xdg-open \"" + filename + "\""; - if (std::system(command.c_str()) == -1) - success = false; + const std::string command = "xdg-open \"" + filename + "\""; + if (std::system(command.c_str()) == 0) + success = true; #endif - if (!success) - { - msg_error("FileSystem::openFileWithDefaultApplication()") << "Could not open file: " << filename; - } } + + return success; } } // namespace system diff --git a/Sofa/framework/Helper/src/sofa/helper/system/FileSystem.h b/Sofa/framework/Helper/src/sofa/helper/system/FileSystem.h index c3747ad3173..ace9bfd9a13 100644 --- a/Sofa/framework/Helper/src/sofa/helper/system/FileSystem.h +++ b/Sofa/framework/Helper/src/sofa/helper/system/FileSystem.h @@ -173,7 +173,7 @@ static std::string append(const std::string_view& existingPath, const std::strin } /// Open a file with the default application associated to its type by the OS -static void openFileWithDefaultApplication(const std::string& filename); +static bool openFileWithDefaultApplication(const std::string& filename); }; From 368c9525bc819ad7d4ec825adb5f65b5e9283f7c Mon Sep 17 00:00:00 2001 From: HanaeRateau Date: Fri, 28 Nov 2025 17:02:21 +0100 Subject: [PATCH 3/3] Fixes open file for Windows --- Sofa/framework/Helper/src/sofa/helper/system/FileSystem.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sofa/framework/Helper/src/sofa/helper/system/FileSystem.cpp b/Sofa/framework/Helper/src/sofa/helper/system/FileSystem.cpp index 4ed69ca8d17..dc1bb6f718e 100644 --- a/Sofa/framework/Helper/src/sofa/helper/system/FileSystem.cpp +++ b/Sofa/framework/Helper/src/sofa/helper/system/FileSystem.cpp @@ -41,6 +41,7 @@ # include # include # include "Shlwapi.h" // for PathFileExists() +#include #else # include # include @@ -510,8 +511,7 @@ bool FileSystem::openFileWithDefaultApplication(const std::string& filename) } #ifdef WIN32 - // According to documentation, values greater than 32 indicate success - if (ShellExecuteA(nullptr, "start", filename.c_str(), nullptr, nullptr, SW_SHOWNORMAL) > 32) + if ((INT_PTR)ShellExecuteA(nullptr, "open", filename.c_str(), nullptr, nullptr, SW_SHOWNORMAL) > 32) success = true; #elif defined(__APPLE__) const std::string command = "open \"" + filename + "\"";