00001 #include <qtextbrowser.h>
00002 #include <qmessagebox.h>
00003 #include <qaction.h>
00004 #include <qapplication.h>
00005
00006 #include "mailfactory.h"
00007 #include "composer.h"
00008 #include "viewmail.h"
00009
00010 AttachItem::AttachItem(QListView *parent, AttachItemStore &attachItemStore)
00011 : QListViewItem(parent), _attachItemStore(attachItemStore)
00012 {
00013 setText(0, _attachItemStore.mimeType());
00014 setText(1, _attachItemStore.fileName());
00015 setText(2, _attachItemStore.description());
00016 }
00017
00018 AttachItem::AttachItem(QListViewItem *parent, AttachItemStore &attachItemStore)
00019 : QListViewItem(parent), _attachItemStore(attachItemStore)
00020 {
00021 setText(0, _attachItemStore.mimeType());
00022 setText(1, _attachItemStore.fileName());
00023 setText(2, _attachItemStore.description());
00024 }
00025
00026 ViewMail::ViewMail(IMAPResponseFETCH &mail, IMAPHandler *handler, QWidget *parent, const char *name, WFlags fl)
00027 : ViewMailBase(parent, name, fl), _inLoop(false), _mail(mail), _handler(handler)
00028 {
00029 setCaption(caption().arg(mail.envelope().from()[0].name()));
00030
00031 _gotBody = false;
00032 _mailHtml = tr(
00033 "<html><body>"
00034 "<div align=center><b>%1</b></div>"
00035 "<b>From:</b> %2<br>"
00036 "<b>To:</b> %3<br>"
00037 "%4"
00038 "%5"
00039 "<b>Date:</b> %6<hr>"
00040 "<font face=fixed>%7</font>")
00041 .arg(deHtml(mail.envelope().subject().isNull() ? tr("(no subject)")
00042 : deHtml(mail.envelope().subject())))
00043 .arg(deHtml(mail.envelope().from().toString().isNull() ? tr("(no from)")
00044 : mail.envelope().from().toString()))
00045 .arg(deHtml(mail.envelope().to().toString().isNull() ? tr("(no recipient)")
00046 : mail.envelope().to().toString()))
00047 .arg(mail.envelope().cc().toString().isNull() ? QString(0)
00048 : tr("<b>Cc:</b> %1<br>").arg(deHtml(mail.envelope().cc().toString())))
00049 .arg(mail.envelope().bcc().toString().isNull() ? QString(0)
00050 : tr("<b>Bcc:</b> %1<br>").arg(deHtml(mail.envelope().bcc().toString())))
00051 .arg(mail.envelope().mailDate().isNull() ? tr("(no date)")
00052 : mail.envelope().mailDate())
00053 .arg("%1");
00054
00055 connect(reply, SIGNAL(activated()), SLOT(slotReply()));
00056 connect(forward, SIGNAL(activated()), SLOT(slotForward()));
00057
00058 attachments->setEnabled(_gotBody);
00059 browser->setText(QString(_mailHtml).arg(tr("Getting mail body from server. Please wait...")));
00060
00061 _handler->iUid("FETCH", QString("%1 (BODY[1])").arg(mail.uid()));
00062 connect(_handler, SIGNAL(gotResponse(IMAPResponse&)), SLOT(slotIMAPUid(IMAPResponse&)));
00063 }
00064
00065 ViewMail::~ViewMail()
00066 {
00067 hide();
00068 }
00069
00070 void ViewMail::hide()
00071 {
00072 QWidget::hide();
00073
00074 if (_inLoop) {
00075 _inLoop = false;
00076 qApp->exit_loop();
00077 }
00078 }
00079
00080 void ViewMail::exec()
00081 {
00082 show();
00083
00084 if (!_inLoop) {
00085 _inLoop = true;
00086 qApp->enter_loop();
00087 }
00088 }
00089
00090 QString ViewMail::deHtml(const QString &string)
00091 {
00092 QString string_ = string;
00093 string_.replace(QRegExp("&"), "&");
00094 string_.replace(QRegExp("<"), "<");
00095 string_.replace(QRegExp(">"), ">");
00096 string_.replace(QRegExp("\\n"), "<br>");
00097 return string_;
00098 }
00099
00100 void ViewMail::slotReply()
00101 {
00102 if (!_gotBody) {
00103 QMessageBox::information(this, tr("Error"), tr("<p>The mail body is not yet downloaded, so you cannot reply yet."), tr("Ok"));
00104 return;
00105 }
00106
00107 QString rtext;
00108 rtext += QString("* %1 wrote on %2:\n")
00109 .arg(_mail.envelope().from()[0].toString())
00110 .arg(_mail.envelope().mailDate());
00111
00112 QString text = _mail.bodyPart(1).data();
00113 QStringList lines = QStringList::split(QRegExp("\\n"), text);
00114 QStringList::Iterator it;
00115 for (it = lines.begin(); it != lines.end(); it++) {
00116 rtext += "> " + *it + "\n";
00117 }
00118 rtext += "\n";
00119
00120 QString prefix;
00121 if (_mail.envelope().subject().find(QRegExp("^Re: *$")) != -1) prefix = "";
00122 else prefix = "Re: ";
00123
00124 SendMail sendMail;
00125 sendMail.setTo(_mail.envelope().from()[0].toString());
00126 sendMail.setSubject(prefix + _mail.envelope().subject());
00127 sendMail.setInReplyTo(_mail.envelope().messageId());
00128 sendMail.setMessage(rtext);
00129
00130 Composer composer(this, 0, true);
00131 composer.setSendMail(sendMail);
00132 composer.showMaximized();
00133 composer.exec();
00134 }
00135
00136 void ViewMail::slotForward()
00137 {
00138 if (!_gotBody) {
00139 QMessageBox::information(this, tr("Error"), tr("<p>The mail body is not yet downloaded, so you cannot forward yet."), tr("Ok"));
00140 return;
00141 }
00142
00143 QString ftext;
00144 ftext += QString("\n----- Forwarded message from %1 -----\n\n")
00145 .arg(_mail.envelope().from()[0].toString());
00146 if (!_mail.envelope().mailDate().isNull())
00147 ftext += QString("Date: %1\n")
00148 .arg(_mail.envelope().mailDate());
00149 if (!_mail.envelope().from()[0].toString().isNull())
00150 ftext += QString("From: %1\n")
00151 .arg(_mail.envelope().from()[0].toString());
00152 if (!_mail.envelope().to().toString().isNull())
00153 ftext += QString("To: %1\n")
00154 .arg(_mail.envelope().to().toString());
00155 if (!_mail.envelope().cc().toString().isNull())
00156 ftext += QString("Cc: %1\n")
00157 .arg(_mail.envelope().cc().toString());
00158 if (!_mail.envelope().bcc().toString().isNull())
00159 ftext += QString("Bcc: %1\n")
00160 .arg(_mail.envelope().bcc().toString());
00161 if (!_mail.envelope().subject().isNull())
00162 ftext += QString("Subject: %1\n")
00163 .arg(_mail.envelope().subject());
00164
00165 ftext += QString("\n%1\n")
00166 .arg(_mail.bodyPart(1).data());
00167
00168 ftext += QString("----- End forwarded message -----\n");
00169
00170 SendMail sendMail;
00171 sendMail.setSubject("Fwd: " + _mail.envelope().subject());
00172 sendMail.setMessage(ftext);
00173
00174 Composer composer(this, 0, true);
00175 composer.setSendMail(sendMail);
00176 composer.showMaximized();
00177 composer.exec();
00178 }
00179
00180 void ViewMail::slotIMAPUid(IMAPResponse &response)
00181 {
00182 disconnect(_handler, SIGNAL(gotResponse(IMAPResponse&)), this, SLOT(slotIMAPUid(IMAPResponse&)));
00183
00184 if (response.statusResponse().status() == IMAPResponseEnums::OK) {
00185 QValueList<IMAPResponseBodyPart> bodyParts;
00186 bodyParts.append(response.FETCH()[0].bodyPart(0));
00187 _mail.setBodyParts(bodyParts);
00188
00189 browser->setText(QString(_mailHtml).arg(deHtml(response.FETCH()[0].bodyPart(0).data())));
00190
00191
00192
00193 _gotBody = true;
00194 } else {
00195 QMessageBox::warning(this, tr("Error"), tr("<p>I was unable to retrieve the mail from the server. You can try again later or give up.</p>"), tr("Ok"));
00196 }
00197 }
00198