diff --git a/CMakeLists.txt b/CMakeLists.txt index 4289b48..e8c8519 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -62,7 +62,7 @@ if (ENABLE_GUI) endif() endif() -find_package(Boost 1.61 REQUIRED COMPONENTS iostreams) +find_package(Boost 1.75 REQUIRED COMPONENTS iostreams) find_package(LibIMEPinyin 1.1.7 REQUIRED) find_package(LibIMETable 1.1.4 REQUIRED) diff --git a/modules/chttrans/chttrans.cpp b/modules/chttrans/chttrans.cpp index 5a693f0..c9c3646 100644 --- a/modules/chttrans/chttrans.cpp +++ b/modules/chttrans/chttrans.cpp @@ -16,6 +16,9 @@ #include #ifdef ENABLE_OPENCC #include "chttrans-opencc.h" +#include +#include +#include #endif #include "chttrans-native.h" #include @@ -215,15 +218,27 @@ void Chttrans::save() { const Configuration *Chttrans::getConfig() const { #ifdef ENABLE_OPENCC - std::vector profiles; - StandardPath::global().scanFiles( - StandardPath::Type::Data, "opencc", - [&profiles](const std::string &path, const std::string &, bool) { - if (stringutils::endsWith(path, ".json")) { - profiles.emplace_back(path); - } - return true; - }); + std::vector> profiles{{"", ""}}; + auto files = StandardPath::global().multiOpen( + StandardPath::Type::Data, "opencc", O_RDONLY, filter::Suffix(".json")); + for (const auto &file : files) { + try { + boost::iostreams::stream_buffer< + boost::iostreams::file_descriptor_source> + buffer(file.second.fd(), + boost::iostreams::file_descriptor_flags:: + never_close_handle); + std::istream in(&buffer); + std::string strBuf(std::istreambuf_iterator(in), {}); + auto jv = boost::json::parse(strBuf); + const auto &obj = jv.as_object(); + auto name = boost::json::value_to(obj.at("name")); + profiles.emplace_back(file.first, std::move(name)); + } catch (const std::exception &e) { + FCITX_WARN() << "Failed to parse " << file.first; + profiles.emplace_back(file.first, file.first); + } + } config_.openCCS2TProfile.annotation().setProfiles(profiles); config_.openCCT2SProfile.annotation().setProfiles(std::move(profiles)); #endif diff --git a/modules/chttrans/chttrans.h b/modules/chttrans/chttrans.h index 2285b7b..b7e4a97 100644 --- a/modules/chttrans/chttrans.h +++ b/modules/chttrans/chttrans.h @@ -21,20 +21,22 @@ #ifdef ENABLE_OPENCC struct OpenCCAnnotation : public fcitx::EnumAnnotation { - void setProfiles(std::vector profiles) { + void + setProfiles(std::vector> profiles) { profiles_ = std::move(profiles); } void dumpDescription(fcitx::RawConfig &config) const { fcitx::EnumAnnotation::dumpDescription(config); for (size_t i = 0; i < profiles_.size(); i++) { - config.setValueByPath("Enum/" + std::to_string(i), profiles_[i]); + config.setValueByPath("Enum/" + std::to_string(i), + profiles_[i].first); config.setValueByPath("EnumI18n/" + std::to_string(i), - profiles_[i]); + profiles_[i].second); } } private: - std::vector profiles_; + std::vector> profiles_; }; #endif