diff options
author | saturneric <[email protected]> | 2023-11-06 09:17:47 +0000 |
---|---|---|
committer | saturneric <[email protected]> | 2023-11-06 09:17:47 +0000 |
commit | 0251f35c93e3f0e0a6853a50fb5bd82c1b9e4187 (patch) | |
tree | 0025299b6b6bb0be2c41e6798ae7b09d16b52773 | |
parent | feat: update thridparty libraries (diff) | |
download | GpgFrontend-0251f35c93e3f0e0a6853a50fb5bd82c1b9e4187.tar.gz GpgFrontend-0251f35c93e3f0e0a6853a50fb5bd82c1b9e4187.zip |
refactor: clean up core's codes
48 files changed, 666 insertions, 384 deletions
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index c8edc8d7..1692daa9 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -24,6 +24,7 @@ # SPDX-License-Identifier: GPL-3.0-or-later +aux_source_directory(./function/basic CORE_SOURCE) aux_source_directory(./function/result_analyse CORE_SOURCE) aux_source_directory(./function/gpg CORE_SOURCE) aux_source_directory(./function/aes CORE_SOURCE) diff --git a/src/core/GpgConstants.h b/src/core/GpgConstants.h index 2cb33606..cc64d491 100644 --- a/src/core/GpgConstants.h +++ b/src/core/GpgConstants.h @@ -66,7 +66,7 @@ using GpgGenKeyResult = std::shared_ptr<struct _gpgme_op_genkey_result>; ///< * @param result * @return GpgEncrResult */ -GPGFRONTEND_CORE_EXPORT auto NewResult(gpgme_encrypt_result_t&& result) +auto GPGFRONTEND_CORE_EXPORT NewResult(gpgme_encrypt_result_t&& result) -> GpgEncrResult; /** @@ -75,7 +75,7 @@ GPGFRONTEND_CORE_EXPORT auto NewResult(gpgme_encrypt_result_t&& result) * @param result * @return GpgDecrResult */ -GPGFRONTEND_CORE_EXPORT auto NewResult(gpgme_decrypt_result_t&& result) +auto GPGFRONTEND_CORE_EXPORT NewResult(gpgme_decrypt_result_t&& result) -> GpgDecrResult; /** @@ -84,7 +84,7 @@ GPGFRONTEND_CORE_EXPORT auto NewResult(gpgme_decrypt_result_t&& result) * @param result * @return GpgSignResult */ -GPGFRONTEND_CORE_EXPORT auto NewResult(gpgme_sign_result_t&& result) +auto GPGFRONTEND_CORE_EXPORT NewResult(gpgme_sign_result_t&& result) -> GpgSignResult; /** @@ -93,7 +93,7 @@ GPGFRONTEND_CORE_EXPORT auto NewResult(gpgme_sign_result_t&& result) * @param result * @return GpgVerifyResult */ -GPGFRONTEND_CORE_EXPORT auto NewResult(gpgme_verify_result_t&& result) +auto GPGFRONTEND_CORE_EXPORT NewResult(gpgme_verify_result_t&& result) -> GpgVerifyResult; /** @@ -102,7 +102,7 @@ GPGFRONTEND_CORE_EXPORT auto NewResult(gpgme_verify_result_t&& result) * @param result * @return GpgGenKeyResult */ -GPGFRONTEND_CORE_EXPORT auto NewResult(gpgme_genkey_result_t&& result) +auto GPGFRONTEND_CORE_EXPORT NewResult(gpgme_genkey_result_t&& result) -> GpgGenKeyResult; // Error Info Printer @@ -113,7 +113,7 @@ GPGFRONTEND_CORE_EXPORT auto NewResult(gpgme_genkey_result_t&& result) * @param err * @return GpgError */ -GPGFRONTEND_CORE_EXPORT auto CheckGpgError(GpgError err) -> GpgError; +auto GPGFRONTEND_CORE_EXPORT CheckGpgError(GpgError err) -> GpgError; /** * @brief @@ -122,7 +122,7 @@ GPGFRONTEND_CORE_EXPORT auto CheckGpgError(GpgError err) -> GpgError; * @param comment * @return GpgError */ -GPGFRONTEND_CORE_EXPORT auto CheckGpgError(GpgError gpgmeError, +auto GPGFRONTEND_CORE_EXPORT CheckGpgError(GpgError gpgmeError, const std::string& comment) -> GpgError; @@ -133,7 +133,7 @@ GPGFRONTEND_CORE_EXPORT auto CheckGpgError(GpgError gpgmeError, * @param predict * @return gpg_err_code_t */ -GPGFRONTEND_CORE_EXPORT auto CheckGpgError2ErrCode( +auto GPGFRONTEND_CORE_EXPORT CheckGpgError2ErrCode( gpgme_error_t err, gpgme_error_t predict = GPG_ERR_NO_ERROR) -> gpg_err_code_t; @@ -145,7 +145,7 @@ GPGFRONTEND_CORE_EXPORT auto CheckGpgError2ErrCode( * @param fingerprint * @return std::string */ -GPGFRONTEND_CORE_EXPORT auto BeautifyFingerprint(BypeArrayConstRef fingerprint) +auto GPGFRONTEND_CORE_EXPORT BeautifyFingerprint(BypeArrayConstRef fingerprint) -> std::string; // File Operation diff --git a/src/core/GpgCoreInit.cpp b/src/core/GpgCoreInit.cpp index c1db2d9d..e6d7f784 100644 --- a/src/core/GpgCoreInit.cpp +++ b/src/core/GpgCoreInit.cpp @@ -34,10 +34,10 @@ #include <boost/date_time.hpp> -#include "GpgFunctionObject.h" -#include "core/GpgContext.h" #include "core/function/GlobalSettingStation.h" +#include "core/function/basic/GpgFunctionObject.h" #include "core/function/gpg/GpgAdvancedOperator.h" +#include "core/function/gpg/GpgContext.h" #include "core/module/ModuleManager.h" #include "core/thread/Task.h" #include "core/thread/TaskRunner.h" diff --git a/src/core/GpgFunctionObject.cpp b/src/core/GpgFunctionObject.cpp deleted file mode 100644 index 2fc9ee42..00000000 --- a/src/core/GpgFunctionObject.cpp +++ /dev/null @@ -1,136 +0,0 @@ -/** - * Copyright (C) 2021 Saturneric <[email protected]> - * - * 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 <https://www.gnu.org/licenses/>. - * - * 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 <[email protected]> starting on May 12, 2021. - * - * SPDX-License-Identifier: GPL-3.0-or-later - * - */ - -#include "core/GpgFunctionObject.h" - -#include <cassert> -#include <functional> -#include <mutex> -#include <shared_mutex> - -void GpgFrontend::ChannelObject::SetChannel(int channel) { - this->channel_ = channel; -} - -auto GpgFrontend::ChannelObject::GetChannel() const -> int { return channel_; } - -auto GpgFrontend::ChannelObject::GetDefaultChannel() -> int { - return kGpgFrontendDefaultChannel; -} - -void GpgFrontend::SingletonStorage::ReleaseChannel(int channel) { - decltype(instances_map_.end()) ins_it; - { - std::shared_lock<std::shared_mutex> lock(instances_mutex_); - ins_it = instances_map_.find(channel); - } - if (ins_it != instances_map_.end()) instances_map_.erase(ins_it); -} - -auto GpgFrontend::SingletonStorage::FindObjectInChannel(int channel) - -> GpgFrontend::ChannelObject* { - // read instances_map_ - decltype(instances_map_.end()) ins_it; - { - std::shared_lock<std::shared_mutex> lock(instances_mutex_); - ins_it = instances_map_.find(channel); - if (ins_it == instances_map_.end()) { - return nullptr; - } - return ins_it->second.get(); - } -} - -auto GpgFrontend::SingletonStorage::GetAllChannelId() -> std::vector<int> { - std::vector<int> channels; - channels.reserve(instances_map_.size()); - for (const auto& [key, value] : instances_map_) { - channels.push_back(key); - } - return channels; -} - -auto GpgFrontend::SingletonStorage::SetObjectInChannel( - int channel, std::unique_ptr<ChannelObject> p_obj) - -> GpgFrontend::ChannelObject* { - { - SPDLOG_TRACE("set channel: {} instance address: {}", channel, - static_cast<void*>(&instances_map_)); - - assert(p_obj != nullptr); - if (p_obj == nullptr) return nullptr; - - auto* raw_obj = p_obj.get(); - p_obj->SetChannel(channel); - { - std::unique_lock<std::shared_mutex> lock(instances_mutex_); - instances_map_.insert({channel, std::move(p_obj)}); - } - return raw_obj; - } -} - -auto GpgFrontend::SingletonStorageCollection::GetSingletonStorage( - const std::type_info& type_id) -> GpgFrontend::SingletonStorage* { - const auto hash = type_id.hash_code(); - - while (true) { - decltype(storages_map_.end()) ins_it; - { - std::shared_lock<std::shared_mutex> lock(storages_mutex_); - ins_it = storages_map_.find(hash); - } - if (ins_it == storages_map_.end()) { - { - std::unique_lock<std::shared_mutex> lock(storages_mutex_); - storages_map_.insert({hash, std::make_unique<SingletonStorage>()}); - } - SPDLOG_TRACE("hash: {} created, storage address: {} type_name: {}", hash, - static_cast<void*>(&storages_map_), type_id.name()); - continue; - } - return ins_it->second.get(); - } -} - -auto GpgFrontend::SingletonStorageCollection::GetInstance( - bool force_refresh = false) -> GpgFrontend::SingletonStorageCollection* { - static SingletonStorageCollection* instance = nullptr; - - if (force_refresh || instance == nullptr) { - instance = new SingletonStorageCollection(); - SPDLOG_DEBUG("new single storage collection created: {}", - static_cast<void*>(instance)); - } - - return instance; -} - -GpgFrontend::ChannelObject::ChannelObject() noexcept = default; - -GpgFrontend::ChannelObject::ChannelObject(int channel) : channel_(channel) {} diff --git a/src/core/function/CacheManager.h b/src/core/function/CacheManager.h index 7803c681..2126decb 100644 --- a/src/core/function/CacheManager.h +++ b/src/core/function/CacheManager.h @@ -32,7 +32,7 @@ #include <optional> #include <shared_mutex> -#include "core/GpgFunctionObject.h" +#include "core/function/basic/GpgFunctionObject.h" namespace GpgFrontend { diff --git a/src/core/function/DataObjectOperator.h b/src/core/function/DataObjectOperator.h index 134d9044..b30bce83 100644 --- a/src/core/function/DataObjectOperator.h +++ b/src/core/function/DataObjectOperator.h @@ -32,8 +32,8 @@ #include <nlohmann/json.hpp> #include <optional> -#include "core/GpgFunctionObject.h" #include "core/function/GlobalSettingStation.h" +#include "core/function/basic/GpgFunctionObject.h" namespace GpgFrontend { diff --git a/src/core/function/GlobalSettingStation.h b/src/core/function/GlobalSettingStation.h index 75c9da3b..fc0197be 100644 --- a/src/core/function/GlobalSettingStation.h +++ b/src/core/function/GlobalSettingStation.h @@ -30,7 +30,7 @@ #include <libconfig.h++> -#include "core/GpgFunctionObject.h" +#include "core/function/basic/GpgFunctionObject.h" namespace GpgFrontend { diff --git a/src/core/function/PassphraseGenerator.h b/src/core/function/PassphraseGenerator.h index dcbc83f5..d4bb7809 100644 --- a/src/core/function/PassphraseGenerator.h +++ b/src/core/function/PassphraseGenerator.h @@ -29,7 +29,7 @@ #pragma once #include "core/GpgFrontendCore.h" -#include "core/GpgFunctionObject.h" +#include "core/function/basic/GpgFunctionObject.h" namespace GpgFrontend { diff --git a/src/core/function/basic/ChannelObject.cpp b/src/core/function/basic/ChannelObject.cpp new file mode 100644 index 00000000..9485a278 --- /dev/null +++ b/src/core/function/basic/ChannelObject.cpp @@ -0,0 +1,45 @@ +/** + * Copyright (C) 2021 Saturneric <[email protected]> + * + * 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 <https://www.gnu.org/licenses/>. + * + * 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 <[email protected]> starting on May 12, 2021. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#include "ChannelObject.h" + +namespace GpgFrontend { + +ChannelObject::ChannelObject() noexcept = default; + +ChannelObject::ChannelObject(int channel) : channel_(channel) {} + +void ChannelObject::SetChannel(int channel) { this->channel_ = channel; } + +auto ChannelObject::GetChannel() const -> int { return channel_; } + +auto ChannelObject::GetDefaultChannel() -> int { + return kGpgFrontendDefaultChannel; +} + +} // namespace GpgFrontend
\ No newline at end of file diff --git a/src/core/function/basic/ChannelObject.h b/src/core/function/basic/ChannelObject.h new file mode 100644 index 00000000..9d247e6a --- /dev/null +++ b/src/core/function/basic/ChannelObject.h @@ -0,0 +1,80 @@ +/** + * Copyright (C) 2021 Saturneric <[email protected]> + * + * 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 <https://www.gnu.org/licenses/>. + * + * 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 <[email protected]> starting on May 12, 2021. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#pragma once + +namespace GpgFrontend { + +static constexpr int kGpgFrontendDefaultChannel = + 0; ///< the default channel id + +/** + * @brief object which in channel system is called "channel" + * + */ +class GPGFRONTEND_CORE_EXPORT ChannelObject { + public: + /** + * @brief Construct a new Default Channel Object object + * + */ + ChannelObject() noexcept; + + /** + * @brief Construct a new Channel Object object + * + * @param channel + */ + explicit ChannelObject(int channel); + + /** + * @brief Get the Default Channel object + * + * @return int + */ + static auto GetDefaultChannel() -> int; + + /** + * @brief Get the Channel object + * + * @return int + */ + [[nodiscard]] auto GetChannel() const -> int; + + /** + * @brief Set the Channel object + * + * @param channel + */ + void SetChannel(int channel); + + private: + int channel_ = kGpgFrontendDefaultChannel; ///< The channel id +}; + +} // namespace GpgFrontend
\ No newline at end of file diff --git a/src/core/function/basic/GpgFunctionObject.cpp b/src/core/function/basic/GpgFunctionObject.cpp new file mode 100644 index 00000000..242621d2 --- /dev/null +++ b/src/core/function/basic/GpgFunctionObject.cpp @@ -0,0 +1,29 @@ +/** + * Copyright (C) 2021 Saturneric <[email protected]> + * + * 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 <https://www.gnu.org/licenses/>. + * + * 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 <[email protected]> starting on May 12, 2021. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#include "GpgFunctionObject.h"
\ No newline at end of file diff --git a/src/core/GpgFunctionObject.h b/src/core/function/basic/GpgFunctionObject.h index b59ca84e..59bc5715 100644 --- a/src/core/GpgFunctionObject.h +++ b/src/core/function/basic/GpgFunctionObject.h @@ -28,118 +28,12 @@ #pragma once -#include <shared_mutex> +#include "core/function/basic/ChannelObject.h" +#include "core/function/basic/SingletonStorage.h" +#include "core/function/basic/SingletonStorageCollection.h" namespace GpgFrontend { -static constexpr int kGpgFrontendDefaultChannel = - 0; ///< the default channel id - -/** - * @brief object which in channel system - * - */ -class GPGFRONTEND_CORE_EXPORT ChannelObject { - public: - /** - * @brief Construct a new Default Channel Object object - * - */ - ChannelObject() noexcept; - - /** - * @brief Construct a new Channel Object object - * - * @param channel - */ - explicit ChannelObject(int channel); - - /** - * @brief Get the Default Channel object - * - * @return int - */ - static auto GetDefaultChannel() -> int; - - /** - * @brief Get the Channel object - * - * @return int - */ - [[nodiscard]] auto GetChannel() const -> int; - - /** - * @brief Set the Channel object - * - * @param channel - */ - void SetChannel(int channel); - - private: - int channel_ = kGpgFrontendDefaultChannel; ///< The channel id -}; - -class GPGFRONTEND_CORE_EXPORT SingletonStorage { - public: - /** - * @brief - * - * @param channel - */ - void ReleaseChannel(int channel); - - /** - * @brief - * - * @param channel - * @return T* - */ - auto FindObjectInChannel(int channel) -> ChannelObject*; - - /** - * @brief Get all the channel ids - * - * @return std::vector<int> - */ - auto GetAllChannelId() -> std::vector<int>; - - /** - * @brief Set a new object in channel object - * - * @param channel - * @param p_obj - * @return T* - */ - auto SetObjectInChannel(int channel, std::unique_ptr<ChannelObject> p_obj) - -> ChannelObject*; - - private: - std::shared_mutex instances_mutex_; ///< mutex for _instances_map - std::map<int, std::unique_ptr<ChannelObject>> - instances_map_; ///< map of singleton instances -}; - -class GPGFRONTEND_CORE_EXPORT SingletonStorageCollection { - public: - /** - * @brief Get the Instance object - * - * @return SingletonStorageCollection* - */ - static auto GetInstance(bool force_refresh) -> SingletonStorageCollection*; - - /** - * @brief Get the Singleton Storage object - * - * @param singleton_function_object - * @return SingletonStorage* - */ - auto GetSingletonStorage(const std::type_info&) -> SingletonStorage*; - - private: - std::shared_mutex storages_mutex_; ///< mutex for storages_map_ - std::map<size_t, std::unique_ptr<SingletonStorage>> storages_map_; -}; /** * @brief * diff --git a/src/core/function/basic/SingletonStorage.cpp b/src/core/function/basic/SingletonStorage.cpp new file mode 100644 index 00000000..567d3572 --- /dev/null +++ b/src/core/function/basic/SingletonStorage.cpp @@ -0,0 +1,118 @@ +/** + * Copyright (C) 2021 Saturneric <[email protected]> + * + * 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 <https://www.gnu.org/licenses/>. + * + * 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 <[email protected]> starting on May 12, 2021. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#include "SingletonStorage.h" + +#include <shared_mutex> + +#include "core/function/basic/ChannelObject.h" + +namespace GpgFrontend { + +class SingletonStorage::Impl { + public: + void ReleaseChannel(int channel) { + decltype(instances_map_.end()) ins_it; + { + std::shared_lock<std::shared_mutex> lock(instances_mutex_); + ins_it = instances_map_.find(channel); + } + if (ins_it != instances_map_.end()) instances_map_.erase(ins_it); + } + + auto FindObjectInChannel(int channel) -> GpgFrontend::ChannelObject* { + // read instances_map_ + decltype(instances_map_.end()) ins_it; + { + std::shared_lock<std::shared_mutex> lock(instances_mutex_); + ins_it = instances_map_.find(channel); + if (ins_it == instances_map_.end()) { + return nullptr; + } + return ins_it->second.get(); + } + } + + auto GetAllChannelId() -> std::vector<int> { + std::vector<int> channels; + channels.reserve(instances_map_.size()); + for (const auto& [key, value] : instances_map_) { + channels.push_back(key); + } + return channels; + } + + auto SetObjectInChannel(int channel, std::unique_ptr<ChannelObject> p_obj) + -> GpgFrontend::ChannelObject* { + { + SPDLOG_TRACE("set channel: {} instance address: {}", channel, + static_cast<void*>(&instances_map_)); + + assert(p_obj != nullptr); + if (p_obj == nullptr) return nullptr; + + auto* raw_obj = p_obj.get(); + p_obj->SetChannel(channel); + { + std::unique_lock<std::shared_mutex> lock(instances_mutex_); + instances_map_.insert({channel, std::move(p_obj)}); + } + return raw_obj; + } + } + + private: + std::shared_mutex instances_mutex_; ///< mutex for _instances_map + std::map<int, std::unique_ptr<ChannelObject>> + instances_map_; ///< map of singleton instances +}; + +SingletonStorage::SingletonStorage() noexcept : p_(std::make_unique<Impl>()) {} + +SingletonStorage::~SingletonStorage() = default; + +void SingletonStorage::ReleaseChannel(int channel) { + p_->ReleaseChannel(channel); +} + +auto SingletonStorage::FindObjectInChannel(int channel) + -> GpgFrontend::ChannelObject* { + return p_->FindObjectInChannel(channel); +} + +auto SingletonStorage::GetAllChannelId() -> std::vector<int> { + return p_->GetAllChannelId(); +} + +auto SingletonStorage::SetObjectInChannel(int channel, + std::unique_ptr<ChannelObject> p_obj) + -> GpgFrontend::ChannelObject* { + return p_->SetObjectInChannel(channel, std::move(p_obj)); +} + +}; // namespace GpgFrontend
\ No newline at end of file diff --git a/src/core/function/basic/SingletonStorage.h b/src/core/function/basic/SingletonStorage.h new file mode 100644 index 00000000..04fb2f07 --- /dev/null +++ b/src/core/function/basic/SingletonStorage.h @@ -0,0 +1,86 @@ +/** + * Copyright (C) 2021 Saturneric <[email protected]> + * + * 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 <https://www.gnu.org/licenses/>. + * + * 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 <[email protected]> starting on May 12, 2021. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#pragma once + +namespace GpgFrontend { + +class ChannelObject; + +class GPGFRONTEND_CORE_EXPORT SingletonStorage { + public: + /** + * @brief + * + */ + SingletonStorage() noexcept; + + /** + * @brief + * + */ + ~SingletonStorage(); + + /** + * @brief + * + * @param channel + */ + void ReleaseChannel(int channel); + + /** + * @brief + * + * @param channel + * @return T* + */ + auto FindObjectInChannel(int channel) -> ChannelObject*; + + /** + * @brief Get all the channel ids + * + * @return std::vector<int> + */ + auto GetAllChannelId() -> std::vector<int>; + + /** + * @brief Set a new object in channel object + * + * @param channel + * @param p_obj + * @return T* + */ + auto SetObjectInChannel(int channel, std::unique_ptr<ChannelObject> p_obj) + -> ChannelObject*; + + private: + class Impl; + std::unique_ptr<Impl> p_; +}; + +} // namespace GpgFrontend
\ No newline at end of file diff --git a/src/core/function/basic/SingletonStorageCollection.cpp b/src/core/function/basic/SingletonStorageCollection.cpp new file mode 100644 index 00000000..a5eb1dc0 --- /dev/null +++ b/src/core/function/basic/SingletonStorageCollection.cpp @@ -0,0 +1,104 @@ +/** + * Copyright (C) 2021 Saturneric <[email protected]> + * + * 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 <https://www.gnu.org/licenses/>. + * + * 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 <[email protected]> starting on May 12, 2021. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#include "SingletonStorageCollection.h" + +#include <shared_mutex> + +#include "core/function/basic/SingletonStorage.h" + +namespace GpgFrontend { + +class SingletonStorageCollection::Impl { + public: + /** + * @brief Get the Instance object + * + * @return SingletonStorageCollection* + */ + static auto GetInstance(bool force_refresh) -> SingletonStorageCollection* { + static SingletonStorageCollection* instance = nullptr; + + if (force_refresh || instance == nullptr) { + instance = new SingletonStorageCollection(); + SPDLOG_DEBUG("new single storage collection created: {}", + static_cast<void*>(instance)); + } + + return instance; + } + + /** + * @brief Get the Singleton Storage object + * + * @param singleton_function_object + * @return SingletonStorage* + */ + auto GetSingletonStorage(const std::type_info& type_id) -> SingletonStorage* { + const auto hash = type_id.hash_code(); + + while (true) { + decltype(storages_map_.end()) ins_it; + { + std::shared_lock<std::shared_mutex> lock(storages_mutex_); + ins_it = storages_map_.find(hash); + } + if (ins_it == storages_map_.end()) { + { + std::unique_lock<std::shared_mutex> lock(storages_mutex_); + storages_map_.insert({hash, std::make_unique<SingletonStorage>()}); + } + SPDLOG_TRACE("hash: {} created, storage address: {} type_name: {}", + hash, static_cast<void*>(&storages_map_), type_id.name()); + continue; + } + return ins_it->second.get(); + } + } + + private: + std::shared_mutex storages_mutex_; ///< mutex for storages_map_ + std::map<size_t, std::unique_ptr<SingletonStorage>> storages_map_; +}; + +SingletonStorageCollection::SingletonStorageCollection() noexcept + : p_(std::make_unique<Impl>()) {} + +SingletonStorageCollection::~SingletonStorageCollection() = default; + +auto SingletonStorageCollection::GetSingletonStorage( + const std::type_info& type_id) -> GpgFrontend::SingletonStorage* { + return p_->GetSingletonStorage(type_id); +} + +auto GpgFrontend::SingletonStorageCollection::GetInstance( + bool force_refresh = false) -> GpgFrontend::SingletonStorageCollection* { + return Impl::GetInstance(force_refresh); +} + +} // namespace GpgFrontend
\ No newline at end of file diff --git a/src/core/function/basic/SingletonStorageCollection.h b/src/core/function/basic/SingletonStorageCollection.h new file mode 100644 index 00000000..16a72fd4 --- /dev/null +++ b/src/core/function/basic/SingletonStorageCollection.h @@ -0,0 +1,69 @@ +/** + * Copyright (C) 2021 Saturneric <[email protected]> + * + * 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 <https://www.gnu.org/licenses/>. + * + * 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 <[email protected]> starting on May 12, 2021. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#pragma once + +namespace GpgFrontend { + +class SingletonStorage; + +class GPGFRONTEND_CORE_EXPORT SingletonStorageCollection { + public: + /** + * @brief Get the Instance object + * + * @return SingletonStorageCollection* + */ + static auto GetInstance(bool force_refresh) -> SingletonStorageCollection*; + + /** + * @brief Get the Singleton Storage object + * + * @param singleton_function_object + * @return SingletonStorage* + */ + auto GetSingletonStorage(const std::type_info&) -> SingletonStorage*; + + private: + class Impl; + std::unique_ptr<Impl> p_; + + /** + * @brief + * + */ + SingletonStorageCollection() noexcept; + + /** + * @brief + * + */ + ~SingletonStorageCollection(); +}; + +} // namespace GpgFrontend
\ No newline at end of file diff --git a/src/core/function/gpg/GpgBasicOperator.h b/src/core/function/gpg/GpgBasicOperator.h index 08c3ae86..1c9720cd 100644 --- a/src/core/function/gpg/GpgBasicOperator.h +++ b/src/core/function/gpg/GpgBasicOperator.h @@ -29,9 +29,9 @@ #pragma once #include "core/GpgConstants.h" -#include "core/GpgContext.h" -#include "core/GpgFunctionObject.h" #include "core/GpgModel.h" +#include "core/function/basic/GpgFunctionObject.h" +#include "core/function/gpg/GpgContext.h" namespace GpgFrontend { diff --git a/src/core/function/gpg/GpgCommandExecutor.cpp b/src/core/function/gpg/GpgCommandExecutor.cpp index 5cf57b27..d05cc626 100644 --- a/src/core/function/gpg/GpgCommandExecutor.cpp +++ b/src/core/function/gpg/GpgCommandExecutor.cpp @@ -30,7 +30,7 @@ #include <boost/format.hpp> #include <utility> -#include "GpgFunctionObject.h" +#include "core/function/basic/GpgFunctionObject.h" #include "core/model/DataObject.h" #include "core/module/Module.h" #include "core/thread/Task.h" diff --git a/src/core/GpgContext.cpp b/src/core/function/gpg/GpgContext.cpp index f218feb8..fb51f09f 100644 --- a/src/core/GpgContext.cpp +++ b/src/core/function/gpg/GpgContext.cpp @@ -26,16 +26,16 @@ * */ -#include "core/GpgContext.h" +#include "core/function/gpg/GpgContext.h" #include <gpg-error.h> #include <gpgme.h> #include <unistd.h> #include "core/GpgConstants.h" -#include "core/GpgFunctionObject.h" #include "core/common/CoreCommonUtil.h" #include "core/function/CoreSignalStation.h" +#include "core/function/basic/GpgFunctionObject.h" #include "core/function/gpg/GpgCommandExecutor.h" #include "core/function/gpg/GpgKeyGetter.h" #include "core/module/ModuleManager.h" diff --git a/src/core/GpgContext.h b/src/core/function/gpg/GpgContext.h index 4162390a..c20ef192 100644 --- a/src/core/GpgContext.h +++ b/src/core/function/gpg/GpgContext.h @@ -28,7 +28,7 @@ #pragma once -#include "GpgFunctionObject.h" +#include "core/function/basic/GpgFunctionObject.h" namespace GpgFrontend { diff --git a/src/core/function/gpg/GpgKeyGetter.cpp b/src/core/function/gpg/GpgKeyGetter.cpp index 070699fa..4919ce05 100644 --- a/src/core/function/gpg/GpgKeyGetter.cpp +++ b/src/core/function/gpg/GpgKeyGetter.cpp @@ -34,7 +34,7 @@ #include <shared_mutex> #include "core/GpgConstants.h" -#include "core/GpgContext.h" +#include "core/function/gpg/GpgContext.h" namespace GpgFrontend { diff --git a/src/core/function/gpg/GpgKeyGetter.h b/src/core/function/gpg/GpgKeyGetter.h index 85424b55..fabd1e4e 100644 --- a/src/core/function/gpg/GpgKeyGetter.h +++ b/src/core/function/gpg/GpgKeyGetter.h @@ -28,8 +28,8 @@ #pragma once -#include "core/GpgFunctionObject.h" #include "core/GpgModel.h" +#include "core/function/basic/GpgFunctionObject.h" namespace GpgFrontend { diff --git a/src/core/function/gpg/GpgKeyImportExporter.h b/src/core/function/gpg/GpgKeyImportExporter.h index 0f324a06..75d65ada 100644 --- a/src/core/function/gpg/GpgKeyImportExporter.h +++ b/src/core/function/gpg/GpgKeyImportExporter.h @@ -28,9 +28,9 @@ #pragma once -#include "core/GpgContext.h" -#include "core/GpgFunctionObject.h" #include "core/GpgModel.h" +#include "core/function/basic/GpgFunctionObject.h" +#include "core/function/gpg/GpgContext.h" namespace GpgFrontend { diff --git a/src/core/function/gpg/GpgKeyManager.cpp b/src/core/function/gpg/GpgKeyManager.cpp index a0e1108a..291e1594 100644 --- a/src/core/function/gpg/GpgKeyManager.cpp +++ b/src/core/function/gpg/GpgKeyManager.cpp @@ -41,8 +41,6 @@ auto GpgFrontend::GpgKeyManager::SignKey( const GpgFrontend::GpgKey& target, GpgFrontend::KeyArgsList& keys, const std::string& uid, const std::unique_ptr<boost::posix_time::ptime>& expires) -> bool { - using namespace boost::posix_time; - GpgBasicOperator::GetInstance().SetSigners(keys); unsigned int flags = 0; diff --git a/src/core/function/gpg/GpgKeyManager.h b/src/core/function/gpg/GpgKeyManager.h index 404ad093..c263032f 100644 --- a/src/core/function/gpg/GpgKeyManager.h +++ b/src/core/function/gpg/GpgKeyManager.h @@ -31,9 +31,9 @@ #include <functional> #include <utility> -#include "core/GpgContext.h" -#include "core/GpgFunctionObject.h" #include "core/GpgModel.h" +#include "core/function/basic/GpgFunctionObject.h" +#include "core/function/gpg/GpgContext.h" namespace GpgFrontend { diff --git a/src/core/function/gpg/GpgKeyOpera.cpp b/src/core/function/gpg/GpgKeyOpera.cpp index 4a8874c1..1b4d9881 100644 --- a/src/core/function/gpg/GpgKeyOpera.cpp +++ b/src/core/function/gpg/GpgKeyOpera.cpp @@ -34,10 +34,10 @@ #include <boost/format.hpp> #include <boost/process/async_pipe.hpp> -#include "GpgCommandExecutor.h" -#include "GpgKeyGetter.h" #include "core/GpgConstants.h" -#include "core/GpgGenKeyInfo.h" +#include "core/function/gpg/GpgCommandExecutor.h" +#include "core/function/gpg/GpgKeyGetter.h" +#include "core/model/GpgGenKeyInfo.h" #include "core/module/ModuleManager.h" namespace GpgFrontend { @@ -55,7 +55,7 @@ void GpgKeyOpera::DeleteKeys(GpgFrontend::KeyIdArgsListPtr key_ids) { auto key = GpgKeyGetter::GetInstance().GetKey(tmp); if (key.IsGood()) { err = CheckGpgError( - gpgme_op_delete_ext(ctx_, gpgme_key_t(key), + gpgme_op_delete_ext(ctx_, static_cast<gpgme_key_t>(key), GPGME_DELETE_ALLOW_SECRET | GPGME_DELETE_FORCE)); assert(gpg_err_code(err) == GPG_ERR_NO_ERROR); } else { @@ -72,26 +72,26 @@ void GpgKeyOpera::DeleteKeys(GpgFrontend::KeyIdArgsListPtr key_ids) { * @param expires date and time * @return if successful */ -GpgError GpgKeyOpera::SetExpire( - const GpgKey& key, const SubkeyId& subkey_fpr, - std::unique_ptr<boost::posix_time::ptime>& expires) { +auto GpgKeyOpera::SetExpire(const GpgKey& key, const SubkeyId& subkey_fpr, + std::unique_ptr<boost::posix_time::ptime>& expires) + -> GpgError { unsigned long expires_time = 0; if (expires != nullptr) { - using namespace boost::posix_time; - using namespace std::chrono; - expires_time = - to_time_t(*expires) - system_clock::to_time_t(system_clock::now()); + expires_time = to_time_t(*expires) - std::chrono::system_clock::to_time_t( + std::chrono::system_clock::now()); } SPDLOG_DEBUG(key.GetId(), subkey_fpr, expires_time); GpgError err; - if (key.GetFingerprint() == subkey_fpr || subkey_fpr.empty()) - err = gpgme_op_setexpire(ctx_, gpgme_key_t(key), expires_time, nullptr, 0); - else - err = gpgme_op_setexpire(ctx_, gpgme_key_t(key), expires_time, + if (key.GetFingerprint() == subkey_fpr || subkey_fpr.empty()) { + err = gpgme_op_setexpire(ctx_, static_cast<gpgme_key_t>(key), expires_time, + nullptr, 0); + } else { + err = gpgme_op_setexpire(ctx_, static_cast<gpgme_key_t>(key), expires_time, subkey_fpr.c_str(), 0); + } return err; } @@ -103,14 +103,14 @@ GpgError GpgKeyOpera::SetExpire( * @return the process doing this job */ void GpgKeyOpera::GenerateRevokeCert(const GpgKey& key, - const std::string& output_file_path) { + const std::string& output_path) { const auto app_path = Module::RetrieveRTValueTypedOrDefault<>( "core", "gpgme.ctx.app_path", std::string{}); // get all components GpgCommandExecutor::ExecuteSync( {app_path, - {"--command-fd", "0", "--status-fd", "1", "--no-tty", "-o", - output_file_path, "--gen-revoke", key.GetFingerprint().c_str()}, + {"--command-fd", "0", "--status-fd", "1", "--no-tty", "-o", output_path, + "--gen-revoke", key.GetFingerprint()}, [=](int exit_code, const std::string& p_out, const std::string& p_err) { if (exit_code != 0) { SPDLOG_ERROR( @@ -155,8 +155,8 @@ void GpgKeyOpera::GenerateRevokeCert(const GpgKey& key, * @param params key generation args * @return error information */ -GpgError GpgKeyOpera::GenerateKey(const std::unique_ptr<GenKeyInfo>& params, - GpgGenKeyResult& result) { +auto GpgKeyOpera::GenerateKey(const std::unique_ptr<GenKeyInfo>& params, + GpgGenKeyResult& result) -> GpgError { auto userid_utf8 = params->GetUserid(); const char* userid = userid_utf8.c_str(); auto algo_utf8 = params->GetAlgo() + params->GetKeySizeStr(); @@ -165,12 +165,9 @@ GpgError GpgKeyOpera::GenerateKey(const std::unique_ptr<GenKeyInfo>& params, const char* algo = algo_utf8.c_str(); unsigned long expires = 0; - { - using namespace boost::posix_time; - using namespace std::chrono; - expires = to_time_t(ptime(params->GetExpireTime())) - - system_clock::to_time_t(system_clock::now()); - } + expires = + to_time_t(boost::posix_time::ptime(params->GetExpireTime())) - + std::chrono::system_clock::to_time_t(std::chrono ::system_clock::now()); GpgError err; @@ -210,10 +207,12 @@ GpgError GpgKeyOpera::GenerateKey(const std::unique_ptr<GenKeyInfo>& params, if (!params->IsNonExpired()) { auto date = params->GetExpireTime().date(); ss << boost::format{"Expire-Date: %1%\n"} % to_iso_string(date); - } else + } else { ss << boost::format{"Expire-Date: 0\n"}; - if (!params->IsNoPassPhrase()) + } + if (!params->IsNoPassPhrase()) { ss << boost::format{"Passphrase: %1%\n"} % params->GetPassPhrase(); + } ss << "</GnupgKeyParms>"; @@ -236,8 +235,9 @@ GpgError GpgKeyOpera::GenerateKey(const std::unique_ptr<GenKeyInfo>& params, * @param params opera args * @return error info */ -GpgError GpgKeyOpera::GenerateSubkey( - const GpgKey& key, const std::unique_ptr<GenKeyInfo>& params) { +auto GpgKeyOpera::GenerateSubkey(const GpgKey& key, + const std::unique_ptr<GenKeyInfo>& params) + -> GpgError { if (!params->IsSubKey()) return GPG_ERR_CANCELED; SPDLOG_DEBUG("generate subkey algo {} key size {}", params->GetAlgo(), @@ -246,12 +246,11 @@ GpgError GpgKeyOpera::GenerateSubkey( auto algo_utf8 = (params->GetAlgo() + params->GetKeySizeStr()); const char* algo = algo_utf8.c_str(); unsigned long expires = 0; - { - using namespace boost::posix_time; - using namespace std::chrono; - expires = to_time_t(ptime(params->GetExpireTime())) - - system_clock::to_time_t(system_clock::now()); - } + + expires = + to_time_t(boost::posix_time::ptime(params->GetExpireTime())) - + std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); + unsigned int flags = 0; if (!params->IsSubKey()) flags |= GPGME_CREATE_CERT; @@ -263,12 +262,12 @@ GpgError GpgKeyOpera::GenerateSubkey( SPDLOG_DEBUG("args: {} {} {} {}", key.GetId(), algo, expires, flags); - auto err = - gpgme_op_createsubkey(ctx_, gpgme_key_t(key), algo, 0, expires, flags); + auto err = gpgme_op_createsubkey(ctx_, static_cast<gpgme_key_t>(key), algo, 0, + expires, flags); return CheckGpgError(err); } -GpgError GpgKeyOpera::ModifyPassword(const GpgKey& key) { +auto GpgKeyOpera::ModifyPassword(const GpgKey& key) -> GpgError { const auto gnupg_version = Module::RetrieveRTValueTypedOrDefault<>( "core", "gpgme.ctx.gnupg_version", std::string{"2.0.0"}); SPDLOG_DEBUG("got gnupg version from rt: {}", gnupg_version); @@ -277,12 +276,13 @@ GpgError GpgKeyOpera::ModifyPassword(const GpgKey& key) { SPDLOG_ERROR("operator not support"); return GPG_ERR_NOT_SUPPORTED; } - auto err = gpgme_op_passwd(ctx_, gpgme_key_t(key), 0); + auto err = gpgme_op_passwd(ctx_, static_cast<gpgme_key_t>(key), 0); return CheckGpgError(err); } -GpgError GpgKeyOpera::ModifyTOFUPolicy(const GpgKey& key, - gpgme_tofu_policy_t tofu_policy) { +auto GpgKeyOpera::ModifyTOFUPolicy(const GpgKey& key, + gpgme_tofu_policy_t tofu_policy) + -> GpgError { const auto gnupg_version = Module::RetrieveRTValueTypedOrDefault<>( "core", "gpgme.ctx.gnupg_version", std::string{"2.0.0"}); SPDLOG_DEBUG("got gnupg version from rt: {}", gnupg_version); @@ -292,7 +292,8 @@ GpgError GpgKeyOpera::ModifyTOFUPolicy(const GpgKey& key, return GPG_ERR_NOT_SUPPORTED; } - auto err = gpgme_op_tofu_policy(ctx_, gpgme_key_t(key), tofu_policy); + auto err = + gpgme_op_tofu_policy(ctx_, static_cast<gpgme_key_t>(key), tofu_policy); return CheckGpgError(err); } diff --git a/src/core/function/gpg/GpgKeyOpera.h b/src/core/function/gpg/GpgKeyOpera.h index e7e27754..bb96b1e8 100644 --- a/src/core/function/gpg/GpgKeyOpera.h +++ b/src/core/function/gpg/GpgKeyOpera.h @@ -29,8 +29,8 @@ #pragma once #include "core/GpgConstants.h" -#include "core/GpgContext.h" #include "core/GpgModel.h" +#include "core/function/gpg/GpgContext.h" namespace GpgFrontend { /** @@ -76,8 +76,9 @@ class GPGFRONTEND_CORE_EXPORT GpgKeyOpera * @param expires * @return GpgError */ - GpgError SetExpire(const GpgKey& key, const SubkeyId& subkey_fpr, - std::unique_ptr<boost::posix_time::ptime>& expires); + auto SetExpire(const GpgKey& key, const SubkeyId& subkey_fpr, + std::unique_ptr<boost::posix_time::ptime>& expires) + -> GpgError; /** * @brief @@ -85,8 +86,7 @@ class GPGFRONTEND_CORE_EXPORT GpgKeyOpera * @param key * @param output_file_name */ - void GenerateRevokeCert(const GpgKey& key, - const std::string& output_file_name); + void GenerateRevokeCert(const GpgKey& key, const std::string& output_path); /** * @brief @@ -94,7 +94,7 @@ class GPGFRONTEND_CORE_EXPORT GpgKeyOpera * @param key * @return GpgFrontend::GpgError */ - GpgFrontend::GpgError ModifyPassword(const GpgKey& key); + auto ModifyPassword(const GpgKey& key) -> GpgFrontend::GpgError; /** * @brief @@ -103,8 +103,8 @@ class GPGFRONTEND_CORE_EXPORT GpgKeyOpera * @param tofu_policy * @return GpgFrontend::GpgError */ - GpgFrontend::GpgError ModifyTOFUPolicy(const GpgKey& key, - gpgme_tofu_policy_t tofu_policy); + auto ModifyTOFUPolicy(const GpgKey& key, gpgme_tofu_policy_t tofu_policy) + -> GpgFrontend::GpgError; /** * @brief * @@ -112,8 +112,8 @@ class GPGFRONTEND_CORE_EXPORT GpgKeyOpera * @param result * @return GpgFrontend::GpgError */ - GpgFrontend::GpgError GenerateKey(const std::unique_ptr<GenKeyInfo>& params, - GpgGenKeyResult& result); + auto GenerateKey(const std::unique_ptr<GenKeyInfo>& params, + GpgGenKeyResult& result) -> GpgFrontend::GpgError; /** * @brief @@ -122,8 +122,9 @@ class GPGFRONTEND_CORE_EXPORT GpgKeyOpera * @param params * @return GpgFrontend::GpgError */ - GpgFrontend::GpgError GenerateSubkey( - const GpgKey& key, const std::unique_ptr<GenKeyInfo>& params); + auto GenerateSubkey(const GpgKey& key, + const std::unique_ptr<GenKeyInfo>& params) + -> GpgFrontend::GpgError; private: GpgContext& ctx_ = diff --git a/src/core/function/gpg/GpgUIDOperator.cpp b/src/core/function/gpg/GpgUIDOperator.cpp index d74cea76..61e8c54c 100644 --- a/src/core/function/gpg/GpgUIDOperator.cpp +++ b/src/core/function/gpg/GpgUIDOperator.cpp @@ -33,38 +33,31 @@ GpgFrontend::GpgUIDOperator::GpgUIDOperator(int channel) : SingletonFunctionObject<GpgUIDOperator>(channel) {} -bool GpgFrontend::GpgUIDOperator::AddUID(const GpgFrontend::GpgKey& key, - const std::string& uid) { - auto err = gpgme_op_adduid(ctx_, gpgme_key_t(key), uid.c_str(), 0); - if (CheckGpgError(err) == GPG_ERR_NO_ERROR) - return true; - else - return false; +auto GpgFrontend::GpgUIDOperator::AddUID(const GpgFrontend::GpgKey& key, + const std::string& uid) -> bool { + auto err = + gpgme_op_adduid(ctx_, static_cast<gpgme_key_t>(key), uid.c_str(), 0); + return CheckGpgError(err) == GPG_ERR_NO_ERROR; } -bool GpgFrontend::GpgUIDOperator::RevUID(const GpgFrontend::GpgKey& key, - const std::string& uid) { - auto err = - CheckGpgError(gpgme_op_revuid(ctx_, gpgme_key_t(key), uid.c_str(), 0)); - if (CheckGpgError(err) == GPG_ERR_NO_ERROR) - return true; - else - return false; +auto GpgFrontend::GpgUIDOperator::RevUID(const GpgFrontend::GpgKey& key, + const std::string& uid) -> bool { + auto err = CheckGpgError( + gpgme_op_revuid(ctx_, static_cast<gpgme_key_t>(key), uid.c_str(), 0)); + return CheckGpgError(err) == GPG_ERR_NO_ERROR; } -bool GpgFrontend::GpgUIDOperator::SetPrimaryUID(const GpgFrontend::GpgKey& key, - const std::string& uid) { +auto GpgFrontend::GpgUIDOperator::SetPrimaryUID(const GpgFrontend::GpgKey& key, + const std::string& uid) + -> bool { auto err = CheckGpgError(gpgme_op_set_uid_flag( - ctx_, gpgme_key_t(key), uid.c_str(), "primary", nullptr)); - if (CheckGpgError(err) == GPG_ERR_NO_ERROR) - return true; - else - return false; + ctx_, static_cast<gpgme_key_t>(key), uid.c_str(), "primary", nullptr)); + return CheckGpgError(err) == GPG_ERR_NO_ERROR; } -bool GpgFrontend::GpgUIDOperator::AddUID(const GpgFrontend::GpgKey& key, +auto GpgFrontend::GpgUIDOperator::AddUID(const GpgFrontend::GpgKey& key, const std::string& name, const std::string& comment, - const std::string& email) { + const std::string& email) -> bool { SPDLOG_DEBUG("new uuid: {} {} {}", name, comment, email); auto uid = boost::format("%1%(%2%)<%3%>") % name % comment % email; return AddUID(key, uid.str()); diff --git a/src/core/function/gpg/GpgUIDOperator.h b/src/core/function/gpg/GpgUIDOperator.h index 7d92b843..02ea7b5f 100644 --- a/src/core/function/gpg/GpgUIDOperator.h +++ b/src/core/function/gpg/GpgUIDOperator.h @@ -28,8 +28,8 @@ #pragma once -#include "core/GpgContext.h" #include "core/GpgModel.h" +#include "core/function/gpg/GpgContext.h" namespace GpgFrontend { /** @@ -53,7 +53,7 @@ class GPGFRONTEND_CORE_EXPORT GpgUIDOperator * @param uid uid args(combine name&comment&email) * @return if successful */ - bool AddUID(const GpgKey& key, const std::string& uid); + auto AddUID(const GpgKey& key, const std::string& uid) -> bool; /** * create a new uid in certain key pair @@ -63,8 +63,8 @@ class GPGFRONTEND_CORE_EXPORT GpgUIDOperator * @param email * @return */ - bool AddUID(const GpgKey& key, const std::string& name, - const std::string& comment, const std::string& email); + auto AddUID(const GpgKey& key, const std::string& name, + const std::string& comment, const std::string& email) -> bool; /** * Revoke(Delete) UID from certain key pair @@ -72,7 +72,7 @@ class GPGFRONTEND_CORE_EXPORT GpgUIDOperator * @param uid target uid * @return if successful */ - bool RevUID(const GpgKey& key, const std::string& uid); + auto RevUID(const GpgKey& key, const std::string& uid) -> bool; /** * Set one of a uid of a key pair as primary @@ -80,7 +80,7 @@ class GPGFRONTEND_CORE_EXPORT GpgUIDOperator * @param uid target uid * @return if successful */ - bool SetPrimaryUID(const GpgKey& key, const std::string& uid); + auto SetPrimaryUID(const GpgKey& key, const std::string& uid) -> bool; private: GpgContext& ctx_ = diff --git a/src/core/GpgGenKeyInfo.cpp b/src/core/model/GpgGenKeyInfo.cpp index 3bb0816c..4f0dc964 100644 --- a/src/core/GpgGenKeyInfo.cpp +++ b/src/core/model/GpgGenKeyInfo.cpp @@ -26,7 +26,7 @@ * */ -#include "core/GpgGenKeyInfo.h" +#include "GpgGenKeyInfo.h" #include <algorithm> #include <boost/date_time/gregorian/greg_date.hpp> diff --git a/src/core/GpgGenKeyInfo.h b/src/core/model/GpgGenKeyInfo.h index 1f8b53e0..b25fe889 100644 --- a/src/core/GpgGenKeyInfo.h +++ b/src/core/model/GpgGenKeyInfo.h @@ -32,8 +32,6 @@ #include <boost/date_time/gregorian/greg_duration_types.hpp> #include <boost/format.hpp> -#include "GpgFrontend.h" - namespace GpgFrontend { class GPGFRONTEND_CORE_EXPORT GenKeyInfo { diff --git a/src/core/thread/CtxCheckTask.cpp b/src/core/thread/CtxCheckTask.cpp index 7fb0fa0a..4ca3978d 100644 --- a/src/core/thread/CtxCheckTask.cpp +++ b/src/core/thread/CtxCheckTask.cpp @@ -28,9 +28,9 @@ #include "core/thread/CtxCheckTask.h" -#include "core/GpgContext.h" #include "core/GpgCoreInit.h" #include "core/common/CoreCommonUtil.h" +#include "core/function/gpg/GpgContext.h" #include "core/function/gpg/GpgKeyGetter.h" #include "thread/Task.h" diff --git a/src/core/thread/TaskRunnerGetter.h b/src/core/thread/TaskRunnerGetter.h index a71df7e7..f44861bb 100644 --- a/src/core/thread/TaskRunnerGetter.h +++ b/src/core/thread/TaskRunnerGetter.h @@ -29,7 +29,7 @@ #pragma once #include "core/GpgFrontendCore.h" -#include "core/GpgFunctionObject.h" +#include "core/function/basic/GpgFunctionObject.h" #include "core/thread/TaskRunner.h" namespace GpgFrontend::Thread { @@ -47,10 +47,11 @@ class GPGFRONTEND_CORE_EXPORT TaskRunnerGetter kTaskRunnerType_External_Process, }; - explicit TaskRunnerGetter(int channel = SingletonFunctionObject::GetDefaultChannel()); + explicit TaskRunnerGetter( + int channel = SingletonFunctionObject::GetDefaultChannel()); - auto GetTaskRunner( - TaskRunnerType runner_type = kTaskRunnerType_Default) -> TaskRunnerPtr; + auto GetTaskRunner(TaskRunnerType runner_type = kTaskRunnerType_Default) + -> TaskRunnerPtr; private: std::map<TaskRunnerType, TaskRunnerPtr> task_runners_; diff --git a/src/ui/dialog/help/GnupgTab.h b/src/ui/dialog/help/GnupgTab.h index f43857e3..9e6191db 100644 --- a/src/ui/dialog/help/GnupgTab.h +++ b/src/ui/dialog/help/GnupgTab.h @@ -32,7 +32,7 @@ #pragma once -#include "core/GpgContext.h" +#include "core/function/gpg/GpgContext.h" #include "ui/GpgFrontendUI.h" class Ui_GnuPGInfo; diff --git a/src/ui/dialog/import_export/KeyImportDetailDialog.h b/src/ui/dialog/import_export/KeyImportDetailDialog.h index ba2f3054..fe4988c0 100644 --- a/src/ui/dialog/import_export/KeyImportDetailDialog.h +++ b/src/ui/dialog/import_export/KeyImportDetailDialog.h @@ -28,7 +28,7 @@ #pragma once -#include "core/GpgContext.h" +#include "core/function/gpg/GpgContext.h" #include "core/function/gpg/GpgKeyImportExporter.h" #include "ui/GpgFrontendUI.h" #include "ui/dialog/GeneralDialog.h" diff --git a/src/ui/dialog/import_export/KeyServerImportDialog.h b/src/ui/dialog/import_export/KeyServerImportDialog.h index 9085e55d..13ae00b5 100644 --- a/src/ui/dialog/import_export/KeyServerImportDialog.h +++ b/src/ui/dialog/import_export/KeyServerImportDialog.h @@ -32,7 +32,7 @@ #include <string> #include "KeyImportDetailDialog.h" -#include "core/GpgContext.h" +#include "core/function/gpg/GpgContext.h" #include "ui/GpgFrontendUI.h" #include "ui/dialog/GeneralDialog.h" #include "ui/widgets/KeyList.h" diff --git a/src/ui/dialog/import_export/KeyUploadDialog.h b/src/ui/dialog/import_export/KeyUploadDialog.h index fe98222e..18bc5255 100644 --- a/src/ui/dialog/import_export/KeyUploadDialog.h +++ b/src/ui/dialog/import_export/KeyUploadDialog.h @@ -28,7 +28,7 @@ #pragma once -#include "core/GpgContext.h" +#include "core/function/gpg/GpgContext.h" #include "ui/GpgFrontendUI.h" #include "ui/dialog/GeneralDialog.h" diff --git a/src/ui/dialog/key_generate/KeygenDialog.h b/src/ui/dialog/key_generate/KeygenDialog.h index 5ddedd96..0debae68 100644 --- a/src/ui/dialog/key_generate/KeygenDialog.h +++ b/src/ui/dialog/key_generate/KeygenDialog.h @@ -28,8 +28,8 @@ #pragma once -#include "core/GpgContext.h" -#include "core/GpgGenKeyInfo.h" +#include "core/function/gpg/GpgContext.h" +#include "core/model/GpgGenKeyInfo.h" #include "ui/GpgFrontendUI.h" #include "ui/dialog/GeneralDialog.h" diff --git a/src/ui/dialog/key_generate/SubkeyGenerateDialog.h b/src/ui/dialog/key_generate/SubkeyGenerateDialog.h index cefc4b53..5a7c4ad4 100644 --- a/src/ui/dialog/key_generate/SubkeyGenerateDialog.h +++ b/src/ui/dialog/key_generate/SubkeyGenerateDialog.h @@ -28,8 +28,8 @@ #pragma once -#include "core/GpgContext.h" -#include "core/GpgGenKeyInfo.h" +#include "core/function/gpg/GpgContext.h" +#include "core/model/GpgGenKeyInfo.h" #include "ui/GpgFrontendUI.h" #include "ui/dialog/GeneralDialog.h" diff --git a/src/ui/dialog/keypair_details/KeyDetailsDialog.h b/src/ui/dialog/keypair_details/KeyDetailsDialog.h index 9b6605f5..6e3fc4a7 100644 --- a/src/ui/dialog/keypair_details/KeyDetailsDialog.h +++ b/src/ui/dialog/keypair_details/KeyDetailsDialog.h @@ -28,7 +28,7 @@ #pragma once -#include "core/GpgContext.h" +#include "core/function/gpg/GpgContext.h" #include "ui/GpgFrontendUI.h" #include "ui/dialog/GeneralDialog.h" diff --git a/src/ui/dialog/keypair_details/KeyNewUIDDialog.h b/src/ui/dialog/keypair_details/KeyNewUIDDialog.h index 6a8c8bca..eadaa2e8 100644 --- a/src/ui/dialog/keypair_details/KeyNewUIDDialog.h +++ b/src/ui/dialog/keypair_details/KeyNewUIDDialog.h @@ -28,7 +28,7 @@ #pragma once -#include "core/GpgContext.h" +#include "core/function/gpg/GpgContext.h" #include "ui/GpgFrontendUI.h" #include "ui/dialog/GeneralDialog.h" diff --git a/src/ui/dialog/keypair_details/KeyPairDetailTab.h b/src/ui/dialog/keypair_details/KeyPairDetailTab.h index 66323ecc..f134af3f 100644 --- a/src/ui/dialog/keypair_details/KeyPairDetailTab.h +++ b/src/ui/dialog/keypair_details/KeyPairDetailTab.h @@ -29,7 +29,7 @@ #pragma once #include "KeySetExpireDateDialog.h" -#include "core/GpgContext.h" +#include "core/function/gpg/GpgContext.h" #include "ui/GpgFrontendUI.h" #include "ui/dialog/import_export/KeyServerImportDialog.h" #include "ui/dialog/import_export/KeyUploadDialog.h" diff --git a/src/ui/dialog/keypair_details/KeyPairSubkeyTab.h b/src/ui/dialog/keypair_details/KeyPairSubkeyTab.h index 63a65278..bde87464 100644 --- a/src/ui/dialog/keypair_details/KeyPairSubkeyTab.h +++ b/src/ui/dialog/keypair_details/KeyPairSubkeyTab.h @@ -29,7 +29,7 @@ #pragma once #include "KeySetExpireDateDialog.h" -#include "core/GpgContext.h" +#include "core/function/gpg/GpgContext.h" #include "ui/GpgFrontendUI.h" #include "ui/dialog/key_generate/SubkeyGenerateDialog.h" diff --git a/src/ui/dialog/keypair_details/KeyPairUIDTab.h b/src/ui/dialog/keypair_details/KeyPairUIDTab.h index 6492d053..0edecafe 100644 --- a/src/ui/dialog/keypair_details/KeyPairUIDTab.h +++ b/src/ui/dialog/keypair_details/KeyPairUIDTab.h @@ -30,7 +30,7 @@ #include "KeyNewUIDDialog.h" #include "KeyUIDSignDialog.h" -#include "core/GpgContext.h" +#include "core/function/gpg/GpgContext.h" #include "ui/GpgFrontendUI.h" namespace GpgFrontend::UI { diff --git a/src/ui/dialog/keypair_details/KeySetExpireDateDialog.h b/src/ui/dialog/keypair_details/KeySetExpireDateDialog.h index 433da21a..ff7c4169 100644 --- a/src/ui/dialog/keypair_details/KeySetExpireDateDialog.h +++ b/src/ui/dialog/keypair_details/KeySetExpireDateDialog.h @@ -28,7 +28,7 @@ #pragma once -#include "core/GpgContext.h" +#include "core/function/gpg/GpgContext.h" #include "core/model/GpgKey.h" #include "core/model/GpgSubKey.h" #include "ui/GpgFrontendUI.h" diff --git a/src/ui/dialog/keypair_details/KeyUIDSignDialog.h b/src/ui/dialog/keypair_details/KeyUIDSignDialog.h index 52ae841f..35d722ad 100644 --- a/src/ui/dialog/keypair_details/KeyUIDSignDialog.h +++ b/src/ui/dialog/keypair_details/KeyUIDSignDialog.h @@ -28,7 +28,7 @@ #pragma once -#include "core/GpgContext.h" +#include "core/function/gpg/GpgContext.h" #include "ui/GpgFrontendUI.h" #include "ui/dialog/GeneralDialog.h" #include "ui/widgets/KeyList.h" diff --git a/src/ui/dialog/settings/SettingsGeneral.cpp b/src/ui/dialog/settings/SettingsGeneral.cpp index 179ceaee..bbcb9603 100644 --- a/src/ui/dialog/settings/SettingsGeneral.cpp +++ b/src/ui/dialog/settings/SettingsGeneral.cpp @@ -28,7 +28,7 @@ #include "SettingsGeneral.h" -#include "core/GpgContext.h" +#include "core/function/gpg/GpgContext.h" #ifdef SUPPORT_MULTI_LANG #include "SettingsDialog.h" diff --git a/src/ui/widgets/KeyList.h b/src/ui/widgets/KeyList.h index 31f2b561..b68a4937 100644 --- a/src/ui/widgets/KeyList.h +++ b/src/ui/widgets/KeyList.h @@ -31,7 +31,7 @@ #include <string> #include <utility> -#include "core/GpgContext.h" +#include "core/function/gpg/GpgContext.h" #include "ui/dialog/import_export/KeyImportDetailDialog.h" class Ui_KeyList; |