From 30ae16c2e16ebc06128bf8708926d4f8efeb30a8 Mon Sep 17 00:00:00 2001 From: Albert Kharisov Date: Fri, 16 Jul 2021 19:50:18 +0300 Subject: [PATCH] [FL-1470] Support archive for IRDA (#582) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: あく --- applications/irda/irda-app-event.hpp | 1 + applications/irda/irda-app-remote-manager.cpp | 37 +++++++++++++------ applications/irda/irda-app-remote-manager.hpp | 8 ++-- applications/irda/irda-app.cpp | 32 ++++++++++++++-- applications/irda/irda-app.hpp | 2 +- applications/irda/irda-runner.cpp | 4 +- 6 files changed, 62 insertions(+), 22 deletions(-) diff --git a/applications/irda/irda-app-event.hpp b/applications/irda/irda-app-event.hpp index d4778ad7..fd0df16c 100644 --- a/applications/irda/irda-app-event.hpp +++ b/applications/irda/irda-app-event.hpp @@ -6,6 +6,7 @@ class IrdaAppEvent { public: enum class Type : uint8_t { Tick, + Exit, Back, MenuSelected, DialogExSelected, diff --git a/applications/irda/irda-app-remote-manager.cpp b/applications/irda/irda-app-remote-manager.cpp index 1eef6b4d..aedd39ef 100644 --- a/applications/irda/irda-app-remote-manager.cpp +++ b/applications/irda/irda-app-remote-manager.cpp @@ -53,10 +53,6 @@ bool IrdaAppRemoteManager::add_remote_with_button( return add_button(button_name, signal); } -IrdaAppRemote::IrdaAppRemote(const std::string& name) - : name(name) { -} - std::vector IrdaAppRemoteManager::get_button_list(void) const { std::vector name_vector; name_vector.reserve(remote->buttons.size()); @@ -77,15 +73,22 @@ const IrdaAppSignal& IrdaAppRemoteManager::get_button_data(size_t index) const { return buttons.at(index).signal; } -std::string IrdaAppRemoteManager::make_filename(const std::string& name) const { - return std::string("/") + irda_directory + "/" + name + irda_extension; +std::string IrdaAppRemoteManager::make_full_name(const std::string& remote_name) const { + return std::string("/") + irda_directory + "/" + remote_name + irda_extension; +} + +std::string IrdaAppRemoteManager::make_remote_name(const std::string& full_name) const { + std::string str(full_name, full_name.find_last_of('/') + 1, full_name.size()); + str.erase(str.find_last_of('.')); + + return str; } bool IrdaAppRemoteManager::delete_remote() { FS_Error fs_res; IrdaAppFileParser file_parser; - fs_res = file_parser.get_fs_api().common.remove(make_filename(remote->name).c_str()); + fs_res = file_parser.get_fs_api().common.remove(make_full_name(remote->name).c_str()); if(fs_res != FSE_OK) { file_parser.get_sd_api().show_error( file_parser.get_sd_api().context, "Error deleting file"); @@ -140,7 +143,7 @@ bool IrdaAppRemoteManager::rename_remote(const char* str) { auto new_name = find_vacant_name(remote_list, str); IrdaAppFileParser file_parser; FS_Error fs_err = file_parser.get_fs_api().common.rename( - make_filename(remote->name).c_str(), make_filename(new_name).c_str()); + make_full_name(remote->name).c_str(), make_full_name(new_name).c_str()); remote->name = new_name; if(fs_err != FSE_OK) { file_parser.get_sd_api().show_error( @@ -176,7 +179,7 @@ bool IrdaAppRemoteManager::store(void) { } bool res = file_parser.get_fs_api().file.open( - &file, make_filename(remote->name).c_str(), FSAM_WRITE, FSOM_CREATE_ALWAYS); + &file, make_full_name(remote->name).c_str(), FSAM_WRITE, FSOM_CREATE_ALWAYS); if(!res) { file_parser.get_sd_api().show_error( @@ -233,20 +236,30 @@ bool IrdaAppRemoteManager::get_remote_list(std::vector& remote_name return true; } -bool IrdaAppRemoteManager::load(const std::string& name) { +bool IrdaAppRemoteManager::load(const std::string& name_arg, bool fullpath) { bool fs_res = false; IrdaAppFileParser file_parser; File file; + std::string full_filename; + std::string remote_name; + + if(fullpath) { + full_filename = name_arg; + remote_name = make_remote_name(name_arg); + } else { + full_filename = make_full_name(name_arg); + remote_name = name_arg; + } fs_res = file_parser.get_fs_api().file.open( - &file, make_filename(name).c_str(), FSAM_READ, FSOM_OPEN_EXISTING); + &file, full_filename.c_str(), FSAM_READ, FSOM_OPEN_EXISTING); if(!fs_res) { file_parser.get_sd_api().show_error( file_parser.get_sd_api().context, "Error opening file"); return false; } - remote = std::make_unique(name); + remote = std::make_unique(remote_name); while(1) { auto file_signal = file_parser.read_signal(&file); diff --git a/applications/irda/irda-app-remote-manager.hpp b/applications/irda/irda-app-remote-manager.hpp index fda82e7e..dd193074 100644 --- a/applications/irda/irda-app-remote-manager.hpp +++ b/applications/irda/irda-app-remote-manager.hpp @@ -25,7 +25,8 @@ class IrdaAppRemote { std::vector buttons; std::string name; public: - IrdaAppRemote(const std::string& name); + IrdaAppRemote(const std::string& name) : name(name) {} + IrdaAppRemote& operator=(std::string& new_name) noexcept { name = new_name; @@ -38,7 +39,8 @@ class IrdaAppRemoteManager { static const char* irda_directory; static const char* irda_extension; std::unique_ptr remote; - std::string make_filename(const std::string& name) const; + std::string make_full_name(const std::string& remote_name) const; + std::string make_remote_name(const std::string& full_name) const; public: bool add_remote_with_button(const char* button_name, const IrdaAppSignal& signal); @@ -58,7 +60,7 @@ public: bool delete_remote(); bool store(); - bool load(const std::string& name); + bool load(const std::string& name, bool fullpath = false); bool check_fs() const; }; diff --git a/applications/irda/irda-app.cpp b/applications/irda/irda-app.cpp index 6cd342ba..29e1d281 100644 --- a/applications/irda/irda-app.cpp +++ b/applications/irda/irda-app.cpp @@ -6,16 +6,29 @@ #include #include -void IrdaApp::run(void) { +int32_t IrdaApp::run(void* args) { IrdaAppEvent event; bool consumed; bool exit = false; + if(args) { + const char* remote_name = static_cast(args); + bool result = remote_manager.load(std::string(remote_name), true); + if(result) { + current_scene = IrdaApp::Scene::Remote; + } else { + printf("Failed to load remote \'%s\'\r\n", remote_name); + return -1; + } + } + scenes[current_scene]->on_enter(this); while(!exit) { view_manager.receive_event(&event); + if(event.type == IrdaAppEvent::Type::Exit) break; + consumed = scenes[current_scene]->on_event(this, &event); if(!consumed) { @@ -26,6 +39,8 @@ void IrdaApp::run(void) { }; scenes[current_scene]->on_exit(this); + + return 0; }; IrdaAppViewManager* IrdaApp::get_view_manager() { @@ -59,6 +74,9 @@ void IrdaApp::search_and_switch_to_previous_scene(const std::initializer_liston_exit(this); - current_scene = previous_scene; - scenes[current_scene]->on_enter(this); + if(previous_scene == Scene::Exit) { + IrdaAppEvent event; + event.type = IrdaAppEvent::Type::Exit; + view_manager.send_event(&event); + } else { + scenes[current_scene]->on_exit(this); + current_scene = previous_scene; + scenes[current_scene]->on_enter(this); + } } bool IrdaApp::switch_to_previous_scene(uint8_t count) { diff --git a/applications/irda/irda-app.hpp b/applications/irda/irda-app.hpp index 233c4c9f..31637e7a 100644 --- a/applications/irda/irda-app.hpp +++ b/applications/irda/irda-app.hpp @@ -45,7 +45,7 @@ public: EditDeleteDone, }; - void run(void); + int32_t run(void* args); void switch_to_next_scene(Scene index); void switch_to_next_scene_without_saving(Scene index); bool switch_to_previous_scene(uint8_t count = 1); diff --git a/applications/irda/irda-runner.cpp b/applications/irda/irda-runner.cpp index 9a26fa13..9753466b 100644 --- a/applications/irda/irda-runner.cpp +++ b/applications/irda/irda-runner.cpp @@ -2,8 +2,8 @@ extern "C" int32_t irda(void* p) { IrdaApp* app = new IrdaApp(); - app->run(); + int32_t result = app->run(p); delete app; - return 0; + return result; }