diff options
author | Saturn&Eric <[email protected]> | 2021-07-16 10:44:24 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2021-07-16 10:44:24 +0000 |
commit | 9601cb5a96bbd59c7e4dd9ba9e74d26e8e50e8ee (patch) | |
tree | 8a90600e52122cc9d45e231fc562ae1e4d380e04 /src | |
parent | Merge branch 'develop' into main (diff) | |
parent | Improve the function of sending mail; (diff) | |
download | GpgFrontend-9601cb5a96bbd59c7e4dd9ba9e74d26e8e50e8ee.tar.gz GpgFrontend-9601cb5a96bbd59c7e4dd9ba9e74d26e8e50e8ee.zip |
Merge pull request #13 from saturneric/developv1.2.1
Version 1.2.1
Diffstat (limited to '')
-rw-r--r-- | src/ui/SendMailDialog.cpp | 173 | ||||
-rwxr-xr-x | src/ui/SettingsDialog.cpp | 200 | ||||
-rw-r--r-- | src/ui/keygen/KeygenDialog.cpp | 3 | ||||
-rw-r--r-- | src/ui/main_window/MainWindowSlotFunction.cpp | 27 |
4 files changed, 339 insertions, 64 deletions
diff --git a/src/ui/SendMailDialog.cpp b/src/ui/SendMailDialog.cpp new file mode 100644 index 00000000..5bea7cb2 --- /dev/null +++ b/src/ui/SendMailDialog.cpp @@ -0,0 +1,173 @@ +/** + * 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]> starting on May 12, 2021. + * + */ + +#include "ui/SendMailDialog.h" + +#include <utility> +#include "smtp/SmtpMime" + +SendMailDialog::SendMailDialog(QString text, QWidget *parent) + : QDialog(parent), appPath(qApp->applicationDirPath()), + settings(RESOURCE_DIR(appPath) + "/conf/gpgfrontend.ini", QSettings::IniFormat), mText(std::move(text)) { + + if (smtpAddress.isEmpty()) { + QMessageBox::critical(this, tr("Incomplete configuration"), + tr("The SMTP address is empty, please go to the setting interface to complete the configuration.")); + + deleteLater(); + return; + + } + + senderEdit = new QLineEdit(); + senderEdit->setText(defaultSender); + recipientEdit = new QTextEdit(); + recipientEdit->setPlaceholderText("One or more email addresses. Please use ; to separate."); + subjectEdit = new QLineEdit(); + + errorLabel = new QLabel(); + + qDebug() << "Send Mail Settings" << smtpAddress << username << password << defaultSender << connectionTypeSettings; + + confirmButton = new QPushButton("Confirm"); + + auto layout = new QGridLayout(); + layout->addWidget(new QLabel("Sender"), 0, 0); + layout->addWidget(senderEdit, 0, 1); + layout->addWidget(new QLabel("Recipient"), 1, 0); + layout->addWidget(recipientEdit, 1, 1); + layout->addWidget(new QLabel("Subject"), 2, 0); + layout->addWidget(subjectEdit, 2, 1); + layout->addWidget(confirmButton, 3, 1); + layout->addWidget(errorLabel, 4, 0, 1, 2); + + connect(confirmButton, SIGNAL(clicked(bool)), this, SLOT(slotConfirm())); + + this->setLayout(layout); + this->setWindowTitle("Send Mail"); + this->setModal(true); + this->setFixedWidth(320); + this->show(); +} + +bool SendMailDialog::check_email_address(const QString &str) { + return re_email.match(str).hasMatch(); +} + +void SendMailDialog::slotConfirm() { + + QString errString; + errorLabel->clear(); + + QStringList rcptStringList = recipientEdit->toPlainText().split(';'); + + if (rcptStringList.isEmpty()) { + errString.append(tr(" Recipient cannot be empty \n")); + } else { + for (const auto& reci : rcptStringList) { + qDebug() << "Receiver" << reci.trimmed(); + if (!check_email_address(reci.trimmed())) { + errString.append(tr(" One or more Recipient's Email Address is invalid \n")); + break; + } + } + } + if (senderEdit->text().isEmpty()) { + errString.append(tr(" Sender cannot be empty \n")); + } else if (!check_email_address(senderEdit->text())) { + errString.append(tr(" Sender's Email Address is invalid \n")); + } + + if (!errString.isEmpty()) { + errorLabel->setAutoFillBackground(true); + QPalette error = errorLabel->palette(); + error.setColor(QPalette::Window, "#ff8080"); + errorLabel->setPalette(error); + errorLabel->setText(errString); + return; + } + + SmtpClient::ConnectionType connectionType = SmtpClient::ConnectionType::TcpConnection; + + if (connectionTypeSettings == "SSL") { + connectionType = SmtpClient::ConnectionType::SslConnection; + } else if (connectionTypeSettings == "TLS") { + connectionType = SmtpClient::ConnectionType::TlsConnection; + } else if (connectionTypeSettings == "STARTTLS") { + connectionType = SmtpClient::ConnectionType::TlsConnection; + } else { + connectionType = SmtpClient::ConnectionType::TcpConnection; + } + + SmtpClient smtp(smtpAddress, port, connectionType); + + // We need to set the username (your email address) and the password + // for smtp authentification. + + smtp.setUser(username); + smtp.setPassword(password); + + // Now we create a MimeMessage object. This will be the email. + + MimeMessage message; + + message.setSender(new EmailAddress(senderEdit->text())); + for (const auto &reci : rcptStringList) { + if(!reci.isEmpty()) + message.addRecipient(new EmailAddress(reci.trimmed())); + } + message.setSubject(subjectEdit->text()); + + // Now add some text to the email. + // First we create a MimeText object. + + MimeText text; + + text.setText(mText); + + // Now add it to the mail + message.addPart(&text); + + // Now we can send the mail + if (!smtp.connectToHost()) { + qDebug() << "Connect to SMTP Server Failed"; + QMessageBox::critical(this, tr("Fail"), tr("Fail to Connect SMTP Server")); + return; + } + if (!smtp.login()) { + qDebug() << "Login to SMTP Server Failed"; + QMessageBox::critical(this, tr("Fail"), tr("Fail to Login into SMTP Server")); + return; + } + if (!smtp.sendMail(message)) { + qDebug() << "Send Mail to SMTP Server Failed"; + QMessageBox::critical(this, tr("Fail"), tr("Fail to Send Mail to SMTP Server")); + return; + } + smtp.quit(); + + // Close after sending email + QMessageBox::information(this, tr("Success"), tr("Succeed in Sending Mail to SMTP Server")); + deleteLater(); +} diff --git a/src/ui/SettingsDialog.cpp b/src/ui/SettingsDialog.cpp index 6bcdc971..d0a107c4 100755 --- a/src/ui/SettingsDialog.cpp +++ b/src/ui/SettingsDialog.cpp @@ -23,6 +23,8 @@ */ #include "ui/SettingsDialog.h" +#include "smtp/SmtpMime" +#include "ui/WaitingDialog.h" SettingsDialog::SettingsDialog(GpgME::GpgContext *ctx, QWidget *parent) : QDialog(parent) { @@ -30,14 +32,14 @@ SettingsDialog::SettingsDialog(GpgME::GpgContext *ctx, QWidget *parent) tabWidget = new QTabWidget; generalTab = new GeneralTab(mCtx); appearanceTab = new AppearanceTab; - mimeTab = new MimeTab; + sendMailTab = new SendMailTab; keyserverTab = new KeyserverTab; advancedTab = new AdvancedTab; gpgPathsTab = new GpgPathsTab; tabWidget->addTab(generalTab, tr("General")); tabWidget->addTab(appearanceTab, tr("Appearance")); - tabWidget->addTab(mimeTab, tr("PGP/Mime")); + tabWidget->addTab(sendMailTab, tr("Send Mail")); tabWidget->addTab(keyserverTab, tr("Key Server")); // tabWidget->addTab(gpgPathsTab, tr("Gpg paths")); tabWidget->addTab(advancedTab, tr("Advanced")); @@ -61,7 +63,7 @@ SettingsDialog::SettingsDialog(GpgME::GpgContext *ctx, QWidget *parent) SLOT(slotSetRestartNeeded(bool))); connect(appearanceTab, SIGNAL(signalRestartNeeded(bool)), this, SLOT(slotSetRestartNeeded(bool))); - connect(mimeTab, SIGNAL(signalRestartNeeded(bool)), this, + connect(sendMailTab, SIGNAL(signalRestartNeeded(bool)), this, SLOT(slotSetRestartNeeded(bool))); connect(keyserverTab, SIGNAL(signalRestartNeeded(bool)), this, SLOT(slotSetRestartNeeded(bool))); @@ -82,7 +84,7 @@ void SettingsDialog::slotSetRestartNeeded(bool needed) { void SettingsDialog::slotAccept() { generalTab->applySettings(); - mimeTab->applySettings(); + sendMailTab->applySettings(); appearanceTab->applySettings(); keyserverTab->applySettings(); advancedTab->applySettings(); @@ -279,51 +281,53 @@ void GeneralTab::slotOwnKeyIdChanged() { // Set ownKeyId to currently selected } -MimeTab::MimeTab(QWidget *parent) +SendMailTab::SendMailTab(QWidget *parent) : QWidget(parent), appPath(qApp->applicationDirPath()), settings(RESOURCE_DIR(appPath) + "/conf/gpgfrontend.ini", QSettings::IniFormat) { - /***************************************** - * MIME-Parsing-Box - *****************************************/ - auto *mimeQPBox = new QGroupBox(tr("Decode quoted printable")); - auto *mimeQPBoxLayout = new QVBoxLayout(); - mimeQPCheckBox = - new QCheckBox(tr("Try to recognize quoted printable."), this); - mimeQPBoxLayout->addWidget(mimeQPCheckBox); - mimeQPBox->setLayout(mimeQPBoxLayout); - - auto *mimeParseBox = new QGroupBox(tr("Parse PGP/MIME (Experimental)")); - auto *mimeParseBoxLayout = new QVBoxLayout(); - mimeParseCheckBox = new QCheckBox( - tr("Try to split attachments from PGP-MIME ecrypted messages."), this); - mimeParseBoxLayout->addWidget(mimeParseCheckBox); - mimeParseBox->setLayout(mimeParseBoxLayout); - - auto *mimeOpenAttachmentBox = - new QGroupBox(tr("Open with external application (Experimental)")); - auto *mimeOpenAttachmentBoxLayout = new QVBoxLayout(); - auto *mimeOpenAttachmentText = new QLabel(tr( - "Open attachments with default application for the filetype.<br> " - "There are at least two possible problems with this behaviour:" - "<ol><li>File needs to be saved unencrypted to attachments folder.<br> " - "Its your job to clean this folder.</li>" - "<li>The external application may have its own temp files.</li></ol>")); - - // mimeOpenAttachmentBox->setDisabled(true); - mimeOpenAttachmentCheckBox = - new QCheckBox(tr("Enable opening with external applications."), this); - - mimeOpenAttachmentBoxLayout->addWidget(mimeOpenAttachmentText); - mimeOpenAttachmentBoxLayout->addWidget(mimeOpenAttachmentCheckBox); - mimeOpenAttachmentBox->setLayout(mimeOpenAttachmentBoxLayout); - auto *mainLayout = new QVBoxLayout; - mainLayout->addWidget(mimeParseBox); - mainLayout->addWidget(mimeOpenAttachmentBox); - mainLayout->addWidget(mimeQPBox); - mainLayout->addStretch(1); - setLayout(mainLayout); + enableCheckBox = new QCheckBox(tr("Enable")); + enableCheckBox->setTristate(false); + + smtpAddress = new QLineEdit(); + username = new QLineEdit(); + password = new QLineEdit(); + password->setEchoMode(QLineEdit::Password); + + portSpin = new QSpinBox(); + portSpin->setMinimum(1); + portSpin->setMaximum(65535); + connectionTypeComboBox = new QComboBox(); + connectionTypeComboBox->addItem("None"); + connectionTypeComboBox->addItem("SSL"); + connectionTypeComboBox->addItem("TLS"); + connectionTypeComboBox->addItem("STARTTLS"); + + defaultSender = new QLineEdit();; + checkConnectionButton = new QPushButton("Check Connection"); + + auto layout = new QGridLayout(); + layout->addWidget(enableCheckBox, 0, 0); + layout->addWidget(new QLabel(tr("SMTP Address")), 1, 0); + layout->addWidget(smtpAddress, 1, 1, 1, 4); + layout->addWidget(new QLabel(tr("Username")), 2, 0); + layout->addWidget(username, 2, 1, 1, 4); + layout->addWidget(new QLabel(tr("Password")), 3, 0); + layout->addWidget(password, 3, 1, 1, 4); + layout->addWidget(new QLabel(tr("Port")), 4, 0); + layout->addWidget(portSpin, 4, 1, 1, 1); + layout->addWidget(new QLabel(tr("Connection Security")), 5, 0); + layout->addWidget(connectionTypeComboBox, 5, 1, 1, 1); + + layout->addWidget(new QLabel(tr("Default Sender")), 6, 0); + layout->addWidget(defaultSender, 6, 1, 1, 4); + layout->addWidget(checkConnectionButton, 7, 0); + + connect(enableCheckBox, SIGNAL(stateChanged(int)), this, SLOT(slotCheckBoxSetEnableDisable(int))); + connect(checkConnectionButton, SIGNAL(clicked(bool)), this, SLOT(slotCheckConnection())); + + + this->setLayout(layout); setSettings(); } @@ -332,30 +336,102 @@ MimeTab::MimeTab(QWidget *parent) * and set the buttons and checkboxes * appropriately **********************************/ -void MimeTab::setSettings() { - - // MIME-Parsing - if (settings.value("mime/parsemime").toBool()) - mimeParseCheckBox->setCheckState(Qt::Checked); +void SendMailTab::setSettings() { + + if (settings.value("sendMail/enable", false).toBool()) + enableCheckBox->setCheckState(Qt::Checked); + else { + enableCheckBox->setCheckState(Qt::Unchecked); + smtpAddress->setDisabled(true); + username->setDisabled(true); + password->setDisabled(true); + portSpin->setDisabled(true); + connectionTypeComboBox->setDisabled(true); + defaultSender->setDisabled(true); + checkConnectionButton->setDisabled(true); + } - // Qouted Printable - if (settings.value("mime/parseQP", true).toBool()) - mimeQPCheckBox->setCheckState(Qt::Checked); + smtpAddress->setText(settings.value("sendMail/smtpAddress", QString()).toString()); + username->setText(settings.value("sendMail/username", QString()).toString()); + password->setText(settings.value("sendMail/password", QString()).toString()); + portSpin->setValue(settings.value("sendMail/port", 25).toInt()); + connectionTypeComboBox->setCurrentText(settings.value("sendMail/connectionType", "None").toString()); + defaultSender->setText(settings.value("sendMail/defaultSender", QString()).toString()); - // Open Attachments with external app - if (settings.value("mime/openAttachment").toBool()) - mimeOpenAttachmentCheckBox->setCheckState(Qt::Checked); } /*********************************** * get the values of the buttons and * write them to settings-file *************************************/ -void MimeTab::applySettings() { - settings.setValue("mime/parsemime", mimeParseCheckBox->isChecked()); - settings.setValue("mime/parseQP", mimeQPCheckBox->isChecked()); - settings.setValue("mime/openAttachment", - mimeOpenAttachmentCheckBox->isChecked()); +void SendMailTab::applySettings() { + + settings.setValue("sendMail/smtpAddress", smtpAddress->text()); + settings.setValue("sendMail/username", username->text()); + settings.setValue("sendMail/password", password->text()); + settings.setValue("sendMail/port", portSpin->value()); + settings.setValue("sendMail/connectionType", connectionTypeComboBox->currentText()); + settings.setValue("sendMail/defaultSender", defaultSender->text()); + + settings.setValue("sendMail/enable", enableCheckBox->isChecked()); +} + +void SendMailTab::slotCheckConnection() { + + SmtpClient::ConnectionType connectionType = SmtpClient::ConnectionType::TcpConnection; + + if (connectionTypeComboBox->currentText() == "SSL") { + connectionType = SmtpClient::ConnectionType::SslConnection; + } else if (connectionTypeComboBox->currentText() == "TLS") { + connectionType = SmtpClient::ConnectionType::TlsConnection; + } else if (connectionTypeComboBox->currentText() == "STARTTLS") { + connectionType = SmtpClient::ConnectionType::TlsConnection; + } else { + connectionType = SmtpClient::ConnectionType::TcpConnection; + } + + SmtpClient smtp(smtpAddress->text(), portSpin->value(), connectionType); + + // We need to set the username (your email address) and the password + // for smtp authentification. + + smtp.setUser(username->text()); + smtp.setPassword(password->text()); + + bool if_success = true; + + if (!smtp.connectToHost()) { + QMessageBox::critical(this, tr("Fail"), tr("Fail to Connect SMTP Server")); + if_success = false; + } + if (if_success && !smtp.login()) { + QMessageBox::critical(this, tr("Fail"), tr("Fail to Login")); + if_success = false; + } + + if (if_success) + QMessageBox::information(this, tr("Success"), tr("Succeed in connecting and login")); + +} + +void SendMailTab::slotCheckBoxSetEnableDisable(int state) { + if (state == Qt::Checked) { + smtpAddress->setEnabled(true); + username->setEnabled(true); + password->setEnabled(true); + portSpin->setEnabled(true); + connectionTypeComboBox->setEnabled(true); + defaultSender->setEnabled(true); + checkConnectionButton->setEnabled(true); + } else { + smtpAddress->setDisabled(true); + username->setDisabled(true); + password->setDisabled(true); + portSpin->setDisabled(true); + connectionTypeComboBox->setDisabled(true); + defaultSender->setDisabled(true); + checkConnectionButton->setDisabled(true); + } } AppearanceTab::AppearanceTab(QWidget *parent) @@ -480,7 +556,7 @@ void AppearanceTab::setSettings() { // infoBoardFontSize auto infoBoardFontSize = settings.value("informationBoard/fontSize", 10).toInt(); - if(infoBoardFontSize < 9 || infoBoardFontSize > 18) + if (infoBoardFontSize < 9 || infoBoardFontSize > 18) infoBoardFontSize = 10; infoBoardFontSizeSpin->setValue(infoBoardFontSize); } diff --git a/src/ui/keygen/KeygenDialog.cpp b/src/ui/keygen/KeygenDialog.cpp index b9fdae3f..8637a643 100644 --- a/src/ui/keygen/KeygenDialog.cpp +++ b/src/ui/keygen/KeygenDialog.cpp @@ -74,8 +74,7 @@ void KeyGenDialog::slotKeyGenAccept() { * primary keys should have a reasonable expiration date (no more than 2 years in the future) */ if(dateEdit->dateTime() > QDateTime::currentDateTime().addYears(2)) { - - errorString.append(tr(" Expiration time no more than 2 years. ")); + errorString.append(tr(" Expiration time no more than 2 years. \n")); } if (errorString.isEmpty()) { diff --git a/src/ui/main_window/MainWindowSlotFunction.cpp b/src/ui/main_window/MainWindowSlotFunction.cpp index 0bddb9b2..8a4b786e 100644 --- a/src/ui/main_window/MainWindowSlotFunction.cpp +++ b/src/ui/main_window/MainWindowSlotFunction.cpp @@ -23,6 +23,7 @@ */ #include "MainWindow.h" +#include "ui/SendMailDialog.h" void MainWindow::slotEncrypt() { @@ -83,6 +84,19 @@ void MainWindow::slotEncrypt() { else infoBoard->slotRefresh(reportText, INFO_ERROR_WARN); + if (resultAnalyse->getStatus() >= 0) { + infoBoard->resetOptionActionsMenu(); + infoBoard->addOptionalAction("Send Mail", [this]() { + if(settings.value("sendMail/enable", false).toBool()) + new SendMailDialog(edit->curTextPage()->toPlainText(), this); + else { + QMessageBox::warning(nullptr, + tr("Function Disabled"), + tr("Please go to the settings interface to enable and configure this function.")); + } + }); + } + delete resultAnalyse; } else if (edit->slotCurPageFileTreeView() != nullptr) { this->slotFileEncrypt(); @@ -344,6 +358,19 @@ void MainWindow::slotEncryptSign() { else infoBoard->slotRefresh(reportText, INFO_ERROR_WARN); + if (status >= 0) { + infoBoard->resetOptionActionsMenu(); + infoBoard->addOptionalAction("Send Mail", [this]() { + if(settings.value("sendMail/enable", false).toBool()) + new SendMailDialog(edit->curTextPage()->toPlainText(), this); + else { + QMessageBox::warning(nullptr, + tr("Function Disabled"), + tr("Please go to the settings interface to enable and configure this function.")); + } + }); + } + delete resultAnalyseEncr; delete resultAnalyseSign; } else if (edit->slotCurPageFileTreeView() != nullptr) { |