/** * 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 . * * 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 starting on May 12, 2021. * * SPDX-License-Identifier: GPL-3.0-or-later * */ #include #include #include "MainWindow.h" #include "core/GpgConstants.h" #include "core/GpgModel.h" #include "core/function/gpg/GpgBasicOperator.h" #include "core/function/gpg/GpgKeyGetter.h" #include "core/function/gpg/GpgKeyImportExporter.h" #include "core/function/gpg/GpgKeyManager.h" #include "core/function/result_analyse/GpgDecryptResultAnalyse.h" #include "core/function/result_analyse/GpgEncryptResultAnalyse.h" #include "core/function/result_analyse/GpgSignResultAnalyse.h" #include "core/function/result_analyse/GpgVerifyResultAnalyse.h" #include "core/model/DataObject.h" #include "core/model/GpgEncryptResult.h" #include "core/module/ModuleManager.h" #include "core/typedef/GpgTypedef.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" #include "ui/dialog/import_export/KeyUploadDialog.h" #include "ui/dialog/keypair_details/KeyDetailsDialog.h" #include "ui/function/SetOwnerTrustLevel.h" #include "ui/widgets/FindWidget.h" namespace GpgFrontend::UI { /** * Encrypt Entry(Text & File) */ void MainWindow::slot_encrypt() { if (edit_->TabCount() == 0 || edit_->SlotCurPageTextEdit() == nullptr) { if (edit_->SlotCurPageFileTreeView() != nullptr) this->SlotFileEncrypt(); return; } auto key_ids = m_key_list_->GetChecked(); if (key_ids->empty()) { // Symmetric Encrypt auto ret = QMessageBox::information( this, _("Symmetric Encryption"), _("No Key Checked. Do you want to encrypt with a " "symmetric cipher using a passphrase?"), QMessageBox::Ok | QMessageBox::Cancel); if (ret == QMessageBox::Cancel) return; auto buffer = GFBuffer(edit_->CurTextPage()->GetTextPage()->toPlainText()); CommonUtils::WaitForOpera( this, _("Symmetrically Encrypting"), [this, buffer](const OperaWaitingHd& op_hd) { GpgFrontend::GpgBasicOperator::GetInstance().EncryptSymmetric( buffer, true, [this, op_hd](GpgError err, const DataObjectPtr& data_obj) { // stop waiting op_hd(); if (data_obj == nullptr || !data_obj->Check()) { throw std::runtime_error("data object doesn't pass checking"); } auto result = ExtractParams(data_obj, 0); auto buffer = ExtractParams(data_obj, 1); auto result_analyse = GpgEncryptResultAnalyse(err, result); result_analyse.Analyse(); process_result_analyse(edit_, info_board_, result_analyse); if (CheckGpgError(err) == GPG_ERR_NO_ERROR) { edit_->SlotFillTextEditWithText(buffer.ConvertToQByteArray()); } info_board_->ResetOptionActionsMenu(); }); }); return; } auto keys = GpgKeyGetter::GetInstance().GetKeys(key_ids); for (const auto& key : *keys) { if (!key.IsHasActualEncryptionCapability()) { QMessageBox::information( this, _("Invalid Operation"), QString( _("The selected key contains a key that does not actually have a " "encrypt usage.")) + "

" + _("For example the Following Key:") + "
" + QString::fromStdString(key.GetUIDs()->front().GetUID())); return; } } auto buffer = GFBuffer(edit_->CurTextPage()->GetTextPage()->toPlainText()); CommonUtils::WaitForOpera( this, _("Encrypting"), [this, keys, buffer](const OperaWaitingHd& op_hd) { GpgFrontend::GpgBasicOperator::GetInstance().Encrypt( {keys->begin(), keys->end()}, buffer, true, [this, op_hd](GpgError err, const DataObjectPtr& data_obj) { // stop waiting op_hd(); if (data_obj == nullptr || !data_obj->Check()) { throw std::runtime_error("data object doesn't pass checking"); } auto result = ExtractParams(data_obj, 0); auto buffer = ExtractParams(data_obj, 1); auto result_analyse = GpgEncryptResultAnalyse(err, result); result_analyse.Analyse(); process_result_analyse(edit_, info_board_, result_analyse); if (CheckGpgError(err) == GPG_ERR_NO_ERROR) { edit_->SlotFillTextEditWithText(buffer.ConvertToQByteArray()); } info_board_->ResetOptionActionsMenu(); }); }); } void MainWindow::slot_sign() { if (edit_->TabCount() == 0 || edit_->SlotCurPageTextEdit() == nullptr) { if (edit_->SlotCurPageFileTreeView() != nullptr) this->SlotFileSign(); return; } auto key_ids = m_key_list_->GetPrivateChecked(); if (key_ids->empty()) { QMessageBox::critical( this, _("No Key Checked"), _("Please check the key in the key toolbox on the right.")); return; } auto keys = GpgKeyGetter::GetInstance().GetKeys(key_ids); for (const auto& key : *keys) { if (!key.IsHasActualSigningCapability()) { QMessageBox::information( this, _("Invalid Operation"), QString( _("The selected key contains a key that does not actually have a " "signature usage.")) + "

" + _("For example the Following Key:") + "
" + key.GetUIDs()->front().GetUID().c_str()); return; } } // set input buffer auto buffer = GFBuffer(edit_->CurTextPage()->GetTextPage()->toPlainText()); CommonUtils::WaitForOpera( this, _("Signing"), [this, keys, buffer](const OperaWaitingHd& hd) { GpgFrontend::GpgBasicOperator::GetInstance().Sign( {keys->begin(), keys->end()}, buffer, GPGME_SIG_MODE_CLEAR, true, [this, hd](GpgError err, const DataObjectPtr& data_obj) { // stop waiting hd(); if (data_obj == nullptr || !data_obj->Check()) { throw std::runtime_error("data object doesn't pass checking"); } auto sign_result = ExtractParams(data_obj, 0); auto sign_out_buffer = ExtractParams(data_obj, 1); auto result_analyse = GpgSignResultAnalyse(err, sign_result); result_analyse.Analyse(); process_result_analyse(edit_, info_board_, result_analyse); if (CheckGpgError(err) == GPG_ERR_NO_ERROR) { edit_->SlotFillTextEditWithText( sign_out_buffer.ConvertToQByteArray()); } }); }); } void MainWindow::slot_decrypt() { if (edit_->TabCount() == 0 || edit_->SlotCurPageTextEdit() == nullptr) { if (edit_->SlotCurPageFileTreeView() != nullptr) this->SlotFileDecrypt(); return; } // data to transfer into task auto buffer = GFBuffer(edit_->CurTextPage()->GetTextPage()->toPlainText()); CommonUtils::WaitForOpera( this, _("Decrypting"), [this, buffer](const OperaWaitingHd& hd) { GpgFrontend::GpgBasicOperator::GetInstance().Decrypt( buffer, [this, hd](GpgError err, const DataObjectPtr& data_obj) { // stop waiting hd(); if (data_obj == nullptr || !data_obj->Check()) { throw std::runtime_error("data object doesn't pass checking"); } auto decrypt_result = ExtractParams(data_obj, 0); auto out_buffer = ExtractParams(data_obj, 1); auto result_analyse = GpgDecryptResultAnalyse(err, decrypt_result); result_analyse.Analyse(); process_result_analyse(edit_, info_board_, result_analyse); if (CheckGpgError(err) == GPG_ERR_NO_ERROR) { edit_->SlotFillTextEditWithText( out_buffer.ConvertToQByteArray()); } }); }); } void MainWindow::slot_verify() { if (edit_->TabCount() == 0 || edit_->SlotCurPageTextEdit() == nullptr) { if (edit_->SlotCurPageFileTreeView() != nullptr) this->SlotFileVerify(); return; } // set input buffer auto buffer = GFBuffer(edit_->CurTextPage()->GetTextPage()->toPlainText()); CommonUtils::WaitForOpera( this, _("Verifying"), [this, buffer](const OperaWaitingHd& hd) { GpgFrontend::GpgBasicOperator::GetInstance().Verify( buffer, GFBuffer(), [this, hd](GpgError err, const DataObjectPtr& data_obj) { // stop waiting hd(); if (data_obj == nullptr || !data_obj->Check()) { throw std::runtime_error("data object doesn't pass checking"); } auto verify_result = ExtractParams(data_obj, 0); // analyse result auto result_analyse = GpgVerifyResultAnalyse(err, verify_result); result_analyse.Analyse(); process_result_analyse(edit_, info_board_, result_analyse); }); }); } void MainWindow::slot_encrypt_sign() { if (edit_->TabCount() == 0 || edit_->SlotCurPageTextEdit() == nullptr) { if (edit_->SlotCurPageFileTreeView() != nullptr) this->SlotFileEncryptSign(); return; } auto key_ids = m_key_list_->GetChecked(); if (key_ids->empty()) { QMessageBox::critical( this, _("No Key Checked"), _("Please check some key in the key toolbox on the right.")); return; } auto keys = GpgKeyGetter::GetInstance().GetKeys(key_ids); for (const auto& key : *keys) { bool key_can_encrypt = key.IsHasActualEncryptionCapability(); if (!key_can_encrypt) { QMessageBox::critical( this, _("Invalid KeyPair"), QString(_("The selected keypair cannot be used for encryption.")) + "

" + _("For example the Following Key:") + "
" + QString::fromStdString(key.GetUIDs()->front().GetUID())); return; } } auto* signers_picker = new SignersPicker(this); QEventLoop loop; connect(signers_picker, &SignersPicker::finished, &loop, &QEventLoop::quit); loop.exec(); // return when canceled if (!signers_picker->GetStatus()) return; auto signer_key_ids = signers_picker->GetCheckedSigners(); auto signer_keys = GpgKeyGetter::GetInstance().GetKeys(signer_key_ids); for (const auto& key : *keys) { SPDLOG_DEBUG("keys {}", key.GetEmail()); } for (const auto& signer : *signer_keys) { SPDLOG_DEBUG("signers {}", signer.GetEmail()); } // data to transfer into task auto buffer = GFBuffer(edit_->CurTextPage()->GetTextPage()->toPlainText()); CommonUtils::WaitForOpera( this, _("Encrypting and Signing"), [this, keys, signer_keys, buffer](const OperaWaitingHd& hd) { GpgFrontend::GpgBasicOperator::GetInstance().EncryptSign( {keys->begin(), keys->end()}, {signer_keys->begin(), signer_keys->end()}, buffer, true, [this, hd](GpgError err, const DataObjectPtr& data_obj) { // stop waiting hd(); if (data_obj == nullptr || !data_obj ->Check()) { throw std::runtime_error("data object doesn't pass checking"); } auto encrypt_result = ExtractParams(data_obj, 0); auto sign_result = ExtractParams(data_obj, 1); auto out_buffer = ExtractParams(data_obj, 2); // analyse result auto encrypt_result_analyse = GpgEncryptResultAnalyse(err, encrypt_result); encrypt_result_analyse.Analyse(); auto sign_result_analyse = GpgSignResultAnalyse(err, sign_result); sign_result_analyse.Analyse(); // show analyse result process_result_analyse(edit_, info_board_, encrypt_result_analyse, sign_result_analyse); if (CheckGpgError(err) == GPG_ERR_NO_ERROR) { edit_->SlotFillTextEditWithText( out_buffer.ConvertToQByteArray()); } }); }); } void MainWindow::slot_decrypt_verify() { if (edit_->TabCount() == 0 || edit_->SlotCurPageTextEdit() == nullptr) { if (edit_->SlotCurPageFileTreeView() != nullptr) this->SlotFileDecryptVerify(); return; } // data to transfer into task auto buffer = GFBuffer(edit_->CurTextPage()->GetTextPage()->toPlainText()); CommonUtils::WaitForOpera( this, _("Decrypting and Verifying"), [this, buffer](const OperaWaitingHd& hd) { GpgFrontend::GpgBasicOperator::GetInstance().DecryptVerify( buffer, [this, hd](GpgError err, const DataObjectPtr& data_obj) { // stop waiting hd(); if (data_obj == nullptr || !data_obj ->Check()) { throw std::runtime_error("data object doesn't pass checking"); } auto decrypt_result = ExtractParams(data_obj, 0); auto verify_result = ExtractParams(data_obj, 1); auto out_buffer = ExtractParams(data_obj, 2); // analyse result auto decrypt_result_analyse = GpgDecryptResultAnalyse(err, decrypt_result); decrypt_result_analyse.Analyse(); auto verify_result_analyse = GpgVerifyResultAnalyse(err, verify_result); verify_result_analyse.Analyse(); // show analyse result process_result_analyse(edit_, info_board_, decrypt_result_analyse, verify_result_analyse); if (CheckGpgError(err) == GPG_ERR_NO_ERROR) { edit_->SlotFillTextEditWithText( out_buffer.ConvertToQByteArray()); } }); }); } void MainWindow::slot_find() { if (edit_->TabCount() == 0 || edit_->CurTextPage() == nullptr) { return; } // At first close verifynotification, if existing edit_->SlotCurPageTextEdit()->CloseNoteByClass("findwidget"); auto* fw = new FindWidget(this, edit_->CurTextPage()); edit_->SlotCurPageTextEdit()->ShowNotificationWidget(fw, "findWidget"); } /* * Append the selected (not checked!) Key(s) To Textedit */ void MainWindow::slot_append_selected_keys() { if (edit_->TabCount() == 0 || edit_->SlotCurPageTextEdit() == nullptr) { return; } auto exported = GpgFrontend::SecureCreateSharedObject(); auto key_ids = m_key_list_->GetSelected(); if (key_ids->empty()) { SPDLOG_ERROR("no key is selected"); return; } if (!GpgKeyImportExporter::GetInstance().ExportKeys(key_ids, exported)) { QMessageBox::critical(this, _("Error"), _("Key Export Operation Failed.")); return; } edit_->CurTextPage()->GetTextPage()->appendPlainText( QString::fromStdString(*exported)); } void MainWindow::slot_append_keys_create_datetime() { if (edit_->TabCount() == 0 || edit_->SlotCurPageTextEdit() == nullptr) { return; } auto key_ids = m_key_list_->GetSelected(); if (key_ids->empty()) { SPDLOG_ERROR("no key is selected"); return; } auto key = GpgKeyGetter::GetInstance().GetKey(key_ids->front()); if (!key.IsGood()) { QMessageBox::critical(this, _("Error"), _("Key Not Found.")); return; } auto create_datetime_format_str = boost::posix_time::to_iso_extended_string(key.GetCreateTime()) + " (UTC) " + "\n"; edit_->CurTextPage()->GetTextPage()->appendPlainText( QString::fromStdString(create_datetime_format_str)); } void MainWindow::slot_append_keys_expire_datetime() { if (edit_->TabCount() == 0 || edit_->SlotCurPageTextEdit() == nullptr) { return; } auto key_ids = m_key_list_->GetSelected(); if (key_ids->empty()) { SPDLOG_ERROR("no key is selected"); return; } auto key = GpgKeyGetter::GetInstance().GetKey(key_ids->front()); if (!key.IsGood()) { QMessageBox::critical(this, _("Error"), _("Key Not Found.")); return; } auto create_datetime_format_str = boost::posix_time::to_iso_extended_string(key.GetCreateTime()) + " (UTC) " + "\n"; edit_->CurTextPage()->GetTextPage()->appendPlainText( QString::fromStdString(create_datetime_format_str)); } void MainWindow::slot_append_keys_fingerprint() { auto key_ids = m_key_list_->GetSelected(); if (key_ids->empty()) return; auto key = GpgKeyGetter::GetInstance().GetKey(key_ids->front()); if (!key.IsGood()) { QMessageBox::critical(this, _("Error"), _("Key Not Found.")); return; } auto fingerprint_format_str = BeautifyFingerprint(key.GetFingerprint()) + "\n"; edit_->CurTextPage()->GetTextPage()->appendPlainText( QString::fromStdString(fingerprint_format_str)); } void MainWindow::slot_copy_mail_address_to_clipboard() { auto key_ids = m_key_list_->GetSelected(); if (key_ids->empty()) return; auto key = GpgKeyGetter::GetInstance().GetKey(key_ids->front()); if (!key.IsGood()) { QMessageBox::critical(this, _("Error"), _("Key Not Found.")); return; } QClipboard* cb = QApplication::clipboard(); cb->setText(QString::fromStdString(key.GetEmail())); } void MainWindow::slot_copy_default_uid_to_clipboard() { auto key_ids = m_key_list_->GetSelected(); if (key_ids->empty()) return; auto key = GpgKeyGetter::GetInstance().GetKey(key_ids->front()); if (!key.IsGood()) { QMessageBox::critical(this, _("Error"), _("Key Not Found.")); return; } QClipboard* cb = QApplication::clipboard(); cb->setText(QString::fromStdString(key.GetUIDs()->front().GetUID())); } void MainWindow::slot_copy_key_id_to_clipboard() { auto key_ids = m_key_list_->GetSelected(); if (key_ids->empty()) return; auto key = GpgKeyGetter::GetInstance().GetKey(key_ids->front()); if (!key.IsGood()) { QMessageBox::critical(this, _("Error"), _("Key Not Found.")); return; } QClipboard* cb = QApplication::clipboard(); cb->setText(QString::fromStdString(key.GetId())); } void MainWindow::slot_show_key_details() { auto key_ids = m_key_list_->GetSelected(); if (key_ids->empty()) return; auto key = GpgKeyGetter::GetInstance().GetKey(key_ids->front()); if (key.IsGood()) { new KeyDetailsDialog(key, this); } else { QMessageBox::critical(this, _("Error"), _("Key Not Found.")); } } void MainWindow::slot_add_key_2_favourite() { auto key_ids = m_key_list_->GetSelected(); if (key_ids->empty()) return; auto key = GpgKeyGetter::GetInstance().GetKey(key_ids->front()); CommonUtils::GetInstance()->AddKey2Favourtie(key); emit SignalUIRefresh(); } void MainWindow::slot_remove_key_from_favourite() { auto key_ids = m_key_list_->GetSelected(); if (key_ids->empty()) return; auto key = GpgKeyGetter::GetInstance().GetKey(key_ids->front()); CommonUtils::GetInstance()->RemoveKeyFromFavourite(key); emit SignalUIRefresh(); } void MainWindow::refresh_keys_from_key_server() { auto key_ids = m_key_list_->GetSelected(); if (key_ids->empty()) return; auto* dialog = new KeyServerImportDialog(this); dialog->show(); dialog->SlotImport(key_ids); } void MainWindow::slot_set_owner_trust_level_of_key() { auto key_ids = m_key_list_->GetSelected(); if (key_ids->empty()) return; auto* function = new SetOwnerTrustLevel(this); function->Exec(key_ids->front()); function->deleteLater(); } void MainWindow::upload_key_to_server() { auto key_ids = m_key_list_->GetSelected(); auto* dialog = new KeyUploadDialog(key_ids, this); dialog->show(); dialog->SlotUpload(); } void MainWindow::SlotOpenFile(QString& path) { edit_->SlotOpenFile(path); } void MainWindow::slot_version_upgrade_nofity() { SPDLOG_DEBUG( "slot version upgrade notify called, checking version info from rt..."); auto is_loading_done = Module::RetrieveRTValueTypedOrDefault<>( "com.bktus.gpgfrontend.module.integrated.version-checking", "version.loading_done", false); SPDLOG_DEBUG("checking version info from rt, is loading done state: {}", is_loading_done); if (!is_loading_done) { SPDLOG_ERROR("invalid version info from rt, loading hasn't done yet"); return; } auto is_need_upgrade = Module::RetrieveRTValueTypedOrDefault<>( "com.bktus.gpgfrontend.module.integrated.version-checking", "version.need_upgrade", false); auto is_current_a_withdrawn_version = Module::RetrieveRTValueTypedOrDefault<>( "com.bktus.gpgfrontend.module.integrated.version-checking", "version.current_a_withdrawn_version", false); auto is_current_version_released = Module::RetrieveRTValueTypedOrDefault<>( "com.bktus.gpgfrontend.module.integrated.version-checking", "version.current_version_released", false); auto latest_version = Module::RetrieveRTValueTypedOrDefault<>( "com.bktus.gpgfrontend.module.integrated.version-checking", "version.latest_version", std::string{}); SPDLOG_DEBUG( "got version info from rt, need upgrade: {}, with drawn: {}, current " "version " "released: {}", is_need_upgrade, is_current_a_withdrawn_version, is_current_version_released); if (is_need_upgrade) { statusBar()->showMessage( QString(_("GpgFrontend Upgradeable (New Version: %1).")) .arg(latest_version.c_str()), 30000); auto update_button = new QPushButton("Update GpgFrontend", this); connect(update_button, &QPushButton::clicked, [=]() { auto* about_dialog = new AboutDialog(2, this); about_dialog->show(); }); statusBar()->addPermanentWidget(update_button, 0); } else if (is_current_a_withdrawn_version) { QMessageBox::warning( this, _("Withdrawn Version"), QString( _("This version(%1) may have been withdrawn by the developer due " "to serious problems. Please stop using this version " "immediately and use the latest stable version.")) .arg(latest_version.c_str()) + "
" + QString(_("You can download the latest stable version(%1) on " "Github Releases " "Page.
")) .arg(latest_version.c_str())); } else if (!is_current_version_released) { statusBar()->showMessage( QString(_("This maybe a BETA Version (Latest Stable Version: %1).")) .arg(latest_version.c_str()), 30000); } } } // namespace GpgFrontend::UI