2021-09-21 10:56:33 +00:00
|
|
|
|
2021-09-10 00:57:43 +00:00
|
|
|
#include "archive_favorites.h"
|
2022-02-10 13:01:49 +00:00
|
|
|
#include "archive_files.h"
|
|
|
|
#include "archive_apps.h"
|
2021-09-21 10:56:33 +00:00
|
|
|
#include "archive_browser.h"
|
|
|
|
|
2022-04-14 11:28:59 +00:00
|
|
|
#define ARCHIVE_FAV_FILE_BUF_LEN 32
|
|
|
|
|
|
|
|
static bool archive_favorites_read_line(File* file, string_t str_result) {
|
|
|
|
string_reset(str_result);
|
|
|
|
uint8_t buffer[ARCHIVE_FAV_FILE_BUF_LEN];
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
do {
|
|
|
|
uint16_t read_count = storage_file_read(file, buffer, ARCHIVE_FAV_FILE_BUF_LEN);
|
|
|
|
if(storage_file_get_error(file) != FSE_OK) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(uint16_t i = 0; i < read_count; i++) {
|
|
|
|
if(buffer[i] == '\n') {
|
|
|
|
uint32_t position = storage_file_tell(file);
|
|
|
|
if(storage_file_get_error(file) != FSE_OK) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
position = position - read_count + i + 1;
|
|
|
|
|
|
|
|
storage_file_seek(file, position, true);
|
|
|
|
if(storage_file_get_error(file) != FSE_OK) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = true;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
string_push_back(str_result, buffer[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(result || read_count == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while(true);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-09-21 10:56:33 +00:00
|
|
|
uint16_t archive_favorites_count(void* context) {
|
|
|
|
furi_assert(context);
|
|
|
|
|
2022-07-26 12:21:51 +00:00
|
|
|
Storage* fs_api = furi_record_open(RECORD_STORAGE);
|
2022-04-14 11:28:59 +00:00
|
|
|
File* file = storage_file_alloc(fs_api);
|
2021-09-21 10:56:33 +00:00
|
|
|
|
|
|
|
string_t buffer;
|
|
|
|
string_init(buffer);
|
|
|
|
|
2022-04-14 11:28:59 +00:00
|
|
|
bool result = storage_file_open(file, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
|
2021-09-21 10:56:33 +00:00
|
|
|
uint16_t lines = 0;
|
|
|
|
|
|
|
|
if(result) {
|
|
|
|
while(1) {
|
2022-04-14 11:28:59 +00:00
|
|
|
if(!archive_favorites_read_line(file, buffer)) {
|
2021-09-21 10:56:33 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(!string_size(buffer)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++lines;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:28:59 +00:00
|
|
|
storage_file_close(file);
|
|
|
|
|
2021-09-21 10:56:33 +00:00
|
|
|
string_clear(buffer);
|
2022-04-14 11:28:59 +00:00
|
|
|
storage_file_free(file);
|
2022-07-26 12:21:51 +00:00
|
|
|
furi_record_close(RECORD_STORAGE);
|
2022-04-14 11:28:59 +00:00
|
|
|
|
2021-09-21 10:56:33 +00:00
|
|
|
return lines;
|
|
|
|
}
|
2021-09-10 00:57:43 +00:00
|
|
|
|
2022-01-29 09:39:10 +00:00
|
|
|
static bool archive_favourites_rescan() {
|
|
|
|
string_t buffer;
|
|
|
|
string_init(buffer);
|
2022-07-26 12:21:51 +00:00
|
|
|
Storage* fs_api = furi_record_open(RECORD_STORAGE);
|
2022-04-14 11:28:59 +00:00
|
|
|
File* file = storage_file_alloc(fs_api);
|
|
|
|
File* fav_item_file = storage_file_alloc(fs_api);
|
2022-01-29 09:39:10 +00:00
|
|
|
|
2022-04-14 11:28:59 +00:00
|
|
|
bool result = storage_file_open(file, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
|
2022-01-29 09:39:10 +00:00
|
|
|
if(result) {
|
|
|
|
while(1) {
|
2022-04-14 11:28:59 +00:00
|
|
|
if(!archive_favorites_read_line(file, buffer)) {
|
2022-01-29 09:39:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(!string_size(buffer)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-02-10 13:01:49 +00:00
|
|
|
if(string_search(buffer, "/app:") == 0) {
|
|
|
|
if(archive_app_is_available(NULL, string_get_cstr(buffer))) {
|
|
|
|
archive_file_append(ARCHIVE_FAV_TEMP_PATH, "%s\n", string_get_cstr(buffer));
|
|
|
|
}
|
|
|
|
} else {
|
2022-04-14 11:28:59 +00:00
|
|
|
bool file_exists = storage_file_open(
|
|
|
|
fav_item_file, string_get_cstr(buffer), FSAM_READ, FSOM_OPEN_EXISTING);
|
2022-02-10 13:01:49 +00:00
|
|
|
if(file_exists) {
|
2022-04-14 11:28:59 +00:00
|
|
|
storage_file_close(fav_item_file);
|
2022-02-10 13:01:49 +00:00
|
|
|
archive_file_append(ARCHIVE_FAV_TEMP_PATH, "%s\n", string_get_cstr(buffer));
|
2022-04-14 17:02:16 +00:00
|
|
|
} else {
|
|
|
|
storage_file_close(fav_item_file);
|
2022-02-10 13:01:49 +00:00
|
|
|
}
|
2022-01-29 09:39:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
string_clear(buffer);
|
|
|
|
|
2022-04-14 11:28:59 +00:00
|
|
|
storage_file_close(file);
|
|
|
|
storage_common_remove(fs_api, ARCHIVE_FAV_PATH);
|
|
|
|
storage_common_rename(fs_api, ARCHIVE_FAV_TEMP_PATH, ARCHIVE_FAV_PATH);
|
2022-04-14 17:02:16 +00:00
|
|
|
storage_common_remove(fs_api, ARCHIVE_FAV_TEMP_PATH);
|
2022-01-29 09:39:10 +00:00
|
|
|
|
2022-04-14 11:28:59 +00:00
|
|
|
storage_file_free(file);
|
|
|
|
storage_file_free(fav_item_file);
|
2022-07-26 12:21:51 +00:00
|
|
|
furi_record_close(RECORD_STORAGE);
|
2022-01-29 09:39:10 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-09-10 00:57:43 +00:00
|
|
|
bool archive_favorites_read(void* context) {
|
|
|
|
furi_assert(context);
|
|
|
|
|
2021-10-12 13:09:34 +00:00
|
|
|
ArchiveBrowserView* browser = context;
|
2022-07-26 12:21:51 +00:00
|
|
|
Storage* fs_api = furi_record_open(RECORD_STORAGE);
|
2022-04-14 11:28:59 +00:00
|
|
|
File* file = storage_file_alloc(fs_api);
|
|
|
|
File* fav_item_file = storage_file_alloc(fs_api);
|
2021-09-10 00:57:43 +00:00
|
|
|
|
|
|
|
string_t buffer;
|
|
|
|
FileInfo file_info;
|
|
|
|
string_init(buffer);
|
|
|
|
|
2022-01-29 09:39:10 +00:00
|
|
|
bool need_refresh = false;
|
2022-04-06 17:44:06 +00:00
|
|
|
uint16_t file_count = 0;
|
|
|
|
|
|
|
|
archive_file_array_rm_all(browser);
|
2022-01-29 09:39:10 +00:00
|
|
|
|
2022-04-14 11:28:59 +00:00
|
|
|
bool result = storage_file_open(file, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
|
2021-09-10 00:57:43 +00:00
|
|
|
|
|
|
|
if(result) {
|
|
|
|
while(1) {
|
2022-04-14 11:28:59 +00:00
|
|
|
if(!archive_favorites_read_line(file, buffer)) {
|
2021-09-10 00:57:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(!string_size(buffer)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-02-10 13:01:49 +00:00
|
|
|
if(string_search(buffer, "/app:") == 0) {
|
|
|
|
if(archive_app_is_available(browser, string_get_cstr(buffer))) {
|
|
|
|
archive_add_app_item(browser, string_get_cstr(buffer));
|
2022-04-06 17:44:06 +00:00
|
|
|
file_count++;
|
2022-02-10 13:01:49 +00:00
|
|
|
} else {
|
|
|
|
need_refresh = true;
|
|
|
|
}
|
|
|
|
} else {
|
2022-04-14 11:28:59 +00:00
|
|
|
bool file_exists = storage_file_open(
|
|
|
|
fav_item_file, string_get_cstr(buffer), FSAM_READ, FSOM_OPEN_EXISTING);
|
2022-04-06 17:44:06 +00:00
|
|
|
if(file_exists) {
|
2022-04-14 17:02:16 +00:00
|
|
|
storage_common_stat(fs_api, string_get_cstr(buffer), &file_info);
|
2022-04-14 11:28:59 +00:00
|
|
|
storage_file_close(fav_item_file);
|
2022-06-09 07:09:52 +00:00
|
|
|
archive_add_file_item(
|
|
|
|
browser, (file_info.flags & FSF_DIRECTORY), string_get_cstr(buffer));
|
2022-04-06 17:44:06 +00:00
|
|
|
file_count++;
|
|
|
|
} else {
|
2022-04-14 17:02:16 +00:00
|
|
|
storage_file_close(fav_item_file);
|
2022-02-10 13:01:49 +00:00
|
|
|
need_refresh = true;
|
2022-04-06 17:44:06 +00:00
|
|
|
}
|
2022-02-10 13:01:49 +00:00
|
|
|
}
|
2022-01-29 09:39:10 +00:00
|
|
|
|
2021-11-15 17:09:40 +00:00
|
|
|
string_reset(buffer);
|
2021-09-10 00:57:43 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-14 11:28:59 +00:00
|
|
|
storage_file_close(file);
|
2021-09-10 00:57:43 +00:00
|
|
|
string_clear(buffer);
|
2022-04-14 11:28:59 +00:00
|
|
|
storage_file_free(file);
|
|
|
|
storage_file_free(fav_item_file);
|
2022-07-26 12:21:51 +00:00
|
|
|
furi_record_close(RECORD_STORAGE);
|
2022-01-29 09:39:10 +00:00
|
|
|
|
2022-04-06 17:44:06 +00:00
|
|
|
archive_set_item_count(browser, file_count);
|
|
|
|
|
2022-01-29 09:39:10 +00:00
|
|
|
if(need_refresh) {
|
|
|
|
archive_favourites_rescan();
|
|
|
|
}
|
|
|
|
|
2021-09-10 00:57:43 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-09-21 10:56:33 +00:00
|
|
|
bool archive_favorites_delete(const char* format, ...) {
|
2021-10-12 13:09:34 +00:00
|
|
|
string_t buffer;
|
|
|
|
string_t filename;
|
2021-09-21 10:56:33 +00:00
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
2021-10-12 13:09:34 +00:00
|
|
|
string_init_vprintf(filename, format, args);
|
2021-09-21 10:56:33 +00:00
|
|
|
va_end(args);
|
2021-09-10 00:57:43 +00:00
|
|
|
|
|
|
|
string_init(buffer);
|
2022-07-26 12:21:51 +00:00
|
|
|
Storage* fs_api = furi_record_open(RECORD_STORAGE);
|
2022-04-14 11:28:59 +00:00
|
|
|
File* file = storage_file_alloc(fs_api);
|
|
|
|
|
|
|
|
bool result = storage_file_open(file, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
|
2021-09-10 00:57:43 +00:00
|
|
|
|
|
|
|
if(result) {
|
|
|
|
while(1) {
|
2022-04-14 11:28:59 +00:00
|
|
|
if(!archive_favorites_read_line(file, buffer)) {
|
2021-09-10 00:57:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(!string_size(buffer)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-10-12 13:09:34 +00:00
|
|
|
if(string_search(buffer, filename)) {
|
|
|
|
archive_file_append(ARCHIVE_FAV_TEMP_PATH, "%s\n", string_get_cstr(buffer));
|
2021-09-10 00:57:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
string_clear(buffer);
|
2021-10-12 13:09:34 +00:00
|
|
|
string_clear(filename);
|
2021-09-10 00:57:43 +00:00
|
|
|
|
2022-04-14 11:28:59 +00:00
|
|
|
storage_file_close(file);
|
|
|
|
storage_common_remove(fs_api, ARCHIVE_FAV_PATH);
|
|
|
|
storage_common_rename(fs_api, ARCHIVE_FAV_TEMP_PATH, ARCHIVE_FAV_PATH);
|
2022-04-14 17:02:16 +00:00
|
|
|
storage_common_remove(fs_api, ARCHIVE_FAV_TEMP_PATH);
|
2021-09-10 00:57:43 +00:00
|
|
|
|
2022-04-14 11:28:59 +00:00
|
|
|
storage_file_free(file);
|
2022-07-26 12:21:51 +00:00
|
|
|
furi_record_close(RECORD_STORAGE);
|
2021-09-10 00:57:43 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-09-21 10:56:33 +00:00
|
|
|
bool archive_is_favorite(const char* format, ...) {
|
2021-10-12 13:09:34 +00:00
|
|
|
string_t buffer;
|
|
|
|
string_t filename;
|
2021-09-21 10:56:33 +00:00
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
2021-10-12 13:09:34 +00:00
|
|
|
string_init_vprintf(filename, format, args);
|
2021-09-21 10:56:33 +00:00
|
|
|
va_end(args);
|
2021-09-10 00:57:43 +00:00
|
|
|
|
|
|
|
string_init(buffer);
|
2022-07-26 12:21:51 +00:00
|
|
|
Storage* fs_api = furi_record_open(RECORD_STORAGE);
|
2022-04-14 11:28:59 +00:00
|
|
|
File* file = storage_file_alloc(fs_api);
|
2021-09-10 00:57:43 +00:00
|
|
|
|
2021-09-21 10:56:33 +00:00
|
|
|
bool found = false;
|
2022-04-14 11:28:59 +00:00
|
|
|
bool result = storage_file_open(file, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
|
2021-09-10 00:57:43 +00:00
|
|
|
|
|
|
|
if(result) {
|
|
|
|
while(1) {
|
2022-04-14 11:28:59 +00:00
|
|
|
if(!archive_favorites_read_line(file, buffer)) {
|
2021-09-10 00:57:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(!string_size(buffer)) {
|
|
|
|
break;
|
|
|
|
}
|
2021-10-12 13:09:34 +00:00
|
|
|
if(!string_search(buffer, filename)) {
|
2021-09-10 00:57:43 +00:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:28:59 +00:00
|
|
|
storage_file_close(file);
|
2021-09-10 00:57:43 +00:00
|
|
|
string_clear(buffer);
|
2021-10-12 13:09:34 +00:00
|
|
|
string_clear(filename);
|
2022-04-14 11:28:59 +00:00
|
|
|
storage_file_free(file);
|
2022-07-26 12:21:51 +00:00
|
|
|
furi_record_close(RECORD_STORAGE);
|
2021-09-10 00:57:43 +00:00
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2022-02-10 13:01:49 +00:00
|
|
|
bool archive_favorites_rename(const char* src, const char* dst) {
|
2021-09-10 00:57:43 +00:00
|
|
|
furi_assert(src);
|
|
|
|
furi_assert(dst);
|
|
|
|
|
2022-07-26 12:21:51 +00:00
|
|
|
Storage* fs_api = furi_record_open(RECORD_STORAGE);
|
2022-04-14 11:28:59 +00:00
|
|
|
File* file = storage_file_alloc(fs_api);
|
2021-09-10 00:57:43 +00:00
|
|
|
|
|
|
|
string_t path;
|
|
|
|
string_t buffer;
|
|
|
|
|
|
|
|
string_init(buffer);
|
|
|
|
string_init(path);
|
|
|
|
|
2022-02-10 13:01:49 +00:00
|
|
|
string_printf(path, "%s", src);
|
2022-04-14 11:28:59 +00:00
|
|
|
bool result = storage_file_open(file, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
|
2021-09-10 00:57:43 +00:00
|
|
|
|
|
|
|
if(result) {
|
|
|
|
while(1) {
|
2022-04-14 11:28:59 +00:00
|
|
|
if(!archive_favorites_read_line(file, buffer)) {
|
2021-09-10 00:57:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(!string_size(buffer)) {
|
|
|
|
break;
|
|
|
|
}
|
2021-09-21 10:56:33 +00:00
|
|
|
|
|
|
|
archive_file_append(
|
|
|
|
ARCHIVE_FAV_TEMP_PATH,
|
2021-10-12 13:09:34 +00:00
|
|
|
"%s\n",
|
2021-09-21 10:56:33 +00:00
|
|
|
string_search(buffer, path) ? string_get_cstr(buffer) : dst);
|
2021-09-10 00:57:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
string_clear(buffer);
|
|
|
|
string_clear(path);
|
|
|
|
|
2022-04-14 11:28:59 +00:00
|
|
|
storage_file_close(file);
|
|
|
|
storage_common_remove(fs_api, ARCHIVE_FAV_PATH);
|
|
|
|
storage_common_rename(fs_api, ARCHIVE_FAV_TEMP_PATH, ARCHIVE_FAV_PATH);
|
2022-04-14 17:02:16 +00:00
|
|
|
storage_common_remove(fs_api, ARCHIVE_FAV_TEMP_PATH);
|
2021-09-10 00:57:43 +00:00
|
|
|
|
2022-04-14 11:28:59 +00:00
|
|
|
storage_file_free(file);
|
2022-07-26 12:21:51 +00:00
|
|
|
furi_record_close(RECORD_STORAGE);
|
2021-09-10 00:57:43 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-09-21 10:56:33 +00:00
|
|
|
void archive_add_to_favorites(const char* file_path) {
|
2021-09-10 00:57:43 +00:00
|
|
|
furi_assert(file_path);
|
|
|
|
|
2021-10-12 13:09:34 +00:00
|
|
|
archive_file_append(ARCHIVE_FAV_PATH, "%s\n", file_path);
|
2021-09-10 00:57:43 +00:00
|
|
|
}
|
2021-10-12 13:09:34 +00:00
|
|
|
|
|
|
|
void archive_favorites_save(void* context) {
|
|
|
|
furi_assert(context);
|
|
|
|
|
|
|
|
ArchiveBrowserView* browser = context;
|
2022-07-26 12:21:51 +00:00
|
|
|
Storage* fs_api = furi_record_open(RECORD_STORAGE);
|
2022-04-14 11:28:59 +00:00
|
|
|
File* file = storage_file_alloc(fs_api);
|
2021-10-12 13:09:34 +00:00
|
|
|
|
2022-04-06 17:44:06 +00:00
|
|
|
for(size_t i = 0; i < archive_file_get_array_size(browser); i++) {
|
2021-10-12 13:09:34 +00:00
|
|
|
ArchiveFile_t* item = archive_get_file_at(browser, i);
|
2022-06-09 07:09:52 +00:00
|
|
|
archive_file_append(ARCHIVE_FAV_TEMP_PATH, "%s\n", string_get_cstr(item->path));
|
2021-10-12 13:09:34 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 11:28:59 +00:00
|
|
|
storage_common_remove(fs_api, ARCHIVE_FAV_PATH);
|
|
|
|
storage_common_rename(fs_api, ARCHIVE_FAV_TEMP_PATH, ARCHIVE_FAV_PATH);
|
2022-04-14 17:02:16 +00:00
|
|
|
storage_common_remove(fs_api, ARCHIVE_FAV_TEMP_PATH);
|
2021-10-12 13:09:34 +00:00
|
|
|
|
2022-04-14 11:28:59 +00:00
|
|
|
storage_file_free(file);
|
2022-07-26 12:21:51 +00:00
|
|
|
furi_record_close(RECORD_STORAGE);
|
2022-04-14 11:28:59 +00:00
|
|
|
}
|