From b5f212842dc0c2dd6356bf49a28b5d433bc8ad17 Mon Sep 17 00:00:00 2001 From: Saturneric Date: Sun, 23 Jan 2022 21:17:48 +0800 Subject: (gpg, ui, project): tidy up codes and comments. 1. let GpgInfo get into namespace GpgFrontend. 2. adjust the code structure. 3. add license statement to project configuration file. --- src/ui/struct/SettingsObject.cpp | 65 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/ui/struct/SettingsObject.cpp (limited to 'src/ui/struct/SettingsObject.cpp') diff --git a/src/ui/struct/SettingsObject.cpp b/src/ui/struct/SettingsObject.cpp new file mode 100644 index 00000000..72ee279e --- /dev/null +++ b/src/ui/struct/SettingsObject.cpp @@ -0,0 +1,65 @@ +/** + * Copyright (C) 2021 Saturneric + * + * This file is part of GpgFrontend. + * + * GpgFrontend is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GpgFrontend is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GpgFrontend. If not, see . + * + * The initial version of the source code is inherited from + * 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. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#include "SettingsObject.h" + +nlohmann::json& GpgFrontend::UI::SettingsObject::Check( + const std::string& key, nlohmann::json default_value) { + if (!nlohmann::json::contains(key)) + nlohmann::json::operator[](key) = std::move(default_value); + return nlohmann::json::operator[](key); +} + +GpgFrontend::UI::SettingsObject GpgFrontend::UI::SettingsObject::Check( + const std::string& key) { + if (!nlohmann::json::contains(key)) nlohmann::json::operator[](key) = {}; + return SettingsObject{nlohmann::json::operator[](key), false}; +} + +GpgFrontend::UI::SettingsObject::SettingsObject(std::string settings_name) + : settings_name_(std::move(settings_name)) { + try { + auto _json_optional = + GlobalSettingStation::GetInstance().GetDataObject(settings_name_); + + if (_json_optional.has_value()) { + nlohmann::json::operator=(_json_optional.value()); + } + + } catch (...) { + } +} + +GpgFrontend::UI::SettingsObject::SettingsObject(nlohmann::json _sub_json, bool) + : nlohmann::json(std::move(_sub_json)), settings_name_({}) {} + +GpgFrontend::UI::SettingsObject::~SettingsObject() { + if (!settings_name_.empty()) + GpgFrontend::UI::GlobalSettingStation::GetInstance().SaveDataObj( + settings_name_, *this); +} -- cgit v1.2.3 From 3780c1baab8562e15eade1c1be25ccebd0e70814 Mon Sep 17 00:00:00 2001 From: Saturneric Date: Wed, 2 Feb 2022 14:47:24 +0800 Subject: (ui): Repair and tidy the signal and slot docking 1. Use more modern ways. 2. Repair partial docking. --- src/ui/struct/SettingsObject.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'src/ui/struct/SettingsObject.cpp') diff --git a/src/ui/struct/SettingsObject.cpp b/src/ui/struct/SettingsObject.cpp index 72ee279e..31e71e46 100644 --- a/src/ui/struct/SettingsObject.cpp +++ b/src/ui/struct/SettingsObject.cpp @@ -30,9 +30,14 @@ nlohmann::json& GpgFrontend::UI::SettingsObject::Check( const std::string& key, nlohmann::json default_value) { - if (!nlohmann::json::contains(key)) - nlohmann::json::operator[](key) = std::move(default_value); - return nlohmann::json::operator[](key); + try { + if (!this->nlohmann::json::contains(key)) + this->nlohmann::json::operator[](key) = std::move(default_value); + return this->nlohmann::json::operator[](key); + } catch (const std::exception& e) { + LOG(ERROR) << e.what(); + throw e; + } } GpgFrontend::UI::SettingsObject GpgFrontend::UI::SettingsObject::Check( -- cgit v1.2.3 From cacca627a62ab2eba9eb4d37cfea40629ca0a89a Mon Sep 17 00:00:00 2001 From: Saturneric Date: Sun, 6 Feb 2022 17:34:14 +0800 Subject: (src): Move and split the Global Settings Station 1. Move Global Settings Station to core 2. Separate the logic of DataObject 3. Resolve dependencies --- 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 31e71e46..1bd426a6 100644 --- a/src/ui/struct/SettingsObject.cpp +++ b/src/ui/struct/SettingsObject.cpp @@ -50,7 +50,7 @@ GpgFrontend::UI::SettingsObject::SettingsObject(std::string settings_name) : settings_name_(std::move(settings_name)) { try { auto _json_optional = - GlobalSettingStation::GetInstance().GetDataObject(settings_name_); + GpgFrontend::DataObjectOperator::GetInstance().GetDataObject(settings_name_); if (_json_optional.has_value()) { nlohmann::json::operator=(_json_optional.value()); @@ -65,6 +65,6 @@ GpgFrontend::UI::SettingsObject::SettingsObject(nlohmann::json _sub_json, bool) GpgFrontend::UI::SettingsObject::~SettingsObject() { if (!settings_name_.empty()) - GpgFrontend::UI::GlobalSettingStation::GetInstance().SaveDataObj( + GpgFrontend::DataObjectOperator::GetInstance().SaveDataObj( settings_name_, *this); } -- cgit v1.2.3 From aa3e4dcef32828707f9da8d9800ac13caa24c547 Mon Sep 17 00:00:00 2001 From: Saturneric Date: Sun, 6 Feb 2022 21:13:03 +0800 Subject: (ui): Improve the security settings auxiliary class 1. Do a type check 2. Do a null check 3. Add some necessary logs --- src/ui/struct/SettingsObject.cpp | 56 +++++++++++++++++++++++++++++++++------- 1 file changed, 46 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 1bd426a6..5405611e 100644 --- a/src/ui/struct/SettingsObject.cpp +++ b/src/ui/struct/SettingsObject.cpp @@ -29,12 +29,29 @@ #include "SettingsObject.h" nlohmann::json& GpgFrontend::UI::SettingsObject::Check( - const std::string& key, nlohmann::json default_value) { + const std::string& key, const nlohmann::json& default_value) { + // check if the self null + if (this->nlohmann::json::is_null()) { + LOG(INFO) << "SettingsObject is null, creating new one"; + this->nlohmann::json::operator=(nlohmann::json::object()); + } + + LOG(INFO) << "Checking key: " << key; + try { - if (!this->nlohmann::json::contains(key)) - this->nlohmann::json::operator[](key) = std::move(default_value); - return this->nlohmann::json::operator[](key); - } catch (const std::exception& e) { + if (!this->nlohmann::json::contains(key) || + this->nlohmann::json::at(key).is_null() || + this->nlohmann::json::at(key).type() != default_value.type()) { + LOG(INFO) << "Added missing key: " << key; + if (default_value.is_null()) { + LOG(WARNING) << "Default value is null, using empty object"; + this->nlohmann::json::operator[](key) = nlohmann::json::object(); + } else { + this->nlohmann::json::operator[](key) = default_value; + } + } + return this->nlohmann::json::at(key); + } catch (nlohmann::json::exception& e) { LOG(ERROR) << e.what(); throw e; } @@ -42,7 +59,20 @@ nlohmann::json& GpgFrontend::UI::SettingsObject::Check( GpgFrontend::UI::SettingsObject GpgFrontend::UI::SettingsObject::Check( const std::string& key) { - if (!nlohmann::json::contains(key)) nlohmann::json::operator[](key) = {}; + // check if the self null + if (this->nlohmann::json::is_null()) { + LOG(INFO) << "SettingsObject is null, creating new one"; + this->nlohmann::json::operator=(nlohmann::json::object()); + } + + LOG(INFO) << "Checking key: " << key; + + if (!nlohmann::json::contains(key) || + this->nlohmann::json::at(key).is_null() || + this->nlohmann::json::at(key).type() != nlohmann::json::value_t::object) { + LOG(INFO) << "Added missing key: " << key; + this->nlohmann::json::operator[](key) = nlohmann::json::object(); + } return SettingsObject{nlohmann::json::operator[](key), false}; } @@ -50,13 +80,19 @@ GpgFrontend::UI::SettingsObject::SettingsObject(std::string settings_name) : settings_name_(std::move(settings_name)) { try { auto _json_optional = - GpgFrontend::DataObjectOperator::GetInstance().GetDataObject(settings_name_); + GpgFrontend::DataObjectOperator::GetInstance().GetDataObject( + settings_name_); if (_json_optional.has_value()) { + LOG(INFO) << "SettingsObject: " << settings_name_ << " loaded."; nlohmann::json::operator=(_json_optional.value()); + } else { + LOG(INFO) << "SettingsObject: " << settings_name_ << " not found."; + nlohmann::json::operator=({}); } - } catch (...) { + } catch (std::exception& e) { + LOG(ERROR) << e.what(); } } @@ -65,6 +101,6 @@ GpgFrontend::UI::SettingsObject::SettingsObject(nlohmann::json _sub_json, bool) GpgFrontend::UI::SettingsObject::~SettingsObject() { if (!settings_name_.empty()) - GpgFrontend::DataObjectOperator::GetInstance().SaveDataObj( - settings_name_, *this); + GpgFrontend::DataObjectOperator::GetInstance().SaveDataObj(settings_name_, + *this); } -- cgit v1.2.3 From e6f6731ad82fc4b7dda4267840df074f992f7442 Mon Sep 17 00:00:00 2001 From: Saturneric Date: Mon, 7 Feb 2022 19:55:13 +0800 Subject: (core, ui): Fix the remaining problems in the data object --- src/ui/struct/SettingsObject.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'src/ui/struct/SettingsObject.cpp') diff --git a/src/ui/struct/SettingsObject.cpp b/src/ui/struct/SettingsObject.cpp index 5405611e..63df0900 100644 --- a/src/ui/struct/SettingsObject.cpp +++ b/src/ui/struct/SettingsObject.cpp @@ -36,12 +36,11 @@ nlohmann::json& GpgFrontend::UI::SettingsObject::Check( this->nlohmann::json::operator=(nlohmann::json::object()); } - LOG(INFO) << "Checking key: " << key; - try { if (!this->nlohmann::json::contains(key) || this->nlohmann::json::at(key).is_null() || - this->nlohmann::json::at(key).type() != default_value.type()) { + this->nlohmann::json::at(key).type_name() != + default_value.type_name()) { LOG(INFO) << "Added missing key: " << key; if (default_value.is_null()) { LOG(WARNING) << "Default value is null, using empty object"; @@ -65,8 +64,6 @@ GpgFrontend::UI::SettingsObject GpgFrontend::UI::SettingsObject::Check( this->nlohmann::json::operator=(nlohmann::json::object()); } - LOG(INFO) << "Checking key: " << key; - if (!nlohmann::json::contains(key) || this->nlohmann::json::at(key).is_null() || this->nlohmann::json::at(key).type() != nlohmann::json::value_t::object) { -- cgit v1.2.3 From f65f0c7b7e6bf471ec80a1ab95df37a157f977e6 Mon Sep 17 00:00:00 2001 From: Saturneric Date: Sat, 12 Mar 2022 14:18:59 +0800 Subject: (core): Fix the setting loading IO problem under Windows --- src/ui/struct/SettingsObject.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'src/ui/struct/SettingsObject.cpp') diff --git a/src/ui/struct/SettingsObject.cpp b/src/ui/struct/SettingsObject.cpp index 63df0900..4a9aa7d6 100644 --- a/src/ui/struct/SettingsObject.cpp +++ b/src/ui/struct/SettingsObject.cpp @@ -76,6 +76,7 @@ GpgFrontend::UI::SettingsObject GpgFrontend::UI::SettingsObject::Check( GpgFrontend::UI::SettingsObject::SettingsObject(std::string settings_name) : settings_name_(std::move(settings_name)) { try { + LOG(INFO) << "Loading settings from: " << this->settings_name_; auto _json_optional = GpgFrontend::DataObjectOperator::GetInstance().GetDataObject( settings_name_); -- cgit v1.2.3