diff options
Diffstat (limited to 'src')
63 files changed, 944 insertions, 591 deletions
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 1692daa9..324b3b25 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -33,6 +33,7 @@ aux_source_directory(./thread CORE_SOURCE) aux_source_directory(./model CORE_SOURCE) aux_source_directory(./common CORE_SOURCE) aux_source_directory(./module CORE_SOURCE) +aux_source_directory(./utils CORE_SOURCE) aux_source_directory(. CORE_SOURCE) # define libgpgfrontend_core diff --git a/src/core/CoreTypedef.h b/src/core/CoreTypedef.h new file mode 100644 index 00000000..fe2ed87b --- /dev/null +++ b/src/core/CoreTypedef.h @@ -0,0 +1,41 @@ +/** + * 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 { + +using ByteArray = std::string; ///< +using ByteArrayPtr = std::shared_ptr<ByteArray>; ///< +using StdBypeArrayPtr = std::shared_ptr<ByteArray>; ///< +using BypeArrayRef = ByteArray&; ///< +using BypeArrayConstRef = const ByteArray&; ///< +using StringArgsPtr = std::unique_ptr<std::vector<std::string>>; ///< +using StringArgsRef = std::vector<std::string>&; ///< + /// +} // namespace GpgFrontend
\ No newline at end of file diff --git a/src/core/GpgConstants.cpp b/src/core/GpgConstants.cpp index 396af639..ade003a8 100644 --- a/src/core/GpgConstants.cpp +++ b/src/core/GpgConstants.cpp @@ -28,224 +28,5 @@ #include "core/GpgConstants.h" -#include <boost/algorithm/string.hpp> -#include <boost/algorithm/string/predicate.hpp> -#include <boost/lexical_cast.hpp> -#include <sstream> - -#include "core/function/FileOperator.h" - -const char* GpgFrontend::GpgConstants::PGP_CRYPT_BEGIN = - "-----BEGIN PGP MESSAGE-----"; ///< -const char* GpgFrontend::GpgConstants::PGP_CRYPT_END = - "-----END PGP MESSAGE-----"; ///< -const char* GpgFrontend::GpgConstants::PGP_SIGNED_BEGIN = - "-----BEGIN PGP SIGNED MESSAGE-----"; ///< -const char* GpgFrontend::GpgConstants::PGP_SIGNED_END = - "-----END PGP SIGNATURE-----"; ///< -const char* GpgFrontend::GpgConstants::PGP_SIGNATURE_BEGIN = - "-----BEGIN PGP SIGNATURE-----"; ///< -const char* GpgFrontend::GpgConstants::PGP_SIGNATURE_END = - "-----END PGP SIGNATURE-----"; ///< -const char* GpgFrontend::GpgConstants::PGP_PUBLIC_KEY_BEGIN = - "-----BEGIN PGP PUBLIC KEY BLOCK-----"; ///< -const char* GpgFrontend::GpgConstants::PGP_PRIVATE_KEY_BEGIN = - "-----BEGIN PGP PRIVATE KEY BLOCK-----"; ///< -const char* GpgFrontend::GpgConstants::GPG_FRONTEND_SHORT_CRYPTO_HEAD = - "GpgF_Scpt://"; ///< - -static inline void Ltrim(std::string& s) { - s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) { - return !std::isspace(ch); - })); -} - -static inline void Rtrim(std::string& s) { - s.erase(std::find_if(s.rbegin(), s.rend(), - [](unsigned char ch) { return !std::isspace(ch); }) - .base(), - s.end()); -} - -static inline auto Trim(std::string& s) -> std::string { - Ltrim(s); - Rtrim(s); - return s; -} - -auto GpgFrontend::CheckGpgError(gpgme_error_t err) -> gpgme_error_t { - if (gpg_err_code(err) != GPG_ERR_NO_ERROR) { - SPDLOG_ERROR("[error: {}] source: {} description: {}", gpg_err_code(err), - gpgme_strsource(err), gpgme_strerror(err)); - } - return err; -} - -auto GpgFrontend::CheckGpgError2ErrCode(gpgme_error_t err, - gpgme_error_t predict) - -> gpg_err_code_t { - auto err_code = gpg_err_code(err); - if (err_code != gpg_err_code(predict)) { - if (err_code == GPG_ERR_NO_ERROR) - SPDLOG_WARN("[Warning {}] Source: {} description: {} predict: {}", - gpg_err_code(err), gpgme_strsource(err), gpgme_strerror(err), - gpgme_strerror(err)); - else - SPDLOG_ERROR("[Error {}] Source: {} description: {} predict: {}", - gpg_err_code(err), gpgme_strsource(err), gpgme_strerror(err), - gpgme_strerror(err)); - } - return err_code; -} - -auto GpgFrontend::CheckGpgError(gpgme_error_t err, const std::string& comment) - -> gpgme_error_t { - if (gpg_err_code(err) != GPG_ERR_NO_ERROR) { - SPDLOG_WARN("[Error {}] Source: {} description: {} predict: {}", - gpg_err_code(err), gpgme_strsource(err), gpgme_strerror(err), - gpgme_strerror(err)); - } - return err; -} - -auto GpgFrontend::BeautifyFingerprint( - GpgFrontend::BypeArrayConstRef fingerprint) -> std::string { - auto len = fingerprint.size(); - std::stringstream out; - decltype(len) count = 0; - while (count < len) { - if ((count != 0U) && !(count % 5)) out << " "; - out << fingerprint[count]; - count++; - } - return out.str(); -} - -auto GpgFrontend::ReadAllDataInFile(const std::string& utf8_path) - -> std::string { - std::string data; - FileOperator::ReadFileStd(utf8_path, data); - return data; -} - -auto GpgFrontend::WriteBufferToFile(const std::string& utf8_path, - const std::string& out_buffer) -> bool { - return FileOperator::WriteFileStd(utf8_path, out_buffer); -} - -auto GpgFrontend::GetFileExtension(const std::string& path) -> std::string { - // Create a path object from given string - std::filesystem::path path_obj(path); - - // Check if file name in the path object has extension - if (path_obj.has_extension()) { - // Fetch the extension from path object and return - return path_obj.extension().u8string(); - } - // In case of no extension return empty string - return {}; -} - -auto GpgFrontend::GetOnlyFileNameWithPath(const std::string& path) - -> std::string { - // Create a path object from given string - std::filesystem::path path_obj(path); - // Check if file name in the path object has extension - if (path_obj.has_filename()) { - // Fetch the extension from path object and return - return (path_obj.parent_path() / path_obj.stem()).u8string(); - } - // In case of no extension return empty string - return {}; -} - -auto GpgFrontend::TextIsSigned(GpgFrontend::BypeArrayRef text) -> int { - using boost::algorithm::ends_with; - using boost::algorithm::starts_with; - - auto trim_text = Trim(text); - if (starts_with(trim_text, GpgConstants::PGP_SIGNED_BEGIN) && - ends_with(trim_text, GpgConstants::PGP_SIGNED_END)) { - return 2; - } - if (text.find(GpgConstants::PGP_SIGNED_BEGIN) != std::string::npos && - text.find(GpgConstants::PGP_SIGNED_END) != std::string::npos) { - return 1; - } - return 0; -} - -auto GpgFrontend::NewResult(gpgme_encrypt_result_t&& result) - -> GpgFrontend::GpgEncrResult { - gpgme_result_ref(result); - return {result, ResultRefDeletor()}; -} - -auto GpgFrontend::NewResult(gpgme_decrypt_result_t&& result) - -> GpgFrontend::GpgDecrResult { - gpgme_result_ref(result); - return {result, ResultRefDeletor()}; -} - -auto GpgFrontend::NewResult(gpgme_sign_result_t&& result) - -> GpgFrontend::GpgSignResult { - gpgme_result_ref(result); - return {result, ResultRefDeletor()}; -} - -auto GpgFrontend::NewResult(gpgme_verify_result_t&& result) - -> GpgFrontend::GpgVerifyResult { - gpgme_result_ref(result); - return {result, ResultRefDeletor()}; -} - -auto GpgFrontend::NewResult(gpgme_genkey_result_t&& result) - -> GpgFrontend::GpgGenKeyResult { - gpgme_result_ref(result); - return {result, ResultRefDeletor()}; -} - -void GpgFrontend::ResultRefDeletor::operator()(void* _result) { - SPDLOG_TRACE("gpgme unref {}", _result); - if (_result != nullptr) gpgme_result_unref(_result); -} - -auto GpgFrontend::CompareSoftwareVersion(const std::string& a, - const std::string& b) -> int { - auto remove_prefix = [](const std::string& version) { - return version.front() == 'v' ? version.substr(1) : version; - }; - - std::string real_version_a = remove_prefix(a); - std::string real_version_b = remove_prefix(b); - - std::vector<std::string> split_a; - std::vector<std::string> split_b; - boost::split(split_a, real_version_a, boost::is_any_of(".")); - boost::split(split_b, real_version_b, boost::is_any_of(".")); - - const auto min_depth = std::min(split_a.size(), split_b.size()); - - for (auto i = 0U; i < min_depth; ++i) { - int num_a = 0; - int num_b = 0; - - try { - num_a = boost::lexical_cast<int>(split_a[i]); - num_b = boost::lexical_cast<int>(split_b[i]); - } catch (boost::bad_lexical_cast&) { - // Handle exception if needed - return 0; - } - - if (num_a != num_b) { - return (num_a > num_b) ? 1 : -1; - } - } - - if (split_a.size() != split_b.size()) { - return (split_a.size() > split_b.size()) ? 1 : -1; - } - - return 0; -} +namespace GpgFrontend { +} // namespace GpgFrontend diff --git a/src/core/GpgConstants.h b/src/core/GpgConstants.h index cc64d491..1f3a4981 100644 --- a/src/core/GpgConstants.h +++ b/src/core/GpgConstants.h @@ -28,197 +28,27 @@ #pragma once -#include "GpgFrontendCore.h" - -const int kRestartCode = 1000; ///< only refresh ui -const int kDeepRestartCode = 1001; // refresh core and ui - namespace GpgFrontend { -using ByteArray = std::string; ///< -using ByteArrayPtr = std::shared_ptr<ByteArray>; ///< -using StdBypeArrayPtr = std::shared_ptr<ByteArray>; ///< -using BypeArrayRef = ByteArray&; ///< -using BypeArrayConstRef = const ByteArray&; ///< -using StringArgsPtr = std::unique_ptr<std::vector<std::string>>; ///< -using StringArgsRef = std::vector<std::string>&; ///< - -using GpgError = gpgme_error_t; - -/** - * @brief Result Deleter - * - */ -struct ResultRefDeletor { - void operator()(void* _result); -}; - -using GpgEncrResult = std::shared_ptr<struct _gpgme_op_encrypt_result>; ///< -using GpgDecrResult = std::shared_ptr<struct _gpgme_op_decrypt_result>; ///< -using GpgSignResult = std::shared_ptr<struct _gpgme_op_sign_result>; ///< -using GpgVerifyResult = std::shared_ptr<struct _gpgme_op_verify_result>; ///< -using GpgGenKeyResult = std::shared_ptr<struct _gpgme_op_genkey_result>; ///< -// Convert from gpgme_xxx_result to GpgXXXResult - -/** - * @brief - * - * @param result - * @return GpgEncrResult - */ -auto GPGFRONTEND_CORE_EXPORT NewResult(gpgme_encrypt_result_t&& result) - -> GpgEncrResult; - -/** - * @brief - * - * @param result - * @return GpgDecrResult - */ -auto GPGFRONTEND_CORE_EXPORT NewResult(gpgme_decrypt_result_t&& result) - -> GpgDecrResult; - -/** - * @brief - * - * @param result - * @return GpgSignResult - */ -auto GPGFRONTEND_CORE_EXPORT NewResult(gpgme_sign_result_t&& result) - -> GpgSignResult; - -/** - * @brief - * - * @param result - * @return GpgVerifyResult - */ -auto GPGFRONTEND_CORE_EXPORT NewResult(gpgme_verify_result_t&& result) - -> GpgVerifyResult; - -/** - * @brief - * - * @param result - * @return GpgGenKeyResult - */ -auto GPGFRONTEND_CORE_EXPORT NewResult(gpgme_genkey_result_t&& result) - -> GpgGenKeyResult; - -// Error Info Printer - -/** - * @brief - * - * @param err - * @return GpgError - */ -auto GPGFRONTEND_CORE_EXPORT CheckGpgError(GpgError err) -> GpgError; - -/** - * @brief - * - * @param gpgmeError - * @param comment - * @return GpgError - */ -auto GPGFRONTEND_CORE_EXPORT CheckGpgError(GpgError gpgmeError, - const std::string& comment) - -> GpgError; - -/** - * @brief - * - * @param err - * @param predict - * @return gpg_err_code_t - */ -auto GPGFRONTEND_CORE_EXPORT CheckGpgError2ErrCode( - gpgme_error_t err, gpgme_error_t predict = GPG_ERR_NO_ERROR) - -> gpg_err_code_t; - -// Fingerprint - -/** - * @brief - * - * @param fingerprint - * @return std::string - */ -auto GPGFRONTEND_CORE_EXPORT BeautifyFingerprint(BypeArrayConstRef fingerprint) - -> std::string; - -// File Operation - -/** - * @brief - * - * @param path - * @return std::string - */ -auto ReadAllDataInFile(const std::string& path) -> std::string; - -/** - * @brief - * - * @param path - * @param out_buffer - * @return true - * @return false - */ -auto GPGFRONTEND_CORE_EXPORT WriteBufferToFile(const std::string& path, - const std::string& out_buffer) - -> bool; - -auto GPGFRONTEND_CORE_EXPORT CompareSoftwareVersion(const std::string& a, - const std::string& b) - -> int; - -/** - * @brief Get the file extension object - * - * @param path - * @return std::string - */ -auto GetFileExtension(const std::string& path) -> std::string; - -/** - * @brief Get the only file name with path object - * - * @param path - * @return std::string - */ -auto GetOnlyFileNameWithPath(const std::string& path) -> std::string; - -// Check - -/** - * @brief - * - * @param text - * @return int - */ -auto TextIsSigned(BypeArrayRef text) -> int; +constexpr int kRestartCode = 1000; ///< only refresh ui +constexpr int kDeepRestartCode = 1001; // refresh core and ui // Channels -const int kGpgfrontendDefaultChannel = 0; ///< -const int kGpgfrontendNonAsciiChannel = 2; ///< - -/** - * @brief - * - */ -class GPGFRONTEND_CORE_EXPORT GpgConstants { - public: - static const char* PGP_CRYPT_BEGIN; ///< - static const char* PGP_CRYPT_END; ///< - static const char* PGP_SIGNED_BEGIN; ///< - static const char* PGP_SIGNED_END; ///< - static const char* PGP_SIGNATURE_BEGIN; ///< - static const char* PGP_SIGNATURE_END; ///< - static const char* PGP_PUBLIC_KEY_BEGIN; ///< - static const char* PGP_PRIVATE_KEY_BEGIN; ///< - static const char* GPG_FRONTEND_SHORT_CRYPTO_HEAD; ///< -}; +constexpr int kGpgfrontendDefaultChannel = 0; ///< +constexpr int kGpgfrontendNonAsciiChannel = 2; ///< + +// HEADER +constexpr const char* PGP_CRYPT_BEGIN = "-----BEGIN PGP MESSAGE-----"; ///< +constexpr const char* PGP_CRYPT_END = "-----END PGP MESSAGE-----"; ///< +constexpr const char* PGP_SIGNED_BEGIN = + "-----BEGIN PGP SIGNED MESSAGE-----"; ///< +constexpr const char* PGP_SIGNED_END = "-----END PGP SIGNATURE-----"; ///< +constexpr const char* PGP_SIGNATURE_BEGIN = + "-----BEGIN PGP SIGNATURE-----"; ///< +constexpr const char* PGP_SIGNATURE_END = "-----END PGP SIGNATURE-----"; ///< +constexpr const char* PGP_PUBLIC_KEY_BEGIN = + "-----BEGIN PGP PUBLIC KEY BLOCK-----"; ///< +constexpr const char* PGP_PRIVATE_KEY_BEGIN = + "-----BEGIN PGP PRIVATE KEY BLOCK-----"; ///< } // namespace GpgFrontend diff --git a/src/core/GpgModel.h b/src/core/GpgModel.h index d227e25d..facb1f72 100644 --- a/src/core/GpgModel.h +++ b/src/core/GpgModel.h @@ -28,13 +28,14 @@ #pragma once -#include "core/GpgConstants.h" #include "core/model/GpgData.h" #include "core/model/GpgKey.h" #include "core/model/GpgSignature.h" namespace GpgFrontend { +using GpgError = gpgme_error_t; ///< + using KeyId = std::string; ///< using SubkeyId = std::string; ///< using KeyIdArgsList = std::vector<KeyId>; ///< @@ -46,7 +47,7 @@ using SignIdArgsListPtr = std::unique_ptr<SignIdArgsList>; ///< using KeyFprArgsListPtr = std::unique_ptr<std::vector<std::string>>; ///< using KeyArgsList = std::vector<GpgKey>; ///< using KeyListPtr = std::shared_ptr<KeyArgsList>; ///< -using GpgKeyLinkList = std::list<GpgFrontend::GpgKey>; ///< +using GpgKeyLinkList = std::list<GpgKey>; ///< using KeyLinkListPtr = std::unique_ptr<GpgKeyLinkList>; ///< using KeyPtr = std::unique_ptr<GpgKey>; ///< using KeyPtrArgsList = const std::initializer_list<KeyPtr>; ///< diff --git a/src/core/function/ArchiveFileOperator.cpp b/src/core/function/ArchiveFileOperator.cpp index 4fc8f5df..27d18803 100644 --- a/src/core/function/ArchiveFileOperator.cpp +++ b/src/core/function/ArchiveFileOperator.cpp @@ -170,10 +170,10 @@ void GpgFrontend::ArchiveFileOperator::CreateArchive( if (r > ARCHIVE_FAILED) { QByteArray buff; #ifdef WINDOWS - FileOperator::ReadFile( - QString::fromStdWString(archive_entry_sourcepath_w(entry)), buff); + ReadFile(QString::fromStdWString(archive_entry_sourcepath_w(entry)), + buff); #else - FileOperator::ReadFile(archive_entry_sourcepath(entry), buff); + ReadFile(archive_entry_sourcepath(entry), buff); #endif archive_write_data(a, buff.data(), buff.size()); } diff --git a/src/core/function/ArchiveFileOperator.h b/src/core/function/ArchiveFileOperator.h index cc74231d..e4bf2011 100644 --- a/src/core/function/ArchiveFileOperator.h +++ b/src/core/function/ArchiveFileOperator.h @@ -29,7 +29,7 @@ #pragma once #include "core/GpgFrontendCore.h" -#include "core/function/FileOperator.h" +#include "core/utils/IOUtils.h" namespace GpgFrontend { diff --git a/src/core/function/DataObjectOperator.cpp b/src/core/function/DataObjectOperator.cpp index fcf77319..2fb794c5 100644 --- a/src/core/function/DataObjectOperator.cpp +++ b/src/core/function/DataObjectOperator.cpp @@ -32,13 +32,13 @@ #include <boost/date_time.hpp> -#include "core/function/FileOperator.h" #include "core/function/PassphraseGenerator.h" +#include "core/utils/IOUtils.h" void GpgFrontend::DataObjectOperator::init_app_secure_key() { SPDLOG_DEBUG("initializing application secure key"); - FileOperator::WriteFileStd(app_secure_key_path_, - PassphraseGenerator::GetInstance().Generate(256)); + WriteFileStd(app_secure_key_path_, + PassphraseGenerator::GetInstance().Generate(256)); std::filesystem::permissions( app_secure_key_path_, std::filesystem::perms::owner_read | std::filesystem::perms::owner_write); @@ -53,7 +53,7 @@ GpgFrontend::DataObjectOperator::DataObjectOperator(int channel) } std::string key; - if (!FileOperator::ReadFileStd(app_secure_key_path_.u8string(), key)) { + if (!ReadFileStd(app_secure_key_path_.u8string(), key)) { SPDLOG_ERROR("failed to read app secure key file: {}", app_secure_key_path_.u8string()); throw std::runtime_error("failed to read app secure key file"); @@ -96,7 +96,7 @@ std::string GpgFrontend::DataObjectOperator::SaveDataObj( SPDLOG_DEBUG("saving data object {} to {} , size: {} bytes", _hash_obj_key, obj_path.u8string(), encoded.size()); - FileOperator::WriteFileStd(obj_path.u8string(), encoded.toStdString()); + WriteFileStd(obj_path.u8string(), encoded.toStdString()); return _key.empty() ? _hash_obj_key : std::string(); } @@ -119,7 +119,7 @@ std::optional<nlohmann::json> GpgFrontend::DataObjectOperator::GetDataObject( } std::string buffer; - if (!FileOperator::ReadFileStd(obj_path.u8string(), buffer)) { + if (!ReadFileStd(obj_path.u8string(), buffer)) { SPDLOG_ERROR("failed to read data object: {}", _key); return {}; } @@ -156,7 +156,7 @@ GpgFrontend::DataObjectOperator::GetDataObjectByRef(const std::string& _ref) { if (!std::filesystem::exists(obj_path)) return {}; std::string buffer; - if (!FileOperator::ReadFileStd(obj_path.u8string(), buffer)) return {}; + if (!ReadFileStd(obj_path.u8string(), buffer)) return {}; auto encoded = QByteArray::fromStdString(buffer); QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::ECB, diff --git a/src/core/function/FileOperator.h b/src/core/function/FileOperator.h deleted file mode 100644 index 53f64e5a..00000000 --- a/src/core/function/FileOperator.h +++ /dev/null @@ -1,89 +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 - * - */ - -#pragma once - -#include "core/GpgFrontendCore.h" - -namespace GpgFrontend { - -/** - * @brief provides file operations - * - */ -class GPGFRONTEND_CORE_EXPORT FileOperator { - public: - /** - * @brief read file content using std struct - * - * - * @param file_name file name - * @param data data read from file - * @return - */ - static bool ReadFileStd(const std::filesystem::path &file_name, - std::string &data); - - /** - * @brief write file content using std struct - * - * @param file_name file name - * @param data data to write to file - * @return - */ - static bool WriteFileStd(const std::filesystem::path &file_name, - const std::string &data); - - /** - * @brief read file content - * - * @param file_name file name - * @param data data read from file - * @return true if success - * @return false if failed - */ - static bool ReadFile(const QString &file_name, QByteArray &data); - - /** - * @brief write file content - * - * @param file_name file name - * @param data data to write to file - * @return true if success - * @return false if failed - */ - static bool WriteFile(const QString &file_name, const QByteArray &data); - - /** - * calculate the hash of a file - * @param file_path - * @return - */ - static std::string CalculateHash(const std::filesystem::path &file_path); -}; -} // namespace GpgFrontend diff --git a/src/core/function/GlobalSettingStation.cpp b/src/core/function/GlobalSettingStation.cpp index 95d35c5c..5a6852f7 100644 --- a/src/core/function/GlobalSettingStation.cpp +++ b/src/core/function/GlobalSettingStation.cpp @@ -29,7 +29,7 @@ #include "GlobalSettingStation.h" #include "GpgFrontendBuildInstallInfo.h" -#include "core/function/FileOperator.h" +#include "core/utils/IOUtils.h" namespace GpgFrontend { diff --git a/src/core/function/KeyPackageOperator.cpp b/src/core/function/KeyPackageOperator.cpp index 9494d7e1..3beaad32 100644 --- a/src/core/function/KeyPackageOperator.cpp +++ b/src/core/function/KeyPackageOperator.cpp @@ -32,11 +32,11 @@ #include <boost/format.hpp> -#include "core/function/FileOperator.h" #include "core/function/KeyPackageOperator.h" #include "core/function/PassphraseGenerator.h" #include "core/function/gpg/GpgKeyGetter.h" #include "core/function/gpg/GpgKeyImportExporter.h" +#include "core/utils/IOUtils.h" namespace GpgFrontend { @@ -44,7 +44,7 @@ bool KeyPackageOperator::GeneratePassphrase( const std::filesystem::path& phrase_path, std::string& phrase) { phrase = PassphraseGenerator::GetInstance().Generate(256); SPDLOG_DEBUG("generated passphrase: {} bytes", phrase.size()); - return FileOperator::WriteFileStd(phrase_path, phrase); + return WriteFileStd(phrase_path, phrase); } bool KeyPackageOperator::GenerateKeyPackage( @@ -69,7 +69,7 @@ bool KeyPackageOperator::GenerateKeyPackage( auto encoded = encryption.encode(data, hash_key); SPDLOG_DEBUG("writing key package: {}", key_package_name); - return FileOperator::WriteFileStd(key_package_path, encoded.toStdString()); + return WriteFileStd(key_package_path, encoded.toStdString()); } bool KeyPackageOperator::ImportKeyPackage( @@ -79,7 +79,7 @@ bool KeyPackageOperator::ImportKeyPackage( SPDLOG_DEBUG("importing key package: {]", key_package_path.u8string()); std::string encrypted_data; - FileOperator::ReadFileStd(key_package_path, encrypted_data); + ReadFileStd(key_package_path, encrypted_data); if (encrypted_data.empty()) { SPDLOG_ERROR("failed to read key package: {}", key_package_path.u8string()); @@ -87,7 +87,7 @@ bool KeyPackageOperator::ImportKeyPackage( }; std::string passphrase; - FileOperator::ReadFileStd(phrase_path, passphrase); + ReadFileStd(phrase_path, passphrase); SPDLOG_DEBUG("passphrase: {} bytes", passphrase.size()); if (passphrase.size() != 256) { SPDLOG_ERROR("failed to read passphrase: {}", phrase_path.u8string()); @@ -105,8 +105,8 @@ bool KeyPackageOperator::ImportKeyPackage( auto key_data = QByteArray::fromBase64(decoded); SPDLOG_DEBUG("key data size: {}", key_data.size()); - if (!key_data.startsWith(GpgConstants::PGP_PUBLIC_KEY_BEGIN) && - !key_data.startsWith(GpgConstants::PGP_PRIVATE_KEY_BEGIN)) { + if (!key_data.startsWith(PGP_PUBLIC_KEY_BEGIN) && + !key_data.startsWith(PGP_PRIVATE_KEY_BEGIN)) { return false; } diff --git a/src/core/function/gpg/GpgBasicOperator.cpp b/src/core/function/gpg/GpgBasicOperator.cpp index 3296076b..836dafaa 100644 --- a/src/core/function/gpg/GpgBasicOperator.cpp +++ b/src/core/function/gpg/GpgBasicOperator.cpp @@ -28,6 +28,8 @@ #include "GpgBasicOperator.h" +#include "core/utils/GpgUtils.h" + GpgFrontend::GpgBasicOperator::GpgBasicOperator(int channel) : SingletonFunctionObject<GpgBasicOperator>(channel) {} diff --git a/src/core/function/gpg/GpgBasicOperator.h b/src/core/function/gpg/GpgBasicOperator.h index 1c9720cd..581b6b95 100644 --- a/src/core/function/gpg/GpgBasicOperator.h +++ b/src/core/function/gpg/GpgBasicOperator.h @@ -28,10 +28,10 @@ #pragma once -#include "core/GpgConstants.h" #include "core/GpgModel.h" #include "core/function/basic/GpgFunctionObject.h" #include "core/function/gpg/GpgContext.h" +#include "core/function/result_analyse/GpgResultAnalyse.h" namespace GpgFrontend { diff --git a/src/core/function/gpg/GpgContext.cpp b/src/core/function/gpg/GpgContext.cpp index fb51f09f..3df684ea 100644 --- a/src/core/function/gpg/GpgContext.cpp +++ b/src/core/function/gpg/GpgContext.cpp @@ -32,7 +32,6 @@ #include <gpgme.h> #include <unistd.h> -#include "core/GpgConstants.h" #include "core/common/CoreCommonUtil.h" #include "core/function/CoreSignalStation.h" #include "core/function/basic/GpgFunctionObject.h" @@ -41,6 +40,8 @@ #include "core/module/ModuleManager.h" #include "core/thread/Task.h" #include "core/thread/TaskRunnerGetter.h" +#include "core/utils/CommonUtils.h" +#include "core/utils/GpgUtils.h" #ifdef _WIN32 #include <windows.h> diff --git a/src/core/function/gpg/GpgFileOpera.cpp b/src/core/function/gpg/GpgFileOpera.cpp index f1b1d1c9..8d5e3369 100644 --- a/src/core/function/gpg/GpgFileOpera.cpp +++ b/src/core/function/gpg/GpgFileOpera.cpp @@ -27,9 +27,9 @@ */ #include "GpgFileOpera.h" -#include "core/GpgConstants.h" -#include "core/function/FileOperator.h" #include "core/function/gpg/GpgBasicOperator.h" +#include "core/utils/GpgUtils.h" +#include "core/utils/IOUtils.h" auto GpgFrontend::GpgFileOpera::EncryptFile(KeyListPtr keys, const std::string& in_path, @@ -47,7 +47,7 @@ auto GpgFrontend::GpgFileOpera::EncryptFile(KeyListPtr keys, #endif std::string in_buffer; - if (!FileOperator::ReadFileStd(in_path_std, in_buffer)) { + if (!ReadFileStd(in_path_std, in_buffer)) { throw std::runtime_error("read file error"); } @@ -57,7 +57,7 @@ auto GpgFrontend::GpgFileOpera::EncryptFile(KeyListPtr keys, std::move(keys), in_buffer, out_buffer, result); if (CheckGpgError(err) == GPG_ERR_NO_ERROR) - if (!FileOperator::WriteFileStd(out_path_std, *out_buffer)) { + if (!WriteFileStd(out_path_std, *out_buffer)) { throw std::runtime_error("WriteBufferToFile error"); }; @@ -79,7 +79,7 @@ auto GpgFrontend::GpgFileOpera::DecryptFile(const std::string& in_path, #endif std::string in_buffer; - if (!FileOperator::ReadFileStd(in_path_std, in_buffer)) { + if (!ReadFileStd(in_path_std, in_buffer)) { throw std::runtime_error("read file error"); } ByteArrayPtr out_buffer; @@ -90,7 +90,7 @@ auto GpgFrontend::GpgFileOpera::DecryptFile(const std::string& in_path, assert(CheckGpgError(err) == GPG_ERR_NO_ERROR); if (CheckGpgError(err) == GPG_ERR_NO_ERROR) - if (!FileOperator::WriteFileStd(out_path_std, *out_buffer)) { + if (!WriteFileStd(out_path_std, *out_buffer)) { throw std::runtime_error("WriteBufferToFile error"); }; @@ -113,7 +113,7 @@ auto GpgFrontend::GpgFileOpera::SignFile(KeyListPtr keys, #endif std::string in_buffer; - if (!FileOperator::ReadFileStd(in_path_std, in_buffer)) { + if (!ReadFileStd(in_path_std, in_buffer)) { throw std::runtime_error("read file error"); } ByteArrayPtr out_buffer; @@ -122,7 +122,7 @@ auto GpgFrontend::GpgFileOpera::SignFile(KeyListPtr keys, std::move(keys), in_buffer, out_buffer, GPGME_SIG_MODE_DETACH, result); if (CheckGpgError(err) == GPG_ERR_NO_ERROR) - if (!FileOperator::WriteFileStd(out_path_std, *out_buffer)) { + if (!WriteFileStd(out_path_std, *out_buffer)) { throw std::runtime_error("WriteBufferToFile error"); }; @@ -144,13 +144,13 @@ auto GpgFrontend::GpgFileOpera::VerifyFile(const std::string& data_path, #endif std::string in_buffer; - if (!FileOperator::ReadFileStd(data_path_std, in_buffer)) { + if (!ReadFileStd(data_path_std, in_buffer)) { throw std::runtime_error("read file error"); } ByteArrayPtr sign_buffer = nullptr; if (!sign_path.empty()) { std::string sign_buffer_str; - if (!FileOperator::ReadFileStd(sign_path_std, sign_buffer_str)) { + if (!ReadFileStd(sign_path_std, sign_buffer_str)) { throw std::runtime_error("read file error"); } sign_buffer = std::make_unique<std::string>(sign_buffer_str); @@ -175,7 +175,7 @@ auto GpgFrontend::GpgFileOpera::EncryptSignFile( #endif std::string in_buffer; - if (!FileOperator::ReadFileStd(in_path_std, in_buffer)) { + if (!ReadFileStd(in_path_std, in_buffer)) { throw std::runtime_error("read file error"); } ByteArrayPtr out_buffer = nullptr; @@ -185,7 +185,7 @@ auto GpgFrontend::GpgFileOpera::EncryptSignFile( sign_res); if (CheckGpgError(err) == GPG_ERR_NO_ERROR) - if (!FileOperator::WriteFileStd(out_path_std, *out_buffer)) { + if (!WriteFileStd(out_path_std, *out_buffer)) { throw std::runtime_error("WriteBufferToFile error"); }; @@ -208,7 +208,7 @@ auto GpgFrontend::GpgFileOpera::DecryptVerifyFile(const std::string& in_path, #endif std::string in_buffer; - if (!FileOperator::ReadFileStd(in_path_std, in_buffer)) { + if (!ReadFileStd(in_path_std, in_buffer)) { throw std::runtime_error("read file error"); } @@ -217,7 +217,7 @@ auto GpgFrontend::GpgFileOpera::DecryptVerifyFile(const std::string& in_path, in_buffer, out_buffer, decr_res, verify_res); if (CheckGpgError(err) == GPG_ERR_NO_ERROR) - if (!FileOperator::WriteFileStd(out_path_std, *out_buffer)) { + if (!WriteFileStd(out_path_std, *out_buffer)) { throw std::runtime_error("write file error"); }; @@ -237,7 +237,7 @@ auto GpgFrontend::GpgFileOpera::EncryptFileSymmetric( #endif std::string in_buffer; - if (!FileOperator::ReadFileStd(in_path_std, in_buffer)) { + if (!ReadFileStd(in_path_std, in_buffer)) { throw std::runtime_error("read file error"); } @@ -246,7 +246,7 @@ auto GpgFrontend::GpgFileOpera::EncryptFileSymmetric( in_buffer, out_buffer, result); if (CheckGpgError(err) == GPG_ERR_NO_ERROR) { - if (!FileOperator::WriteFileStd(out_path_std, *out_buffer)) { + if (!WriteFileStd(out_path_std, *out_buffer)) { throw std::runtime_error("WriteBufferToFile error"); } }; diff --git a/src/core/function/gpg/GpgFileOpera.h b/src/core/function/gpg/GpgFileOpera.h index 8fd6bb34..722555fc 100644 --- a/src/core/function/gpg/GpgFileOpera.h +++ b/src/core/function/gpg/GpgFileOpera.h @@ -29,6 +29,7 @@ #pragma once #include "core/GpgModel.h" +#include "core/function/result_analyse/GpgResultAnalyse.h" namespace GpgFrontend { diff --git a/src/core/function/gpg/GpgKeyGetter.cpp b/src/core/function/gpg/GpgKeyGetter.cpp index 4919ce05..73d40a0a 100644 --- a/src/core/function/gpg/GpgKeyGetter.cpp +++ b/src/core/function/gpg/GpgKeyGetter.cpp @@ -33,8 +33,8 @@ #include <mutex> #include <shared_mutex> -#include "core/GpgConstants.h" #include "core/function/gpg/GpgContext.h" +#include "core/utils/GpgUtils.h" namespace GpgFrontend { diff --git a/src/core/function/gpg/GpgKeyImportExporter.cpp b/src/core/function/gpg/GpgKeyImportExporter.cpp index 1fa252a4..29bbf60e 100644 --- a/src/core/function/gpg/GpgKeyImportExporter.cpp +++ b/src/core/function/gpg/GpgKeyImportExporter.cpp @@ -28,8 +28,8 @@ #include "GpgKeyImportExporter.h" -#include "core/GpgConstants.h" #include "core/function/gpg/GpgKeyGetter.h" +#include "core/utils/GpgUtils.h" GpgFrontend::GpgKeyImportExporter::GpgKeyImportExporter(int channel) : SingletonFunctionObject<GpgKeyImportExporter>(channel), diff --git a/src/core/function/gpg/GpgKeyManager.cpp b/src/core/function/gpg/GpgKeyManager.cpp index 291e1594..3e27f946 100644 --- a/src/core/function/gpg/GpgKeyManager.cpp +++ b/src/core/function/gpg/GpgKeyManager.cpp @@ -33,6 +33,7 @@ #include "core/function/gpg/GpgBasicOperator.h" #include "core/function/gpg/GpgKeyGetter.h" +#include "core/utils/GpgUtils.h" GpgFrontend::GpgKeyManager::GpgKeyManager(int channel) : SingletonFunctionObject<GpgKeyManager>(channel) {} diff --git a/src/core/function/gpg/GpgKeyOpera.cpp b/src/core/function/gpg/GpgKeyOpera.cpp index 1b4d9881..c31a79e3 100644 --- a/src/core/function/gpg/GpgKeyOpera.cpp +++ b/src/core/function/gpg/GpgKeyOpera.cpp @@ -37,8 +37,11 @@ #include "core/GpgConstants.h" #include "core/function/gpg/GpgCommandExecutor.h" #include "core/function/gpg/GpgKeyGetter.h" +#include "core/function/result_analyse/GpgResultAnalyse.h" #include "core/model/GpgGenKeyInfo.h" #include "core/module/ModuleManager.h" +#include "core/utils/CommonUtils.h" +#include "core/utils/GpgUtils.h" namespace GpgFrontend { diff --git a/src/core/function/gpg/GpgKeyOpera.h b/src/core/function/gpg/GpgKeyOpera.h index bb96b1e8..2c92b9b0 100644 --- a/src/core/function/gpg/GpgKeyOpera.h +++ b/src/core/function/gpg/GpgKeyOpera.h @@ -28,9 +28,9 @@ #pragma once -#include "core/GpgConstants.h" #include "core/GpgModel.h" #include "core/function/gpg/GpgContext.h" +#include "core/function/result_analyse/GpgResultAnalyse.h" namespace GpgFrontend { /** diff --git a/src/core/function/gpg/GpgUIDOperator.cpp b/src/core/function/gpg/GpgUIDOperator.cpp index 61e8c54c..2c0445b1 100644 --- a/src/core/function/gpg/GpgUIDOperator.cpp +++ b/src/core/function/gpg/GpgUIDOperator.cpp @@ -28,7 +28,9 @@ #include "GpgUIDOperator.h" -#include "boost/format.hpp" +#include <boost/format.hpp> + +#include "core/utils/GpgUtils.h" GpgFrontend::GpgUIDOperator::GpgUIDOperator(int channel) : SingletonFunctionObject<GpgUIDOperator>(channel) {} diff --git a/src/core/function/result_analyse/GpgDecryptResultAnalyse.cpp b/src/core/function/result_analyse/GpgDecryptResultAnalyse.cpp index f655f653..4426d3b9 100644 --- a/src/core/function/result_analyse/GpgDecryptResultAnalyse.cpp +++ b/src/core/function/result_analyse/GpgDecryptResultAnalyse.cpp @@ -28,7 +28,8 @@ #include "GpgDecryptResultAnalyse.h" -#include "function/gpg/GpgKeyGetter.h" +#include "core/GpgModel.h" +#include "core/function/gpg/GpgKeyGetter.h" GpgFrontend::GpgDecryptResultAnalyse::GpgDecryptResultAnalyse( GpgError m_error, GpgDecrResult m_result) diff --git a/src/core/function/result_analyse/GpgResultAnalyse.h b/src/core/function/result_analyse/GpgResultAnalyse.h index 513e4515..de4551c1 100644 --- a/src/core/function/result_analyse/GpgResultAnalyse.h +++ b/src/core/function/result_analyse/GpgResultAnalyse.h @@ -29,9 +29,16 @@ #include <sstream> -#include "core/GpgConstants.h" +#include "core/GpgModel.h" + namespace GpgFrontend { +using GpgEncrResult = std::shared_ptr<struct _gpgme_op_encrypt_result>; ///< +using GpgDecrResult = std::shared_ptr<struct _gpgme_op_decrypt_result>; ///< +using GpgSignResult = std::shared_ptr<struct _gpgme_op_sign_result>; ///< +using GpgVerifyResult = std::shared_ptr<struct _gpgme_op_verify_result>; ///< +using GpgGenKeyResult = std::shared_ptr<struct _gpgme_op_genkey_result>; ///< + class GPGFRONTEND_CORE_EXPORT GpgResultAnalyse { public: /** diff --git a/src/core/function/result_analyse/GpgVerifyResultAnalyse.cpp b/src/core/function/result_analyse/GpgVerifyResultAnalyse.cpp index 7928ed04..b9947cd7 100644 --- a/src/core/function/result_analyse/GpgVerifyResultAnalyse.cpp +++ b/src/core/function/result_analyse/GpgVerifyResultAnalyse.cpp @@ -32,7 +32,8 @@ #include "GpgFrontend.h" #include "core/GpgConstants.h" -#include "function/gpg/GpgKeyGetter.h" +#include "core/function/gpg/GpgKeyGetter.h" +#include "core/utils/CommonUtils.h" GpgFrontend::GpgVerifyResultAnalyse::GpgVerifyResultAnalyse( GpgError error, GpgVerifyResult result) diff --git a/src/core/function/result_analyse/GpgVerifyResultAnalyse.h b/src/core/function/result_analyse/GpgVerifyResultAnalyse.h index c9e7b689..88361110 100644 --- a/src/core/function/result_analyse/GpgVerifyResultAnalyse.h +++ b/src/core/function/result_analyse/GpgVerifyResultAnalyse.h @@ -29,7 +29,7 @@ #pragma once #include "GpgResultAnalyse.h" -#include "core/model/GpgKeySignature.h" + namespace GpgFrontend { /** diff --git a/src/core/model/GpgData.h b/src/core/model/GpgData.h index a4b58638..39097b0c 100644 --- a/src/core/model/GpgData.h +++ b/src/core/model/GpgData.h @@ -28,7 +28,7 @@ #pragma once -#include "core/GpgConstants.h" +#include "core/CoreTypedef.h" namespace GpgFrontend { /** diff --git a/src/core/model/GpgSubKey.h b/src/core/model/GpgSubKey.h index 589b428a..906a5fda 100644 --- a/src/core/model/GpgSubKey.h +++ b/src/core/model/GpgSubKey.h @@ -30,8 +30,6 @@ #include <boost/date_time.hpp> -#include "core/GpgConstants.h" - namespace GpgFrontend { /** diff --git a/src/core/model/GpgTOFUInfo.h b/src/core/model/GpgTOFUInfo.h index f9f6a451..9deec33f 100644 --- a/src/core/model/GpgTOFUInfo.h +++ b/src/core/model/GpgTOFUInfo.h @@ -28,8 +28,6 @@ #pragma once -#include "core/GpgConstants.h" - namespace GpgFrontend { /** * @brief diff --git a/src/core/utils/CommonUtils.cpp b/src/core/utils/CommonUtils.cpp new file mode 100644 index 00000000..0eb4933e --- /dev/null +++ b/src/core/utils/CommonUtils.cpp @@ -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 + * + */ + +#include "CommonUtils.h" + +#include <boost/algorithm/string.hpp> +#include <boost/lexical_cast.hpp> + +namespace GpgFrontend { + +auto BeautifyFingerprint(BypeArrayConstRef fingerprint) -> std::string { + auto len = fingerprint.size(); + std::stringstream out; + decltype(len) count = 0; + while (count < len) { + if ((count != 0U) && !(count % 5)) out << " "; + out << fingerprint[count]; + count++; + } + return out.str(); +} + +auto CompareSoftwareVersion(const std::string& a, const std::string& b) -> int { + auto remove_prefix = [](const std::string& version) { + return version.front() == 'v' ? version.substr(1) : version; + }; + + std::string real_version_a = remove_prefix(a); + std::string real_version_b = remove_prefix(b); + + std::vector<std::string> split_a; + std::vector<std::string> split_b; + boost::split(split_a, real_version_a, boost::is_any_of(".")); + boost::split(split_b, real_version_b, boost::is_any_of(".")); + + const auto min_depth = std::min(split_a.size(), split_b.size()); + + for (auto i = 0U; i < min_depth; ++i) { + int num_a = 0; + int num_b = 0; + + try { + num_a = boost::lexical_cast<int>(split_a[i]); + num_b = boost::lexical_cast<int>(split_b[i]); + } catch (boost::bad_lexical_cast&) { + // Handle exception if needed + return 0; + } + + if (num_a != num_b) { + return (num_a > num_b) ? 1 : -1; + } + } + + if (split_a.size() != split_b.size()) { + return (split_a.size() > split_b.size()) ? 1 : -1; + } + + return 0; +} +} // namespace GpgFrontend
\ No newline at end of file diff --git a/src/core/utils/CommonUtils.h b/src/core/utils/CommonUtils.h new file mode 100644 index 00000000..94a727e9 --- /dev/null +++ b/src/core/utils/CommonUtils.h @@ -0,0 +1,48 @@ +/** + * 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 + +#include "core/CoreTypedef.h" + +namespace GpgFrontend { + +/** + * @brief + * + * @param fingerprint + * @return std::string + */ +auto GPGFRONTEND_CORE_EXPORT BeautifyFingerprint(BypeArrayConstRef fingerprint) + -> std::string; + +auto GPGFRONTEND_CORE_EXPORT CompareSoftwareVersion(const std::string& a, + const std::string& b) + -> int; + +} // namespace GpgFrontend
\ No newline at end of file diff --git a/src/core/utils/FilesystemUtils.cpp b/src/core/utils/FilesystemUtils.cpp new file mode 100644 index 00000000..edf86297 --- /dev/null +++ b/src/core/utils/FilesystemUtils.cpp @@ -0,0 +1,58 @@ +/** + * 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 "FilesystemUtils.h" + +namespace GpgFrontend { + +auto GetOnlyFileNameWithPath(const std::string& path) -> std::string { + // Create a path object from given string + std::filesystem::path path_obj(path); + // Check if file name in the path object has extension + if (path_obj.has_filename()) { + // Fetch the extension from path object and return + return (path_obj.parent_path() / path_obj.stem()).u8string(); + } + // In case of no extension return empty string + return {}; +} + +auto GetFileExtension(const std::string& path) -> std::string { + // Create a path object from given string + std::filesystem::path path_obj(path); + + // Check if file name in the path object has extension + if (path_obj.has_extension()) { + // Fetch the extension from path object and return + return path_obj.extension().u8string(); + } + // In case of no extension return empty string + return {}; +} + +} // namespace GpgFrontend
\ No newline at end of file diff --git a/src/core/utils/FilesystemUtils.h b/src/core/utils/FilesystemUtils.h new file mode 100644 index 00000000..23b73b2b --- /dev/null +++ b/src/core/utils/FilesystemUtils.h @@ -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 + * + */ + +#pragma once + +/** + * @brief Get the file extension object + * + * @param path + * @return std::string + */ +auto GetFileExtension(const std::string& path) -> std::string; + +/** + * @brief Get the only file name with path object + * + * @param path + * @return std::string + */ +auto GetOnlyFileNameWithPath(const std::string& path) -> std::string;
\ No newline at end of file diff --git a/src/core/utils/GpgUtils.cpp b/src/core/utils/GpgUtils.cpp new file mode 100644 index 00000000..a8e15e31 --- /dev/null +++ b/src/core/utils/GpgUtils.cpp @@ -0,0 +1,133 @@ +/** + * 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 "GpgUtils.h" + +#include "core/utils/IOUtils.h" + +namespace GpgFrontend { + +static inline void Ltrim(std::string& s) { + s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) { + return !std::isspace(ch); + })); +} + +static inline void Rtrim(std::string& s) { + s.erase(std::find_if(s.rbegin(), s.rend(), + [](unsigned char ch) { return !std::isspace(ch); }) + .base(), + s.end()); +} + +static inline auto Trim(std::string& s) -> std::string { + Ltrim(s); + Rtrim(s); + return s; +} + +auto CheckGpgError(gpgme_error_t err) -> gpgme_error_t { + if (gpg_err_code(err) != GPG_ERR_NO_ERROR) { + SPDLOG_ERROR("[error: {}] source: {} description: {}", gpg_err_code(err), + gpgme_strsource(err), gpgme_strerror(err)); + } + return err; +} + +auto CheckGpgError2ErrCode(gpgme_error_t err, gpgme_error_t predict) + -> gpg_err_code_t { + auto err_code = gpg_err_code(err); + if (err_code != gpg_err_code(predict)) { + if (err_code == GPG_ERR_NO_ERROR) + SPDLOG_WARN("[Warning {}] Source: {} description: {} predict: {}", + gpg_err_code(err), gpgme_strsource(err), gpgme_strerror(err), + gpgme_strerror(err)); + else + SPDLOG_ERROR("[Error {}] Source: {} description: {} predict: {}", + gpg_err_code(err), gpgme_strsource(err), gpgme_strerror(err), + gpgme_strerror(err)); + } + return err_code; +} + +auto CheckGpgError(gpgme_error_t err, const std::string& comment) + -> gpgme_error_t { + if (gpg_err_code(err) != GPG_ERR_NO_ERROR) { + SPDLOG_WARN("[Error {}] Source: {} description: {} predict: {}", + gpg_err_code(err), gpgme_strsource(err), gpgme_strerror(err), + gpgme_strerror(err)); + } + return err; +} + +auto TextIsSigned(BypeArrayRef text) -> int { + using boost::algorithm::ends_with; + using boost::algorithm::starts_with; + + auto trim_text = Trim(text); + if (starts_with(trim_text, PGP_SIGNED_BEGIN) && + ends_with(trim_text, PGP_SIGNED_END)) { + return 2; + } + if (text.find(PGP_SIGNED_BEGIN) != std::string::npos && + text.find(PGP_SIGNED_END) != std::string::npos) { + return 1; + } + return 0; +} + +auto NewResult(gpgme_encrypt_result_t&& result) -> GpgEncrResult { + gpgme_result_ref(result); + return {result, ResultRefDeletor()}; +} + +auto NewResult(gpgme_decrypt_result_t&& result) -> GpgDecrResult { + gpgme_result_ref(result); + return {result, ResultRefDeletor()}; +} + +auto NewResult(gpgme_sign_result_t&& result) -> GpgSignResult { + gpgme_result_ref(result); + return {result, ResultRefDeletor()}; +} + +auto NewResult(gpgme_verify_result_t&& result) -> GpgVerifyResult { + gpgme_result_ref(result); + return {result, ResultRefDeletor()}; +} + +auto NewResult(gpgme_genkey_result_t&& result) -> GpgGenKeyResult { + gpgme_result_ref(result); + return {result, ResultRefDeletor()}; +} + +void ResultRefDeletor::operator()(void* _result) { + SPDLOG_TRACE("gpgme unref {}", _result); + if (_result != nullptr) gpgme_result_unref(_result); +} +} // namespace GpgFrontend diff --git a/src/core/utils/GpgUtils.h b/src/core/utils/GpgUtils.h new file mode 100644 index 00000000..201c7320 --- /dev/null +++ b/src/core/utils/GpgUtils.h @@ -0,0 +1,133 @@ +/** + * 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 + +#include "core/GpgModel.h" +#include "core/function/result_analyse/GpgResultAnalyse.h" + +namespace GpgFrontend { + +/** + * @brief Result Deleter + * + */ +struct ResultRefDeletor { + void operator()(void* _result); +}; + +// Convert from gpgme_xxx_result to GpgXXXResult + +/** + * @brief + * + * @param result + * @return GpgEncrResult + */ +auto GPGFRONTEND_CORE_EXPORT NewResult(gpgme_encrypt_result_t&& result) + -> GpgEncrResult; + +/** + * @brief + * + * @param result + * @return GpgDecrResult + */ +auto GPGFRONTEND_CORE_EXPORT NewResult(gpgme_decrypt_result_t&& result) + -> GpgDecrResult; + +/** + * @brief + * + * @param result + * @return GpgSignResult + */ +auto GPGFRONTEND_CORE_EXPORT NewResult(gpgme_sign_result_t&& result) + -> GpgSignResult; + +/** + * @brief + * + * @param result + * @return GpgVerifyResult + */ +auto GPGFRONTEND_CORE_EXPORT NewResult(gpgme_verify_result_t&& result) + -> GpgVerifyResult; + +/** + * @brief + * + * @param result + * @return GpgGenKeyResult + */ +auto GPGFRONTEND_CORE_EXPORT NewResult(gpgme_genkey_result_t&& result) + -> GpgGenKeyResult; + +// Error Info Printer + +/** + * @brief + * + * @param err + * @return GpgError + */ +auto GPGFRONTEND_CORE_EXPORT CheckGpgError(GpgError err) -> GpgError; + +/** + * @brief + * + * @param gpgmeError + * @param comment + * @return GpgError + */ +auto GPGFRONTEND_CORE_EXPORT CheckGpgError(GpgError gpgmeError, + const std::string& comment) + -> GpgError; + +/** + * @brief + * + * @param err + * @param predict + * @return gpg_err_code_t + */ +auto GPGFRONTEND_CORE_EXPORT CheckGpgError2ErrCode( + gpgme_error_t err, gpgme_error_t predict = GPG_ERR_NO_ERROR) + -> gpg_err_code_t; + +// Check + +/** + * @brief + * + * @param text + * @return int + */ +auto TextIsSigned(BypeArrayRef text) -> int; + +} // namespace GpgFrontend
\ No newline at end of file diff --git a/src/core/function/FileOperator.cpp b/src/core/utils/IOUtils.cpp index c70036f5..5d0a2a54 100644 --- a/src/core/function/FileOperator.cpp +++ b/src/core/utils/IOUtils.cpp @@ -26,12 +26,15 @@ * */ -#include "FileOperator.h" +#include "IOUtils.h" #include <sstream> -bool GpgFrontend::FileOperator::ReadFile(const QString& file_name, - QByteArray& data) { +#include "GpgModel.h" + +namespace GpgFrontend { + +auto ReadFile(const QString& file_name, QByteArray& data) -> bool { QFile file(file_name); if (!file.open(QIODevice::ReadOnly)) { SPDLOG_ERROR("failed to open file: {}", file_name.toStdString()); @@ -42,8 +45,7 @@ bool GpgFrontend::FileOperator::ReadFile(const QString& file_name, return true; } -bool GpgFrontend::FileOperator::WriteFile(const QString& file_name, - const QByteArray& data) { +auto WriteFile(const QString& file_name, const QByteArray& data) -> bool { QFile file(file_name); if (!file.open(QIODevice::WriteOnly)) { SPDLOG_ERROR("failed to open file: {}", file_name.toStdString()); @@ -54,8 +56,8 @@ bool GpgFrontend::FileOperator::WriteFile(const QString& file_name, return true; } -bool GpgFrontend::FileOperator::ReadFileStd( - const std::filesystem::path& file_name, std::string& data) { +auto ReadFileStd(const std::filesystem::path& file_name, std::string& data) + -> bool { QByteArray byte_data; #ifdef WINDOWS bool res = ReadFile(QString::fromStdU16String(file_name.u16string()).toUtf8(), @@ -68,14 +70,13 @@ bool GpgFrontend::FileOperator::ReadFileStd( return res; } -bool GpgFrontend::FileOperator::WriteFileStd( - const std::filesystem::path& file_name, const std::string& data) { +auto WriteFileStd(const std::filesystem::path& file_name, + const std::string& data) -> bool { return WriteFile(QString::fromStdString(file_name.u8string()).toUtf8(), QByteArray::fromStdString(data)); } -std::string GpgFrontend::FileOperator::CalculateHash( - const std::filesystem::path& file_path) { +auto CalculateHash(const std::filesystem::path& file_path) -> std::string { // Returns empty QByteArray() on failure. QFileInfo info(QString::fromStdString(file_path.string())); std::stringstream ss; @@ -124,3 +125,15 @@ std::string GpgFrontend::FileOperator::CalculateHash( return ss.str(); } + +auto ReadAllDataInFile(const std::string& utf8_path) -> std::string { + std::string data; + ReadFileStd(utf8_path, data); + return data; +} + +auto WriteBufferToFile(const std::string& utf8_path, + const std::string& out_buffer) -> bool { + return WriteFileStd(utf8_path, out_buffer); +} +} // namespace GpgFrontend
\ No newline at end of file diff --git a/src/core/utils/IOUtils.h b/src/core/utils/IOUtils.h new file mode 100644 index 00000000..3240e88a --- /dev/null +++ b/src/core/utils/IOUtils.h @@ -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 + * + */ + +#pragma once + +namespace GpgFrontend { + +/** + * @brief read file content using std struct + * + * + * @param file_name file name + * @param data data read from file + * @return + */ +auto GPGFRONTEND_CORE_EXPORT ReadFileStd(const std::filesystem::path &file_name, + std::string &data) -> bool; + +/** + * @brief write file content using std struct + * + * @param file_name file name + * @param data data to write to file + * @return + */ +auto GPGFRONTEND_CORE_EXPORT WriteFileStd( + const std::filesystem::path &file_name, const std::string &data) -> bool; + +/** + * @brief read file content + * + * @param file_name file name + * @param data data read from file + * @return true if success + * @return false if failed + */ +auto GPGFRONTEND_CORE_EXPORT ReadFile(const QString &file_name, + QByteArray &data) -> bool; + +/** + * @brief write file content + * + * @param file_name file name + * @param data data to write to file + * @return true if success + * @return false if failed + */ +auto GPGFRONTEND_CORE_EXPORT WriteFile(const QString &file_name, + const QByteArray &data) -> bool; + +/** + * calculate the hash of a file + * @param file_path + * @return + */ +auto GPGFRONTEND_CORE_EXPORT +CalculateHash(const std::filesystem::path &file_path) -> std::string; + +/** + * @brief + * + * @param path + * @return std::string + */ +auto GPGFRONTEND_CORE_EXPORT ReadAllDataInFile(const std::string &path) + -> std::string; + +/** + * @brief + * + * @param path + * @param out_buffer + * @return true + * @return false + */ +auto GPGFRONTEND_CORE_EXPORT WriteBufferToFile(const std::string &path, + const std::string &out_buffer) + -> bool; +} // namespace GpgFrontend diff --git a/src/init.cpp b/src/init.cpp index b6ae32b5..8cbdd034 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -31,6 +31,7 @@ #include <spdlog/sinks/rotating_file_sink.h> #include <spdlog/sinks/stdout_color_sinks.h> +#include <boost/date_time.hpp> #include <filesystem> #include <string> diff --git a/src/main.cpp b/src/main.cpp index e1a220b6..53b80944 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -164,7 +164,8 @@ int main(int argc, char* argv[]) { SPDLOG_DEBUG("restart loop refresh, event loop code: {}, restart count: {}", return_from_event_loop_code, restart_count); - } while (return_from_event_loop_code == kRestartCode && restart_count < 3); + } while (return_from_event_loop_code == GpgFrontend::kRestartCode && + restart_count < 3); // shutdown the logging system for core GpgFrontend::Module::ShutdownGpgFrontendModules(); @@ -179,7 +180,7 @@ int main(int argc, char* argv[]) { SPDLOG_INFO("GpgFrontend about to exit."); // deep restart mode - if (return_from_event_loop_code == kRestartCode || + if (return_from_event_loop_code == GpgFrontend::kRestartCode || return_from_event_loop_code == CRASH_CODE) { // log for debug SPDLOG_DEBUG( diff --git a/src/module/integrated/version_checking_module/GpgTOFUInfo.h b/src/module/integrated/version_checking_module/GpgTOFUInfo.h new file mode 100644 index 00000000..9deec33f --- /dev/null +++ b/src/module/integrated/version_checking_module/GpgTOFUInfo.h @@ -0,0 +1,142 @@ +/** + * 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 { +/** + * @brief + * + */ +class GPGFRONTEND_CORE_EXPORT GpgTOFUInfo { + public: + /** + * @brief + * + * @return unsigned + */ + [[nodiscard]] unsigned GetValidity() const; + /** + * @brief + * + * @return unsigned + */ + [[nodiscard]] unsigned GetPolicy() const; + + /** + * @brief + * + * @return unsigned long + */ + [[nodiscard]] unsigned long GetSignCount() const; + + /** + * @brief + * + * @return unsigned long + */ + [[nodiscard]] unsigned long GetEncrCount() const; + + /** + * @brief + * + * @return unsigned long + */ + [[nodiscard]] unsigned long GetSignFirst() const; + + /** + * @brief + * + * @return unsigned long + */ + [[nodiscard]] unsigned long GetSignLast() const; + + /** + * @brief + * + * @return unsigned long + */ + [[nodiscard]] unsigned long GetEncrLast() const; + + /** + * @brief + * + * @return std::string + */ + [[nodiscard]] std::string GetDescription() const; + + /** + * @brief Construct a new Gpg T O F U Info object + * + */ + GpgTOFUInfo(); + + /** + * @brief Construct a new Gpg T O F U Info object + * + * @param tofu_info + */ + explicit GpgTOFUInfo(gpgme_tofu_info_t tofu_info); + + /** + * @brief Construct a new Gpg T O F U Info object + * + * @param o + */ + GpgTOFUInfo(GpgTOFUInfo&& o) noexcept; + + /** + * @brief Construct a new Gpg T O F U Info object + * + */ + GpgTOFUInfo(const GpgTOFUInfo&) = delete; + + /** + * @brief + * + * @param o + * @return GpgTOFUInfo& + */ + GpgTOFUInfo& operator=(GpgTOFUInfo&& o) noexcept; + + /** + * @brief + * + * @return GpgTOFUInfo& + */ + GpgTOFUInfo& operator=(const GpgTOFUInfo&) = delete; + + private: + using SubkeyRefHandler = + std::unique_ptr<struct _gpgme_tofu_info, + std::function<void(gpgme_tofu_info_t)>>; ///< + + SubkeyRefHandler _tofu_info_ref = nullptr; ///< +}; + +} // namespace GpgFrontend diff --git a/src/module/integrated/version_checking_module/SoftwareVersion.cpp b/src/module/integrated/version_checking_module/SoftwareVersion.cpp index 48bf7039..117212cb 100644 --- a/src/module/integrated/version_checking_module/SoftwareVersion.cpp +++ b/src/module/integrated/version_checking_module/SoftwareVersion.cpp @@ -28,7 +28,7 @@ #include "SoftwareVersion.h" -#include "core/GpgConstants.h" +#include "core/utils/CommonUtils.h" namespace GpgFrontend::Module::Integrated::VersionCheckingModule { diff --git a/src/signal.cpp b/src/signal.cpp index 5588cc85..f6aff819 100644 --- a/src/signal.cpp +++ b/src/signal.cpp @@ -27,6 +27,7 @@ */ #include <csetjmp> +#include <iostream> #include "GpgFrontend.h" @@ -52,8 +53,8 @@ void handle_signal(int sig) { _repeat_handle_num = 1, last_sig = sig; if (_repeat_handle_num > 3) { - std::cout << "The same signal appears three times," - << "execute the termination operation." << sig; + std::cout << "The same signal appears three times," + << "execute the termination operation." << sig; exit(-1); } diff --git a/src/ui/GpgFrontendUI.h b/src/ui/GpgFrontendUI.h index fbc59a00..f67da43e 100644 --- a/src/ui/GpgFrontendUI.h +++ b/src/ui/GpgFrontendUI.h @@ -37,5 +37,5 @@ * Project internal dependencies */ #include "GpgFrontend.h" -#include "core/GpgModel.h" +#include "core/GpgFrontendCore.h" #include "ui/GpgFrontendUIExport.h" diff --git a/src/ui/UserInterfaceUtils.cpp b/src/ui/UserInterfaceUtils.cpp index cba1b754..d2ff0e7b 100644 --- a/src/ui/UserInterfaceUtils.cpp +++ b/src/ui/UserInterfaceUtils.cpp @@ -37,13 +37,13 @@ #include "core/common/CoreCommonUtil.h" #include "core/function/CacheManager.h" #include "core/function/CoreSignalStation.h" -#include "core/function/FileOperator.h" #include "core/function/GlobalSettingStation.h" #include "core/function/gpg/GpgKeyGetter.h" #include "core/module/ModuleManager.h" #include "core/thread/Task.h" #include "core/thread/TaskRunner.h" #include "core/thread/TaskRunnerGetter.h" +#include "core/utils/IOUtils.h" #include "ui/SignalStation.h" #include "ui/dialog/WaitingDialog.h" #include "ui/dialog/gnupg/GnuPGControllerDialog.h" @@ -236,7 +236,7 @@ void CommonUtils::SlotImportKeyFromFile(QWidget *parent) { " (*.gpg);;All Files (*)"); if (!file_name.isNull()) { QByteArray key_buffer; - if (!FileOperator::ReadFile(file_name, key_buffer)) { + if (!ReadFile(file_name, key_buffer)) { QMessageBox::critical(nullptr, _("File Open Failed"), _("Failed to open file: ") + file_name); return; diff --git a/src/ui/dialog/SignersPicker.h b/src/ui/dialog/SignersPicker.h index 034bf4ab..6768b816 100644 --- a/src/ui/dialog/SignersPicker.h +++ b/src/ui/dialog/SignersPicker.h @@ -29,6 +29,7 @@ #pragma once #include "GpgFrontendUI.h" +#include "core/GpgModel.h" #include "ui/dialog//GeneralDialog.h" namespace GpgFrontend::UI { @@ -55,7 +56,7 @@ class SignersPicker : public GeneralDialog { * * @return GpgFrontend::KeyIdArgsListPtr */ - GpgFrontend::KeyIdArgsListPtr GetCheckedSigners(); + KeyIdArgsListPtr GetCheckedSigners(); /** * diff --git a/src/ui/dialog/details/VerifyDetailsDialog.h b/src/ui/dialog/details/VerifyDetailsDialog.h index d1514981..44c2e387 100644 --- a/src/ui/dialog/details/VerifyDetailsDialog.h +++ b/src/ui/dialog/details/VerifyDetailsDialog.h @@ -28,6 +28,7 @@ #pragma once +#include "core/function/result_analyse/GpgResultAnalyse.h" #include "ui/GpgFrontendUI.h" #include "ui/widgets/PlainTextEditorPage.h" #include "ui/widgets/VerifyKeyDetailBox.h" diff --git a/src/ui/dialog/import_export/ExportKeyPackageDialog.h b/src/ui/dialog/import_export/ExportKeyPackageDialog.h index 0b99f530..72f5b09e 100644 --- a/src/ui/dialog/import_export/ExportKeyPackageDialog.h +++ b/src/ui/dialog/import_export/ExportKeyPackageDialog.h @@ -29,6 +29,7 @@ #pragma once #include "GpgFrontendUI.h" +#include "core/GpgModel.h" #include "ui/dialog/GeneralDialog.h" class Ui_exportKeyPackageDialog; diff --git a/src/ui/dialog/import_export/KeyUploadDialog.h b/src/ui/dialog/import_export/KeyUploadDialog.h index 18bc5255..52c9b205 100644 --- a/src/ui/dialog/import_export/KeyUploadDialog.h +++ b/src/ui/dialog/import_export/KeyUploadDialog.h @@ -28,6 +28,7 @@ #pragma once +#include "core/GpgModel.h" #include "core/function/gpg/GpgContext.h" #include "ui/GpgFrontendUI.h" #include "ui/dialog/GeneralDialog.h" diff --git a/src/ui/dialog/key_generate/SubkeyGenerateDialog.cpp b/src/ui/dialog/key_generate/SubkeyGenerateDialog.cpp index c28877bb..d3f3510d 100644 --- a/src/ui/dialog/key_generate/SubkeyGenerateDialog.cpp +++ b/src/ui/dialog/key_generate/SubkeyGenerateDialog.cpp @@ -35,6 +35,7 @@ #include "core/function/GlobalSettingStation.h" #include "core/function/gpg/GpgKeyGetter.h" #include "core/function/gpg/GpgKeyOpera.h" +#include "core/utils/GpgUtils.h" #include "ui/SignalStation.h" #include "ui/dialog/WaitingDialog.h" diff --git a/src/ui/dialog/key_generate/SubkeyGenerateDialog.h b/src/ui/dialog/key_generate/SubkeyGenerateDialog.h index 5a7c4ad4..e0a2ed2f 100644 --- a/src/ui/dialog/key_generate/SubkeyGenerateDialog.h +++ b/src/ui/dialog/key_generate/SubkeyGenerateDialog.h @@ -28,6 +28,7 @@ #pragma once +#include "core/GpgModel.h" #include "core/function/gpg/GpgContext.h" #include "core/model/GpgGenKeyInfo.h" #include "ui/GpgFrontendUI.h" diff --git a/src/ui/dialog/keypair_details/KeyDetailsDialog.h b/src/ui/dialog/keypair_details/KeyDetailsDialog.h index 6e3fc4a7..bb5cc3dc 100644 --- a/src/ui/dialog/keypair_details/KeyDetailsDialog.h +++ b/src/ui/dialog/keypair_details/KeyDetailsDialog.h @@ -28,6 +28,7 @@ #pragma once +#include "core/GpgModel.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 eadaa2e8..62fabeb8 100644 --- a/src/ui/dialog/keypair_details/KeyNewUIDDialog.h +++ b/src/ui/dialog/keypair_details/KeyNewUIDDialog.h @@ -28,6 +28,7 @@ #pragma once +#include "core/GpgModel.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.cpp b/src/ui/dialog/keypair_details/KeyPairDetailTab.cpp index 9dbb18c4..b3002b3d 100644 --- a/src/ui/dialog/keypair_details/KeyPairDetailTab.cpp +++ b/src/ui/dialog/keypair_details/KeyPairDetailTab.cpp @@ -31,8 +31,9 @@ #include "core/function/gpg/GpgKeyGetter.h" #include "core/function/gpg/GpgKeyImportExporter.h" #include "core/model/GpgKey.h" -#include "dialog/WaitingDialog.h" +#include "core/utils/CommonUtils.h" #include "ui/SignalStation.h" +#include "ui/dialog/WaitingDialog.h" namespace GpgFrontend::UI { KeyPairDetailTab::KeyPairDetailTab(const std::string& key_id, QWidget* parent) diff --git a/src/ui/dialog/keypair_details/KeyPairOperaTab.cpp b/src/ui/dialog/keypair_details/KeyPairOperaTab.cpp index 094b1849..5dbfd216 100644 --- a/src/ui/dialog/keypair_details/KeyPairOperaTab.cpp +++ b/src/ui/dialog/keypair_details/KeyPairOperaTab.cpp @@ -33,6 +33,8 @@ #include "core/function/gpg/GpgKeyImportExporter.h" #include "core/function/gpg/GpgKeyManager.h" #include "core/function/gpg/GpgKeyOpera.h" +#include "core/utils/GpgUtils.h" +#include "core/utils/IOUtils.h" #include "ui/SignalStation.h" #include "ui/UserInterfaceUtils.h" #include "ui/dialog/import_export/KeyUploadDialog.h" diff --git a/src/ui/dialog/keypair_details/KeyPairSubkeyTab.cpp b/src/ui/dialog/keypair_details/KeyPairSubkeyTab.cpp index d453c069..aa2a2ad6 100644 --- a/src/ui/dialog/keypair_details/KeyPairSubkeyTab.cpp +++ b/src/ui/dialog/keypair_details/KeyPairSubkeyTab.cpp @@ -29,6 +29,7 @@ #include "KeyPairSubkeyTab.h" #include "core/function/gpg/GpgKeyGetter.h" +#include "core/utils/CommonUtils.h" #include "ui/SignalStation.h" namespace GpgFrontend::UI { diff --git a/src/ui/dialog/keypair_details/KeySetExpireDateDialog.cpp b/src/ui/dialog/keypair_details/KeySetExpireDateDialog.cpp index 2ffc64d4..de653534 100644 --- a/src/ui/dialog/keypair_details/KeySetExpireDateDialog.cpp +++ b/src/ui/dialog/keypair_details/KeySetExpireDateDialog.cpp @@ -33,6 +33,7 @@ #include "core/function/GlobalSettingStation.h" #include "core/function/gpg/GpgKeyGetter.h" #include "core/function/gpg/GpgKeyOpera.h" +#include "core/utils/GpgUtils.h" #include "ui/SignalStation.h" #include "ui_ModifiedExpirationDateTime.h" diff --git a/src/ui/dialog/keypair_details/KeySetExpireDateDialog.h b/src/ui/dialog/keypair_details/KeySetExpireDateDialog.h index ff7c4169..7bce70bd 100644 --- a/src/ui/dialog/keypair_details/KeySetExpireDateDialog.h +++ b/src/ui/dialog/keypair_details/KeySetExpireDateDialog.h @@ -28,9 +28,8 @@ #pragma once +#include "core/GpgModel.h" #include "core/function/gpg/GpgContext.h" -#include "core/model/GpgKey.h" -#include "core/model/GpgSubKey.h" #include "ui/GpgFrontendUI.h" #include "ui/dialog/GeneralDialog.h" diff --git a/src/ui/main_window/KeyMgmt.cpp b/src/ui/main_window/KeyMgmt.cpp index 1926512a..cbe1251d 100644 --- a/src/ui/main_window/KeyMgmt.cpp +++ b/src/ui/main_window/KeyMgmt.cpp @@ -35,6 +35,7 @@ #include "core/function/gpg/GpgKeyGetter.h" #include "core/function/gpg/GpgKeyImportExporter.h" #include "core/function/gpg/GpgKeyOpera.h" +#include "core/utils/IOUtils.h" #include "ui/SignalStation.h" #include "ui/UserInterfaceUtils.h" #include "ui/dialog/import_export/ExportKeyPackageDialog.h" diff --git a/src/ui/main_window/MainWindowSlotFunction.cpp b/src/ui/main_window/MainWindowSlotFunction.cpp index ac7ba156..dbcfb01e 100644 --- a/src/ui/main_window/MainWindowSlotFunction.cpp +++ b/src/ui/main_window/MainWindowSlotFunction.cpp @@ -42,6 +42,8 @@ #include "core/function/result_analyse/GpgVerifyResultAnalyse.h" #include "core/model/DataObject.h" #include "core/module/ModuleManager.h" +#include "core/utils/CommonUtils.h" +#include "core/utils/GpgUtils.h" #include "ui/UserInterfaceUtils.h" #include "ui/dialog/SignersPicker.h" #include "ui/dialog/help/AboutDialog.h" @@ -260,13 +262,6 @@ void MainWindow::slot_decrypt() { QByteArray text = edit_->CurTextPage()->GetTextPage()->toPlainText().toUtf8(); - if (text.trimmed().startsWith(GpgConstants::GPG_FRONTEND_SHORT_CRYPTO_HEAD)) { - QMessageBox::critical( - this, _("Notice"), - _("Short Crypto Text only supports Decrypt & Verify.")); - return; - } - // data to transfer into task auto data_object = TransferParams( edit_->CurTextPage()->GetTextPage()->toPlainText().toStdString()); diff --git a/src/ui/main_window/MainWindowSlotUI.cpp b/src/ui/main_window/MainWindowSlotUI.cpp index 038a8aab..6c9705ac 100644 --- a/src/ui/main_window/MainWindowSlotUI.cpp +++ b/src/ui/main_window/MainWindowSlotUI.cpp @@ -153,8 +153,8 @@ void MainWindow::slot_add_pgp_header() { QString content = edit_->CurTextPage()->GetTextPage()->toPlainText().trimmed(); - content.prepend("\n\n").prepend(GpgConstants::PGP_CRYPT_BEGIN); - content.append("\n").append(GpgConstants::PGP_CRYPT_END); + content.prepend("\n\n").prepend(PGP_CRYPT_BEGIN); + content.append("\n").append(PGP_CRYPT_END); edit_->SlotFillTextEditWithText(content); } @@ -165,8 +165,8 @@ void MainWindow::slot_cut_pgp_header() { } QString content = edit_->CurTextPage()->GetTextPage()->toPlainText(); - int start = content.indexOf(GpgConstants::PGP_CRYPT_BEGIN); - int end = content.indexOf(GpgConstants::PGP_CRYPT_END); + int start = content.indexOf(PGP_CRYPT_BEGIN); + int end = content.indexOf(PGP_CRYPT_END); if (start < 0 || end < 0) { return; @@ -177,8 +177,8 @@ void MainWindow::slot_cut_pgp_header() { content.remove(start, headEnd - start); // remove tail - end = content.indexOf(GpgConstants::PGP_CRYPT_END); - content.remove(end, QString(GpgConstants::PGP_CRYPT_END).size()); + end = content.indexOf(PGP_CRYPT_END); + content.remove(end, QString(PGP_CRYPT_END).size()); edit_->SlotFillTextEditWithText(content.trimmed()); } diff --git a/src/ui/widgets/FilePage.cpp b/src/ui/widgets/FilePage.cpp index a8c3ba84..c4ada5f7 100644 --- a/src/ui/widgets/FilePage.cpp +++ b/src/ui/widgets/FilePage.cpp @@ -417,7 +417,7 @@ void FilePage::slot_delete_item() { } void FilePage::slot_calculate_hash() { - auto info_str = FileOperator::CalculateHash(selected_path_); + auto info_str = CalculateHash(selected_path_); emit SignalRefreshInfoBoard(info_str.c_str(), InfoBoardStatus::INFO_ERROR_OK); } diff --git a/src/ui/widgets/PlainTextEditorPage.cpp b/src/ui/widgets/PlainTextEditorPage.cpp index 1ac12d0d..9cd76886 100644 --- a/src/ui/widgets/PlainTextEditorPage.cpp +++ b/src/ui/widgets/PlainTextEditorPage.cpp @@ -132,10 +132,9 @@ void PlainTextEditorPage::slot_format_gpg_header() { QString content = ui_->textPage->toPlainText(); // Get positions of the gpg-headers, if they exist - int start = content.indexOf(GpgFrontend::GpgConstants::PGP_SIGNED_BEGIN); - int startSig = - content.indexOf(GpgFrontend::GpgConstants::PGP_SIGNATURE_BEGIN); - int endSig = content.indexOf(GpgFrontend::GpgConstants::PGP_SIGNATURE_END); + int start = content.indexOf(GpgFrontend::PGP_SIGNED_BEGIN); + int startSig = content.indexOf(GpgFrontend::PGP_SIGNATURE_BEGIN); + int endSig = content.indexOf(GpgFrontend::PGP_SIGNATURE_END); if (start < 0 || startSig < 0 || endSig < 0 || sign_marked_) { return; diff --git a/src/ui/widgets/VerifyKeyDetailBox.cpp b/src/ui/widgets/VerifyKeyDetailBox.cpp index 3be3905e..4eff6584 100644 --- a/src/ui/widgets/VerifyKeyDetailBox.cpp +++ b/src/ui/widgets/VerifyKeyDetailBox.cpp @@ -30,6 +30,7 @@ #include "core/function/GlobalSettingStation.h" #include "core/function/gpg/GpgKeyGetter.h" +#include "core/utils/CommonUtils.h" namespace GpgFrontend::UI { |