diff options
author | rtk0c <[email protected]> | 2021-03-29 17:55:02 -0700 |
---|---|---|
committer | rtk0c <[email protected]> | 2021-03-29 17:55:02 -0700 |
commit | 70cc233165b5efa3a3888af34f7afce095fe6947 (patch) | |
tree | b15e8f2e3816acc204846188e78514f2ba6ad816 /core/src/Model/GlobalStates.cpp | |
parent | 6032ae108064650324b2c45352e1baa5b36961cc (diff) |
More work on project tab
Diffstat (limited to 'core/src/Model/GlobalStates.cpp')
-rw-r--r-- | core/src/Model/GlobalStates.cpp | 110 |
1 files changed, 110 insertions, 0 deletions
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 <json/reader.h> +#include <json/value.h> +#include <json/writer.h> +#include <cassert> +#include <filesystem> +#include <fstream> +#include <memory> + +namespace fs = std::filesystem; + +static std::unique_ptr<GlobalStates> globalStateInstance; +static fs::path globalDataPath; + +void GlobalStates::Init() { + globalStateInstance = std::make_unique<GlobalStates>(); + 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::RecentProject>& 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(); +} |