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
00044
00045
00046
00047
00048
00049
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
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
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
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
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
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