diff --git a/src/mimefile.cpp b/src/mimefile.cpp index d981a4b..3589146 100644 --- a/src/mimefile.cpp +++ b/src/mimefile.cpp @@ -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] --- */ diff --git a/src/mimefile.h b/src/mimefile.h index 0ec74f2..ac896d9 100644 --- a/src/mimefile.h +++ b/src/mimefile.h @@ -51,7 +51,7 @@ protected: /* [4] Protected methods */ - void writeContent(QIODevice &device); + void writeContent(QIODevice &device) const; /* [4] --- */ diff --git a/src/mimemessage.cpp b/src/mimemessage.cpp index ec79503..080ce6a 100644 --- a/src/mimemessage.cpp +++ b/src/mimemessage.cpp @@ -140,7 +140,7 @@ const QList & 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 ----------- */ diff --git a/src/mimemessage.h b/src/mimemessage.h index 9735c6a..b29a745 100644 --- a/src/mimemessage.h +++ b/src/mimemessage.h @@ -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] --- */ diff --git a/src/mimemultipart.cpp b/src/mimemultipart.cpp index c922108..8ea594a 100644 --- a/src/mimemultipart.cpp +++ b/src/mimemultipart.cpp @@ -54,10 +54,10 @@ const QList & MimeMultiPart::getParts() const { return parts; } -void MimeMultiPart::writeContent(QIODevice &device) { - QList::iterator it; +void MimeMultiPart::writeContent(QIODevice &device) const { + QList::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"); diff --git a/src/mimemultipart.h b/src/mimemultipart.h index f4770c4..1450ad4 100644 --- a/src/mimemultipart.h +++ b/src/mimemultipart.h @@ -60,7 +60,7 @@ public: void addPart(MimePart *part); - void writeContent(QIODevice &device); + void writeContent(QIODevice &device) const; /* [3] --- */ diff --git a/src/mimepart.cpp b/src/mimepart.cpp index 58d1f5b..764d52c 100644 --- a/src/mimepart.cpp +++ b/src/mimepart.cpp @@ -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: diff --git a/src/mimepart.h b/src/mimepart.h index bb3d65a..acb057d 100644 --- a/src/mimepart.h +++ b/src/mimepart.h @@ -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 diff --git a/src/mimetext.cpp b/src/mimetext.cpp index 288c6b9..a6213d6 100644 --- a/src/mimetext.cpp +++ b/src/mimetext.cpp @@ -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] --- */ diff --git a/src/mimetext.h b/src/mimetext.h index eb75894..3c171ec 100644 --- a/src/mimetext.h +++ b/src/mimetext.h @@ -52,7 +52,7 @@ protected: /* [4] Protected methods */ - void writeContent(QIODevice &device); + void writeContent(QIODevice &device) const; /* [4] --- */ diff --git a/src/quotedprintable.cpp b/src/quotedprintable.cpp index 1d83ddb..cf54af0 100644 --- a/src/quotedprintable.cpp +++ b/src/quotedprintable.cpp @@ -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); diff --git a/src/smtpclient.cpp b/src/smtpclient.cpp index a0e8190..37d48d6 100644 --- a/src/smtpclient.cpp +++ b/src/smtpclient.cpp @@ -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 *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 { diff --git a/src/smtpclient.h b/src/smtpclient.h index 19cb27c..bffe59f 100644 --- a/src/smtpclient.h +++ b/src/smtpclient.h @@ -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::const_iterator addressIt; - const QList *addressList; + const MimeMessage *email; int rcptType; enum _RcptType { _TO = 1, _CC = 2, _BCC = 3}; + QList::const_iterator addressIt; + QList::const_iterator addressItEnd; + /* [4] --- */ /* [5] Protected methods */ + void login(); void setConnectionType(ConnectionType ct); void changeState(ClientState state); void processResponse();