aboutsummaryrefslogtreecommitdiffstats
path: root/src/ui/widgets/PlainTextEditorPage.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/ui/widgets/PlainTextEditorPage.cpp')
-rw-r--r--src/ui/widgets/PlainTextEditorPage.cpp179
1 files changed, 40 insertions, 139 deletions
diff --git a/src/ui/widgets/PlainTextEditorPage.cpp b/src/ui/widgets/PlainTextEditorPage.cpp
index c9a65a4c..7d951006 100644
--- a/src/ui/widgets/PlainTextEditorPage.cpp
+++ b/src/ui/widgets/PlainTextEditorPage.cpp
@@ -1,5 +1,5 @@
/**
- * Copyright (C) 2021 Saturneric
+ * Copyright (C) 2021 Saturneric <[email protected]>
*
* This file is part of GpgFrontend.
*
@@ -19,56 +19,49 @@
* The initial version of the source code is inherited from
* the gpg4usb project, which is under GPL-3.0-or-later.
*
- * The source code version of this software was modified and released
- * by Saturneric<[email protected]><[email protected]> starting on May 12, 2021.
+ * All the source code of GpgFrontend was modified and released by
+ * Saturneric <[email protected]> starting on May 12, 2021.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
*
*/
#include "PlainTextEditorPage.h"
-#include <boost/format.hpp>
-#include <string>
-#include <utility>
-
-#include "core/function/CharsetOperator.h"
#include "core/thread/FileReadTask.h"
-#include "core/thread/Task.h"
#include "core/thread/TaskRunnerGetter.h"
#include "ui/struct/SettingsObject.h"
+#include "ui/struct/settings/AppearanceSO.h"
#include "ui_PlainTextEditor.h"
namespace GpgFrontend::UI {
PlainTextEditorPage::PlainTextEditorPage(QString file_path, QWidget *parent)
: QWidget(parent),
- ui_(std::make_shared<Ui_PlainTextEditor>()),
+ ui_(GpgFrontend::SecureCreateSharedObject<Ui_PlainTextEditor>()),
full_file_path_(std::move(file_path)) {
ui_->setupUi(this);
ui_->textPage->setFocus();
ui_->loadingLabel->setHidden(true);
- // Front in same width
- SettingsObject general_settings_state("general_settings_state");
-
// font size
- auto editor_font_size =
- general_settings_state.Check("text_editor").Check("font_size", 10);
- ui_->textPage->setFont(QFont("Courier", editor_font_size));
+ AppearanceSO appearance(SettingsObject("general_settings_state"));
+ ui_->textPage->setFont(QFont("Courier", appearance.text_editor_font_size));
this->setAttribute(Qt::WA_DeleteOnClose);
- this->ui_->characterLabel->setText(_("0 character"));
- this->ui_->lfLabel->setText(_("lf"));
- this->ui_->encodingLabel->setText(_("utf-8"));
+ this->ui_->characterLabel->setText(tr("0 character"));
+ this->ui_->lfLabel->setHidden(true);
+ this->ui_->encodingLabel->setText("Unicode");
connect(ui_->textPage, &QPlainTextEdit::textChanged, this, [=]() {
// if file is loading
if (!read_done_) return;
auto text = ui_->textPage->document()->toPlainText();
- auto str = boost::format(_("%1% character(s)")) % text.size();
- this->ui_->characterLabel->setText(str.str().c_str());
+ auto str = tr("%1 character(s)").arg(text.size());
+ this->ui_->characterLabel->setText(str);
});
if (full_file_path_.isEmpty()) {
@@ -76,7 +69,7 @@ PlainTextEditorPage::PlainTextEditorPage(QString file_path, QWidget *parent)
ui_->loadingLabel->setHidden(true);
} else {
read_done_ = false;
- ui_->loadingLabel->setText(_("Loading..."));
+ ui_->loadingLabel->setText(tr("Loading..."));
ui_->loadingLabel->setHidden(false);
}
}
@@ -87,24 +80,11 @@ const QString &PlainTextEditorPage::GetFilePath() const {
QPlainTextEdit *PlainTextEditorPage::GetTextPage() { return ui_->textPage; }
-bool PlainTextEditorPage::WillCharsetChange() const {
- // detect if the line-ending will change
- if (is_crlf_) return true;
-
- // detect if the charset of the file will change
- if (charset_name_ != "UTF-8" && charset_name_ != "ISO-8859-1")
- return true;
- else
- return false;
-}
-
void PlainTextEditorPage::NotifyFileSaved() {
this->is_crlf_ = false;
- this->charset_confidence_ = 100;
- this->charset_name_ = "UTF-8";
- this->ui_->lfLabel->setText(_("lf"));
- this->ui_->encodingLabel->setText(_("UTF-8"));
+ this->ui_->lfLabel->setText(tr("lf"));
+ this->ui_->encodingLabel->setText(tr("UTF-8"));
}
void PlainTextEditorPage::SetFilePath(const QString &filePath) {
@@ -130,10 +110,9 @@ void PlainTextEditorPage::slot_format_gpg_header() {
QString content = ui_->textPage->toPlainText();
// Get positions of the gpg-headers, if they exist
- int start = content.indexOf(GpgFrontend::GpgConstants::PGP_SIGNED_BEGIN);
- int startSig =
- content.indexOf(GpgFrontend::GpgConstants::PGP_SIGNATURE_BEGIN);
- int endSig = content.indexOf(GpgFrontend::GpgConstants::PGP_SIGNATURE_END);
+ int start = content.indexOf(GpgFrontend::PGP_SIGNED_BEGIN);
+ int startSig = content.indexOf(GpgFrontend::PGP_SIGNATURE_BEGIN);
+ int endSig = content.indexOf(GpgFrontend::PGP_SIGNATURE_END);
if (start < 0 || startSig < 0 || endSig < 0 || sign_marked_) {
return;
@@ -168,12 +147,12 @@ void PlainTextEditorPage::ReadFile() {
ui_->loadingLabel->setHidden(false);
ui_->textPage->document()->blockSignals(true);
- auto text_page = this->GetTextPage();
+ auto *text_page = this->GetTextPage();
text_page->setReadOnly(true);
- const auto target_path = this->full_file_path_.toStdString();
+ const auto target_path = this->full_file_path_;
- auto *task_runner =
+ auto task_runner =
GpgFrontend::Thread::TaskRunnerGetter::GetInstance().GetTaskRunner();
auto *read_task = new FileReadTask(target_path);
@@ -182,13 +161,12 @@ void PlainTextEditorPage::ReadFile() {
connect(this, &PlainTextEditorPage::SignalUIBytesDisplayed, read_task,
&FileReadTask::SignalFileBytesReadNext, Qt::QueuedConnection);
- connect(read_task, &FileReadTask::SignalTaskRunnableEnd, this,
- []() { SPDLOG_DEBUG("read thread closed"); });
+ connect(read_task, &FileReadTask::SignalTaskShouldEnd, this,
+ []() { GF_UI_LOG_DEBUG("read thread closed"); });
connect(this, &PlainTextEditorPage::close, read_task,
- [=]() { read_task->SignalTaskRunnableEnd(0); });
+ [=]() { read_task->SignalTaskShouldEnd(0); });
connect(read_task, &FileReadTask::SignalFileBytesReadEnd, this, [=]() {
// set the UI
- if (!binary_mode_) text_page->setReadOnly(false);
this->read_done_ = true;
this->ui_->textPage->setEnabled(true);
text_page->document()->setModified(false);
@@ -200,104 +178,27 @@ void PlainTextEditorPage::ReadFile() {
task_runner->PostTask(read_task);
}
-std::string binary_to_string(const std::string &source) {
- static char syms[] = "0123456789ABCDEF";
- std::stringstream ss;
- for (unsigned char c : source)
- ss << syms[((c >> 4) & 0xf)] << syms[c & 0xf] << " ";
- return ss.str();
+auto BinaryToString(const QByteArray &source) -> QString {
+ static const char kSyms[] = "0123456789ABCDEF";
+ QString buffer;
+ QTextStream ss(&buffer);
+ for (auto c : source) ss << kSyms[((c >> 4) & 0xf)] << kSyms[c & 0xf] << " ";
+ return buffer;
}
void PlainTextEditorPage::slot_insert_text(QByteArray bytes_data) {
- std::string data = bytes_data.toStdString();
- SPDLOG_DEBUG("data size: {}", data.size());
- read_bytes_ += data.size();
- // If binary format is detected, the entire file is converted to binary
- // format for display.
- bool if_last_binary_mode = binary_mode_;
- if (!binary_mode_ && !read_done_) {
- detect_encoding(data);
- }
+ GF_UI_LOG_TRACE("inserting data read to editor, data size: {}",
+ bytes_data.size());
+ read_bytes_ += bytes_data.size();
- if (binary_mode_) {
- // change formery displayed text to binary format
- if (if_last_binary_mode != binary_mode_) {
- auto text_buffer =
- ui_->textPage->document()->toRawText().toLocal8Bit().toStdString();
- ui_->textPage->clear();
- this->GetTextPage()->insertPlainText(
- binary_to_string(text_buffer).c_str());
- this->ui_->lfLabel->setText("None");
- }
+ // insert the text to the text page
+ this->GetTextPage()->insertPlainText(bytes_data);
- // insert new data
- this->GetTextPage()->insertPlainText(binary_to_string(data).c_str());
+ auto text = this->GetTextPage()->toPlainText();
+ auto str = tr("%1 character(s)").arg(text.size());
+ this->ui_->characterLabel->setText(str);
- // update the size of the file
- auto str = boost::format(_("%1% byte(s)")) % read_bytes_;
- this->ui_->characterLabel->setText(str.str().c_str());
- } else {
- // detect crlf/lf line ending
- detect_cr_lf(data);
-
- // when reding from a text file
- // try convert the any of thetext to utf8
- std::string utf8_data;
- if (!read_done_ && charset_confidence_ > 25) {
- CharsetOperator::Convert2Utf8(data, utf8_data, charset_name_);
- } else {
- // when editing a text file, do nothing.
- utf8_data = data;
- }
-
- // insert the text to the text page
- this->GetTextPage()->insertPlainText(utf8_data.c_str());
-
- auto text = this->GetTextPage()->toPlainText();
- auto str = boost::format(_("%1% character(s)")) % text.size();
- this->ui_->characterLabel->setText(str.str().c_str());
- }
QTimer::singleShot(25, this, &PlainTextEditorPage::SignalUIBytesDisplayed);
}
-void PlainTextEditorPage::detect_encoding(const std::string &data) {
- // skip the binary data to avoid the false detection of the encoding
- if (binary_mode_) return;
-
- // detect the encoding
- auto charset = CharsetOperator::Detect(data);
- this->charset_name_ = std::get<0>(charset).c_str();
- this->language_name_ = std::get<1>(charset).c_str();
- this->charset_confidence_ = std::get<2>(charset);
-
- // probably there is no need to detect the encoding again
- if (this->charset_confidence_ < 10) {
- binary_mode_ = true;
- }
-
- if (binary_mode_) {
- // hide the line ending label, when the file is binary
- this->ui_->lfLabel->setHidden(true);
- this->ui_->encodingLabel->setText(_("binary"));
- } else {
- ui_->encodingLabel->setText(this->charset_name_.c_str());
- }
-}
-
-void PlainTextEditorPage::detect_cr_lf(const std::string &data) {
- if (binary_mode_) {
- return;
- }
-
- // if contain crlf, set the label to crlf
- if (is_crlf_) return;
-
- if (data.find("\r\n") != std::string::npos) {
- this->ui_->lfLabel->setText("crlf");
- is_crlf_ = true;
- } else {
- this->ui_->lfLabel->setText("lf");
- }
-}
-
} // namespace GpgFrontend::UI