Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

mailfactory.cpp

Go to the documentation of this file.
00001 #include <qmessagebox.h>
00002 #include <qtextstream.h>
00003 #include <qfile.h>
00004 
00005 #include <qpe/mimetype.h>
00006 
00007 #include "miscfunctions.h"
00008 #include "mailfactory.h"
00009 #include "defines.h"
00010 
00011 MailFactory::MailFactory(SendMail &smail, QWidget *parent)
00012         : QObject(), _smail(smail), _parent(parent)
00013 {
00014         _abort = false;
00015         Account account = _smail.account();
00016 
00017         if (!_smail.from().isEmpty())
00018                 _header += "From: " + _smail.from() + "\n";
00019         if (!_smail.replyTo().isEmpty())
00020                 _header += "Reply-To: " + _smail.replyTo() + "\n";
00021         if (!_smail.to().isEmpty())
00022                 _header += "To: " + _smail.to() + "\n";
00023         if (!_smail.cc().isEmpty())
00024                 _header += "Cc: " + _smail.cc() + "\n";
00025         if (!_smail.bcc().isEmpty())
00026                 _header += "Bcc: " + _smail.bcc() + "\n";
00027         if (!_smail.subject().isEmpty())
00028                 _header += "Subject: " + _smail.subject() + "\n";
00029         if (!_smail.priority().isEmpty() || (_smail.priority() != "Normal"))
00030                 _header += "Priority: " + _smail.priority() + "\n";
00031         _header += "Date: " + MiscFunctions::rfcDate() + "\n";
00032         if (!_smail.account().org().isEmpty())
00033                 _header += "Organization: " + _smail.account().org() + "\n";
00034         if (_smail.needsMime())
00035                 _header += "Mime-Version: 1.0\n";
00036         _header += "Message-Id: <" + MiscFunctions::uniqueString() + "." + account.email() + ">\n";
00037         if (!_smail.inReplyTo().isEmpty())
00038                 _header += "In-Reply-To: " + _smail.inReplyTo() + "\n";
00039         if (!QString((QString) USERAGENT).isEmpty())
00040                 _header += (QString) "User-Agent: " + USERAGENT + "\n";
00041 
00042         if (!_smail.needsMime()) {
00043 //              if (_smail.encrypt() && !_smail.sign()) {
00044 //                      openPgpEncrypt(_smail.message(), _header, _body);
00045 //              } else if (!_smail.encrypt() && _smail.sign()) {
00046 //                      openPgpSign(_smail.message(), _header, _body);
00047 //              } else if (_smail.encrypt() && _smail.sign()) {
00048 //                      openPgpSignEncrypt(_smail.message(), _header, _body);
00049 //              } else  {
00050                         _body += _smail.message();
00051 //              }
00052         } else {
00053                 QString boundary = MiscFunctions::uniqueString();
00054 
00055                 _header += "Content-Type: multipart/mixed; boundary=\"" + boundary + "\"\n";
00056 
00057                 _body += "This is a multi-part message in MIME format.\n\n";
00058                 _body += "--" + boundary + "\n";
00059 
00060 //              if (_smail.encrypt() && !_smail.sign()) {
00061 //                      QString header, body;
00062 //                      openPgpEncrypt(_smail.message(), header, body);
00063 //                      _body += header + "\n" + body + "\n";
00064 //              } else if (!_smail.encrypt() && _smail.sign()) {
00065 //                      QString header, body;
00066 //                      openPgpSign(_smail.message(), header, body);
00067 //                      _body += header + "\n" + body + "\n";
00068 //              } else if (_smail.encrypt() && _smail.sign()) {
00069 //                      QString header, body;
00070 //                      openPgpSignEncrypt(_smail.message(), header, body);
00071 //                      _body += header + "\n" + body + "\n";
00072 //              } else {
00073 
00074                         // TODO: Do proper charset handling!
00075                         _body += "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
00076                         _body += "Content-Transfer-Encoding: 8bit\n\n";
00077                         _body += _smail.message() + "\n";
00078 //              }
00079 
00080                 QValueList<Attachment> attachments = _smail.attachments();
00081                 QValueList<Attachment>::Iterator it;
00082                 for (it = attachments.begin(); it != attachments.end(); it++) {
00083                         QFile f((*it).fileName());
00084                         if (f.open(IO_ReadOnly)) {
00085                                 QTextStream t(&f);
00086                                 QString file;
00087                                 while (!t.atEnd()) file += t.readLine() + "\n";
00088                                 f.close();
00089                                 QString mimetype = (new MimeType((*it).docLnk()))->id(); 
00090 
00091                                 _body += "\n--" + boundary + "\n";
00092                                 _body += "Content-Type: " + mimetype + "; name=\"" + (*it).newName() + "\"\n";
00093 
00094                                 // TODO: Decide which content transfer encoding is best. B64 for binary, QP for text.
00095                                 _body += "Content-Transfer-Encoding: base64\n";
00096 
00097                                 _body += "Content-Disposition: attachment; filename=\"" + (*it).newName() + "\"\n";
00098                                 if (!(*it).description().isEmpty()) 
00099                                         _body += "Content-Description: " + (*it).description() + "\n";
00100 
00101                                 _body += "\n" + MiscFunctions::encodeBase64(file) + "\n";
00102                         } else {
00103                                 int ret = QMessageBox::critical(_parent, tr("Error"), tr("<p>Couldn't attach file '%1'. Continue anyway or abort?</p>").arg((*it).fileName()), tr("Continue"), tr("Abort"));
00104                                 if (ret != 0) {
00105                                         _abort = true;
00106                                         break;
00107                                 }
00108                         }
00109                 }
00110                 _body += "\n--" + boundary + "--";
00111         }
00112 
00113         if (_abort) {
00114                 _body = QString(0);
00115                 _header = QString(0);
00116         }
00117 }
00118 
00119 // Unfinished GPG code.
00120 /*
00121 void MailFactory::openPgpEncrypt(const QString &text, QString &header, QString &body)
00122 {
00123         QString boundary = MiscFunctions::uniqueString();
00124 
00125         header += "Content-Type: multipart/encrypted; boundary=\"" + boundary + "\"; protocol=\"application/pgp-encrypted\"\n";
00126 
00127         body += "--" + boundary + "\n";
00128         body += "Content-Type: application/pgp-encrypted\n\n";
00129         body += "Version: 1\n\n";
00130         body += "--" + boundary + "\n";
00131         body += "Content-Type: application/octet-stream\n\n";
00132         body += GpgHandling::encrypt(_smail.gpgReceivers(), text);
00133         body += "\n--" + boundary + "--\n";
00134 }
00135 
00136 void MailFactory::openPgpSign(const QString &text, QString &header, QString &body)
00137 {
00138         QString boundary = MiscFunctions::uniqueString();
00139 
00140         header += "Content-Type: multipart/signed; boundary=\"" + boundary + "\"; protocol=\"application/pgp-signature\"\n";
00141 
00142         body += "--" + boundary + "\n";
00143 
00144         QString temp;
00145         temp += "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
00146         temp += "Content-Transfer-Encoding: quoted-printable\n\n";
00147         temp += MiscFunctions::encodeQPrintable(text) + "\n";
00148         body += temp;
00149 
00150         temp.replace(QRegExp("\n"), "\r\n");
00151         QString signature = GpgHandling::sign(temp, _parent);
00152 
00153         body += "\n--" + boundary + "\n";
00154         body += "Content-Type: application/pgp-signature\n\n";
00155         body += signature + "\n";
00156         body += "\n--" + boundary + "--\n";
00157 }
00158 
00159 void MailFactory::openPgpSignEncrypt(const QString &text, QString &header, QString &message)
00160 {
00161         QString header_, message_;
00162         openPgpSign(text, header_, message_);
00163         openPgpEncrypt(header_ + "\n" + message_, header, message);
00164 }
00165 */
00166 void MailFactory::genMail(QString &header, QString &message, SendMail &smail, QWidget *parent)
00167 {
00168         MailFactory factory(smail, parent);
00169 
00170         header = factory._header;
00171         message = factory._body;
00172 }
00173 

Generated on Sat Nov 5 16:18:08 2005 for OPIE by  doxygen 1.4.2