diff options
author | Saturneric <[email protected]> | 2022-01-04 16:03:18 +0000 |
---|---|---|
committer | Saturneric <[email protected]> | 2022-01-04 16:03:18 +0000 |
commit | c2f05ec557ec53f62872e19cc463257481421e4f (patch) | |
tree | 9fd060663bca9f34cf3126e2a4c7cd4728159e9b | |
parent | <fix, refactor>(core, ui): fixed known bugs for v2.0.4-beta.1. (diff) | |
download | GpgFrontend-c2f05ec557ec53f62872e19cc463257481421e4f.tar.gz GpgFrontend-c2f05ec557ec53f62872e19cc463257481421e4f.zip |
<feature, fix>(core, ui): support file symmetric encryption.
1. improve ui for file & text symmetric encryption.
-rw-r--r-- | src/gpg/function/GpgFileOpera.cpp | 16 | ||||
-rw-r--r-- | src/gpg/function/GpgFileOpera.h | 4 | ||||
-rw-r--r-- | src/ui/main_window/MainWindowFileSlotFunction.cpp | 92 | ||||
-rw-r--r-- | src/ui/main_window/MainWindowSlotFunction.cpp | 10 |
4 files changed, 80 insertions, 42 deletions
diff --git a/src/gpg/function/GpgFileOpera.cpp b/src/gpg/function/GpgFileOpera.cpp index b5c907a2..8babfa6d 100644 --- a/src/gpg/function/GpgFileOpera.cpp +++ b/src/gpg/function/GpgFileOpera.cpp @@ -134,3 +134,19 @@ gpg_error_t GpgFrontend::GpgFileOpera::DecryptVerifyFile( return err; } +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; + + auto err = BasicOperator::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)) { + throw std::runtime_error("write_buffer_to_file error"); + }; + + return err; +} diff --git a/src/gpg/function/GpgFileOpera.h b/src/gpg/function/GpgFileOpera.h index 6447e43b..f4508f42 100644 --- a/src/gpg/function/GpgFileOpera.h +++ b/src/gpg/function/GpgFileOpera.h @@ -42,6 +42,10 @@ class GpgFileOpera : public SingletonFunctionObject<GpgFileOpera> { GpgEncrResult& result, int _channel = GPGFRONTEND_DEFAULT_CHANNEL); + static unsigned int EncryptFileSymmetric( + const std::string& in_path, const std::string& out_path, + GpgEncrResult& result, int _channel = GPGFRONTEND_DEFAULT_CHANNEL); + static GpgError DecryptFile(const std::string& in_path, const std::string& out_path, GpgDecrResult& result); diff --git a/src/ui/main_window/MainWindowFileSlotFunction.cpp b/src/ui/main_window/MainWindowFileSlotFunction.cpp index dc31462a..ee7c690b 100644 --- a/src/ui/main_window/MainWindowFileSlotFunction.cpp +++ b/src/ui/main_window/MainWindowFileSlotFunction.cpp @@ -60,28 +60,9 @@ void MainWindow::slotFileEncrypt() { // check selected keys auto key_ids = mKeyList->getChecked(); - auto p_keys = GpgKeyGetter::GetInstance().GetKeys(key_ids); - - if (p_keys->empty()) { - QMessageBox::critical( - this, _("No Key Selected"), - _("Please select the key in the key toolbox on the right.")); - return; - } - - // check key abilities - for (const auto& key : *p_keys) { - bool key_can_encrypt = key.CanEncrActual(); - - if (!key_can_encrypt) { - QMessageBox::critical( - nullptr, _("Invalid KeyPair"), - QString(_("The selected keypair cannot be used for encryption.")) + - "<br/><br/>" + _("For example the Following Key:") + " <br/>" + - QString::fromStdString(key.uids()->front().uid())); - return; - } - } + GpgEncrResult result = nullptr; + GpgError error; + bool if_error = false; // Detect ascii mode auto& settings = GlobalSettingStation::GetInstance().GetUISettings(); @@ -100,27 +81,64 @@ void MainWindow::slotFileEncrypt() { } auto out_path = path + _extension; + if (QFile::exists(out_path)) { - auto ret = QMessageBox::warning( - this, _("Warning"), - _("The target file already exists, do you need to overwrite it?"), - QMessageBox::Ok | QMessageBox::Cancel); + boost::filesystem::path _out_path = out_path.toStdString(); + auto out_file_name = boost::format(_("The target file(%1%) already exists, " + "do you need to overwrite it?")) % + _out_path.filename(); + auto ret = + QMessageBox::warning(this, _("Warning"), out_file_name.str().c_str(), + QMessageBox::Ok | QMessageBox::Cancel); if (ret == QMessageBox::Cancel) return; } - GpgEncrResult result = nullptr; - GpgError error; - bool if_error = false; - process_operation(this, _("Encrypting"), [&]() { - try { - error = - GpgFileOpera::EncryptFile(std::move(p_keys), path.toStdString(), - out_path.toStdString(), result, _channel); - } catch (const std::runtime_error& e) { - if_error = true; + if (key_ids->empty()) { + // Symmetric Encrypt + auto ret = QMessageBox::information( + this, _("Symmetric Encryption"), + _("No Key Selected. Do you want to encrypt with a " + "symmetric cipher using a passphrase?"), + QMessageBox::Ok | QMessageBox::Cancel); + + if (ret == QMessageBox::Cancel) return; + + process_operation(this, _("Symmetrically Encrypting"), [&]() { + try { + error = GpgFrontend::GpgFileOpera::EncryptFileSymmetric( + path.toStdString(), out_path.toStdString(), result, _channel); + } catch (const std::runtime_error& e) { + if_error = true; + } + }); + } else { + auto p_keys = GpgKeyGetter::GetInstance().GetKeys(key_ids); + + // check key abilities + for (const auto& key : *p_keys) { + bool key_can_encrypt = key.CanEncrActual(); + + if (!key_can_encrypt) { + QMessageBox::critical( + nullptr, _("Invalid KeyPair"), + QString(_("The selected keypair cannot be used for encryption.")) + + "<br/><br/>" + _("For example the Following Key:") + " <br/>" + + QString::fromStdString(key.uids()->front().uid())); + return; + } } - }); + + process_operation(this, _("Encrypting"), [&]() { + try { + error = + GpgFileOpera::EncryptFile(std::move(p_keys), path.toStdString(), + out_path.toStdString(), result, _channel); + } catch (const std::runtime_error& e) { + if_error = true; + } + }); + } if (!if_error) { auto resultAnalyse = EncryptResultAnalyse(error, std::move(result)); diff --git a/src/ui/main_window/MainWindowSlotFunction.cpp b/src/ui/main_window/MainWindowSlotFunction.cpp index d5aa262a..57fb5bae 100644 --- a/src/ui/main_window/MainWindowSlotFunction.cpp +++ b/src/ui/main_window/MainWindowSlotFunction.cpp @@ -59,11 +59,11 @@ void MainWindow::slotEncrypt() { if (key_ids->empty()) { // Symmetric Encrypt - auto ret = - QMessageBox::warning(this, _("Warning"), - _("No Key Selected. Do you want to encrypt with a " - "symmetric cipher using a passphrase?"), - QMessageBox::Ok | QMessageBox::Cancel); + auto ret = QMessageBox::information( + this, _("Symmetric Encryption"), + _("No Key Selected. Do you want to encrypt with a " + "symmetric cipher using a passphrase?"), + QMessageBox::Ok | QMessageBox::Cancel); if (ret == QMessageBox::Cancel) return; |