1
0
Fork 0
mirror of https://github.com/HamletDuFromage/aio-switch-updater.git synced 2024-11-13 21:56:42 +00:00
AIO-switch-updater/source/ams_tab.cpp

223 lines
9.4 KiB
C++
Raw Normal View History

#include "ams_tab.hpp"
2021-09-11 14:48:13 +01:00
#include <filesystem>
#include <string>
2021-02-10 16:28:47 +00:00
#include "confirm_page.hpp"
2021-03-10 20:43:00 +00:00
#include "current_cfw.hpp"
2021-09-11 14:48:13 +01:00
#include "dialogue_page.hpp"
#include "download.hpp"
#include "extract.hpp"
#include "fs.hpp"
2021-09-11 14:48:13 +01:00
#include "utils.hpp"
#include "worker_page.hpp"
namespace i18n = brls::i18n;
using namespace i18n::literals;
2021-09-15 16:23:37 +01:00
AmsTab::AmsTab(const nlohmann::json& nxlinks, const bool erista, const bool hideStandardEntries) : brls::List()
{
this->erista = erista;
2021-09-15 16:50:34 +01:00
this->hekate = util::getValueFromKey(nxlinks, "hekate");
auto cfws = util::getValueFromKey(nxlinks, "cfws");
2021-09-11 14:48:13 +01:00
if (!hideStandardEntries) {
this->description = new brls::Label(brls::LabelStyle::DESCRIPTION, "menus/main/ams_text"_i18n + (CurrentCfw::running_cfw == CFW::ams ? "\n" + "menus/ams_update/current_ams"_i18n + CurrentCfw::getAmsInfo() : "") + (erista ? "\n" + "menus/ams_update/erista_rev"_i18n : "\n" + "menus/ams_update/mariko_rev"_i18n), true);
this->addView(description);
2021-09-15 16:50:34 +01:00
CreateDownloadItems(util::getValueFromKey(cfws, "Atmosphere"));
description = new brls::Label(
brls::LabelStyle::DESCRIPTION,
"menus/ams_update/deepsea_label"_i18n,
2021-09-11 14:48:13 +01:00
true);
this->addView(description);
listItem = new brls::ListItem("menus/ams_update/get_custom_deepsea"_i18n);
listItem->setHeight(LISTITEM_HEIGHT);
listItem->getClickEvent()->subscribe([this](brls::View* view) {
nlohmann::ordered_json modules;
download::getRequest(DEEPSEA_META_JSON, modules);
this->ShowCustomDeepseaBuilder(modules);
});
this->addView(listItem);
CreateDownloadItems(util::getValueFromKey(cfws, "DeepSea"), false);
}
2021-07-09 00:14:07 +01:00
auto custom_pack = fs::parseJsonFile(CUSTOM_PACKS_PATH);
if (custom_pack.size() != 0) {
description = new brls::Label(
brls::LabelStyle::DESCRIPTION,
fmt::format("menus/ams_update/custom_packs_label"_i18n, CUSTOM_PACKS_PATH),
2021-09-11 14:48:13 +01:00
true);
this->addView(description);
2021-07-09 00:14:07 +01:00
2021-09-11 14:48:13 +01:00
CreateDownloadItems(cfws.size() ? custom_pack : nlohmann::ordered_json::object(), true); // TODO: better way to check for availability of the links
2021-07-09 00:14:07 +01:00
}
}
2021-07-09 00:14:07 +01:00
void AmsTab::CreateDownloadItems(const nlohmann::ordered_json& cfw_links, bool hekate)
{
std::string operation("menus/ams_update/getting_ams"_i18n);
std::vector<std::pair<std::string, std::string>> links;
2021-07-09 00:14:07 +01:00
links = download::getLinksFromJson(cfw_links);
2021-09-11 14:48:13 +01:00
if (links.size()) {
2021-09-15 16:23:37 +01:00
auto hekate_link = download::getLinksFromJson(this->hekate);
2021-02-10 02:57:42 +00:00
std::string hekate_url = hekate_link[0].second;
2021-03-10 20:43:00 +00:00
std::string text_hekate = "menus/common/download"_i18n + hekate_link[0].first;
2020-12-13 13:52:51 +00:00
2021-09-11 14:48:13 +01:00
for (const auto& link : links) {
2021-03-16 14:56:46 +00:00
std::string url = link.second;
std::string text("menus/common/download"_i18n + link.first + "menus/common/from"_i18n + url);
listItem = new brls::ListItem(link.first);
listItem->setHeight(LISTITEM_HEIGHT);
2021-10-03 18:54:09 +01:00
listItem->getClickEvent()->subscribe([this, text, text_hekate, url, hekate_url, operation, hekate](brls::View* view) {
2021-09-11 14:48:13 +01:00
if (!erista && !std::filesystem::exists(MARIKO_PAYLOAD_PATH)) {
brls::Application::crash("menus/errors/mariko_payload_missing"_i18n);
}
else {
CreateStagedFrames(text, url, operation, erista, hekate, text_hekate, hekate_url);
}
});
this->addView(listItem);
}
}
2021-09-11 14:48:13 +01:00
else {
description = new brls::Label(
brls::LabelStyle::SMALL,
2021-03-10 20:43:00 +00:00
"menus/main/links_not_found"_i18n,
2021-09-11 14:48:13 +01:00
true);
description->setHorizontalAlign(NVG_ALIGN_CENTER);
this->addView(description);
}
}
void AmsTab::CreateStagedFrames(const std::string& text, const std::string& url, const std::string& operation, bool erista, bool hekate, const std::string& text_hekate, const std::string& hekate_url)
{
brls::StagedAppletFrame* stagedFrame = new brls::StagedAppletFrame();
stagedFrame->setTitle(operation);
stagedFrame->addStage(
2021-09-11 14:48:13 +01:00
new ConfirmPage(stagedFrame, text));
stagedFrame->addStage(
new WorkerPage(stagedFrame, "menus/common/downloading"_i18n, [url]() { util::downloadArchive(url, contentType::ams_cfw); }));
stagedFrame->addStage(
new WorkerPage(stagedFrame, "menus/common/extracting"_i18n, []() { util::extractArchive(contentType::ams_cfw); }));
2021-09-11 14:48:13 +01:00
if (hekate) {
stagedFrame->addStage(
new DialoguePage_ams(stagedFrame, text_hekate, erista));
stagedFrame->addStage(
new WorkerPage(stagedFrame, "menus/common/downloading"_i18n, [hekate_url]() { util::downloadArchive(hekate_url, contentType::bootloaders); }));
stagedFrame->addStage(
new WorkerPage(stagedFrame, "menus/common/extracting"_i18n, []() { util::extractArchive(contentType::bootloaders); }));
}
stagedFrame->addStage(
2021-09-11 14:48:13 +01:00
new ConfirmPage(stagedFrame, "menus/ams_update/reboot_rcm"_i18n, false, true, erista));
brls::Application::pushView(stagedFrame);
}
2021-09-11 14:48:13 +01:00
std::string AmsTab::GetRepoName(const std::string& repo)
{
return repo.substr(repo.find("/") + 1, repo.length());
}
std::set<std::string> AmsTab::GetLastDownloadedModules(const std::string& json_path)
{
nlohmann::json package = fs::parseJsonFile(json_path);
std::set<std::string> res;
2021-09-11 14:48:13 +01:00
if (package.find("modules") != package.end()) {
for (const auto& module : package.at("modules")) {
res.insert(module.get<std::string>());
}
}
return res;
}
nlohmann::ordered_json AmsTab::SortDeepseaModules(const nlohmann::ordered_json& modules)
{
nlohmann::ordered_json sorted_modules = nlohmann::ordered_json::object();
2021-09-11 14:48:13 +01:00
if (modules.find("modules") != modules.end()) {
for (const auto& module : modules.at("modules").items()) {
sorted_modules[std::string(module.value().at("category"))][module.key()] = module.value();
}
}
return sorted_modules;
}
void AmsTab::ShowCustomDeepseaBuilder(nlohmann::ordered_json& modules)
{
modules = SortDeepseaModules(modules);
std::map<std::string, std::string> name_map;
brls::TabFrame* appView = new brls::TabFrame();
appView->setIcon("romfs:/deepsea_icon.png");
std::vector<brls::List*> lists;
std::set<std::string> old_modules = GetLastDownloadedModules(DEEPSEA_PACKAGE_PATH);
brls::ToggleListItem* deepseaListItem;
for (const auto& category : modules.items()) {
brls::List* list = new brls::List();
2021-09-11 14:48:13 +01:00
for (const auto& module : category.value().items()) {
auto module_value = module.value();
std::string requirements = "";
2021-09-11 14:48:13 +01:00
if (!module_value.at("requires").empty()) {
requirements = "menus/ams_update/depends_on"_i18n;
for (const auto& r : module.value().at("requires")) {
requirements += " " + r.get<std::string>() + ",";
}
requirements.pop_back();
}
2021-09-11 14:48:13 +01:00
if (module_value.at("required")) {
deepseaListItem = new UnTogglableListItem(module_value.at("displayName"), 1, requirements, "Required", "o");
}
else {
2021-09-11 14:48:13 +01:00
deepseaListItem = new ::brls::ToggleListItem(module_value.at("displayName"),
old_modules.find(module.key()) != old_modules.end() ? 1 : 0,
requirements,
"menus/common/selected"_i18n,
"menus/common/off"_i18n);
}
name_map.insert(std::pair(module_value.at("displayName"), module.key()));
2021-10-03 18:54:09 +01:00
deepseaListItem->registerAction("menus/ams_update/show_module_description"_i18n, brls::Key::Y, [module_value] {
2022-03-30 21:23:15 +01:00
util::showDialogBoxInfo(fmt::format("{}:\n{}", module_value.at("repo"), module_value.at("description")));
return true;
});
list->addView(deepseaListItem);
}
lists.push_back(list);
appView->addTab(category.key(), list);
}
2021-10-03 18:54:09 +01:00
appView->registerAction("menus/ams_update/download_deepsea_package"_i18n, brls::Key::X, [this, lists, name_map] {
std::set<std::string> desired_modules;
2021-09-11 14:48:13 +01:00
for (const auto& list : lists) {
for (size_t i = 0; i < list->getViewsCount(); i++) {
2021-09-11 14:48:13 +01:00
if (brls::ToggleListItem* item = dynamic_cast<brls::ToggleListItem*>(list->getChild(i))) {
if (item->getToggleState()) {
desired_modules.insert(name_map.at(item->getLabel()));
}
}
}
}
std::string request_url = DEEPSEA_BUILD_URL;
2021-09-11 14:48:13 +01:00
for (const auto& e : desired_modules)
request_url += e + ";";
2021-10-03 18:54:09 +01:00
this->CreateStagedFrames("menus/common/download"_i18n + "Custom DeepSea package" + "menus/common/from"_i18n + request_url,
request_url,
"menus/ams_update/get_custom_deepsea"_i18n,
this->erista);
return true;
});
appView->registerAction("", brls::Key::PLUS, [this] { return true; });
brls::PopupFrame::open("menus/ams_update/deepsea_builder"_i18n, appView, modules.empty() ? "menus/ams_update/cant_fetch_deepsea"_i18n : "menus/ams_update/build_your_deepsea"_i18n, "");
}
bool UnTogglableListItem::onClick()
{
return true;
}