Diffstat (limited to 'noncore/unsupported/mail2/libmail/mailfactory.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/unsupported/mail2/libmail/mailfactory.cpp | 173 |
1 files changed, 173 insertions, 0 deletions
diff --git a/noncore/unsupported/mail2/libmail/mailfactory.cpp b/noncore/unsupported/mail2/libmail/mailfactory.cpp new file mode 100644 index 0000000..58cf5f8 --- a/dev/null +++ b/noncore/unsupported/mail2/libmail/mailfactory.cpp | |||
@@ -0,0 +1,173 @@ | |||
1 | #include <qmessagebox.h> | ||
2 | #include <qtextstream.h> | ||
3 | #include <qfile.h> | ||
4 | |||
5 | #include <qpe/mimetype.h> | ||
6 | |||
7 | #include "miscfunctions.h" | ||
8 | #include "mailfactory.h" | ||
9 | #include "defines.h" | ||
10 | |||
11 | MailFactory::MailFactory(SendMail &smail, QWidget *parent) | ||
12 | : QObject(), _smail(smail), _parent(parent) | ||
13 | { | ||
14 | _abort = false; | ||
15 | Account account = _smail.account(); | ||
16 | |||
17 | if (!_smail.from().isEmpty()) | ||
18 | _header += "From: " + _smail.from() + "\n"; | ||
19 | if (!_smail.replyTo().isEmpty()) | ||
20 | _header += "Reply-To: " + _smail.replyTo() + "\n"; | ||
21 | if (!_smail.to().isEmpty()) | ||
22 | _header += "To: " + _smail.to() + "\n"; | ||
23 | if (!_smail.cc().isEmpty()) | ||
24 | _header += "Cc: " + _smail.cc() + "\n"; | ||
25 | if (!_smail.bcc().isEmpty()) | ||
26 | _header += "Bcc: " + _smail.bcc() + "\n"; | ||
27 | if (!_smail.subject().isEmpty()) | ||
28 | _header += "Subject: " + _smail.subject() + "\n"; | ||
29 | if (!_smail.priority().isEmpty() || (_smail.priority() != "Normal")) | ||
30 | _header += "Priority: " + _smail.priority() + "\n"; | ||
31 | _header += "Date: " + MiscFunctions::rfcDate() + "\n"; | ||
32 | if (!_smail.account().org().isEmpty()) | ||
33 | _header += "Organization: " + _smail.account().org() + "\n"; | ||
34 | if (_smail.needsMime()) | ||
35 | _header += "Mime-Version: 1.0\n"; | ||
36 | _header += "Message-Id: <" + MiscFunctions::uniqueString() + "." + account.email() + ">\n"; | ||
37 | if (!_smail.inReplyTo().isEmpty()) | ||
38 | _header += "In-Reply-To: " + _smail.inReplyTo() + "\n"; | ||
39 | if (!QString((QString) USERAGENT).isEmpty()) | ||
40 | _header += (QString) "User-Agent: " + USERAGENT + "\n"; | ||
41 | |||
42 | if (!_smail.needsMime()) { | ||
43 | // if (_smail.encrypt() && !_smail.sign()) { | ||
44 | // openPgpEncrypt(_smail.message(), _header, _body); | ||
45 | // } else if (!_smail.encrypt() && _smail.sign()) { | ||
46 | // openPgpSign(_smail.message(), _header, _body); | ||
47 | // } else if (_smail.encrypt() && _smail.sign()) { | ||
48 | // openPgpSignEncrypt(_smail.message(), _header, _body); | ||
49 | // } else { | ||
50 | _body += _smail.message(); | ||
51 | // } | ||
52 | } else { | ||
53 | QString boundary = MiscFunctions::uniqueString(); | ||
54 | |||
55 | _header += "Content-Type: multipart/mixed; boundary=\"" + boundary + "\"\n"; | ||
56 | |||
57 | _body += "This is a multi-part message in MIME format.\n\n"; | ||
58 | _body += "--" + boundary + "\n"; | ||
59 | |||
60 | // if (_smail.encrypt() && !_smail.sign()) { | ||
61 | // QString header, body; | ||
62 | // openPgpEncrypt(_smail.message(), header, body); | ||
63 | // _body += header + "\n" + body + "\n"; | ||
64 | // } else if (!_smail.encrypt() && _smail.sign()) { | ||
65 | // QString header, body; | ||
66 | // openPgpSign(_smail.message(), header, body); | ||
67 | // _body += header + "\n" + body + "\n"; | ||
68 | // } else if (_smail.encrypt() && _smail.sign()) { | ||
69 | // QString header, body; | ||
70 | // openPgpSignEncrypt(_smail.message(), header, body); | ||
71 | // _body += header + "\n" + body + "\n"; | ||
72 | // } else { | ||
73 | |||
74 | // TODO: Do proper charset handling! | ||
75 | _body += "Content-Type: text/plain; charset=\"iso-8859-1\"\n"; | ||
76 | _body += "Content-Transfer-Encoding: 8bit\n\n"; | ||
77 | _body += _smail.message() + "\n"; | ||
78 | // } | ||
79 | |||
80 | QValueList<Attachment> attachments = _smail.attachments(); | ||
81 | QValueList<Attachment>::Iterator it; | ||
82 | for (it = attachments.begin(); it != attachments.end(); it++) { | ||
83 | QFile f((*it).fileName()); | ||
84 | if (f.open(IO_ReadOnly)) { | ||
85 | QTextStream t(&f); | ||
86 | QString file; | ||
87 | while (!t.atEnd()) file += t.readLine() + "\n"; | ||
88 | f.close(); | ||
89 | QString mimetype = (new MimeType(*(*it).docLnk()))->id(); | ||
90 | |||
91 | _body += "\n--" + boundary + "\n"; | ||
92 | _body += "Content-Type: " + mimetype + "; name=\"" + (*it).newName() + "\"\n"; | ||
93 | |||
94 | // TODO: Decide which content transfer encoding is best. B64 for binary, QP for text. | ||
95 | _body += "Content-Transfer-Encoding: base64\n"; | ||
96 | |||
97 | _body += "Content-Disposition: attachment; filename=\"" + (*it).newName() + "\"\n"; | ||
98 | if (!(*it).description().isEmpty()) | ||
99 | _body += "Content-Description: " + (*it).description() + "\n"; | ||
100 | |||
101 | _body += "\n" + MiscFunctions::encodeBase64(file) + "\n"; | ||
102 | } else { | ||
103 | int ret = QMessageBox::critical(_parent, tr("Error"), tr("<p>Couldn't attach file '%1'. Continue anyway or abort?</p>"), tr("Continue"), tr("Abort")); | ||
104 | if (ret == 1) { | ||
105 | it = attachments.end(); | ||
106 | _abort = true; | ||
107 | } | ||
108 | } | ||
109 | } | ||
110 | _body += "\n--" + boundary + "--"; | ||
111 | } | ||
112 | |||
113 | if (_abort) { | ||
114 | _body = QString(0); | ||
115 | _header = QString(0); | ||
116 | } | ||
117 | } | ||
118 | |||
119 | // Unfinished GPG code. | ||
120 | /* | ||
121 | void MailFactory::openPgpEncrypt(const QString &text, QString &header, QString &body) | ||
122 | { | ||
123 | QString boundary = MiscFunctions::uniqueString(); | ||
124 | |||
125 | header += "Content-Type: multipart/encrypted; boundary=\"" + boundary + "\"; protocol=\"application/pgp-encrypted\"\n"; | ||
126 | |||
127 | body += "--" + boundary + "\n"; | ||
128 | body += "Content-Type: application/pgp-encrypted\n\n"; | ||
129 | body += "Version: 1\n\n"; | ||
130 | body += "--" + boundary + "\n"; | ||
131 | body += "Content-Type: application/octet-stream\n\n"; | ||
132 | body += GpgHandling::encrypt(_smail.gpgReceivers(), text); | ||
133 | body += "\n--" + boundary + "--\n"; | ||
134 | } | ||
135 | |||
136 | void MailFactory::openPgpSign(const QString &text, QString &header, QString &body) | ||
137 | { | ||
138 | QString boundary = MiscFunctions::uniqueString(); | ||
139 | |||
140 | header += "Content-Type: multipart/signed; boundary=\"" + boundary + "\"; protocol=\"application/pgp-signature\"\n"; | ||
141 | |||
142 | body += "--" + boundary + "\n"; | ||
143 | |||
144 | QString temp; | ||
145 | temp += "Content-Type: text/plain; charset=\"iso-8859-1\"\n"; | ||
146 | temp += "Content-Transfer-Encoding: quoted-printable\n\n"; | ||
147 | temp += MiscFunctions::encodeQPrintable(text) + "\n"; | ||
148 | body += temp; | ||
149 | |||
150 | temp.replace(QRegExp("\n"), "\r\n"); | ||
151 | QString signature = GpgHandling::sign(temp, _parent); | ||
152 | |||
153 | body += "\n--" + boundary + "\n"; | ||
154 | body += "Content-Type: application/pgp-signature\n\n"; | ||
155 | body += signature + "\n"; | ||
156 | body += "\n--" + boundary + "--\n"; | ||
157 | } | ||
158 | |||
159 | void MailFactory::openPgpSignEncrypt(const QString &text, QString &header, QString &message) | ||
160 | { | ||
161 | QString header_, message_; | ||
162 | openPgpSign(text, header_, message_); | ||
163 | openPgpEncrypt(header_ + "\n" + message_, header, message); | ||
164 | } | ||
165 | */ | ||
166 | void MailFactory::genMail(QString &header, QString &message, SendMail &smail, QWidget *parent) | ||
167 | { | ||
168 | MailFactory factory(smail, parent); | ||
169 | |||
170 | header = factory._header; | ||
171 | message = factory._body; | ||
172 | } | ||
173 | |||