|
| 1 | +/* |
| 2 | + ------------------------------------------------------------------------------- |
| 3 | + This file is part of the Private Message Database. |
| 4 | + Copyright (C) 2025 Dirk Stolle |
| 5 | +
|
| 6 | + This program is free software: you can redistribute it and/or modify |
| 7 | + it under the terms of the GNU General Public License as published by |
| 8 | + the Free Software Foundation, either version 3 of the License, or |
| 9 | + (at your option) any later version. |
| 10 | +
|
| 11 | + This program is distributed in the hope that it will be useful, |
| 12 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + GNU General Public License for more details. |
| 15 | +
|
| 16 | + You should have received a copy of the GNU General Public License |
| 17 | + along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + ------------------------------------------------------------------------------- |
| 19 | +*/ |
| 20 | + |
| 21 | +#include "open_file.hpp" |
| 22 | +#include <iostream> |
| 23 | +#include <boost/version.hpp> |
| 24 | +#if (BOOST_VERSION <= 108100) || defined(USE_BOOST_PROCESS_V1) |
| 25 | + #include <boost/process.hpp> |
| 26 | +#else |
| 27 | + #include <boost/asio/io_context.hpp> |
| 28 | + #include <boost/process/v2/process.hpp> |
| 29 | + #if defined(_WIN32) |
| 30 | + #include <boost/process/v2/windows/default_launcher.hpp> |
| 31 | + #else |
| 32 | + #include <boost/process/v2/posix/fork_and_forget_launcher.hpp> |
| 33 | + #endif |
| 34 | +#endif |
| 35 | +#include "browser_detection.hpp" |
| 36 | +#include "../libstriezel/filesystem/directory.hpp" |
| 37 | +#include "../libstriezel/hash/sha256/BufferSourceUtility.hpp" |
| 38 | + |
| 39 | +std::string getFirstFolderFileName(const FolderMap& fm) |
| 40 | +{ |
| 41 | + const auto folders = fm.getPresentFolders(); |
| 42 | + if (folders.empty()) |
| 43 | + { |
| 44 | + return ""; |
| 45 | + } |
| 46 | + const std::string& name = *folders.begin(); |
| 47 | + return "folder_" |
| 48 | + + SHA256::computeFromBuffer((uint8_t*) name.c_str(), name.length() * 8).toHexString() |
| 49 | + + ".html"; |
| 50 | +} |
| 51 | + |
| 52 | +void openFirstIndexFile(const FolderMap& fm, const std::string& html_dir) |
| 53 | +{ |
| 54 | + const auto fileName = getFirstFolderFileName(fm); |
| 55 | + if (fileName.empty()) |
| 56 | + { |
| 57 | + return; |
| 58 | + } |
| 59 | + const auto fullFileName = libstriezel::filesystem::slashify(html_dir) + fileName; |
| 60 | + const auto browser = detect_browser(); |
| 61 | + if (!browser.has_value()) |
| 62 | + { |
| 63 | + std::cout << "Info: Could not find a browser to open generated files.\n" |
| 64 | + << "Info: Open " << fullFileName << " in a browser to see the generated HTML files.\n"; |
| 65 | + return; |
| 66 | + } |
| 67 | + // Open file via Boost Process. |
| 68 | + std::cout << "Opening " << fullFileName << " in browser ...\n"; |
| 69 | + std::vector<std::string> params = additional_parameters(browser.value().type); |
| 70 | + #if (BOOST_VERSION <= 108100) || defined(USE_BOOST_PROCESS_V1) |
| 71 | + std::string command = browser.value().path.string(); |
| 72 | + for (const auto& param: params) |
| 73 | + { |
| 74 | + command += " " + param; |
| 75 | + } |
| 76 | + command += " " + fullFileName; |
| 77 | + boost::process::child child(command); |
| 78 | + child.detach(); |
| 79 | + #else |
| 80 | + // After Boost 1.81.0 use process v2 API. |
| 81 | + boost::asio::io_context context; |
| 82 | + params.push_back(fullFileName); |
| 83 | + #if defined(_WIN32) |
| 84 | + auto launcher = boost::process::v2::windows::default_launcher(); |
| 85 | + #else |
| 86 | + auto launcher = boost::process::v2::posix::fork_and_forget_launcher(); |
| 87 | + #endif |
| 88 | + boost::process::v2::process proc(context, browser.value().path.string(), params); |
| 89 | + proc.detach(); |
| 90 | + #endif |
| 91 | +} |
0 commit comments