Merge "persistent color scheme selection"
[trojita.git] / src / Gui / OneEnvelopeAddress.cpp
blobf0445ed4b9e979caad67f689b25b7e62309cdf6d
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/>.
24 #include <QClipboard>
25 #include <QContextMenuEvent>
26 #include <QDesktopServices>
27 #include <QFontMetrics>
28 #include <QGuiApplication>
29 #include <QHeaderView>
30 #include <QMenu>
31 #include <QUrlQuery>
32 #include "Composer/Mailto.h"
33 #include "Gui/MessageView.h"
34 #include "Gui/OneEnvelopeAddress.h"
35 #include "Gui/Util.h"
36 #include "Plugins/PluginManager.h"
37 #include "Plugins/AddressbookPlugin.h"
38 #include "UiUtils/IconLoader.h"
40 namespace Gui {
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);
46 setIndent(5);
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));
57 });
59 processAddress();
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))
70 return;
72 auto job = addressbook->requestPrettyNamesForAddress(m_address.mailbox + QLatin1Char('@') + m_address.host);
73 if (!job)
74 return;
76 connect(job, &Plugins::AddressbookNamesJob::prettyNamesForAddressAvailable, this, &OneEnvelopeAddress::finishProcessAddress);
77 job->setAutoDelete(true);
78 job->start();
81 void OneEnvelopeAddress::finishProcessAddress(const QStringList &matchingDisplayNames)
83 QUrl url = m_address.asUrl();
84 QString icon = QString::fromUtf8("<span style=\"width: 4px;\">&nbsp;</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)
94 QUrl url(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());
100 return;
103 // show fast version without data from addressbook now
104 m_link = addr.prettyName(Imap::Message::MailAddress::FORMAT_READABLE).toHtmlEscaped();
105 setToolTip(m_link);
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))
110 return;
112 auto job = addressbook->requestPrettyNamesForAddress(addr.mailbox + QLatin1Char('@') + addr.host);
113 if (!job)
114 return;
116 connect(job, &Plugins::AddressbookNamesJob::prettyNamesForAddressAvailable, this, &OneEnvelopeAddress::finishOnLinkHovered);
117 job->setAutoDelete(true);
118 job->start();
121 void OneEnvelopeAddress::finishOnLinkHovered(const QStringList &matchingDisplayNames)
123 if (matchingDisplayNames.isEmpty())
124 return;
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()));
145 menu->addAction(a);
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));
150 menu->addAction(a);
151 menu->exec(e->globalPos());