Archive refactoring WIP (#688)
* view_dispatcher queue * refactoring, all works * scenes Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
171
applications/archive/helpers/archive_favorites.c
Normal file
171
applications/archive/helpers/archive_favorites.c
Normal file
@@ -0,0 +1,171 @@
|
||||
#include "archive_favorites.h"
|
||||
#include "archive_files.h"
|
||||
#include "../views/archive_main_view.h"
|
||||
|
||||
bool archive_favorites_read(void* context) {
|
||||
furi_assert(context);
|
||||
|
||||
ArchiveMainView* archive_view = context;
|
||||
FileWorker* file_worker = file_worker_alloc(true);
|
||||
|
||||
string_t buffer;
|
||||
FileInfo file_info;
|
||||
string_init(buffer);
|
||||
|
||||
bool result = file_worker_open(file_worker, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_ALWAYS);
|
||||
|
||||
if(result) {
|
||||
while(1) {
|
||||
if(!file_worker_read_until(file_worker, buffer, '\n')) {
|
||||
break;
|
||||
}
|
||||
if(!string_size(buffer)) {
|
||||
break;
|
||||
}
|
||||
|
||||
archive_view_add_item(archive_view, &file_info, string_get_cstr(buffer));
|
||||
string_clean(buffer);
|
||||
}
|
||||
}
|
||||
string_clear(buffer);
|
||||
file_worker_close(file_worker);
|
||||
file_worker_free(file_worker);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool archive_favorites_delete(const char* file_path, const char* name) {
|
||||
furi_assert(file_path);
|
||||
furi_assert(name);
|
||||
|
||||
FileWorker* file_worker = file_worker_alloc(true);
|
||||
|
||||
string_t path;
|
||||
string_t buffer;
|
||||
string_init(buffer);
|
||||
|
||||
string_init_printf(path, "%s/%s", file_path, name);
|
||||
|
||||
bool result = file_worker_open(file_worker, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
|
||||
if(result) {
|
||||
while(1) {
|
||||
if(!file_worker_read_until(file_worker, buffer, '\n')) {
|
||||
break;
|
||||
}
|
||||
if(!string_size(buffer)) {
|
||||
break;
|
||||
}
|
||||
|
||||
if(string_search(buffer, path)) {
|
||||
string_t temp;
|
||||
string_init_printf(temp, "%s\r\n", string_get_cstr(buffer));
|
||||
archive_file_append(ARCHIVE_FAV_TEMP_PATH, temp);
|
||||
string_clear(temp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
string_clear(buffer);
|
||||
string_clear(path);
|
||||
|
||||
file_worker_close(file_worker);
|
||||
file_worker_remove(file_worker, ARCHIVE_FAV_PATH);
|
||||
file_worker_rename(file_worker, ARCHIVE_FAV_TEMP_PATH, ARCHIVE_FAV_PATH);
|
||||
|
||||
file_worker_free(file_worker);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool archive_is_favorite(const char* file_path, const char* name) {
|
||||
furi_assert(file_path);
|
||||
furi_assert(name);
|
||||
|
||||
FileWorker* file_worker = file_worker_alloc(true);
|
||||
|
||||
string_t path;
|
||||
string_t buffer;
|
||||
string_init(buffer);
|
||||
bool found = false;
|
||||
|
||||
string_init_printf(path, "%s/%s", file_path, name);
|
||||
bool result = file_worker_open(file_worker, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_ALWAYS);
|
||||
|
||||
if(result) {
|
||||
while(1) {
|
||||
if(!file_worker_read_until(file_worker, buffer, '\n')) {
|
||||
break;
|
||||
}
|
||||
if(!string_size(buffer)) {
|
||||
break;
|
||||
}
|
||||
if(!string_search(buffer, path)) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
string_clear(buffer);
|
||||
string_clear(path);
|
||||
file_worker_close(file_worker);
|
||||
file_worker_free(file_worker);
|
||||
|
||||
return found;
|
||||
}
|
||||
|
||||
bool archive_favorites_rename(const char* file_path, const char* src, const char* dst) {
|
||||
furi_assert(file_path);
|
||||
furi_assert(src);
|
||||
furi_assert(dst);
|
||||
|
||||
FileWorker* file_worker = file_worker_alloc(true);
|
||||
|
||||
string_t path;
|
||||
string_t buffer;
|
||||
string_t temp;
|
||||
|
||||
string_init(buffer);
|
||||
string_init(temp);
|
||||
string_init(path);
|
||||
|
||||
string_printf(path, "%s/%s", file_path, src);
|
||||
bool result = file_worker_open(file_worker, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
|
||||
|
||||
if(result) {
|
||||
while(1) {
|
||||
if(!file_worker_read_until(file_worker, buffer, '\n')) {
|
||||
break;
|
||||
}
|
||||
if(!string_size(buffer)) {
|
||||
break;
|
||||
}
|
||||
string_printf(
|
||||
temp, "%s\r\n", string_search(buffer, path) ? string_get_cstr(buffer) : dst);
|
||||
archive_file_append(ARCHIVE_FAV_TEMP_PATH, temp);
|
||||
string_clean(temp);
|
||||
}
|
||||
}
|
||||
|
||||
string_clear(temp);
|
||||
string_clear(buffer);
|
||||
string_clear(path);
|
||||
|
||||
file_worker_close(file_worker);
|
||||
file_worker_remove(file_worker, ARCHIVE_FAV_PATH);
|
||||
file_worker_rename(file_worker, ARCHIVE_FAV_TEMP_PATH, ARCHIVE_FAV_PATH);
|
||||
|
||||
file_worker_free(file_worker);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void archive_add_to_favorites(const char* file_path, const char* name) {
|
||||
furi_assert(file_path);
|
||||
furi_assert(name);
|
||||
|
||||
string_t buffer_src;
|
||||
|
||||
string_init_printf(buffer_src, "%s/%s\r\n", file_path, name);
|
||||
archive_file_append(ARCHIVE_FAV_PATH, buffer_src);
|
||||
string_clear(buffer_src);
|
||||
}
|
11
applications/archive/helpers/archive_favorites.h
Normal file
11
applications/archive/helpers/archive_favorites.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
#include "file-worker.h"
|
||||
|
||||
#define ARCHIVE_FAV_PATH "/any/favorites.txt"
|
||||
#define ARCHIVE_FAV_TEMP_PATH "/any/favorites.tmp"
|
||||
|
||||
bool archive_favorites_read(void* context);
|
||||
bool archive_favorites_delete(const char* file_path, const char* name);
|
||||
bool archive_is_favorite(const char* file_path, const char* name);
|
||||
bool archive_favorites_rename(const char* file_path, const char* src, const char* dst);
|
||||
void archive_add_to_favorites(const char* file_path, const char* name);
|
143
applications/archive/helpers/archive_files.c
Normal file
143
applications/archive/helpers/archive_files.c
Normal file
@@ -0,0 +1,143 @@
|
||||
#include "archive_files.h"
|
||||
#include "archive_favorites.h"
|
||||
#include "../archive_i.h"
|
||||
|
||||
bool filter_by_extension(FileInfo* file_info, const char* tab_ext, const char* name) {
|
||||
furi_assert(file_info);
|
||||
furi_assert(tab_ext);
|
||||
furi_assert(name);
|
||||
|
||||
bool result = false;
|
||||
|
||||
if(strcmp(tab_ext, "*") == 0) {
|
||||
result = true;
|
||||
} else if(strstr(name, tab_ext) != NULL) {
|
||||
result = true;
|
||||
} else if(file_info->flags & FSF_DIRECTORY) {
|
||||
result = true;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void archive_trim_file_ext(char* name) {
|
||||
size_t str_len = strlen(name);
|
||||
char* end = name + str_len;
|
||||
while(end > name && *end != '.' && *end != '\\' && *end != '/') {
|
||||
--end;
|
||||
}
|
||||
if((end > name && *end == '.') && (*(end - 1) != '\\' && *(end - 1) != '/')) {
|
||||
*end = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
void set_file_type(ArchiveFile_t* file, FileInfo* file_info) {
|
||||
furi_assert(file);
|
||||
furi_assert(file_info);
|
||||
|
||||
for(size_t i = 0; i < SIZEOF_ARRAY(known_ext); i++) {
|
||||
if(string_search_str(file->name, known_ext[i], 0) != STRING_FAILURE) {
|
||||
file->type = i;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if(file_info->flags & FSF_DIRECTORY) {
|
||||
file->type = ArchiveFileTypeFolder;
|
||||
} else {
|
||||
file->type = ArchiveFileTypeUnknown;
|
||||
}
|
||||
}
|
||||
|
||||
bool archive_get_filenames(void* context, uint8_t tab_id, const char* path) {
|
||||
furi_assert(context);
|
||||
|
||||
ArchiveMainView* main_view = context;
|
||||
archive_file_array_clean(main_view);
|
||||
|
||||
if(tab_id != ArchiveTabFavorites) {
|
||||
archive_read_dir(main_view, path);
|
||||
} else {
|
||||
archive_favorites_read(main_view);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool archive_read_dir(void* context, const char* path) {
|
||||
furi_assert(context);
|
||||
|
||||
ArchiveMainView* main_view = context;
|
||||
FileInfo file_info;
|
||||
Storage* fs_api = furi_record_open("storage");
|
||||
File* directory = storage_file_alloc(fs_api);
|
||||
char name[MAX_NAME_LEN];
|
||||
|
||||
if(!storage_dir_open(directory, path)) {
|
||||
storage_dir_close(directory);
|
||||
storage_file_free(directory);
|
||||
return false;
|
||||
}
|
||||
|
||||
while(1) {
|
||||
if(!storage_dir_read(directory, &file_info, name, MAX_NAME_LEN)) {
|
||||
break;
|
||||
}
|
||||
|
||||
uint16_t files_cnt = archive_file_array_size(main_view);
|
||||
|
||||
if(files_cnt > MAX_FILES) {
|
||||
break;
|
||||
} else if(storage_file_get_error(directory) == FSE_OK) {
|
||||
archive_view_add_item(main_view, &file_info, name);
|
||||
} else {
|
||||
storage_dir_close(directory);
|
||||
storage_file_free(directory);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
storage_dir_close(directory);
|
||||
storage_file_free(directory);
|
||||
|
||||
furi_record_close("storage");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void archive_file_append(const char* path, string_t string) {
|
||||
furi_assert(path);
|
||||
furi_assert(string);
|
||||
|
||||
FileWorker* file_worker = file_worker_alloc(false);
|
||||
|
||||
if(!file_worker_open(file_worker, path, FSAM_WRITE, FSOM_OPEN_APPEND)) {
|
||||
FURI_LOG_E("Archive", "Append open error");
|
||||
}
|
||||
|
||||
if(!file_worker_write(file_worker, string_get_cstr(string), string_size(string))) {
|
||||
FURI_LOG_E("Archive", "Append write error");
|
||||
}
|
||||
|
||||
file_worker_close(file_worker);
|
||||
file_worker_free(file_worker);
|
||||
}
|
||||
|
||||
void archive_delete_file(void* context, string_t path, string_t name) {
|
||||
furi_assert(context);
|
||||
furi_assert(path);
|
||||
furi_assert(name);
|
||||
ArchiveMainView* main_view = context;
|
||||
FileWorker* file_worker = file_worker_alloc(false);
|
||||
|
||||
string_t full_path;
|
||||
string_init(full_path);
|
||||
string_printf(full_path, "%s/%s", string_get_cstr(path), string_get_cstr(name));
|
||||
file_worker_remove(file_worker, string_get_cstr(full_path));
|
||||
file_worker_free(file_worker);
|
||||
string_clear(full_path);
|
||||
|
||||
if(archive_is_favorite(string_get_cstr(path), string_get_cstr(name))) {
|
||||
archive_favorites_delete(string_get_cstr(path), string_get_cstr(name));
|
||||
}
|
||||
|
||||
archive_file_array_remove_selected(main_view);
|
||||
}
|
56
applications/archive/helpers/archive_files.h
Normal file
56
applications/archive/helpers/archive_files.h
Normal file
@@ -0,0 +1,56 @@
|
||||
#pragma once
|
||||
#include "file-worker.h"
|
||||
|
||||
#define MAX_FILES 100 //temp
|
||||
|
||||
typedef enum {
|
||||
ArchiveFileTypeIButton,
|
||||
ArchiveFileTypeNFC,
|
||||
ArchiveFileTypeSubGhz,
|
||||
ArchiveFileTypeLFRFID,
|
||||
ArchiveFileTypeIrda,
|
||||
ArchiveFileTypeFolder,
|
||||
ArchiveFileTypeUnknown,
|
||||
AppIdTotal,
|
||||
} ArchiveFileTypeEnum;
|
||||
|
||||
typedef struct {
|
||||
string_t name;
|
||||
ArchiveFileTypeEnum type;
|
||||
bool fav;
|
||||
} ArchiveFile_t;
|
||||
|
||||
static void ArchiveFile_t_init(ArchiveFile_t* obj) {
|
||||
obj->type = ArchiveFileTypeUnknown;
|
||||
string_init(obj->name);
|
||||
}
|
||||
|
||||
static void ArchiveFile_t_init_set(ArchiveFile_t* obj, const ArchiveFile_t* src) {
|
||||
obj->type = src->type;
|
||||
string_init_set(obj->name, src->name);
|
||||
}
|
||||
|
||||
static void ArchiveFile_t_set(ArchiveFile_t* obj, const ArchiveFile_t* src) {
|
||||
obj->type = src->type;
|
||||
string_set(obj->name, src->name);
|
||||
}
|
||||
|
||||
static void ArchiveFile_t_clear(ArchiveFile_t* obj) {
|
||||
string_clear(obj->name);
|
||||
}
|
||||
|
||||
ARRAY_DEF(
|
||||
files_array,
|
||||
ArchiveFile_t,
|
||||
(INIT(API_2(ArchiveFile_t_init)),
|
||||
SET(API_6(ArchiveFile_t_set)),
|
||||
INIT_SET(API_6(ArchiveFile_t_init_set)),
|
||||
CLEAR(API_2(ArchiveFile_t_clear))))
|
||||
|
||||
bool filter_by_extension(FileInfo* file_info, const char* tab_ext, const char* name);
|
||||
void set_file_type(ArchiveFile_t* file, FileInfo* file_info);
|
||||
void archive_trim_file_ext(char* name);
|
||||
bool archive_get_filenames(void* context, uint8_t tab_id, const char* path);
|
||||
bool archive_read_dir(void* context, const char* path);
|
||||
void archive_file_append(const char* path, string_t string);
|
||||
void archive_delete_file(void* context, string_t path, string_t name);
|
Reference in New Issue
Block a user