From 387285e1e4135a574425fe152d696199402827fb Mon Sep 17 00:00:00 2001 From: arm64v8a <48624112+arm64v8a@users.noreply.github.com> Date: Sat, 22 Jul 2023 16:39:43 +0900 Subject: [PATCH] add flag_reorder --- db/Database.cpp | 52 ++++++++++++++++++++++++++++++++++++++ main/NekoGui_DataStore.hpp | 1 + main/main.cpp | 1 + 3 files changed, 54 insertions(+) diff --git a/db/Database.cpp b/db/Database.cpp index 2295ba892..941f9e180 100644 --- a/db/Database.cpp +++ b/db/Database.cpp @@ -81,6 +81,58 @@ namespace NekoGui { defaultGroup->name = QObject::tr("Default"); NekoGui::profileManager->AddGroup(defaultGroup); } + // + if (dataStore->flag_reorder) { + { + // remove all (contains orphan) + for (const auto &profile: profiles) { + QFile::remove(profile.second->fn); + } + } + std::map gidOld2New; + { + int i = 0; + int ii = 0; + QList newProfilesIdOrder; + std::map> newProfiles; + for (auto gid: groupsTabOrder) { + auto group = GetGroup(gid); + gidOld2New[gid] = ii++; + for (auto const &profile: group->ProfilesWithOrder()) { + auto oldId = profile->id; + auto newId = i++; + profile->id = newId; + profile->gid = gidOld2New[gid]; + profile->fn = QString("profiles/%1.json").arg(newId); + profile->Save(); + newProfiles[newId] = profile; + newProfilesIdOrder << newId; + } + group->order = {}; + group->Save(); + } + profiles = newProfiles; + profilesIdOrder = newProfilesIdOrder; + } + { + QList newGroupsIdOrder; + std::map> newGroups; + for (auto oldGid: groupsTabOrder) { + auto newId = gidOld2New[oldGid]; + auto group = groups[oldGid]; + QFile::remove(group->fn); + group->id = newId; + group->fn = QString("groups/%1.json").arg(newId); + group->Save(); + newGroups[newId] = group; + newGroupsIdOrder << newId; + } + groups = newGroups; + groupsIdOrder = newGroupsIdOrder; + groupsTabOrder = newGroupsIdOrder; + } + MessageBoxInfo(software_name, "Profiles and groups reorder complete."); + } } void ProfileManager::SaveManager() { diff --git a/main/NekoGui_DataStore.hpp b/main/NekoGui_DataStore.hpp index 492de2bd3..7842accb6 100644 --- a/main/NekoGui_DataStore.hpp +++ b/main/NekoGui_DataStore.hpp @@ -88,6 +88,7 @@ namespace NekoGui { bool flag_debug = false; bool flag_linux_run_core_as_admin = false; bool flag_restart_tun_on = false; + bool flag_reorder = false; // Saved diff --git a/main/main.cpp b/main/main.cpp index d6193584f..c8c88579f 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -92,6 +92,7 @@ int main(int argc, char* argv[]) { if (NekoGui::dataStore->argv.contains("-debug")) NekoGui::dataStore->flag_debug = true; if (NekoGui::dataStore->argv.contains("-flag_linux_run_core_as_admin")) NekoGui::dataStore->flag_linux_run_core_as_admin = true; if (NekoGui::dataStore->argv.contains("-flag_restart_tun_on")) NekoGui::dataStore->flag_restart_tun_on = true; + if (NekoGui::dataStore->argv.contains("-flag_reorder")) NekoGui::dataStore->flag_reorder = true; #ifdef NKR_CPP_USE_APPDATA NekoGui::dataStore->flag_use_appdata = true; // Example: Package & MacOS #endif