-
Notifications
You must be signed in to change notification settings - Fork 2
/
RuntimeConfigParser.cpp
135 lines (105 loc) · 3.54 KB
/
RuntimeConfigParser.cpp
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
#include "RuntimeConfigParser.h"
#include "HelperFunctions.h"
#include "Configuration.h"
#include <QDir>
RuntimeConfigParser::RuntimeConfigParser() :
mRecentFiles{},
mIsWelcomeDialogEnabledOnStartup(gui::SHOW_WELCOME_DIALOG_ON_STARTUP),
mLastFilePath(QDir::homePath()) // default values
{}
bool RuntimeConfigParser::LoadRuntimeConfig(const QString& pPath)
{
QFile loadFile(pPath);
if (!loadFile.open(QIODevice::ReadOnly))
{
return false;
}
QByteArray rawData = loadFile.readAll();
QJsonObject json(QJsonDocument::fromJson(rawData).object());
if (json.contains(file::runtime_config::JSON_WELCOME_DIALOG_ENABLED_IDENTIFIER))
{
mIsWelcomeDialogEnabledOnStartup = json[file::runtime_config::JSON_WELCOME_DIALOG_ENABLED_IDENTIFIER].toBool();
}
if (json.contains(file::runtime_config::JSON_RECENT_FILES_IDENTIFIER))
{
auto recentFiles = json[file::runtime_config::JSON_RECENT_FILES_IDENTIFIER].toArray();
for (uint32_t fileIdx = 0; fileIdx < recentFiles.size(); fileIdx++)
{
auto path = recentFiles[fileIdx].toString();
mRecentFiles.push_back(QFileInfo(path));
}
}
if (json.contains(file::runtime_config::JSON_LAST_FILE_PATH_IDENTIFIER))
{
mLastFilePath = json[file::runtime_config::JSON_LAST_FILE_PATH_IDENTIFIER].toString();
}
return true;
}
bool RuntimeConfigParser::SaveRuntimeConfig(const QString& pPath)
{
QFile saveFile(pPath);
if (!saveFile.open(QIODevice::WriteOnly))
{
return false;
}
auto json = QJsonObject();
if (mRecentFiles.size() > 5)
{
mRecentFiles.resize(5); // New recent files have to be appended to front
}
QJsonArray recentFiles;
for (const auto& filePath : mRecentFiles)
{
recentFiles.append(QJsonValue(filePath.filePath()));
}
json[file::runtime_config::JSON_WELCOME_DIALOG_ENABLED_IDENTIFIER] = mIsWelcomeDialogEnabledOnStartup;
json[file::runtime_config::JSON_RECENT_FILES_IDENTIFIER] = recentFiles;
json[file::runtime_config::JSON_LAST_FILE_PATH_IDENTIFIER] = mLastFilePath;
saveFile.write(QJsonDocument(json).toJson());
return true;
}
bool RuntimeConfigParser::IsWelcomeDialogEnabledOnStartup() const
{
return mIsWelcomeDialogEnabledOnStartup;
}
void RuntimeConfigParser::IsWelcomeDialogEnabledOnStartup(bool pShowOnStartup)
{
mIsWelcomeDialogEnabledOnStartup = pShowOnStartup;
SaveRuntimeConfig(GetRuntimeConfigAbsolutePath());
}
const std::vector<QFileInfo>& RuntimeConfigParser::GetRecentFilePaths() const
{
return mRecentFiles;
}
void RuntimeConfigParser::AddRecentFilePath(const QFileInfo &pFilePath)
{
auto pos = std::find(mRecentFiles.begin(), mRecentFiles.end(), pFilePath);
if (pos != mRecentFiles.end())
{
mRecentFiles.erase(pos);
}
mRecentFiles.insert(mRecentFiles.begin(), pFilePath);
if (mRecentFiles.size() > 5)
{
mRecentFiles.resize(5);
}
SaveRuntimeConfig(GetRuntimeConfigAbsolutePath());
}
void RuntimeConfigParser::RemoveRecentFilePath(const QFileInfo &pFilePath)
{
auto pos = std::find(mRecentFiles.begin(), mRecentFiles.end(), pFilePath);
if (pos != mRecentFiles.end())
{
mRecentFiles.erase(pos);
}
SaveRuntimeConfig(GetRuntimeConfigAbsolutePath());
}
const QString& RuntimeConfigParser::GetLastFilePath() const
{
return mLastFilePath;
}
void RuntimeConfigParser::SetLastFilePath(const QString& pLastFilePath)
{
mLastFilePath = pLastFilePath;
SaveRuntimeConfig(GetRuntimeConfigAbsolutePath());
}