diff --git a/include/applist.h b/include/applist.h index 26e5001..fd882f5 100644 --- a/include/applist.h +++ b/include/applist.h @@ -31,6 +31,7 @@ namespace AppList { void Sort(std::vector &entries, std::vector &pages, std::vector &folders); int Backup(void); int Restore(void); + bool Compare(const char *db_name); } #endif diff --git a/source/applist.cpp b/source/applist.cpp index a0fa044..b7b3301 100644 --- a/source/applist.cpp +++ b/source/applist.cpp @@ -226,4 +226,64 @@ namespace AppList { return 0; } + + bool Compare(const char *db_name) { + std::vector app_entries; + std::vector loadout_entries; + std::vector diff_entries; + + sqlite3 *db; + int ret = sqlite3_open_v2(path, &db, SQLITE_OPEN_READWRITE, nullptr); + + if (ret) + return false; + + std::string query = "SELECT title FROM tbl_appinfo_icon;"; + + sqlite3_stmt *stmt; + ret = sqlite3_prepare_v2(db, query.c_str(), -1, &stmt, nullptr); + + while ((ret = sqlite3_step(stmt)) == SQLITE_ROW) { + std::string entry = reinterpret_cast(sqlite3_column_text(stmt, 0)); + app_entries.push_back(entry); + } + + if (ret != SQLITE_DONE) { + sqlite3_finalize(stmt); + sqlite3_close(db); + return false; + } + + sqlite3_finalize(stmt); + sqlite3_close(db); + + std::string db_path = "ux0:data/VITAHomebrewSorter/loadouts/" + std::string(db_name); + ret = sqlite3_open_v2(db_path.c_str(), &db, SQLITE_OPEN_READWRITE, nullptr); + + if (ret) + return false; + + ret = sqlite3_prepare_v2(db, query.c_str(), -1, &stmt, nullptr); + + while ((ret = sqlite3_step(stmt)) == SQLITE_ROW) { + std::string entry = reinterpret_cast(sqlite3_column_text(stmt, 0)); + loadout_entries.push_back(entry); + } + + if (ret != SQLITE_DONE) { + sqlite3_finalize(stmt); + sqlite3_close(db); + return false; + } + + sqlite3_finalize(stmt); + sqlite3_close(db); + + if (app_entries.empty() || loadout_entries.empty()) + return false; + + std::set_difference(app_entries.begin(), app_entries.end(), loadout_entries.begin(), loadout_entries.end(), + std::inserter(diff_entries, diff_entries.begin())); + return (diff_entries.size() > 0); + } } diff --git a/source/gui.cpp b/source/gui.cpp index 740b0b5..fb699ee 100644 --- a/source/gui.cpp +++ b/source/gui.cpp @@ -32,6 +32,7 @@ namespace GUI { StateConfirm, StateRestore, StateLoadoutRestore, + StateWarning, StateDone }; @@ -100,49 +101,98 @@ namespace GUI { } } - static void ConfirmRestorePopup(State *state, std::vector &entries, const char *name) { - ImGui::OpenPopup(*state == StateConfirm? "Confirmation" : "Restoration"); + static void Prompt(State *state, std::vector &entries, const char *name) { + if (*state == StateNone) + return; + + std::string title, prompt; + + switch (*state) { + case StateConfirm: + title = "Confirm"; + prompt = "Are you sure you want to apply this sorting method?"; + break; + + case StateRestore: + title = "Restore Backup"; + prompt = "Are you sure you want to restore this backup?"; + break; + + case StateLoadoutRestore: + title = "Restore Loadout"; + prompt = "Are you sure you want to apply this loadout?"; + break; + + case StateWarning: + title = "Warning"; + prompt = "This loadout is outdated and may not include some newly installed apps."; + break; + + case StateDone: + title = "Restart"; + prompt = "Do you wish to restart your device?"; + break; + + default: + break; + } + + ImGui::OpenPopup(title.c_str()); ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(20, 20)); ImGui::SetNextWindowPos(ImVec2(480.0f, 272.0f), ImGuiCond_Appearing, ImVec2(0.5f, 0.5f)); - if (ImGui::BeginPopupModal(*state == StateConfirm? "Confirmation" : "Restoration", nullptr, ImGuiWindowFlags_AlwaysAutoResize)) { - switch (*state) { - case StateConfirm: - ImGui::Text("Are you sure you want to apply this sorting method?"); - break; + if (ImGui::BeginPopupModal(title.c_str(), nullptr, ImGuiWindowFlags_AlwaysAutoResize)) { + ImGui::Text(prompt.c_str()); - case StateRestore: - ImGui::Text("Are you sure you want to restore this backup?"); - break; - - case StateLoadoutRestore: - ImGui::Text("Are you sure you want to apply this loadout?"); - break; - - default: - break; + if ((*state == StateConfirm) || (*state == StateRestore) || (*state == StateLoadoutRestore)) { + ImGui::Dummy(ImVec2(0.0f, 5.0f)); + ImGui::Text("You must reboot your device for the changes to take effect."); + } + else if (*state == StateWarning) { + ImGui::Dummy(ImVec2(0.0f, 5.0f)); + ImGui::Text("Do you still wish to continue restoring this loadout?"); } - ImGui::Dummy(ImVec2(0.0f, 5.0f)); - ImGui::Text("You must reboot your device for the changes to take effect."); ImGui::Dummy(ImVec2(0.0f, 5.0f)); if (ImGui::Button("Ok", ImVec2(120, 0))) { - if (*state == StateConfirm) { - AppList::Backup(); - - if (!backupExists) + switch (*state) { + case StateConfirm: + AppList::Backup(); backupExists = true; - - AppList::Save(entries); + AppList::Save(entries); + *state = StateDone; + break; + + case StateRestore: + AppList::Restore(); + *state = StateDone; + break; + + case StateLoadoutRestore: + if (AppList::Compare(name)) { + *state = StateWarning; + } + else { + Loadouts::Restore(name); + *state = StateDone; + } + break; + + case StateWarning: + Loadouts::Restore(name); + *state = StateDone; + break; + + case StateDone: + scePowerRequestColdReset(); + break; + + default: + break; } - else if (*state == StateRestore) - AppList::Restore(); - else if (*state == StateLoadoutRestore) - Loadouts::Restore(name); - + ImGui::CloseCurrentPopup(); - *state = StateDone; } ImGui::SameLine(0.0f, 15.0f); @@ -158,31 +208,6 @@ namespace GUI { ImGui::PopStyleVar(); } - static void RestartPopup(State *state) { - ImGui::OpenPopup("Restart"); - ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(20, 20)); - ImGui::SetNextWindowPos(ImVec2(480.0f, 272.0f), ImGuiCond_Appearing, ImVec2(0.5f, 0.5f)); - - if (ImGui::BeginPopupModal("Restart", nullptr, ImGuiWindowFlags_AlwaysAutoResize)) { - ImGui::Text("Do you wish to restart your device?"); - ImGui::Dummy(ImVec2(0.0f, 5.0f)); - - if (ImGui::Button("Ok", ImVec2(120, 0))) - scePowerRequestColdReset(); - - ImGui::SameLine(0.0f, 15.0f); - - if (ImGui::Button("Cancel", ImVec2(120, 0))) { - ImGui::CloseCurrentPopup(); - *state = StateNone; - } - - ImGui::EndPopup(); - } - - ImGui::PopStyleVar(); - } - int RenderLoop(void) { bool done = false; backupExists = FS::FileExists("ux0:/data/VITAHomebrewSorter/backup/app.db"); @@ -346,29 +371,7 @@ namespace GUI { } GUI::ExitWindow(); - - switch(state) { - case StateNone: - break; - - case StateConfirm: - GUI::ConfirmRestorePopup(&state, apps, loadout_name.c_str()); - break; - - case StateRestore: - GUI::ConfirmRestorePopup(&state, apps, loadout_name.c_str()); - break; - - case StateLoadoutRestore: - GUI::ConfirmRestorePopup(&state, apps, loadout_name.c_str()); - break; - - case StateDone: - GUI::RestartPopup(&state); - break; - - } - + GUI::Prompt(&state, apps, loadout_name.c_str()); Renderer::End(ImVec4(0.45f, 0.55f, 0.60f, 1.00f)); }