From 2b7f4757660685896cfde92a35f566de61d24300 Mon Sep 17 00:00:00 2001 From: bluetiger9 Date: Sun, 13 Nov 2011 19:18:58 +0200 Subject: [PATCH] The structure of MimePart and its subclasses was modified. Now the following properties are part of MimePart: content-type, content- added to project. --- demos/demo3.cpp | 7 +-- demos/demo4.cpp | 4 +- src/mimeattachment.cpp | 5 +- src/mimefile.cpp | 33 +++------- src/mimefile.h | 8 --- src/mimehtml.cpp | 52 ++-------------- src/mimehtml.h | 12 +--- src/mimeinlinefile.cpp | 19 ++---- src/mimeinlinefile.h | 5 -- src/mimepart.cpp | 133 ++++++++++++++++++++++++++++++++++++++-- src/mimepart.h | 49 ++++++++++----- src/mimetext.cpp | 54 +++------------- src/mimetext.h | 7 --- src/quotedprintable.cpp | 64 +++++++++++++++++++ src/quotedprintable.h | 33 ++++++++++ 15 files changed, 296 insertions(+), 189 deletions(-) create mode 100644 src/quotedprintable.cpp create mode 100644 src/quotedprintable.h diff --git a/demos/demo3.cpp b/demos/demo3.cpp index 8571ef3..3e3e8ec 100644 --- a/demos/demo3.cpp +++ b/demos/demo3.cpp @@ -25,19 +25,18 @@ int main(int argc, char *argv[]) SmtpClient smtp("smtp.gmail.com", 465, SmtpClient::SslConnection); - smtp.setUser("your_email_address@gmail.com"); + smtp.setUser("your_email@gmail.com"); smtp.setPassword("your_password"); // Create a MimeMessage MimeMessage message; - message.setSender(new EmailAddress("your_email_address@gmail.com", "Your Name")); + message.setSender(new EmailAddress("your_email@gmail.com", "Your Name")); message.addRecipient(new EmailAddress("recipient@host.com", "Recipient's Name")); message.setSubject("SmtpClient for Qt - Demo"); // Add some text - MimeText text; text.setText("Hi!\n This is an email with some attachments."); message.addPart(&text); @@ -46,7 +45,7 @@ int main(int argc, char *argv[]) MimeAttachment attachment (new QFile("image1.jpg")); // the file type can be setted. (by default is application/octet-stream) - attachment.setType("image/jpg"); + attachment.setContentType("image/jpg"); // Now add it to message message.addPart(&attachment); diff --git a/demos/demo4.cpp b/demos/demo4.cpp index 72bb5d2..7749d3c 100644 --- a/demos/demo4.cpp +++ b/demos/demo4.cpp @@ -52,11 +52,11 @@ int main(int argc, char *argv[]) // An unique content id must be setted image1.setContentId("image1"); - image1.setType("image/jpg"); + image1.setContentType("image/jpg"); MimeInlineFile image2 (new QFile("image2.jpg")); image2.setContentId("image2"); - image2.setType("image/jpg"); + image2.setContentType("image/jpg"); message.addPart(&html); message.addPart(&image1); diff --git a/src/mimeattachment.cpp b/src/mimeattachment.cpp index 922288e..ce17a72 100644 --- a/src/mimeattachment.cpp +++ b/src/mimeattachment.cpp @@ -35,9 +35,10 @@ MimeAttachment::~MimeAttachment() void MimeAttachment::prepare() { - MimeFile::prepare(); + this->header += "Content-disposition: attachment\n"; - this->header += "Content-disposition: attachment;\n"; + /* !!! IMPORTANT !!! */ + MimeFile::prepare(); } /* [2] --- */ diff --git a/src/mimefile.cpp b/src/mimefile.cpp index 38d2be3..869e26f 100644 --- a/src/mimefile.cpp +++ b/src/mimefile.cpp @@ -23,8 +23,9 @@ MimeFile::MimeFile(QFile *file) { this->file = file; - this->type = "application/octet-stream"; - this->name = QFileInfo(*file).fileName(); + this->cType = "application/octet-stream"; + this->cName = QFileInfo(*file).fileName(); + this->cEncoding = Base64; } MimeFile::~MimeFile() @@ -36,26 +37,6 @@ MimeFile::~MimeFile() /* [2] Getters and setters */ -void MimeFile::setName(const QString & name) -{ - this->name = name; -} - -void MimeFile::setType(const QString & type) -{ - this->type = type; -} - -const QString & MimeFile::getName() const -{ - return name; -} - -const QString & MimeFile::getType() const -{ - return type; -} - /* [2] --- */ @@ -63,12 +44,12 @@ const QString & MimeFile::getType() const void MimeFile::prepare() { - this->header = "Content-Type: " + type + "; name=" + name + "\n"; - this->header += "Content-Transfer-Encoding: base64\n"; - file->open(QIODevice::ReadOnly); - this->content = file->readAll().toBase64() + "\n"; + this->content = file->readAll(); file->close(); + + /* !!! IMPORTANT !!!! */ + MimePart::prepare(); } /* [3] --- */ diff --git a/src/mimefile.h b/src/mimefile.h index e34b075..4db958b 100644 --- a/src/mimefile.h +++ b/src/mimefile.h @@ -35,12 +35,6 @@ public: /* [2] Getters and Setters */ - void setName(const QString & name); - void setType(const QString & type); - - const QString & getName() const; - const QString & getType() const; - /* [2] --- */ protected: @@ -48,8 +42,6 @@ protected: /* [3] Protected members */ QFile* file; - QString name; - QString type; /* [3] --- */ diff --git a/src/mimehtml.cpp b/src/mimehtml.cpp index 6e0789d..2ef4635 100644 --- a/src/mimehtml.cpp +++ b/src/mimehtml.cpp @@ -18,11 +18,9 @@ /* [1] Constructors and Destructors */ -MimeHtml::MimeHtml(const QString &html) +MimeHtml::MimeHtml(const QString &html) : MimeText(html) { - this->html = html; - this->encoding = _7Bit; - this->charset = "utf-8"; + this->cType = "text/html"; } MimeHtml::~MimeHtml() {} @@ -34,33 +32,14 @@ MimeHtml::~MimeHtml() {} void MimeHtml::setHtml(const QString & html) { - this->html = html; -} - -void MimeHtml::setEncoding(MimePart::Encoding enc) -{ - this->encoding = enc; -} - -void MimeHtml::setCharset(const QString & charset) -{ - this->charset = charset; + this->text = html; } const QString & MimeHtml::getHtml() const { - return html; + return text; } -const QString & MimeHtml::getCharset() const -{ - return charset; -} - -MimePart::Encoding MimeHtml::getEncoding() const -{ - return encoding; -} /* [2] --- */ @@ -69,27 +48,8 @@ MimePart::Encoding MimeHtml::getEncoding() const void MimeHtml::prepare() { - this->header = "Content-Type: text/html; charset=" - + this->charset + "\n"; - - this->header += "Content-Transfer-Encoding: "; - - switch (encoding) - { - case _7Bit: - header += "7bit\n"; - content = this->html.toAscii(); - break; - case _8Bit: - header += "8bit\n"; - content = this->html.toUtf8(); - break; - case Base64: - header += "base64\n"; - content = QByteArray().append(this->html).toBase64(); - } - - this->content += "\n"; + /* !!! IMPORTANT !!! */ + MimeText::prepare(); } /* [3] --- */ diff --git a/src/mimehtml.h b/src/mimehtml.h index 00335bd..a4bf608 100644 --- a/src/mimehtml.h +++ b/src/mimehtml.h @@ -17,9 +17,9 @@ #ifndef MIMEHTML_H #define MIMEHTML_H -#include "mimepart.h" +#include "mimetext.h" -class MimeHtml : public MimePart +class MimeHtml : public MimeText { Q_OBJECT public: @@ -35,12 +35,8 @@ public: /* [2] Getters and Setters */ void setHtml(const QString & html); - void setEncoding(Encoding enc); - void setCharset(const QString & charset); const QString& getHtml() const; - const QString& getCharset() const; - Encoding getEncoding() const; /* [2] --- */ @@ -48,10 +44,6 @@ protected: /* [3] Protected members */ - QString html; - QString charset; - Encoding encoding; - /* [3] --- */ diff --git a/src/mimeinlinefile.cpp b/src/mimeinlinefile.cpp index 83930bd..020252e 100644 --- a/src/mimeinlinefile.cpp +++ b/src/mimeinlinefile.cpp @@ -21,7 +21,6 @@ MimeInlineFile::MimeInlineFile(QFile *f) : MimeFile(f) { - return; } MimeInlineFile::~MimeInlineFile() @@ -32,27 +31,17 @@ MimeInlineFile::~MimeInlineFile() /* [2] Getters and Setters */ -void MimeInlineFile::setContentId(const QString & cid) -{ - this->cid = cid; -} - -const QString & MimeInlineFile::getContentId() const -{ - return cid; -} - /* [2] --- */ /* [3] Protected methods */ void MimeInlineFile::prepare() -{ - MimeFile::prepare(); +{ + this->header += "Content-Disposition: inline\n"; - this->header += "Content-id: <" + cid + ">\n"; - this->header += "Content-disposition: inline\n"; + /* !!! IMPORTANT !!! */ + MimeFile::prepare(); } /* [3] --- */ diff --git a/src/mimeinlinefile.h b/src/mimeinlinefile.h index e2930de..00766a3 100644 --- a/src/mimeinlinefile.h +++ b/src/mimeinlinefile.h @@ -33,17 +33,12 @@ public: /* [2] Getters and Setters */ - void setContentId(const QString & cid); - const QString & getContentId() const; - /* [2] --- */ protected: /* [3] Protected members */ - QString cid; - /* [3] --- */ diff --git a/src/mimepart.cpp b/src/mimepart.cpp index 00f3c35..70aec14 100644 --- a/src/mimepart.cpp +++ b/src/mimepart.cpp @@ -15,11 +15,14 @@ */ #include "mimepart.h" +#include "quotedprintable.h" /* [1] Constructors and Destructors */ MimePart::MimePart() { + cEncoding = _7Bit; + prepared = false; } MimePart::~MimePart() @@ -32,7 +35,7 @@ MimePart::~MimePart() /* [2] Getters and Setters */ -void MimePart::setContent(const QString & content) +void MimePart::setContent(const QByteArray & content) { this->content = content; } @@ -52,11 +55,61 @@ const QString& MimePart::getHeader() const return header; } -const QString& MimePart::getContent() const +const QByteArray& MimePart::getContent() const { return content; } +void MimePart::setContentId(const QString & cId) +{ + this->cId = cId; +} + +const QString & MimePart::getContentId() const +{ + return this->cId; +} + +void MimePart::setContentName(const QString & cName) +{ + this->cName = cName; +} + +const QString & MimePart::getContentName() const +{ + return this->cName; +} + +void MimePart::setContentType(const QString & cType) +{ + this->cType = cType; +} + +const QString & MimePart::getContentType() const +{ + return this->cType; +} + +void MimePart::setCharset(const QString & charset) +{ + this->cCharset = charset; +} + +const QString & MimePart::getCharset() const +{ + return this->cCharset; +} + +void MimePart::setEncoding(Encoding enc) +{ + this->cEncoding = enc; +} + +MimePart::Encoding MimePart::getEncoding() const +{ + return this->cEncoding; +} + /* [2] --- */ @@ -64,9 +117,10 @@ const QString& MimePart::getContent() const QString MimePart::toString() { - prepare(); + if (!prepared) + prepare(); - return header + "\n" + content; + return mimeString; } /* [3] --- */ @@ -74,6 +128,75 @@ QString MimePart::toString() /* [4] Protected methods */ -void MimePart::prepare() {} +void MimePart::prepare() +{ + mimeString = QString(); + + /* === Header Prepare === */ + + /* Content-Type */ + mimeString.append("Content-Type: ").append(cType); + + if (cName != "") + mimeString.append("; name=\"").append(cName).append("\""); + + if (cCharset != "") + mimeString.append("; charset=").append(cCharset); + + mimeString.append("\n"); + /* ------------ */ + + /* Content-Transfer-Encoding */ + mimeString.append("Content-Transfer-Encoding: "); + switch (cEncoding) + { + case _7Bit: + mimeString.append("7bit\n"); + break; + case _8Bit: + mimeString.append("8bit\n"); + break; + case Base64: + mimeString.append("base64\n"); + break; + case QuotedPrintable: + mimeString.append("quoted-printable\n"); + break; + } + /* ------------------------ */ + + /* Content-Id */ + if (cId != NULL) + mimeString.append("Content-ID: <").append(cId).append(">\n"); + /* ---------- */ + + + /* ------------------------- */ + + mimeString.append(header).append("\n"); + + /* === End of Header Prepare === */ + + /* === Content Encoding === */ + switch (cEncoding) + { + case _7Bit: + mimeString.append(QString(content).toAscii()); + break; + case _8Bit: + mimeString.append(content); + break; + case Base64: + mimeString.append(content.toBase64()); + break; + case QuotedPrintable: + mimeString.append(QuotedPrintable::encode(content)); + break; + } + mimeString.append("\n"); + /* === End of Content Encoding === */ + + prepared = true; +} /* [4] --- */ diff --git a/src/mimepart.h b/src/mimepart.h index 80d6407..617cc26 100644 --- a/src/mimepart.h +++ b/src/mimepart.h @@ -25,11 +25,11 @@ class MimePart : public QObject public: /* [0] Enumerations */ - - enum Encoding { - Base64, + enum Encoding { _7Bit, - _8Bit + _8Bit, + Base64, + QuotedPrintable }; @@ -47,13 +47,28 @@ public: /* [2] Getters and Setters */ const QString& getHeader() const; - const QString& getContent() const; + const QByteArray& getContent() const; - void setContent(const QString & content); + void setContent(const QByteArray & content); void setHeader(const QString & header); void addHeaderLine(const QString & line); + void setContentId(const QString & cId); + const QString & getContentId() const; + + void setContentName(const QString & cName); + const QString & getContentName() const; + + void setContentType(const QString & cType); + const QString & getContentType() const; + + void setCharset(const QString & charset); + const QString & getCharset() const; + + void setEncoding(Encoding enc); + Encoding getEncoding() const; + /* [2] --- */ @@ -61,23 +76,29 @@ public: QString toString(); + virtual void prepare(); + /* [3] --- */ + + protected: /* [4] Protected members */ QString header; - QString content; + QByteArray content; + + QString cId; + QString cName; + QString cType; + QString cCharset; + Encoding cEncoding; + + QString mimeString; + bool prepared; /* [4] --- */ - - - /* [5] Protected methods */ - - virtual void prepare(); - - /* [5] --- */ }; #endif // MIMEPART_H diff --git a/src/mimetext.cpp b/src/mimetext.cpp index dd70024..74cf3ad 100644 --- a/src/mimetext.cpp +++ b/src/mimetext.cpp @@ -18,11 +18,12 @@ /* [1] Constructors and Destructors */ -MimeText::MimeText(const QString &text) +MimeText::MimeText(const QString &txt) { - this->text = text; - this->charset = "utf-8"; - this->encoding = _7Bit; + this->text = txt; + this->cType = "text/plain"; + this->cCharset = "utf-8"; + this->cEncoding = _8Bit; } MimeText::~MimeText() { } @@ -37,31 +38,11 @@ void MimeText::setText(const QString & text) this->text = text; } -void MimeText::setEncoding(MimePart::Encoding enc) -{ - this->encoding = enc; -} - -void MimeText::setCharset(const QString & charset) -{ - this->charset = charset; -} - const QString & MimeText::getText() const { return text; } -MimePart::Encoding MimeText::getEncoding() const -{ - return encoding; -} - -const QString & MimeText::getCharset() const -{ - return charset; -} - /* [2] --- */ @@ -69,28 +50,11 @@ const QString & MimeText::getCharset() const void MimeText::prepare() { - this->header = "Content-Type: text/plain; charset=" - + this->charset + "\n"; - - this->header += "Content-Transfer-Encoding: "; - - switch (encoding) - { - case _7Bit: - header += "7bit\n"; - content = this->text.toAscii(); - break; - case _8Bit: - header += "8bit\n"; - content = this->text.toUtf8(); - break; - case Base64: - header += "base64\n"; - content = QByteArray().append(this->text).toBase64(); - } - - this->content += "\n"; + this->content.clear(); + this->content.append(text); + /* !!! IMPORTANT !!! */ + MimePart::prepare(); } /* [3] --- */ diff --git a/src/mimetext.h b/src/mimetext.h index 198374f..69a1859 100644 --- a/src/mimetext.h +++ b/src/mimetext.h @@ -34,12 +34,8 @@ public: /* [2] Getters and Setters*/ void setText(const QString & text); - void setEncoding(Encoding enc); - void setCharset(const QString & charset); const QString & getText() const; - MimePart::Encoding getEncoding() const; - const QString & getCharset() const; /* [2] --- */ @@ -48,9 +44,6 @@ protected: /* [3] Protected members */ QString text; - QString charset; - Encoding encoding; - /* [3] --- */ diff --git a/src/quotedprintable.cpp b/src/quotedprintable.cpp new file mode 100644 index 0000000..18dd4c8 --- /dev/null +++ b/src/quotedprintable.cpp @@ -0,0 +1,64 @@ +/* + Copyright (c) 2011 - Tőkés Attila (tokes_atti@yahoo.com) + + This project is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + This project is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY. + + See the LICENSE file for more details. +*/ + +#include "quotedprintable.h" + +QString& QuotedPrintable::encode(const QByteArray &input) +{ + QString *output = new QString(); + + char byte; + const char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; + + for (int i = 0; i < input.length() ; ++i) + { + byte = input[i]; + + if ((byte == 0x20) || (byte >= 33) && (byte <= 126) && (byte != 61)) + { + output->append(byte); + } + else + { + output->append('='); + output->append(hex[((byte >> 4) & 0x0F)]); + output->append(hex[(byte & 0x0F)]); + } + } + + return *output; +} + + +QByteArray& QuotedPrintable::decode(const QString &input) +{ + // 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F + const int hexVal[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 10, 11, 12, 13, 14, 15}; + + QByteArray *output = new QByteArray(); + + for (int i = 0; i < input.length(); ++i) + { + if (input.at(i).toAscii() == '=') + { + output->append((hexVal[input.at(++i).toAscii() - '0'] << 4) + hexVal[input.at(++i).toAscii() - '0']); + } + else + { + output->append(input.at(i).toAscii()); + } + } + + return *output; +} diff --git a/src/quotedprintable.h b/src/quotedprintable.h new file mode 100644 index 0000000..0be28e2 --- /dev/null +++ b/src/quotedprintable.h @@ -0,0 +1,33 @@ +/* + Copyright (c) 2011 - Tőkés Attila (tokes_atti@yahoo.com) + + This project is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + This project is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY. + + See the LICENSE file for more details. +*/ + +#ifndef QUOTEDPRINTABLE_H +#define QUOTEDPRINTABLE_H + +#include +#include + +class QuotedPrintable : public QObject +{ + Q_OBJECT +public: + + static QString& encode(const QByteArray &input); + static QByteArray& decode(const QString &input); + +private: + QuotedPrintable(); +}; + +#endif // QUOTEDPRINTABLE_H