diff options
author | Saturneric <[email protected]> | 2022-01-02 08:40:17 +0000 |
---|---|---|
committer | Saturneric <[email protected]> | 2022-01-02 08:40:17 +0000 |
commit | 5c9f64f4a41cc84a273db0726fe34388fe7b9935 (patch) | |
tree | 26ca494783647b63435710f956e8362befe8a64f | |
parent | <fixed>(core, ui): fixed bugs in result analyse (diff) | |
download | GpgFrontend-5c9f64f4a41cc84a273db0726fe34388fe7b9935.tar.gz GpgFrontend-5c9f64f4a41cc84a273db0726fe34388fe7b9935.zip |
<feat, refactor>(ui): change smtp test to multiply thread
-rw-r--r-- | src/ui/function/FileReadThread.h | 3 | ||||
-rw-r--r-- | src/ui/function/SMTPSendMailThread.cpp | 88 | ||||
-rw-r--r-- | src/ui/function/SMTPSendMailThread.h | 85 | ||||
-rw-r--r-- | src/ui/function/SMTPTestThread.cpp | 43 | ||||
-rw-r--r-- | src/ui/function/SMTPTestThread.h | 69 | ||||
-rw-r--r-- | src/ui/settings/SettingsSendMail.cpp | 209 | ||||
-rw-r--r-- | src/ui/settings/SettingsSendMail.h | 6 | ||||
-rw-r--r-- | src/ui/smtp/SendMailDialog.cpp | 6 |
8 files changed, 418 insertions, 91 deletions
diff --git a/src/ui/function/FileReadThread.h b/src/ui/function/FileReadThread.h index ebfcfb3c..46ed6cbc 100644 --- a/src/ui/function/FileReadThread.h +++ b/src/ui/function/FileReadThread.h @@ -26,6 +26,7 @@ #define GPGFRONTEND_FILEREADTHREAD_H #include "ui/GpgFrontendUI.h" + namespace GpgFrontend::UI { class FileReadThread : public QThread { @@ -35,7 +36,7 @@ class FileReadThread : public QThread { explicit FileReadThread(std::string path); signals: - + void sendReadBlock(const QString& block); void readDone(); diff --git a/src/ui/function/SMTPSendMailThread.cpp b/src/ui/function/SMTPSendMailThread.cpp new file mode 100644 index 00000000..94e97749 --- /dev/null +++ b/src/ui/function/SMTPSendMailThread.cpp @@ -0,0 +1,88 @@ +/** + * 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. + * + * Foobar 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 Foobar. If not, see <https://www.gnu.org/licenses/>. + * + * The initial version of the source code is inherited from gpg4usb-team. + * Their source code version also complies with GNU General Public License. + * + * The source code version of this software was modified and released + * by Saturneric<[email protected]><[email protected]> starting on May 12, 2021. + * + */ + +#include "SMTPSendMailThread.h" + +void SMTPSendMailThread::run() { + SmtpClient smtp(host_.c_str(), port_, connection_type_); + + if (identify_) { + smtp.setUser(username_.c_str()); + smtp.setPassword(password_.c_str()); + } + + // Now we can send the mail + if (!smtp.connectToHost()) { + emit signalSMTPResult("Fail to connect SMTP server"); + return; + } + if (!smtp.login()) { + emit signalSMTPResult("Fail to login"); + return; + } + if (!smtp.sendMail(message)) { + emit signalSMTPResult("Fail to send mail"); + return; + } + smtp.quit(); + emit signalSMTPResult("Succeed in sending a test email"); +} + +void SMTPSendMailThread::setBCC(const QString& bccs) { + QStringList bcc_string_list = bccs.split(';'); + for (const auto& bcc : bcc_string_list) { + if (!bcc.isEmpty()) message.addBcc(new EmailAddress(bcc.trimmed())); + } +} + +void SMTPSendMailThread::setCC(const QString& ccs) { + QStringList cc_string_list = ccs.split(';'); + for (const auto& cc : cc_string_list) { + if (!cc.isEmpty()) message.addCc(new EmailAddress(cc.trimmed())); + } +} + +void SMTPSendMailThread::setRecipient(const QString& recipients) { + QStringList rcpt_string_list = recipients.split(';'); + for (const auto& rcpt : rcpt_string_list) { + if (!rcpt.isEmpty()) message.addRecipient(new EmailAddress(rcpt.trimmed())); + } +} + +void SMTPSendMailThread::setSender(const QString& sender) { + message.setSender(new EmailAddress(sender)); +} + +void SMTPSendMailThread::addTextContent(const QString& content) { + auto text = std::make_unique<MimeText>(content); + texts_.push_back(std::move(text)); + message.addPart(texts_.back().get()); +} + +void SMTPSendMailThread::addFileContent(const QString& file_name, + const QByteArray& content) { + auto file = std::make_unique<MimeFile>(content, file_name); + files_.push_back(std::move(file)); + message.addPart(files_.back().get()); +} diff --git a/src/ui/function/SMTPSendMailThread.h b/src/ui/function/SMTPSendMailThread.h new file mode 100644 index 00000000..ff417f5d --- /dev/null +++ b/src/ui/function/SMTPSendMailThread.h @@ -0,0 +1,85 @@ +/** + * 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. + * + * Foobar 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 Foobar. If not, see <https://www.gnu.org/licenses/>. + * + * The initial version of the source code is inherited from gpg4usb-team. + * Their source code version also complies with GNU General Public License. + * + * The source code version of this software was modified and released + * by Saturneric<[email protected]><[email protected]> starting on May 12, 2021. + * + */ + +#ifndef GPGFRONTEND_SMTPSENDMAILTHREAD_H +#define GPGFRONTEND_SMTPSENDMAILTHREAD_H + +#include <utility> + +#ifdef SMTP_SUPPORT +#include "smtp/SmtpMime" +#endif + +#include "ui/GpgFrontendUI.h" + +class SMTPSendMailThread : public QThread { + Q_OBJECT + public: + explicit SMTPSendMailThread(std::string host, int port, + SmtpClient::ConnectionType connection_type, + bool identify, std::string username, + std::string password, QWidget* parent = nullptr) + : QThread(parent), + host_(std::move(host)), + port_(port), + connection_type_(connection_type), + identify_(identify), + username_(std::move(username)), + password_(std::move(password)) {} + + void setSender(const QString& sender); + + void setRecipient(const QString& recipients); + + void setCC(const QString& ccs); + + void setBCC(const QString& bccs); + + void setSubject(const QString& subject) { message.setSubject(subject); } + + void addTextContent(const QString& content); + + void addFileContent(const QString& file_name, const QByteArray& content); + + signals: + void signalSMTPResult(const QString& result); + + protected: + void run() override; + + private: + std::string host_; + int port_; + SmtpClient::ConnectionType connection_type_; + + bool identify_; + std::string username_; + std::string password_; + + MimeMessage message; + std::vector<std::unique_ptr<MimeText>> texts_; + std::vector<std::unique_ptr<MimeFile>> files_; +}; + +#endif // GPGFRONTEND_SMTPSENDMAILTHREAD_H diff --git a/src/ui/function/SMTPTestThread.cpp b/src/ui/function/SMTPTestThread.cpp new file mode 100644 index 00000000..63c41a90 --- /dev/null +++ b/src/ui/function/SMTPTestThread.cpp @@ -0,0 +1,43 @@ +/** + * 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. + * + * Foobar 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 Foobar. If not, see <https://www.gnu.org/licenses/>. + * + * The initial version of the source code is inherited from gpg4usb-team. + * Their source code version also complies with GNU General Public License. + * + * The source code version of this software was modified and released + * by Saturneric<[email protected]><[email protected]> starting on May 12, 2021. + * + */ + +#include "SMTPTestThread.h" + +void SMTPTestThread::run() { + SmtpClient smtp(host_.c_str(), port_, connection_type_); + if (identify_) { + smtp.setUser(username_.c_str()); + smtp.setPassword(password_.c_str()); + } + if (!smtp.connectToHost()) { + emit signalSMTPTestResult("Fail to connect SMTP server"); + return; + } + if (!smtp.login()) { + emit signalSMTPTestResult("Fail to login"); + return; + } + smtp.quit(); + emit signalSMTPTestResult("Succeed in testing connection"); +} diff --git a/src/ui/function/SMTPTestThread.h b/src/ui/function/SMTPTestThread.h new file mode 100644 index 00000000..e2f2048d --- /dev/null +++ b/src/ui/function/SMTPTestThread.h @@ -0,0 +1,69 @@ +/** + * 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. + * + * Foobar 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 Foobar. If not, see <https://www.gnu.org/licenses/>. + * + * The initial version of the source code is inherited from gpg4usb-team. + * Their source code version also complies with GNU General Public License. + * + * The source code version of this software was modified and released + * by Saturneric<[email protected]><[email protected]> starting on May 12, 2021. + * + */ + +#ifndef GPGFRONTEND_SMTPTESTTHREAD_H +#define GPGFRONTEND_SMTPTESTTHREAD_H + +#include <utility> + +#ifdef SMTP_SUPPORT +#include "smtp/SmtpMime" +#endif + +#include "ui/GpgFrontendUI.h" + +class SMTPTestThread : public QThread { + Q_OBJECT + public: + explicit SMTPTestThread(std::string host, int port, + SmtpClient::ConnectionType connection_type, + bool identify, std::string username, + std::string password, QWidget* parent = nullptr) + : QThread(parent), + host_(std::move(host)), + port_(port), + connection_type_(connection_type), + identify_(identify), + username_(std::move(username)), + password_(std::move(password)) { + + } + + signals: + void signalSMTPTestResult(const QString& result); + + protected: + void run() override; + + private: + std::string host_; + int port_; + SmtpClient::ConnectionType connection_type_; + + bool identify_; + std::string username_; + std::string password_; +}; + +#endif // GPGFRONTEND_SMTPTESTTHREAD_H diff --git a/src/ui/settings/SettingsSendMail.cpp b/src/ui/settings/SettingsSendMail.cpp index 733ed82f..742c0644 100644 --- a/src/ui/settings/SettingsSendMail.cpp +++ b/src/ui/settings/SettingsSendMail.cpp @@ -28,6 +28,8 @@ #include "smtp/SmtpMime" #endif +#include "ui/function/SMTPSendMailThread.h" +#include "ui/function/SMTPTestThread.h" #include "ui/settings/GlobalSettingStation.h" #include "ui_SendMailSettings.h" @@ -62,6 +64,17 @@ SendMailTab::SendMailTab(QWidget* parent) ui->passwordEdit->setDisabled(state != Qt::Checked); }); + connect(ui->connextionSecurityComboBox, &QComboBox::currentTextChanged, this, + [=](const QString& current_text) { + if (current_text == "SSL") { + connection_type_ = SmtpClient::ConnectionType::SslConnection; + } else if (current_text == "TLS" || current_text == "STARTTLS") { + connection_type_ = SmtpClient::ConnectionType::TlsConnection; + } else { + connection_type_ = SmtpClient::ConnectionType::TcpConnection; + } + }); + ui->generalGroupBox->setTitle(_("General")); ui->identityGroupBox->setTitle(_("Identity Information")); ui->preferenceGroupBox->setTitle(_("Preference")); @@ -138,7 +151,7 @@ void SendMailTab::setSettings() { } catch (...) { LOG(ERROR) << _("Setting Operation Error") << _("default_sender"); } - + ui->identityCheckBox->setCheckState(Qt::Unchecked); try { bool identity_enable = settings.lookup("smtp.identity_enable"); @@ -231,105 +244,129 @@ void SendMailTab::applySettings() { #ifdef SMTP_SUPPORT void SendMailTab::slotCheckConnection() { - SmtpClient::ConnectionType connectionType; - const auto selectedConnType = ui->connextionSecurityComboBox->currentText(); - if (selectedConnType == "SSL") { - connectionType = SmtpClient::ConnectionType::SslConnection; - } else if (selectedConnType == "TLS" || selectedConnType == "STARTTLS") { - connectionType = SmtpClient::ConnectionType::TlsConnection; - } else { - connectionType = SmtpClient::ConnectionType::TcpConnection; - } - - SmtpClient smtp(ui->smtpServerAddressEdit->text(), ui->portSpin->value(), - connectionType); - - if (ui->identityCheckBox->isChecked()) { - smtp.setUser(ui->usernameEdit->text()); - smtp.setPassword(ui->passwordEdit->text()); - } + auto host = ui->smtpServerAddressEdit->text().toStdString(); + auto port = ui->portSpin->value(); + auto connection_type = connection_type_; + bool identity_needed = ui->identityCheckBox->isChecked(); + auto username = ui->usernameEdit->text().toStdString(); + auto password = ui->passwordEdit->text().toStdString(); + + auto thread = new SMTPTestThread(host, port, connection_type, identity_needed, + username, password); + + // Waiting Dialog + auto* waiting_dialog = new QProgressDialog(this); + waiting_dialog->setMaximum(0); + waiting_dialog->setMinimum(0); + auto waiting_dialog_label = + new QLabel(QString(_("Test SMTP Connection...")) + "<br /><br />" + + _("If the process does not end for a long time, please check " + "again whether your SMTP server configuration is correct.")); + waiting_dialog_label->setWordWrap(true); + waiting_dialog->setLabel(waiting_dialog_label); + waiting_dialog->resize(420, 120); + connect(thread, &SMTPTestThread::signalSMTPTestResult, this, + &SendMailTab::slotTestSMTPConnectionResult); + connect(thread, &QThread::finished, [=]() { + waiting_dialog->finished(0); + waiting_dialog->deleteLater(); + }); + connect(waiting_dialog, &QProgressDialog::canceled, [=]() { + LOG(INFO) << "cancel clicked"; + if (thread->isRunning()) thread->terminate(); + QCoreApplication::quit(); + exit(0); + }); - if (!smtp.connectToHost()) { - QMessageBox::critical(this, _("Fail"), _("Fail to Connect SMTP Server")); - ui->senTestMailButton->setDisabled(true); - return; - } - if (!smtp.login()) { - QMessageBox::critical(this, _("Fail"), _("Fail to Login")); - ui->senTestMailButton->setDisabled(true); - return; - } + // Show Waiting Dialog + waiting_dialog->show(); + waiting_dialog->setFocus(); - QMessageBox::information(this, _("Success"), - _("Succeed in connecting and login")); - ui->senTestMailButton->setDisabled(false); + thread->start(); + QEventLoop loop; + connect(thread, &QThread::finished, &loop, &QEventLoop::quit); + loop.exec(); } #endif #ifdef SMTP_SUPPORT void SendMailTab::slotSendTestMail() { - if (ui->defaultSenderEmailEdit->text().isEmpty()) { - QMessageBox::critical(this, _("Fail"), _("Given a default sender first")); - return; - } - - SmtpClient::ConnectionType connectionType; - const auto selectedConnType = ui->connextionSecurityComboBox->currentText(); - if (selectedConnType == "SSL") { - connectionType = SmtpClient::ConnectionType::SslConnection; - } else if (selectedConnType == "TLS" || selectedConnType == "STARTTLS") { - connectionType = SmtpClient::ConnectionType::TlsConnection; - } else { - connectionType = SmtpClient::ConnectionType::TcpConnection; - } - - SmtpClient smtp(ui->smtpServerAddressEdit->text(), ui->portSpin->value(), - connectionType); + auto host = ui->smtpServerAddressEdit->text().toStdString(); + auto port = ui->portSpin->value(); + auto connection_type = connection_type_; + bool identity_needed = ui->identityCheckBox->isChecked(); + auto username = ui->usernameEdit->text().toStdString(); + auto password = ui->passwordEdit->text().toStdString(); + auto sender_address = ui->defaultSenderEmailEdit->text(); - if (ui->identityCheckBox->isChecked()) { - smtp.setUser(ui->usernameEdit->text()); - smtp.setPassword(ui->passwordEdit->text()); - } + auto thread = new SMTPSendMailThread(host, port, connection_type, + identity_needed, username, password); + + // Waiting Dialog + auto* waiting_dialog = new QProgressDialog(this); + waiting_dialog->setMaximum(0); + waiting_dialog->setMinimum(0); + auto waiting_dialog_label = + new QLabel(QString(_("Test SMTP Send Mail Ability...")) + "<br /><br />" + + _("If the process does not end for a long time, please check " + "again whether your SMTP server configuration is correct.")); + waiting_dialog_label->setWordWrap(true); + waiting_dialog->setLabel(waiting_dialog_label); + waiting_dialog->resize(420, 120); + connect(thread, &SMTPSendMailThread::signalSMTPResult, this, + &SendMailTab::slotTestSMTPConnectionResult); + connect(thread, &QThread::finished, [=]() { + waiting_dialog->finished(0); + waiting_dialog->deleteLater(); + }); + connect(waiting_dialog, &QProgressDialog::canceled, [=]() { + LOG(INFO) << "cancel clicked"; + if (thread->isRunning()) thread->terminate(); + QCoreApplication::quit(); + exit(0); + }); - MimeMessage message; + thread->setSender(sender_address); + thread->setRecipient(sender_address); + thread->setSubject(_("Test Email from GpgFrontend")); + thread->addTextContent( + _("Hello, this is a test email from GpgFrontend. If you receive this " + "email, it means that you have configured the correct SMTP server " + "parameters.")); + + // Show Waiting Dialog + waiting_dialog->show(); + waiting_dialog->setFocus(); + + thread->start(); + QEventLoop loop; + connect(thread, &QThread::finished, &loop, &QEventLoop::quit); + loop.exec(); +} - auto sender_address = ui->defaultSenderEmailEdit->text(); - message.setSender(new EmailAddress(sender_address)); - message.addRecipient(new EmailAddress(sender_address)); - message.setSubject(_("Test Email from GpgFrontend")); - - MimeText text; - text.setText(_("Hello, this is a test email from GpgFrontend.")); - // Now add it to the mail - message.addPart(&text); - // Now we can send the mail - if (!smtp.connectToHost()) { - LOG(INFO) << "Connect to SMTP Server Failed"; - QMessageBox::critical(this, _("Fail"), _("Fail to Connect SMTP Server")); +void SendMailTab::slotTestSMTPConnectionResult(const QString& result) { + if (result == "Fail to connect SMTP server") { + QMessageBox::critical(this, _("Fail"), _("Fail to Connect SMTP Server.")); ui->senTestMailButton->setDisabled(true); - return; - } - if (!smtp.login()) { - LOG(INFO) << "Login to SMTP Server Failed"; - QMessageBox::critical(this, _("Fail"), _("Fail to Login into SMTP Server")); + } else if (result == "Fail to login") { + QMessageBox::critical(this, _("Fail"), _("Fail to Login.")); ui->senTestMailButton->setDisabled(true); - return; - } - if (!smtp.sendMail(message)) { - LOG(INFO) << "Send Mail to SMTP Server Failed"; - QMessageBox::critical( - this, _("Fail"), - _("Fail to send a test email to the SMTP Server, please " - "recheck your configuration.")); + } else if (result == "Fail to send mail") { + QMessageBox::critical(this, _("Fail"), _("Fail to Login.")); + ui->senTestMailButton->setDisabled(true); + } else if (result == "Succeed in testing connection") { + QMessageBox::information(this, _("Success"), + _("Succeed in connecting and login")); + ui->senTestMailButton->setDisabled(false); + } else if (result == "Succeed in sending a test email") { + QMessageBox::information( + this, _("Success"), + _("Succeed in sending a test email to the SMTP Server")); + ui->senTestMailButton->setDisabled(false); + } else { + QMessageBox::critical(this, _("Fail"), _("Unknown error.")); ui->senTestMailButton->setDisabled(true); - return; } - smtp.quit(); - - // Close after sending email - QMessageBox::information( - this, _("Success"), - _("Succeed in sending a test email to the SMTP Server")); } #endif diff --git a/src/ui/settings/SettingsSendMail.h b/src/ui/settings/SettingsSendMail.h index cc733f28..39ae0b71 100644 --- a/src/ui/settings/SettingsSendMail.h +++ b/src/ui/settings/SettingsSendMail.h @@ -5,6 +5,7 @@ #ifndef GPGFRONTEND_SETTINGSSENDMAIL_H #define GPGFRONTEND_SETTINGSSENDMAIL_H +#include "smtp/SmtpMime" #include "ui/GpgFrontendUI.h" class Ui_SendMailSettings; @@ -22,6 +23,8 @@ class SendMailTab : public QWidget { private slots: + void slotTestSMTPConnectionResult(const QString& result); + #ifdef SMTP_SUPPORT void slotCheckConnection(); @@ -32,7 +35,8 @@ class SendMailTab : public QWidget { std::shared_ptr<Ui_SendMailSettings> ui; QRegularExpression re_email{ R"((?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\]))"}; - + SmtpClient::ConnectionType connection_type_ = + SmtpClient::ConnectionType::TcpConnection; signals: void signalRestartNeeded(bool needed); diff --git a/src/ui/smtp/SendMailDialog.cpp b/src/ui/smtp/SendMailDialog.cpp index 374b3875..66bfc546 100644 --- a/src/ui/smtp/SendMailDialog.cpp +++ b/src/ui/smtp/SendMailDialog.cpp @@ -181,13 +181,13 @@ void SendMailDialog::slotConfirm() { MimeMessage message; message.setSender(new EmailAddress(ui->senderEdit->text())); - for (const auto& reci : rcpt_string_list) { - if (!reci.isEmpty()) message.addRecipient(new EmailAddress(reci.trimmed())); + for (const auto& rcpt : rcpt_string_list) { + if (!rcpt.isEmpty()) message.addRecipient(new EmailAddress(rcpt.trimmed())); } for (const auto& cc : cc_string_list) { if (!cc.isEmpty()) message.addCc(new EmailAddress(cc.trimmed())); } - for (const auto& bcc : cc_string_list) { + for (const auto& bcc : bcc_string_list) { if (!bcc.isEmpty()) message.addBcc(new EmailAddress(bcc.trimmed())); } message.setSubject(ui->subjectEdit->text()); |