aboutsummaryrefslogtreecommitdiffstats
path: root/src/ui/struct/SettingsObject.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/ui/struct/SettingsObject.cpp')
-rw-r--r--src/ui/struct/SettingsObject.cpp81
1 files changed, 21 insertions, 60 deletions
diff --git a/src/ui/struct/SettingsObject.cpp b/src/ui/struct/SettingsObject.cpp
index d03a4fe3..cc5e85bf 100644
--- a/src/ui/struct/SettingsObject.cpp
+++ b/src/ui/struct/SettingsObject.cpp
@@ -28,81 +28,42 @@
#include "SettingsObject.h"
-nlohmann::json& GpgFrontend::UI::SettingsObject::Check(
- const QString& key, const nlohmann::json& default_value) {
- // check if the self null
- if (this->nlohmann::json::is_null()) {
- GF_UI_LOG_DEBUG("settings object is null, creating new one");
- this->nlohmann::json::operator=(nlohmann::json::object());
- }
+#include "core/function/DataObjectOperator.h"
- try {
- auto s_key = key.toStdString();
- if (!this->nlohmann::json::contains(s_key) ||
- this->nlohmann::json::at(s_key).is_null() ||
- this->nlohmann::json::at(s_key).type_name() !=
- default_value.type_name()) {
- GF_UI_LOG_DEBUG("added missing key: {}", key);
- if (default_value.is_null()) {
- GF_UI_LOG_WARN("default value is null, using empty object");
- this->nlohmann::json::operator[](s_key) = nlohmann::json::object();
- } else {
- this->nlohmann::json::operator[](s_key) = default_value;
- }
- }
- return this->nlohmann::json::at(s_key);
- } catch (nlohmann::json::exception& e) {
- GF_UI_LOG_ERROR(e.what());
- throw e;
- }
-}
-
-GpgFrontend::UI::SettingsObject GpgFrontend::UI::SettingsObject::Check(
- const QString& key) {
- // check if the self null
- if (this->nlohmann::json::is_null()) {
- GF_UI_LOG_DEBUG("settings object is null, creating new one");
- this->nlohmann::json::operator=(nlohmann::json::object());
- }
+namespace GpgFrontend::UI {
- auto s_key = key.toStdString();
- if (!nlohmann::json::contains(s_key) ||
- this->nlohmann::json::at(s_key).is_null() ||
- this->nlohmann::json::at(s_key).type() !=
- nlohmann::json::value_t::object) {
- GF_UI_LOG_DEBUG("added missing key: {}", key);
- this->nlohmann::json::operator[](s_key) = nlohmann::json::object();
- }
- return SettingsObject{nlohmann::json::operator[](s_key), false};
-}
-
-GpgFrontend::UI::SettingsObject::SettingsObject(QString settings_name)
+SettingsObject::SettingsObject(QString settings_name)
: settings_name_(std::move(settings_name)) {
try {
GF_UI_LOG_DEBUG("loading settings from: {}", this->settings_name_);
- auto _json_optional =
- GpgFrontend::DataObjectOperator::GetInstance().GetDataObject(
- settings_name_);
+ auto json_optional =
+ DataObjectOperator::GetInstance().GetDataObject(settings_name_);
- if (_json_optional.has_value()) {
+ if (json_optional.has_value() && json_optional->isObject()) {
GF_UI_LOG_DEBUG("settings object: {} loaded.", settings_name_);
- nlohmann::json::operator=(_json_optional.value());
+ QJsonObject::operator=(json_optional.value().object());
} else {
GF_UI_LOG_DEBUG("settings object: {} not found.", settings_name_);
- nlohmann::json::operator=({});
+ QJsonObject::operator=({});
}
} catch (std::exception& e) {
- GF_UI_LOG_ERROR(e.what());
+ GF_UI_LOG_ERROR("load setting object error: {}", e.what());
}
}
-GpgFrontend::UI::SettingsObject::SettingsObject(nlohmann::json _sub_json, bool)
- : nlohmann::json(std::move(_sub_json)) {}
+SettingsObject::SettingsObject(QJsonObject sub_json)
+ : QJsonObject(std::move(sub_json)) {}
-GpgFrontend::UI::SettingsObject::~SettingsObject() {
+SettingsObject::~SettingsObject() {
if (!settings_name_.isEmpty()) {
- GpgFrontend::DataObjectOperator::GetInstance().SaveDataObj(settings_name_,
- *this);
+ DataObjectOperator::GetInstance().SaveDataObj(settings_name_,
+ QJsonDocument(*this));
}
-} \ No newline at end of file
+}
+
+void SettingsObject::Store(const QJsonObject& json) {
+ auto* parent = (static_cast<QJsonObject*>(this));
+ *parent = json;
+}
+} // namespace GpgFrontend::UI \ No newline at end of file