diff options
m--------- | modules | 0 | ||||
-rw-r--r-- | src/ui/dialog/keypair_details/KeyPairDetailTab.cpp | 86 | ||||
-rw-r--r-- | src/ui/dialog/keypair_details/KeyPairDetailTab.h | 12 | ||||
-rw-r--r-- | src/ui/dialog/keypair_details/KeyPairOperaTab.cpp | 109 | ||||
-rw-r--r-- | src/ui/dialog/keypair_details/KeyPairOperaTab.h | 2 |
5 files changed, 192 insertions, 17 deletions
diff --git a/modules b/modules -Subproject 99a5cb5ab598d66b090f01750877ed5b1835ec0 +Subproject 405522f3b09913544bbec43a802d4e13ccd84f1 diff --git a/src/ui/dialog/keypair_details/KeyPairDetailTab.cpp b/src/ui/dialog/keypair_details/KeyPairDetailTab.cpp index ebd16aa9..bdc2c9e8 100644 --- a/src/ui/dialog/keypair_details/KeyPairDetailTab.cpp +++ b/src/ui/dialog/keypair_details/KeyPairDetailTab.cpp @@ -29,8 +29,11 @@ #include "KeyPairDetailTab.h" #include "core/GpgModel.h" +#include "core/function/GlobalSettingStation.h" #include "core/function/gpg/GpgKeyGetter.h" #include "core/model/GpgKey.h" +#include "core/module/ModuleManager.h" +#include "core/thread/TaskRunnerGetter.h" #include "core/utils/CommonUtils.h" #include "ui/UISignalStation.h" @@ -277,14 +280,13 @@ void KeyPairDetailTab::slot_refresh_key_info() { exp_label_->hide(); if (key_.IsExpired()) { - icon_label_->show(); - exp_label_->show(); - exp_label_->setText(tr("Warning: The primary key has expired.")); - } - if (key_.IsRevoked()) { - icon_label_->show(); - exp_label_->show(); - exp_label_->setText(tr("Warning: The primary key has been revoked.")); + slot_refresh_notice(":/icons/warning.png", + tr("Warning: The primary key has expired.")); + } else if (key_.IsRevoked()) { + slot_refresh_notice(":/icons/warning.png", + tr("Warning: The primary key has been revoked.")); + } else { + slot_query_key_publish_state(); } } @@ -299,4 +301,72 @@ void KeyPairDetailTab::slot_refresh_key() { this->slot_refresh_key_info(); } +void KeyPairDetailTab::slot_query_key_publish_state() { + bool forbid_all_gnupg_connection = + GlobalSettingStation::GetInstance() + .GetSettings() + .value("network/forbid_all_gnupg_connection") + .toBool(); + if (forbid_all_gnupg_connection) return; + + if (!Module::IsModuleActivate( + "com.bktus.gpgfrontend.module.key_server_sync")) { + return; + } + + Thread::TaskRunnerGetter::GetInstance() + .GetTaskRunner(Thread::TaskRunnerGetter::kTaskRunnerType_Network) + ->PostTask(new Thread::Task( + [this, + fpr = key_.GetFingerprint()](const DataObjectPtr& data_obj) -> int { + Module::TriggerEvent( + "REQUEST_GET_PUBLIC_KEY_BY_FINGERPRINT", + { + {"fingerprint", QString(fpr)}, + }, + [=](Module::EventIdentifier i, + Module::Event::ListenerIdentifier ei, + Module::Event::Params p) { + LOG_D() << "REQUEST_GET_PUBLIC_KEY_BY_FINGERPRINT callback: " + << i << ei; + + if (p["ret"] != "0" || !p["error_msg"].isEmpty()) { + LOG_E() << "An error occurred trying to get data from key:" + << fpr << "error message: " << p["error_msg"] + << "reply data: " << p["reply_data"]; + } else if (p.contains("key_data")) { + const auto key_data = p["key_data"]; + LOG_D() << "got key data of key " << fpr + << " from key server: " << key_data; + + if (!key_data.isEmpty()) { + slot_refresh_notice( + ":/icons/sent.png", + tr("Notice: The key has been published on " + "keys.openpgp.org.")); + } + } + }); + + return 0; + }, + QString("key_%1_query_task").arg(key_.GetFingerprint()))); +} + +void KeyPairDetailTab::slot_refresh_notice(const QString& icon, + const QString& info) { + icon_label_->hide(); + exp_label_->hide(); + + if (!icon.isEmpty()) { + QPixmap pixmap(icon); + icon_label_->setPixmap(pixmap.scaled(24, 24, Qt::KeepAspectRatio)); + icon_label_->show(); + } + + if (!info.isEmpty()) { + exp_label_->setText(info); + exp_label_->show(); + } +} } // namespace GpgFrontend::UI diff --git a/src/ui/dialog/keypair_details/KeyPairDetailTab.h b/src/ui/dialog/keypair_details/KeyPairDetailTab.h index 0e425fa7..da58e36e 100644 --- a/src/ui/dialog/keypair_details/KeyPairDetailTab.h +++ b/src/ui/dialog/keypair_details/KeyPairDetailTab.h @@ -54,6 +54,18 @@ class KeyPairDetailTab : public QWidget { */ void slot_refresh_key(); + /** + * @brief + * + */ + void slot_query_key_publish_state(); + + /** + * @brief + * + */ + void slot_refresh_notice(const QString& icon, const QString& info); + private: int current_gpg_context_channel_; GpgKey key_; ///< diff --git a/src/ui/dialog/keypair_details/KeyPairOperaTab.cpp b/src/ui/dialog/keypair_details/KeyPairOperaTab.cpp index b2978938..cd8eb335 100644 --- a/src/ui/dialog/keypair_details/KeyPairOperaTab.cpp +++ b/src/ui/dialog/keypair_details/KeyPairOperaTab.cpp @@ -35,6 +35,7 @@ #include "core/function/gpg/GpgKeyOpera.h" #include "core/model/GpgKey.h" #include "core/module/ModuleManager.h" +#include "core/thread/TaskRunnerGetter.h" #include "core/typedef/GpgTypedef.h" #include "core/utils/GpgUtils.h" #include "core/utils/IOUtils.h" @@ -97,8 +98,7 @@ KeyPairOperaTab::KeyPairOperaTab(int channel, const QString& key_id, bool forbid_all_gnupg_connection = settings.value("network/forbid_all_gnupg_connection").toBool(); - auto* key_server_opera_button = - new QPushButton(tr("Key Server Operation (Pubkey)")); + auto* key_server_opera_button = new QPushButton(tr("Key Server Operations")); key_server_opera_button->setStyleSheet("text-align:center;"); key_server_opera_button->setMenu(key_server_opera_menu_); key_server_opera_button->setDisabled(forbid_all_gnupg_connection); @@ -152,16 +152,14 @@ KeyPairOperaTab::KeyPairOperaTab(int channel, const QString& key_id, void KeyPairOperaTab::CreateOperaMenu() { key_server_opera_menu_ = new QMenu(this); - auto* upload_key_pair = - new QAction(tr("Upload Key Pair to Key Server"), this); + auto* upload_key_pair = new QAction(tr("Publish Key to Key Server"), this); connect(upload_key_pair, &QAction::triggered, this, - &KeyPairOperaTab::slot_upload_key_to_server); + &KeyPairOperaTab::slot_publish_key_to_server); if (!(m_key_.IsPrivateKey() && m_key_.IsHasMasterKey())) { upload_key_pair->setDisabled(true); } - auto* update_key_pair = - new QAction(tr("Sync Key Pair From Key Server"), this); + auto* update_key_pair = new QAction(tr("Refresh Key From Key Server"), this); connect(update_key_pair, &QAction::triggered, this, &KeyPairOperaTab::slot_update_key_from_server); @@ -348,7 +346,96 @@ void KeyPairOperaTab::slot_modify_edit_datetime() { dialog->show(); } -void KeyPairOperaTab::slot_upload_key_to_server() { +void KeyPairOperaTab::slot_publish_key_to_server() { + if (Module::IsModuleActivate( + "com.bktus.gpgfrontend.module.key_server_sync")) { + auto [err, gf_buffer] = + GpgKeyImportExporter::GetInstance(current_gpg_context_channel_) + .ExportKey(m_key_, false, true, false); + + Thread::TaskRunnerGetter::GetInstance() + .GetTaskRunner(Thread::TaskRunnerGetter::kTaskRunnerType_Network) + ->PostTask(new Thread::Task( + [this, fpr = m_key_.GetFingerprint(), + key_text = gf_buffer.ConvertToQByteArray()]( + const DataObjectPtr& data_obj) -> int { + Module::TriggerEvent( + "REQUEST_UPLOAD_PUBLIC_KEY", + { + {"key_text", QString::fromUtf8(key_text)}, + }, + [=](Module::EventIdentifier i, + Module::Event::ListenerIdentifier ei, + Module::Event::Params p) { + LOG_D() << "REQUEST_UPLOAD_PUBLIC_KEY " + "callback: " + << i << ei; + + if (p["ret"] != "0" || !p["error_msg"].isEmpty()) { + LOG_E() << "An error occurred trying to get data " + "from key:" + << fpr << "error message: " << p["error_msg"] + << "reply data: " << p["reply_data"]; + + // Notify user of the error + QString error_message = p["error_msg"]; + QMessageBox::critical( + this, tr("Key Upload Failed"), + tr("Failed to upload key to the server.\n" + "Fingerprint: %1\n" + "Error: %2") + .arg(fpr, error_message)); + } else if (p.contains("token") && p.contains("status") && + p.contains("fingerprint")) { + const auto token = p["token"]; + const auto status = p["status"]; + const auto reply_fpr = p["fingerprint"]; + LOG_D() << "got key data of key " << fpr + << "from key server, token: " << token + << "fpr: " << fpr << "status: " << status; + + // Handle successful response + QString status_message = + tr("The following email addresses have status:\n"); + QJsonDocument json_doc = + QJsonDocument::fromJson(status.toUtf8()); + QStringList email_list; + if (!json_doc.isNull() && json_doc.isObject()) { + QJsonObject json_obj = json_doc.object(); + for (auto it = json_obj.constBegin(); + it != json_obj.constEnd(); ++it) { + status_message += + QString("%1: %2\n") + .arg(it.key(), it.value().toString()); + email_list.append(it.key()); + } + } else { + status_message += + tr("Could not parse status information."); + } + + // Notify user of successful upload and status details + QMessageBox::information( + this, tr("Key Upload Successful"), + tr("The key was successfully uploaded to the key " + "server keys.openpgp.org.\n" + "Fingerprint: %1\n\n" + "%2\n" + "Please check your email (%3) for further " + "verification from keys.openpgp.org.\n\n" + "Note: For verification, you can find more " + "information here: " + "https://keys.openpgp.org/about") + .arg(fpr, status_message, email_list.join(", "))); + } + }); + + return 0; + }, + QString("key_%1_publish_task").arg(m_key_.GetId()))); + return; + } + auto keys = std::make_unique<KeyIdArgsList>(); keys->push_back(m_key_.GetId()); auto* dialog = new KeyUploadDialog(current_gpg_context_channel_, keys, this); @@ -357,6 +444,12 @@ void KeyPairOperaTab::slot_upload_key_to_server() { } void KeyPairOperaTab::slot_update_key_from_server() { + if (Module::IsModuleActivate( + "com.bktus.gpgfrontend.module.key_server_sync")) { + CommonUtils::GetInstance()->ImportKeyByKeyServerSyncModule( + this, current_gpg_context_channel_, {m_key_.GetFingerprint()}); + return; + } CommonUtils::GetInstance()->ImportKeyFromKeyServer( current_gpg_context_channel_, {m_key_.GetId()}); } diff --git a/src/ui/dialog/keypair_details/KeyPairOperaTab.h b/src/ui/dialog/keypair_details/KeyPairOperaTab.h index 28b2bc68..632246dd 100644 --- a/src/ui/dialog/keypair_details/KeyPairOperaTab.h +++ b/src/ui/dialog/keypair_details/KeyPairOperaTab.h @@ -94,7 +94,7 @@ class KeyPairOperaTab : public QWidget { * @brief * */ - void slot_upload_key_to_server(); + void slot_publish_key_to_server(); /** * @brief |