Skip to content

Instantly share code, notes, and snippets.

@DeiVadder
Created May 23, 2017 05:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DeiVadder/59fbb28d4d98d07858dde3682955e099 to your computer and use it in GitHub Desktop.
Save DeiVadder/59fbb28d4d98d07858dde3682955e099 to your computer and use it in GitHub Desktop.
SmtpTest
#include "smtpemail.h"
#include "ui_smtpemail.h"
#include "sslsocket.h"
#include <QTimer>
#include <QSettings>
#include <QThread>
#include <QDebug>
QString nl = "\r\n";
QString csv= " <=> ";
SmtpEmail::SmtpEmail(QWidget *parent) :
QFrame(parent),
ui(new Ui::SmtpEmail)
{
qDebug() << "Support SSL: " << QSslSocket::supportsSsl()
<< "\nLib Version Number: " << QSslSocket::sslLibraryVersionNumber()
<< "\nLib Version String: " << QSslSocket::sslLibraryVersionString()
<< "\nLib Build Version Number: " << QSslSocket::sslLibraryBuildVersionNumber()
<< "\nLib Build Version String: " << QSslSocket::sslLibraryBuildVersionString();
ui->setupUi(this);
#ifdef Q_OS_MACOS
ui->verticalLayout_4->setContentsMargins(12,12,52,52);
ui->verticalLayout_5->setContentsMargins(12,12,52,52);
#endif
SslSocket *smtp = new SslSocket("smtp.gmail.com",465,SmtpClient::SslConnection);
QThread *thread = new QThread();
smtp->initSslSocket();
smtp->moveToThread(thread);
connect(thread, &QThread::started, smtp, &SslSocket::initSslSocket);
connect(thread, &QThread::finished, smtp, &SslSocket::deleteLater);
connect(thread, &QThread::finished, thread, &QThread::deleteLater);
connect(this, &SmtpEmail::destroyed, thread, &QThread::quit);
/* Testing
QTimer *timer = new QTimer();
timer->moveToThread(&smtpThread);
timer->setInterval(500);
connect(timer, &QTimer::timeout, this, &SmtpEmail::msg1,Qt::DirectConnection);
connect(&smtpThread, &QThread::started, timer, QOverload<>::of(&QTimer::start));
smtpThread.start();
qDebug() << "IsRunning"<<smtpThread.isRunning();
connect(smtp, &SslSocket::errorMsg, smtp,[=](QString s){qDebug()<< "Queued:"<< s;},Qt::QueuedConnection );
connect(smtp, &SslSocket::errorMsg, smtp,[=](QString s){qDebug()<< "Direct:"<< s;},Qt::DirectConnection );
connect(smtp, &SslSocket::errorMsg, this, &SmtpEmail::msg1, Qt::QueuedConnection/ *Qt::QueuedConnection* /);
connect(smtp, &SslSocket::errorMsg, this, &SmtpEmail::msg2/*, Qt::DirectConnection*/);
*/
connect(smtp, &SslSocket::errorMsg, this, &SmtpEmail::errorManagement/*,Qt::DirectConnection*/);
connect(smtp, &SslSocket::allDataAvailable, smtp, &SslSocket::sendMail);
connect(smtp, &SslSocket::allDone, this, &SmtpEmail::stopAnimation);
connect(smtp, &SslSocket::allDone, this, [=]{lastContact = QDateTime::currentDateTime(); qDebug() << "Signal check";});
connect(this, &SmtpEmail::setPw, smtp, &SslSocket::setpw);
connect(this, &SmtpEmail::setSubject, smtp, &SslSocket::setSubject);
connect(this, &SmtpEmail::setSender, smtp, &SslSocket::setSender);
connect(this, &SmtpEmail::setCustMail, smtp, &SslSocket::setCustMail);
connect(this, &SmtpEmail::setText, smtp, &SslSocket::setText);
connect(this, &SmtpEmail::setFoe, smtp, &SslSocket::setFoe);
connect(this, &SmtpEmail::setInfo, smtp, &SslSocket::setInfo);
connect(this, &SmtpEmail::setModbus, smtp, &SslSocket::setModbus);
connect(this, &SmtpEmail::doGesture, ui->stackedWidgetSupport, &jhStackedWidget::setSlidePermission);
connect(this, &SmtpEmail::mouseEvent, ui->stackedWidgetSupport, &jhStackedWidget::mouseEvent);
connect(ui->btnBack, &cButton::signalMouseEvent, ui->stackedWidgetSupport, &jhStackedWidget::mouseEvent);
connect(ui->btnNext, &cButton::signalMouseEvent, ui->stackedWidgetSupport, &jhStackedWidget::mouseEvent);
connect(ui->btnExit, &cButton::signalMouseEvent, ui->stackedWidgetSupport, &jhStackedWidget::mouseEvent);
connect(ui->listWidget, &jhListWidget::signalMouseEvent, ui->stackedWidgetSupport, &jhStackedWidget::mouseEvent);
thread->start();
QTimer::singleShot(1000,this,[=]{
QString pw;
for(auto s : l)
pw.append(s+trenner);
pw.append("24P");
emit setPw(pw);
});
ui->stackedWidgetSupport->disableBTTB(true);
ui->stackedWidgetSupport->setMinMoveDistanceY(-1);
qDebug() << "MinMoveDistance Y = " << ui->stackedWidgetSupport->getMinMoveDistanceY();
connect(ui->btnExit, &cButton::clicked, this, &SmtpEmail::close);
connect(this, &SmtpEmail::close, this, &SmtpEmail::resetClass);
connect(ui->btnBack, &cButton::clicked, this, [=]{emit doGesture(false);},Qt::DirectConnection);
connect(ui->btnBack, &cButton::clicked, ui->stackedWidgetSupport, &jhStackedWidget::slideInBack, Qt::QueuedConnection);
connect(ui->btnNext, &cButton::clicked, this, &SmtpEmail::nextSide);
connect(ui->stackedWidgetSupport, &jhStackedWidget::currentChanged, this, &SmtpEmail::onStackChange);
connect(ui->cBoxFoe_1, &SmtpCheckBox::checkedSet, ui->cBoxFoe_2, &SmtpCheckBox::setChecked);
connect(ui->cBoxFoe_2, &SmtpCheckBox::checkedSet, ui->cBoxFoe_1, &SmtpCheckBox::setChecked);
connect(ui->cBoxFoe_3, &SmtpCheckBox::checkedSet, ui->cBoxFoe_4, &SmtpCheckBox::setChecked);
connect(ui->cBoxFoe_4, &SmtpCheckBox::checkedSet, ui->cBoxFoe_3, &SmtpCheckBox::setChecked);
connect(ui->cBoxFoe_5, &SmtpCheckBox::checkedSet, ui->cBoxFoe_6, &SmtpCheckBox::setChecked);
connect(ui->cBoxFoe_6, &SmtpCheckBox::checkedSet, ui->cBoxFoe_5, &SmtpCheckBox::setChecked);
connect(ui->cBoxFoe_7, &SmtpCheckBox::checkedSet, ui->cBoxFoe_8, &SmtpCheckBox::setChecked);
connect(ui->cBoxFoe_8, &SmtpCheckBox::checkedSet, ui->cBoxFoe_7, &SmtpCheckBox::setChecked);
connect(ui->cBoxFoe_9, &SmtpCheckBox::checkedSet, ui->cBoxFoe_10,&SmtpCheckBox::setChecked);
connect(ui->cBoxFoe_10,&SmtpCheckBox::checkedSet, ui->cBoxFoe_9, &SmtpCheckBox::setChecked);
connect(ui->cBoxFoe_11,&SmtpCheckBox::checkedSet, ui->cBoxFoe_12,&SmtpCheckBox::setChecked);
connect(ui->cBoxFoe_12,&SmtpCheckBox::checkedSet, ui->cBoxFoe_11,&SmtpCheckBox::setChecked);
connect(ui->cBoxFoe_13,&SmtpCheckBox::checkedSet, ui->cBoxFoe_14,&SmtpCheckBox::setChecked);
connect(ui->cBoxFoe_14,&SmtpCheckBox::checkedSet, ui->cBoxFoe_13,&SmtpCheckBox::setChecked);
connect(ui->cBoxInfo_1, &SmtpCheckBox::checkedSet, ui->cBoxInfo_2, &SmtpCheckBox::setChecked);
connect(ui->cBoxInfo_2, &SmtpCheckBox::checkedSet, ui->cBoxInfo_1, &SmtpCheckBox::setChecked);
connect(ui->cBoxInfo_3, &SmtpCheckBox::checkedSet, ui->cBoxInfo_4, &SmtpCheckBox::setChecked);
connect(ui->cBoxInfo_4, &SmtpCheckBox::checkedSet, ui->cBoxInfo_3, &SmtpCheckBox::setChecked);
connect(ui->cBoxInfo_5, &SmtpCheckBox::checkedSet, ui->cBoxInfo_6, &SmtpCheckBox::setChecked);
connect(ui->cBoxInfo_6, &SmtpCheckBox::checkedSet, ui->cBoxInfo_5, &SmtpCheckBox::setChecked);
connect(ui->cBoxInfo_7, &SmtpCheckBox::checkedSet, ui->cBoxInfo_8, &SmtpCheckBox::setChecked);
connect(ui->cBoxInfo_8, &SmtpCheckBox::checkedSet, ui->cBoxInfo_7, &SmtpCheckBox::setChecked);
connect(ui->cBoxInfo_9, &SmtpCheckBox::checkedSet, ui->cBoxInfo_10,&SmtpCheckBox::setChecked);
connect(ui->cBoxInfo_10,&SmtpCheckBox::checkedSet, ui->cBoxInfo_9, &SmtpCheckBox::setChecked);
connect(ui->cBoxInfo_11,&SmtpCheckBox::checkedSet, ui->cBoxInfo_12,&SmtpCheckBox::setChecked);
connect(ui->cBoxInfo_12,&SmtpCheckBox::checkedSet, ui->cBoxInfo_11,&SmtpCheckBox::setChecked);
for(int i = 1; i < 15; i++){
QCheckBox *cBox = findChild<QCheckBox *>("cBoxFoe_"+QString::number(i));
if(cBox) connect(cBox, &QCheckBox::clicked, this, &SmtpEmail::checkFoe);
else qDebug() << "Foe failed";
cBox = findChild<QCheckBox*>("cBoxInfo_"+QString::number(i));
if(cBox) connect(cBox, &QCheckBox::clicked, this, &SmtpEmail::checkInfo);
else qDebug() << "Info failed";
}
connect(ui->leInfo_1, &QLineEdit::textEdited, this, &SmtpEmail::checkInfo);
connect(ui->leInfo_2, &QLineEdit::textEdited, this, &SmtpEmail::checkInfo);
connect(ui->leInfo_3, &QLineEdit::textEdited, this, &SmtpEmail::checkInfo);
connect(ui->leInfo_4, &QLineEdit::textEdited, this, &SmtpEmail::checkInfo);
connect(ui->leInfo_5, &QLineEdit::textEdited, this, &SmtpEmail::checkInfo);
resetClass();
settingsRead();
}
SmtpEmail::~SmtpEmail()
{
delete ui;
}
void SmtpEmail::setModbusAttachment(const QList<QString> &lModbusRegCoils)
{
QString modbus;
for(auto s: lModbusRegCoils)
modbus.append(s+nl);
emit setModbus(modbus);
}
void SmtpEmail::sendEmail()
{
qDebug() << "sendEmail";
emit setAnimationText(tr("Bitte warten, E-Mail wird versendet ..."));
emit hideProgress();
emit startAnimation();
QString subject = ui->leBetreff->text().isEmpty() ? getDefaultSubject() : ui->leBetreff->text();
emit setSubject(subject);
QString sender = ui->leVon->text().isEmpty() ? tr("Unbekannt") : ui->leVon->text();
emit setSender(sender);
if(!ui->leCc->text().isEmpty())
emit setCustMail(ui->leCc->text(), ui->leCc->text());
else
emit setCustMail(QString(),QString());
QString standart("In diese E-Mail wurde als Support-Request am "
+ QDate::currentDate().toString("yyyy.MM.dd")+
" erstellt. \r\n"
"Die verwendete Version von uConfig ist v"
+version+"\r\n"
"Letze Kontakt via uConfig war am:"
+ lastContact.toString() + "\r\n"
"Der Kunde hat folgendes als Bestätigungsadresse angegeben:\r\n"
+ui->leCc->text()+
"\r\n\r\n");
QString text = standart.append(ui->tEditMail->toPlainText());
emit setText(text);
emit setFoe(createFoeString());
emit setInfo(createInfoString());
}
void SmtpEmail::resetClass()
{
if(init)
settingsWrite();
else
init = true;
ui->cBoxFoe_1->setCheckState(Qt::PartiallyChecked); ui->cBoxFoe_8->setCheckState(Qt::PartiallyChecked);
ui->cBoxFoe_2->setCheckState(Qt::PartiallyChecked); ui->cBoxFoe_9->setCheckState(Qt::PartiallyChecked);
ui->cBoxFoe_3->setCheckState(Qt::PartiallyChecked); ui->cBoxFoe_10->setCheckState(Qt::PartiallyChecked);
ui->cBoxFoe_4->setCheckState(Qt::PartiallyChecked); ui->cBoxFoe_11->setCheckState(Qt::PartiallyChecked);
ui->cBoxFoe_5->setCheckState(Qt::PartiallyChecked); ui->cBoxFoe_12->setCheckState(Qt::PartiallyChecked);
ui->cBoxFoe_6->setCheckState(Qt::PartiallyChecked); ui->cBoxFoe_13->setCheckState(Qt::PartiallyChecked);
ui->cBoxFoe_7->setCheckState(Qt::PartiallyChecked); ui->cBoxFoe_14->setCheckState(Qt::PartiallyChecked);
ui->cBoxInfo_1->setCheckState(Qt::PartiallyChecked); ui->cBoxInfo_8->setCheckState(Qt::PartiallyChecked);
ui->cBoxInfo_2->setCheckState(Qt::PartiallyChecked); ui->cBoxInfo_9->setCheckState(Qt::PartiallyChecked);
ui->cBoxInfo_3->setCheckState(Qt::PartiallyChecked); ui->cBoxInfo_10->setCheckState(Qt::PartiallyChecked);
ui->cBoxInfo_4->setCheckState(Qt::PartiallyChecked); ui->cBoxInfo_11->setCheckState(Qt::PartiallyChecked);
ui->cBoxInfo_5->setCheckState(Qt::PartiallyChecked); ui->cBoxInfo_12->setCheckState(Qt::PartiallyChecked);
ui->cBoxInfo_6->setCheckState(Qt::PartiallyChecked); ui->cBoxInfo_7->setCheckState(Qt::PartiallyChecked);
//Reset
ui->lblError->clear();
ui->btnBack->hide();
ui->stackedWidgetSupport->setCurrentIndex(0);
}
void SmtpEmail::errorManagement(QString s)
{
qDebug() << "ErrorManagement smtpMail";
ui->lblError->show();
ui->lblError->setText(s);
QTimer::singleShot(5000, ui->lblError, &QLabel::hide);
emit stopAnimation();
}
//TestSlots
void SmtpEmail::msg1()
{
qDebug() << "msg1"<< QTime::currentTime().toString("mm.zzz");
}
void SmtpEmail::msg2(QString s)
{
qDebug() << "Slot Direct"<< s;
}
void SmtpEmail::updateTexts()
{
ui->retranslateUi(this);
resetClass();
}
void SmtpEmail::onStackChange(int current)
{
switch (current) {
case 0://StartSeite
ui->btnNext->changeState(false);
ui->btnBack->hide();
break;
case 1://FoeSeite
ui->btnNext->changeState(false);
ui->btnBack->show();
checkFoe();
break;
case 2://InfoSeite
ui->btnNext->changeState(false);
ui->btnBack->show();
checkInfo();
break;
case 3://Email
ui->btnNext->changeState(true);
ui->leBetreff->setText(getDefaultSubject());
ui->btnBack->show();
break;
}
ui->btnNext->update();
}
void SmtpEmail::nextSide()
{
switch(ui->stackedWidgetSupport->currentIndex()){
case 0://Startseite
ui->stackedWidgetSupport->slideInNext();break;
case 1://SupportFOE
if(checkFoe()) {ui->stackedWidgetSupport->slideInNext();
}else emit newMessage(tr("Bitte füllen Sie alle Punkte aus."));
break;
case 2://SupportInfo
if(checkInfo()) ui->stackedWidgetSupport->slideInNext();
else emit newMessage(tr("Bitte füllen Sie alle Punkte aus."));
break;
case 3://SupportEmail
sendEmail();
break;
}
}
QString SmtpEmail::getDefaultSubject()
{
return "Support request - uConfig - "+QDate::currentDate().toString("yyyy.MM.dd");
}
const QString SmtpEmail::createFoeString()
{
QString s;
QList<QString> list{
"Keine Bus-Adresse wurde doppelt vergeben.",
"Die Baudrate sowie die Parität sind bei allen Geräten, pro Linie, identisch.",
"Die Dipschalter wurde alle mindestens 1-mal umgeschaltet.",
"A/B-Anschlussdrähte sind nicht vertauscht worden.",
"Die Drähte an den Anschlussstellen sind nicht gebrochen oder haben einen Wackelkontakt.",
"Die Error-LED zeigt eine Busstörung an.",
"HInweis:Gilt nur für einen Betrieb unter AC!",
"Versorgung und GND sind an den richtigen Pins angeschlossen."
};
list = normLength(list);
s.append(list[0] + csv);
s.append(checkStateToString(ui->cBoxFoe_1->checkState())+nl);
s.append(list[1] + csv);
s.append(checkStateToString(ui->cBoxFoe_3->checkState())+nl);
s.append(list[2] + csv);
s.append(checkStateToString(ui->cBoxFoe_5->checkState())+nl);
s.append(list[3] + csv);
s.append(checkStateToString(ui->cBoxFoe_7->checkState())+nl);
s.append(list[4] + csv);
s.append(checkStateToString(ui->cBoxFoe_9->checkState())+nl);
s.append(list[5] + csv);
s.append(checkStateToString(ui->cBoxFoe_11->checkState())+nl);
s.append(list[6] + nl);
s.append(list[7] + csv);
s.append(checkStateToString(ui->cBoxFoe_13->checkState())+nl);
return s;
}
const QString SmtpEmail::createInfoString()
{
QString s;
QList<QString> list{
"Handelt es sich um eine AC oder DC-Betriebsspannung.",
"Wie hoch ist die Betriebsspannung?",
"Wieviele Teilnehmer hängen an dem RS485-Bus?",
"Welche Länge hat der Bus?",
"Welche Art Kabel wurde verwendet?",
"Handelt es sich um eine Erstinbetriebnahme des Gerätes?",
"Sind alle Geräte die an dem Bus hängen betroffen?",
"Wenn Geräte getauscht werden, wandert die Störung mit?",
"Ist die Störung permanent vorhanden?",
"Sind die Hinweise in der FAQ Busstörung / Betriebsspannung beachten worden?"
};
list = normLength(list);
s.append(list[0] + csv);
s.append(checkStateToString(ui->cBoxInfo_1->checkState())+nl);
s.append(list[1] + csv);
s.append("Busanfang: "+ui->leInfo_1->text() + " || Busende: "+ui->leInfo_2->text()+nl);
s.append(list[2] + csv);
s.append(ui->leInfo_3->text()+nl);
s.append(list[3] + csv);
s.append(ui->leInfo_4->text()+nl);
s.append(list[4] + csv);
s.append(ui->leInfo_5->text()+nl);
s.append(list[5] + csv);
s.append(checkStateToString(ui->cBoxInfo_4->checkState())+nl);
s.append(list[6] + csv);
s.append(checkStateToString(ui->cBoxInfo_5->checkState())+nl);
s.append(list[7] + csv);
s.append(checkStateToString(ui->cBoxInfo_8->checkState())+nl);
s.append(list[8] + csv);
s.append(checkStateToString(ui->cBoxInfo_10->checkState())+nl);
s.append(list[9] + csv);
s.append(checkStateToString(ui->cBoxInfo_12->checkState())+nl);
return s;
}
const QString SmtpEmail::checkStateToString(Qt::CheckState state)
{
switch (state) {
case Qt::Checked: return "Ja";
case Qt::Unchecked: return "Nein";
default:return "Nicht beantwortet!";
}
}
const QList<QString> &SmtpEmail::normLength(QList<QString> &list)
{
int length = 0;
for(QString &s : list){
if(s.length() > length)
length = s.length();
}
for(QString &s : list){
while(s.length() < length)
s.append(" ");
}
return list;
}
void SmtpEmail::settingsWrite()
{
QSettings settings("abc","abc");
settings.beginGroup("Support");
settings.setValue("LastUser",ui->leVon->text());
settings.setValue("UserAdress",ui->leCc->text());
if(!lastContact.isNull()){
settings.setValue("LastContact",lastContact);
lastContact = QDateTime();
}
settings.endGroup();
}
void SmtpEmail::settingsRead()
{
QSettings settings("abc","abc");
settings.beginGroup("Support");
ui->leVon->setText(settings.value("LastUser").toString());
ui->leCc->setText( settings.value("UserAdress").toString());
lastContact = settings.value("LastContact").toDateTime();
settings.endGroup();
}
bool SmtpEmail::checkFoe()
{
qDebug() << "Check Foe";
if(ui->cBoxFoe_1->checkState() != Qt::PartiallyChecked &&
ui->cBoxFoe_3->checkState() != Qt::PartiallyChecked &&
ui->cBoxFoe_5->checkState() != Qt::PartiallyChecked &&
ui->cBoxFoe_7->checkState() != Qt::PartiallyChecked &&
ui->cBoxFoe_9->checkState() != Qt::PartiallyChecked &&
ui->cBoxFoe_11->checkState() != Qt::PartiallyChecked &&
ui->cBoxFoe_13->checkState() != Qt::PartiallyChecked)
{
emit doGesture(false);
return true;
}else{
emit doGesture(true);
return false;
}
}
bool SmtpEmail::checkInfo()
{
qDebug() << "Check Info";
if(ui->cBoxInfo_1->checkState() != Qt::PartiallyChecked &&
ui->cBoxInfo_3->checkState() != Qt::PartiallyChecked &&
ui->cBoxInfo_5->checkState() != Qt::PartiallyChecked &&
ui->cBoxInfo_7->checkState() != Qt::PartiallyChecked &&
ui->cBoxInfo_9->checkState() != Qt::PartiallyChecked &&
ui->cBoxInfo_11->checkState() != Qt::PartiallyChecked &&
!ui->leInfo_1->text().isEmpty() && !ui->leInfo_2->text().isEmpty() &&
!ui->leInfo_3->text().isEmpty() && !ui->leInfo_4->text().isEmpty() &&
!ui->leInfo_5->text().isEmpty())
{
emit doGesture(false);
return true;
}else{
emit doGesture(true);
return false;
}
}
bool SmtpEmail::event(QEvent *e)
{
if(e->type() == QEvent::MouseButtonPress ||
e->type() == QEvent::MouseButtonRelease ||
e->type() == QEvent::MouseMove ){
QMouseEvent *ev =(QMouseEvent *)e;
emit mouseEvent(ev->type(),mapToGlobal(ev->pos()));
}
return false;
}
#ifndef SMTPEMAIL_H
#define SMTPEMAIL_H
#include "smtpemail.h"
#include <QFrame>
#include <QCheckBox>
#include <QDateTime>
#include <QMouseEvent>
#include <QObject>
namespace Ui {
class SmtpEmail;
}
class SmtpEmail : public QFrame
{
Q_OBJECT
public:
explicit SmtpEmail(QWidget *parent = 0);
~SmtpEmail();
inline void setVersion(const QString &s){version = s;}
public slots:
void setModbusAttachment(const QList<QString> &lModbusRegCoils);
void updateTexts();
void errorManagement(QString s);
//Tests
void msg1();
void msg2(QString s);
signals:
void close();
void setPw(QString s);
void setSubject(QString s);
void setSender(QString s);
void setCustMail(QString mail, QString sender);
void setText(QString s);
void setFoe(QString s);
void setInfo(QString s);
void setModbus(QString s);
void startAnimation();
void stopAnimation();
void hideProgress(bool b = false);
void setAnimationText(QString s);
void newMessage(QString s);
void doGesture(bool doIt);
void mouseEvent(QMouseEvent::Type t, QPoint p);
private slots:
void sendEmail();
void resetClass();
void onStackChange(int current);
void nextSide();
private:
QString getDefaultSubject();
const QString createFoeString();
const QString createInfoString();
const QString checkStateToString(Qt::CheckState state);
const QList<QString> & normLength(QList<QString> &list);
void settingsWrite();
void settingsRead();
bool checkFoe();
bool checkInfo();
protected:
virtual bool event(QEvent *e)Q_DECL_OVERRIDE;
private:
Ui::SmtpEmail *ui;
QString trenner= "-";
QString version;
QList<QString> l{"1","2","3",4};
bool init = false;
QDateTime lastContact;
};
#endif // SMTPEMAIL_H
#include "sslsocket.h"
#include <QMetaEnum>
SslSocket::SslSocket(QString host, int port, SmtpClient::ConnectionType connection) : m_host(host), m_port(port), m_type(connection)
{
}
SslSocket::~SslSocket()
{
if(smtp)
smtp->deleteLater();
}
void SslSocket::initSslSocket()
{
smtp = new SmtpClient(m_host,m_port,m_type);
smtp->setUser("Kunde.Thermokon@gmail.com");
connect(smtp, &SmtpClient::smtpError, this, [=](SmtpClient::SmtpError e) {
emit errorMsg(tr("App- oder Serverfehler: ")+ QString::number(e));
});
connect(this, &SslSocket::error, this, &SslSocket::errorManagement);
connect(smtp, &SmtpClient::connected, this, &SslSocket::connected,Qt::QueuedConnection);
connect(smtp, &SmtpClient::loggedIn, this, &SslSocket::loggedIn, Qt::QueuedConnection);
connect(smtp, &SmtpClient::emailSend, this, &SslSocket::emailSend, Qt::QueuedConnection);
connect(this, &SslSocket::allDone, smtp, &SmtpClient::quit, Qt::QueuedConnection);
//Test
Timer *timer = new QTimer();
timer->setInterval(500);
connect(timer, &QTimer::timeout, timer, [=]{qDebug() <<"Thread"<< QTime::currentTime().toString("ss.zzz"); emit errorMsg(QTime::currentTime().toString("ss.zzz"));});
timer->start();
}
void SslSocket::setCustMail(QString email, QString sender){
if(!email.isEmpty() && !email.isEmpty())
comfirm = new EmailAddress(email,sender);
else
comfirm = Q_NULLPTR;
checkData();
}
void SslSocket::sendMail()
{
qDebug() << "SendMail";
smtp->connectToHost();
}
void SslSocket::errorManagement(SslSocket::errorType e)
{
qDebug() << "errorManagement";
switch (e) {
case connectError:
emit errorMsg(tr("Verbindungsfehler! Es konnte keine Verbindung zum E-Mailserver hergestellt werden."));
break;
case loginError:
emit errorMsg(tr("Loginfehler! Auf den E-Mailserver konnte nocht eingelogt werden"));
break;
case sendError:
if(bBest)
emit errorMsg(tr("Übertragungsfehler! Die Bestätigungbenachrichtigung konnte nicht versendet werden."));
else
emit errorMsg(tr("Übertragungsfehler! Die E-Mail konnte nicht versendet werden."));
break;
}
qDebug() << "Signals send";
}
void SslSocket::connected(bool state)
{
if(state){
qDebug() << "Connect war erfolgreich";
smtp->login();
}else{
qDebug() << "Verbindung zum Server beendet";
}
}
void SslSocket::loggedIn(bool state)
{
if(state){
qDebug() << "Login war erfolgreich" <<QTime::currentTime();
createAndSend();
}else{
qDebug() << "Login fehlgeschlagen";
emit error(loginError);
}
}
void SslSocket::emailSend(bool state)
{
if(state){
if(comfirm && !bBest){
//Bestätigung wurde noch nicht gesendet
bBest = true;
//Send Bestätigungs E-Mail
MimeMessage best;
//Absender
best.setSender(new EmailAddress("E-Mail3",m_sender));
//Empfänger
best.addRecipient(comfirm);
best.setSubject(tr("abc"));
MimeText bestText;
bestText.setText(tr("abc"));
best.addPart(&bestText);
smtp->sendMail(best);
}else{
bBest = false;
emit allDone();
}
}else{
emit error(sendError);
bBest = false;
qDebug() << "Email could not be send";
}
}
void SslSocket::createAndSend()
{
//Stelle Nachricht zusammen
MimeMessage message;
//Absender
message.setSender(new EmailAddress("E-Mail1",m_sender));
//Empfänger
message.addRecipient(new EmailAddress("E-Mail2", "Support"));
//Betreff
message.setSubject(m_subject);
//Haupt E-Mail Text
MimeText mime;
mime.setText(m_text);
message.addPart(&mime);
//Dateianhänge
MimeText foe;
foe.setContentName("FOE.txt");
foe.setText(m_foe);
message.addPart(&foe);
MimeText info;
info.setContentName("Info.txt");
info.setText(m_info);
message.addPart(&info);
MimeText modbus;
modbus.setContentName("Mod.txt");
modbus.setText(m_modbus);
message.addPart(&modbus);
m_modbus.clear();
smtp->sendMail(message);
}
#ifndef SSLSOCKET_H
#define SSLSOCKET_H
#include <QObject>
#include <QDebug>
#include "smtpclient.h"
#include "mimetext.h"
class SslSocket : public QObject
{
Q_OBJECT
public:
SslSocket(QString host, int port, SmtpClient::ConnectionType connection);
~SslSocket();
enum errorType{
connectError,
loginError,
sendError
};
Q_ENUM(errorType)
public slots:
void initSslSocket();
void setSubject(QString s) {m_subject = s; checkData();}
void setSender(QString s) {m_sender = s; checkData();}
void setCustMail(QString email, QString sender);
void setText(QString s) {m_text = s; checkData();}
void setFoe(QString s) {m_foe = s; checkData();}
void setInfo(QString s) {m_info = s;checkData();}
void setModbus(QString s) {m_modbus = s;}
void setpw(QString s) {smtp->setPassword(s);}
void sendMail();
signals:
void errorMsg(QString msg);
void error(errorType error);
void allDataAvailable();
void allDone();
private slots:
void errorManagement(errorType e);
void connected(bool state);
void loggedIn(bool state);
void emailSend(bool state);
private:
void checkData(){cntData++; if(cntData >= 6){emit allDataAvailable();cntData = 0;}}
void createAndSend();
private:
SmtpClient *smtp;
MimeMessage *msg;
bool bBest = false;
int cntData = 0;
QString m_host,m_subject,m_sender;
QString m_text, m_foe, m_info, m_modbus;
int m_port;
SmtpClient::ConnectionType m_type;
EmailAddress *comfirm = Q_NULLPTR;
};
#endif // SSLSOCKET_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment