From 95997d27106daf91336847f50efaaa32279b7fc7 Mon Sep 17 00:00:00 2001 From: saturneric Date: Mon, 16 Oct 2023 17:54:05 +0800 Subject: fix: check and update copyright at files --- src/ui/struct/SettingsObject.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/ui/struct/SettingsObject.cpp') diff --git a/src/ui/struct/SettingsObject.cpp b/src/ui/struct/SettingsObject.cpp index d5230089..4b60410c 100644 --- a/src/ui/struct/SettingsObject.cpp +++ b/src/ui/struct/SettingsObject.cpp @@ -1,5 +1,5 @@ /** - * Copyright (C) 2021 Saturneric + * Copyright (C) 2021 Saturneric * * This file is part of GpgFrontend. * @@ -20,7 +20,7 @@ * the gpg4usb project, which is under GPL-3.0-or-later. * * All the source code of GpgFrontend was modified and released by - * Saturneric starting on May 12, 2021. + * Saturneric starting on May 12, 2021. * * SPDX-License-Identifier: GPL-3.0-or-later * -- cgit v1.2.3 From 644aa4397b03dbef73f8bfedc13925b51cad836b Mon Sep 17 00:00:00 2001 From: saturneric Date: Fri, 5 Jan 2024 20:55:15 +0800 Subject: feat: integrate logging api to core --- src/ui/struct/SettingsObject.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'src/ui/struct/SettingsObject.cpp') diff --git a/src/ui/struct/SettingsObject.cpp b/src/ui/struct/SettingsObject.cpp index 4b60410c..cf11fc5e 100644 --- a/src/ui/struct/SettingsObject.cpp +++ b/src/ui/struct/SettingsObject.cpp @@ -32,7 +32,7 @@ nlohmann::json& GpgFrontend::UI::SettingsObject::Check( const std::string& key, const nlohmann::json& default_value) { // check if the self null if (this->nlohmann::json::is_null()) { - SPDLOG_DEBUG("settings object is null, creating new one"); + GF_UI_LOG_DEBUG("settings object is null, creating new one"); this->nlohmann::json::operator=(nlohmann::json::object()); } @@ -41,9 +41,9 @@ nlohmann::json& GpgFrontend::UI::SettingsObject::Check( this->nlohmann::json::at(key).is_null() || this->nlohmann::json::at(key).type_name() != default_value.type_name()) { - SPDLOG_DEBUG("added missing key: {}", key); + GF_UI_LOG_DEBUG("added missing key: {}", key); if (default_value.is_null()) { - SPDLOG_WARN("default value is null, using empty object"); + GF_UI_LOG_WARN("default value is null, using empty object"); this->nlohmann::json::operator[](key) = nlohmann::json::object(); } else { this->nlohmann::json::operator[](key) = default_value; @@ -51,7 +51,7 @@ nlohmann::json& GpgFrontend::UI::SettingsObject::Check( } return this->nlohmann::json::at(key); } catch (nlohmann::json::exception& e) { - SPDLOG_ERROR(e.what()); + GF_UI_LOG_ERROR(e.what()); throw e; } } @@ -60,14 +60,14 @@ GpgFrontend::UI::SettingsObject GpgFrontend::UI::SettingsObject::Check( const std::string& key) { // check if the self null if (this->nlohmann::json::is_null()) { - SPDLOG_DEBUG("settings object is null, creating new one"); + GF_UI_LOG_DEBUG("settings object is null, creating new one"); this->nlohmann::json::operator=(nlohmann::json::object()); } if (!nlohmann::json::contains(key) || this->nlohmann::json::at(key).is_null() || this->nlohmann::json::at(key).type() != nlohmann::json::value_t::object) { - SPDLOG_DEBUG("added missing key: {}", key); + GF_UI_LOG_DEBUG("added missing key: {}", key); this->nlohmann::json::operator[](key) = nlohmann::json::object(); } return SettingsObject{nlohmann::json::operator[](key), false}; @@ -76,21 +76,21 @@ GpgFrontend::UI::SettingsObject GpgFrontend::UI::SettingsObject::Check( GpgFrontend::UI::SettingsObject::SettingsObject(std::string settings_name) : settings_name_(std::move(settings_name)) { try { - SPDLOG_DEBUG("loading settings from: {}", this->settings_name_); + GF_UI_LOG_DEBUG("loading settings from: {}", this->settings_name_); auto _json_optional = GpgFrontend::DataObjectOperator::GetInstance().GetDataObject( settings_name_); if (_json_optional.has_value()) { - SPDLOG_DEBUG("settings object: {} loaded.", settings_name_); + GF_UI_LOG_DEBUG("settings object: {} loaded.", settings_name_); nlohmann::json::operator=(_json_optional.value()); } else { - SPDLOG_DEBUG("settings object: {} not found.", settings_name_); + GF_UI_LOG_DEBUG("settings object: {} not found.", settings_name_); nlohmann::json::operator=({}); } } catch (std::exception& e) { - SPDLOG_ERROR(e.what()); + GF_UI_LOG_ERROR(e.what()); } } -- cgit v1.2.3 From bf538056b24a68b8fd235b1c50991ee8eb46a776 Mon Sep 17 00:00:00 2001 From: saturneric Date: Fri, 12 Jan 2024 14:02:37 +0800 Subject: refactor: use QString instead of std::string and improve threading system --- src/ui/struct/SettingsObject.cpp | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) (limited to 'src/ui/struct/SettingsObject.cpp') diff --git a/src/ui/struct/SettingsObject.cpp b/src/ui/struct/SettingsObject.cpp index cf11fc5e..d03a4fe3 100644 --- a/src/ui/struct/SettingsObject.cpp +++ b/src/ui/struct/SettingsObject.cpp @@ -29,7 +29,7 @@ #include "SettingsObject.h" nlohmann::json& GpgFrontend::UI::SettingsObject::Check( - const std::string& key, const nlohmann::json& default_value) { + 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"); @@ -37,19 +37,20 @@ nlohmann::json& GpgFrontend::UI::SettingsObject::Check( } try { - if (!this->nlohmann::json::contains(key) || - this->nlohmann::json::at(key).is_null() || - this->nlohmann::json::at(key).type_name() != + 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[](key) = nlohmann::json::object(); + this->nlohmann::json::operator[](s_key) = nlohmann::json::object(); } else { - this->nlohmann::json::operator[](key) = default_value; + this->nlohmann::json::operator[](s_key) = default_value; } } - return this->nlohmann::json::at(key); + return this->nlohmann::json::at(s_key); } catch (nlohmann::json::exception& e) { GF_UI_LOG_ERROR(e.what()); throw e; @@ -57,23 +58,25 @@ nlohmann::json& GpgFrontend::UI::SettingsObject::Check( } GpgFrontend::UI::SettingsObject GpgFrontend::UI::SettingsObject::Check( - const std::string& key) { + 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()); } - if (!nlohmann::json::contains(key) || - this->nlohmann::json::at(key).is_null() || - this->nlohmann::json::at(key).type() != nlohmann::json::value_t::object) { + 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[](key) = nlohmann::json::object(); + this->nlohmann::json::operator[](s_key) = nlohmann::json::object(); } - return SettingsObject{nlohmann::json::operator[](key), false}; + return SettingsObject{nlohmann::json::operator[](s_key), false}; } -GpgFrontend::UI::SettingsObject::SettingsObject(std::string settings_name) +GpgFrontend::UI::SettingsObject::SettingsObject(QString settings_name) : settings_name_(std::move(settings_name)) { try { GF_UI_LOG_DEBUG("loading settings from: {}", this->settings_name_); @@ -95,10 +98,11 @@ GpgFrontend::UI::SettingsObject::SettingsObject(std::string settings_name) } GpgFrontend::UI::SettingsObject::SettingsObject(nlohmann::json _sub_json, bool) - : nlohmann::json(std::move(_sub_json)), settings_name_({}) {} + : nlohmann::json(std::move(_sub_json)) {} GpgFrontend::UI::SettingsObject::~SettingsObject() { - if (!settings_name_.empty()) + if (!settings_name_.isEmpty()) { GpgFrontend::DataObjectOperator::GetInstance().SaveDataObj(settings_name_, *this); + } } \ No newline at end of file -- cgit v1.2.3 From 6c632d70b391f8b317c68f7db8cfd217f9370995 Mon Sep 17 00:00:00 2001 From: saturneric Date: Mon, 15 Jan 2024 17:22:32 +0800 Subject: feat: use qt json support components in data object and infos gathering module --- src/ui/struct/SettingsObject.cpp | 81 +++++++++++----------------------------- 1 file changed, 21 insertions(+), 60 deletions(-) (limited to 'src/ui/struct/SettingsObject.cpp') 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(this)); + *parent = json; +} +} // namespace GpgFrontend::UI \ No newline at end of file -- cgit v1.2.3