blob: a449afb6924793cf2edbecd1fa22da82a6eed4f2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
#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()
{
Init(StandardDirectories::UserData() / "cplt");
}
void GlobalStates::Init(std::filesystem::path userDataDir)
{
globalStateInstance = std::make_unique<GlobalStates>();
globalDataPath = userDataDir;
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;
globalStateInstance->SetCurrentProject(nullptr);
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::MoveProjectToTop(const Project& project)
{
for (auto it = mRecentProjects.begin(); it != mRecentProjects.end(); ++it) {
if (it->Path == project.GetPath()) {
std::rotate(it, it + 1, mRecentProjects.end());
MarkDirty();
return;
}
}
AddRecentProject(project);
}
void GlobalStates::RemoveRecentProject(int idx)
{
assert(idx >= 0 && idx < mRecentProjects.size());
mRecentProjects.erase(mRecentProjects.begin() + idx);
MarkDirty();
}
bool GlobalStates::HasCurrentProject() const
{
return mCurrentProject != nullptr;
}
Project* GlobalStates::GetCurrentProject() const
{
return mCurrentProject.get();
}
void GlobalStates::SetCurrentProject(std::unique_ptr<Project> project)
{
if (mCurrentProject) {
mCurrentProject->WriteToDisk();
mCurrentProject = nullptr;
}
if (project) {
MoveProjectToTop(*project);
}
mCurrentProject = std::move(project);
}
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();
}
|