From b1c0d94a22d2027b42d1da0eca5b23ed28f17368 Mon Sep 17 00:00:00 2001 From: Dima yawaflua Andreev Date: Sun, 17 Nov 2024 21:39:20 +0200 Subject: [PATCH] Fix language support and compatibility with windows. --- CMakeLists.txt | 15 ++++++------ lang/he.json | 2 +- main.cpp | 44 ++++++++++++++++++++---------------- models/AlertResponse.cpp | 5 ++-- utils/check_file_exists.h | 14 ++++++++++++ utils/localization_manager.h | 1 + 6 files changed, 51 insertions(+), 30 deletions(-) create mode 100644 utils/check_file_exists.h diff --git a/CMakeLists.txt b/CMakeLists.txt index e0d329a..5fe35fc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,28 +24,29 @@ add_executable(tzeva_adom main.cpp models/AlertMessage_OREF.cpp utils/OREFToTzevaAdom.cpp models/OrefAlertResponse.cpp + utils/check_file_exists.h ) find_package(PkgConfig REQUIRED) pkg_check_modules(GLIB REQUIRED glib-2.0) pkg_check_modules(NOTIFY REQUIRED libnotify) pkg_check_modules(SDL REQUIRED libnotify) -find_package(fmt REQUIRED) +pkg_check_modules(SDL2 REQUIRED libnotify) +pkg_check_modules(SDL2_mixer REQUIRED libnotify) +pkg_check_modules(fmt REQUIRED fmt) +pkg_check_modules(boost_filesystem REQUIRED) + include_directories(lang) include_directories(${GLIB_INCLUDE_DIRS}) include_directories(${NOTIFY_INCLUDE_DIRS}) include_directories(${CURL_INCLUDE_DIRS}) +include_directories(${fmt_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS}) -find_package(SDL2 REQUIRED) -find_package(SDL2_mixer REQUIRED) -include_directories(${SDL2_INCLUDE_DIRS} ${SDL2_MIXER_INCLUDE_DIRS}) -find_package(Boost REQUIRED COMPONENTS filesystem) -include_directories(${Boost_INCLUDE_DIRS}) # Линкуем библиотеки target_include_directories(tzeva_adom PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} "lang/") -target_link_libraries(tzeva_adom PRIVATE SDL2 SDL2_mixer ${Boost_LIBRARIES} CURL::libcurl nlohmann_json::nlohmann_json ${GLIB_LIBRARIES} fmt::fmt ${NOTIFY_LIBRARIES} ${CURL_LIBRARIES}) +target_link_libraries(tzeva_adom PRIVATE SDL2 SDL2_mixer ${SDL2_LIBRARIES} ${SDL2_MIXER_LIBRARIES} ${fmt_LIBRARIES} ${Boost_LIBRARIES} CURL::libcurl nlohmann_json::nlohmann_json ${GLIB_LIBRARIES} ${NOTIFY_LIBRARIES} ${CURL_LIBRARIES}) add_custom_command(TARGET tzeva_adom POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_directory diff --git a/lang/he.json b/lang/he.json index a5b291e..858b2f2 100644 --- a/lang/he.json +++ b/lang/he.json @@ -14,5 +14,5 @@ "true": "כן", "false": "לא", "threat": "איום: ", - "cities": "הסדר: " + "cities": "העירים: " } \ No newline at end of file diff --git a/main.cpp b/main.cpp index c0ea72b..872edee 100644 --- a/main.cpp +++ b/main.cpp @@ -4,12 +4,13 @@ #include #include #include + #include #include "spdlog/spdlog.h" #include #include #include -#include +#include "utils/check_file_exists.h" #include #include "CONFIG.cpp" @@ -18,6 +19,7 @@ #include "models/OrefAlertResponse.cpp" #include "utils/image_downloader.cpp" #include "utils/localization_manager.h" +#include class AlertResponse; @@ -27,7 +29,7 @@ int64_t lastId = 0; json cities_n_areas_list; bool is_cities_loaded = false; bool is_test = false; -std::string this_path = boost::filesystem::current_path().c_str(); +std::string this_path = std::filesystem::current_path().c_str(); std::string lang = "en"; std::vector test_alert_variable; bool is_oref = false; @@ -59,9 +61,9 @@ void process_alert(const std::string& data) { std::unique_ptr first_alert; if (is_oref) { - spdlog::debug("OREF_alertDate: {}", response[0]["alertDate"]); - spdlog::debug("OREF_title: {}", response[0]["title"]); - spdlog::debug("OREF_data: {}", response[0]["data"]); + spdlog::debug("OREF_alertDate: {}", response[0]["alertDate"].get()); + spdlog::debug("OREF_title: {}", response[0]["title"].get()); + spdlog::debug("OREF_data: {}", response[0]["data"].get()); spdlog::debug("OREF_category: {}",std::to_string(response[0]["category"].get())); spdlog::debug("OREF selected, create oref object"); auto response_oref = response.at(0); @@ -96,7 +98,7 @@ void process_alert(const std::string& data) { spdlog::debug("Cities: {}", cities); - std::string icon_url = boost::filesystem::current_path().c_str()+fmt::format("/threat{}.{}", std::to_string(threat), threat == 0 ? "png" : "svg"); + std::string icon_url = this_path+fmt::format("/threat{}.{}", std::to_string(threat), threat == 0 ? "png" : "svg"); spdlog::debug("Threat: {}", first_alert->get_threat()); spdlog::debug("Threat name: {}", localization_manager.getString(fmt::format("threat_{}", threat))); @@ -104,17 +106,19 @@ void process_alert(const std::string& data) { spdlog::debug("Icon path: {}", icon_url); std::string localised_cities_names = ""; - for (auto city: first_alert->get_cities_arr()) { - localised_cities_names += fmt::format("{} ", cities_n_areas_list["cities"][city][lang]); - } + if (lang != "he") + for (auto city: first_alert->get_cities_arr()) + localised_cities_names += fmt::format("{} ", cities_n_areas_list["cities"][city][lang].get()); + else + localised_cities_names = first_alert->get_cities(); spdlog::debug("Init notification"); notify_init(localization_manager.getString(fmt::format("threat_{}", std::to_string(threat))).c_str()); NotifyNotification* n = notify_notification_new (localization_manager.getString(fmt::format("threat_{}", std::to_string(threat))).c_str(), fmt::format( - "{}: {}\n"\ - "{}: {}\n"\ - "{}: {}", + "{} {}\n"\ + "{} {}\n"\ + "{} {}", localization_manager.getString("cities"), localised_cities_names, localization_manager.getString("threat"), @@ -154,10 +158,10 @@ void fetch_alerts_history(std::atomic& running) { curl = curl_easy_init(); // Parse cities and areas names on he, ru and en - if (is_oref && lang == "he") { - spdlog::error("Lang is not supported. Use TZEVA_ADOM"); - return; - } + // if (is_oref && lang == "he") { + // spdlog::error("Lang is not supported. Use TZEVA_ADOM"); + // return; + // } if (!is_cities_loaded) { std::string readBuffer; //Only tzeva_adom variable of cities list, because thats easier to work, than oref variable(without linq) @@ -240,7 +244,7 @@ int main(int argc, char** argv) { " -d --debug: Show debug messages\n"\ " -t --test: Create test alert end exit\n"\ " -l --lang: Choose language: ru, en, he\n"\ - " -o --oref: Use OREF uri`s for alerts"\ + " -o --oref: Use OREF api`s for alerts"\ ""); return 0; } @@ -251,10 +255,10 @@ int main(int argc, char** argv) { last_flag = argv[i]; } - spdlog::debug("Path: {}", boost::filesystem::current_path().c_str()); + spdlog::debug("Path: {}", this_path); for (int i = 0; i <= 5; i++) { - if (!boost::filesystem::exists(this_path+fmt::format("/threat{}.{}", std::to_string(i), i == 0 ? "png" : "svg"))) { + if (!file_exists(this_path+fmt::format("/threat{}.{}", std::to_string(i), i == 0 ? "png" : "svg"))) { tzeva_adom::download_file( fmt::format( THREAT_IMAGES_URL, @@ -267,7 +271,7 @@ int main(int argc, char** argv) { localization_manager = tzeva_adom::LocalizationManager(); localization_manager.setCurrentLanguage(lang); //Download tzeva-adom bell sound - if (!boost::filesystem::exists(this_path+fmt::format("/bell.mp3"))) { + if (!file_exists(this_path+fmt::format("/bell.mp3"))) { tzeva_adom::download_file( BELL_SOUND_URL, fmt::format("bell.mp3") diff --git a/models/AlertResponse.cpp b/models/AlertResponse.cpp index 74c5e90..0b6da45 100644 --- a/models/AlertResponse.cpp +++ b/models/AlertResponse.cpp @@ -16,7 +16,7 @@ #include #include "models/Interfaces/IAlertResponse.cpp" #include "models/Interfaces/IAlert.cpp" - +#include namespace tzeva_adom { using nlohmann::json; @@ -127,7 +127,8 @@ namespace tzeva_adom { std::vector get_cities_arr() override { std::vector cities_to_return = {} ; for (auto alert : alerts) { - cities_to_return.push_back(fmt::format("{}", fmt::join(alert.get_cities(), ""))); + for (auto city : alert.get_cities()) + cities_to_return.push_back(city); } return cities_to_return; } diff --git a/utils/check_file_exists.h b/utils/check_file_exists.h new file mode 100644 index 0000000..32b4e48 --- /dev/null +++ b/utils/check_file_exists.h @@ -0,0 +1,14 @@ +// +// Created by yawaflua on 15/11/2024. +// + +#ifndef CHECK_FILE_EXISTS_H +#define CHECK_FILE_EXISTS_H + + +inline bool file_exists (const std::string& name) { + struct stat buffer; + return (stat (name.c_str(), &buffer) == 0); +} + +#endif //CHECK_FILE_EXISTS_H diff --git a/utils/localization_manager.h b/utils/localization_manager.h index b34376e..d72210f 100644 --- a/utils/localization_manager.h +++ b/utils/localization_manager.h @@ -6,6 +6,7 @@ #include #include #include // Подключение библиотеки JSON +#include namespace tzeva_adom { class LocalizationManager {