Saving project settings on shutdown

This commit is contained in:
Nav
2022-01-22 16:14:29 +00:00
parent 051b7e1e8e
commit 9ebd2b9d57
2 changed files with 41 additions and 4 deletions

View File

@@ -130,6 +130,7 @@ void Application::shutdown() {
this->stopTargetController();
this->stopSignalHandler();
this->saveProjectSettings();
Thread::setThreadState(ThreadState::STOPPED);
}
@@ -138,10 +139,14 @@ void Application::loadProjectSettings() {
auto jsonSettingsFile = QFile(QString::fromStdString(projectSettingsPath));
if (jsonSettingsFile.exists()) {
auto jsonObject = QJsonDocument::fromJson(jsonSettingsFile.readAll()).object();
jsonSettingsFile.close();
try {
if (!jsonSettingsFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
throw Exception("Failed to open settings file.");
}
const auto jsonObject = QJsonDocument::fromJson(jsonSettingsFile.readAll()).object();
jsonSettingsFile.close();
this->projectSettings = ProjectSettings(jsonObject);
return;
@@ -155,6 +160,33 @@ void Application::loadProjectSettings() {
this->projectSettings = ProjectSettings();
}
void Application::saveProjectSettings() {
const auto projectSettingsPath = Paths::projectSettingsPath();
auto jsonSettingsFile = QFile(QString::fromStdString(projectSettingsPath));
Logger::debug("Saving project settings to " + projectSettingsPath);
QDir().mkpath(QString::fromStdString(Paths::projectSettingsDirPath()));
try {
const auto jsonDocument = QJsonDocument(this->projectSettings->toJson());
if (!jsonSettingsFile.open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text)) {
throw Exception(
"failed to open/create settings file (" + projectSettingsPath + "). Check file permissions."
);
}
jsonSettingsFile.write(jsonDocument.toJson());
jsonSettingsFile.close();
} catch (const Exception& exception) {
Logger::error(
"Failed to save project settings - " + exception.getMessage()
);
}
}
void Application::loadProjectConfiguration() {
auto jsonConfigFile = QFile(QString::fromStdString(Paths::projectConfigPath()));
@@ -163,7 +195,7 @@ void Application::loadProjectConfiguration() {
+ Paths::projectDirPath());
}
if (!jsonConfigFile.open(QIODevice::ReadOnly)) {
if (!jsonConfigFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
throw InvalidConfig("Failed to load Bloom configuration file (bloom.json) Working directory: "
+ Paths::projectDirPath());
}

View File

@@ -212,6 +212,11 @@ namespace Bloom
*/
void loadProjectSettings();
/**
* Saves the current project settings.
*/
void saveProjectSettings();
/**
* Extracts the project config from the user's JSON config file and populates the following members:
* - this->projectConfig