diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/core/CMakeLists.txt | 5 | ||||
-rw-r--r-- | src/core/GpgFrontendCore.h | 46 | ||||
-rw-r--r-- | src/core/file/FileOperator.cpp | 67 | ||||
-rw-r--r-- | src/core/file/FileOperator.h | 85 | ||||
-rw-r--r-- | src/core/function/GpgFileOpera.cpp | 62 | ||||
-rw-r--r-- | src/ui/UserInterfaceUtils.cpp | 78 | ||||
-rw-r--r-- | src/ui/settings/GlobalSettingStation.cpp | 96 |
7 files changed, 349 insertions, 90 deletions
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 373927d1..27a4f81c 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -28,6 +28,7 @@ aux_source_directory(./result_analyse GPG_SOURCE) aux_source_directory(./function GPG_SOURCE) aux_source_directory(./model GPG_SOURCE) aux_source_directory(./aes GPG_SOURCE) +aux_source_directory(./file GPG_SOURCE) aux_source_directory(. GPG_SOURCE) add_library(gpgfrontend_core STATIC ${GPG_SOURCE}) @@ -52,7 +53,9 @@ target_link_libraries(gpgfrontend_core target_link_libraries(gpgfrontend_core Qt5::Core) # set up pch -target_precompile_headers(gpgfrontend_core PUBLIC ${CMAKE_SOURCE_DIR}/src/GpgFrontend.h) +target_precompile_headers(gpgfrontend_core + PUBLIC ${CMAKE_SOURCE_DIR}/src/GpgFrontend.h + PUBLIC GpgFrontendCore.h) if (MINGW) message(STATUS "Link GPG Static Library For MINGW") diff --git a/src/core/GpgFrontendCore.h b/src/core/GpgFrontendCore.h new file mode 100644 index 00000000..696f97bc --- /dev/null +++ b/src/core/GpgFrontendCore.h @@ -0,0 +1,46 @@ +/** +* Copyright (C) 2021 Saturneric +* +* This file is part of GpgFrontend. +* +* GpgFrontend is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GpgFrontend is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GpgFrontend. If not, see <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 +* + */ + +#ifndef GPGFRONTEND_GPGFRONTENDCORE_H +#define GPGFRONTEND_GPGFRONTENDCORE_H + +#include "GpgFrontend.h" + +// std includes +#include <filesystem> + +// boost includes +#include <boost/format.hpp> + +// Qt includes +#include <QtCore> + +#include "core/GpgModel.h" + + +#endif // GPGFRONTEND_GPGFRONTENDCORE_H diff --git a/src/core/file/FileOperator.cpp b/src/core/file/FileOperator.cpp new file mode 100644 index 00000000..e721fc7e --- /dev/null +++ b/src/core/file/FileOperator.cpp @@ -0,0 +1,67 @@ +/** +* Copyright (C) 2021 Saturneric +* +* This file is part of GpgFrontend. +* +* GpgFrontend is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GpgFrontend is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GpgFrontend. If not, see <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 "FileOperator.h" + +bool GpgFrontend::FileOperator::ReadFile(const QString& file_name, + QByteArray& data) { + QFile file(file_name); + if (!file.open(QIODevice::ReadOnly)) { + LOG(ERROR) << "failed to open file" << file_name.toStdString(); + return false; + } + data = file.readAll(); + file.close(); + return true; +} + +bool GpgFrontend::FileOperator::WriteFile(const QString& file_name, + const QByteArray& data) { + QFile file(file_name); + if (!file.open(QIODevice::WriteOnly)) { + LOG(ERROR) << "failed to open file" << file_name.toStdString(); + return false; + } + file.write(data); + file.close(); + return true; +} + +bool GpgFrontend::FileOperator::ReadFileStd( + const std::filesystem::path& file_name, std::string& data) { + QByteArray byte_data; + bool res = ReadFile(QString::fromStdString(file_name.string()), byte_data); + data = byte_data.toStdString(); + return res; +} + +bool GpgFrontend::FileOperator::WriteFileStd( + const std::filesystem::path& file_name, const std::string& data) { + return WriteFile(QString::fromStdString(file_name.string()), + QByteArray::fromStdString(data)); +} diff --git a/src/core/file/FileOperator.h b/src/core/file/FileOperator.h new file mode 100644 index 00000000..de195a96 --- /dev/null +++ b/src/core/file/FileOperator.h @@ -0,0 +1,85 @@ +/** + * Copyright (C) 2021 Saturneric + * + * This file is part of GpgFrontend. + * + * GpgFrontend is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GpgFrontend is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GpgFrontend. If not, see <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 + * + */ + +#ifndef GPGFRONTEND_FILEOPERATOR_H +#define GPGFRONTEND_FILEOPERATOR_H + +#include "core/GpgFrontendCore.h" + +namespace GpgFrontend { + +/** + * @brief provides file operations + * + */ +class 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); +}; +} // namespace GpgFrontend + +#endif // GPGFRONTEND_FILEOPERATOR_H diff --git a/src/core/function/GpgFileOpera.cpp b/src/core/function/GpgFileOpera.cpp index c9c4fc0e..15c359da 100644 --- a/src/core/function/GpgFileOpera.cpp +++ b/src/core/function/GpgFileOpera.cpp @@ -32,18 +32,23 @@ #include "GpgConstants.h" #include "core/function/GpgBasicOperator.h" +#include "core/file/FileOperator.h" GpgFrontend::GpgError GpgFrontend::GpgFileOpera::EncryptFile( KeyListPtr keys, const std::string& in_path, const std::string& out_path, GpgEncrResult& result, int _channel) { - std::string in_buffer = read_all_data_in_file(in_path); - std::unique_ptr<std::string> out_buffer; + + std::string in_buffer; + if(!FileOperator::ReadFileStd(in_path, in_buffer)) { + throw std::runtime_error("read file error"); + } + std::unique_ptr<std::string> out_buffer = nullptr; auto err = GpgBasicOperator::GetInstance(_channel).Encrypt( std::move(keys), in_buffer, out_buffer, result); if (check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR) - if (!write_buffer_to_file(out_path, *out_buffer)) { + if (!FileOperator::WriteFileStd(out_path, *out_buffer)) { throw std::runtime_error("write_buffer_to_file error"); }; @@ -53,7 +58,10 @@ GpgFrontend::GpgError GpgFrontend::GpgFileOpera::EncryptFile( GpgFrontend::GpgError GpgFrontend::GpgFileOpera::DecryptFile( const std::string& in_path, const std::string& out_path, GpgDecrResult& result) { - std::string in_buffer = read_all_data_in_file(in_path); + std::string in_buffer; + if(!FileOperator::ReadFileStd(in_path, in_buffer)) { + throw std::runtime_error("read file error"); + } std::unique_ptr<std::string> out_buffer; auto err = @@ -62,7 +70,7 @@ GpgFrontend::GpgError GpgFrontend::GpgFileOpera::DecryptFile( assert(check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR); if (check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR) - if (!write_buffer_to_file(out_path, *out_buffer)) { + if (!FileOperator::WriteFileStd(out_path, *out_buffer)) { throw std::runtime_error("write_buffer_to_file error"); }; @@ -74,14 +82,17 @@ gpgme_error_t GpgFrontend::GpgFileOpera::SignFile(KeyListPtr keys, const std::string& out_path, GpgSignResult& result, int _channel) { - auto in_buffer = read_all_data_in_file(in_path); + std::string in_buffer; + if(!FileOperator::ReadFileStd(in_path, in_buffer)) { + throw std::runtime_error("read file error"); + } std::unique_ptr<std::string> out_buffer; auto err = GpgBasicOperator::GetInstance(_channel).Sign( std::move(keys), in_buffer, out_buffer, GPGME_SIG_MODE_DETACH, result); if (check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR) - if (!write_buffer_to_file(out_path, *out_buffer)) { + if (!FileOperator::WriteFileStd(out_path, *out_buffer)) { throw std::runtime_error("write_buffer_to_file error"); }; @@ -91,11 +102,18 @@ gpgme_error_t GpgFrontend::GpgFileOpera::SignFile(KeyListPtr keys, gpgme_error_t GpgFrontend::GpgFileOpera::VerifyFile( const std::string& data_path, const std::string& sign_path, GpgVerifyResult& result, int _channel) { - auto in_buffer = read_all_data_in_file(data_path); + std::string in_buffer; + if(!FileOperator::ReadFileStd(data_path, in_buffer)) { + throw std::runtime_error("read file error"); + } std::unique_ptr<std::string> sign_buffer = nullptr; if (!sign_path.empty()) { + std::string sign_buffer_str; + if (!FileOperator::ReadFileStd(sign_path, sign_buffer_str)) { + throw std::runtime_error("read file error"); + } sign_buffer = - std::make_unique<std::string>(read_all_data_in_file(sign_path)); + std::make_unique<std::string>(sign_buffer_str); } auto err = GpgBasicOperator::GetInstance(_channel).Verify(in_buffer, sign_buffer, result); @@ -106,7 +124,10 @@ gpg_error_t GpgFrontend::GpgFileOpera::EncryptSignFile( KeyListPtr keys, KeyListPtr signer_keys, const std::string& in_path, const std::string& out_path, GpgEncrResult& encr_res, GpgSignResult& sign_res, int _channel) { - auto in_buffer = read_all_data_in_file(in_path); + std::string in_buffer; + if(!FileOperator::ReadFileStd(in_path, in_buffer)) { + throw std::runtime_error("read file error"); + } std::unique_ptr<std::string> out_buffer = nullptr; auto err = GpgBasicOperator::GetInstance(_channel).EncryptSign( @@ -114,7 +135,7 @@ gpg_error_t GpgFrontend::GpgFileOpera::EncryptSignFile( sign_res); if (check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR) - if (!write_buffer_to_file(out_path, *out_buffer)) { + if (!FileOperator::WriteFileStd(out_path, *out_buffer)) { throw std::runtime_error("write_buffer_to_file error"); }; @@ -124,16 +145,18 @@ gpg_error_t GpgFrontend::GpgFileOpera::EncryptSignFile( gpg_error_t GpgFrontend::GpgFileOpera::DecryptVerifyFile( const std::string& in_path, const std::string& out_path, GpgDecrResult& decr_res, GpgVerifyResult& verify_res) { - auto in_buffer = read_all_data_in_file(in_path); + std::string in_buffer; + if(!FileOperator::ReadFileStd(in_path, in_buffer)) { + throw std::runtime_error("read file error"); + } std::unique_ptr<std::string> out_buffer = nullptr; - auto err = GpgBasicOperator::GetInstance().DecryptVerify(in_buffer, out_buffer, decr_res, verify_res); if (check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR) - if (!write_buffer_to_file(out_path, *out_buffer)) { - throw std::runtime_error("write_buffer_to_file error"); + if (!FileOperator::WriteFileStd(out_path, *out_buffer)) { + throw std::runtime_error("write file error"); }; return err; @@ -141,14 +164,17 @@ gpg_error_t GpgFrontend::GpgFileOpera::DecryptVerifyFile( unsigned int GpgFrontend::GpgFileOpera::EncryptFileSymmetric( const std::string& in_path, const std::string& out_path, GpgFrontend::GpgEncrResult& result, int _channel) { - std::string in_buffer = read_all_data_in_file(in_path); - std::unique_ptr<std::string> out_buffer; + std::string in_buffer; + if(!FileOperator::ReadFileStd(in_path, in_buffer)) { + throw std::runtime_error("read file error"); + } + std::unique_ptr<std::string> out_buffer; auto err = GpgBasicOperator::GetInstance(_channel).EncryptSymmetric( in_buffer, out_buffer, result); if (check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR) - if (!write_buffer_to_file(out_path, *out_buffer)) { + if (!FileOperator::WriteFileStd(out_path, *out_buffer)) { throw std::runtime_error("write_buffer_to_file error"); }; diff --git a/src/ui/UserInterfaceUtils.cpp b/src/ui/UserInterfaceUtils.cpp index 2f085c21..ab81242d 100644 --- a/src/ui/UserInterfaceUtils.cpp +++ b/src/ui/UserInterfaceUtils.cpp @@ -30,9 +30,10 @@ #include <utility> -#include "dialog/WaitingDialog.h" +#include "core/file/FileOperator.h" #include "core/result_analyse/GpgResultAnalyse.h" #include "ui/SignalStation.h" +#include "ui/dialog/WaitingDialog.h" #include "ui/mail/SendMailDialog.h" #include "ui/settings/GlobalSettingStation.h" #include "ui/widgets/InfoBoardWidget.h" @@ -44,8 +45,8 @@ std::unique_ptr<GpgFrontend::UI::CommonUtils> GpgFrontend::UI::CommonUtils::instance_ = nullptr; #ifdef SMTP_SUPPORT -void send_an_email(QWidget* parent, InfoBoardWidget* info_board, - const QString& text, bool attach_signature) { +void send_an_email(QWidget *parent, InfoBoardWidget *info_board, + const QString &text, bool attach_signature) { info_board->AddOptionalAction(_("Send Encrypted Mail"), [=]() { bool smtp_enabled = false; try { @@ -68,8 +69,8 @@ void send_an_email(QWidget* parent, InfoBoardWidget* info_board, } #endif -void show_verify_details(QWidget* parent, InfoBoardWidget* info_board, - GpgError error, const GpgVerifyResult& verify_result) { +void show_verify_details(QWidget *parent, InfoBoardWidget *info_board, + GpgError error, const GpgVerifyResult &verify_result) { // take out result info_board->ResetOptionActionsMenu(); info_board->AddOptionalAction("Show Verify Details", [=]() { @@ -77,8 +78,8 @@ void show_verify_details(QWidget* parent, InfoBoardWidget* info_board, }); } -void import_unknown_key_from_keyserver(QWidget* parent, - const GpgVerifyResultAnalyse& verify_res) { +void import_unknown_key_from_keyserver( + QWidget *parent, const GpgVerifyResultAnalyse &verify_res) { QMessageBox::StandardButton reply; reply = QMessageBox::question( parent, _("Public key not found locally"), @@ -89,7 +90,7 @@ void import_unknown_key_from_keyserver(QWidget* parent, if (reply == QMessageBox::Yes) { auto dialog = KeyServerImportDialog(true, parent); auto key_ids = std::make_unique<KeyIdArgsList>(); - auto* signature = verify_res.GetSignatures(); + auto *signature = verify_res.GetSignatures(); while (signature != nullptr) { LOG(INFO) << "signature fpr" << signature->fpr; key_ids->push_back(signature->fpr); @@ -100,8 +101,8 @@ void import_unknown_key_from_keyserver(QWidget* parent, } } -void refresh_info_board(InfoBoardWidget* info_board, int status, - const std::string& report_text) { +void refresh_info_board(InfoBoardWidget *info_board, int status, + const std::string &report_text) { if (status < 0) info_board->SlotRefresh(QString::fromStdString(report_text), INFO_ERROR_CRITICAL); @@ -112,16 +113,16 @@ void refresh_info_board(InfoBoardWidget* info_board, int status, INFO_ERROR_WARN); } -void process_result_analyse(TextEdit* edit, InfoBoardWidget* info_board, - const GpgResultAnalyse& result_analyse) { +void process_result_analyse(TextEdit *edit, InfoBoardWidget *info_board, + const GpgResultAnalyse &result_analyse) { info_board->AssociateTabWidget(edit->tab_widget_); refresh_info_board(info_board, result_analyse.GetStatus(), result_analyse.GetResultReport()); } -void process_result_analyse(TextEdit* edit, InfoBoardWidget* info_board, - const GpgResultAnalyse& result_analyse_a, - const GpgResultAnalyse& result_analyse_b) { +void process_result_analyse(TextEdit *edit, InfoBoardWidget *info_board, + const GpgResultAnalyse &result_analyse_a, + const GpgResultAnalyse &result_analyse_b) { LOG(INFO) << "process_result_analyse Started"; info_board->AssociateTabWidget(edit->tab_widget_); @@ -132,14 +133,14 @@ void process_result_analyse(TextEdit* edit, InfoBoardWidget* info_board, result_analyse_a.GetResultReport() + result_analyse_b.GetResultReport()); } -void process_operation(QWidget* parent, const std::string& waiting_title, - const std::function<void()>& func) { +void process_operation(QWidget *parent, const std::string &waiting_title, + const std::function<void()> &func) { auto thread = QThread::create(func); QApplication::connect(thread, &QThread::finished, thread, &QThread::deleteLater); thread->start(); - auto* dialog = + auto *dialog = new WaitingDialog(QString::fromStdString(waiting_title), parent); while (thread->isRunning()) { QApplication::processEvents(); @@ -147,7 +148,7 @@ void process_operation(QWidget* parent, const std::string& waiting_title, dialog->close(); } -CommonUtils* CommonUtils::GetInstance() { +CommonUtils *CommonUtils::GetInstance() { if (instance_ == nullptr) { instance_ = std::make_unique<CommonUtils>(); } @@ -155,7 +156,8 @@ CommonUtils* CommonUtils::GetInstance() { } CommonUtils::CommonUtils() : QWidget(nullptr) { - connect(this, &CommonUtils::SignalKeyStatusUpdated, SignalStation::GetInstance(), + connect(this, &CommonUtils::SignalKeyStatusUpdated, + SignalStation::GetInstance(), &SignalStation::SignalKeyDatabaseRefresh); connect(this, &CommonUtils::SignalGnupgNotInstall, this, []() { QMessageBox::critical( @@ -168,42 +170,48 @@ CommonUtils::CommonUtils() : QWidget(nullptr) { }); } -void CommonUtils::SlotImportKeys(QWidget* parent, - const std::string& in_buffer) { +void CommonUtils::SlotImportKeys(QWidget *parent, + const std::string &in_buffer) { GpgImportInformation result = GpgKeyImportExporter::GetInstance().ImportKey( std::make_unique<ByteArray>(in_buffer)); emit SignalKeyStatusUpdated(); new KeyImportDetailDialog(result, false, parent); } -void CommonUtils::SlotImportKeyFromFile(QWidget* parent) { +void CommonUtils::SlotImportKeyFromFile(QWidget *parent) { QString file_name = QFileDialog::getOpenFileName( this, _("Open Key"), QString(), QString(_("Key Files")) + " (*.asc *.txt);;" + _("Keyring files") + " (*.gpg);;All Files (*)"); if (!file_name.isNull()) { - SlotImportKeys(parent, read_all_data_in_file(file_name.toStdString())); + QByteArray key_buffer; + if (!FileOperator::ReadFile(file_name, key_buffer)) { + QMessageBox::critical(nullptr, _("File Open Failed"), + _("Failed to open file: ") + file_name); + return; + } + SlotImportKeys(parent, key_buffer.toStdString()); } } -void CommonUtils::SlotImportKeyFromKeyServer(QWidget* parent) { +void CommonUtils::SlotImportKeyFromKeyServer(QWidget *parent) { auto dialog = new KeyServerImportDialog(false, parent); dialog->show(); } -void CommonUtils::SlotImportKeyFromClipboard(QWidget* parent) { - QClipboard* cb = QApplication::clipboard(); +void CommonUtils::SlotImportKeyFromClipboard(QWidget *parent) { + QClipboard *cb = QApplication::clipboard(); SlotImportKeys(parent, cb->text(QClipboard::Clipboard).toUtf8().toStdString()); } void CommonUtils::SlotExecuteGpgCommand( - const QStringList& arguments, - const std::function<void(QProcess*)>& interact_func) { + const QStringList &arguments, + const std::function<void(QProcess *)> &interact_func) { QEventLoop looper; auto dialog = new WaitingDialog(_("Processing"), nullptr); dialog->show(); - auto* gpg_process = new QProcess(&looper); + auto *gpg_process = new QProcess(&looper); gpg_process->setProcessChannelMode(QProcess::MergedChannels); connect(gpg_process, @@ -244,12 +252,12 @@ void CommonUtils::SlotExecuteGpgCommand( } void CommonUtils::SlotImportKeyFromKeyServer( - int ctx_channel, const KeyIdArgsList& key_ids, - const ImportCallbackFunctiopn& callback) { + int ctx_channel, const KeyIdArgsList &key_ids, + const ImportCallbackFunctiopn &callback) { std::string target_keyserver; if (target_keyserver.empty()) { try { - auto& settings = GlobalSettingStation::GetInstance().GetUISettings(); + auto &settings = GlobalSettingStation::GetInstance().GetUISettings(); target_keyserver = settings.lookup("keyserver.default_server").c_str(); @@ -272,7 +280,7 @@ void CommonUtils::SlotImportKeyFromKeyServer( auto network_manager = std::make_unique<QNetworkAccessManager>(); // LOOP decltype(key_ids.size()) current_index = 1, all_index = key_ids.size(); - for (const auto& key_id : key_ids) { + for (const auto &key_id : key_ids) { // New Req Url QUrl req_url(target_keyserver_url.scheme() + "://" + target_keyserver_url.host() + @@ -282,7 +290,7 @@ void CommonUtils::SlotImportKeyFromKeyServer( LOG(INFO) << "request url" << req_url.toString().toStdString(); // Waiting for reply - QNetworkReply* reply = network_manager->get(QNetworkRequest(req_url)); + QNetworkReply *reply = network_manager->get(QNetworkRequest(req_url)); QEventLoop loop; connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit); loop.exec(); diff --git a/src/ui/settings/GlobalSettingStation.cpp b/src/ui/settings/GlobalSettingStation.cpp index 54e7daaf..5401536f 100644 --- a/src/ui/settings/GlobalSettingStation.cpp +++ b/src/ui/settings/GlobalSettingStation.cpp @@ -34,10 +34,12 @@ #include <vmime/security/cert/openssl/X509Certificate_OpenSSL.hpp> #include <vmime/vmime.hpp> +#include "core/file/FileOperator.h" + std::unique_ptr<GpgFrontend::UI::GlobalSettingStation> GpgFrontend::UI::GlobalSettingStation::instance_ = nullptr; -GpgFrontend::UI::GlobalSettingStation& +GpgFrontend::UI::GlobalSettingStation & GpgFrontend::UI::GlobalSettingStation::GetInstance() { if (instance_ == nullptr) { instance_ = std::make_unique<GlobalSettingStation>(); @@ -52,7 +54,7 @@ void GpgFrontend::UI::GlobalSettingStation::SyncSettings() noexcept { LOG(INFO) << _("Updated ui configuration successfully written to") << ui_config_path_; - } catch (const FileIOException& fioex) { + } catch (const FileIOException &fioex) { LOG(ERROR) << _("I/O error while writing ui configuration file") << ui_config_path_; } @@ -70,26 +72,35 @@ GpgFrontend::UI::GlobalSettingStation::GlobalSettingStation() noexcept { LOG(INFO) << _("App Log Path") << app_log_path_; LOG(INFO) << _("App Locale Path") << app_locale_path_; - if (!is_directory(app_configure_path_)) create_directory(app_configure_path_); + if (!is_directory(app_configure_path_)) + create_directory(app_configure_path_); - if (!is_directory(app_data_path_)) create_directory(app_data_path_); + if (!is_directory(app_data_path_)) + create_directory(app_data_path_); - if (!is_directory(app_log_path_)) create_directory(app_log_path_); + if (!is_directory(app_log_path_)) + create_directory(app_log_path_); - if (!is_directory(ui_config_dir_path_)) create_directory(ui_config_dir_path_); + if (!is_directory(ui_config_dir_path_)) + create_directory(ui_config_dir_path_); - if (!is_directory(app_secure_path_)) create_directory(app_secure_path_); + if (!is_directory(app_secure_path_)) + create_directory(app_secure_path_); if (!exists(app_secure_key_path_)) { init_app_secure_key(); } - const auto key = - GpgFrontend::read_all_data_in_file(app_secure_key_path_.string()); + std::string key; + if (!FileOperator::ReadFileStd(app_secure_key_path_.string(), key)) { + LOG(ERROR) << _("Failed to read app secure key file") + << app_secure_key_path_; + } hash_key_ = QCryptographicHash::hash(QByteArray::fromStdString(key), QCryptographicHash::Sha256); - if (!exists(app_data_objs_path_)) create_directory(app_data_objs_path_); + if (!exists(app_data_objs_path_)) + create_directory(app_data_objs_path_); if (!exists(ui_config_path_)) { try { @@ -97,7 +108,7 @@ GpgFrontend::UI::GlobalSettingStation::GlobalSettingStation() noexcept { LOG(INFO) << _("UserInterface configuration successfully written to") << ui_config_path_; - } catch (const FileIOException& fioex) { + } catch (const FileIOException &fioex) { LOG(ERROR) << _("I/O error while writing UserInterface configuration file") << ui_config_path_; @@ -107,9 +118,9 @@ GpgFrontend::UI::GlobalSettingStation::GlobalSettingStation() noexcept { this->ui_cfg_.readFile(ui_config_path_.string().c_str()); LOG(INFO) << _("UserInterface configuration successfully read from") << ui_config_path_; - } catch (const FileIOException& fioex) { + } catch (const FileIOException &fioex) { LOG(ERROR) << _("I/O error while reading UserInterface configure file"); - } catch (const ParseException& pex) { + } catch (const ParseException &pex) { LOG(ERROR) << _("Parse error at ") << pex.getFile() << ":" << pex.getLine() << " - " << pex.getError(); } @@ -117,18 +128,24 @@ GpgFrontend::UI::GlobalSettingStation::GlobalSettingStation() noexcept { } void GpgFrontend::UI::GlobalSettingStation::AddRootCert( - const boost::filesystem::path& path) { - auto out_buffer = GpgFrontend::read_all_data_in_file(path.string()); + const boost::filesystem::path &path) { + + std::string out_buffer; + if (!FileOperator::ReadFileStd(path.string(), out_buffer)) { + LOG(ERROR) << _("Failed to read root certificate file") << path; + return; + } auto mem_bio = std::shared_ptr<BIO>( BIO_new_mem_buf(out_buffer.data(), static_cast<int>(out_buffer.size())), - [](BIO* _p) { BIO_free(_p); }); + [](BIO *_p) { BIO_free(_p); }); auto x509 = std::shared_ptr<X509>( PEM_read_bio_X509(mem_bio.get(), nullptr, nullptr, nullptr), - [](X509* _p) { X509_free(_p); }); + [](X509 *_p) { X509_free(_p); }); - if (!x509) return; + if (!x509) + return; root_certs_.push_back(x509); } @@ -140,7 +157,7 @@ GpgFrontend::UI::GlobalSettingStation::GetCertVerifier() const { std::vector<vmime::shared_ptr<vmime::security::cert::X509Certificate>> _root_certs; - for (const auto& cert : root_certs_) { + for (const auto &cert : root_certs_) { _root_certs.push_back( std::make_shared<vmime::security::cert::X509Certificate_OpenSSL>( cert.get())); @@ -148,18 +165,17 @@ GpgFrontend::UI::GlobalSettingStation::GetCertVerifier() const { return p_cv; } -const std::vector<std::shared_ptr<X509>>& +const std::vector<std::shared_ptr<X509>> & GpgFrontend::UI::GlobalSettingStation::GetRootCerts() { return root_certs_; } -std::string GpgFrontend::UI::GlobalSettingStation::generate_passphrase( - int len) { +std::string +GpgFrontend::UI::GlobalSettingStation::generate_passphrase(int len) { std::uniform_int_distribution<int> dist(999, 99999); - static const char alphanum[] = - "0123456789" - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz"; + static const char alphanum[] = "0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz"; std::string tmp_str; tmp_str.reserve(len); @@ -173,13 +189,13 @@ std::string GpgFrontend::UI::GlobalSettingStation::generate_passphrase( void GpgFrontend::UI::GlobalSettingStation::init_app_secure_key() { GpgFrontend::write_buffer_to_file(app_secure_key_path_.string(), generate_passphrase(256)); - boost::filesystem::permissions( - app_secure_key_path_, - boost::filesystem::owner_read | boost::filesystem::owner_write); + boost::filesystem::permissions(app_secure_key_path_, + boost::filesystem::owner_read | + boost::filesystem::owner_write); } std::string GpgFrontend::UI::GlobalSettingStation::SaveDataObj( - const std::string& _key, const nlohmann::json& value) { + const std::string &_key, const nlohmann::json &value) { std::string _hash_obj_key = {}; if (_key.empty()) { _hash_obj_key = @@ -212,7 +228,7 @@ std::string GpgFrontend::UI::GlobalSettingStation::SaveDataObj( } std::optional<nlohmann::json> -GpgFrontend::UI::GlobalSettingStation::GetDataObject(const std::string& _key) { +GpgFrontend::UI::GlobalSettingStation::GetDataObject(const std::string &_key) { try { auto _hash_obj_key = QCryptographicHash::hash(hash_key_ + QByteArray::fromStdString(_key), @@ -226,7 +242,11 @@ GpgFrontend::UI::GlobalSettingStation::GetDataObject(const std::string& _key) { return {}; } - auto buffer = GpgFrontend::read_all_data_in_file(obj_path.string()); + std::string buffer; + if (!FileOperator::ReadFileStd(obj_path.string(), buffer)) { + return {}; + } + auto encoded = QByteArray::fromStdString(buffer); QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::ECB, @@ -242,16 +262,20 @@ GpgFrontend::UI::GlobalSettingStation::GetDataObject(const std::string& _key) { } std::optional<nlohmann::json> GpgFrontend::UI::GlobalSettingStation::GetDataObjectByRef( - const std::string& _ref) { - if (_ref.size() != 64) return {}; + const std::string &_ref) { + if (_ref.size() != 64) + return {}; try { auto _hash_obj_key = _ref; const auto obj_path = app_data_objs_path_ / _hash_obj_key; - if (!boost::filesystem::exists(obj_path)) return {}; + if (!boost::filesystem::exists(obj_path)) + return {}; - auto buffer = GpgFrontend::read_all_data_in_file(obj_path.string()); + std::string buffer; + if (!FileOperator::ReadFileStd(obj_path.string(), buffer)) + return {}; auto encoded = QByteArray::fromStdString(buffer); QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::ECB, |