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 */
void MimeFile::writeContent(QIODevice &device) {
file->open(QIODevice::ReadOnly);
this->content = file->readAll();
void MimeFile::writeContent(QIODevice &device) const {
file->open(QIODevice::ReadOnly);
const QByteArray &fileContent = file->readAll();
file->close();
MimePart::writeContent(device);
this->content.clear();
MimePart::writeContent(device, fileContent);
}
/* [3] --- */

View File

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

View File

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

View File

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

View File

@ -54,10 +54,10 @@ const QList<MimePart*> & MimeMultiPart::getParts() const {
return parts;
}
void MimeMultiPart::writeContent(QIODevice &device) {
QList<MimePart*>::iterator it;
void MimeMultiPart::writeContent(QIODevice &device) const {
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(cBoundary.toLatin1());
device.write("\r\n");

View File

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

View File

@ -131,7 +131,7 @@ int MimePart::getMaxLineLength() const {
/* [3] Public methods */
QString MimePart::toString()
QString MimePart::toString() const
{
QBuffer out;
out.open(QIODevice::WriteOnly);
@ -139,7 +139,7 @@ QString MimePart::toString()
return QString(out.buffer());
}
void MimePart::writeToDevice(QIODevice &device) {
void MimePart::writeToDevice(QIODevice &device) const {
QString header;
/* === Header Prepare === */
@ -203,7 +203,11 @@ void MimePart::writeToDevice(QIODevice &device) {
/* [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)
{
case _7Bit:

View File

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

View File

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

View File

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

View File

@ -20,14 +20,13 @@
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'};
QString output;
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))) {
output.append(byte);

View File

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

View File

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