/** * 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 . * * 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 starting on May 12, 2021. * * SPDX-License-Identifier: GPL-3.0-or-later * */ #include "KeyUploadDialog.h" #include #include #include "core/GpgModel.h" #include "core/function/gpg/GpgKeyGetter.h" #include "core/function/gpg/GpgKeyImportExporter.h" #include "core/utils/GpgUtils.h" #include "ui/UserInterfaceUtils.h" #include "ui/struct/SettingsObject.h" namespace GpgFrontend::UI { KeyUploadDialog::KeyUploadDialog(const KeyIdArgsListPtr& keys_ids, QWidget* parent) : GeneralDialog(typeid(KeyUploadDialog).name(), parent), m_keys_(GpgKeyGetter::GetInstance().GetKeys(keys_ids)) { auto* pb = new QProgressBar(); pb->setRange(0, 0); pb->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); pb->setTextVisible(false); auto* layout = new QVBoxLayout(); layout->addWidget(pb); layout->setContentsMargins(0, 0, 0, 0); layout->setSpacing(0); this->setLayout(layout); this->setModal(true); this->setWindowTitle(_("Uploading Public Key")); this->setFixedSize(240, 42); this->setPosCenterOfScreen(); } void KeyUploadDialog::SlotUpload() { GpgKeyImportExporter::GetInstance().ExportKeys( *m_keys_, false, true, false, false, [=](GpgError err, const DataObjectPtr& data_obj) { if (CheckGpgError(err) != GPG_ERR_NO_ERROR) { CommonUtils::RaiseMessageBox(this, err); return; } if (data_obj == nullptr || !data_obj->Check()) { throw std::runtime_error("data object doesn't pass checking"); } auto gf_buffer = ExtractParams(data_obj, 0); slot_upload_key_to_server(gf_buffer); // Done this->hide(); this->close(); }); } void KeyUploadDialog::slot_upload_key_to_server( const GpgFrontend::GFBuffer& keys_data) { QString target_keyserver; try { SettingsObject key_server_json("key_server"); const auto key_server_list = key_server_json.Check("server_list", nlohmann::json::array()); size_t default_key_server_index = key_server_json.Check("default_server", 0); if (default_key_server_index >= key_server_list.size()) { throw std::runtime_error("default_server index out of range"); } target_keyserver = QString::fromStdString( key_server_list[default_key_server_index].get()); GF_UI_LOG_DEBUG("set target key server to default key server: {}", target_keyserver); } catch (...) { GF_UI_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 req_url(target_keyserver + "/pks/add"); auto* qnam = new QNetworkAccessManager(this); // Building Post Data QByteArray post_data; auto data = keys_data.ConvertToQByteArray(); data.replace("\n", "%0A"); data.replace("\r", "%0D"); data.replace("(", "%28"); data.replace(")", "%29"); data.replace("/", "%2F"); data.replace(":", "%3A"); data.replace("+", "%2B"); data.replace("=", "%3D"); data.replace(" ", "+"); QNetworkRequest request(req_url); request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded"); post_data.append("keytext").append("=").append(data); // Send Post Data QNetworkReply* reply = qnam->post(request, post_data); connect(reply, &QNetworkReply::finished, this, &KeyUploadDialog::slot_upload_finished); // Keep Waiting while (reply->isRunning()) { QApplication::processEvents(); } } void KeyUploadDialog::slot_upload_finished() { auto* reply = qobject_cast(sender()); QByteArray response = reply->readAll(); GF_UI_LOG_DEBUG("response: {}", response.toStdString()); auto error = reply->error(); if (error != QNetworkReply::NoError) { GF_UI_LOG_DEBUG("error from reply: {}", reply->errorString().toStdString()); QString message; switch (error) { case QNetworkReply::ContentNotFoundError: message = _("Key Not Found"); break; case QNetworkReply::TimeoutError: message = _("Timeout"); break; case QNetworkReply::HostNotFoundError: message = _("Key Server Not Found"); break; default: message = _("Connection Error"); } QMessageBox::critical(this, "Upload Failed", message); return; } QMessageBox::information(this, _("Upload Success"), _("Upload Public Key Successfully")); GF_UI_LOG_DEBUG("success while contacting keyserver!"); reply->deleteLater(); } } // namespace GpgFrontend::UI