Loader pick all menu apps from file

This commit is contained in:
Willy-JL
2023-07-20 23:44:42 +01:00
parent 310599f73a
commit dee8f1d5dc
10 changed files with 160 additions and 116 deletions
+94 -26
View File
@@ -134,9 +134,9 @@ FuriPubSub* loader_get_pubsub(Loader* loader) {
return loader->pubsub;
}
ExtMainAppList_t* loader_get_ext_main_apps(Loader* loader) {
MenuAppList_t* loader_get_menu_apps(Loader* loader) {
furi_assert(loader);
return &loader->ext_main_apps;
return &loader->menu_apps;
}
// callbacks
@@ -195,6 +195,31 @@ bool loader_menu_load_fap_meta(
return true;
}
static void loader_make_menu_file(Storage* storage) {
Stream* new = file_stream_alloc(storage);
if(!storage_file_exists(storage, XTREME_MENU_PATH)) {
if(file_stream_open(new, XTREME_MENU_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
stream_write_format(new, "MenuAppList Version %u\n", 0);
for(size_t i = 0; i < FLIPPER_APPS_COUNT; i++) {
stream_write_format(new, "%s\n", FLIPPER_APPS[i].name);
}
for(size_t i = 0; i < FLIPPER_EXTERNAL_APPS_COUNT - 1; i++) {
stream_write_format(new, "%s\n", FLIPPER_EXTERNAL_APPS[i].name);
}
Stream* old = file_stream_alloc(storage);
if(file_stream_open(old, XTREME_MENU_OLD_PATH, FSAM_READ, FSOM_OPEN_EXISTING)) {
stream_copy(old, new, stream_size(old));
}
file_stream_close(old);
stream_free(old);
storage_common_remove(storage, XTREME_MENU_OLD_PATH);
}
file_stream_close(new);
}
file_stream_close(new);
stream_free(new);
}
static Loader* loader_alloc() {
Loader* loader = malloc(sizeof(Loader));
loader->pubsub = furi_pubsub_alloc();
@@ -205,33 +230,76 @@ static Loader* loader_alloc() {
loader->app.thread = NULL;
loader->app.insomniac = false;
loader->app.fap = NULL;
ExtMainAppList_init(loader->ext_main_apps);
MenuAppList_init(loader->menu_apps);
if(furi_hal_is_normal_boot()) {
Storage* storage = furi_record_open(RECORD_STORAGE);
FuriString* path = furi_string_alloc();
FuriString* name = furi_string_alloc();
Stream* stream = file_stream_alloc(storage);
if(file_stream_open(stream, XTREME_APPS_PATH, FSAM_READ, FSOM_OPEN_EXISTING)) {
while(stream_read_line(stream, path)) {
furi_string_replace_all(path, "\r", "");
furi_string_replace_all(path, "\n", "");
const Icon* icon;
if(!loader_menu_load_fap_meta(storage, path, name, &icon)) continue;
ExtMainAppList_push_back(
loader->ext_main_apps,
(ExtMainApp){
.name = strdup(furi_string_get_cstr(name)),
.path = strdup(furi_string_get_cstr(path)),
.icon = icon});
if(!furi_hal_is_normal_boot()) return loader;
Storage* storage = furi_record_open(RECORD_STORAGE);
Stream* stream = file_stream_alloc(storage);
FuriString* line = furi_string_alloc();
FuriString* name = furi_string_alloc();
do {
if(!file_stream_open(stream, XTREME_MENU_PATH, FSAM_READ_WRITE, FSOM_OPEN_EXISTING)) {
file_stream_close(stream);
loader_make_menu_file(storage);
if(!file_stream_open(stream, XTREME_MENU_PATH, FSAM_READ_WRITE, FSOM_OPEN_EXISTING))
break;
}
uint32_t version;
if(!stream_read_line(stream, line) ||
sscanf(furi_string_get_cstr(line), "MenuAppList Version %lu", &version) != 1 ||
version > 0) {
file_stream_close(stream);
storage_common_remove(storage, XTREME_MENU_PATH);
loader_make_menu_file(storage);
if(!file_stream_open(stream, XTREME_MENU_PATH, FSAM_READ_WRITE, FSOM_OPEN_EXISTING))
break;
if(!stream_read_line(stream, line) ||
sscanf(furi_string_get_cstr(line), "MenuAppList Version %lu", &version) != 1 ||
version > 0)
break;
}
while(stream_read_line(stream, line)) {
furi_string_replace_all(line, "\r", "");
furi_string_replace_all(line, "\n", "");
const char* label = NULL;
const Icon* icon = NULL;
const char* exe = NULL;
if(storage_file_exists(storage, furi_string_get_cstr(line))) {
if(loader_menu_load_fap_meta(storage, line, name, &icon)) {
label = strdup(furi_string_get_cstr(name));
exe = strdup(furi_string_get_cstr(line));
}
} else {
for(size_t i = 0; !exe && i < FLIPPER_APPS_COUNT; i++) {
if(!strcmp(furi_string_get_cstr(line), FLIPPER_APPS[i].name)) {
label = FLIPPER_APPS[i].name;
icon = FLIPPER_APPS[i].icon;
exe = FLIPPER_APPS[i].name;
}
}
for(size_t i = 0; !exe && i < FLIPPER_EXTERNAL_APPS_COUNT; i++) {
if(!strcmp(furi_string_get_cstr(line), FLIPPER_EXTERNAL_APPS[i].name)) {
label = FLIPPER_EXTERNAL_APPS[i].name;
icon = FLIPPER_EXTERNAL_APPS[i].icon;
exe = FLIPPER_EXTERNAL_APPS[i].name;
}
}
}
if(label && exe && icon) {
MenuAppList_push_back(
loader->menu_apps, (MenuApp){.label = label, .icon = icon, .exe = exe});
}
}
file_stream_close(stream);
stream_free(stream);
furi_string_free(name);
furi_string_free(path);
furi_record_close(RECORD_STORAGE);
}
} while(false);
furi_string_free(name);
furi_string_free(line);
file_stream_close(stream);
stream_free(stream);
furi_record_close(RECORD_STORAGE);
return loader;
}