diff options
| -rw-r--r-- | src/core/function/gpg/GpgContext.cpp | 78 | ||||
| -rw-r--r-- | src/ui/widgets/PlainTextEditorPage.cpp | 20 |
2 files changed, 77 insertions, 21 deletions
diff --git a/src/core/function/gpg/GpgContext.cpp b/src/core/function/gpg/GpgContext.cpp index ca0be2c4..4243335c 100644 --- a/src/core/function/gpg/GpgContext.cpp +++ b/src/core/function/gpg/GpgContext.cpp @@ -158,8 +158,8 @@ class GpgContext::Impl { [[nodiscard]] auto Good() const -> bool { return good_; } - auto SetPassphraseCb(const gpgme_ctx_t &ctx, - gpgme_passphrase_cb_t cb) -> bool { + auto SetPassphraseCb(const gpgme_ctx_t &ctx, gpgme_passphrase_cb_t cb) + -> bool { if (gpgme_get_pinentry_mode(ctx) != GPGME_PINENTRY_MODE_LOOPBACK) { if (CheckGpgError(gpgme_set_pinentry_mode( ctx, GPGME_PINENTRY_MODE_LOOPBACK)) != GPG_ERR_NO_ERROR) { @@ -252,8 +252,8 @@ class GpgContext::Impl { return res == static_cast<ssize_t>(pass_size + 1) ? 0 : GPG_ERR_CANCELED; } - static auto TestStatusCb(void *hook, const char *keyword, - const char *args) -> gpgme_error_t { + static auto TestStatusCb(void *hook, const char *keyword, const char *args) + -> gpgme_error_t { FLOG_D("keyword %s", keyword); return GPG_ERR_NO_ERROR; } @@ -281,9 +281,11 @@ class GpgContext::Impl { private: GpgContext *parent_; - GpgContextInitArgs args_{}; ///< - gpgme_ctx_t ctx_ref_ = nullptr; ///< - gpgme_ctx_t binary_ctx_ref_ = nullptr; ///< + GpgContextInitArgs args_{}; ///< + gpgme_ctx_t ctx_ref_ = nullptr; ///< + gpgme_ctx_t binary_ctx_ref_ = nullptr; ///< + gpgme_ctx_t cms_ctx_ref_ = nullptr; ///< + gpgme_ctx_t cms_binary_ctx_ref_ = nullptr; ///< bool good_ = true; std::mutex ctx_ref_lock_; @@ -305,7 +307,8 @@ class GpgContext::Impl { kill_gpg_agent(); good_ = launch_gpg_agent() && default_ctx_initialize(args) && - binary_ctx_initialize(args); + binary_ctx_initialize(args) && cms_default_ctx_initialize(args) && + cms_binary_ctx_initialize(args); } static auto component_type_to_q_string(GpgComponentType type) -> QString { @@ -339,8 +342,7 @@ class GpgContext::Impl { return CheckGpgError(gpgme_set_keylist_mode( ctx, GPGME_KEYLIST_MODE_LOCAL | GPGME_KEYLIST_MODE_WITH_SECRET | GPGME_KEYLIST_MODE_SIGS | - GPGME_KEYLIST_MODE_SIG_NOTATIONS | - GPGME_KEYLIST_MODE_WITH_TOFU)) == GPG_ERR_NO_ERROR; + GPGME_KEYLIST_MODE_SIG_NOTATIONS)) == GPG_ERR_NO_ERROR; } auto set_ctx_openpgp_engine_info(gpgme_ctx_t ctx) -> bool { @@ -437,6 +439,62 @@ class GpgContext::Impl { return true; } + auto cms_default_ctx_initialize(const GpgContextInitArgs &args) -> bool { + gpgme_ctx_t p_ctx; + if (auto err = CheckGpgError(gpgme_new(&p_ctx)); err != GPG_ERR_NO_ERROR) { + LOG_W() << "get new gpg context error: " + << DescribeGpgErrCode(err).second; + return false; + } + + assert(p_ctx != nullptr); + cms_ctx_ref_ = p_ctx; + + if (auto err = + CheckGpgError(gpgme_set_protocol(cms_ctx_ref_, GPGME_PROTOCOL_CMS)); + err != GPG_ERR_NO_ERROR) { + LOG_W() << "get new gpg context error: " + << DescribeGpgErrCode(err).second; + return false; + } + + if (!common_ctx_initialize(cms_ctx_ref_, args)) { + FLOG_W("get new ctx failed, binary"); + return false; + } + + gpgme_set_armor(cms_ctx_ref_, 1); + return true; + } + + auto cms_binary_ctx_initialize(const GpgContextInitArgs &args) -> bool { + gpgme_ctx_t p_ctx; + if (auto err = CheckGpgError(gpgme_new(&p_ctx)); err != GPG_ERR_NO_ERROR) { + LOG_W() << "get new gpg context error: " + << DescribeGpgErrCode(err).second; + return false; + } + + assert(p_ctx != nullptr); + cms_binary_ctx_ref_ = p_ctx; + + if (auto err = CheckGpgError( + gpgme_set_protocol(cms_binary_ctx_ref_, GPGME_PROTOCOL_CMS)); + err != GPG_ERR_NO_ERROR) { + LOG_W() << "get new gpg context error: " + << DescribeGpgErrCode(err).second; + return false; + } + + if (!common_ctx_initialize(cms_binary_ctx_ref_, args)) { + FLOG_W("get new ctx failed, binary"); + return false; + } + + gpgme_set_armor(cms_binary_ctx_ref_, 0); + return true; + } + auto binary_ctx_initialize(const GpgContextInitArgs &args) -> bool { gpgme_ctx_t p_ctx; if (auto err = CheckGpgError(gpgme_new(&p_ctx)); err != GPG_ERR_NO_ERROR) { diff --git a/src/ui/widgets/PlainTextEditorPage.cpp b/src/ui/widgets/PlainTextEditorPage.cpp index 232d5195..cdb81590 100644 --- a/src/ui/widgets/PlainTextEditorPage.cpp +++ b/src/ui/widgets/PlainTextEditorPage.cpp @@ -78,17 +78,7 @@ PlainTextEditorPage::PlainTextEditorPage(QString file_path, QWidget *parent) } void PlainTextEditorPage::closeEvent(QCloseEvent *event) { - // clean up - if (ui_ && (ui_->textPage != nullptr)) { - auto text = ui_->textPage->toPlainText(); - if (!text.isEmpty()) { - ui_->textPage->setPlainText(QString(text.size(), QChar(0x2022))); - text.fill(QLatin1Char('X')); - } - ui_->textPage->setUndoRedoEnabled(false); - ui_->textPage->setUndoRedoEnabled(true); - ui_->textPage->setPlainText(QString()); - } + if (ui_ && (ui_->textPage != nullptr)) Clear(); QWidget::closeEvent(event); } @@ -243,6 +233,14 @@ void PlainTextEditorPage::slot_insert_text(QByteArray bytes_data) { auto PlainTextEditorPage::ReadDone() const -> bool { return this->read_done_; } void PlainTextEditorPage::Clear() { + // If the text page is not empty, we will clear it to avoid memory leak. + auto text = ui_->textPage->toPlainText(); + if (!text.isEmpty()) { + // Fill the text page with bullet characters to avoid memory leak. + ui_->textPage->setPlainText(QString(text.size(), QChar(0x2022))); + text.fill(QLatin1Char('X')); + } + this->ui_->textPage->clear(); this->ui_->textPage->setUndoRedoEnabled(false); this->ui_->textPage->setUndoRedoEnabled(true); |
