Code and API cleanup.

This commit is contained in:
Attila Tőkés 2014-11-05 22:00:13 +02:00
parent e049dfd51b
commit 9897598885
13 changed files with 72 additions and 156 deletions

View File

@ -45,14 +45,12 @@ MimeFile::~MimeFile()
/* [3] Protected methods */ /* [3] Protected methods */
void MimeFile::writeContent(QIODevice &device) { void MimeFile::writeContent(QIODevice &device) const {
file->open(QIODevice::ReadOnly); file->open(QIODevice::ReadOnly);
this->content = file->readAll(); const QByteArray &fileContent = file->readAll();
file->close(); file->close();
MimePart::writeContent(device); MimePart::writeContent(device, fileContent);
this->content.clear();
} }
/* [3] --- */ /* [3] --- */

View File

@ -51,7 +51,7 @@ protected:
/* [4] Protected methods */ /* [4] Protected methods */
void writeContent(QIODevice &device); void writeContent(QIODevice &device) const;
/* [4] --- */ /* [4] --- */

View File

@ -140,7 +140,7 @@ const QList<MimePart*> & MimeMessage::getParts() const
/* [3] Public Methods */ /* [3] Public Methods */
QString MimeMessage::toString() QString MimeMessage::toString() const
{ {
QBuffer out; QBuffer out;
out.open(QIODevice::WriteOnly); out.open(QIODevice::WriteOnly);
@ -168,7 +168,7 @@ QByteArray MimeMessage::formatAddress(EmailAddress *address, MimePart::Encoding
return result; return result;
} }
void MimeMessage::writeToDevice(QIODevice &out) { void MimeMessage::writeToDevice(QIODevice &out) const {
/* =========== MIME HEADER ============ */ /* =========== MIME HEADER ============ */
/* ---------- Sender / From ----------- */ /* ---------- Sender / From ----------- */

View File

@ -69,8 +69,8 @@ public:
/* [3] Public methods */ /* [3] Public methods */
virtual QString toString(); virtual QString toString() const;
void writeToDevice(QIODevice &device); void writeToDevice(QIODevice &device) const;
/* [3] --- */ /* [3] --- */

View File

@ -54,10 +54,10 @@ const QList<MimePart*> & MimeMultiPart::getParts() const {
return parts; return parts;
} }
void MimeMultiPart::writeContent(QIODevice &device) { void MimeMultiPart::writeContent(QIODevice &device) const {
QList<MimePart*>::iterator it; QList<MimePart*>::const_iterator it;
for (it = parts.begin(); it != parts.end(); it++) { for (it = parts.constBegin(); it != parts.constEnd(); it++) {
device.write("--" ); device.write("--" );
device.write(cBoundary.toLatin1()); device.write(cBoundary.toLatin1());
device.write("\r\n"); device.write("\r\n");

View File

@ -60,7 +60,7 @@ public:
void addPart(MimePart *part); void addPart(MimePart *part);
void writeContent(QIODevice &device); void writeContent(QIODevice &device) const;
/* [3] --- */ /* [3] --- */

View File

@ -131,7 +131,7 @@ int MimePart::getMaxLineLength() const {
/* [3] Public methods */ /* [3] Public methods */
QString MimePart::toString() QString MimePart::toString() const
{ {
QBuffer out; QBuffer out;
out.open(QIODevice::WriteOnly); out.open(QIODevice::WriteOnly);
@ -139,7 +139,7 @@ QString MimePart::toString()
return QString(out.buffer()); return QString(out.buffer());
} }
void MimePart::writeToDevice(QIODevice &device) { void MimePart::writeToDevice(QIODevice &device) const {
QString header; QString header;
/* === Header Prepare === */ /* === Header Prepare === */
@ -203,7 +203,11 @@ void MimePart::writeToDevice(QIODevice &device) {
/* [4] Protected methods */ /* [4] Protected methods */
void MimePart::writeContent(QIODevice &device) { void MimePart::writeContent(QIODevice &device) const {
this->writeContent(device, content);
}
void MimePart::writeContent(QIODevice &device, const QByteArray &content) const {
switch (cEncoding) switch (cEncoding)
{ {
case _7Bit: case _7Bit:

View File

@ -82,8 +82,8 @@ public:
/* [3] Public methods */ /* [3] Public methods */
virtual QString toString(); virtual QString toString() const;
void writeToDevice(QIODevice &device); void writeToDevice(QIODevice &device) const;
/* [3] --- */ /* [3] --- */
@ -108,7 +108,8 @@ protected:
/* [4] --- */ /* [4] --- */
virtual void writeContent(QIODevice &device); virtual void writeContent(QIODevice &device) const;
void writeContent(QIODevice &device, const QByteArray &content) const;
}; };
#endif // MIMEPART_H #endif // MIMEPART_H

View File

@ -50,9 +50,8 @@ const QString & MimeText::getText() const
/* [3] Protected Methods */ /* [3] Protected Methods */
void MimeText::writeContent(QIODevice &device) { void MimeText::writeContent(QIODevice &device) const {
this->content = text.toLocal8Bit(); MimePart::writeContent(device, text.toLocal8Bit());
MimePart::writeContent(device);
} }
/* [3] --- */ /* [3] --- */

View File

@ -52,7 +52,7 @@ protected:
/* [4] Protected methods */ /* [4] Protected methods */
void writeContent(QIODevice &device); void writeContent(QIODevice &device) const;
/* [4] --- */ /* [4] --- */

View File

@ -20,14 +20,13 @@
QString QuotedPrintable::encode(const QByteArray &input) QString QuotedPrintable::encode(const QByteArray &input)
{ {
QString output;
char byte;
static const char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; static const char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
QString output;
for (int i = 0; i < input.length() ; ++i) for (int i = 0; i < input.length() ; ++i)
{ {
byte = input[i]; const char byte = input[i];
if ((byte == 0x20) || ((byte >= 33) && (byte <= 126) && (byte != 61))) { if ((byte == 0x20) || ((byte >= 33) && (byte <= 126) && (byte != 61))) {
output.append(byte); output.append(byte);

View File

@ -28,17 +28,15 @@
SmtpClient::SmtpClient(const QString & host, int port, ConnectionType connectionType) : SmtpClient::SmtpClient(const QString & host, int port, ConnectionType connectionType) :
state(UnconnectedState), state(UnconnectedState),
host(host),
port(port),
name("localhost"), name("localhost"),
authMethod(AuthPlain),
isReadyConnected(false), isReadyConnected(false),
isAuthenticated(false), isAuthenticated(false),
isMailSent(false) isMailSent(false)
{ {
setConnectionType(connectionType); setConnectionType(connectionType);
this->host = host;
this->port = port;
connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)),
this, SLOT(socketStateChanged(QAbstractSocket::SocketState))); this, SLOT(socketStateChanged(QAbstractSocket::SocketState)));
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), connect(socket, SIGNAL(error(QAbstractSocket::SocketError)),
@ -53,50 +51,6 @@ SmtpClient::~SmtpClient() {}
/* [2] Getters and Setters */ /* [2] Getters and Setters */
/**
* @brief Sets the username to the specified value.
*/
void SmtpClient::setUser(const QString &user)
{
this->user = user;
}
/**
* @brief Sets the password to the specified value
*/
void SmtpClient::setPassword(const QString &password)
{
this->password = password;
}
/**
* @brief Changes the authentication method to the specified.
*/
void SmtpClient::setAuthMethod(AuthMethod method)
{
this->authMethod = method;
}
/**
* @brief Sets the host of connection.
* @deprecated Use the constructor.
*/
void SmtpClient::setHost(QString &host)
{
this->host = host;
}
/**
* @brief Sets the connection port to the specified value.
* @param port
* @deprecated Use the constructor.
*/
void SmtpClient::setPort(int port)
{
this->port = port;
}
/** /**
* @brief Returns the host name of the server. * @brief Returns the host name of the server.
*/ */
@ -105,30 +59,6 @@ const QString& SmtpClient::getHost() const
return this->host; return this->host;
} }
/**
* @brief Returns the username used for authenticating.
*/
const QString& SmtpClient::getUser() const
{
return this->user;
}
/**
* @brief Returns the password used for authenticating.
*/
const QString& SmtpClient::getPassword() const
{
return this->password;
}
/**
* @brief Returns the authentication method used.
*/
SmtpClient::AuthMethod SmtpClient::getAuthMethod() const
{
return this->authMethod;
}
/** /**
* @brief Return the port. * @brief Return the port.
*/ */
@ -191,45 +121,38 @@ QTcpSocket* SmtpClient::getSocket() {
/* [3] Public methods */ /* [3] Public methods */
bool SmtpClient::connectToHost() void SmtpClient::connectToHost()
{ {
if (state != UnconnectedState) if (state != UnconnectedState)
return false; return;
changeState(ConnectingState); changeState(ConnectingState);
return true;
} }
bool SmtpClient::login() void SmtpClient::login()
{ {
if (!isReadyConnected || isAuthenticated) if (!isReadyConnected || isAuthenticated)
return false; return;
changeState(AuthenticatingState); changeState(AuthenticatingState);
return true;
} }
bool SmtpClient::login(const QString &user, const QString &password, AuthMethod method) void SmtpClient::login(const QString &user, const QString &password, AuthMethod method)
{ {
this->user = user; this->authInfo = AuthInfo(user, password, method);
this->password = password; login();
this->authMethod = method;
clearUserDataAfterLogin = true;
return login();
} }
bool SmtpClient::sendMail(MimeMessage& email) void SmtpClient::sendMail(const MimeMessage & email)
{ {
if (!isReadyConnected) if (!isReadyConnected)
return false; return;
isMailSent = false; isMailSent = false;
this->email = &email; this->email = &email;
this->rcptType = 0; this->rcptType = 0;
changeState(MailSendingState); changeState(MailSendingState);
return true;
} }
void SmtpClient::quit() void SmtpClient::quit()
@ -340,7 +263,7 @@ void SmtpClient::changeState(SmtpClient::ClientState state) {
case AuthenticatingState: case AuthenticatingState:
isAuthenticated = false; isAuthenticated = false;
changeState(authMethod == AuthPlain ? _AUTH_PLAIN_0 : _AUTH_LOGIN_0); changeState(authInfo.authMethod == AuthPlain ? _AUTH_PLAIN_0 : _AUTH_LOGIN_0);
break; break;
case MailSendingState: case MailSendingState:
@ -390,8 +313,8 @@ void SmtpClient::changeState(SmtpClient::ClientState state) {
/* --- AUTH --- */ /* --- AUTH --- */
case _AUTH_PLAIN_0: case _AUTH_PLAIN_0:
// Sending command: AUTH PLAIN base64('\0' + username + '\0' + password) // Sending command: AUTH PLAIN base64('\0' + username + '\0' + password)
sendMessage("AUTH PLAIN " + QByteArray().append((char) 0).append(user) sendMessage("AUTH PLAIN " + QByteArray().append((char) 0).append(authInfo.username)
.append((char) 0).append(password).toBase64()); .append((char) 0).append(authInfo.password).toBase64());
break; break;
case _AUTH_LOGIN_0: case _AUTH_LOGIN_0:
@ -400,20 +323,17 @@ void SmtpClient::changeState(SmtpClient::ClientState state) {
case _AUTH_LOGIN_1_USER: case _AUTH_LOGIN_1_USER:
// Send the username in base64 // Send the username in base64
sendMessage(QByteArray().append(user).toBase64()); sendMessage(QByteArray().append(authInfo.username).toBase64());
break; break;
case _AUTH_LOGIN_2_PASS: case _AUTH_LOGIN_2_PASS:
// Send the password in base64 // Send the password in base64
sendMessage(QByteArray().append(password).toBase64()); sendMessage(QByteArray().append(authInfo.password).toBase64());
break; break;
case _READY_Authenticated: case _READY_Authenticated:
isAuthenticated = true; isAuthenticated = true;
if (clearUserDataAfterLogin) { authInfo = AuthInfo();
password = ""; user = "";
clearUserDataAfterLogin = false;
}
changeState(ReadyState); changeState(ReadyState);
emit authenticated(); emit authenticated();
break; break;
@ -425,6 +345,7 @@ void SmtpClient::changeState(SmtpClient::ClientState state) {
case _MAIL_1_RCPT_INIT: case _MAIL_1_RCPT_INIT:
rcptType++; rcptType++;
const QList<EmailAddress*> *addressList;
switch (rcptType) switch (rcptType)
{ {
case _TO: case _TO:
@ -441,11 +362,12 @@ void SmtpClient::changeState(SmtpClient::ClientState state) {
return; return;
} }
addressIt = addressList->constBegin(); addressIt = addressList->constBegin();
addressItEnd = addressList->constEnd();
changeState(_MAIL_2_RCPT); changeState(_MAIL_2_RCPT);
break; break;
case _MAIL_2_RCPT: case _MAIL_2_RCPT:
if (addressIt != addressList->end()) { if (addressIt != addressItEnd) {
sendMessage("RCPT TO: <" + (*addressIt)->getAddress() + ">"); sendMessage("RCPT TO: <" + (*addressIt)->getAddress() + ">");
addressIt++; addressIt++;
} else { } else {

View File

@ -112,25 +112,12 @@ public:
/* [2] Getters and Setters */ /* [2] Getters and Setters */
const QString& getHost() const; const QString& getHost() const;
void setHost(QString &host);
int getPort() const; int getPort() const;
void setPort(int port); ConnectionType getConnectionType() const;
const QString& getName() const; const QString& getName() const;
void setName(const QString &name); void setName(const QString &name);
ConnectionType getConnectionType() const;
const QString & getUser() const;
void setUser(const QString &host);
const QString & getPassword() const;
void setPassword(const QString &password);
SmtpClient::AuthMethod getAuthMethod() const;
void setAuthMethod(AuthMethod method);
const QString & getResponseText() const; const QString & getResponseText() const;
int getResponseCode() const; int getResponseCode() const;
@ -141,11 +128,9 @@ public:
/* [3] Public methods */ /* [3] Public methods */
bool connectToHost(); void connectToHost();
bool login(); void login(const QString &user, const QString &password, AuthMethod method = AuthLogin);
bool login(const QString &user, const QString &password, void sendMail(const MimeMessage & email);
AuthMethod method = AuthLogin);
bool sendMail(MimeMessage& email);
void quit(); void quit();
bool waitForReadyConnected(int msec = 30000); bool waitForReadyConnected(int msec = 30000);
@ -158,19 +143,25 @@ protected:
/* [4] Protected members */ /* [4] Protected members */
QTcpSocket *socket; struct AuthInfo {
ClientState state; QString username;
bool syncMode;
QString host;
int port;
ConnectionType connectionType;
QString name;
QString user;
QString password; QString password;
AuthMethod authMethod; AuthMethod authMethod;
bool clearUserDataAfterLogin;
AuthInfo(const QString & username = "", const QString &password = "", AuthMethod authMethod = AuthPlain) :
username(username), password(password), authMethod(authMethod) {}
};
QTcpSocket *socket;
ClientState state;
const QString host;
const int port;
ConnectionType connectionType;
QString name;
AuthInfo authInfo;
QString responseText; QString responseText;
QString tempResponse; QString tempResponse;
@ -180,17 +171,19 @@ protected:
bool isAuthenticated; bool isAuthenticated;
bool isMailSent; bool isMailSent;
MimeMessage *email; const MimeMessage *email;
QList<EmailAddress*>::const_iterator addressIt;
const QList<EmailAddress*> *addressList;
int rcptType; int rcptType;
enum _RcptType { _TO = 1, _CC = 2, _BCC = 3}; enum _RcptType { _TO = 1, _CC = 2, _BCC = 3};
QList<EmailAddress*>::const_iterator addressIt;
QList<EmailAddress*>::const_iterator addressItEnd;
/* [4] --- */ /* [4] --- */
/* [5] Protected methods */ /* [5] Protected methods */
void login();
void setConnectionType(ConnectionType ct); void setConnectionType(ConnectionType ct);
void changeState(ClientState state); void changeState(ClientState state);
void processResponse(); void processResponse();