diff options
author | Saturneric <[email protected]> | 2022-01-12 14:57:14 +0000 |
---|---|---|
committer | Saturneric <[email protected]> | 2022-01-12 14:57:14 +0000 |
commit | 1b18bc1457ddc5165430a81e34cb40a1dd29d6b4 (patch) | |
tree | 1f960754e26e442bc811930175e5a19de1456eb8 /src | |
parent | <refactor>(ui): Refactoring some settings based on encrypted object settings ... (diff) | |
download | GpgFrontend-1b18bc1457ddc5165430a81e34cb40a1dd29d6b4.tar.gz GpgFrontend-1b18bc1457ddc5165430a81e34cb40a1dd29d6b4.zip |
<refactor, fix>(ui): Refactored settings object to fix issues.
1. Fixed the problem of duplicate storage of unnamed settings objects.
2. Rename the settings object.
Diffstat (limited to 'src')
-rw-r--r-- | src/ui/MainWindow.cpp | 16 | ||||
-rw-r--r-- | src/ui/data_struct/SettingsObject.cpp (renamed from src/ui/data_struct/SettingsObj.cpp) | 18 | ||||
-rw-r--r-- | src/ui/data_struct/SettingsObject.h (renamed from src/ui/data_struct/SettingsObj.h) | 19 | ||||
-rw-r--r-- | src/ui/settings/SettingsSendMail.cpp | 6 |
4 files changed, 31 insertions, 28 deletions
diff --git a/src/ui/MainWindow.cpp b/src/ui/MainWindow.cpp index a528184f..2cab0f38 100644 --- a/src/ui/MainWindow.cpp +++ b/src/ui/MainWindow.cpp @@ -29,7 +29,7 @@ #include "ui/thread/VersionCheckThread.h" #endif #include "ui/SignalStation.h" -#include "ui/data_struct/SettingsObj.h" +#include "ui/data_struct/SettingsObject.h" #include "ui/settings/GlobalSettingStation.h" namespace GpgFrontend::UI { @@ -144,7 +144,7 @@ void MainWindow::restoreSettings() { LOG(INFO) << _("Called"); try { - SettingsObj main_windows_state("main_windows_state"); + SettingsObject main_windows_state("main_windows_state"); std::string window_state = main_windows_state.Check( "window_state", saveState().toBase64().toStdString()); @@ -189,7 +189,7 @@ void MainWindow::restoreSettings() { this->setIconSize(QSize(width, height)); importButton->setIconSize(QSize(width, height)); - SettingsObj key_server_json("key_server"); + SettingsObject key_server_json("key_server"); if (!key_server_json.contains("server_list")) { key_server_json["server_list"] = {"https://keyserver.ubuntu.com", @@ -223,7 +223,7 @@ void MainWindow::restoreSettings() { try { // Checked Keys - SettingsObj default_key_checked("default_key_checked"); + SettingsObject default_key_checked("default_key_checked"); if (save_key_checked) { auto key_ids_ptr = std::make_unique<KeyIdArgsList>(); for (auto& it : default_key_checked) { @@ -237,7 +237,7 @@ void MainWindow::restoreSettings() { LOG(ERROR) << "restore default_key_checked failed"; } - SettingsObj smtp_passport("smtp_passport"); + SettingsObject smtp_passport("smtp_passport"); smtp_passport.Check("enable", false); prohibit_update_checking_ = false; @@ -260,7 +260,7 @@ void MainWindow::saveSettings() { auto& settings = GlobalSettingStation::GetInstance().GetUISettings(); try { - SettingsObj main_windows_state("main_windows_state"); + SettingsObject main_windows_state("main_windows_state"); // window position and size main_windows_state["window_state"] = saveState().toBase64().toStdString(); @@ -276,9 +276,9 @@ void MainWindow::saveSettings() { if (save_key_checked) { auto key_ids_need_to_store = mKeyList->getChecked(); - SettingsObj default_key_checked("default_key_checked"); + SettingsObject default_key_checked("default_key_checked"); default_key_checked.clear(); - + for (const auto& key_id : *key_ids_need_to_store) default_key_checked.push_back(key_id); } else { diff --git a/src/ui/data_struct/SettingsObj.cpp b/src/ui/data_struct/SettingsObject.cpp index d069dfe7..d8d1b76c 100644 --- a/src/ui/data_struct/SettingsObj.cpp +++ b/src/ui/data_struct/SettingsObject.cpp @@ -22,22 +22,22 @@ * */ -#include "SettingsObj.h" +#include "SettingsObject.h" -nlohmann::json& GpgFrontend::UI::SettingsObj::Check( +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::SettingsObj GpgFrontend::UI::SettingsObj::Check( +GpgFrontend::UI::SettingsObject GpgFrontend::UI::SettingsObject::Check( const std::string& key) { if (!nlohmann::json::contains(key)) nlohmann::json::operator[](key) = {}; - return SettingsObj{nlohmann::json::operator[](key), false}; + return SettingsObject{nlohmann::json::operator[](key), false}; } -GpgFrontend::UI::SettingsObj::SettingsObj(std::string settings_name) +GpgFrontend::UI::SettingsObject::SettingsObject(std::string settings_name) : settings_name_(std::move(settings_name)) { try { auto _json_optional = @@ -51,5 +51,11 @@ GpgFrontend::UI::SettingsObj::SettingsObj(std::string settings_name) } } -GpgFrontend::UI::SettingsObj::SettingsObj(nlohmann::json _sub_json, bool) +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); +} diff --git a/src/ui/data_struct/SettingsObj.h b/src/ui/data_struct/SettingsObject.h index ee3da705..af63b74f 100644 --- a/src/ui/data_struct/SettingsObj.h +++ b/src/ui/data_struct/SettingsObject.h @@ -22,8 +22,8 @@ * */ -#ifndef GPGFRONTEND_SETTINGSOBJ_H -#define GPGFRONTEND_SETTINGSOBJ_H +#ifndef GPGFRONTEND_SETTINGSOBJECT_H +#define GPGFRONTEND_SETTINGSOBJECT_H #include <utility> @@ -31,24 +31,21 @@ namespace GpgFrontend::UI { -class SettingsObj : public nlohmann::json { +class SettingsObject : public nlohmann::json { public: - explicit SettingsObj(std::string settings_name); + explicit SettingsObject(std::string settings_name); - explicit SettingsObj(nlohmann::json _sub_json, bool); + explicit SettingsObject(nlohmann::json _sub_json, bool); - ~SettingsObj() { - GpgFrontend::UI::GlobalSettingStation::GetInstance().SaveDataObj( - settings_name_, *this); - } + ~SettingsObject(); nlohmann::json& Check(const std::string& key, nlohmann::json default_value); - SettingsObj Check(const std::string& key); + SettingsObject Check(const std::string& key); private: std::string settings_name_; }; } // namespace GpgFrontend::UI -#endif // GPGFRONTEND_SETTINGSOBJ_H +#endif // GPGFRONTEND_SETTINGSOBJECT_H diff --git a/src/ui/settings/SettingsSendMail.cpp b/src/ui/settings/SettingsSendMail.cpp index 680cd07f..5408b535 100644 --- a/src/ui/settings/SettingsSendMail.cpp +++ b/src/ui/settings/SettingsSendMail.cpp @@ -28,7 +28,7 @@ #include "smtp/SmtpMime" #endif -#include "ui/data_struct/SettingsObj.h" +#include "ui/data_struct/SettingsObject.h" #include "ui/thread/SMTPSendMailThread.h" #include "ui/thread/SMTPTestThread.h" #include "ui_SendMailSettings.h" @@ -97,7 +97,7 @@ SendMailTab::SendMailTab(QWidget* parent) } void SendMailTab::setSettings() { - auto smtp_passport = SettingsObj("smtp_passport"); + auto smtp_passport = SettingsObject("smtp_passport"); ui->smtpServerAddressEdit->setText( std::string{smtp_passport.Check("smtp_address", {})}.c_str()); @@ -138,7 +138,7 @@ void SendMailTab::setSettings() { void SendMailTab::applySettings() { try { - auto smtp_passport = SettingsObj("smtp_passport"); + auto smtp_passport = SettingsObject("smtp_passport"); smtp_passport["smtp_address"] = ui->smtpServerAddressEdit->text().toStdString(); |