unleashed-firmware/applications/main/archive/scenes/archive_scene_show.c

148 lines
4.3 KiB
C
Raw Normal View History

#include "../archive_i.h"
#include "../helpers/archive_browser.h"
#include <storage/storage.h>
#define TAG "Archive"
2022-10-28 14:16:47 +00:00
#define SHOW_MAX_FILE_SIZE 5000
void archive_scene_show_widget_callback(GuiButtonType result, InputType type, void* context) {
furi_assert(context);
ArchiveApp* app = (ArchiveApp*)context;
if(type == InputTypeShort) {
view_dispatcher_send_custom_event(app->view_dispatcher, result);
}
}
2022-10-28 14:16:47 +00:00
static bool text_show_read_lines(File* file, FuriString* str_result) {
//furi_string_reset(str_result);
uint8_t buffer[SHOW_MAX_FILE_SIZE];
uint16_t read_count = storage_file_read(file, buffer, SHOW_MAX_FILE_SIZE);
if(storage_file_get_error(file) != FSE_OK) {
return false;
}
for(uint16_t i = 0; i < read_count; i++) {
furi_string_push_back(str_result, buffer[i]);
}
return true;
}
void archive_scene_show_on_enter(void* context) {
furi_assert(context);
ArchiveApp* instance = context;
FuriString* filename;
filename = furi_string_alloc();
2022-10-28 14:16:47 +00:00
FuriString* buffer;
buffer = furi_string_alloc();
ArchiveFile_t* current = archive_get_current_file(instance->browser);
Storage* fs_api = furi_record_open(RECORD_STORAGE);
File* file = storage_file_alloc(fs_api);
FileInfo fileinfo;
2022-10-28 13:05:17 +00:00
FS_Error error = storage_common_stat(fs_api, furi_string_get_cstr(current->path), &fileinfo);
if(error == FSE_OK) {
2022-10-28 14:16:47 +00:00
if((fileinfo.size < SHOW_MAX_FILE_SIZE) && (fileinfo.size > 2)) {
2022-10-28 13:05:17 +00:00
bool ok = storage_file_open(
file, furi_string_get_cstr(current->path), FSAM_READ, FSOM_OPEN_EXISTING);
if(ok) {
2022-10-28 14:16:47 +00:00
if(!text_show_read_lines(file, buffer)) {
goto text_file_read_err;
}
if(!furi_string_size(buffer)) {
goto text_file_read_err;
}
2022-10-28 13:05:17 +00:00
2022-10-28 14:16:47 +00:00
storage_file_seek(file, 0, true);
2022-10-28 13:05:17 +00:00
2022-10-28 14:16:47 +00:00
widget_add_text_scroll_element(
instance->widget, 0, 0, 128, 64, furi_string_get_cstr(buffer));
2022-10-28 13:05:17 +00:00
} else {
2022-10-28 14:16:47 +00:00
text_file_read_err:
2022-10-28 13:05:17 +00:00
widget_add_text_box_element(
instance->widget,
0,
0,
128,
64,
AlignLeft,
AlignCenter,
"\e#Error:\nStorage file open error\e#",
false);
}
storage_file_close(file);
2022-10-28 14:16:47 +00:00
} else if(fileinfo.size < 2) {
widget_add_text_box_element(
instance->widget,
0,
0,
128,
64,
AlignLeft,
AlignCenter,
"\e#Error:\nFile is too small\e#",
false);
2022-10-28 13:05:17 +00:00
} else {
widget_add_text_box_element(
instance->widget,
0,
0,
128,
64,
AlignLeft,
AlignCenter,
"\e#Error:\nFile is too large to show\e#",
false);
}
} else {
widget_add_text_box_element(
2022-10-28 13:05:17 +00:00
instance->widget,
0,
0,
128,
64,
AlignLeft,
AlignCenter,
"\e#Error:\nFile system error\e#",
false);
}
path_extract_filename(current->path, filename, false);
// This one to return and cursor select this file
path_extract_filename_no_ext(furi_string_get_cstr(current->path), filename);
strlcpy(instance->text_store, furi_string_get_cstr(filename), MAX_NAME_LEN);
2022-10-28 14:16:47 +00:00
furi_string_free(buffer);
storage_file_free(file);
2022-10-28 05:24:09 +00:00
furi_record_close(RECORD_STORAGE);
furi_string_free(filename);
view_dispatcher_switch_to_view(instance->view_dispatcher, ArchiveViewWidget);
}
bool archive_scene_show_on_event(void* context, SceneManagerEvent event) {
furi_assert(context);
ArchiveApp* app = (ArchiveApp*)context;
if(event.type == SceneManagerEventTypeCustom) {
scene_manager_next_scene(app->scene_manager, ArchiveAppSceneBrowser);
return true;
}
return false;
}
void archive_scene_show_on_exit(void* context) {
furi_assert(context);
ArchiveApp* app = (ArchiveApp*)context;
widget_reset(app->widget);
}