Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FL-2356] Infrared: Fix opening files outside app folder #1050

Merged
merged 2 commits into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions applications/infrared/infrared_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ int32_t InfraredApp::run(void* args) {
bool exit = false;

if(args) {
std::string full_name = static_cast<const char*>(args);
std::string remote_name(full_name, full_name.find_last_of('/') + 1, full_name.size());
std::string path = static_cast<const char*>(args);
std::string remote_name(path, path.find_last_of('/') + 1, path.size());
remote_name.erase(remote_name.find_last_of('.'));
bool result = remote_manager.load(remote_name);
path.erase(path.find_last_of('/'));
bool result = remote_manager.load(path, remote_name);
if(result) {
current_scene = InfraredApp::Scene::Remote;
} else {
Expand Down
34 changes: 19 additions & 15 deletions applications/infrared/infrared_app_remote_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@

static const std::string default_remote_name = "remote";

std::string InfraredAppRemoteManager::make_full_name(const std::string& remote_name) const {
return std::string("") + InfraredApp::infrared_directory + "/" + remote_name +
InfraredApp::infrared_extension;
std::string InfraredAppRemoteManager::make_full_name(
const std::string& path,
const std::string& remote_name) const {
return std::string("") + path + "/" + remote_name + InfraredApp::infrared_extension;
}

std::string InfraredAppRemoteManager::find_vacant_remote_name(const std::string& name) {
bool exist = true;
FileWorkerCpp file_worker;

if(!file_worker.is_file_exist(make_full_name(name).c_str(), &exist)) {
if(!file_worker.is_file_exist(
make_full_name(InfraredApp::infrared_directory, name).c_str(), &exist)) {
return std::string();
} else if(!exist) {
return name;
Expand All @@ -35,7 +37,7 @@ std::string InfraredAppRemoteManager::find_vacant_remote_name(const std::string&
bool file_worker_result = false;
std::string new_name;
do {
new_name = make_full_name(name + std::to_string(++i));
new_name = make_full_name(InfraredApp::infrared_directory, name + std::to_string(++i));
file_worker_result = file_worker.is_file_exist(new_name.c_str(), &exist);
} while(file_worker_result && exist);

Expand All @@ -57,7 +59,7 @@ bool InfraredAppRemoteManager::add_remote_with_button(
return false;
}

remote = std::make_unique<InfraredAppRemote>(new_name);
remote = std::make_unique<InfraredAppRemote>(InfraredApp::infrared_directory, new_name);
return add_button(button_name, signal);
}

Expand All @@ -84,7 +86,7 @@ const InfraredAppSignal& InfraredAppRemoteManager::get_button_data(size_t index)
bool InfraredAppRemoteManager::delete_remote() {
bool result;
FileWorkerCpp file_worker;
result = file_worker.remove(make_full_name(remote->name).c_str());
result = file_worker.remove(make_full_name(remote->path, remote->name).c_str());

reset_remote();
return result;
Expand Down Expand Up @@ -128,8 +130,8 @@ bool InfraredAppRemoteManager::rename_remote(const char* str) {
}

FileWorkerCpp file_worker;
std::string old_filename = make_full_name(remote->name);
std::string new_filename = make_full_name(new_name);
std::string old_filename = make_full_name(remote->path, remote->name);
std::string new_filename = make_full_name(remote->path, new_name);
bool result = file_worker.rename(old_filename.c_str(), new_filename.c_str());

remote->name = new_name;
Expand Down Expand Up @@ -160,8 +162,10 @@ bool InfraredAppRemoteManager::store(void) {
Storage* storage = static_cast<Storage*>(furi_record_open("storage"));
FlipperFormat* ff = flipper_format_file_alloc(storage);

FURI_LOG_I("RemoteManager", "store file: \'%s\'", make_full_name(remote->name).c_str());
result = flipper_format_file_open_always(ff, make_full_name(remote->name).c_str());
FURI_LOG_I(
"RemoteManager", "store file: \'%s\'", make_full_name(remote->path, remote->name).c_str());
result =
flipper_format_file_open_always(ff, make_full_name(remote->path, remote->name).c_str());
if(result) {
result = flipper_format_write_header_cstr(ff, "IR signals file", 1);
}
Expand All @@ -179,13 +183,13 @@ bool InfraredAppRemoteManager::store(void) {
return result;
}

bool InfraredAppRemoteManager::load(const std::string& remote_name) {
bool InfraredAppRemoteManager::load(const std::string& path, const std::string& remote_name) {
bool result = false;
Storage* storage = static_cast<Storage*>(furi_record_open("storage"));
FlipperFormat* ff = flipper_format_file_alloc(storage);

FURI_LOG_I("RemoteManager", "load file: \'%s\'", make_full_name(remote_name).c_str());
result = flipper_format_file_open_existing(ff, make_full_name(remote_name).c_str());
FURI_LOG_I("RemoteManager", "load file: \'%s\'", make_full_name(path, remote_name).c_str());
result = flipper_format_file_open_existing(ff, make_full_name(path, remote_name).c_str());
if(result) {
string_t header;
string_init(header);
Expand All @@ -197,7 +201,7 @@ bool InfraredAppRemoteManager::load(const std::string& remote_name) {
string_clear(header);
}
if(result) {
remote = std::make_unique<InfraredAppRemote>(remote_name);
remote = std::make_unique<InfraredAppRemote>(path, remote_name);
InfraredAppSignal signal;
std::string signal_name;
while(infrared_parser_read_signal(ff, signal, signal_name)) {
Expand Down
14 changes: 9 additions & 5 deletions applications/infrared/infrared_app_remote_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,18 @@ class InfraredAppRemote {
std::vector<InfraredAppRemoteButton> buttons;
/** Name of remote */
std::string name;
/** Path to remote file */
std::string path;

public:
/** Initialize new remote
*
*
* @param path - remote file path
* @param name - new remote name
*/
InfraredAppRemote(const std::string& name)
: name(name) {
InfraredAppRemote(const std::string& path, const std::string& name)
: name(name)
, path(path) {
}
};

Expand All @@ -79,7 +83,7 @@ class InfraredAppRemoteManager {
* @param remote_name name of remote
* @retval full name of remote on disk
*/
std::string make_full_name(const std::string& remote_name) const;
std::string make_full_name(const std::string& path, const std::string& remote_name) const;

public:
/** Restriction to button name length. Buttons larger are ignored. */
Expand Down Expand Up @@ -184,5 +188,5 @@ class InfraredAppRemoteManager {
* @param name - name of remote to load
* @retval true if success, false otherwise
*/
bool load(const std::string& name);
bool load(const std::string& path, const std::string& name);
};
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void InfraredAppSceneRemoteList::on_enter(InfraredApp* app) {
last_selected_remote_name);

if(file_select_result) {
if(remote_manager->load(std::string(filename_ts->text))) {
if(remote_manager->load(InfraredApp::infrared_directory, std::string(filename_ts->text))) {
app->switch_to_next_scene(InfraredApp::Scene::Remote);
result = true;
}
Expand Down