1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
/**
* 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]><[email protected]> starting on May 12, 2021.
*
*/
#include "ui/widgets/PlainTextEditorPage.h"
#include <boost/filesystem.hpp>
#include <boost/format.hpp>
#include <utility>
#include "ui/encoding/TextEncodingDetect.h"
#include "ui/thread/FileReadThread.h"
#include "ui_PlainTextEditor.h"
namespace GpgFrontend::UI {
PlainTextEditorPage::PlainTextEditorPage(QString filePath, QWidget* parent)
: QWidget(parent),
ui(std::make_shared<Ui_PlainTextEditor>()),
full_file_path_(std::move(filePath)) {
ui->setupUi(this);
if (full_file_path_.isEmpty()) read_done_ = true;
ui->textPage->setFocus();
ui->loadingLabel->setHidden(true);
// Front in same width
this->setFont({"Courier"});
this->setAttribute(Qt::WA_DeleteOnClose);
this->ui->characterLabel->setText(_("0 character"));
this->ui->lfLabel->setText(_("None"));
this->ui->encodingLabel->setText(_("Binary"));
connect(ui->textPage, &QPlainTextEdit::textChanged, this, [=]() {
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());
detect_encoding(text.toStdString());
});
ui->loadingLabel->setText(_("Loading..."));
}
const QString& PlainTextEditorPage::getFilePath() const {
return full_file_path_;
}
QPlainTextEdit* PlainTextEditorPage::getTextPage() { return ui->textPage; }
void PlainTextEditorPage::setFilePath(const QString& filePath) {
full_file_path_ = filePath;
}
void PlainTextEditorPage::showNotificationWidget(QWidget* widget,
const char* className) {
widget->setProperty(className, true);
ui->verticalLayout->addWidget(widget);
}
void PlainTextEditorPage::closeNoteByClass(const char* className) {
QList<QWidget*> widgets = findChildren<QWidget*>();
for (QWidget* widget : widgets) {
if (widget->property(className) == true) {
widget->close();
}
}
}
void PlainTextEditorPage::slotFormatGpgHeader() {
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);
if (start < 0 || startSig < 0 || endSig < 0 || signMarked) {
return;
}
signMarked = true;
// Set the fontstyle for the header
QTextCharFormat signFormat;
signFormat.setForeground(QBrush(QColor::fromRgb(80, 80, 80)));
signFormat.setFontPointSize(9);
// set font style for the signature
QTextCursor cursor(ui->textPage->document());
cursor.setPosition(startSig, QTextCursor::MoveAnchor);
cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, endSig);
cursor.setCharFormat(signFormat);
// set the font style for the header
int headEnd = content.indexOf("\n\n", start);
cursor.setPosition(start, QTextCursor::MoveAnchor);
cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, headEnd);
cursor.setCharFormat(signFormat);
}
void PlainTextEditorPage::ReadFile() {
LOG(INFO) << "called";
read_done_ = false;
read_bytes_ = 0;
ui->textPage->setEnabled(false);
ui->textPage->setReadOnly(true);
ui->textPage->blockSignals(true);
ui->loadingLabel->setHidden(false);
ui->textPage->document()->blockSignals(true);
auto text_page = this->getTextPage();
text_page->setReadOnly(true);
auto thread = new FileReadThread(this->full_file_path_.toStdString());
connect(thread, &FileReadThread::sendReadBlock, this,
&PlainTextEditorPage::slotInsertText);
connect(thread, &FileReadThread::readDone, this, [=]() {
LOG(INFO) << "thread read done";
if (!binary_mode_) {
text_page->setReadOnly(false);
}
});
connect(thread, &FileReadThread::finished, this, [=]() {
LOG(INFO) << "thread finished";
thread->deleteLater();
read_done_ = true;
read_thread_ = nullptr;
ui->textPage->setEnabled(true);
text_page->document()->setModified(false);
ui->textPage->blockSignals(false);
ui->textPage->document()->blockSignals(false);
ui->loadingLabel->setHidden(true);
});
connect(this, &PlainTextEditorPage::destroyed, [=]() {
LOG(INFO) << "request interruption for read thread";
thread->requestInterruption();
read_thread_ = nullptr;
});
this->read_thread_ = thread;
thread->start();
}
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();
}
void PlainTextEditorPage::slotInsertText(const std::string& data) {
LOG(INFO) << "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_) {
detect_encoding(data);
}
if (binary_mode_) {
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");
}
this->getTextPage()->insertPlainText(binary_to_string(data).c_str());
auto str = boost::format(_("%1% byte(s)")) % read_bytes_;
this->ui->characterLabel->setText(str.str().c_str());
} else {
this->getTextPage()->insertPlainText(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());
detect_cr_lf(text);
}
}
void PlainTextEditorPage::PrepareToDestroy() {
if (read_thread_) {
read_thread_->requestInterruption();
read_thread_ = nullptr;
}
}
void PlainTextEditorPage::detect_encoding(const std::string& data) {
AutoIt::Common::TextEncodingDetect text_detect;
AutoIt::Common::TextEncodingDetect::Encoding encoding =
text_detect.DetectEncoding((unsigned char*)(data.data()), data.size());
if (encoding == AutoIt::Common::TextEncodingDetect::None) {
binary_mode_ = true;
ui->encodingLabel->setText(_("Binary"));
} else if (encoding == AutoIt::Common::TextEncodingDetect::ASCII) {
ui->encodingLabel->setText(_("ASCII(7 bits)"));
} else if (encoding == AutoIt::Common::TextEncodingDetect::ANSI) {
ui->encodingLabel->setText(_("ASCII(8 bits)"));
} else if (encoding == AutoIt::Common::TextEncodingDetect::UTF8_BOM ||
encoding == AutoIt::Common::TextEncodingDetect::UTF8_NOBOM) {
ui->encodingLabel->setText(_("UTF-8"));
} else if (encoding == AutoIt::Common::TextEncodingDetect::UTF16_LE_BOM ||
encoding == AutoIt::Common::TextEncodingDetect::UTF16_LE_NOBOM) {
ui->encodingLabel->setText(_("UTF-16"));
} else if (encoding == AutoIt::Common::TextEncodingDetect::UTF16_BE_BOM ||
encoding == AutoIt::Common::TextEncodingDetect::UTF16_BE_NOBOM) {
ui->encodingLabel->setText(_("UTF-16(BE)"));
}
}
void PlainTextEditorPage::detect_cr_lf(const QString& data) {
if (binary_mode_) {
this->ui->lfLabel->setText("None");
return;
}
if (data.contains("\r\n")) {
this->ui->lfLabel->setText("CRLF");
} else {
this->ui->lfLabel->setText("LF");
}
}
} // namespace GpgFrontend::UI
|