1 /* Copyright (C) 2006 - 2014 Jan Kundrát <jkt@flaska.net>
3 This file is part of the Trojita Qt IMAP e-mail client,
4 http://trojita.flaska.net/
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of
9 the License or (at your option) version 3 or any later version
10 accepted by the membership of KDE e.V. (or its successor approved
11 by the membership of KDE e.V.), which shall act as a proxy
12 defined in Section 14 of version 3 of the license.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "EnvelopeView.h"
23 #include <QFontMetrics>
26 #include <QGridLayout>
27 #include <QLayoutItem>
28 #include <QVBoxLayout>
29 #include "Gui/AddressRowWidget.h"
30 #include "Gui/MessageView.h"
32 #include "Imap/Model/ItemRoles.h"
33 #include "Imap/Model/MailboxTree.h"
34 #include "Imap/Model/Model.h"
35 #include "UiUtils/Formatting.h"
39 EnvelopeView::EnvelopeView(QWidget
*parent
, MessageView
*messageView
): QWidget(parent
), m_messageView(messageView
)
41 QVBoxLayout
*lay
= new QVBoxLayout(this);
42 lay
->setContentsMargins(0, 0, 0, 0);
47 #define SET_LABEL_OPTIONS(LBL) \
49 LBL->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); \
50 LBL->setWordWrap(true); \
51 LBL->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::LinksAccessibleByMouse); \
54 #define ADD_ROW(LBL, WDG) \
56 QLabel *l = new QLabel(LBL, this); \
57 l->setAlignment(Qt::AlignRight); \
58 form->addWidget(l, form->rowCount(), 0, Qt::AlignTop); \
59 form->addWidget(WDG, form->rowCount()-1, 1, Qt::AlignTop); \
63 void EnvelopeView::setMessage(const QModelIndex
&index
)
65 while (QLayoutItem
*item
= layout()->takeAt(0)) {
67 item
->widget()->deleteLater();
68 } else if (item
->layout()) {
69 item
->layout()->deleteLater();
74 Q_FOREACH(QWidget
*w
, findChildren
<QWidget
*>()) {
78 if (!index
.isValid()) {
82 const Imap::Message::Envelope e
= index
.data(Imap::Mailbox::RoleMessageEnvelope
).value
<Imap::Message::Envelope
>();
87 if (e
.date
.isValid()) {
88 subDate
= QStringLiteral("<table style=\"margin:0px; margin-left:4em; float:right;\"><tr style=\"margin:0px;\"><td style=\"margin:0px;\">%1</td></tr></table>").arg(e
.date
.toLocalTime().toString(Qt::SystemLocaleLongDate
));
90 subDate
+= QStringLiteral("<span style=\"font:bold large;\">%1</span>").arg(e
.subject
.toHtmlEscaped());
91 auto lbl
= new QLabel(subDate
, this);
92 SET_LABEL_OPTIONS(lbl
)
93 layout()->addWidget(lbl
);
95 QGridLayout
*form
= new QGridLayout();
97 form
->setContentsMargins(0, 0, 0, 0);
98 static_cast<QBoxLayout
*>(layout())->addLayout(form
);
101 AddressRowWidget
*senderWidget
= 0;
103 if (!e
.from
.isEmpty()) {
104 senderLabel
= tr("From");
105 senderWidget
= new AddressRowWidget(this, QString(), e
.from
, m_messageView
);
107 if (!e
.sender
.isEmpty() && e
.sender
!= e
.from
) {
109 senderWidget
->addAddresses(QStringLiteral(" %1").arg(tr("sent via")), e
.sender
, m_messageView
);
111 senderLabel
= tr("Sender");
112 senderWidget
= new AddressRowWidget(this, QString(), e
.sender
, m_messageView
);
115 if (!e
.replyTo
.isEmpty() && e
.replyTo
!= e
.from
) {
117 senderWidget
->addAddresses(QStringLiteral(", %1").arg(tr("replies to")), e
.replyTo
, m_messageView
);
119 senderLabel
= tr("Reply-To");
120 senderWidget
= new AddressRowWidget(this, QString(), e
.replyTo
, m_messageView
);
124 ADD_ROW(senderLabel
, senderWidget
)
127 AddressRowWidget
*receiverWidget
= 0;
128 QString receiverLabel
;
129 if (!e
.to
.isEmpty()) {
130 receiverLabel
= tr("To");
131 receiverWidget
= new AddressRowWidget(this, QString(), e
.to
, m_messageView
);
133 if (!e
.cc
.isEmpty()) {
134 if (receiverWidget
) {
135 receiverWidget
->addAddresses(QStringLiteral(" %1").arg(tr("Cc'd to")), e
.cc
, m_messageView
);
137 receiverLabel
= tr("Cc");
138 receiverWidget
= new AddressRowWidget(this, QString(), e
.cc
, m_messageView
);
141 if (!e
.bcc
.isEmpty()) {
142 if (receiverWidget
) {
143 receiverWidget
->addAddresses(QStringLiteral(" %1").arg(tr("Bcc'd to")), e
.bcc
, m_messageView
);
145 receiverLabel
= tr("Bcc");
146 receiverWidget
= new AddressRowWidget(this, QString(), e
.bcc
, m_messageView
);
150 ADD_ROW(receiverLabel
, receiverWidget
)
153 QVariantList headerListPost
= index
.data(Imap::Mailbox::RoleMessageHeaderListPost
).toList();
154 if (!headerListPost
.isEmpty()) {
157 Q_FOREACH(const QVariant
&item
, headerListPost
) {
158 const QString scheme
= item
.toUrl().scheme().toLower();
159 const bool isMailTo
= scheme
== QLatin1String("mailto");
160 if (isMailTo
|| scheme
== QLatin1String("http") || scheme
== QLatin1String("https")) {
161 QString target
= item
.toUrl().toString();
162 // eg. github uses reply+<some hash>@reply.github.com
163 QString caption
= isMailTo
? item
.toUrl().toString(QUrl::RemoveScheme
) : target
;
164 elided
= elided
|| UiUtils::elideAddress(caption
);
165 target
= target
.toHtmlEscaped();
166 buf
<< tr("<a href=\"%1\">%2</a>").arg(target
, caption
.toHtmlEscaped());
168 buf
<< item
.toUrl().toString().toHtmlEscaped();
171 auto lbl
= new QLabel(QString(QLatin1String("<html> %1</html>")).arg(buf
.join(tr(", "))));
172 SET_LABEL_OPTIONS(lbl
)
173 lbl
->setOpenExternalLinks(true);
175 connect(lbl
, &QLabel::linkHovered
, lbl
, &QLabel::setToolTip
);
176 ADD_ROW(tr("Mailing List"), lbl
)
179 // separating the message
180 QFrame
*line
= new QFrame(this);
181 line
->setFrameStyle(QFrame::HLine
|QFrame::Plain
);
182 layout()->addWidget(line
);