diff options
Diffstat (limited to 'src/ui/UserInterfaceUtils.cpp')
-rw-r--r-- | src/ui/UserInterfaceUtils.cpp | 143 |
1 files changed, 126 insertions, 17 deletions
diff --git a/src/ui/UserInterfaceUtils.cpp b/src/ui/UserInterfaceUtils.cpp index 9e5fab10..30615f9f 100644 --- a/src/ui/UserInterfaceUtils.cpp +++ b/src/ui/UserInterfaceUtils.cpp @@ -29,6 +29,8 @@ #include "gpg/result_analyse/ResultAnalyse.h" #include "ui/SignalStation.h" #include "ui/WaitingDialog.h" +#include "ui/settings/GlobalSettingStation.h" +#include "ui/smtp/SendMailDialog.h" #include "ui/widgets/InfoBoardWidget.h" #include "ui/widgets/TextEdit.h" @@ -37,6 +39,27 @@ namespace GpgFrontend::UI { std::unique_ptr<GpgFrontend::UI::CommonUtils> GpgFrontend::UI::CommonUtils::_instance = nullptr; +void send_an_email(QWidget* parent, InfoBoardWidget* info_board, + const QString& text) { + info_board->addOptionalAction("Send Encrypted Mail", [=]() { + bool smtp_enabled = false; + try { + smtp_enabled = GlobalSettingStation::GetInstance().GetUISettings().lookup( + "smtp.enable"); + } catch (...) { + LOG(INFO) << "Reading smtp settings error"; + } + if (smtp_enabled) { + auto dialog = new SendMailDialog(text, parent); + dialog->show(); + } else { + QMessageBox::warning(nullptr, _("Function Disabled"), + _("Please go to the settings interface to " + "enable and configure this function.")); + } + }); +} + void show_verify_details(QWidget* parent, InfoBoardWidget* info_board, GpgError error, const GpgVerifyResult& verify_result) { // take out result @@ -163,26 +186,29 @@ void CommonUtils::slotExecuteGpgCommand( QEventLoop looper; auto dialog = new WaitingDialog(_("Processing"), nullptr); dialog->show(); - auto* gpgProcess = new QProcess(&looper); - gpgProcess->setProcessChannelMode(QProcess::MergedChannels); - - connect(gpgProcess, qOverload<int, QProcess::ExitStatus>(&QProcess::finished), - &looper, &QEventLoop::quit); - connect(gpgProcess, qOverload<int, QProcess::ExitStatus>(&QProcess::finished), - dialog, &WaitingDialog::deleteLater); - connect(gpgProcess, &QProcess::errorOccurred, &looper, &QEventLoop::quit); - connect(gpgProcess, &QProcess::started, + auto* gpg_process = new QProcess(&looper); + gpg_process->setProcessChannelMode(QProcess::MergedChannels); + + connect(gpg_process, + qOverload<int, QProcess::ExitStatus>(&QProcess::finished), &looper, + &QEventLoop::quit); + connect(gpg_process, + qOverload<int, QProcess::ExitStatus>(&QProcess::finished), dialog, + &WaitingDialog::deleteLater); + connect(gpg_process, &QProcess::errorOccurred, &looper, &QEventLoop::quit); + connect(gpg_process, &QProcess::started, []() -> void { LOG(ERROR) << "Gpg Process Started Success"; }); - connect(gpgProcess, &QProcess::readyReadStandardOutput, - [interact_func, gpgProcess]() { interact_func(gpgProcess); }); - connect(gpgProcess, &QProcess::errorOccurred, this, [=]() -> void { + connect(gpg_process, &QProcess::readyReadStandardOutput, + [interact_func, gpg_process]() { interact_func(gpg_process); }); + connect(gpg_process, &QProcess::errorOccurred, this, [=]() -> void { LOG(ERROR) << "Error in Process"; dialog->close(); QMessageBox::critical(nullptr, _("Failure"), _("Failed to execute command.")); }); - connect(gpgProcess, qOverload<int, QProcess::ExitStatus>(&QProcess::finished), - this, [=](int, QProcess::ExitStatus status) { + connect(gpg_process, + qOverload<int, QProcess::ExitStatus>(&QProcess::finished), this, + [=](int, QProcess::ExitStatus status) { dialog->close(); if (status == QProcess::NormalExit) QMessageBox::information(nullptr, _("Success"), @@ -192,12 +218,95 @@ void CommonUtils::slotExecuteGpgCommand( _("Finished executing command.")); }); - gpgProcess->setProgram(GpgContext::GetInstance().GetInfo().AppPath.c_str()); - gpgProcess->setArguments(arguments); - gpgProcess->start(); + gpg_process->setProgram(GpgContext::GetInstance().GetInfo().AppPath.c_str()); + gpg_process->setArguments(arguments); + gpg_process->start(); looper.exec(); dialog->close(); dialog->deleteLater(); } +void CommonUtils::slotDoImportKeyFromKeyServer( + QNetworkReply* network_reply, const std::string& key_id, + size_t current_index, size_t all_index, + const ImportCallbackFunctiopn& _callback) { + auto key_data = network_reply->readAll(); + auto key_data_ptr = + std::make_shared<ByteArray>(key_data.constData(), key_data.length()); + std::string status; + auto error = network_reply->error(); + if (error != QNetworkReply::NoError) { + switch (error) { + case QNetworkReply::ContentNotFoundError: + status = _("Key Not Found"); + break; + case QNetworkReply::TimeoutError: + status = _("Timeout"); + break; + case QNetworkReply::HostNotFoundError: + status = _("Key Server Not Found"); + break; + default: + status = _("Connection Error"); + } + } + + auto thread = QThread::create([=]() { + // need copy + auto unique_key_data_ptr = std::make_unique<ByteArray>(*key_data_ptr); + GpgImportInformation result = GpgKeyImportExportor::GetInstance().ImportKey( + std::move(unique_key_data_ptr)); + + std::string new_status = status; + if (result.imported == 1) { + new_status = _("The key has been updated"); + } else { + new_status = _("No need to update the key"); + } + _callback(key_id, new_status, current_index, all_index); + }); + connect(thread, &QThread::finished, thread, &QThread::deleteLater); + thread->start(); +} + +void CommonUtils::slotImportKeyFromKeyServer( + const KeyIdArgsList& key_ids, const ImportCallbackFunctiopn& callback) { + std::string target_keyserver; + if (target_keyserver.empty()) { + try { + auto& settings = GlobalSettingStation::GetInstance().GetUISettings(); + + target_keyserver = settings.lookup("keyserver.default_server").c_str(); + + LOG(INFO) << _("Set target Key Server to default Key Server") + << target_keyserver; + } catch (...) { + LOG(ERROR) << _("Cannot read default_keyserver From Settings"); + QMessageBox::critical( + nullptr, _("Default Keyserver Not Found"), + _("Cannot read default keyserver from your settings, " + "please set a default keyserver first")); + return; + } + } + QUrl target_keyserver_url(target_keyserver.c_str()); + + // LOOP + decltype(key_ids.size()) current_index = 1, all_index = key_ids.size(); + for (const auto& key_id : key_ids) { + QUrl req_url(target_keyserver_url.scheme() + "://" + + target_keyserver_url.host() + "/pks/lookup?op=get&search=0x" + + key_id.c_str() + "&options=mr"); + + LOG(INFO) << "request url" << req_url.toString().toStdString(); + + QNetworkReply* reply = _network_manager->get(QNetworkRequest(req_url)); + connect(reply, &QNetworkReply::finished, this, [=]() { + this->slotDoImportKeyFromKeyServer(reply, key_id, current_index, + all_index, callback); + reply->deleteLater(); + }); + current_index++; + } +} } // namespace GpgFrontend::UI
\ No newline at end of file |