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 <QHeaderView>
24 #include <QFontMetrics>
25 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
26 # include <QTextDocument>
30 #include "MessageView.h"
32 #include "Imap/Model/ItemRoles.h"
33 #include "Imap/Model/MailboxTree.h"
34 #include "Imap/Model/Model.h"
38 EnvelopeView::EnvelopeView(QWidget
*parent
, MessageView
*messageView
): QLabel(parent
)
40 // we create a dummy header, pass it through the style and the use it's color roles so we
41 // know what headers in general look like in the system
42 QHeaderView
helpingHeader(Qt::Horizontal
);
43 helpingHeader
.ensurePolished();
45 setBackgroundRole(helpingHeader
.backgroundRole());
46 setForegroundRole(helpingHeader
.foregroundRole());
47 setTextInteractionFlags(Qt::TextSelectableByMouse
| Qt::LinksAccessibleByMouse
);
50 connect(this, SIGNAL(linkHovered(QString
)), this, SLOT(onLinkHovered(QString
)));
52 QFontMetrics
fm(font());
53 int iconSize
= fm
.boundingRect(QLatin1Char('M')).height();
54 contactKnownUrl
= Gui::Util::resizedImageAsDataUrl(QLatin1String(":/icons/contact-known.svg"), iconSize
);
55 contactUnknownUrl
= Gui::Util::resizedImageAsDataUrl(QLatin1String(":/icons/contact-unknown.svgz"), iconSize
);
57 connect(this, SIGNAL(linkActivated(QString
)), messageView
, SLOT(headerLinkActivated(QString
)));
58 connect(this, SIGNAL(addressDetailsRequested(QString
,QStringList
&)),
59 messageView
, SIGNAL(addressDetailsRequested(QString
,QStringList
&)));
63 void EnvelopeView::setMessage(const QModelIndex
&index
)
65 setText(headerText(index
));
68 /** @short Return a HTML representation of the address list */
69 QString
EnvelopeView::htmlizeAddresses(const QList
<Imap::Message::MailAddress
> &addresses
)
71 Q_ASSERT(!addresses
.isEmpty());
73 Q_FOREACH(const Imap::Message::MailAddress
&addr
, addresses
) {
74 QStringList matchingDisplayNames
;
75 emit
addressDetailsRequested(addr
.mailbox
+ QLatin1Char('@') + addr
.host
, matchingDisplayNames
);
76 QUrl url
= addr
.asUrl();
77 QString icon
= QString::fromUtf8("<span style='width: 4px;'> </span><a href='x-trojita-manage-contact:%1'>"
78 "<img src='%2' align='center'/></a>").arg(
79 url
.toString(QUrl::RemoveScheme
),
80 matchingDisplayNames
.isEmpty() ? contactUnknownUrl
: contactKnownUrl
);
81 buf
<< addr
.prettyName(Imap::Message::MailAddress::FORMAT_CLICKABLE
) + icon
;
83 return buf
.join(QLatin1String(", "));
86 QString
EnvelopeView::headerText(const QModelIndex
&index
)
91 const Imap::Message::Envelope e
= index
.data(Imap::Mailbox::RoleMessageEnvelope
).value
<Imap::Message::Envelope
>();
94 if (!e
.from
.isEmpty())
95 res
+= tr("<b>From:</b> %1<br/>").arg(htmlizeAddresses(e
.from
));
96 if (!e
.sender
.isEmpty() && e
.sender
!= e
.from
)
97 res
+= tr("<b>Sender:</b> %1<br/>").arg(htmlizeAddresses(e
.sender
));
98 if (!e
.replyTo
.isEmpty() && e
.replyTo
!= e
.from
)
99 res
+= tr("<b>Reply-To:</b> %1<br/>").arg(htmlizeAddresses(e
.replyTo
));
100 QVariantList headerListPost
= index
.data(Imap::Mailbox::RoleMessageHeaderListPost
).toList();
101 if (!headerListPost
.isEmpty()) {
103 Q_FOREACH(const QVariant
&item
, headerListPost
) {
104 const QString scheme
= item
.toUrl().scheme().toLower();
105 if (scheme
== QLatin1String("http") || scheme
== QLatin1String("https") || scheme
== QLatin1String("mailto")) {
106 QString target
= item
.toUrl().toString();
107 QString caption
= item
.toUrl().toString(scheme
== QLatin1String("mailto") ? QUrl::RemoveScheme
: QUrl::None
);
108 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
109 target
= Qt::escape(target
);
110 caption
= Qt::escape(caption
);
112 target
= target
.toHtmlEscaped();
113 caption
= caption
.toHtmlEscaped();
115 buf
<< tr("<a href=\"%1\">%2</a>").arg(target
, caption
);
117 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
118 buf
<< Qt::escape(item
.toUrl().toString());
120 buf
<< item
.toUrl().toString().toHtmlEscaped();
124 res
+= tr("<b>List-Post:</b> %1<br/>").arg(buf
.join(tr(", ")));
127 res
+= tr("<b>To:</b> %1<br/>").arg(htmlizeAddresses(e
.to
));
129 res
+= tr("<b>Cc:</b> %1<br/>").arg(htmlizeAddresses(e
.cc
));
130 if (!e
.bcc
.isEmpty())
131 res
+= tr("<b>Bcc:</b> %1<br/>").arg(htmlizeAddresses(e
.bcc
));
132 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
133 res
+= tr("<b>Subject:</b> %1").arg(Qt::escape(e
.subject
));
135 res
+= tr("<b>Subject:</b> %1").arg(e
.subject
.toHtmlEscaped());
137 if (e
.date
.isValid())
138 res
+= tr("<br/><b>Date:</b> %1").arg(e
.date
.toLocalTime().toString(Qt::SystemLocaleLongDate
));
142 void EnvelopeView::onLinkHovered(const QString
&target
)
146 Imap::Message::MailAddress addr
;
147 if (Imap::Message::MailAddress::fromUrl(addr
, url
, QLatin1String("mailto")) ||
148 Imap::Message::MailAddress::fromUrl(addr
, url
, QLatin1String("x-trojita-manage-contact"))) {
149 QStringList matchingIdentities
;
150 emit
addressDetailsRequested(addr
.mailbox
+ QLatin1Char('@') + addr
.host
, matchingIdentities
);
151 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
152 QString link
= Qt::escape(addr
.prettyName(Imap::Message::MailAddress::FORMAT_READABLE
));
153 QString identities
= Qt::escape(matchingIdentities
.join(QLatin1String("<br/>\n")));
155 QString link
= addr
.prettyName(Imap::Message::MailAddress::FORMAT_READABLE
).toHtmlEscaped();
156 QString identities
= matchingIdentities
.join(QLatin1String("<br/>\n")).toHtmlEscaped();
158 if (matchingIdentities
.isEmpty()) {
161 setToolTip(link
+ tr("<hr/><b>Address Book:</b><br/>") + identities
);
164 setToolTip(QString());