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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
/**
* Copyright (C) 2021 Saturneric <[email protected]>
*
* 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.
*
* GpgFrontend 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 GpgFrontend. If not, see <https://www.gnu.org/licenses/>.
*
* The initial version of the source code is inherited from
* the gpg4usb project, which is under GPL-3.0-or-later.
*
* 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 <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/posix_time/posix_time_io.hpp>
#include <cstddef>
#include "MainWindow.h"
#include "core/GpgConstants.h"
#include "core/GpgModel.h"
#include "core/function/gpg/GpgKeyGetter.h"
#include "core/function/gpg/GpgKeyImportExporter.h"
#include "core/function/gpg/GpgKeyManager.h"
#include "core/module/ModuleManager.h"
#include "core/typedef/GpgTypedef.h"
#include "core/utils/CommonUtils.h"
#include "core/utils/GpgUtils.h"
#include "ui/UserInterfaceUtils.h"
#include "ui/dialog/help/AboutDialog.h"
#include "ui/dialog/import_export/KeyUploadDialog.h"
#include "ui/dialog/keypair_details/KeyDetailsDialog.h"
#include "ui/function/SetOwnerTrustLevel.h"
#include "ui/widgets/FindWidget.h"
namespace GpgFrontend::UI {
void MainWindow::slot_find() {
if (edit_->TabCount() == 0 || edit_->CurTextPage() == nullptr) {
return;
}
// At first close verifynotification, if existing
edit_->SlotCurPageTextEdit()->CloseNoteByClass("findwidget");
auto* fw = new FindWidget(this, edit_->CurTextPage());
edit_->SlotCurPageTextEdit()->ShowNotificationWidget(fw, "findWidget");
}
/*
* Append the selected (not checked!) Key(s) To Textedit
*/
void MainWindow::slot_append_selected_keys() {
if (edit_->TabCount() == 0 || edit_->SlotCurPageTextEdit() == nullptr) {
return;
}
auto exported = GpgFrontend::SecureCreateSharedObject<ByteArray>();
auto key_ids = m_key_list_->GetSelected();
if (key_ids->empty()) {
GF_UI_LOG_ERROR("no key is selected");
return;
}
if (!GpgKeyImportExporter::GetInstance().ExportKeys(key_ids, exported)) {
QMessageBox::critical(this, _("Error"), _("Key Export Operation Failed."));
return;
}
edit_->CurTextPage()->GetTextPage()->appendPlainText(
QString::fromStdString(*exported));
}
void MainWindow::slot_append_keys_create_datetime() {
if (edit_->TabCount() == 0 || edit_->SlotCurPageTextEdit() == nullptr) {
return;
}
auto key_ids = m_key_list_->GetSelected();
if (key_ids->empty()) {
GF_UI_LOG_ERROR("no key is selected");
return;
}
auto key = GpgKeyGetter::GetInstance().GetKey(key_ids->front());
if (!key.IsGood()) {
QMessageBox::critical(this, _("Error"), _("Key Not Found."));
return;
}
auto create_datetime_format_str =
boost::posix_time::to_iso_extended_string(key.GetCreateTime()) +
" (UTC) " + "\n";
edit_->CurTextPage()->GetTextPage()->appendPlainText(
QString::fromStdString(create_datetime_format_str));
}
void MainWindow::slot_append_keys_expire_datetime() {
if (edit_->TabCount() == 0 || edit_->SlotCurPageTextEdit() == nullptr) {
return;
}
auto key_ids = m_key_list_->GetSelected();
if (key_ids->empty()) {
GF_UI_LOG_ERROR("no key is selected");
return;
}
auto key = GpgKeyGetter::GetInstance().GetKey(key_ids->front());
if (!key.IsGood()) {
QMessageBox::critical(this, _("Error"), _("Key Not Found."));
return;
}
auto create_datetime_format_str =
boost::posix_time::to_iso_extended_string(key.GetCreateTime()) +
" (UTC) " + "\n";
edit_->CurTextPage()->GetTextPage()->appendPlainText(
QString::fromStdString(create_datetime_format_str));
}
void MainWindow::slot_append_keys_fingerprint() {
auto key_ids = m_key_list_->GetSelected();
if (key_ids->empty()) return;
auto key = GpgKeyGetter::GetInstance().GetKey(key_ids->front());
if (!key.IsGood()) {
QMessageBox::critical(this, _("Error"), _("Key Not Found."));
return;
}
auto fingerprint_format_str =
BeautifyFingerprint(key.GetFingerprint()) + "\n";
edit_->CurTextPage()->GetTextPage()->appendPlainText(
QString::fromStdString(fingerprint_format_str));
}
void MainWindow::slot_copy_mail_address_to_clipboard() {
auto key_ids = m_key_list_->GetSelected();
if (key_ids->empty()) return;
auto key = GpgKeyGetter::GetInstance().GetKey(key_ids->front());
if (!key.IsGood()) {
QMessageBox::critical(this, _("Error"), _("Key Not Found."));
return;
}
QClipboard* cb = QApplication::clipboard();
cb->setText(QString::fromStdString(key.GetEmail()));
}
void MainWindow::slot_copy_default_uid_to_clipboard() {
auto key_ids = m_key_list_->GetSelected();
if (key_ids->empty()) return;
auto key = GpgKeyGetter::GetInstance().GetKey(key_ids->front());
if (!key.IsGood()) {
QMessageBox::critical(this, _("Error"), _("Key Not Found."));
return;
}
QClipboard* cb = QApplication::clipboard();
cb->setText(QString::fromStdString(key.GetUIDs()->front().GetUID()));
}
void MainWindow::slot_copy_key_id_to_clipboard() {
auto key_ids = m_key_list_->GetSelected();
if (key_ids->empty()) return;
auto key = GpgKeyGetter::GetInstance().GetKey(key_ids->front());
if (!key.IsGood()) {
QMessageBox::critical(this, _("Error"), _("Key Not Found."));
return;
}
QClipboard* cb = QApplication::clipboard();
cb->setText(QString::fromStdString(key.GetId()));
}
void MainWindow::slot_show_key_details() {
auto key_ids = m_key_list_->GetSelected();
if (key_ids->empty()) return;
auto key = GpgKeyGetter::GetInstance().GetKey(key_ids->front());
if (key.IsGood()) {
new KeyDetailsDialog(key, this);
} else {
QMessageBox::critical(this, _("Error"), _("Key Not Found."));
}
}
void MainWindow::slot_add_key_2_favourite() {
auto key_ids = m_key_list_->GetSelected();
if (key_ids->empty()) return;
auto key = GpgKeyGetter::GetInstance().GetKey(key_ids->front());
CommonUtils::GetInstance()->AddKey2Favourtie(key);
emit SignalUIRefresh();
}
void MainWindow::slot_remove_key_from_favourite() {
auto key_ids = m_key_list_->GetSelected();
if (key_ids->empty()) return;
auto key = GpgKeyGetter::GetInstance().GetKey(key_ids->front());
CommonUtils::GetInstance()->RemoveKeyFromFavourite(key);
emit SignalUIRefresh();
}
void MainWindow::refresh_keys_from_key_server() {
auto key_ids = m_key_list_->GetSelected();
if (key_ids->empty()) return;
auto* dialog = new KeyServerImportDialog(this);
dialog->show();
dialog->SlotImport(key_ids);
}
void MainWindow::slot_set_owner_trust_level_of_key() {
auto key_ids = m_key_list_->GetSelected();
if (key_ids->empty()) return;
auto* function = new SetOwnerTrustLevel(this);
function->Exec(key_ids->front());
function->deleteLater();
}
void MainWindow::upload_key_to_server() {
auto key_ids = m_key_list_->GetSelected();
auto* dialog = new KeyUploadDialog(key_ids, this);
dialog->show();
dialog->SlotUpload();
}
void MainWindow::SlotOpenFile(const QString& path) {
QFileInfo const info(path);
if (!info.isFile() || !info.isReadable()) {
QMessageBox::critical(this, _("Error"),
_("Cannot open this file. Please make sure that this "
"is a regular file and it's readable."));
return;
}
if (info.size() > static_cast<qint64>(1024 * 1024)) {
QMessageBox::critical(
this, _("Error"),
_("Cannot open this file. The file is TOO LARGE (>1MB) for "
"GpgFrontend Text Editor."));
return;
}
edit_->SlotOpenFile(path);
}
void MainWindow::slot_version_upgrade_nofity() {
GF_UI_LOG_DEBUG(
"slot version upgrade notify called, checking version info from rt...");
auto is_loading_done = Module::RetrieveRTValueTypedOrDefault<>(
"com.bktus.gpgfrontend.module.integrated.version-checking",
"version.loading_done", false);
GF_UI_LOG_DEBUG("checking version info from rt, is loading done state: {}",
is_loading_done);
if (!is_loading_done) {
GF_UI_LOG_ERROR("invalid version info from rt, loading hasn't done yet");
return;
}
auto is_need_upgrade = Module::RetrieveRTValueTypedOrDefault<>(
"com.bktus.gpgfrontend.module.integrated.version-checking",
"version.need_upgrade", false);
auto is_current_a_withdrawn_version = Module::RetrieveRTValueTypedOrDefault<>(
"com.bktus.gpgfrontend.module.integrated.version-checking",
"version.current_a_withdrawn_version", false);
auto is_current_version_released = Module::RetrieveRTValueTypedOrDefault<>(
"com.bktus.gpgfrontend.module.integrated.version-checking",
"version.current_version_released", false);
auto latest_version = Module::RetrieveRTValueTypedOrDefault<>(
"com.bktus.gpgfrontend.module.integrated.version-checking",
"version.latest_version", std::string{});
GF_UI_LOG_DEBUG(
"got version info from rt, need upgrade: {}, with drawn: {}, current "
"version "
"released: {}",
is_need_upgrade, is_current_a_withdrawn_version,
is_current_version_released);
if (is_need_upgrade) {
statusBar()->showMessage(
QString(_("GpgFrontend Upgradeable (New Version: %1)."))
.arg(latest_version.c_str()),
30000);
auto update_button = new QPushButton("Update GpgFrontend", this);
connect(update_button, &QPushButton::clicked, [=]() {
auto* about_dialog = new AboutDialog(2, this);
about_dialog->show();
});
statusBar()->addPermanentWidget(update_button, 0);
} else if (is_current_a_withdrawn_version) {
QMessageBox::warning(
this, _("Withdrawn Version"),
QString(
_("This version(%1) may have been withdrawn by the developer due "
"to serious problems. Please stop using this version "
"immediately and use the latest stable version."))
.arg(latest_version.c_str()) +
"<br/>" +
QString(_("You can download the latest stable version(%1) on "
"Github Releases "
"Page.<br/>"))
.arg(latest_version.c_str()));
} else if (!is_current_version_released) {
statusBar()->showMessage(
QString(_("This maybe a BETA Version (Latest Stable Version: %1)."))
.arg(latest_version.c_str()),
30000);
}
}
void MainWindow::slot_refresh_current_file_view() {
if (edit_->CurFilePage() != nullptr) {
edit_->CurFilePage()->update();
}
}
} // namespace GpgFrontend::UI
|