Archive show decide between binary and text viewer

This commit is contained in:
Willy-JL
2023-08-10 01:08:16 +02:00
parent d4e1d28187
commit c5700767b6
3 changed files with 30 additions and 153 deletions

View File

@@ -52,9 +52,32 @@ static void archive_loader_callback(const void* message, void* context) {
}
}
static void archive_show_file(Loader* loader, const char* path) {
File* file = storage_file_alloc(furi_record_open(RECORD_STORAGE));
bool text = true;
if(storage_file_open(file, path, FSAM_READ, FSOM_OPEN_EXISTING)) {
uint8_t buf[1000];
size_t read = storage_file_read(file, buf, sizeof(buf));
for(size_t i = 0; i < read; i++) {
const char c = buf[i];
if((c < ' ' || c > '~') && c != '\r' && c != '\n') {
text = false;
break;
}
}
}
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
if(text) {
loader_start_with_gui_error(loader, EXT_PATH("apps/Tools/text_viewer.fap"), path);
} else {
loader_start_with_gui_error(loader, EXT_PATH("apps/Tools/hex_viewer.fap"), path);
}
}
static void
archive_run_in_app(ArchiveBrowserView* browser, ArchiveFile_t* selected, bool favorites) {
UNUSED(browser);
Loader* loader = furi_record_open(RECORD_LOADER);
const char* app_name = archive_get_flipper_app_name(selected->type);
@@ -84,8 +107,10 @@ static void
loader_start_with_gui_error(loader, app_name, str);
}
}
} else {
} else if(selected->type == ArchiveFileTypeApplication) {
loader_start_with_gui_error(loader, furi_string_get_cstr(selected->path), NULL);
} else {
archive_show_file(loader, furi_string_get_cstr(selected->path));
}
furi_record_close(RECORD_LOADER);
@@ -176,10 +201,10 @@ bool archive_scene_browser_on_event(void* context, SceneManagerEvent event) {
consumed = true;
break;
case ArchiveBrowserEventFileMenuShow:
archive_show_file(
furi_record_open(RECORD_LOADER), furi_string_get_cstr(selected->path));
furi_record_close(RECORD_LOADER);
archive_show_file_menu(browser, false, false);
scene_manager_set_scene_state(
archive->scene_manager, ArchiveAppSceneBrowser, SCENE_STATE_NEED_REFRESH);
scene_manager_next_scene(archive->scene_manager, ArchiveAppSceneShow);
consumed = true;
break;
case ArchiveBrowserEventFileMenuPaste:

View File

@@ -4,4 +4,3 @@ ADD_SCENE(archive, rename, Rename)
ADD_SCENE(archive, delete, Delete)
ADD_SCENE(archive, search, Search)
ADD_SCENE(archive, info, Info)
ADD_SCENE(archive, show, Show)

View File

@@ -1,147 +0,0 @@
#include "../archive_i.h"
#include "../helpers/archive_browser.h"
#include <storage/storage.h>
#define TAG "Archive"
#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);
}
}
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();
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;
FS_Error error = storage_common_stat(fs_api, furi_string_get_cstr(current->path), &fileinfo);
if(error == FSE_OK) {
if((fileinfo.size < SHOW_MAX_FILE_SIZE) && (fileinfo.size > 2)) {
bool ok = storage_file_open(
file, furi_string_get_cstr(current->path), FSAM_READ, FSOM_OPEN_EXISTING);
if(ok) {
if(!text_show_read_lines(file, buffer)) {
goto text_file_read_err;
}
if(!furi_string_size(buffer)) {
goto text_file_read_err;
}
storage_file_seek(file, 0, true);
widget_add_text_scroll_element(
instance->widget, 0, 0, 128, 64, furi_string_get_cstr(buffer));
} else {
text_file_read_err:
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);
} 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);
} 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(
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);
furi_string_free(buffer);
storage_file_free(file);
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_previous_scene(app->scene_manager);
return true;
}
return false;
}
void archive_scene_show_on_exit(void* context) {
furi_assert(context);
ArchiveApp* app = (ArchiveApp*)context;
widget_reset(app->widget);
}