game_version_dialog: use boost::filesystem::path for path calculations

This commit is contained in:
Subhraman Sarkar 2024-12-20 12:16:40 +05:30
parent 36fa52fc1a
commit 920e155ef6
3 changed files with 37 additions and 22 deletions

View file

@ -32,6 +32,7 @@
#include <boost/algorithm/string/predicate.hpp>
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/format.hpp>
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/process.hpp>
@ -825,6 +826,32 @@ static const bfs::path& get_user_data_path()
return user_data_dir;
}
utils::optional<std::string> get_game_manual_file(const std::string& locale_code)
{
utils::optional<std::string> manual_path_opt;
const std::string& manual_dir(game_config::path + "/doc/manual/");
boost::format manual_template(manual_dir + "manual.%s.html");
bfs::path manual_path((manual_template % locale_code).str());
if(bfs::exists(manual_path)) {
return "file://" + bfs::canonical(manual_path).string();
}
// Split the given locale code: "en_GB" -> "en", "GB"
// If the result of split() is empty then locale_code is empty (likely using System Language)
// Assume en is always available as a fall-back
const auto& split_locale_code = utils::split(locale_code, '_');
const std::string& language_code = split_locale_code.empty() ? "en" : split_locale_code[0];
manual_path = (manual_template % language_code).str();
if(bfs::exists(manual_path)) {
// If a filename like manual.en_GB.html is not found, try manual.en.html
return "file://" + bfs::canonical(manual_path).string();
}
return {};
}
std::string get_user_data_dir()
{
return get_user_data_path().string();

View file

@ -153,6 +153,8 @@ std::string get_credentials_file();
std::string get_default_prefs_file();
std::string get_save_index_file();
std::string get_lua_history_file();
/** location of the game manual file correponding to the given locale (default: en) */
utils::optional<std::string> get_game_manual_file(const std::string& locale_code = "en");
/**
* parent directory for everything that should be synced between systems.
* implemented due to limitations of Steam's AutoCloud (non-SDK) syncing, but will also simplify things if it's ever added for any other platforms.

View file

@ -23,7 +23,6 @@
#include "desktop/version.hpp"
#include "filesystem.hpp"
#include "formula/string_utils.hpp"
#include "gui/dialogs/migrate_version_selection.hpp"
#include "gui/widgets/button.hpp"
#include "gui/widgets/styled_widget.hpp"
#include "gui/widgets/listbox.hpp"
@ -31,6 +30,7 @@
#include "gui/widgets/text_box_base.hpp"
#include "gui/widgets/window.hpp"
#include "gui/dialogs/message.hpp"
#include "gui/dialogs/migrate_version_selection.hpp"
#include "gui/dialogs/end_credits.hpp"
#include "gettext.hpp"
#include "help/help.hpp"
@ -264,35 +264,21 @@ void game_version::show_license() {
}
void game_version::report_issue() {
if (!desktop::open_object_is_supported()) {
show_message("", _("Opening links is not supported, contact your packager"), dialogs::message::auto_close);
return;
} else {
if (desktop::open_object_is_supported()) {
desktop::open_object("https://bugs.wesnoth.org");
} else {
show_message("", _("Opening links is not supported, contact your packager"), dialogs::message::auto_close);
}
}
void game_version::show_manual() {
if (desktop::open_object_is_supported()) {
const std::string& locale_code = get_language().localename;
const std::vector<std::string>& split_locale_code = utils::split(locale_code, '_');
// If the result of split() is empty then locale_code is empty (likely using System Language)
// Assume en is always available as a fall-back
const std::string& language_code = split_locale_code.empty() ? "en" : split_locale_code[0];
const std::string& local_directory = game_config::path + "/doc/manual/";
const std::string& web_directory = "www.wesnoth.org/manual/dev/";
const std::string& locale_file_name = "manual." + locale_code + ".html";
const std::string& language_file_name = "manual." + language_code + ".html";
if(filesystem::file_exists(local_directory + locale_file_name)) {
desktop::open_object("file://" + local_directory + locale_file_name);
} else if(filesystem::file_exists(local_directory + language_file_name)) {
// If a filename like manual.en_GB.html is not found, try manual.en.html
desktop::open_object("file://" + local_directory + language_file_name);
const auto& manual_path = filesystem::get_game_manual_file(get_language().localename);
if (manual_path) {
desktop::open_object(manual_path.value());
} else {
// Use web manual as a last resort
desktop::open_object("https://" + web_directory + language_file_name);
desktop::open_object("https://www.wesnoth.org/manual/dev/manual." + get_language().localename + ".html");
}
} else {
show_message("", _("Opening links is not supported, contact your packager"), dialogs::message::auto_close);