Skip to content

Commit

Permalink
[FL-1470] Support archive for IRDA (#582)
Browse files Browse the repository at this point in the history
Co-authored-by: あく <[email protected]>
  • Loading branch information
Albert Kharisov and skotopes authored Jul 16, 2021
1 parent 13c5a8c commit 30ae16c
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 22 deletions.
1 change: 1 addition & 0 deletions applications/irda/irda-app-event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class IrdaAppEvent {
public:
enum class Type : uint8_t {
Tick,
Exit,
Back,
MenuSelected,
DialogExSelected,
Expand Down
37 changes: 25 additions & 12 deletions applications/irda/irda-app-remote-manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> IrdaAppRemoteManager::get_button_list(void) const {
std::vector<std::string> name_vector;
name_vector.reserve(remote->buttons.size());
Expand All @@ -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");
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -233,20 +236,30 @@ bool IrdaAppRemoteManager::get_remote_list(std::vector<std::string>& 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<IrdaAppRemote>(name);
remote = std::make_unique<IrdaAppRemote>(remote_name);

while(1) {
auto file_signal = file_parser.read_signal(&file);
Expand Down
8 changes: 5 additions & 3 deletions applications/irda/irda-app-remote-manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class IrdaAppRemote {
std::vector<IrdaAppRemoteButton> 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;
Expand All @@ -38,7 +39,8 @@ class IrdaAppRemoteManager {
static const char* irda_directory;
static const char* irda_extension;
std::unique_ptr<IrdaAppRemote> 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);
Expand All @@ -58,7 +60,7 @@ class IrdaAppRemoteManager {
bool delete_remote();

bool store();
bool load(const std::string& name);
bool load(const std::string& name, bool fullpath = false);
bool check_fs() const;
};

32 changes: 28 additions & 4 deletions applications/irda/irda-app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,29 @@
#include <stdio.h>
#include <callback-connector.h>

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<const char*>(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) {
Expand All @@ -26,6 +39,8 @@ void IrdaApp::run(void) {
};

scenes[current_scene]->on_exit(this);

return 0;
};

IrdaAppViewManager* IrdaApp::get_view_manager() {
Expand Down Expand Up @@ -59,6 +74,9 @@ void IrdaApp::search_and_switch_to_previous_scene(const std::initializer_list<Sc

while(!scene_found) {
previous_scene = get_previous_scene();

if(previous_scene == Scene::Exit) break;

for(Scene element : scenes_list) {
if(previous_scene == element) {
scene_found = true;
Expand All @@ -67,9 +85,15 @@ void IrdaApp::search_and_switch_to_previous_scene(const std::initializer_list<Sc
}
}

scenes[current_scene]->on_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) {
Expand Down
2 changes: 1 addition & 1 deletion applications/irda/irda-app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class IrdaApp {
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);
Expand Down
4 changes: 2 additions & 2 deletions applications/irda/irda-runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 30ae16c

Please sign in to comment.