diff options
author | Saturneric <[email protected]> | 2021-11-28 13:38:59 +0000 |
---|---|---|
committer | Saturneric <[email protected]> | 2021-11-28 13:38:59 +0000 |
commit | 9c1446c97ffff92a6c90442f9148239f24ac9a54 (patch) | |
tree | 73689befad5467dfa02ea0fe8934c52c99c8a874 | |
parent | Solve key generation and related update issues. (diff) | |
download | GpgFrontend-9c1446c97ffff92a6c90442f9148239f24ac9a54.tar.gz GpgFrontend-9c1446c97ffff92a6c90442f9148239f24ac9a54.zip |
Fix some known issues in basic operations and file operations.
Diffstat (limited to '')
-rw-r--r-- | src/gpg/GpgConstants.cpp | 10 | ||||
-rw-r--r-- | src/gpg/function/GpgFileOpera.cpp | 37 | ||||
-rw-r--r-- | src/gpg/function/GpgFileOpera.h | 27 | ||||
-rw-r--r-- | src/ui/UserInterfaceUtils.cpp | 7 | ||||
-rw-r--r-- | src/ui/main_window/MainWindowFileSlotFunction.cpp | 68 | ||||
-rw-r--r-- | src/ui/main_window/MainWindowSlotFunction.cpp | 14 | ||||
-rw-r--r-- | src/ui/widgets/EditorPage.cpp | 61 | ||||
-rw-r--r-- | src/ui/widgets/EditorPage.h | 3 | ||||
-rw-r--r-- | src/ui/widgets/FilePage.cpp | 91 | ||||
-rw-r--r-- | src/ui/widgets/FilePage.h | 8 | ||||
-rw-r--r-- | src/ui/widgets/TextEdit.cpp | 97 | ||||
-rw-r--r-- | src/ui/widgets/TextEdit.h | 21 |
12 files changed, 171 insertions, 273 deletions
diff --git a/src/gpg/GpgConstants.cpp b/src/gpg/GpgConstants.cpp index 3d80cf90..6f1b2250 100644 --- a/src/gpg/GpgConstants.cpp +++ b/src/gpg/GpgConstants.cpp @@ -25,6 +25,7 @@ #include "gpg/GpgConstants.h" #include <gpg-error.h> + #include <boost/algorithm/string/predicate.hpp> #include <filesystem> @@ -115,8 +116,7 @@ std::string GpgFrontend::read_all_data_in_file(const std::string& path) { std::ifstream in_file; in_file.open(path, std::ios::in); - if (!in_file.good()) - throw std::runtime_error("cannot open file"); + if (!in_file.good()) throw std::runtime_error("cannot open file"); std::istreambuf_iterator<char> begin(in_file); std::istreambuf_iterator<char> end; std::string in_buffer(begin, end); @@ -126,10 +126,8 @@ std::string GpgFrontend::read_all_data_in_file(const std::string& path) { bool GpgFrontend::write_buffer_to_file(const std::string& path, const std::string& out_buffer) { - std::ofstream out_file(path); - out_file.open(path.c_str(), std::ios::out); - if (!out_file.good()) - return false; + std::ofstream out_file(std::filesystem::path(path), std::ios::out); + if (!out_file.good()) return false; out_file.write(out_buffer.c_str(), out_buffer.size()); out_file.close(); return true; diff --git a/src/gpg/function/GpgFileOpera.cpp b/src/gpg/function/GpgFileOpera.cpp index 0f78c30c..b8e204b8 100644 --- a/src/gpg/function/GpgFileOpera.cpp +++ b/src/gpg/function/GpgFileOpera.cpp @@ -22,19 +22,15 @@ * */ #include "gpg/function/GpgFileOpera.h" -#include "GpgConstants.h" -#include "gpg/function/BasicOperator.h" - -#include <boost/process/detail/config.hpp> -#include <iterator> #include <memory> #include <string> +#include "GpgConstants.h" +#include "gpg/function/BasicOperator.h" + GpgFrontend::GpgError GpgFrontend::GpgFileOpera::EncryptFile( - KeyArgsList&& keys, - const std::string& path, - GpgEncrResult& result) { + KeyArgsList&& keys, const std::string& path, GpgEncrResult& result) { std::string in_buffer = read_all_data_in_file(path); std::unique_ptr<std::string> out_buffer; @@ -48,8 +44,7 @@ GpgFrontend::GpgError GpgFrontend::GpgFileOpera::EncryptFile( } GpgFrontend::GpgError GpgFrontend::GpgFileOpera::DecryptFile( - const std::string& path, - GpgDecrResult& result) { + const std::string& path, GpgDecrResult& result) { std::string in_buffer = read_all_data_in_file(path); std::unique_ptr<std::string> out_buffer; @@ -96,7 +91,7 @@ gpgme_error_t GpgFrontend::GpgFileOpera::VerifyFile(const std::string& path, assert(check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR); return err; } else { - auto sign_buffer = + sign_buffer = std::make_unique<std::string>(read_all_data_in_file(path + ".sig")); auto err = @@ -108,9 +103,7 @@ gpgme_error_t GpgFrontend::GpgFileOpera::VerifyFile(const std::string& path, // TODO gpg_error_t GpgFrontend::GpgFileOpera::EncryptSignFile( - KeyArgsList&& keys, - const std::string& path, - GpgEncrResult& encr_res, + KeyArgsList&& keys, const std::string& path, GpgEncrResult& encr_res, GpgSignResult& sign_res) { auto in_buffer = read_all_data_in_file(path); std::unique_ptr<std::string> out_buffer = nullptr; @@ -123,16 +116,20 @@ gpg_error_t GpgFrontend::GpgFileOpera::EncryptSignFile( std::move(keys), std::move(signerKeys), in_buffer, out_buffer, encr_res, sign_res); - assert(check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR); - - write_buffer_to_file(path + ".gpg", *out_buffer); + auto out_path = path + ".gpg"; + LOG(INFO) << "EncryptSignFile out_path" << out_path; + LOG(INFO) << "EncryptSignFile out_buffer size" << out_buffer->size(); - return err; + bool result = write_buffer_to_file(out_path, *out_buffer); + LOG(INFO) << "EncryptSignFile write_buffer_to_file result" << result; + if (result) + return err; + else + throw std::runtime_error("write_buffer_to_file failed."); } gpg_error_t GpgFrontend::GpgFileOpera::DecryptVerifyFile( - const std::string& path, - GpgDecrResult& decr_res, + const std::string& path, GpgDecrResult& decr_res, GpgVerifyResult& verify_res) { auto in_buffer = read_all_data_in_file(path); std::unique_ptr<std::string> out_buffer = nullptr; diff --git a/src/gpg/function/GpgFileOpera.h b/src/gpg/function/GpgFileOpera.h index dece2e52..ff57b2c0 100644 --- a/src/gpg/function/GpgFileOpera.h +++ b/src/gpg/function/GpgFileOpera.h @@ -33,26 +33,23 @@ namespace GpgFrontend { class GpgFileOpera : public SingletonFunctionObject<GpgFileOpera> { public: - GpgError EncryptFile(KeyArgsList&& keys, - const std::string& path, - GpgEncrResult& result); + static GpgError EncryptFile(KeyArgsList&& keys, const std::string& path, + GpgEncrResult& result); - GpgError DecryptFile(const std::string& path, GpgDecrResult& result); + static GpgError DecryptFile(const std::string& path, GpgDecrResult& result); - GpgError SignFile(KeyArgsList&& keys, - const std::string& path, - GpgSignResult& result); + static GpgError SignFile(KeyArgsList&& keys, const std::string& path, + GpgSignResult& result); - GpgError VerifyFile(const std::string& path, GpgVerifyResult& result); + static GpgError VerifyFile(const std::string& path, GpgVerifyResult& result); - GpgError EncryptSignFile(KeyArgsList&& keys, - const std::string& path, - GpgEncrResult& encr_res, - GpgSignResult& sign_res); + static GpgError EncryptSignFile(KeyArgsList&& keys, const std::string& path, + GpgEncrResult& encr_res, + GpgSignResult& sign_res); - GpgError DecryptVerifyFile(const std::string& path, - GpgDecrResult& decr_res, - GpgVerifyResult& verify_res); + static GpgError DecryptVerifyFile(const std::string& path, + GpgDecrResult& decr_res, + GpgVerifyResult& verify_res); }; } // namespace GpgFrontend diff --git a/src/ui/UserInterfaceUtils.cpp b/src/ui/UserInterfaceUtils.cpp index 8a6021c0..4dd571a1 100644 --- a/src/ui/UserInterfaceUtils.cpp +++ b/src/ui/UserInterfaceUtils.cpp @@ -53,13 +53,16 @@ void process_result_analyse(TextEdit* edit, InfoBoardWidget* info_board, void process_result_analyse(TextEdit* edit, InfoBoardWidget* info_board, const ResultAnalyse& result_analyse_a, const ResultAnalyse& result_analyse_b) { + + LOG(INFO) << "process_result_analyse Started"; + info_board->associateTabWidget(edit->tabWidget); info_board->associateFileTreeView(edit->curFilePage()); refresh_info_board( info_board, - std::min(result_analyse_a.getStatus(), result_analyse_a.getStatus()), - result_analyse_a.getResultReport() + result_analyse_a.getResultReport()); + std::min(result_analyse_a.getStatus(), result_analyse_b.getStatus()), + result_analyse_a.getResultReport() + result_analyse_b.getResultReport()); } void process_operation(QWidget* parent, const std::string& waiting_title, diff --git a/src/ui/main_window/MainWindowFileSlotFunction.cpp b/src/ui/main_window/MainWindowFileSlotFunction.cpp index 2590cdda..2edac85d 100644 --- a/src/ui/main_window/MainWindowFileSlotFunction.cpp +++ b/src/ui/main_window/MainWindowFileSlotFunction.cpp @@ -25,41 +25,10 @@ #include "MainWindow.h" #include "gpg/function/GpgFileOpera.h" #include "gpg/function/GpgKeyGetter.h" +#include "ui/UserInterfaceUtils.h" namespace GpgFrontend::UI { -void refresh_info_board(InfoBoardWidget* info_board, int status, - const std::string& report_text) { - if (status < 0) - info_board->slotRefresh(QString::fromStdString(report_text), - INFO_ERROR_CRITICAL); - else if (status > 0) - info_board->slotRefresh(QString::fromStdString(report_text), INFO_ERROR_OK); - else - info_board->slotRefresh(QString::fromStdString(report_text), - INFO_ERROR_WARN); -} - -void process_result_analyse(TextEdit* edit, InfoBoardWidget* info_board, - const ResultAnalyse& result_analyse) { - info_board->associateTabWidget(edit->tabWidget); - info_board->associateFileTreeView(edit->curFilePage()); - refresh_info_board(info_board, result_analyse.getStatus(), - result_analyse.getResultReport()); -} - -void process_result_analyse(TextEdit* edit, InfoBoardWidget* info_board, - const ResultAnalyse& result_analyse_a, - const ResultAnalyse& result_analyse_b) { - info_board->associateTabWidget(edit->tabWidget); - info_board->associateFileTreeView(edit->curFilePage()); - - refresh_info_board( - info_board, - std::min(result_analyse_a.getStatus(), result_analyse_a.getStatus()), - result_analyse_a.getResultReport() + result_analyse_a.getResultReport()); -} - bool file_pre_check(QWidget* parent, const QString& path) { QFileInfo file_info(path); QFileInfo path_info(file_info.absolutePath()); @@ -81,21 +50,6 @@ bool file_pre_check(QWidget* parent, const QString& path) { return true; } -void process_operation(QWidget* parent, const std::string& waiting_title, - const std::function<void()>& func) { - auto thread = QThread::create(func); - QApplication::connect(thread, SIGNAL(finished()), thread, - SLOT(deleteLater())); - thread->start(); - - auto* dialog = - new WaitingDialog(QString::fromStdString(waiting_title), parent); - while (thread->isRunning()) { - QApplication::processEvents(); - } - dialog->close(); -} - void MainWindow::slotFileEncrypt() { auto fileTreeView = edit->slotCurPageFileTreeView(); auto path = fileTreeView->getSelected(); @@ -135,8 +89,8 @@ void MainWindow::slotFileEncrypt() { bool if_error = false; process_operation(this, tr("Encrypting").toStdString(), [&]() { try { - error = GpgFileOpera::GetInstance().EncryptFile( - std::move(*keys), path.toStdString(), result); + error = GpgFileOpera::EncryptFile(std::move(*keys), path.toStdString(), + result); } catch (const std::runtime_error& e) { if_error = true; } @@ -183,8 +137,7 @@ void MainWindow::slotFileDecrypt() { bool if_error = false; process_operation(this, tr("Decrypting").toStdString(), [&]() { try { - error = - GpgFileOpera::GetInstance().DecryptFile(path.toStdString(), result); + error = GpgFileOpera::DecryptFile(path.toStdString(), result); } catch (const std::runtime_error& e) { if_error = true; } @@ -244,8 +197,8 @@ void MainWindow::slotFileSign() { process_operation(this, tr("Signing").toStdString(), [&]() { try { - error = GpgFileOpera::GetInstance().SignFile(std::move(*keys), - path.toStdString(), result); + error = + GpgFileOpera::SignFile(std::move(*keys), path.toStdString(), result); } catch (const std::runtime_error& e) { if_error = true; } @@ -312,8 +265,7 @@ void MainWindow::slotFileVerify() { bool if_error = false; process_operation(this, tr("Verifying").toStdString(), [&]() { try { - error = GpgFileOpera::GetInstance().VerifyFile(dataFilePath.toStdString(), - result); + error = GpgFileOpera::VerifyFile(dataFilePath.toStdString(), result); } catch (const std::runtime_error& e) { if_error = true; } @@ -404,7 +356,7 @@ void MainWindow::slotFileEncryptSign() { process_operation(this, tr("Encrypting and Signing").toStdString(), [&]() { try { - error = GpgFileOpera::GetInstance().EncryptSignFile( + error = GpgFileOpera::EncryptSignFile( std::move(*keys), path.toStdString(), encr_result, sign_result); } catch (const std::runtime_error& e) { if_error = true; @@ -448,8 +400,8 @@ void MainWindow::slotFileDecryptVerify() { bool if_error = false; process_operation(this, tr("Decrypting and Verifying").toStdString(), [&]() { try { - error = GpgFileOpera::GetInstance().DecryptVerifyFile(path.toStdString(), - d_result, v_result); + error = GpgFileOpera::DecryptVerifyFile(path.toStdString(), d_result, + v_result); } catch (const std::runtime_error& e) { if_error = true; } diff --git a/src/ui/main_window/MainWindowSlotFunction.cpp b/src/ui/main_window/MainWindowSlotFunction.cpp index 9107e4d7..37150160 100644 --- a/src/ui/main_window/MainWindowSlotFunction.cpp +++ b/src/ui/main_window/MainWindowSlotFunction.cpp @@ -235,10 +235,11 @@ void MainWindow::slotVerify() { if (edit->tabCount() == 0) return; if (edit->slotCurPageTextEdit() != nullptr) { - QByteArray text = edit->curTextPage()->toPlainText().toUtf8(); + auto text = edit->curTextPage()->toPlainText().toUtf8(); // TODO(Saturneric) PreventNoDataErr - auto sig_buffer = std::make_unique<ByteArray>(nullptr); + auto sig_buffer = std::make_unique<ByteArray>(); + sig_buffer.reset(); GpgVerifyResult result = nullptr; GpgError error; @@ -303,10 +304,7 @@ void MainWindow::slotEncryptSign() { } } - QVector<GpgKey> signerKeys; - auto signersPicker = new SignersPicker(this); - QEventLoop loop; connect(signersPicker, SIGNAL(finished(int)), &loop, SLOT(quit())); loop.exec(); @@ -315,11 +313,11 @@ void MainWindow::slotEncryptSign() { auto signer_keys = GpgKeyGetter::GetInstance().GetKeys(key_ids); for (const auto& key : *keys) { - qDebug() << "Keys " << QString::fromStdString(key.email()); + LOG(INFO) << "Keys " << key.email(); } for (const auto& signer : *signer_keys) { - qDebug() << "Signers " << QString::fromStdString(signer.email()); + LOG(INFO) << "Signers " << signer.email(); } GpgEncrResult encr_result = nullptr; @@ -354,7 +352,7 @@ void MainWindow::slotEncryptSign() { } } #endif - + LOG(INFO) << "ResultAnalyse Started"; auto encrypt_res = EncryptResultAnalyse(error, std::move(encr_result)); auto sign_res = SignResultAnalyse(error, std::move(sign_result)); encrypt_res.analyse(); diff --git a/src/ui/widgets/EditorPage.cpp b/src/ui/widgets/EditorPage.cpp index 76db1b0b..26976048 100644 --- a/src/ui/widgets/EditorPage.cpp +++ b/src/ui/widgets/EditorPage.cpp @@ -24,6 +24,7 @@ #include "ui/widgets/EditorPage.h" +#include <boost/filesystem.hpp> #include <utility> namespace GpgFrontend::UI { @@ -40,21 +41,17 @@ EditorPage::EditorPage(QString filePath, QWidget* parent) mainLayout->addWidget(textPage); mainLayout->setContentsMargins(0, 0, 0, 0); setLayout(mainLayout); - - setAttribute(Qt::WA_DeleteOnClose); + textPage->setFocus(); // Front in same width this->setFont({"Courier"}); + this->setAttribute(Qt::WA_DeleteOnClose); } -const QString& EditorPage::getFilePath() const { - return fullFilePath; -} +const QString& EditorPage::getFilePath() const { return fullFilePath; } -QTextEdit* EditorPage::getTextPage() { - return textPage; -} +QTextEdit* EditorPage::getTextPage() { return textPage; } void EditorPage::setFilePath(const QString& filePath) { fullFilePath = filePath; @@ -108,4 +105,52 @@ void EditorPage::slotFormatGpgHeader() { cursor.setCharFormat(signFormat); } +void EditorPage::readFile() { + if (this->readThread != nullptr) { + this->readThread->requestInterruption(); + this->readThread = nullptr; + } + LOG(INFO) << "EditorPage::readFile Started"; + this->readThread = QThread::create([&]() { + LOG(INFO) << "EditorPage::readFile read_thread Started"; + boost::filesystem::path read_file_path(this->fullFilePath.toStdString()); + if (is_regular_file(read_file_path)) { + LOG(INFO) << "EditorPage::readFile read_thread Read Open"; + + auto fp = fopen(read_file_path.c_str(), "r"); + size_t read_size; + qDebug() << "Thread Start Reading"; + // Event loop + this->getTextPage()->setReadOnly(true); + char buffer[8192]; + while ((read_size = fread(buffer, sizeof(char), sizeof buffer, fp)) > 0) { + // Check isInterruptionRequested + if (QThread::currentThread()->isInterruptionRequested()) { + LOG(INFO) + << "EditorPage::readFile ReadThread isInterruptionRequested "; + fclose(fp); + return; + } + + LOG(INFO) << "EditorPage::readFile read_thread Read block size" + << read_size; + this->getTextPage()->insertPlainText( + QString::fromLocal8Bit(buffer, read_size)); + + QThread::msleep(32); + } + fclose(fp); + this->getTextPage()->setReadOnly(false); + LOG(INFO) << "Thread End Reading"; + } + }); + connect(this->readThread, SIGNAL(finished()), this->readThread, + SLOT(deleteLater())); + connect(this, &QWidget::destroyed, [&]() { + LOG(INFO) << "QWidget::destroyed RequestInterruption for readThread"; + this->readThread->requestInterruption(); + }); + this->readThread->start(); +} + } // namespace GpgFrontend::UI diff --git a/src/ui/widgets/EditorPage.h b/src/ui/widgets/EditorPage.h index 4d7d7ab6..0f637500 100644 --- a/src/ui/widgets/EditorPage.h +++ b/src/ui/widgets/EditorPage.h @@ -89,11 +89,14 @@ class EditorPage : public QWidget { const QString uuid = QUuid::createUuid().toString(); + void readFile(); + private: QTextEdit* textPage; /** The textedit of the tab */ QVBoxLayout* mainLayout; /** The layout for the tab */ QString fullFilePath; /** The path to the file handled in the tab */ bool signMarked{}; /** true, if the signed header is marked, false if not */ + QThread* readThread = nullptr; private slots: diff --git a/src/ui/widgets/FilePage.cpp b/src/ui/widgets/FilePage.cpp index 07d3b762..2cb59ae1 100644 --- a/src/ui/widgets/FilePage.cpp +++ b/src/ui/widgets/FilePage.cpp @@ -24,6 +24,8 @@ #include "ui/widgets/FilePage.h" +#include <boost/filesystem.hpp> + #include "MainWindow.h" namespace GpgFrontend::UI { @@ -43,7 +45,7 @@ FilePage::FilePage(QWidget* parent) : QWidget(parent) { dirTreeView->setIndentation(20); dirTreeView->setRootIndex(dirModel->index(QDir::currentPath())); dirTreeView->setContextMenuPolicy(Qt::CustomContextMenu); - mPath = dirModel->rootPath(); + mPath = boost::filesystem::path(dirModel->rootPath().toStdString()); createPopupMenu(); @@ -105,60 +107,61 @@ FilePage::FilePage(QWidget* parent) : QWidget(parent) { connect(dirTreeView, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(onCustomContextMenu(const QPoint&))); - emit pathChanged(mPath); + emit pathChanged(mPath.c_str()); } void FilePage::fileTreeViewItemClicked(const QModelIndex& index) { - selectedPath = dirModel->fileInfo(index).absoluteFilePath(); - qDebug() << "selectedPath" << selectedPath; + selectedPath = boost::filesystem::path( + dirModel->fileInfo(index).absoluteFilePath().toStdString()); + LOG(INFO) << "FilePage::fileTreeViewItemClicked selectedPath" << selectedPath; } void FilePage::slotUpLevel() { QModelIndex currentRoot = dirTreeView->rootIndex(); - mPath = dirModel->fileInfo(currentRoot).absoluteFilePath(); - QDir dir(mPath); - dir.makeAbsolute(); - dir.setPath(QDir::cleanPath(dir.filePath(QStringLiteral("..")))); - mPath = dir.absolutePath(); - auto fileInfo = QFileInfo(dir.absolutePath()); - if (fileInfo.isDir() && fileInfo.isReadable() && fileInfo.isExecutable()) { - pathEdit->setText(mPath); - slotGoPath(); + mPath = boost::filesystem::path( + dirModel->fileInfo(currentRoot).absoluteFilePath().toStdString()); + + if (mPath.has_parent_path()) { + mPath = mPath.parent_path(); + auto fileInfo = QFileInfo(mPath.c_str()); + if (fileInfo.isDir() && fileInfo.isReadable() && fileInfo.isExecutable()) { + pathEdit->setText(mPath.c_str()); + slotGoPath(); + } + LOG(INFO) << "FilePage::slotUpLevel Current Root mPath" << mPath; + emit pathChanged(mPath.c_str()); } - qDebug() << "Current Root mPath" << mPath; - emit pathChanged(mPath); } void FilePage::fileTreeViewItemDoubleClicked(const QModelIndex& index) { - mPath = dirModel->fileInfo(index).absoluteFilePath(); - auto fileInfo = QFileInfo(mPath); + mPath = boost::filesystem::path( + dirModel->fileInfo(index).absoluteFilePath().toStdString()); + auto fileInfo = QFileInfo(mPath.c_str()); auto targetModelIndex = dirTreeView->model()->index(index.row(), 0, index.parent()); if (fileInfo.isDir() && fileInfo.isReadable() && fileInfo.isExecutable()) { dirTreeView->setRootIndex(targetModelIndex); - pathEdit->setText(mPath); + pathEdit->setText(mPath.c_str()); } - qDebug() << "Index mPath" << mPath; - emit pathChanged(mPath); + LOG(INFO) << "FilePage::fileTreeViewItemDoubleClicked Index mPath" << mPath; + emit pathChanged(mPath.c_str()); } -QString FilePage::getSelected() const { - return selectedPath; -} +QString FilePage::getSelected() const { return selectedPath.c_str(); } void FilePage::slotGoPath() { qDebug() << "getSelected" << pathEdit->text(); auto fileInfo = QFileInfo(pathEdit->text()); if (fileInfo.isDir() && fileInfo.isReadable() && fileInfo.isExecutable()) { - mPath = fileInfo.filePath(); - qDebug() << "Set Path" << mPath; + mPath = boost::filesystem::path(fileInfo.filePath().toStdString()); + LOG(INFO) << "FilePage::slotGoPath Set Path" << mPath; dirTreeView->setRootIndex(dirModel->index(fileInfo.filePath())); } else { QMessageBox::critical(this, "Error", "The path is unprivileged or unreachable."); } - emit pathChanged(mPath); + emit pathChanged(mPath.c_str()); } void FilePage::createPopupMenu() { @@ -188,10 +191,11 @@ void FilePage::createPopupMenu() { void FilePage::onCustomContextMenu(const QPoint& point) { QModelIndex index = dirTreeView->indexAt(point); - selectedPath = dirModel->fileInfo(index).absoluteFilePath(); - qDebug() << "Right Click" << selectedPath; + selectedPath = boost::filesystem::path( + dirModel->fileInfo(index).absoluteFilePath().toStdString()); + LOG(INFO) << "FilePage::onCustomContextMenu Right Click" << selectedPath; if (index.isValid()) { - QFileInfo info(selectedPath); + QFileInfo info(selectedPath.c_str()); encryptItemAct->setEnabled( info.isFile() && (info.suffix() != "gpg" && info.suffix() != "sig")); decryptItemAct->setEnabled(info.isFile() && info.suffix() == "gpg"); @@ -205,11 +209,13 @@ void FilePage::onCustomContextMenu(const QPoint& point) { } void FilePage::slotOpenItem() { - QFileInfo info(mPath); + QFileInfo info(selectedPath.c_str()); if (info.isDir()) { - qDebug() << "getSelected" << pathEdit->text(); + LOG(INFO) << "FilePage::slotOpenItem getSelected" + << pathEdit->text().toStdString(); if (info.isReadable() && info.isExecutable()) { - qDebug() << "Set Path" << info.filePath(); + LOG(INFO) << "FilePage::slotOpenItem Set Path" + << info.filePath().toStdString(); dirTreeView->setRootIndex(dirModel->index(info.filePath())); } else { QMessageBox::critical(this, "Error", @@ -217,9 +223,9 @@ void FilePage::slotOpenItem() { } } else { auto mainWindow = qobject_cast<MainWindow*>(firstParent); - qDebug() << "Open Item" << mPath; - if (mainWindow != nullptr) - mainWindow->slotOpenFile(mPath); + LOG(INFO) << "FilePage::slotOpenItem Open Item" << selectedPath; + auto qt_path = QString::fromStdString(selectedPath.string()); + if (mainWindow != nullptr) mainWindow->slotOpenFile(qt_path); } } @@ -231,8 +237,7 @@ void FilePage::slotDeleteItem() { tr("Are you sure you want to delete it?"), QMessageBox::Ok | QMessageBox::Cancel); - if (ret == QMessageBox::Cancel) - return; + if (ret == QMessageBox::Cancel) return; qDebug() << "Delete Item" << data.toString(); @@ -244,26 +249,22 @@ void FilePage::slotDeleteItem() { void FilePage::slotEncryptItem() { auto mainWindow = qobject_cast<MainWindow*>(firstParent); - if (mainWindow != nullptr) - mainWindow->slotFileEncryptSign(); + if (mainWindow != nullptr) mainWindow->slotFileEncryptSign(); } void FilePage::slotDecryptItem() { auto mainWindow = qobject_cast<MainWindow*>(firstParent); - if (mainWindow != nullptr) - mainWindow->slotFileDecryptVerify(); + if (mainWindow != nullptr) mainWindow->slotFileDecryptVerify(); } void FilePage::slotSignItem() { auto mainWindow = qobject_cast<MainWindow*>(firstParent); - if (mainWindow != nullptr) - mainWindow->slotFileSign(); + if (mainWindow != nullptr) mainWindow->slotFileSign(); } void FilePage::slotVerifyItem() { auto mainWindow = qobject_cast<MainWindow*>(firstParent); - if (mainWindow != nullptr) - mainWindow->slotFileVerify(); + if (mainWindow != nullptr) mainWindow->slotFileVerify(); } void FilePage::keyPressEvent(QKeyEvent* event) { diff --git a/src/ui/widgets/FilePage.h b/src/ui/widgets/FilePage.h index be7ceea7..9fd4308e 100644 --- a/src/ui/widgets/FilePage.h +++ b/src/ui/widgets/FilePage.h @@ -25,6 +25,8 @@ #ifndef GPGFRONTEND_FILEPAGE_H #define GPGFRONTEND_FILEPAGE_H +#include <boost/filesystem.hpp> + #include "ui/GpgFrontendUI.h" namespace GpgFrontend::UI { @@ -65,8 +67,10 @@ class FilePage : public QWidget { QFileSystemModel* dirModel; QTreeView* dirTreeView; QLineEdit* pathEdit; - QString mPath; - QString selectedPath; + + // using boost path + boost::filesystem::path mPath; + boost::filesystem::path selectedPath; QPushButton* upLevelButton; QPushButton* goPathButton; diff --git a/src/ui/widgets/TextEdit.cpp b/src/ui/widgets/TextEdit.cpp index db60cca7..aaac98ee 100644 --- a/src/ui/widgets/TextEdit.cpp +++ b/src/ui/widgets/TextEdit.cpp @@ -41,12 +41,6 @@ TextEdit::TextEdit(QWidget* parent) : QWidget(parent) { connect(tabWidget, SIGNAL(tabCloseRequested(int)), this, SLOT(removeTab(int))); - connect(this, &TextEdit::insertTargetTextPage, this, - &TextEdit::slotInsertTargetTextPage); - connect(this, &TextEdit::readTargetTextPageStart, this, - &TextEdit::slotReadTargetTextPageStart); - connect(this, &TextEdit::readTargetTextPageDone, this, - &TextEdit::slotReadTargetTextPageDone); slotNewTab(); setAcceptDrops(false); } @@ -78,95 +72,23 @@ void TextEdit::slotNewFileTab() const { void TextEdit::slotOpenFile(QString& path) { QFile file(path); - - if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { + LOG(INFO) << "TextEdit::slotOpenFile path" << path.toStdString(); + auto result = file.open(QIODevice::ReadOnly | QIODevice::Text); + if (result) { auto* page = new EditorPage(path); - pagesHashMap.insert(page->uuid, page); QApplication::setOverrideCursor(Qt::WaitCursor); - - auto read_thread = QThread::create([&, page]() { - QFile targetFile(path); - - if (targetFile.open(QIODevice::ReadOnly | QIODevice::Text)) { - emit readTargetTextPageStart(page->uuid); - QTextStream in(&targetFile); - QString readText; - qDebug() << "Thread Start Reading" << path; - while (!((readText = in.read(8192)).isEmpty())) { - emit insertTargetTextPage(page->uuid, readText); - QThread::msleep(64); - } - targetFile.close(); - emit readTargetTextPageDone(page->uuid); - qDebug() << "Thread End Reading" << path; - } - }); - - page->setFilePath(path); - QTextDocument* document = page->getTextPage()->document(); - document->setModified(false); - tabWidget->addTab(page, strippedName(path)); tabWidget->setCurrentIndex(tabWidget->count() - 1); QApplication::restoreOverrideCursor(); page->getTextPage()->setFocus(); - - file.close(); - read_thread->start(); - + page->readFile(); } else { QMessageBox::warning( this, tr("Warning"), tr("Cannot read file %1:\n%2.").arg(path).arg(file.errorString())); } -} -void TextEdit::slotInsertTargetTextPage(const QString& pagePtr, - const QString& text) { - auto it = pagesHashMap.find(pagePtr); - if (it != pagesHashMap.end()) { - auto* taregtTextPage = qobject_cast<EditorPage*>(it.value()); - if (taregtTextPage != nullptr) { - taregtTextPage->getTextPage()->insertPlainText(text); - taregtTextPage->getTextPage()->document()->setModified(false); - } - } -} - -void TextEdit::slotReadTargetTextPageStart(const QString& pagePtr) { - auto it = pagesHashMap.find(pagePtr); - if (it != pagesHashMap.end()) { - auto* taregtTextPage = qobject_cast<EditorPage*>(it.value()); - if (taregtTextPage != nullptr) { - qDebug() << "Setting TextEdit At Start" << pagePtr; - taregtTextPage->getTextPage()->setReadOnly(true); - auto index = tabWidget->indexOf(taregtTextPage); - if (index != -1) { - tabWidget->setTabText( - index, "[Loading] " + strippedName(taregtTextPage->getFilePath())); - } - } - } -} - -void TextEdit::slotReadTargetTextPageDone(const QString& pagePtr) { - auto it = pagesHashMap.find(pagePtr); - if (it != pagesHashMap.end()) { - auto* taregtTextPage = qobject_cast<EditorPage*>(it.value()); - if (taregtTextPage != nullptr) { - qDebug() << "Setting TextEdit At End" << pagePtr; - taregtTextPage->getTextPage()->setReadOnly(false); - auto index = tabWidget->indexOf(taregtTextPage); - if (index != -1) { - tabWidget->setTabText(index, - strippedName(taregtTextPage->getFilePath())); - } - taregtTextPage->getTextPage()->document()->setModified(false); - connect(taregtTextPage->getTextPage()->document(), - SIGNAL(modificationChanged(bool)), this, - SLOT(slotShowModified())); - } - } + file.close(); } void TextEdit::slotOpen() { @@ -254,7 +176,7 @@ bool TextEdit::saveFile(const QString& fileName) { } bool TextEdit::slotSaveAs() { - if (tabWidget->count() == 0 || slotCurPageTextEdit() == 0) { + if (tabWidget->count() == 0 || slotCurPageTextEdit() == nullptr) { return true; } @@ -430,9 +352,7 @@ FilePage* TextEdit::curFilePage() const { } } -int TextEdit::tabCount() const { - return tabWidget->count(); -} +int TextEdit::tabCount() const { return tabWidget->count(); } EditorPage* TextEdit::slotCurPageTextEdit() const { auto* curPage = qobject_cast<EditorPage*>(tabWidget->currentWidget()); @@ -633,7 +553,7 @@ void TextEdit::slotSelectAll() const { curTextPage()->selectAll(); } -void TextEdit::slotFilePagePathChanged(const QString& path) { +void TextEdit::slotFilePagePathChanged(const QString& path) const { int index = tabWidget->currentIndex(); QString mPath; QFileInfo fileInfo(path); @@ -644,7 +564,6 @@ void TextEdit::slotFilePagePathChanged(const QString& path) { mPath = tPath; } mPath.prepend("[Browser] "); - mPath.append("/"); tabWidget->setTabText(index, mPath); } diff --git a/src/ui/widgets/TextEdit.h b/src/ui/widgets/TextEdit.h index 91bacf65..beebc8c4 100644 --- a/src/ui/widgets/TextEdit.h +++ b/src/ui/widgets/TextEdit.h @@ -178,23 +178,6 @@ class TextEdit : public QWidget { */ void slotSwitchTabDown() const; - /** - * @details Insert text in target Text Edit - */ - void slotInsertTargetTextPage(const QString& pagePtr, const QString& text); - - void slotReadTargetTextPageStart(const QString& pageStr); - - void slotReadTargetTextPageDone(const QString& pagePtr); - - signals: - - void readTargetTextPageStart(const QString& pagePtr); - - void insertTargetTextPage(const QString& pagePtr, const QString& text); - - void readTargetTextPageDone(const QString& pagePtr); - private: /** * @details return just a filename stripped of a whole path @@ -217,11 +200,9 @@ class TextEdit : public QWidget { */ int countPage; /* TODO */ - QHash<const QString, QWidget*> pagesHashMap; - private slots: - void slotFilePagePathChanged(const QString& path); + void slotFilePagePathChanged(const QString& path) const; /** * @details Remove the tab with given index |