diff options
Diffstat (limited to 'src/ui/main_window/MainWindowSlotFunction.cpp')
-rw-r--r-- | src/ui/main_window/MainWindowSlotFunction.cpp | 692 |
1 files changed, 550 insertions, 142 deletions
diff --git a/src/ui/main_window/MainWindowSlotFunction.cpp b/src/ui/main_window/MainWindowSlotFunction.cpp index 023b3d73..106c3cd1 100644 --- a/src/ui/main_window/MainWindowSlotFunction.cpp +++ b/src/ui/main_window/MainWindowSlotFunction.cpp @@ -28,7 +28,7 @@ void MainWindow::slotEncrypt() { if (edit->tabCount() == 0) return; - if(edit->slotCurPageTextEdit() != nullptr) { + if (edit->slotCurPageTextEdit() != nullptr) { QVector<GpgKey> keys; mKeyList->getCheckedKeys(keys); @@ -69,84 +69,91 @@ void MainWindow::slotEncrypt() { infoBoard->slotRefresh(reportText, INFO_ERROR_WARN); delete resultAnalyse; - } else if(edit->slotCurPageFileTreeView() != nullptr) { + } else if (edit->slotCurPageFileTreeView() != nullptr) { this->slotFileEncrypt(); } } void MainWindow::slotSign() { - if (edit->tabCount() == 0 || edit->slotCurPageTextEdit() == nullptr) { - return; - } - QVector<GpgKey> keys; + if (edit->tabCount() == 0) return; - mKeyList->getPrivateCheckedKeys(keys); + if (edit->slotCurPageTextEdit() != nullptr) { - if (keys.isEmpty()) { - QMessageBox::critical(this, tr("No Key Selected"), tr("No Key Selected")); - return; - } + QVector<GpgKey> keys; - for (const auto &key : keys) { - if (!GpgME::GpgContext::checkIfKeyCanSign(key)) { - QMessageBox::information(this, - tr("Invalid Operation"), - tr("The selected key contains a key that does not actually have a signature usage.<br/>") - + tr("<br/>For example the Following Key: <br/>") + key.uids.first().uid); + mKeyList->getPrivateCheckedKeys(keys); + + if (keys.isEmpty()) { + QMessageBox::critical(this, tr("No Key Selected"), tr("No Key Selected")); return; } - } - auto *tmp = new QByteArray(); + for (const auto &key : keys) { + if (!GpgME::GpgContext::checkIfKeyCanSign(key)) { + QMessageBox::information(this, + tr("Invalid Operation"), + tr("The selected key contains a key that does not actually have a signature usage.<br/>") + + tr("<br/>For example the Following Key: <br/>") + key.uids.first().uid); + return; + } + } - gpgme_sign_result_t result = nullptr; + auto *tmp = new QByteArray(); - auto error = mCtx->sign(keys, edit->curTextPage()->toPlainText().toUtf8(), tmp, false, &result); - infoBoard->associateTextEdit(edit->curTextPage()); - edit->slotFillTextEditWithText(QString::fromUtf8(*tmp)); + gpgme_sign_result_t result = nullptr; - auto resultAnalyse = new SignResultAnalyse(error, result); + auto error = mCtx->sign(keys, edit->curTextPage()->toPlainText().toUtf8(), tmp, false, &result); + infoBoard->associateTextEdit(edit->curTextPage()); + edit->slotFillTextEditWithText(QString::fromUtf8(*tmp)); - auto &reportText = resultAnalyse->getResultReport(); - if (resultAnalyse->getStatus() < 0) - infoBoard->slotRefresh(reportText, INFO_ERROR_CRITICAL); - else if (resultAnalyse->getStatus() > 0) - infoBoard->slotRefresh(reportText, INFO_ERROR_OK); - else - infoBoard->slotRefresh(reportText, INFO_ERROR_WARN); + auto resultAnalyse = new SignResultAnalyse(error, result); - delete resultAnalyse; + auto &reportText = resultAnalyse->getResultReport(); + if (resultAnalyse->getStatus() < 0) + infoBoard->slotRefresh(reportText, INFO_ERROR_CRITICAL); + else if (resultAnalyse->getStatus() > 0) + infoBoard->slotRefresh(reportText, INFO_ERROR_OK); + else + infoBoard->slotRefresh(reportText, INFO_ERROR_WARN); + + delete resultAnalyse; + } else if (edit->slotCurPageFileTreeView() != nullptr) { + this->slotFileSign(); + } } void MainWindow::slotDecrypt() { - if (edit->tabCount() == 0 || edit->slotCurPageTextEdit() == nullptr) { - return; - } + if (edit->tabCount() == 0) return; - auto *decrypted = new QByteArray(); - QByteArray text = edit->curTextPage()->toPlainText().toUtf8(); - GpgME::GpgContext::preventNoDataErr(&text); + if (edit->slotCurPageTextEdit() != nullptr) { - gpgme_decrypt_result_t result = nullptr; - // try decrypt, if fail do nothing, especially don't replace text - auto error = mCtx->decrypt(text, decrypted, &result); - infoBoard->associateTextEdit(edit->curTextPage()); + auto *decrypted = new QByteArray(); + QByteArray text = edit->curTextPage()->toPlainText().toUtf8(); + GpgME::GpgContext::preventNoDataErr(&text); - if(gpgme_err_code(error) == GPG_ERR_NO_ERROR) - edit->slotFillTextEditWithText(QString::fromUtf8(*decrypted)); + gpgme_decrypt_result_t result = nullptr; + // try decrypt, if fail do nothing, especially don't replace text + auto error = mCtx->decrypt(text, decrypted, &result); + infoBoard->associateTextEdit(edit->curTextPage()); - auto resultAnalyse = new DecryptResultAnalyse(mCtx, error, result); + if (gpgme_err_code(error) == GPG_ERR_NO_ERROR) + edit->slotFillTextEditWithText(QString::fromUtf8(*decrypted)); - auto &reportText = resultAnalyse->getResultReport(); - if (resultAnalyse->getStatus() < 0) - infoBoard->slotRefresh(reportText, INFO_ERROR_CRITICAL); - else if (resultAnalyse->getStatus() > 0) - infoBoard->slotRefresh(reportText, INFO_ERROR_OK); - else - infoBoard->slotRefresh(reportText, INFO_ERROR_WARN); + auto resultAnalyse = new DecryptResultAnalyse(mCtx, error, result); - delete resultAnalyse; + auto &reportText = resultAnalyse->getResultReport(); + if (resultAnalyse->getStatus() < 0) + infoBoard->slotRefresh(reportText, INFO_ERROR_CRITICAL); + else if (resultAnalyse->getStatus() > 0) + infoBoard->slotRefresh(reportText, INFO_ERROR_OK); + else + infoBoard->slotRefresh(reportText, INFO_ERROR_WARN); + + delete resultAnalyse; + } else if (edit->slotCurPageFileTreeView() != nullptr) { + this->slotFileDecrypt(); + } } void MainWindow::slotFind() { @@ -163,39 +170,136 @@ void MainWindow::slotFind() { } void MainWindow::slotVerify() { + + if (edit->tabCount() == 0) return; + + if (edit->slotCurPageTextEdit() != nullptr) { + + QByteArray text = edit->curTextPage()->toPlainText().toUtf8(); + GpgME::GpgContext::preventNoDataErr(&text); + + + gpgme_verify_result_t result; + + auto error = mCtx->verify(&text, nullptr, &result); + + auto resultAnalyse = new VerifyResultAnalyse(mCtx, error, result); + infoBoard->associateTextEdit(edit->curTextPage()); + + auto &reportText = resultAnalyse->getResultReport(); + if (resultAnalyse->getStatus() < 0) + infoBoard->slotRefresh(reportText, INFO_ERROR_CRITICAL); + else if (resultAnalyse->getStatus() > 0) + infoBoard->slotRefresh(reportText, INFO_ERROR_OK); + else + infoBoard->slotRefresh(reportText, INFO_ERROR_WARN); + + if (resultAnalyse->getStatus() >= 0) { + infoBoard->resetOptionActionsMenu(); + infoBoard->addOptionalAction("Show Verify Details", [this, error, result]() { + VerifyDetailsDialog(this, mCtx, mKeyList, error, result); + }); + } + delete resultAnalyse; + } else if (edit->slotCurPageFileTreeView() != nullptr) { + this->slotFileVerify(); + } +} + +void MainWindow::slotEncryptSign() { + + + if (edit->tabCount() == 0) return; + + if (edit->slotCurPageTextEdit() != nullptr) { + + QVector<GpgKey> keys; + mKeyList->getCheckedKeys(keys); + + if (keys.empty()) { + QMessageBox::critical(nullptr, tr("No Key Selected"), tr("No Key Selected")); + return; + } + + for (const auto &key : keys) { + if (!GpgME::GpgContext::checkIfKeyCanSign(key) || !GpgME::GpgContext::checkIfKeyCanEncr(key)) { + QMessageBox::information(nullptr, + tr("Invalid Operation"), + tr("The selected key cannot be used for signing and encryption at the same time.<br/>") + + tr("<br/>For example the Following Key: <br/>") + key.uids.first().uid); + return; + } + } + + auto *tmp = new QByteArray(); + gpgme_encrypt_result_t encr_result = nullptr; + gpgme_sign_result_t sign_result = nullptr; + + auto error = mCtx->encryptSign(keys, edit->curTextPage()->toPlainText().toUtf8(), tmp, &encr_result, + &sign_result); + auto *tmp2 = new QString(*tmp); + edit->slotFillTextEditWithText(*tmp2); + + auto resultAnalyseEncr = new EncryptResultAnalyse(error, encr_result); + auto resultAnalyseSign = new SignResultAnalyse(error, sign_result); + int status = std::min(resultAnalyseEncr->getStatus(), resultAnalyseSign->getStatus()); + auto reportText = resultAnalyseEncr->getResultReport() + resultAnalyseSign->getResultReport(); + + infoBoard->associateTextEdit(edit->curTextPage()); + + if (status < 0) + infoBoard->slotRefresh(reportText, INFO_ERROR_CRITICAL); + else if (status > 0) + infoBoard->slotRefresh(reportText, INFO_ERROR_OK); + else + infoBoard->slotRefresh(reportText, INFO_ERROR_WARN); + + delete resultAnalyseEncr; + delete resultAnalyseSign; + } else if (edit->slotCurPageFileTreeView() != nullptr) { + this->slotFileVerify(); + } +} + +void MainWindow::slotDecryptVerify() { + if (edit->tabCount() == 0 || edit->slotCurPageTextEdit() == nullptr) { return; } - // If an unknown key is found, enable the importfromkeyserveraction - + auto *decrypted = new QByteArray(); QByteArray text = edit->curTextPage()->toPlainText().toUtf8(); GpgME::GpgContext::preventNoDataErr(&text); + gpgme_decrypt_result_t d_result = nullptr; + gpgme_verify_result_t v_result = nullptr; + // try decrypt, if fail do nothing, especially don't replace text + auto error = mCtx->decryptVerify(text, decrypted, &d_result, &v_result); + infoBoard->associateTextEdit(edit->curTextPage()); - gpgme_verify_result_t result; - - auto error = mCtx->verify(&text, nullptr, &result); + if (gpgme_err_code(error) == GPG_ERR_NO_ERROR) + edit->slotFillTextEditWithText(QString::fromUtf8(*decrypted)); - auto resultAnalyse = new VerifyResultAnalyse(mCtx, error, result); - infoBoard->associateTextEdit(edit->curTextPage()); + auto resultAnalyseDecrypt = new DecryptResultAnalyse(mCtx, error, d_result); + auto resultAnalyseVerify = new VerifyResultAnalyse(mCtx, error, v_result); - auto &reportText = resultAnalyse->getResultReport(); - if (resultAnalyse->getStatus() < 0) + int status = std::min(resultAnalyseDecrypt->getStatus(), resultAnalyseVerify->getStatus()); + auto &reportText = resultAnalyseDecrypt->getResultReport() + resultAnalyseVerify->getResultReport(); + if (status < 0) infoBoard->slotRefresh(reportText, INFO_ERROR_CRITICAL); - else if (resultAnalyse->getStatus() > 0) + else if (status > 0) infoBoard->slotRefresh(reportText, INFO_ERROR_OK); else infoBoard->slotRefresh(reportText, INFO_ERROR_WARN); - if (resultAnalyse->getStatus() >= 0) { + if (resultAnalyseVerify->getStatus() >= 0) { infoBoard->resetOptionActionsMenu(); - infoBoard->addOptionalAction("Show Verify Details", [this, error, result]() { - VerifyDetailsDialog(this, mCtx, mKeyList, error, result); + infoBoard->addOptionalAction("Show Verify Details", [this, error, v_result]() { + VerifyDetailsDialog(this, mCtx, mKeyList, error, v_result); }); } - - delete resultAnalyse; + delete resultAnalyseDecrypt; + delete resultAnalyseVerify; } /* @@ -254,21 +358,27 @@ void MainWindow::slotFileEncrypt() { auto path = fileTreeView->getSelected(); QFileInfo fileInfo(path); - if(!fileInfo.isFile()) { + QFileInfo pathInfo(fileInfo.absolutePath()); + + if (!fileInfo.isFile()) { QMessageBox::critical(this, tr("Error"), tr("Select a file before doing it.")); return; } - if(!fileInfo.isReadable()) { + if (!fileInfo.isReadable()) { QMessageBox::critical(this, tr("Error"), tr("No permission to read this file.")); return; } - if(QFile::exists(path + ".asc")) { + if (!pathInfo.isWritable()) { + QMessageBox::critical(this, tr("Error"), tr("No permission to create file.")); + return; + } + if (QFile::exists(path + ".asc")) { auto ret = QMessageBox::warning(this, - tr("Warning"), - tr("The target file already exists, do you need to overwrite it?"), - QMessageBox::Ok | QMessageBox::Cancel); + tr("Warning"), + tr("The target file already exists, do you need to overwrite it?"), + QMessageBox::Ok | QMessageBox::Cancel); - if(ret == QMessageBox::Cancel) + if (ret == QMessageBox::Cancel) return; } @@ -276,7 +386,7 @@ void MainWindow::slotFileEncrypt() { mKeyList->getCheckedKeys(keys); - if(keys.empty()) { + if (keys.empty()) { QMessageBox::critical(this, tr("No Key Selected"), tr("No Key Selected")); return; } @@ -292,121 +402,419 @@ void MainWindow::slotFileEncrypt() { } } - if(!GpgFileOpera::encryptFile(mCtx, keys, path)) { + try { + gpgme_encrypt_result_t result; + auto error = GpgFileOpera::encryptFile(mCtx, keys, path, &result); + + auto resultAnalyse = new EncryptResultAnalyse(error, result); + auto &reportText = resultAnalyse->getResultReport(); + infoBoard->associateTabWidget(edit->tabWidget); + infoBoard->associateFileTreeView(edit->curFilePage()); + + if (resultAnalyse->getStatus() < 0) + infoBoard->slotRefresh(reportText, INFO_ERROR_CRITICAL); + else if (resultAnalyse->getStatus() > 0) + infoBoard->slotRefresh(reportText, INFO_ERROR_OK); + else + infoBoard->slotRefresh(reportText, INFO_ERROR_WARN); + + delete resultAnalyse; + + fileTreeView->update(); + + } catch (std::runtime_error &e) { QMessageBox::critical(this, tr("Error"), tr("An error occurred during operation.")); } - fileTreeView->update(); - } void MainWindow::slotFileDecrypt() { - QStringList *keyList; - keyList = mKeyList->getChecked(); - new FileEncryptionDialog(mCtx, *keyList, FileEncryptionDialog::Decrypt, this); + + auto fileTreeView = edit->slotCurPageFileTreeView(); + auto path = fileTreeView->getSelected(); + + QFileInfo fileInfo(path); + QFileInfo pathInfo(fileInfo.absolutePath()); + if (!fileInfo.isFile()) { + QMessageBox::critical(this, tr("Error"), tr("Select a file before doing it.")); + return; + } + if (!fileInfo.isReadable()) { + QMessageBox::critical(this, tr("Error"), tr("No permission to read this file.")); + return; + } + if (!pathInfo.isWritable()) { + QMessageBox::critical(this, tr("Error"), tr("No permission to create file.")); + return; + } + + QString outFileName, fileExtension = fileInfo.suffix(); + + if (fileExtension == "asc" || fileExtension == "gpg") { + int pos = path.lastIndexOf(QChar('.')); + outFileName = path.left(pos); + } else { + outFileName = path + ".out"; + } + + if (QFile::exists(outFileName)) { + auto ret = QMessageBox::warning(this, + tr("Warning"), + tr("The target file already exists, do you need to overwrite it?"), + QMessageBox::Ok | QMessageBox::Cancel); + + if (ret == QMessageBox::Cancel) + return; + } + + try { + gpgme_decrypt_result_t result; + auto error = GpgFileOpera::decryptFile(mCtx, path, &result); + + auto resultAnalyse = new DecryptResultAnalyse(mCtx, error, result); + auto &reportText = resultAnalyse->getResultReport(); + infoBoard->associateTabWidget(edit->tabWidget); + infoBoard->associateFileTreeView(edit->curFilePage()); + + if (resultAnalyse->getStatus() < 0) + infoBoard->slotRefresh(reportText, INFO_ERROR_CRITICAL); + else if (resultAnalyse->getStatus() > 0) + infoBoard->slotRefresh(reportText, INFO_ERROR_OK); + else + infoBoard->slotRefresh(reportText, INFO_ERROR_WARN); + + delete resultAnalyse; + + fileTreeView->update(); + } catch (std::runtime_error &e) { + QMessageBox::critical(this, tr("Error"), tr("An error occurred during operation.")); + return; + } + + } void MainWindow::slotFileSign() { - QStringList *keyList; - keyList = mKeyList->getChecked(); - new FileEncryptionDialog(mCtx, *keyList, FileEncryptionDialog::Sign, this); + + auto fileTreeView = edit->slotCurPageFileTreeView(); + auto path = fileTreeView->getSelected(); + + QFileInfo fileInfo(path); + QFileInfo pathInfo(fileInfo.absolutePath()); + + if (!fileInfo.isFile()) { + QMessageBox::critical(this, tr("Error"), tr("Select a file before doing it.")); + return; + } + if (!fileInfo.isReadable()) { + QMessageBox::critical(this, tr("Error"), tr("No permission to read this file.")); + return; + } + if (!pathInfo.isWritable()) { + QMessageBox::critical(this, tr("Error"), tr("No permission to create file.")); + return; + } + + if (QFile::exists(path + ".sig")) { + auto ret = QMessageBox::warning(this, + tr("Warning"), + tr("The target file already exists, do you need to overwrite it?"), + QMessageBox::Ok | QMessageBox::Cancel); + + if (ret == QMessageBox::Cancel) + return; + } + + QVector<GpgKey> keys; + + mKeyList->getCheckedKeys(keys); + + if (keys.empty()) { + QMessageBox::critical(this, tr("No Key Selected"), tr("No Key Selected")); + return; + } + + for (const auto &key : keys) { + if (!GpgME::GpgContext::checkIfKeyCanEncr(key)) { + QMessageBox::information(this, + tr("Invalid Operation"), + tr("The selected key contains a key that does not actually have a encrypt usage.<br/>") + + tr("<br/>For example the Following Key: <br/>") + key.uids.first().uid); + return; + + } + } + + try { + gpgme_sign_result_t result; + auto error = GpgFileOpera::signFile(mCtx, keys, path, &result); + + auto resultAnalyse = new SignResultAnalyse(error, result); + auto &reportText = resultAnalyse->getResultReport(); + infoBoard->associateTabWidget(edit->tabWidget); + infoBoard->associateFileTreeView(edit->curFilePage()); + + if (resultAnalyse->getStatus() < 0) + infoBoard->slotRefresh(reportText, INFO_ERROR_CRITICAL); + else if (resultAnalyse->getStatus() > 0) + infoBoard->slotRefresh(reportText, INFO_ERROR_OK); + else + infoBoard->slotRefresh(reportText, INFO_ERROR_WARN); + + delete resultAnalyse; + + fileTreeView->update(); + + } catch (std::runtime_error &e) { + QMessageBox::critical(this, tr("Error"), tr("An error occurred during operation.")); + } + + fileTreeView->update(); + } void MainWindow::slotFileVerify() { - QStringList *keyList; - keyList = mKeyList->getChecked(); - new FileEncryptionDialog(mCtx, *keyList, FileEncryptionDialog::Verify, this); + + auto fileTreeView = edit->slotCurPageFileTreeView(); + auto path = fileTreeView->getSelected(); + + QFileInfo fileInfo(path); + + QString signFilePath, dataFilePath; + + if (fileInfo.suffix() == "sig" || fileInfo.suffix() == "gpg") { + int pos = path.lastIndexOf(QChar('.')); + dataFilePath = path.left(pos); + signFilePath = path; + } else { + dataFilePath = path; + signFilePath = path + ".sig"; + } + + QFileInfo dataFileInfo(dataFilePath), signFileInfo(signFilePath); + + if (!dataFileInfo.isFile() || !signFileInfo.isFile()) { + QMessageBox::critical(this, tr("Error"), + tr("Please select the appropriate target file or signature file. Ensure that both are in this directory.")); + return; + } + if (!dataFileInfo.isReadable()) { + QMessageBox::critical(this, tr("Error"), tr("No permission to read target file.")); + return; + } + if (!fileInfo.isReadable()) { + QMessageBox::critical(this, tr("Error"), tr("No permission to read signature file.")); + return; + } + + try { + gpgme_verify_result_t result; + auto error = GpgFileOpera::verifyFile(mCtx, dataFilePath, &result); + + auto resultAnalyse = new VerifyResultAnalyse(mCtx, error, result); + auto &reportText = resultAnalyse->getResultReport(); + infoBoard->associateTabWidget(edit->tabWidget); + infoBoard->associateFileTreeView(edit->curFilePage()); + + if (resultAnalyse->getStatus() < 0) + infoBoard->slotRefresh(reportText, INFO_ERROR_CRITICAL); + else if (resultAnalyse->getStatus() > 0) + infoBoard->slotRefresh(reportText, INFO_ERROR_OK); + else + infoBoard->slotRefresh(reportText, INFO_ERROR_WARN); + + if (resultAnalyse->getStatus() >= 0) { + infoBoard->resetOptionActionsMenu(); + infoBoard->addOptionalAction("Show Verify Details", [this, error, result]() { + VerifyDetailsDialog(this, mCtx, mKeyList, error, result); + }); + } + + delete resultAnalyse; + + fileTreeView->update(); + } catch (std::runtime_error &e) { + QMessageBox::critical(this, tr("Error"), tr("An error occurred during operation.")); + return; + } } -void MainWindow::slotEncryptSign() { +void MainWindow::slotFileEncryptSign() { + auto fileTreeView = edit->slotCurPageFileTreeView(); + auto path = fileTreeView->getSelected(); - if (edit->tabCount() == 0 || edit->slotCurPageTextEdit() == nullptr) { + QFileInfo fileInfo(path); + QFileInfo pathInfo(fileInfo.absolutePath()); + + if (!fileInfo.isFile()) { + QMessageBox::critical(this, tr("Error"), tr("Select a file before doing it.")); + return; + } + if (!fileInfo.isReadable()) { + QMessageBox::critical(this, tr("Error"), tr("No permission to read this file.")); + return; + } + if (!pathInfo.isWritable()) { + QMessageBox::critical(this, tr("Error"), tr("No permission to create file.")); return; } + if (QFile::exists(path + ".asc")) { + auto ret = QMessageBox::warning(this, + tr("Warning"), + tr("The target file already exists, do you need to overwrite it?"), + QMessageBox::Ok | QMessageBox::Cancel); + + if (ret == QMessageBox::Cancel) + return; + } QVector<GpgKey> keys; + mKeyList->getCheckedKeys(keys); if (keys.empty()) { - QMessageBox::critical(nullptr, tr("No Key Selected"), tr("No Key Selected")); + QMessageBox::critical(this, tr("No Key Selected"), tr("No Key Selected")); return; } for (const auto &key : keys) { - if (!GpgME::GpgContext::checkIfKeyCanSign(key) || !GpgME::GpgContext::checkIfKeyCanEncr(key)) { - QMessageBox::information(nullptr, + if (!GpgME::GpgContext::checkIfKeyCanEncr(key)) { + QMessageBox::information(this, tr("Invalid Operation"), - tr("The selected key cannot be used for signing and encryption at the same time.<br/>") + tr("The selected key contains a key that does not actually have a encrypt usage.<br/>") + tr("<br/>For example the Following Key: <br/>") + key.uids.first().uid); return; + } } - auto *tmp = new QByteArray(); - gpgme_encrypt_result_t encr_result = nullptr; - gpgme_sign_result_t sign_result = nullptr; + try { - auto error = mCtx->encryptSign(keys, edit->curTextPage()->toPlainText().toUtf8(), tmp, &encr_result, &sign_result); - auto *tmp2 = new QString(*tmp); - edit->slotFillTextEditWithText(*tmp2); + gpgme_encrypt_result_t encr_result = nullptr; + gpgme_sign_result_t sign_result = nullptr; - auto resultAnalyseEncr = new EncryptResultAnalyse(error, encr_result); - auto resultAnalyseSign = new SignResultAnalyse(error, sign_result); - int status = std::min(resultAnalyseEncr->getStatus(), resultAnalyseSign->getStatus()); - auto reportText = resultAnalyseEncr->getResultReport() + resultAnalyseSign->getResultReport(); + auto error = GpgFileOpera::encryptSignFile(mCtx, keys, path, &encr_result, &sign_result); - infoBoard->associateTextEdit(edit->curTextPage()); + auto resultAnalyseEncr = new EncryptResultAnalyse(error, encr_result); + auto resultAnalyseSign = new SignResultAnalyse(error, sign_result); + int status = std::min(resultAnalyseEncr->getStatus(), resultAnalyseSign->getStatus()); + auto reportText = resultAnalyseEncr->getResultReport() + resultAnalyseSign->getResultReport(); - if (status < 0) - infoBoard->slotRefresh(reportText, INFO_ERROR_CRITICAL); - else if (status > 0) - infoBoard->slotRefresh(reportText, INFO_ERROR_OK); - else - infoBoard->slotRefresh(reportText, INFO_ERROR_WARN); + infoBoard->associateFileTreeView(edit->curFilePage()); + + if (status < 0) + infoBoard->slotRefresh(reportText, INFO_ERROR_CRITICAL); + else if (status > 0) + infoBoard->slotRefresh(reportText, INFO_ERROR_OK); + else + infoBoard->slotRefresh(reportText, INFO_ERROR_WARN); + + delete resultAnalyseEncr; + delete resultAnalyseSign; - delete resultAnalyseEncr; - delete resultAnalyseSign; + fileTreeView->update(); + + } catch (std::runtime_error &e) { + QMessageBox::critical(this, tr("Error"), tr("An error occurred during operation.")); + } } -void MainWindow::slotDecryptVerify() { +void MainWindow::slotFileDecryptVerify() { + auto fileTreeView = edit->slotCurPageFileTreeView(); + auto path = fileTreeView->getSelected(); - if (edit->tabCount() == 0 || edit->slotCurPageTextEdit() == nullptr) { + QFileInfo fileInfo(path); + QFileInfo pathInfo(fileInfo.absolutePath()); + if (!fileInfo.isFile()) { + QMessageBox::critical(this, tr("Error"), tr("Select a file(.gpg/.asc) before doing it.")); + return; + } + if (!fileInfo.isReadable()) { + QMessageBox::critical(this, tr("Error"), tr("No permission to read this file.")); + return; + } + if (!pathInfo.isWritable()) { + QMessageBox::critical(this, tr("Error"), tr("No permission to create file.")); return; } - auto *decrypted = new QByteArray(); - QByteArray text = edit->curTextPage()->toPlainText().toUtf8(); - GpgME::GpgContext::preventNoDataErr(&text); + QString outFileName, fileExtension = fileInfo.suffix(); - gpgme_decrypt_result_t d_result = nullptr; - gpgme_verify_result_t v_result = nullptr; - // try decrypt, if fail do nothing, especially don't replace text - auto error = mCtx->decryptVerify(text, decrypted, &d_result, &v_result); - infoBoard->associateTextEdit(edit->curTextPage()); + if (fileExtension == "asc" || fileExtension == "gpg") { + int pos = path.lastIndexOf(QChar('.')); + outFileName = path.left(pos); + } else { + outFileName = path + ".out"; + } - if(gpgme_err_code(error) == GPG_ERR_NO_ERROR) - edit->slotFillTextEditWithText(QString::fromUtf8(*decrypted)); + if (QFile::exists(outFileName)) { + auto ret = QMessageBox::warning(this, + tr("Warning"), + tr("The target file already exists, do you need to overwrite it?"), + QMessageBox::Ok | QMessageBox::Cancel); - auto resultAnalyseDecrypt = new DecryptResultAnalyse(mCtx, error, d_result); - auto resultAnalyseVerify = new VerifyResultAnalyse(mCtx, error, v_result); + if (ret == QMessageBox::Cancel) + return; + } - int status = std::min(resultAnalyseDecrypt->getStatus(), resultAnalyseVerify->getStatus()); - auto &reportText = resultAnalyseDecrypt->getResultReport() + resultAnalyseVerify->getResultReport(); - if (status < 0) - infoBoard->slotRefresh(reportText, INFO_ERROR_CRITICAL); - else if (status > 0) - infoBoard->slotRefresh(reportText, INFO_ERROR_OK); - else - infoBoard->slotRefresh(reportText, INFO_ERROR_WARN); + try { - if (resultAnalyseVerify->getStatus() >= 0) { - infoBoard->resetOptionActionsMenu(); - infoBoard->addOptionalAction("Show Verify Details", [this, error, v_result]() { - VerifyDetailsDialog(this, mCtx, mKeyList, error, v_result); - }); + gpgme_decrypt_result_t d_result = nullptr; + gpgme_verify_result_t v_result = nullptr; + // try decrypt, if fail do nothing, especially don't replace text + auto error = GpgFileOpera::decryptVerifyFile(mCtx, path, &d_result, &v_result); + infoBoard->associateFileTreeView(edit->curFilePage()); + + auto resultAnalyseDecrypt = new DecryptResultAnalyse(mCtx, error, d_result); + auto resultAnalyseVerify = new VerifyResultAnalyse(mCtx, error, v_result); + + int status = std::min(resultAnalyseDecrypt->getStatus(), resultAnalyseVerify->getStatus()); + auto &reportText = resultAnalyseDecrypt->getResultReport() + resultAnalyseVerify->getResultReport(); + if (status < 0) + infoBoard->slotRefresh(reportText, INFO_ERROR_CRITICAL); + else if (status > 0) + infoBoard->slotRefresh(reportText, INFO_ERROR_OK); + else + infoBoard->slotRefresh(reportText, INFO_ERROR_WARN); + + if (resultAnalyseVerify->getStatus() >= 0) { + infoBoard->resetOptionActionsMenu(); + infoBoard->addOptionalAction("Show Verify Details", [this, error, v_result]() { + VerifyDetailsDialog(this, mCtx, mKeyList, error, v_result); + }); + } + delete resultAnalyseDecrypt; + delete resultAnalyseVerify; + + fileTreeView->update(); + } catch (std::runtime_error &e) { + QMessageBox::critical(this, tr("Error"), tr("An error occurred during operation.")); + return; } - delete resultAnalyseDecrypt; - delete resultAnalyseVerify; +} + +void MainWindow::slotFileEncryptCustom() { + QStringList *keyList; + keyList = mKeyList->getChecked(); + new FileEncryptionDialog(mCtx, *keyList, FileEncryptionDialog::Encrypt, this); +} + +void MainWindow::slotFileDecryptCustom() { + QStringList *keyList; + keyList = mKeyList->getChecked(); + new FileEncryptionDialog(mCtx, *keyList, FileEncryptionDialog::Decrypt, this); +} + +void MainWindow::slotFileSignCustom() { + QStringList *keyList; + keyList = mKeyList->getChecked(); + new FileEncryptionDialog(mCtx, *keyList, FileEncryptionDialog::Sign, this); +} + +void MainWindow::slotFileVerifyCustom() { + QStringList *keyList; + keyList = mKeyList->getChecked(); + new FileEncryptionDialog(mCtx, *keyList, FileEncryptionDialog::Verify, this); } void MainWindow::slotOpenFile(QString &path) { |