From daaa83549d42395161e34a40e18c3615801b1501 Mon Sep 17 00:00:00 2001 From: Saturneric Date: Sun, 29 Aug 2021 18:49:58 +0800 Subject: Start to rewrite gpg core. --- src/ui/main_window/MainWindowServerSlotFunction.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/ui/main_window/MainWindowServerSlotFunction.cpp') diff --git a/src/ui/main_window/MainWindowServerSlotFunction.cpp b/src/ui/main_window/MainWindowServerSlotFunction.cpp index 3a7e9f71..dbbf178a 100644 --- a/src/ui/main_window/MainWindowServerSlotFunction.cpp +++ b/src/ui/main_window/MainWindowServerSlotFunction.cpp @@ -38,7 +38,7 @@ QString MainWindow::getCryptText(const QString &shortenCryptoText) { QString ownKeyId = settings.value("general/ownKeyId").toString(); - GpgKey key = mCtx->getKeyById(ownKeyId); + GpgKey key = mCtx->getKeyRefById(ownKeyId); if (!key.good) { QMessageBox::critical(this, tr("Invalid Own Key"), tr("Own Key can not be use to do any operation. " @@ -149,7 +149,7 @@ void MainWindow::shortenCryptText() { QNetworkRequest request(reqUrl); request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); - GpgKey key = mCtx->getKeyById(ownKeyId); + GpgKey key = mCtx->getKeyRefById(ownKeyId); if (!key.good) { QMessageBox::critical(this, tr("Invalid Own Key"), tr("Own Key can not be use to do any operation.")); return; -- cgit v1.2.3 From 3c65d087eeee687ac01af2e80f3dd538f9a2c230 Mon Sep 17 00:00:00 2001 From: Saturneric Date: Sat, 2 Oct 2021 22:08:50 +0800 Subject: UI Framework Modified. --- .../main_window/MainWindowServerSlotFunction.cpp | 372 +++++++++++---------- 1 file changed, 199 insertions(+), 173 deletions(-) (limited to 'src/ui/main_window/MainWindowServerSlotFunction.cpp') diff --git a/src/ui/main_window/MainWindowServerSlotFunction.cpp b/src/ui/main_window/MainWindowServerSlotFunction.cpp index dbbf178a..6266efda 100644 --- a/src/ui/main_window/MainWindowServerSlotFunction.cpp +++ b/src/ui/main_window/MainWindowServerSlotFunction.cpp @@ -23,216 +23,242 @@ */ #include "MainWindow.h" -#include "server/ComUtils.h" -#include "ui/ShowCopyDialog.h" +#ifdef SERVER_SUPPORT #include "rapidjson/document.h" #include "rapidjson/prettywriter.h" +#include "server/ComUtils.h" +#endif +#include "ui/ShowCopyDialog.h" + +namespace GpgFrontend::UI { + +#ifdef SERVER_SUPPORT /** * get full size crypt text from server using short crypto text * @param shortenCryptoText short crypto text([GpgFrontend_ShortCrypto]://) * @return */ -QString MainWindow::getCryptText(const QString &shortenCryptoText) { - - QString ownKeyId = settings.value("general/ownKeyId").toString(); - - GpgKey key = mCtx->getKeyRefById(ownKeyId); - if (!key.good) { - QMessageBox::critical(this, tr("Invalid Own Key"), - tr("Own Key can not be use to do any operation. " - "Please go to the setting interface to select an OwnKey and get a ServiceToken.")); - return {}; - } - - auto utils = new ComUtils(this); +QString MainWindow::getCryptText(const QString& shortenCryptoText) { + QString ownKeyId = settings.value("general/ownKeyId").toString(); + + GpgKey key = mCtx->getKeyRefById(ownKeyId); + if (!key.good) { + QMessageBox::critical(this, tr("Invalid Own Key"), + tr("Own Key can not be use to do any operation. " + "Please go to the setting interface to select an " + "OwnKey and get a ServiceToken.")); + return {}; + } - QString serviceToken = settings.value("general/serviceToken").toString(); - if (serviceToken.isEmpty() || !utils->checkServiceTokenFormat(serviceToken)) { - QMessageBox::critical(this, tr("Error"), - tr("Please obtain a Service Token from the server in the settings.")); - return {}; - } + auto utils = new ComUtils(this); - QUrl reqUrl(utils->getUrl(ComUtils::GetFullCryptText)); - QNetworkRequest request(reqUrl); - request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); + QString serviceToken = settings.value("general/serviceToken").toString(); + if (serviceToken.isEmpty() || !utils->checkServiceTokenFormat(serviceToken)) { + QMessageBox::critical( + this, tr("Error"), + tr("Please obtain a Service Token from the server in the settings.")); + return {}; + } - // Sign Shorten Text - auto outSignTextBase64 = ComUtils::getSignStringBase64(mCtx, shortenCryptoText, key); + QUrl reqUrl(utils->getUrl(ComUtils::GetFullCryptText)); + QNetworkRequest request(reqUrl); + request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); - rapidjson::Document doc; - doc.SetObject(); + // Sign Shorten Text + auto outSignTextBase64 = + ComUtils::getSignStringBase64(mCtx, shortenCryptoText, key); - rapidjson::Value s, t; + rapidjson::Document doc; + doc.SetObject(); - // Signature - s.SetString(outSignTextBase64.constData(), outSignTextBase64.count()); - // Service Token - const auto t_byte_array = serviceToken.toUtf8(); - t.SetString(t_byte_array.constData(), t_byte_array.count()); + rapidjson::Value s, t; - rapidjson::Document::AllocatorType &allocator = doc.GetAllocator(); + // Signature + s.SetString(outSignTextBase64.constData(), outSignTextBase64.count()); + // Service Token + const auto t_byte_array = serviceToken.toUtf8(); + t.SetString(t_byte_array.constData(), t_byte_array.count()); - doc.AddMember("signature", s, allocator); - doc.AddMember("serviceToken", t, allocator); + rapidjson::Document::AllocatorType& allocator = doc.GetAllocator(); - rapidjson::StringBuffer sb; - rapidjson::PrettyWriter writer(sb); - doc.Accept(writer); + doc.AddMember("signature", s, allocator); + doc.AddMember("serviceToken", t, allocator); - QByteArray postData(sb.GetString()); - qDebug() << "postData" << QString::fromUtf8(postData); + rapidjson::StringBuffer sb; + rapidjson::PrettyWriter writer(sb); + doc.Accept(writer); - QNetworkReply *reply = utils->getNetworkManager().post(request, postData); + QByteArray postData(sb.GetString()); + qDebug() << "postData" << QString::fromUtf8(postData); - auto dialog = new WaitingDialog(tr("Getting Cpt From Server"), this); - dialog->show(); + QNetworkReply* reply = utils->getNetworkManager().post(request, postData); - while (reply->isRunning()) QApplication::processEvents(); + auto dialog = new WaitingDialog(tr("Getting Cpt From Server"), this); + dialog->show(); - dialog->close(); + while (reply->isRunning()) + QApplication::processEvents(); - QByteArray replyData = reply->readAll().constData(); - if (utils->checkServerReply(replyData)) { - /** - * { - * "cryptoText" : ... - * "sha": ... - * "serviceToken": ... - * "date": ... - * } - */ + dialog->close(); - if (!utils->checkDataValueStr("cryptoText") - || !utils->checkDataValueStr("sha") - || !utils->checkDataValueStr("serviceToken")) { - QMessageBox::critical(this, tr("Error"), - tr("The communication content with the server does not meet the requirements")); - return {}; - } + QByteArray replyData = reply->readAll().constData(); + if (utils->checkServerReply(replyData)) { + /** + * { + * "cryptoText" : ... + * "sha": ... + * "serviceToken": ... + * "date": ... + * } + */ - auto cryptoText = utils->getDataValueStr("cryptoText"); - auto sha = utils->getDataValueStr("sha"); - auto serviceTokenFromServer = utils->getDataValueStr("serviceToken"); + if (!utils->checkDataValueStr("cryptoText") || + !utils->checkDataValueStr("sha") || + !utils->checkDataValueStr("serviceToken")) { + QMessageBox::critical(this, tr("Error"), + tr("The communication content with the server does " + "not meet the requirements")); + return {}; + } - QCryptographicHash sha_generator(QCryptographicHash::Sha256); - sha_generator.addData(cryptoText.toUtf8()); + auto cryptoText = utils->getDataValueStr("cryptoText"); + auto sha = utils->getDataValueStr("sha"); + auto serviceTokenFromServer = utils->getDataValueStr("serviceToken"); - if (sha_generator.result().toHex() == sha && serviceToken == serviceTokenFromServer) { - return cryptoText; - } else QMessageBox::critical(this, tr("Error"), tr("Invalid short ciphertext")); + QCryptographicHash sha_generator(QCryptographicHash::Sha256); + sha_generator.addData(cryptoText.toUtf8()); - return {}; - } + if (sha_generator.result().toHex() == sha && + serviceToken == serviceTokenFromServer) { + return cryptoText; + } else + QMessageBox::critical(this, tr("Error"), tr("Invalid short ciphertext")); return {}; -} + } -void MainWindow::shortenCryptText() { + return {}; +} - // gather information - QString serviceToken = settings.value("general/serviceToken").toString(); - QString ownKeyId = settings.value("general/ownKeyId").toString(); - QByteArray cryptoText = edit->curTextPage()->toPlainText().toUtf8(); +#endif - auto utils = new ComUtils(this); +#ifdef SERVER_SUPPORT - if (serviceToken.isEmpty() || !utils->checkServiceTokenFormat(serviceToken)) { - QMessageBox::critical(this, tr("Invalid Service Token"), - tr("Please go to the setting interface to get a ServiceToken.")); - return; +void MainWindow::shortenCryptText() { + // gather information + QString serviceToken = settings.value("general/serviceToken").toString(); + QString ownKeyId = settings.value("general/ownKeyId").toString(); + QByteArray cryptoText = edit->curTextPage()->toPlainText().toUtf8(); + + auto utils = new ComUtils(this); + + if (serviceToken.isEmpty() || !utils->checkServiceTokenFormat(serviceToken)) { + QMessageBox::critical( + this, tr("Invalid Service Token"), + tr("Please go to the setting interface to get a ServiceToken.")); + return; + } + + QUrl reqUrl(utils->getUrl(ComUtils::ShortenCryptText)); + QNetworkRequest request(reqUrl); + request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); + + GpgKey key = mCtx->getKeyRefById(ownKeyId); + if (!key.good) { + QMessageBox::critical(this, tr("Invalid Own Key"), + tr("Own Key can not be use to do any operation.")); + return; + } + + QCryptographicHash ch(QCryptographicHash::Md5); + ch.addData(cryptoText); + QString md5 = ch.result().toHex(); + + qDebug() << "md5" << md5; + + QByteArray signText = QString("[%1][%2]").arg(serviceToken, md5).toUtf8(); + + QCryptographicHash sha(QCryptographicHash::Sha256); + sha.addData(signText); + QString shaText = sha.result().toHex(); + + qDebug() << "shaText" << shaText; + + QByteArray outSignTextBase64 = + ComUtils::getSignStringBase64(mCtx, signText, key); + + rapidjson::Value c, s, m, t; + + rapidjson::Document doc; + doc.SetObject(); + + c.SetString(cryptoText.constData(), cryptoText.count()); + auto m_byte_array = shaText.toUtf8(); + m.SetString(m_byte_array.constData(), m_byte_array.count()); + s.SetString(outSignTextBase64.constData(), outSignTextBase64.count()); + auto t_byte_array = serviceToken.toUtf8(); + t.SetString(t_byte_array.constData(), t_byte_array.count()); + + rapidjson::Document::AllocatorType& allocator = doc.GetAllocator(); + + doc.AddMember("cryptoText", c, allocator); + doc.AddMember("sha", m, allocator); + doc.AddMember("sign", s, allocator); + doc.AddMember("serviceToken", t, allocator); + + rapidjson::StringBuffer sb; + rapidjson::PrettyWriter writer(sb); + doc.Accept(writer); + + QByteArray postData(sb.GetString()); + qDebug() << "postData" << QString::fromUtf8(postData); + + QNetworkReply* reply = networkAccessManager->post(request, postData); + + auto* dialog = new WaitingDialog(tr("Getting Scpt From Server"), this); + dialog->show(); + while (reply->isRunning()) + QApplication::processEvents(); + dialog->close(); + + if (utils->checkServerReply(reply->readAll().constData())) { + /** + * { + * "shortenText" : ... + * "md5": ... + * } + */ + + if (!utils->checkDataValueStr("shortenText") || + !utils->checkDataValueStr("md5")) { + QMessageBox::critical(this, tr("Error"), + tr("The communication content with the server does " + "not meet the requirements")); + return; } - QUrl reqUrl(utils->getUrl(ComUtils::ShortenCryptText)); - QNetworkRequest request(reqUrl); - request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); - - GpgKey key = mCtx->getKeyRefById(ownKeyId); - if (!key.good) { - QMessageBox::critical(this, tr("Invalid Own Key"), tr("Own Key can not be use to do any operation.")); - return; + QString shortenText = utils->getDataValueStr("shortenText"); + + QCryptographicHash md5_generator(QCryptographicHash::Md5); + md5_generator.addData(shortenText.toUtf8()); + if (md5_generator.result().toHex() == utils->getDataValueStr("md5")) { + auto* dialog = + new ShowCopyDialog(shortenText, + tr("Notice: Use Decrypt & Verify operation to " + "decrypt this short crypto text."), + this); + dialog->show(); + } else { + QMessageBox::critical( + this, tr("Error"), + tr("There is a problem with the communication with the server")); + return; } - - QCryptographicHash ch(QCryptographicHash::Md5); - ch.addData(cryptoText); - QString md5 = ch.result().toHex(); - - qDebug() << "md5" << md5; - - QByteArray signText = QString("[%1][%2]").arg(serviceToken, md5).toUtf8(); - - QCryptographicHash sha(QCryptographicHash::Sha256); - sha.addData(signText); - QString shaText = sha.result().toHex(); - - qDebug() << "shaText" << shaText; - - QByteArray outSignTextBase64 = ComUtils::getSignStringBase64(mCtx, signText, key); - - rapidjson::Value c, s, m, t; - - rapidjson::Document doc; - doc.SetObject(); - - c.SetString(cryptoText.constData(), cryptoText.count()); - auto m_byte_array = shaText.toUtf8(); - m.SetString(m_byte_array.constData(), m_byte_array.count()); - s.SetString(outSignTextBase64.constData(), outSignTextBase64.count()); - auto t_byte_array = serviceToken.toUtf8(); - t.SetString(t_byte_array.constData(), t_byte_array.count()); - - rapidjson::Document::AllocatorType &allocator = doc.GetAllocator(); - - doc.AddMember("cryptoText", c, allocator); - doc.AddMember("sha", m, allocator); - doc.AddMember("sign", s, allocator); - doc.AddMember("serviceToken", t, allocator); - - rapidjson::StringBuffer sb; - rapidjson::PrettyWriter writer(sb); - doc.Accept(writer); - - QByteArray postData(sb.GetString()); - qDebug() << "postData" << QString::fromUtf8(postData); - - QNetworkReply *reply = networkAccessManager->post(request, postData); - - auto *dialog = new WaitingDialog(tr("Getting Scpt From Server"), this); - dialog->show(); - while (reply->isRunning()) QApplication::processEvents(); - dialog->close(); - - if (utils->checkServerReply(reply->readAll().constData())) { - - /** - * { - * "shortenText" : ... - * "md5": ... - * } - */ - - if (!utils->checkDataValueStr("shortenText") || !utils->checkDataValueStr("md5")) { - QMessageBox::critical(this, tr("Error"), - tr("The communication content with the server does not meet the requirements")); - return; - } - - QString shortenText = utils->getDataValueStr("shortenText"); - - QCryptographicHash md5_generator(QCryptographicHash::Md5); - md5_generator.addData(shortenText.toUtf8()); - if (md5_generator.result().toHex() == utils->getDataValueStr("md5")) { - auto *dialog = new ShowCopyDialog(shortenText, - tr("Notice: Use Decrypt & Verify operation to decrypt this short crypto text."), - this); - dialog->show(); - } else { - QMessageBox::critical(this, tr("Error"), tr("There is a problem with the communication with the server")); - return; - } - } - + } } +#endif + +} // namespace GpgFrontend::UI -- cgit v1.2.3 From 1ae8663decb3163b92d32b80cefb46eb678a5af6 Mon Sep 17 00:00:00 2001 From: Saturneric Date: Thu, 2 Dec 2021 01:25:46 +0800 Subject: Add i18n Support 1. Remove Qt Linguist. 2. Add GNU gettext libraries. 3. Modified source codes to meet with i18n support. --- .../main_window/MainWindowServerSlotFunction.cpp | 56 +++++++++++----------- 1 file changed, 27 insertions(+), 29 deletions(-) (limited to 'src/ui/main_window/MainWindowServerSlotFunction.cpp') diff --git a/src/ui/main_window/MainWindowServerSlotFunction.cpp b/src/ui/main_window/MainWindowServerSlotFunction.cpp index 6266efda..17491db7 100644 --- a/src/ui/main_window/MainWindowServerSlotFunction.cpp +++ b/src/ui/main_window/MainWindowServerSlotFunction.cpp @@ -1,7 +1,7 @@ /** - * This file is part of GPGFrontend. + * This file is part of GpgFrontend. * - * GPGFrontend is free software: you can redistribute it and/or modify + * 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. @@ -45,10 +45,10 @@ QString MainWindow::getCryptText(const QString& shortenCryptoText) { GpgKey key = mCtx->getKeyRefById(ownKeyId); if (!key.good) { - QMessageBox::critical(this, tr("Invalid Own Key"), - tr("Own Key can not be use to do any operation. " - "Please go to the setting interface to select an " - "OwnKey and get a ServiceToken.")); + QMessageBox::critical(this, _("Invalid Own Key"), + _("Own Key can not be use to do any operation. " + "Please go to the setting interface to select an " + "OwnKey and get a ServiceToken.")); return {}; } @@ -57,8 +57,8 @@ QString MainWindow::getCryptText(const QString& shortenCryptoText) { QString serviceToken = settings.value("general/serviceToken").toString(); if (serviceToken.isEmpty() || !utils->checkServiceTokenFormat(serviceToken)) { QMessageBox::critical( - this, tr("Error"), - tr("Please obtain a Service Token from the server in the settings.")); + this, _("Error"), + _("Please obtain a Service Token from the server in the settings.")); return {}; } @@ -95,11 +95,10 @@ QString MainWindow::getCryptText(const QString& shortenCryptoText) { QNetworkReply* reply = utils->getNetworkManager().post(request, postData); - auto dialog = new WaitingDialog(tr("Getting Cpt From Server"), this); + auto dialog = new WaitingDialog(_("Getting Cpt From Server"), this); dialog->show(); - while (reply->isRunning()) - QApplication::processEvents(); + while (reply->isRunning()) QApplication::processEvents(); dialog->close(); @@ -117,9 +116,9 @@ QString MainWindow::getCryptText(const QString& shortenCryptoText) { if (!utils->checkDataValueStr("cryptoText") || !utils->checkDataValueStr("sha") || !utils->checkDataValueStr("serviceToken")) { - QMessageBox::critical(this, tr("Error"), - tr("The communication content with the server does " - "not meet the requirements")); + QMessageBox::critical(this, _("Error"), + _("The communication content with the server does " + "not meet the requirements")); return {}; } @@ -134,7 +133,7 @@ QString MainWindow::getCryptText(const QString& shortenCryptoText) { serviceToken == serviceTokenFromServer) { return cryptoText; } else - QMessageBox::critical(this, tr("Error"), tr("Invalid short ciphertext")); + QMessageBox::critical(this, _("Error"), _("Invalid short ciphertext")); return {}; } @@ -156,8 +155,8 @@ void MainWindow::shortenCryptText() { if (serviceToken.isEmpty() || !utils->checkServiceTokenFormat(serviceToken)) { QMessageBox::critical( - this, tr("Invalid Service Token"), - tr("Please go to the setting interface to get a ServiceToken.")); + this, _("Invalid Service Token"), + _("Please go to the setting interface to get a ServiceToken.")); return; } @@ -167,8 +166,8 @@ void MainWindow::shortenCryptText() { GpgKey key = mCtx->getKeyRefById(ownKeyId); if (!key.good) { - QMessageBox::critical(this, tr("Invalid Own Key"), - tr("Own Key can not be use to do any operation.")); + QMessageBox::critical(this, _("Invalid Own Key"), + _("Own Key can not be use to do any operation.")); return; } @@ -217,10 +216,9 @@ void MainWindow::shortenCryptText() { QNetworkReply* reply = networkAccessManager->post(request, postData); - auto* dialog = new WaitingDialog(tr("Getting Scpt From Server"), this); + auto* dialog = new WaitingDialog(_("Getting Scpt From Server"), this); dialog->show(); - while (reply->isRunning()) - QApplication::processEvents(); + while (reply->isRunning()) QApplication::processEvents(); dialog->close(); if (utils->checkServerReply(reply->readAll().constData())) { @@ -233,9 +231,9 @@ void MainWindow::shortenCryptText() { if (!utils->checkDataValueStr("shortenText") || !utils->checkDataValueStr("md5")) { - QMessageBox::critical(this, tr("Error"), - tr("The communication content with the server does " - "not meet the requirements")); + QMessageBox::critical(this, _("Error"), + _("The communication content with the server does " + "not meet the requirements")); return; } @@ -246,14 +244,14 @@ void MainWindow::shortenCryptText() { if (md5_generator.result().toHex() == utils->getDataValueStr("md5")) { auto* dialog = new ShowCopyDialog(shortenText, - tr("Notice: Use Decrypt & Verify operation to " - "decrypt this short crypto text."), + _("Notice: Use Decrypt & Verify operation to " + "decrypt this short crypto text."), this); dialog->show(); } else { QMessageBox::critical( - this, tr("Error"), - tr("There is a problem with the communication with the server")); + this, _("Error"), + _("There is a problem with the communication with the server")); return; } } -- cgit v1.2.3