aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSaturneric <[email protected]>2022-05-22 11:10:22 +0000
committerSaturneric <[email protected]>2022-05-22 11:14:16 +0000
commit6b4a67d5b9d2cbc43b087c090bd3f5371f320ac8 (patch)
tree852ec9dbd593c21690cde869bd264eb57ce64854
parentfix: solve key server import not working issues (diff)
downloadGpgFrontend-6b4a67d5b9d2cbc43b087c090bd3f5371f320ac8.tar.gz
GpgFrontend-6b4a67d5b9d2cbc43b087c090bd3f5371f320ac8.zip
perf: improve ListedKeyServerTestThread
1. fixes #9
-rw-r--r--src/ui/settings/SettingsKeyServer.cpp74
-rw-r--r--src/ui/thread/ListedKeyServerTestTask.cpp88
-rw-r--r--src/ui/thread/ListedKeyServerTestTask.h (renamed from src/ui/thread/ListedKeyServerTestThread.h)35
-rw-r--r--src/ui/thread/ListedKeyServerTestThread.cpp49
4 files changed, 150 insertions, 96 deletions
diff --git a/src/ui/settings/SettingsKeyServer.cpp b/src/ui/settings/SettingsKeyServer.cpp
index e80e4598..94655871 100644
--- a/src/ui/settings/SettingsKeyServer.cpp
+++ b/src/ui/settings/SettingsKeyServer.cpp
@@ -29,8 +29,10 @@
#include "SettingsKeyServer.h"
#include "core/function/GlobalSettingStation.h"
+#include "core/thread/Task.h"
+#include "core/thread/TaskRunnerGetter.h"
#include "ui/struct/SettingsObject.h"
-#include "ui/thread/ListedKeyServerTestThread.h"
+#include "ui/thread/ListedKeyServerTestTask.h"
#include "ui_KeyServerSettings.h"
namespace GpgFrontend::UI {
@@ -128,8 +130,6 @@ void KeyserverTab::SetSettings() {
this->key_server_str_list_.append(key_server_str.c_str());
}
- auto& settings = GlobalSettingStation::GetInstance().GetUISettings();
-
int default_key_server_index = key_server_json.Check("default_server", 0);
std::string default_key_server =
key_server_list[default_key_server_index].get<std::string>();
@@ -223,9 +223,8 @@ void KeyserverTab::slot_refresh_table() {
}
void KeyserverTab::slot_test_listed_key_server() {
- auto timeout =
- QInputDialog::getInt(this, _("Set TCP Timeout"), tr("timeout(ms): "),
- QLineEdit::Normal, 800, 3000);
+ auto timeout = QInputDialog::getInt(this, _("Set TCP Timeout"),
+ tr("timeout(ms): "), 2500, 200, 16000);
QStringList urls;
const auto row_size = ui_->keyServerListTable->rowCount();
@@ -234,28 +233,30 @@ void KeyserverTab::slot_test_listed_key_server() {
urls.push_back(keyserver_url);
}
- auto thread = new ListedKeyServerTestThread(urls, timeout, this);
- connect(thread,
- &GpgFrontend::UI::ListedKeyServerTestThread::
- SignalKeyServerListTestResult,
- this, [=](const QStringList& result) {
- const auto row_size = ui_->keyServerListTable->rowCount();
- if (result.size() != row_size) return;
- ui_->keyServerListTable->blockSignals(true);
- for (int i = 0; i < row_size; i++) {
- const auto status = result[i];
- auto status_iem = ui_->keyServerListTable->item(i, 3);
- if (status == "Reachable") {
- status_iem->setText(_("Reachable"));
- status_iem->setForeground(QBrush(QColor::fromRgb(0, 255, 0)));
- } else {
- status_iem->setText(_("Not Reachable"));
- status_iem->setForeground(QBrush(QColor::fromRgb(255, 0, 0)));
- }
- }
- ui_->keyServerListTable->blockSignals(false);
- });
- connect(thread, &QThread::finished, thread, &QThread::deleteLater);
+ auto* task = new ListedKeyServerTestTask(urls, timeout, this);
+
+ connect(
+ task,
+ &GpgFrontend::UI::ListedKeyServerTestTask::SignalKeyServerListTestResult,
+ this,
+ [=](std::vector<ListedKeyServerTestTask::KeyServerTestResultType>
+ result) {
+ const auto row_size = ui_->keyServerListTable->rowCount();
+ if (result.size() != row_size) return;
+ ui_->keyServerListTable->blockSignals(true);
+ for (int i = 0; i < row_size; i++) {
+ const auto status = result[i];
+ auto status_iem = ui_->keyServerListTable->item(i, 3);
+ if (status == ListedKeyServerTestTask::kTestResultType_Success) {
+ status_iem->setText(_("Reachable"));
+ status_iem->setForeground(QBrush(QColor::fromRgb(0, 255, 0)));
+ } else {
+ status_iem->setText(_("Not Reachable"));
+ status_iem->setForeground(QBrush(QColor::fromRgb(255, 0, 0)));
+ }
+ }
+ ui_->keyServerListTable->blockSignals(false);
+ });
// Waiting Dialog
auto* waiting_dialog = new QProgressDialog(this);
@@ -269,23 +270,18 @@ void KeyserverTab::slot_test_listed_key_server() {
waiting_dialog_label->setWordWrap(true);
waiting_dialog->setLabel(waiting_dialog_label);
waiting_dialog->resize(420, 120);
- connect(thread, &QThread::finished, [=]() {
- waiting_dialog->finished(0);
+ waiting_dialog->setModal(true);
+ connect(task, &Thread::Task::SignalTaskFinished, [=]() {
+ waiting_dialog->close();
waiting_dialog->deleteLater();
});
- connect(waiting_dialog, &QProgressDialog::canceled, [=]() {
- LOG(INFO) << "cancel clicked";
- if (thread->isRunning()) thread->terminate();
- });
-
// Show Waiting Dialog
waiting_dialog->show();
waiting_dialog->setFocus();
- thread->start();
- QEventLoop loop;
- connect(thread, &QThread::finished, &loop, &QEventLoop::quit);
- loop.exec();
+ Thread::TaskRunnerGetter::GetInstance()
+ .GetTaskRunner(Thread::TaskRunnerGetter::kTaskRunnerType_Network)
+ ->PostTask(task);
}
void KeyserverTab::contextMenuEvent(QContextMenuEvent* event) {
diff --git a/src/ui/thread/ListedKeyServerTestTask.cpp b/src/ui/thread/ListedKeyServerTestTask.cpp
new file mode 100644
index 00000000..9d4ca74d
--- /dev/null
+++ b/src/ui/thread/ListedKeyServerTestTask.cpp
@@ -0,0 +1,88 @@
+/**
+ * Copyright (C) 2021 Saturneric
+ *
+ * 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.
+ *
+ * The source code version of this software was modified and released
+ * by Saturneric<[email protected]><[email protected]> starting on May 12, 2021.
+ *
+ */
+
+#include "ListedKeyServerTestTask.h"
+
+#include <vector>
+
+GpgFrontend::UI::ListedKeyServerTestTask::ListedKeyServerTestTask(
+ const QStringList& urls, int timeout, QWidget* parent)
+ : urls_(urls),
+ timeout_(timeout),
+ network_manager_(new QNetworkAccessManager(this)),
+ result_(urls_.size(), kTestResultType_Error) {
+ qRegisterMetaType<std::vector<KeyServerTestResultType>>(
+ "std::vector<KeyServerTestResultType>");
+}
+
+void GpgFrontend::UI::ListedKeyServerTestTask::run() {
+ SetFinishAfterRun(false);
+
+ size_t index = 0;
+ for (const auto& url : urls_) {
+ auto key_url = QUrl{url};
+ LOG(INFO) << "key server request: " << key_url.host().toStdString();
+
+ auto* network_reply = network_manager_->get(QNetworkRequest{key_url});
+ auto* timer = new QTimer(this);
+
+ connect(network_reply, &QNetworkReply::finished, this,
+ [this, index, network_reply]() {
+ LOG(INFO) << "key server domain reply"
+ << urls_[index].toStdString();
+ this->slot_process_network_reply(index, network_reply);
+ });
+
+ connect(timer, &QTimer::timeout, this, [this, index, network_reply]() {
+ LOG(INFO) << "timeout for key server" << urls_[index].toStdString();
+ if (network_reply->isRunning()) {
+ network_reply->abort();
+ this->slot_process_network_reply(index, network_reply);
+ }
+ });
+
+ timer->start(timeout_);
+
+ index++;
+ }
+}
+
+void GpgFrontend::UI::ListedKeyServerTestTask::slot_process_network_reply(
+ int index, QNetworkReply* reply) {
+ if (!reply->isRunning() && reply->error() == QNetworkReply::NoError) {
+ result_[index] = kTestResultType_Success;
+ } else {
+ if (!reply->isFinished())
+ result_[index] = kTestResultType_Timeout;
+ else
+ result_[index] = kTestResultType_Error;
+ }
+
+ if (++result_count_ == urls_.size()) {
+ emit SignalKeyServerListTestResult(result_);
+ emit SignalTaskFinished();
+ }
+}
diff --git a/src/ui/thread/ListedKeyServerTestThread.h b/src/ui/thread/ListedKeyServerTestTask.h
index dd7c2fa8..8edaccb0 100644
--- a/src/ui/thread/ListedKeyServerTestThread.h
+++ b/src/ui/thread/ListedKeyServerTestTask.h
@@ -27,7 +27,10 @@
#ifndef GPGFRONTEND_LISTEDKEYSERVERTESTTHREAD_H
#define GPGFRONTEND_LISTEDKEYSERVERTESTTHREAD_H
+#include <vector>
+
#include "GpgFrontendUI.h"
+#include "core/thread/Task.h"
namespace GpgFrontend::UI {
@@ -35,12 +38,17 @@ namespace GpgFrontend::UI {
* @brief
*
*/
-class ListedKeyServerTestThread : public QThread {
+class ListedKeyServerTestTask : public Thread::Task {
Q_OBJECT
public:
- explicit ListedKeyServerTestThread(const QStringList& urls, int timeout,
- QWidget* parent = nullptr)
- : QThread(parent), urls_(urls), timeout_(timeout) {}
+ enum KeyServerTestResultType {
+ kTestResultType_Success,
+ kTestResultType_Timeout,
+ kTestResultType_Error,
+ };
+
+ explicit ListedKeyServerTestTask(const QStringList& urls, int timeout,
+ QWidget* parent = nullptr);
signals:
/**
@@ -48,7 +56,8 @@ class ListedKeyServerTestThread : public QThread {
*
* @param result
*/
- void SignalKeyServerListTestResult(const QStringList& result);
+ void SignalKeyServerListTestResult(
+ std::vector<KeyServerTestResultType> result);
protected:
/**
@@ -58,9 +67,19 @@ class ListedKeyServerTestThread : public QThread {
void run() override;
private:
- QStringList urls_; ///<
- QStringList result_; ///<
- int timeout_ = 500; ///<
+ QStringList urls_; ///<
+ std::vector<KeyServerTestResultType> result_; ///<
+ QNetworkAccessManager* network_manager_; ///<
+ int timeout_ = 500; ///<
+ int result_count_ = 0; ///<
+
+ /**
+ * @brief
+ *
+ * @param index
+ * @param reply
+ */
+ void slot_process_network_reply(int index, QNetworkReply* reply);
};
} // namespace GpgFrontend::UI
diff --git a/src/ui/thread/ListedKeyServerTestThread.cpp b/src/ui/thread/ListedKeyServerTestThread.cpp
deleted file mode 100644
index 8c86f0be..00000000
--- a/src/ui/thread/ListedKeyServerTestThread.cpp
+++ /dev/null
@@ -1,49 +0,0 @@
-/**
- * Copyright (C) 2021 Saturneric
- *
- * 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.
- *
- * The source code version of this software was modified and released
- * by Saturneric<[email protected]><[email protected]> starting on May 12, 2021.
- *
- */
-
-#include "ListedKeyServerTestThread.h"
-
-void GpgFrontend::UI::ListedKeyServerTestThread::run() {
- for (const auto& url : urls_) {
- const auto keyserver_url = url;
-
- auto key_url = QUrl{keyserver_url};
-
- LOG(INFO) << "key server domain" << key_url.host().toStdString();
-
- QTcpSocket socket(nullptr);
- socket.abort();
- socket.connectToHost(key_url.host(), 80);
- if (socket.waitForConnected(timeout_)) {
- result_.push_back("Reachable");
- } else {
- result_.push_back("Not Reachable");
- }
- socket.close();
- }
-
- emit SignalKeyServerListTestResult(result_);
-}