diff options
author | Saturneric <[email protected]> | 2021-10-02 14:08:50 +0000 |
---|---|---|
committer | Saturneric <[email protected]> | 2021-10-02 14:16:27 +0000 |
commit | 3c65d087eeee687ac01af2e80f3dd538f9a2c230 (patch) | |
tree | 1e860dc6343c1897e2224a002f2ca44c574381b3 /src/ui/FileEncryptionDialog.cpp | |
parent | The basic functions of the core pass the test. (diff) | |
download | GpgFrontend-3c65d087eeee687ac01af2e80f3dd538f9a2c230.tar.gz GpgFrontend-3c65d087eeee687ac01af2e80f3dd538f9a2c230.zip |
UI Framework Modified.
Diffstat (limited to 'src/ui/FileEncryptionDialog.cpp')
-rwxr-xr-x | src/ui/FileEncryptionDialog.cpp | 466 |
1 files changed, 229 insertions, 237 deletions
diff --git a/src/ui/FileEncryptionDialog.cpp b/src/ui/FileEncryptionDialog.cpp index 55c8e5f0..d141df1e 100755 --- a/src/ui/FileEncryptionDialog.cpp +++ b/src/ui/FileEncryptionDialog.cpp @@ -24,266 +24,258 @@ #include "ui/FileEncryptionDialog.h" -FileEncryptionDialog::FileEncryptionDialog(GpgFrontend::GpgContext *ctx, QStringList keyList, DialogAction action, - QWidget *parent) - : QDialog(parent), mAction(action), mCtx(ctx){ - - if (mAction == Decrypt) { - setWindowTitle(tr("Decrypt File")); - } else if (mAction == Encrypt) { - setWindowTitle(tr("Encrypt File")); - } else if (mAction == Sign) { - setWindowTitle(tr("Sign File")); - } else if (mAction == Verify) { - setWindowTitle(tr("Verify File")); - } - - setModal(true); - - auto *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); - connect(buttonBox, SIGNAL(accepted()), this, SLOT(slotExecuteAction())); - connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject())); - - auto *groupBox1 = new QGroupBox(tr("Input Parameters")); - - /* Setup input & Outputfileselection*/ - inputFileEdit = new QLineEdit(); - auto *fb1 = new QPushButton("Select"); - connect(fb1, SIGNAL(clicked()), this, SLOT(slotSelectInputFile())); - auto *fl1 = new QLabel(tr("Target File")); - fl1->setBuddy(inputFileEdit); - - outputFileEdit = new QLineEdit(); - auto *fb2 = new QPushButton("Select"); - connect(fb2, SIGNAL(clicked()), this, SLOT(slotSelectOutputFile())); - auto *fl2 = new QLabel(tr("Output File")); - fl2->setBuddy(outputFileEdit); - - auto *gLayout = new QGridLayout(); - gLayout->addWidget(fl1, 0, 0); - gLayout->addWidget(inputFileEdit, 0, 1); - gLayout->addWidget(fb1, 0, 2); - signFileEdit = new QLineEdit(); - // verify does not need outfile, but signature file - if (mAction != Verify) { - gLayout->addWidget(fl2, 1, 0); - gLayout->addWidget(outputFileEdit, 1, 1); - gLayout->addWidget(fb2, 1, 2); - } else { - auto *sfb1 = new QPushButton("Select"); - connect(sfb1, SIGNAL(clicked()), this, SLOT(slotSelectSignFile())); - auto *sfl1 = new QLabel(tr("Signature File(.sig) Path")); - sfl1->setBuddy(signFileEdit); - - gLayout->addWidget(sfl1, 1, 0); - gLayout->addWidget(signFileEdit, 1, 1); - gLayout->addWidget(sfb1, 1, 2); - } - groupBox1->setLayout(gLayout); - - /*Setup KeyList*/ - mKeyList = new KeyList(mCtx, KeyListRow::ONLY_SECRET_KEY, - KeyListColumn::NAME | KeyListColumn::EmailAddress | KeyListColumn::Usage); - if(mAction == Verify) - mKeyList->setFilter([](const GpgKey &key) -> bool { - if(key.disabled || key.expired || key.revoked) return false; - else return true; - }); - - if(mAction == Encrypt) - mKeyList->setFilter([](const GpgKey &key) -> bool { - if(!GpgFrontend::GpgContext::checkIfKeyCanEncr(key)) return false; - else return true; - }); - - if(mAction == Sign) - mKeyList->setFilter([](const GpgKey &key) -> bool { - if(!GpgFrontend::GpgContext::checkIfKeyCanSign(key)) return false; - else return true; - }); - - if(mAction == Decrypt) - mKeyList->setDisabled(true); - - mKeyList->slotRefresh(); - mKeyList->setChecked(&keyList); - - statusLabel = new QLabel(); - statusLabel->setStyleSheet("QLabel {color: red;}"); - - auto *vbox2 = new QVBoxLayout(); - vbox2->addWidget(groupBox1); - vbox2->addWidget(mKeyList); - vbox2->addWidget(statusLabel); - vbox2->addWidget(buttonBox); - vbox2->addStretch(0); - setLayout(vbox2); - - this->setMinimumWidth(480); - this->show(); - +#include "gpg/function/BasicOperator.h" +#include "gpg/function/GpgKeyGetter.h" + +namespace GpgFrontend::UI { +FileEncryptionDialog::FileEncryptionDialog(KeyIdArgsListPtr keyList, + DialogAction action, + QWidget* parent) + : QDialog(parent), mAction(action) { + if (mAction == Decrypt) { + setWindowTitle(tr("Decrypt File")); + } else if (mAction == Encrypt) { + setWindowTitle(tr("Encrypt File")); + } else if (mAction == Sign) { + setWindowTitle(tr("Sign File")); + } else if (mAction == Verify) { + setWindowTitle(tr("Verify File")); + } + + setModal(true); + + auto* buttonBox = + new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); + connect(buttonBox, SIGNAL(accepted()), this, SLOT(slotExecuteAction())); + connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject())); + + auto* groupBox1 = new QGroupBox(tr("Input Parameters")); + + /* Setup input & Outputfileselection*/ + inputFileEdit = new QLineEdit(); + auto* fb1 = new QPushButton("Select"); + connect(fb1, SIGNAL(clicked()), this, SLOT(slotSelectInputFile())); + auto* fl1 = new QLabel(tr("Target File")); + fl1->setBuddy(inputFileEdit); + + outputFileEdit = new QLineEdit(); + auto* fb2 = new QPushButton("Select"); + connect(fb2, SIGNAL(clicked()), this, SLOT(slotSelectOutputFile())); + auto* fl2 = new QLabel(tr("Output File")); + fl2->setBuddy(outputFileEdit); + + auto* gLayout = new QGridLayout(); + gLayout->addWidget(fl1, 0, 0); + gLayout->addWidget(inputFileEdit, 0, 1); + gLayout->addWidget(fb1, 0, 2); + signFileEdit = new QLineEdit(); + // verify does not need outfile, but signature file + if (mAction != Verify) { + gLayout->addWidget(fl2, 1, 0); + gLayout->addWidget(outputFileEdit, 1, 1); + gLayout->addWidget(fb2, 1, 2); + } else { + auto* sfb1 = new QPushButton("Select"); + connect(sfb1, SIGNAL(clicked()), this, SLOT(slotSelectSignFile())); + auto* sfl1 = new QLabel(tr("Signature File(.sig) Path")); + sfl1->setBuddy(signFileEdit); + + gLayout->addWidget(sfl1, 1, 0); + gLayout->addWidget(signFileEdit, 1, 1); + gLayout->addWidget(sfb1, 1, 2); + } + groupBox1->setLayout(gLayout); + + /*Setup KeyList*/ + mKeyList = new KeyList( + KeyListRow::ONLY_SECRET_KEY, + KeyListColumn::NAME | KeyListColumn::EmailAddress | KeyListColumn::Usage); + if (mAction == Verify) + mKeyList->setFilter([](const GpgKey& key) -> bool { + if (key.disabled() || key.expired() || key.revoked()) + return false; + else + return true; + }); + + if (mAction == Encrypt) + mKeyList->setFilter([](const GpgKey& key) -> bool { + if (!key.CanEncrActual()) + return false; + else + return true; + }); + + if (mAction == Sign) + mKeyList->setFilter([](const GpgKey& key) -> bool { + if (!key.CanSignActual()) + return false; + else + return true; + }); + + if (mAction == Decrypt) + mKeyList->setDisabled(true); + + mKeyList->slotRefresh(); + mKeyList->setChecked(keyList); + + statusLabel = new QLabel(); + statusLabel->setStyleSheet("QLabel {color: red;}"); + + auto* vbox2 = new QVBoxLayout(); + vbox2->addWidget(groupBox1); + vbox2->addWidget(mKeyList); + vbox2->addWidget(statusLabel); + vbox2->addWidget(buttonBox); + vbox2->addStretch(0); + setLayout(vbox2); + + this->setMinimumWidth(480); + this->show(); } void FileEncryptionDialog::slotSelectInputFile() { - QString path = ""; - if (inputFileEdit->text().size() > 0) { - path = QFileInfo(inputFileEdit->text()).absolutePath(); - } - -// QString infileName = QFileDialog::getOpenFileName(this, tr("Open File"), path, tr("Files") + tr("All Files (*)")); - QString infileName = QFileDialog::getOpenFileName(this, tr("Open File"), path); - inputFileEdit->setText(infileName); - - // try to find a matching output-filename, if not yet done - if (!infileName.isEmpty() - && outputFileEdit->text().size() == 0 - && signFileEdit->text().size() == 0) { - if (mAction == Encrypt) { - outputFileEdit->setText(infileName + ".asc"); - } else if (mAction == Sign) { - outputFileEdit->setText(infileName + ".sig"); - } else if (mAction == Verify) { - signFileEdit->setText(infileName + ".sig"); - } else { - if (infileName.endsWith(".asc", Qt::CaseInsensitive)) { - QString ofn = infileName; - ofn.chop(4); - outputFileEdit->setText(ofn); - } else { - outputFileEdit->setText(infileName + ".out"); - } - } + QString path = ""; + if (inputFileEdit->text().size() > 0) { + path = QFileInfo(inputFileEdit->text()).absolutePath(); + } + + // QString infileName = QFileDialog::getOpenFileName(this, tr("Open File"), + // path, tr("Files") + tr("All Files (*)")); + QString infileName = + QFileDialog::getOpenFileName(this, tr("Open File"), path); + inputFileEdit->setText(infileName); + + // try to find a matching output-filename, if not yet done + if (!infileName.isEmpty() && outputFileEdit->text().size() == 0 && + signFileEdit->text().size() == 0) { + if (mAction == Encrypt) { + outputFileEdit->setText(infileName + ".asc"); + } else if (mAction == Sign) { + outputFileEdit->setText(infileName + ".sig"); + } else if (mAction == Verify) { + signFileEdit->setText(infileName + ".sig"); + } else { + if (infileName.endsWith(".asc", Qt::CaseInsensitive)) { + QString ofn = infileName; + ofn.chop(4); + outputFileEdit->setText(ofn); + } else { + outputFileEdit->setText(infileName + ".out"); + } } + } } void FileEncryptionDialog::slotSelectOutputFile() { - QString path = ""; - if (outputFileEdit->text().size() > 0) { - path = QFileInfo(outputFileEdit->text()).absolutePath(); - } - - QString outfileName = QFileDialog::getSaveFileName(this, tr("Save File"), path, nullptr, nullptr, - QFileDialog::DontConfirmOverwrite); - outputFileEdit->setText(outfileName); - + QString path = ""; + if (outputFileEdit->text().size() > 0) { + path = QFileInfo(outputFileEdit->text()).absolutePath(); + } + + QString outfileName = + QFileDialog::getSaveFileName(this, tr("Save File"), path, nullptr, + nullptr, QFileDialog::DontConfirmOverwrite); + outputFileEdit->setText(outfileName); } void FileEncryptionDialog::slotSelectSignFile() { - QString path = ""; - if (signFileEdit->text().size() > 0) { - path = QFileInfo(signFileEdit->text()).absolutePath(); - } - - QString signfileName = QFileDialog::getSaveFileName(this, tr("Open File"), path, nullptr, nullptr, - QFileDialog::DontConfirmOverwrite); - signFileEdit->setText(signfileName); - - if (inputFileEdit->text().size() == 0 && signfileName.endsWith(".sig", Qt::CaseInsensitive)) { - QString sfn = signfileName; - sfn.chop(4); - inputFileEdit->setText(sfn); - } - + QString path = ""; + if (signFileEdit->text().size() > 0) { + path = QFileInfo(signFileEdit->text()).absolutePath(); + } + + QString signfileName = + QFileDialog::getSaveFileName(this, tr("Open File"), path, nullptr, + nullptr, QFileDialog::DontConfirmOverwrite); + signFileEdit->setText(signfileName); + + if (inputFileEdit->text().size() == 0 && + signfileName.endsWith(".sig", Qt::CaseInsensitive)) { + QString sfn = signfileName; + sfn.chop(4); + inputFileEdit->setText(sfn); + } } void FileEncryptionDialog::slotExecuteAction() { - - QFile infile; - infile.setFileName(inputFileEdit->text()); - if (!infile.open(QIODevice::ReadOnly)) { - statusLabel->setText(tr("Couldn't open file")); - inputFileEdit->setStyleSheet("QLineEdit { background: yellow }"); - return; - } - - QByteArray inBuffer = infile.readAll(); - auto *outBuffer = new QByteArray(); - infile.close(); - - QVector<GpgKey> keys; - mKeyList->getCheckedKeys(keys); - - qDebug() << "slotExecuteAction" << mAction; - - if (mAction == Encrypt) { - qDebug() << "Action Encrypt"; - gpgme_error_t err = mCtx->encrypt(keys, inBuffer, outBuffer, nullptr); - if (gpgme_err_code(err) != GPG_ERR_NO_ERROR) { - qDebug() << "Error" << gpgme_strerror(err); - QMessageBox::warning(this, tr("Error"), - tr("Error Occurred During Encryption")); - return; - } + QFile infile; + infile.setFileName(inputFileEdit->text()); + if (!infile.open(QIODevice::ReadOnly)) { + statusLabel->setText(tr("Couldn't open file")); + inputFileEdit->setStyleSheet("QLineEdit { background: yellow }"); + return; + } + auto in_data = read_all_data_in_file(inputFileEdit->text().toStdString()); + auto out_data = std::make_unique<ByteArray>(); + + auto key_ids = mKeyList->getChecked(); + auto keys = std::vector<GpgKey>(); + for (const auto& key_id : *key_ids) + keys.push_back(GpgKeyGetter::GetInstance().GetKey(key_id)); + + if (mAction == Encrypt) { + qDebug() << "Action Encrypt"; + GpgEncrResult result = nullptr; + gpgme_error_t err = BasicOperator::GetInstance().Encrypt( + std::move(keys), in_data, out_data, result); + if (gpgme_err_code(err) != GPG_ERR_NO_ERROR) { + qDebug() << "Error" << gpgme_strerror(err); + + QMessageBox::warning(this, tr("Error"), + tr("Error Occurred During Encryption")); + return; } - - if (mAction == Decrypt) { - qDebug() << "Action Decrypt"; - gpgme_error_t err = mCtx->decrypt(inBuffer, outBuffer, nullptr); - if (gpgme_err_code(err) != GPG_ERR_NO_ERROR) { - qDebug() << "Error" << gpgme_strerror(err); - QMessageBox::warning(this, tr("Error"), - tr("Error Occurred During Decryption")); - return; - } + } + + else if (mAction == Decrypt) { + qDebug() << "Action Decrypt"; + GpgDecrResult result = nullptr; + gpgme_error_t err = + BasicOperator::GetInstance().Decrypt(in_data, out_data, result); + if (gpgme_err_code(err) != GPG_ERR_NO_ERROR) { + qDebug() << "Error" << gpgme_strerror(err); + QMessageBox::warning(this, tr("Error"), + tr("Error Occurred During Decryption")); + return; } - - if (mAction == Sign) { - qDebug() << "Action Sign"; - gpgme_error_t err = mCtx->sign(keys, inBuffer, outBuffer, GPGME_SIG_MODE_DETACH, nullptr); - if (gpgme_err_code(err) != GPG_ERR_NO_ERROR) { - qDebug() << "Error" << gpgme_strerror(err); - QMessageBox::warning(this, tr("Error"), - tr("Error Occurred During Signature")); - return; - } + } + + else if (mAction == Sign) { + qDebug() << "Action Sign"; + GpgSignResult result = nullptr; + gpgme_error_t err = BasicOperator::GetInstance().Sign( + std::move(keys), in_data, out_data, GPGME_SIG_MODE_DETACH, result); + if (gpgme_err_code(err) != GPG_ERR_NO_ERROR) { + qDebug() << "Error" << gpgme_strerror(err); + QMessageBox::warning(this, tr("Error"), + tr("Error Occurred During Signature")); + return; } + } - if (mAction == Verify) { - QFile sign_file; - sign_file.setFileName(signFileEdit->text()); - if (!sign_file.open(QIODevice::ReadOnly)) { - statusLabel->setText(tr("Couldn't open file")); - signFileEdit->setStyleSheet("QLineEdit { background: yellow }"); - return; - } - auto signBuffer = sign_file.readAll(); - gpgme_verify_result_t result; - auto error = mCtx->verify(&inBuffer, &signBuffer, &result); - new VerifyDetailsDialog(this, mCtx, mKeyList, error, result); - return; - } + if (mAction == Verify) { + auto sign_data = std::make_unique<ByteArray>( + read_all_data_in_file(signFileEdit->text().toStdString())); + GpgVerifyResult result = nullptr; + auto error = + BasicOperator::GetInstance().Verify(in_data, sign_data, result); + new VerifyDetailsDialog(this, mKeyList, error, std::move(result)); + return; + } - QFile outfile(outputFileEdit->text()); - if (outfile.exists()) { - QMessageBox::StandardButton ret; - ret = QMessageBox::warning(this, tr("File"), - tr("File exists! Do you want to overwrite it?"), - QMessageBox::Ok | QMessageBox::Cancel); - if (ret == QMessageBox::Cancel) { - return; - } - } + write_buffer_to_file(outputFileEdit->text().toStdString(), *out_data); - if (!outfile.open(QFile::WriteOnly)) { - QMessageBox::warning(this, tr("File"), - tr("Cannot write file %1:\n%2.") - .arg(outputFileEdit->text()) - .arg(outfile.errorString())); - return; - } - - QDataStream out(&outfile); - out.writeRawData(outBuffer->data(), outBuffer->length()); - outfile.close(); - QMessageBox::information(nullptr, "Done", "Output saved to " + outputFileEdit->text()); - - accept(); + accept(); } void FileEncryptionDialog::slotShowKeyList() { - mKeyList->show(); + mKeyList->show(); } void FileEncryptionDialog::slotHideKeyList() { - mKeyList->hide(); + mKeyList->hide(); } + +} // namespace GpgFrontend::UI |