diff options
-rw-r--r-- | include/MainWindow.h | 2 | ||||
-rw-r--r-- | include/gpg/GpgFileOpera.h | 37 | ||||
-rw-r--r-- | include/ui/widgets/FilePage.h | 5 | ||||
-rw-r--r-- | include/ui/widgets/TextEdit.h | 10 | ||||
-rw-r--r-- | src/gpg/GpgFileOpera.cpp | 52 | ||||
-rw-r--r-- | src/ui/main_window/MainWindowSlotFunction.cpp | 131 | ||||
-rw-r--r-- | src/ui/main_window/MainWindowSlotUI.cpp | 8 | ||||
-rw-r--r-- | src/ui/widgets/FilePage.cpp | 30 | ||||
-rw-r--r-- | src/ui/widgets/TextEdit.cpp | 23 |
9 files changed, 234 insertions, 64 deletions
diff --git a/include/MainWindow.h b/include/MainWindow.h index c5937e0d..efe56919 100644 --- a/include/MainWindow.h +++ b/include/MainWindow.h @@ -41,6 +41,8 @@ #include "gpg/result_analyse/EncryptResultAnalyse.h" #include "gpg/result_analyse/DecryptResultAnalyse.h" +#include "gpg/GpgFileOpera.h" + /** * @brief diff --git a/include/gpg/GpgFileOpera.h b/include/gpg/GpgFileOpera.h new file mode 100644 index 00000000..c6b4aec8 --- /dev/null +++ b/include/gpg/GpgFileOpera.h @@ -0,0 +1,37 @@ +/** + * 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. + * + * Foobar 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 Foobar. If not, see <https://www.gnu.org/licenses/>. + * + * The initial version of the source code is inherited from gpg4usb-team. + * Their source code version also complies with GNU General Public License. + * + * The source code version of this software was modified and released + * by Saturneric<[email protected]> starting on May 12, 2021. + * + */ + +#ifndef GPGFRONTEND_GPGFILEOPERA_H +#define GPGFRONTEND_GPGFILEOPERA_H + +#include "GpgFrontend.h" +#include "gpg/GpgContext.h" + +class GpgFileOpera { +public: + static bool encryptFile(GpgME::GpgContext *ctx, QVector<GpgKey> &keys, const QString &mPath); +}; + + +#endif //GPGFRONTEND_GPGFILEOPERA_H diff --git a/include/ui/widgets/FilePage.h b/include/ui/widgets/FilePage.h index 6585bed9..c343571d 100644 --- a/include/ui/widgets/FilePage.h +++ b/include/ui/widgets/FilePage.h @@ -33,7 +33,7 @@ public: explicit FilePage(QWidget* parent = nullptr); - void getSelected(QString &path); + QString getSelected() const; private slots: @@ -42,13 +42,16 @@ private slots: void fileTreeViewItemDoubleClicked(const QModelIndex &index); void slotUpLevel(); + void slotGoPath(); private: QFileSystemModel *dirModel; QTreeView *dirTreeView; + QLineEdit *pathEdit; QString mPath; QPushButton *upLevelButton; + QPushButton *goPathButton; }; diff --git a/include/ui/widgets/TextEdit.h b/include/ui/widgets/TextEdit.h index f98da145..99b8bcc7 100644 --- a/include/ui/widgets/TextEdit.h +++ b/include/ui/widgets/TextEdit.h @@ -82,10 +82,16 @@ public: public slots: /** - * @details Return pointer to the currently activated tabpage. + * @details Return pointer to the currently activated text edit tab page. * */ - [[nodiscard]] EditorPage *slotCurPage() const; + [[nodiscard]] EditorPage *slotCurPageTextEdit() const; + + /** + * @details Return pointer to the currently activated file treeview tab page. + * + */ + [[nodiscard]] FilePage *slotCurPageFileTreeView() const; /** * @details Insert a ">" at the begining of every line of current textedit. diff --git a/src/gpg/GpgFileOpera.cpp b/src/gpg/GpgFileOpera.cpp new file mode 100644 index 00000000..c3a9b5d0 --- /dev/null +++ b/src/gpg/GpgFileOpera.cpp @@ -0,0 +1,52 @@ +/** + * 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. + * + * Foobar 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 Foobar. If not, see <https://www.gnu.org/licenses/>. + * + * The initial version of the source code is inherited from gpg4usb-team. + * Their source code version also complies with GNU General Public License. + * + * The source code version of this software was modified and released + * by Saturneric<[email protected]> starting on May 12, 2021. + * + */ +#include "gpg/GpgFileOpera.h" + +bool GpgFileOpera::encryptFile(GpgME::GpgContext *ctx, QVector<GpgKey> &keys, const QString &mPath) { + + QFileInfo fileInfo(mPath); + + if(!fileInfo.isFile() || !fileInfo.isReadable()) return false; + + QFile infile; + infile.setFileName(mPath); + if (!infile.open(QIODevice::ReadOnly)) + return false; + + QByteArray inBuffer = infile.readAll(); + auto *outBuffer = new QByteArray(); + infile.close(); + + if (gpg_err_code(ctx->encrypt(keys, inBuffer, outBuffer, nullptr)) != GPG_ERR_NO_ERROR) return false; + + QFile outfile(mPath + ".asc"); + + if (!outfile.open(QFile::WriteOnly)) + return false; + + QDataStream out(&outfile); + out.writeRawData(outBuffer->data(), outBuffer->length()); + outfile.close(); + return true; +} diff --git a/src/ui/main_window/MainWindowSlotFunction.cpp b/src/ui/main_window/MainWindowSlotFunction.cpp index 60c33d83..dcfac10b 100644 --- a/src/ui/main_window/MainWindowSlotFunction.cpp +++ b/src/ui/main_window/MainWindowSlotFunction.cpp @@ -25,53 +25,57 @@ #include "MainWindow.h" void MainWindow::slotEncrypt() { - if (edit->tabCount() == 0 || edit->slotCurPage() == nullptr) { - return; - } - QVector<GpgKey> keys; - mKeyList->getCheckedKeys(keys); + if (edit->tabCount() == 0) return; - if (keys.count() == 0) { - QMessageBox::critical(nullptr, tr("No Key Selected"), tr("No Key Selected")); - return; - } + if(edit->slotCurPageTextEdit() != nullptr) { - for (const auto &key : keys) { - if (!GpgME::GpgContext::checkIfKeyCanEncr(key)) { - QMessageBox::information(nullptr, - tr("Invalid Operation"), - tr("The selected key contains a key that does not actually have a encrypt function.<br/>") - + tr("<br/>For example the Following Key: <br/>") + key.uids.first().uid); + QVector<GpgKey> keys; + mKeyList->getCheckedKeys(keys); + + if (keys.count() == 0) { + QMessageBox::critical(nullptr, tr("No Key Selected"), tr("No Key Selected")); return; + } + + for (const auto &key : keys) { + if (!GpgME::GpgContext::checkIfKeyCanEncr(key)) { + QMessageBox::information(nullptr, + 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; + } } - } - auto *tmp = new QByteArray(); + auto *tmp = new QByteArray(); - gpgme_encrypt_result_t result = nullptr; - auto error = mCtx->encrypt(keys, edit->curTextPage()->toPlainText().toUtf8(), tmp, &result); + gpgme_encrypt_result_t result = nullptr; + auto error = mCtx->encrypt(keys, edit->curTextPage()->toPlainText().toUtf8(), tmp, &result); - auto resultAnalyse = new EncryptResultAnalyse(error, result); - auto &reportText = resultAnalyse->getResultReport(); + auto resultAnalyse = new EncryptResultAnalyse(error, result); + auto &reportText = resultAnalyse->getResultReport(); - auto *tmp2 = new QString(*tmp); - edit->slotFillTextEditWithText(*tmp2); - infoBoard->associateTextEdit(edit->curTextPage()); + auto *tmp2 = new QString(*tmp); + edit->slotFillTextEditWithText(*tmp2); + infoBoard->associateTextEdit(edit->curTextPage()); - 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->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; + delete resultAnalyse; + } else if(edit->slotCurPageFileTreeView() != nullptr) { + this->slotFileEncrypt(); + } } void MainWindow::slotSign() { - if (edit->tabCount() == 0 || edit->slotCurPage() == nullptr) { + if (edit->tabCount() == 0 || edit->slotCurPageTextEdit() == nullptr) { return; } @@ -88,7 +92,7 @@ void MainWindow::slotSign() { if (!GpgME::GpgContext::checkIfKeyCanSign(key)) { QMessageBox::information(nullptr, tr("Invalid Operation"), - tr("The selected key contains a key that does not actually have a signature function.<br/>") + 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; } @@ -116,7 +120,7 @@ void MainWindow::slotSign() { } void MainWindow::slotDecrypt() { - if (edit->tabCount() == 0 || edit->slotCurPage() == nullptr) { + if (edit->tabCount() == 0 || edit->slotCurPageTextEdit() == nullptr) { return; } @@ -151,15 +155,15 @@ void MainWindow::slotFind() { } // At first close verifynotification, if existing - edit->slotCurPage()->closeNoteByClass("findwidget"); + edit->slotCurPageTextEdit()->closeNoteByClass("findwidget"); auto *fw = new FindWidget(this, edit->curTextPage()); - edit->slotCurPage()->showNotificationWidget(fw, "findWidget"); + edit->slotCurPageTextEdit()->showNotificationWidget(fw, "findWidget"); } void MainWindow::slotVerify() { - if (edit->tabCount() == 0 || edit->slotCurPage() == nullptr) { + if (edit->tabCount() == 0 || edit->slotCurPageTextEdit() == nullptr) { return; } @@ -198,7 +202,7 @@ void MainWindow::slotVerify() { * Append the selected (not checked!) Key(s) To Textedit */ void MainWindow::slotAppendSelectedKeys() { - if (edit->tabCount() == 0 || edit->slotCurPage() == nullptr) { + if (edit->tabCount() == 0 || edit->slotCurPageTextEdit() == nullptr) { return; } @@ -245,9 +249,50 @@ void MainWindow::uploadKeyToServer() { } void MainWindow::slotFileEncrypt() { - QStringList *keyList; - keyList = mKeyList->getChecked(); - new FileEncryptionDialog(mCtx, *keyList, FileEncryptionDialog::Encrypt, this); + + auto fileTreeView = edit->slotCurPageFileTreeView(); + auto path = fileTreeView->getSelected(); + + QFileInfo fileInfo(path); + if(!fileInfo.isFile()) { + QMessageBox::critical(this, tr("Error"), tr("Can only encrypt a file.")); + return; + } + if(!fileInfo.isReadable()) { + QMessageBox::critical(this, tr("Error"), tr("No permission to read this 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); + + for (const auto &key : keys) { + if (!GpgME::GpgContext::checkIfKeyCanEncr(key)) { + QMessageBox::information(nullptr, + 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; + + } + } + + if(!GpgFileOpera::encryptFile(mCtx, keys, path)) { + QMessageBox::critical(this, tr("Error"), tr("An error occurred during operation.")); + } + + fileTreeView->update(); + } void MainWindow::slotFileDecrypt() { @@ -270,7 +315,7 @@ void MainWindow::slotFileVerify() { void MainWindow::slotEncryptSign() { - if (edit->tabCount() == 0 || edit->slotCurPage() == nullptr) { + if (edit->tabCount() == 0 || edit->slotCurPageTextEdit() == nullptr) { return; } @@ -320,7 +365,7 @@ void MainWindow::slotEncryptSign() { void MainWindow::slotDecryptVerify() { - if (edit->tabCount() == 0 || edit->slotCurPage() == nullptr) { + if (edit->tabCount() == 0 || edit->slotCurPageTextEdit() == nullptr) { return; } diff --git a/src/ui/main_window/MainWindowSlotUI.cpp b/src/ui/main_window/MainWindowSlotUI.cpp index 8ee09f64..cad8669a 100644 --- a/src/ui/main_window/MainWindowSlotUI.cpp +++ b/src/ui/main_window/MainWindowSlotUI.cpp @@ -63,7 +63,7 @@ void MainWindow::slotCheckAttachmentFolder() { } void MainWindow::slotImportKeyFromEdit() { - if (edit->tabCount() == 0 || edit->slotCurPage() == 0) { + if (edit->tabCount() == 0 || edit->slotCurPageTextEdit() == 0) { return; } @@ -163,7 +163,7 @@ void MainWindow::slotOpenSettingsDialog() { } void MainWindow::slotCleanDoubleLinebreaks() { - if (edit->tabCount() == 0 || edit->slotCurPage() == nullptr) { + if (edit->tabCount() == 0 || edit->slotCurPageTextEdit() == nullptr) { return; } @@ -173,7 +173,7 @@ void MainWindow::slotCleanDoubleLinebreaks() { } void MainWindow::slotAddPgpHeader() { - if (edit->tabCount() == 0 || edit->slotCurPage() == nullptr) { + if (edit->tabCount() == 0 || edit->slotCurPageTextEdit() == nullptr) { return; } @@ -187,7 +187,7 @@ void MainWindow::slotAddPgpHeader() { void MainWindow::slotCutPgpHeader() { - if (edit->tabCount() == 0 || edit->slotCurPage() == nullptr) { + if (edit->tabCount() == 0 || edit->slotCurPageTextEdit() == nullptr) { return; } diff --git a/src/ui/widgets/FilePage.cpp b/src/ui/widgets/FilePage.cpp index e3d73670..d1e04879 100644 --- a/src/ui/widgets/FilePage.cpp +++ b/src/ui/widgets/FilePage.cpp @@ -28,21 +28,28 @@ FilePage::FilePage(QWidget *parent) : QWidget(parent) { qDebug() << "New File Page"; dirModel = new QFileSystemModel(); - dirModel->setRootPath(QDir::currentPath()); dirTreeView = new QTreeView(); dirTreeView->setModel(dirModel); dirTreeView->setAnimated(true); dirTreeView->setIndentation(20); - dirTreeView->setSortingEnabled(true); dirTreeView->setRootIndex(dirModel->index(QDir::currentPath())); upLevelButton = new QPushButton("UP Level"); connect(upLevelButton, SIGNAL(clicked(bool)), this, SLOT(slotUpLevel())); + goPathButton = new QPushButton("Go Path"); + connect(goPathButton, SIGNAL(clicked(bool)), this, SLOT(slotGoPath())); + + pathEdit = new QLineEdit(); + pathEdit->setFixedWidth(520); + pathEdit->setText(dirModel->rootPath()); + auto *menuLayout = new QHBoxLayout(); menuLayout->addWidget(upLevelButton); + menuLayout->addWidget(pathEdit); + menuLayout->addWidget(goPathButton); menuLayout->addStretch(0); auto *layout = new QVBoxLayout(); @@ -63,10 +70,11 @@ void FilePage::fileTreeViewItemClicked(const QModelIndex &index) { void FilePage::slotUpLevel() { QModelIndex currentRoot = dirTreeView->rootIndex(); - mPath = dirModel->fileInfo(currentRoot).absoluteFilePath(); + mPath = dirModel->fileInfo(currentRoot.parent()).absoluteFilePath(); auto fileInfo = QFileInfo(mPath); if(fileInfo.isDir() && fileInfo.isReadable() && fileInfo.isExecutable()) { dirTreeView->setRootIndex(currentRoot.parent()); + pathEdit->setText(mPath); } qDebug() << "Current Root mPath" << mPath; } @@ -76,13 +84,25 @@ void FilePage::fileTreeViewItemDoubleClicked(const QModelIndex &index) { auto fileInfo = QFileInfo(mPath); if(fileInfo.isDir() && fileInfo.isReadable() && fileInfo.isExecutable()) { dirTreeView->setRootIndex(index); + pathEdit->setText(mPath); } qDebug() << "Index mPath" << mPath; } -void FilePage::getSelected(QString &path) { +QString FilePage::getSelected() const { QModelIndex index = dirTreeView->currentIndex(); QVariant data = dirTreeView->model()->data(index); - path = data.toString(); qDebug() << "Target Path" << mPath; + return data.toString(); +} + +void FilePage::slotGoPath() { + qDebug() << "getSelected" << pathEdit->text(); + auto fileInfo = QFileInfo(pathEdit->text()); + if(fileInfo.isDir() && fileInfo.isReadable() && fileInfo.isExecutable()) { + qDebug() << "Set Path" << fileInfo.filePath(); + dirTreeView->setRootIndex(dirModel->index(fileInfo.filePath())); + } else { + QMessageBox::critical(this, "Error", "The path is unprivileged or unreachable."); + } } diff --git a/src/ui/widgets/TextEdit.cpp b/src/ui/widgets/TextEdit.cpp index 49049baa..00abe16f 100644 --- a/src/ui/widgets/TextEdit.cpp +++ b/src/ui/widgets/TextEdit.cpp @@ -108,11 +108,11 @@ void TextEdit::slotOpen() { } void TextEdit::slotSave() { - if (tabWidget->count() == 0 || slotCurPage() == 0) { + if (tabWidget->count() == 0 || slotCurPageTextEdit() == 0) { return; } - QString fileName = slotCurPage()->getFilePath(); + QString fileName = slotCurPageTextEdit()->getFilePath(); if (fileName.isEmpty()) { //QString docname = tabWidget->tabText(tabWidget->currentIndex()); @@ -131,7 +131,7 @@ bool TextEdit::saveFile(const QString &fileName) { QFile file(fileName); if (file.open(QIODevice::WriteOnly | QIODevice::Text)) { - EditorPage *page = slotCurPage(); + EditorPage *page = slotCurPageTextEdit(); QTextStream outputStream(&file); QApplication::setOverrideCursor(Qt::WaitCursor); @@ -158,11 +158,11 @@ bool TextEdit::saveFile(const QString &fileName) { bool TextEdit::slotSaveAs() { - if (tabWidget->count() == 0 || slotCurPage() == 0) { + if (tabWidget->count() == 0 || slotCurPageTextEdit() == 0) { return true; } - EditorPage *page = slotCurPage(); + EditorPage *page = slotCurPageTextEdit(); QString path; if (page->getFilePath() != "") { path = page->getFilePath(); @@ -178,7 +178,7 @@ bool TextEdit::slotSaveAs() { void TextEdit::slotCloseTab() { removeTab(tabWidget->currentIndex()); if (tabWidget->count() != 0) { - slotCurPage()->getTextPage()->setFocus(); + slotCurPageTextEdit()->getTextPage()->setFocus(); } } @@ -218,7 +218,7 @@ void TextEdit::removeTab(int index) { */ bool TextEdit::maybeSaveCurrentTab(bool askToSave) { - EditorPage *page = slotCurPage(); + EditorPage *page = slotCurPageTextEdit(); // if this page is no textedit, there should be nothing to save if (page == nullptr) { return true; @@ -339,11 +339,16 @@ int TextEdit::tabCount() const { return tabWidget->count(); } -EditorPage *TextEdit::slotCurPage() const { +EditorPage *TextEdit::slotCurPageTextEdit() const { auto *curPage = qobject_cast<EditorPage *>(tabWidget->currentWidget()); return curPage; } +FilePage *TextEdit::slotCurPageFileTreeView() const { + auto *curPage = qobject_cast<FilePage *>(tabWidget->currentWidget()); + return curPage; +} + void TextEdit::slotQuote() const { if (tabWidget->count() == 0 || curTextPage() == nullptr) { return; @@ -386,7 +391,7 @@ void TextEdit::loadFile(const QString &fileName) { QApplication::setOverrideCursor(Qt::WaitCursor); curTextPage()->setPlainText(in.readAll()); QApplication::restoreOverrideCursor(); - slotCurPage()->setFilePath(fileName); + slotCurPageTextEdit()->setFilePath(fileName); tabWidget->setTabText(tabWidget->currentIndex(), strippedName(fileName)); file.close(); // statusBar()->showMessage(tr("File loaded"), 2000); |