1
mirror of https://github.com/DarkFlippers/unleashed-firmware.git synced 2025-12-12 12:42:30 +04:00

Merge branch 'fz-dev' into dev

This commit is contained in:
MX
2022-10-05 19:27:46 +03:00
368 changed files with 5461 additions and 3819 deletions

View File

@@ -1,7 +1,6 @@
#include "file_browser_worker.h"
#include <core/check.h>
#include <core/common_defines.h>
#include "m-string.h"
#include "storage/filesystem_api_defines.h"
#include <m-array.h>
#include <stdbool.h>
@@ -37,8 +36,8 @@ ARRAY_DEF(idx_last_array, int32_t)
struct BrowserWorker {
FuriThread* thread;
string_t filter_extension;
string_t path_next;
FuriString* filter_extension;
FuriString* path_next;
int32_t item_sel_idx;
uint32_t load_offset;
uint32_t load_count;
@@ -52,11 +51,11 @@ struct BrowserWorker {
BrowserWorkerLongLoadCallback long_load_cb;
};
static bool browser_path_is_file(string_t path) {
static bool browser_path_is_file(FuriString* path) {
bool state = false;
FileInfo file_info;
Storage* storage = furi_record_open(RECORD_STORAGE);
if(storage_common_stat(storage, string_get_cstr(path), &file_info) == FSE_OK) {
if(storage_common_stat(storage, furi_string_get_cstr(path), &file_info) == FSE_OK) {
if((file_info.flags & FSF_DIRECTORY) == 0) {
state = true;
}
@@ -65,18 +64,18 @@ static bool browser_path_is_file(string_t path) {
return state;
}
static bool browser_path_trim(string_t path) {
static bool browser_path_trim(FuriString* path) {
bool is_root = false;
size_t filename_start = string_search_rchar(path, '/');
string_left(path, filename_start);
if((string_empty_p(path)) || (filename_start == STRING_FAILURE)) {
string_set_str(path, BROWSER_ROOT);
size_t filename_start = furi_string_search_rchar(path, '/');
furi_string_left(path, filename_start);
if((furi_string_empty(path)) || (filename_start == FURI_STRING_FAILURE)) {
furi_string_set(path, BROWSER_ROOT);
is_root = true;
}
return is_root;
}
static bool browser_filter_by_name(BrowserWorker* browser, string_t name, bool is_folder) {
static bool browser_filter_by_name(BrowserWorker* browser, FuriString* name, bool is_folder) {
if(is_folder) {
// Skip assets folders (if enabled)
if(browser->skip_assets) {
@@ -88,29 +87,29 @@ static bool browser_filter_by_name(BrowserWorker* browser, string_t name, bool i
}
} else {
// Filter files by extension
if((string_empty_p(browser->filter_extension)) ||
(string_cmp_str(browser->filter_extension, "*") == 0)) {
if((furi_string_empty(browser->filter_extension)) ||
(furi_string_cmp_str(browser->filter_extension, "*") == 0)) {
return true;
}
if(string_end_with_string_p(name, browser->filter_extension)) {
if(furi_string_end_with(name, browser->filter_extension)) {
return true;
}
}
return false;
}
static bool browser_folder_check_and_switch(string_t path) {
static bool browser_folder_check_and_switch(FuriString* path) {
FileInfo file_info;
Storage* storage = furi_record_open(RECORD_STORAGE);
bool is_root = false;
if(string_search_rchar(path, '/') == 0) {
if(furi_string_search_rchar(path, '/') == 0) {
is_root = true;
}
while(1) {
// Check if folder is existing and navigate back if not
if(storage_common_stat(storage, string_get_cstr(path), &file_info) == FSE_OK) {
if(storage_common_stat(storage, furi_string_get_cstr(path), &file_info) == FSE_OK) {
if(file_info.flags & FSF_DIRECTORY) {
break;
}
@@ -126,8 +125,8 @@ static bool browser_folder_check_and_switch(string_t path) {
static bool browser_folder_init(
BrowserWorker* browser,
string_t path,
string_t filename,
FuriString* path,
FuriString* filename,
uint32_t* item_cnt,
int32_t* file_idx) {
bool state = false;
@@ -138,13 +137,13 @@ static bool browser_folder_init(
File* directory = storage_file_alloc(storage);
char name_temp[FILE_NAME_LEN_MAX];
string_t name_str;
string_init(name_str);
FuriString* name_str;
name_str = furi_string_alloc();
*item_cnt = 0;
*file_idx = -1;
if(storage_dir_open(directory, string_get_cstr(path))) {
if(storage_dir_open(directory, furi_string_get_cstr(path))) {
state = true;
while(1) {
if(!storage_dir_read(directory, &file_info, name_temp, FILE_NAME_LEN_MAX)) {
@@ -152,10 +151,10 @@ static bool browser_folder_init(
}
if((storage_file_get_error(directory) == FSE_OK) && (name_temp[0] != '\0')) {
total_files_cnt++;
string_set_str(name_str, name_temp);
furi_string_set(name_str, name_temp);
if(browser_filter_by_name(browser, name_str, (file_info.flags & FSF_DIRECTORY))) {
if(!string_empty_p(filename)) {
if(string_cmp(name_str, filename) == 0) {
if(!furi_string_empty(filename)) {
if(furi_string_cmp(name_str, filename) == 0) {
*file_idx = *item_cnt;
}
}
@@ -171,7 +170,7 @@ static bool browser_folder_init(
}
}
string_clear(name_str);
furi_string_free(name_str);
storage_dir_close(directory);
storage_file_free(directory);
@@ -182,20 +181,20 @@ static bool browser_folder_init(
}
static bool
browser_folder_load(BrowserWorker* browser, string_t path, uint32_t offset, uint32_t count) {
browser_folder_load(BrowserWorker* browser, FuriString* path, uint32_t offset, uint32_t count) {
FileInfo file_info;
Storage* storage = furi_record_open(RECORD_STORAGE);
File* directory = storage_file_alloc(storage);
char name_temp[FILE_NAME_LEN_MAX];
string_t name_str;
string_init(name_str);
FuriString* name_str;
name_str = furi_string_alloc();
uint32_t items_cnt = 0;
do {
if(!storage_dir_open(directory, string_get_cstr(path))) {
if(!storage_dir_open(directory, furi_string_get_cstr(path))) {
break;
}
@@ -205,7 +204,7 @@ static bool
break;
}
if(storage_file_get_error(directory) == FSE_OK) {
string_set_str(name_str, name_temp);
furi_string_set(name_str, name_temp);
if(browser_filter_by_name(browser, name_str, (file_info.flags & FSF_DIRECTORY))) {
items_cnt++;
}
@@ -227,9 +226,9 @@ static bool
break;
}
if(storage_file_get_error(directory) == FSE_OK) {
string_set_str(name_str, name_temp);
furi_string_set(name_str, name_temp);
if(browser_filter_by_name(browser, name_str, (file_info.flags & FSF_DIRECTORY))) {
string_printf(name_str, "%s/%s", string_get_cstr(path), name_temp);
furi_string_printf(name_str, "%s/%s", furi_string_get_cstr(path), name_temp);
if(browser->list_item_cb) {
browser->list_item_cb(
browser->cb_ctx, name_str, (file_info.flags & FSF_DIRECTORY), false);
@@ -245,7 +244,7 @@ static bool
}
} while(0);
string_clear(name_str);
furi_string_free(name_str);
storage_dir_close(directory);
storage_file_free(directory);
@@ -261,12 +260,12 @@ static int32_t browser_worker(void* context) {
FURI_LOG_D(TAG, "Start");
uint32_t items_cnt = 0;
string_t path;
string_init_set_str(path, BROWSER_ROOT);
FuriString* path;
path = furi_string_alloc_set(BROWSER_ROOT);
browser->item_sel_idx = -1;
string_t filename;
string_init(filename);
FuriString* filename;
filename = furi_string_alloc();
furi_thread_flags_set(furi_thread_get_id(browser->thread), WorkerEvtConfigChange);
@@ -286,7 +285,7 @@ static int32_t browser_worker(void* context) {
}
if(flags & WorkerEvtFolderEnter) {
string_set(path, browser->path_next);
furi_string_set(path, browser->path_next);
bool is_root = browser_folder_check_and_switch(path);
// Push previous selected item index to history array
@@ -297,13 +296,13 @@ static int32_t browser_worker(void* context) {
FURI_LOG_D(
TAG,
"Enter folder: %s items: %u idx: %d",
string_get_cstr(path),
furi_string_get_cstr(path),
items_cnt,
file_idx);
if(browser->folder_cb) {
browser->folder_cb(browser->cb_ctx, items_cnt, file_idx, is_root);
}
string_reset(filename);
furi_string_reset(filename);
}
if(flags & WorkerEvtFolderExit) {
@@ -317,7 +316,11 @@ static int32_t browser_worker(void* context) {
idx_last_array_pop_back(&file_idx, browser->idx_last);
}
FURI_LOG_D(
TAG, "Exit to: %s items: %u idx: %d", string_get_cstr(path), items_cnt, file_idx);
TAG,
"Exit to: %s items: %u idx: %d",
furi_string_get_cstr(path),
items_cnt,
file_idx);
if(browser->folder_cb) {
browser->folder_cb(browser->cb_ctx, items_cnt, file_idx, is_root);
}
@@ -327,12 +330,12 @@ static int32_t browser_worker(void* context) {
bool is_root = browser_folder_check_and_switch(path);
int32_t file_idx = 0;
string_reset(filename);
furi_string_reset(filename);
browser_folder_init(browser, path, filename, &items_cnt, &file_idx);
FURI_LOG_D(
TAG,
"Refresh folder: %s items: %u idx: %d",
string_get_cstr(path),
furi_string_get_cstr(path),
items_cnt,
browser->item_sel_idx);
if(browser->folder_cb) {
@@ -350,21 +353,22 @@ static int32_t browser_worker(void* context) {
}
}
string_clear(filename);
string_clear(path);
furi_string_free(filename);
furi_string_free(path);
FURI_LOG_D(TAG, "End");
return 0;
}
BrowserWorker* file_browser_worker_alloc(string_t path, const char* filter_ext, bool skip_assets) {
BrowserWorker*
file_browser_worker_alloc(FuriString* path, const char* filter_ext, bool skip_assets) {
BrowserWorker* browser = malloc(sizeof(BrowserWorker));
idx_last_array_init(browser->idx_last);
string_init_set_str(browser->filter_extension, filter_ext);
browser->filter_extension = furi_string_alloc_set(filter_ext);
browser->skip_assets = skip_assets;
string_init_set(browser->path_next, path);
browser->path_next = furi_string_alloc_set(path);
browser->thread = furi_thread_alloc();
furi_thread_set_name(browser->thread, "BrowserWorker");
@@ -383,8 +387,8 @@ void file_browser_worker_free(BrowserWorker* browser) {
furi_thread_join(browser->thread);
furi_thread_free(browser->thread);
string_clear(browser->filter_extension);
string_clear(browser->path_next);
furi_string_free(browser->filter_extension);
furi_string_free(browser->path_next);
idx_last_array_clear(browser->idx_last);
@@ -426,19 +430,19 @@ void file_browser_worker_set_long_load_callback(
void file_browser_worker_set_config(
BrowserWorker* browser,
string_t path,
FuriString* path,
const char* filter_ext,
bool skip_assets) {
furi_assert(browser);
string_set(browser->path_next, path);
string_set_str(browser->filter_extension, filter_ext);
furi_string_set(browser->path_next, path);
furi_string_set(browser->filter_extension, filter_ext);
browser->skip_assets = skip_assets;
furi_thread_flags_set(furi_thread_get_id(browser->thread), WorkerEvtConfigChange);
}
void file_browser_worker_folder_enter(BrowserWorker* browser, string_t path, int32_t item_idx) {
void file_browser_worker_folder_enter(BrowserWorker* browser, FuriString* path, int32_t item_idx) {
furi_assert(browser);
string_set(browser->path_next, path);
furi_string_set(browser->path_next, path);
browser->item_sel_idx = item_idx;
furi_thread_flags_set(furi_thread_get_id(browser->thread), WorkerEvtFolderEnter);
}