Added some state check to the connectToHost, login, sendMail,

waitForReadyConnected, waitForAuthenticated, waitForMailSent functions.
This commit is contained in:
Tőkés Attila 2012-10-28 21:31:34 +02:00
parent 49080a5a0c
commit 34883564f5

View File

@ -191,12 +191,18 @@ QTcpSocket* SmtpClient::getSocket() {
bool SmtpClient::connectToHost() bool SmtpClient::connectToHost()
{ {
if (state != UnconnectedState)
return false;
changeState(ConnectingState); changeState(ConnectingState);
return true; return true;
} }
bool SmtpClient::login() bool SmtpClient::login()
{ {
if (!isReadyConnected || isAuthenticated)
return false;
changeState(AuthenticatingState); changeState(AuthenticatingState);
return true; return true;
} }
@ -211,6 +217,11 @@ bool SmtpClient::login(const QString &user, const QString &password, AuthMethod
bool SmtpClient::sendMail(MimeMessage& email) bool SmtpClient::sendMail(MimeMessage& email)
{ {
if (!isReadyConnected)
return false;
isMailSent = false;
this->email = &email; this->email = &email;
this->rcptType = 0; this->rcptType = 0;
changeState(MailSendingState); changeState(MailSendingState);
@ -225,32 +236,50 @@ void SmtpClient::quit()
bool SmtpClient::waitForReadyConnected(int msec) { bool SmtpClient::waitForReadyConnected(int msec) {
QEventLoop loop; if (state == UnconnectedState)
return false;
QTimer::singleShot(msec, &loop, SLOT(quit())); QEventLoop loop;
QObject::connect(this, SIGNAL(readyConnected()), &loop, SLOT(quit())); QObject::connect(this, SIGNAL(readyConnected()), &loop, SLOT(quit()));
if (isReadyConnected)
return true;
loop.exec(); loop.exec();
QTimer::singleShot(msec, &loop, SLOT(quit()));
return isReadyConnected; return isReadyConnected;
} }
bool SmtpClient::waitForAuthenticated(int msec) { bool SmtpClient::waitForAuthenticated(int msec) {
QEventLoop loop; if (!isReadyConnected)
return false;
QTimer::singleShot(msec, &loop, SLOT(quit())); QEventLoop loop;
QObject::connect(this, SIGNAL(authenticated()), &loop, SLOT(quit())); QObject::connect(this, SIGNAL(authenticated()), &loop, SLOT(quit()));
if (isAuthenticated)
return true;
loop.exec(); loop.exec();
QTimer::singleShot(msec, &loop, SLOT(quit()));
return isAuthenticated; return isAuthenticated;
} }
bool SmtpClient::waitForMailSent(int msec) { bool SmtpClient::waitForMailSent(int msec) {
QEventLoop loop; if (!isReadyConnected)
return false;
QTimer::singleShot(msec, &loop, SLOT(quit())); QEventLoop loop;
QObject::connect(this, SIGNAL(mailSent()), &loop, SLOT(quit())); QObject::connect(this, SIGNAL(mailSent()), &loop, SLOT(quit()));
if (isMailSent)
return true;
loop.exec(); loop.exec();
QTimer::singleShot(msec, &loop, SLOT(quit()));
return isMailSent; return isMailSent;
} }