1 /* Copyright (C) 2006 - 2015 Jan Kundrát <jkt@kde.org>
2 Copyright (C) 2013 - 2015 Pali Rohár <pali.rohar@gmail.com>
4 This file is part of the Trojita Qt IMAP e-mail client,
5 http://trojita.flaska.net/
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of
10 the License or (at your option) version 3 or any later version
11 accepted by the membership of KDE e.V. (or its successor approved
12 by the membership of KDE e.V.), which shall act as a proxy
13 defined in Section 14 of version 3 of the license.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include <QContextMenuEvent>
26 #include <QDesktopServices>
27 #include <QFontMetrics>
28 #include <QGuiApplication>
29 #include <QHeaderView>
32 #include "Composer/Mailto.h"
33 #include "Gui/MessageView.h"
34 #include "Gui/OneEnvelopeAddress.h"
36 #include "Plugins/PluginManager.h"
37 #include "Plugins/AddressbookPlugin.h"
38 #include "UiUtils/IconLoader.h"
42 OneEnvelopeAddress::OneEnvelopeAddress(QWidget
*parent
, const Imap::Message::MailAddress
&address
, MessageView
*messageView
, const Position lastOneInRow
):
43 QLabel(parent
), m_address(address
), m_lastOneInRow(lastOneInRow
), m_pluginManager(messageView
->pluginManager())
45 setTextInteractionFlags(Qt::TextSelectableByMouse
| Qt::LinksAccessibleByMouse
);
47 connect(this, &QLabel::linkHovered
, this, &OneEnvelopeAddress::onLinkHovered
);
49 QFontMetrics
fm(font());
50 int iconSize
= fm
.boundingRect(QLatin1Char('M')).height();
51 contactKnownUrl
= Gui::Util::resizedImageAsDataUrl(QStringLiteral(":/icons/contact-known.svg"), iconSize
);
52 contactUnknownUrl
= Gui::Util::resizedImageAsDataUrl(QStringLiteral(":/icons/contact-unknown.svg"), iconSize
);
54 connect(this, &QLabel::linkActivated
, [](const QString
&link
) {
55 // Trojita is registered to handle any mailto: URL
56 QDesktopServices::openUrl(QUrl(link
));
62 void OneEnvelopeAddress::processAddress()
64 // show fast version without data from addressbook now
65 finishProcessAddress(QStringList());
67 // and later if there is an addressbook and all addressbook jobs have finished, show the full version
68 Plugins::AddressbookPlugin
*addressbook
= m_pluginManager
->addressbook();
69 if (!addressbook
|| !(addressbook
->features() & Plugins::AddressbookPlugin::FeaturePrettyNames
))
72 auto job
= addressbook
->requestPrettyNamesForAddress(m_address
.mailbox
+ QLatin1Char('@') + m_address
.host
);
76 connect(job
, &Plugins::AddressbookNamesJob::prettyNamesForAddressAvailable
, this, &OneEnvelopeAddress::finishProcessAddress
);
77 job
->setAutoDelete(true);
81 void OneEnvelopeAddress::finishProcessAddress(const QStringList
&matchingDisplayNames
)
83 QUrl url
= m_address
.asUrl();
84 QString icon
= QString::fromUtf8("<span style=\"width: 4px;\"> </span><a href=\"x-trojita-manage-contact:%1\">"
85 "<img src=\"%2\" align=\"center\"/></a>").arg(
86 url
.toString(QUrl::RemoveScheme
),
87 matchingDisplayNames
.isEmpty() ? contactUnknownUrl
: contactKnownUrl
);
88 QString address
= m_address
.prettyName(Imap::Message::MailAddress::FORMAT_SHORT_CLICKABLE
);
89 setText(address
+ icon
+ (m_lastOneInRow
== Position::Middle
? tr(",") : QString()));
92 void OneEnvelopeAddress::onLinkHovered(const QString
&target
)
96 Imap::Message::MailAddress addr
;
97 if (!Imap::Message::MailAddress::fromUrl(addr
, url
, QStringLiteral("mailto")) &&
98 !Imap::Message::MailAddress::fromUrl(addr
, url
, QStringLiteral("x-trojita-manage-contact"))) {
99 setToolTip(QString());
103 // show fast version without data from addressbook now
104 m_link
= addr
.prettyName(Imap::Message::MailAddress::FORMAT_READABLE
).toHtmlEscaped();
107 // and later if there is an addressbook and all addressbook jobs have finished, show the full version
108 Plugins::AddressbookPlugin
*addressbook
= m_pluginManager
->addressbook();
109 if (!addressbook
|| !(addressbook
->features() & Plugins::AddressbookPlugin::FeaturePrettyNames
))
112 auto job
= addressbook
->requestPrettyNamesForAddress(addr
.mailbox
+ QLatin1Char('@') + addr
.host
);
116 connect(job
, &Plugins::AddressbookNamesJob::prettyNamesForAddressAvailable
, this, &OneEnvelopeAddress::finishOnLinkHovered
);
117 job
->setAutoDelete(true);
121 void OneEnvelopeAddress::finishOnLinkHovered(const QStringList
&matchingDisplayNames
)
123 if (matchingDisplayNames
.isEmpty())
126 // Addressbook can contain empty display name for email address
127 // Instead empty line show something better in tooltip
128 QStringList matchingIdentities
;
129 Q_FOREACH(const QString
&str
, matchingDisplayNames
) {
130 matchingIdentities
<< (str
.isEmpty() ? tr("(empty display name)") : str
.toHtmlEscaped());
133 QString identities
= matchingIdentities
.join(QStringLiteral("<br/>\n"));
134 setToolTip(m_link
+ tr("<hr/><b>Address Book:</b><br/>") + identities
);
137 void OneEnvelopeAddress::contextMenuEvent(QContextMenuEvent
*e
)
139 QScopedPointer
<QMenu
> menu(new QMenu(this));
140 // NOTE: when QT_VERSION >= QT_VERSION_CHECK(5,6,0), we can get rid of this verbose code and just call menu->addAction(...)
141 auto a
= new QAction(UiUtils::loadIcon(QStringLiteral("edit-copy")), tr("Copy e-mail address"), menu
.data());
142 connect(a
, &QAction::triggered
, this, [this](){
143 QGuiApplication::clipboard()->setText(Composer::extractOneMailAddress(m_address
.asUrl()));
146 a
= new QAction(tr("Copy text"), this);
147 connect(a
, &QAction::triggered
, this, [this]() {
148 QGuiApplication::clipboard()->setText(m_address
.prettyName(Imap::Message::MailAddress::FORMAT_JUST_NAME
));
151 menu
->exec(e
->globalPos());