From 70cc233165b5efa3a3888af34f7afce095fe6947 Mon Sep 17 00:00:00 2001 From: rtk0c Date: Mon, 29 Mar 2021 17:55:02 -0700 Subject: More work on project tab --- core/src/Model/GlobalStates.cpp | 110 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 core/src/Model/GlobalStates.cpp (limited to 'core/src/Model/GlobalStates.cpp') diff --git a/core/src/Model/GlobalStates.cpp b/core/src/Model/GlobalStates.cpp new file mode 100644 index 0000000..cd076f4 --- /dev/null +++ b/core/src/Model/GlobalStates.cpp @@ -0,0 +1,110 @@ +#include "GlobalStates.hpp" + +#include "Model/Project.hpp" +#include "Utils/StandardDirectories.hpp" + +#include +#include +#include +#include +#include +#include +#include + +namespace fs = std::filesystem; + +static std::unique_ptr globalStateInstance; +static fs::path globalDataPath; + +void GlobalStates::Init() { + globalStateInstance = std::make_unique(); + globalDataPath = StandardDirectories::UserData() / "cplt"; + fs::create_directories(globalDataPath); + + // Reading recent projects + [&]() { + std::ifstream ifs(globalDataPath / "recents.json"); + if (!ifs) return; + + Json::Value root; + ifs >> root; + + if (!root.isObject()) return; + if (auto& recents = root["RecentProjects"]; recents.isArray()) { + for (auto& elm : recents) { + if (!elm.isString()) continue; + + fs::path path(elm.asCString()); + if (!fs::exists(path)) continue; + + auto utf8String = path.string(); + globalStateInstance->mRecentProjects.push_back(RecentProject{ + .path = std::move(path), + .cachedUtf8String = std::move(utf8String), + }); + } + } + }(); +} + +void GlobalStates::Shutdown() { + if (!globalStateInstance) return; + if (globalStateInstance->mDirty) { + globalStateInstance->WriteToDisk(); + } +} + +GlobalStates& GlobalStates::GetInstance() { + return *globalStateInstance; +} + +const std::filesystem::path& GlobalStates::GetUserDataPath() { + return globalDataPath; +} + +const std::vector& GlobalStates::GetRecentProjects() const { + return mRecentProjects; +} + +void GlobalStates::ClearRecentProjects() { + mRecentProjects.clear(); + MarkDirty(); +} + +void GlobalStates::AddRecentProject(const Project& project) { + mRecentProjects.push_back(RecentProject{ + .path = project.GetPath(), + .cachedUtf8String = project.GetPath().string(), + }); + MarkDirty(); +} + +void GlobalStates::RemoveRecentProject(int idx) { + assert(idx >= 0 && idx < mRecentProjects.size()); + + mRecentProjects.erase(mRecentProjects.begin() + idx); + MarkDirty(); +} + +void GlobalStates::WriteToDisk() const { + Json::Value root; + + auto& recentProjects = root["RecentProjects"] = Json::Value(Json::arrayValue); + for (auto& [path, _] : mRecentProjects) { + recentProjects.append(Json::Value(path.string())); + } + + std::ofstream ofs(globalDataPath / "recents.json"); + ofs << root; + + mDirty = false; +} + +bool GlobalStates::IsDirty() const { + return mDirty; +} + +void GlobalStates::MarkDirty() { + mDirty = true; + OnModified(); +} -- cgit v1.2.3-70-g09d2