2022-05-27 11:19:21 +00:00
|
|
|
#include "file_browser_worker.h"
|
2022-07-20 10:56:33 +00:00
|
|
|
#include <core/check.h>
|
|
|
|
#include <core/common_defines.h>
|
2022-05-27 11:19:21 +00:00
|
|
|
#include "storage/filesystem_api_defines.h"
|
|
|
|
#include <m-array.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <storage/storage.h>
|
|
|
|
#include <furi.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include "toolbox/path.h"
|
|
|
|
|
|
|
|
#define TAG "BrowserWorker"
|
|
|
|
|
|
|
|
#define ASSETS_DIR "assets"
|
2022-07-26 12:21:51 +00:00
|
|
|
#define BROWSER_ROOT STORAGE_ANY_PATH_PREFIX
|
2022-05-27 11:19:21 +00:00
|
|
|
#define FILE_NAME_LEN_MAX 256
|
|
|
|
#define LONG_LOAD_THRESHOLD 100
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
WorkerEvtStop = (1 << 0),
|
|
|
|
WorkerEvtLoad = (1 << 1),
|
|
|
|
WorkerEvtFolderEnter = (1 << 2),
|
|
|
|
WorkerEvtFolderExit = (1 << 3),
|
2022-06-09 07:09:52 +00:00
|
|
|
WorkerEvtFolderRefresh = (1 << 4),
|
|
|
|
WorkerEvtConfigChange = (1 << 5),
|
2022-05-27 11:19:21 +00:00
|
|
|
} WorkerEvtFlags;
|
|
|
|
|
2022-06-09 07:09:52 +00:00
|
|
|
#define WORKER_FLAGS_ALL \
|
|
|
|
(WorkerEvtStop | WorkerEvtLoad | WorkerEvtFolderEnter | WorkerEvtFolderExit | \
|
|
|
|
WorkerEvtFolderRefresh | WorkerEvtConfigChange)
|
2022-05-27 11:19:21 +00:00
|
|
|
|
|
|
|
ARRAY_DEF(idx_last_array, int32_t)
|
|
|
|
|
|
|
|
struct BrowserWorker {
|
|
|
|
FuriThread* thread;
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* filter_extension;
|
2022-11-28 18:59:24 +00:00
|
|
|
FuriString* path_start;
|
|
|
|
FuriString* path_current;
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* path_next;
|
2022-05-27 11:19:21 +00:00
|
|
|
int32_t item_sel_idx;
|
|
|
|
uint32_t load_offset;
|
|
|
|
uint32_t load_count;
|
|
|
|
bool skip_assets;
|
2022-12-07 10:52:44 +00:00
|
|
|
bool hide_dot_files;
|
2022-05-27 11:19:21 +00:00
|
|
|
idx_last_array_t idx_last;
|
|
|
|
|
|
|
|
void* cb_ctx;
|
|
|
|
BrowserWorkerFolderOpenCallback folder_cb;
|
|
|
|
BrowserWorkerListLoadCallback list_load_cb;
|
|
|
|
BrowserWorkerListItemCallback list_item_cb;
|
|
|
|
BrowserWorkerLongLoadCallback long_load_cb;
|
|
|
|
};
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
static bool browser_path_is_file(FuriString* path) {
|
2022-05-27 11:19:21 +00:00
|
|
|
bool state = false;
|
|
|
|
FileInfo file_info;
|
2022-07-26 12:21:51 +00:00
|
|
|
Storage* storage = furi_record_open(RECORD_STORAGE);
|
2022-10-05 15:15:23 +00:00
|
|
|
if(storage_common_stat(storage, furi_string_get_cstr(path), &file_info) == FSE_OK) {
|
2022-05-27 11:19:21 +00:00
|
|
|
if((file_info.flags & FSF_DIRECTORY) == 0) {
|
|
|
|
state = true;
|
|
|
|
}
|
|
|
|
}
|
2022-07-26 12:21:51 +00:00
|
|
|
furi_record_close(RECORD_STORAGE);
|
2022-05-27 11:19:21 +00:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
static bool browser_path_trim(FuriString* path) {
|
2022-05-27 11:19:21 +00:00
|
|
|
bool is_root = false;
|
2022-10-05 15:15:23 +00:00
|
|
|
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);
|
2022-05-27 11:19:21 +00:00
|
|
|
is_root = true;
|
|
|
|
}
|
|
|
|
return is_root;
|
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
static bool browser_filter_by_name(BrowserWorker* browser, FuriString* name, bool is_folder) {
|
2022-12-07 10:52:44 +00:00
|
|
|
// Skip dot files if enabled
|
|
|
|
if(browser->hide_dot_files) {
|
|
|
|
if(furi_string_start_with_str(name, ".")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-27 11:19:21 +00:00
|
|
|
if(is_folder) {
|
|
|
|
// Skip assets folders (if enabled)
|
|
|
|
if(browser->skip_assets) {
|
2022-10-05 15:15:23 +00:00
|
|
|
return ((furi_string_cmp_str(name, ASSETS_DIR) == 0) ? (false) : (true));
|
2022-05-27 11:19:21 +00:00
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Filter files by extension
|
2022-10-05 15:15:23 +00:00
|
|
|
if((furi_string_empty(browser->filter_extension)) ||
|
|
|
|
(furi_string_cmp_str(browser->filter_extension, "*") == 0)) {
|
2022-05-27 11:19:21 +00:00
|
|
|
return true;
|
|
|
|
}
|
2022-10-05 15:15:23 +00:00
|
|
|
if(furi_string_end_with(name, browser->filter_extension)) {
|
2022-05-27 11:19:21 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
static bool browser_folder_check_and_switch(FuriString* path) {
|
2022-05-27 11:19:21 +00:00
|
|
|
FileInfo file_info;
|
2022-07-26 12:21:51 +00:00
|
|
|
Storage* storage = furi_record_open(RECORD_STORAGE);
|
2022-05-27 11:19:21 +00:00
|
|
|
bool is_root = false;
|
2022-08-18 16:54:17 +00:00
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
if(furi_string_search_rchar(path, '/') == 0) {
|
2022-08-18 16:54:17 +00:00
|
|
|
is_root = true;
|
|
|
|
}
|
|
|
|
|
2022-05-27 11:19:21 +00:00
|
|
|
while(1) {
|
|
|
|
// Check if folder is existing and navigate back if not
|
2022-10-05 15:15:23 +00:00
|
|
|
if(storage_common_stat(storage, furi_string_get_cstr(path), &file_info) == FSE_OK) {
|
2022-05-27 11:19:21 +00:00
|
|
|
if(file_info.flags & FSF_DIRECTORY) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(is_root) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
is_root = browser_path_trim(path);
|
|
|
|
}
|
2022-07-26 12:21:51 +00:00
|
|
|
furi_record_close(RECORD_STORAGE);
|
2022-05-27 11:19:21 +00:00
|
|
|
return is_root;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool browser_folder_init(
|
|
|
|
BrowserWorker* browser,
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* path,
|
|
|
|
FuriString* filename,
|
2022-05-27 11:19:21 +00:00
|
|
|
uint32_t* item_cnt,
|
|
|
|
int32_t* file_idx) {
|
|
|
|
bool state = false;
|
|
|
|
FileInfo file_info;
|
|
|
|
uint32_t total_files_cnt = 0;
|
|
|
|
|
2022-07-26 12:21:51 +00:00
|
|
|
Storage* storage = furi_record_open(RECORD_STORAGE);
|
2022-05-27 11:19:21 +00:00
|
|
|
File* directory = storage_file_alloc(storage);
|
|
|
|
|
|
|
|
char name_temp[FILE_NAME_LEN_MAX];
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* name_str;
|
|
|
|
name_str = furi_string_alloc();
|
2022-05-27 11:19:21 +00:00
|
|
|
|
|
|
|
*item_cnt = 0;
|
|
|
|
*file_idx = -1;
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
if(storage_dir_open(directory, furi_string_get_cstr(path))) {
|
2022-05-27 11:19:21 +00:00
|
|
|
state = true;
|
|
|
|
while(1) {
|
|
|
|
if(!storage_dir_read(directory, &file_info, name_temp, FILE_NAME_LEN_MAX)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if((storage_file_get_error(directory) == FSE_OK) && (name_temp[0] != '\0')) {
|
|
|
|
total_files_cnt++;
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_set(name_str, name_temp);
|
2022-05-27 11:19:21 +00:00
|
|
|
if(browser_filter_by_name(browser, name_str, (file_info.flags & FSF_DIRECTORY))) {
|
2022-10-05 15:15:23 +00:00
|
|
|
if(!furi_string_empty(filename)) {
|
|
|
|
if(furi_string_cmp(name_str, filename) == 0) {
|
2022-05-27 11:19:21 +00:00
|
|
|
*file_idx = *item_cnt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(*item_cnt)++;
|
|
|
|
}
|
|
|
|
if(total_files_cnt == LONG_LOAD_THRESHOLD) {
|
|
|
|
// There are too many files in folder and counting them will take some time - send callback to app
|
|
|
|
if(browser->long_load_cb) {
|
|
|
|
browser->long_load_cb(browser->cb_ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(name_str);
|
2022-05-27 11:19:21 +00:00
|
|
|
|
|
|
|
storage_dir_close(directory);
|
|
|
|
storage_file_free(directory);
|
|
|
|
|
2022-07-26 12:21:51 +00:00
|
|
|
furi_record_close(RECORD_STORAGE);
|
2022-05-27 11:19:21 +00:00
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2022-10-05 15:15:23 +00:00
|
|
|
browser_folder_load(BrowserWorker* browser, FuriString* path, uint32_t offset, uint32_t count) {
|
2022-05-27 11:19:21 +00:00
|
|
|
FileInfo file_info;
|
|
|
|
|
2022-07-26 12:21:51 +00:00
|
|
|
Storage* storage = furi_record_open(RECORD_STORAGE);
|
2022-05-27 11:19:21 +00:00
|
|
|
File* directory = storage_file_alloc(storage);
|
|
|
|
|
|
|
|
char name_temp[FILE_NAME_LEN_MAX];
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* name_str;
|
|
|
|
name_str = furi_string_alloc();
|
2022-05-27 11:19:21 +00:00
|
|
|
|
|
|
|
uint32_t items_cnt = 0;
|
|
|
|
|
|
|
|
do {
|
2022-10-05 15:15:23 +00:00
|
|
|
if(!storage_dir_open(directory, furi_string_get_cstr(path))) {
|
2022-05-27 11:19:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
items_cnt = 0;
|
|
|
|
while(items_cnt < offset) {
|
|
|
|
if(!storage_dir_read(directory, &file_info, name_temp, FILE_NAME_LEN_MAX)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(storage_file_get_error(directory) == FSE_OK) {
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_set(name_str, name_temp);
|
2022-05-27 11:19:21 +00:00
|
|
|
if(browser_filter_by_name(browser, name_str, (file_info.flags & FSF_DIRECTORY))) {
|
|
|
|
items_cnt++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(items_cnt != offset) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(browser->list_load_cb) {
|
|
|
|
browser->list_load_cb(browser->cb_ctx, offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
items_cnt = 0;
|
|
|
|
while(items_cnt < count) {
|
|
|
|
if(!storage_dir_read(directory, &file_info, name_temp, FILE_NAME_LEN_MAX)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(storage_file_get_error(directory) == FSE_OK) {
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_set(name_str, name_temp);
|
2022-05-27 11:19:21 +00:00
|
|
|
if(browser_filter_by_name(browser, name_str, (file_info.flags & FSF_DIRECTORY))) {
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_printf(name_str, "%s/%s", furi_string_get_cstr(path), name_temp);
|
2022-05-27 11:19:21 +00:00
|
|
|
if(browser->list_item_cb) {
|
|
|
|
browser->list_item_cb(
|
|
|
|
browser->cb_ctx, name_str, (file_info.flags & FSF_DIRECTORY), false);
|
|
|
|
}
|
|
|
|
items_cnt++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(browser->list_item_cb) {
|
|
|
|
browser->list_item_cb(browser->cb_ctx, NULL, false, true);
|
|
|
|
}
|
|
|
|
} while(0);
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(name_str);
|
2022-05-27 11:19:21 +00:00
|
|
|
|
|
|
|
storage_dir_close(directory);
|
|
|
|
storage_file_free(directory);
|
|
|
|
|
2022-07-26 12:21:51 +00:00
|
|
|
furi_record_close(RECORD_STORAGE);
|
2022-05-27 11:19:21 +00:00
|
|
|
|
|
|
|
return (items_cnt == count);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int32_t browser_worker(void* context) {
|
|
|
|
BrowserWorker* browser = (BrowserWorker*)context;
|
|
|
|
furi_assert(browser);
|
|
|
|
FURI_LOG_D(TAG, "Start");
|
|
|
|
|
|
|
|
uint32_t items_cnt = 0;
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* path;
|
|
|
|
path = furi_string_alloc_set(BROWSER_ROOT);
|
2022-05-27 11:19:21 +00:00
|
|
|
browser->item_sel_idx = -1;
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* filename;
|
|
|
|
filename = furi_string_alloc();
|
2022-05-27 11:19:21 +00:00
|
|
|
|
2022-06-20 14:54:48 +00:00
|
|
|
furi_thread_flags_set(furi_thread_get_id(browser->thread), WorkerEvtConfigChange);
|
2022-05-27 11:19:21 +00:00
|
|
|
|
|
|
|
while(1) {
|
2022-07-20 10:56:33 +00:00
|
|
|
uint32_t flags =
|
|
|
|
furi_thread_flags_wait(WORKER_FLAGS_ALL, FuriFlagWaitAny, FuriWaitForever);
|
|
|
|
furi_assert((flags & FuriFlagError) == 0);
|
2022-05-27 11:19:21 +00:00
|
|
|
|
2022-06-09 07:09:52 +00:00
|
|
|
if(flags & WorkerEvtConfigChange) {
|
|
|
|
// If start path is a path to the file - try finding index of this file in a folder
|
|
|
|
if(browser_path_is_file(browser->path_next)) {
|
|
|
|
path_extract_filename(browser->path_next, filename, false);
|
|
|
|
}
|
|
|
|
idx_last_array_reset(browser->idx_last);
|
|
|
|
|
2022-06-20 14:54:48 +00:00
|
|
|
furi_thread_flags_set(furi_thread_get_id(browser->thread), WorkerEvtFolderEnter);
|
2022-06-09 07:09:52 +00:00
|
|
|
}
|
|
|
|
|
2022-05-27 11:19:21 +00:00
|
|
|
if(flags & WorkerEvtFolderEnter) {
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_set(path, browser->path_next);
|
2022-05-27 11:19:21 +00:00
|
|
|
bool is_root = browser_folder_check_and_switch(path);
|
|
|
|
|
|
|
|
// Push previous selected item index to history array
|
|
|
|
idx_last_array_push_back(browser->idx_last, browser->item_sel_idx);
|
|
|
|
|
|
|
|
int32_t file_idx = 0;
|
|
|
|
browser_folder_init(browser, path, filename, &items_cnt, &file_idx);
|
2022-11-28 18:59:24 +00:00
|
|
|
furi_string_set(browser->path_current, path);
|
2022-05-27 11:19:21 +00:00
|
|
|
FURI_LOG_D(
|
|
|
|
TAG,
|
2022-10-07 13:35:15 +00:00
|
|
|
"Enter folder: %s items: %lu idx: %ld",
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_get_cstr(path),
|
2022-05-27 11:19:21 +00:00
|
|
|
items_cnt,
|
|
|
|
file_idx);
|
|
|
|
if(browser->folder_cb) {
|
|
|
|
browser->folder_cb(browser->cb_ctx, items_cnt, file_idx, is_root);
|
|
|
|
}
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_reset(filename);
|
2022-05-27 11:19:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(flags & WorkerEvtFolderExit) {
|
|
|
|
browser_path_trim(path);
|
|
|
|
bool is_root = browser_folder_check_and_switch(path);
|
|
|
|
|
|
|
|
int32_t file_idx = 0;
|
|
|
|
browser_folder_init(browser, path, filename, &items_cnt, &file_idx);
|
|
|
|
if(idx_last_array_size(browser->idx_last) > 0) {
|
|
|
|
// Pop previous selected item index from history array
|
|
|
|
idx_last_array_pop_back(&file_idx, browser->idx_last);
|
|
|
|
}
|
2022-11-28 18:59:24 +00:00
|
|
|
furi_string_set(browser->path_current, path);
|
2022-05-27 11:19:21 +00:00
|
|
|
FURI_LOG_D(
|
2022-10-05 15:15:23 +00:00
|
|
|
TAG,
|
2022-10-07 13:35:15 +00:00
|
|
|
"Exit to: %s items: %lu idx: %ld",
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_get_cstr(path),
|
|
|
|
items_cnt,
|
|
|
|
file_idx);
|
2022-05-27 11:19:21 +00:00
|
|
|
if(browser->folder_cb) {
|
|
|
|
browser->folder_cb(browser->cb_ctx, items_cnt, file_idx, is_root);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-09 07:09:52 +00:00
|
|
|
if(flags & WorkerEvtFolderRefresh) {
|
|
|
|
bool is_root = browser_folder_check_and_switch(path);
|
|
|
|
|
|
|
|
int32_t file_idx = 0;
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_reset(filename);
|
2022-06-09 07:09:52 +00:00
|
|
|
browser_folder_init(browser, path, filename, &items_cnt, &file_idx);
|
|
|
|
FURI_LOG_D(
|
|
|
|
TAG,
|
2022-10-07 13:35:15 +00:00
|
|
|
"Refresh folder: %s items: %lu idx: %ld",
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_get_cstr(path),
|
2022-06-09 07:09:52 +00:00
|
|
|
items_cnt,
|
|
|
|
browser->item_sel_idx);
|
|
|
|
if(browser->folder_cb) {
|
|
|
|
browser->folder_cb(browser->cb_ctx, items_cnt, browser->item_sel_idx, is_root);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-27 11:19:21 +00:00
|
|
|
if(flags & WorkerEvtLoad) {
|
2022-10-07 13:35:15 +00:00
|
|
|
FURI_LOG_D(
|
|
|
|
TAG, "Load offset: %lu cnt: %lu", browser->load_offset, browser->load_count);
|
2022-05-27 11:19:21 +00:00
|
|
|
browser_folder_load(browser, path, browser->load_offset, browser->load_count);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(flags & WorkerEvtStop) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(filename);
|
|
|
|
furi_string_free(path);
|
2022-05-27 11:19:21 +00:00
|
|
|
|
|
|
|
FURI_LOG_D(TAG, "End");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-12-07 10:52:44 +00:00
|
|
|
BrowserWorker* file_browser_worker_alloc(
|
|
|
|
FuriString* path,
|
|
|
|
const char* filter_ext,
|
|
|
|
bool skip_assets,
|
|
|
|
bool hide_dot_files) {
|
2022-11-05 15:07:24 +00:00
|
|
|
BrowserWorker* browser = malloc(sizeof(BrowserWorker)); //-V773
|
2022-05-27 11:19:21 +00:00
|
|
|
|
|
|
|
idx_last_array_init(browser->idx_last);
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
browser->filter_extension = furi_string_alloc_set(filter_ext);
|
2022-05-27 11:19:21 +00:00
|
|
|
browser->skip_assets = skip_assets;
|
2022-12-07 10:52:44 +00:00
|
|
|
browser->hide_dot_files = hide_dot_files;
|
2022-11-28 18:59:24 +00:00
|
|
|
browser->path_start = furi_string_alloc_set(path);
|
|
|
|
browser->path_current = furi_string_alloc_set(path);
|
2022-10-05 15:15:23 +00:00
|
|
|
browser->path_next = furi_string_alloc_set(path);
|
2022-05-27 11:19:21 +00:00
|
|
|
|
2022-11-28 18:59:24 +00:00
|
|
|
if(browser_path_is_file(browser->path_start)) {
|
|
|
|
browser_path_trim(browser->path_start);
|
|
|
|
}
|
|
|
|
|
2022-11-23 12:49:17 +00:00
|
|
|
browser->thread = furi_thread_alloc_ex("BrowserWorker", 2048, browser_worker, browser);
|
2022-05-27 11:19:21 +00:00
|
|
|
furi_thread_start(browser->thread);
|
|
|
|
|
|
|
|
return browser;
|
|
|
|
}
|
|
|
|
|
|
|
|
void file_browser_worker_free(BrowserWorker* browser) {
|
|
|
|
furi_assert(browser);
|
|
|
|
|
2022-06-20 14:54:48 +00:00
|
|
|
furi_thread_flags_set(furi_thread_get_id(browser->thread), WorkerEvtStop);
|
2022-05-27 11:19:21 +00:00
|
|
|
furi_thread_join(browser->thread);
|
|
|
|
furi_thread_free(browser->thread);
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(browser->filter_extension);
|
|
|
|
furi_string_free(browser->path_next);
|
2022-11-28 18:59:24 +00:00
|
|
|
furi_string_free(browser->path_current);
|
|
|
|
furi_string_free(browser->path_start);
|
2022-05-27 11:19:21 +00:00
|
|
|
|
|
|
|
idx_last_array_clear(browser->idx_last);
|
|
|
|
|
|
|
|
free(browser);
|
|
|
|
}
|
|
|
|
|
|
|
|
void file_browser_worker_set_callback_context(BrowserWorker* browser, void* context) {
|
|
|
|
furi_assert(browser);
|
|
|
|
browser->cb_ctx = context;
|
|
|
|
}
|
|
|
|
|
|
|
|
void file_browser_worker_set_folder_callback(
|
|
|
|
BrowserWorker* browser,
|
|
|
|
BrowserWorkerFolderOpenCallback cb) {
|
|
|
|
furi_assert(browser);
|
|
|
|
browser->folder_cb = cb;
|
|
|
|
}
|
|
|
|
|
|
|
|
void file_browser_worker_set_list_callback(
|
|
|
|
BrowserWorker* browser,
|
|
|
|
BrowserWorkerListLoadCallback cb) {
|
|
|
|
furi_assert(browser);
|
|
|
|
browser->list_load_cb = cb;
|
|
|
|
}
|
|
|
|
|
|
|
|
void file_browser_worker_set_item_callback(
|
|
|
|
BrowserWorker* browser,
|
|
|
|
BrowserWorkerListItemCallback cb) {
|
|
|
|
furi_assert(browser);
|
|
|
|
browser->list_item_cb = cb;
|
|
|
|
}
|
|
|
|
|
|
|
|
void file_browser_worker_set_long_load_callback(
|
|
|
|
BrowserWorker* browser,
|
|
|
|
BrowserWorkerLongLoadCallback cb) {
|
|
|
|
furi_assert(browser);
|
|
|
|
browser->long_load_cb = cb;
|
|
|
|
}
|
|
|
|
|
2022-06-09 07:09:52 +00:00
|
|
|
void file_browser_worker_set_config(
|
|
|
|
BrowserWorker* browser,
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* path,
|
2022-06-09 07:09:52 +00:00
|
|
|
const char* filter_ext,
|
2022-12-07 10:52:44 +00:00
|
|
|
bool skip_assets,
|
|
|
|
bool hide_dot_files) {
|
2022-06-09 07:09:52 +00:00
|
|
|
furi_assert(browser);
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_set(browser->path_next, path);
|
|
|
|
furi_string_set(browser->filter_extension, filter_ext);
|
2022-06-09 07:09:52 +00:00
|
|
|
browser->skip_assets = skip_assets;
|
2022-12-07 10:52:44 +00:00
|
|
|
browser->hide_dot_files = hide_dot_files;
|
2022-06-20 14:54:48 +00:00
|
|
|
furi_thread_flags_set(furi_thread_get_id(browser->thread), WorkerEvtConfigChange);
|
2022-06-09 07:09:52 +00:00
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
void file_browser_worker_folder_enter(BrowserWorker* browser, FuriString* path, int32_t item_idx) {
|
2022-05-27 11:19:21 +00:00
|
|
|
furi_assert(browser);
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_set(browser->path_next, path);
|
2022-05-27 11:19:21 +00:00
|
|
|
browser->item_sel_idx = item_idx;
|
2022-06-20 14:54:48 +00:00
|
|
|
furi_thread_flags_set(furi_thread_get_id(browser->thread), WorkerEvtFolderEnter);
|
2022-05-27 11:19:21 +00:00
|
|
|
}
|
|
|
|
|
2022-11-28 18:59:24 +00:00
|
|
|
bool file_browser_worker_is_in_start_folder(BrowserWorker* browser) {
|
|
|
|
furi_assert(browser);
|
|
|
|
return (furi_string_cmp(browser->path_start, browser->path_current) == 0);
|
|
|
|
}
|
|
|
|
|
2022-05-27 11:19:21 +00:00
|
|
|
void file_browser_worker_folder_exit(BrowserWorker* browser) {
|
|
|
|
furi_assert(browser);
|
2022-06-20 14:54:48 +00:00
|
|
|
furi_thread_flags_set(furi_thread_get_id(browser->thread), WorkerEvtFolderExit);
|
2022-05-27 11:19:21 +00:00
|
|
|
}
|
|
|
|
|
2022-06-09 07:09:52 +00:00
|
|
|
void file_browser_worker_folder_refresh(BrowserWorker* browser, int32_t item_idx) {
|
|
|
|
furi_assert(browser);
|
|
|
|
browser->item_sel_idx = item_idx;
|
2022-06-20 14:54:48 +00:00
|
|
|
furi_thread_flags_set(furi_thread_get_id(browser->thread), WorkerEvtFolderRefresh);
|
2022-06-09 07:09:52 +00:00
|
|
|
}
|
|
|
|
|
2022-05-27 11:19:21 +00:00
|
|
|
void file_browser_worker_load(BrowserWorker* browser, uint32_t offset, uint32_t count) {
|
|
|
|
furi_assert(browser);
|
|
|
|
browser->load_offset = offset;
|
|
|
|
browser->load_count = count;
|
2022-06-20 14:54:48 +00:00
|
|
|
furi_thread_flags_set(furi_thread_get_id(browser->thread), WorkerEvtLoad);
|
2022-05-27 11:19:21 +00:00
|
|
|
}
|