File indexing completed on 2024-05-19 05:54:10

0001 /*
0002     SPDX-FileCopyrightText: 2007-2008 Robert Knight <robertknight@gmail.com>
0003     SPDX-FileCopyrightText: 2020 Tomaz Canabrava <tcanabrava@gmail.com>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "UrlFilterHotspot.h"
0009 
0010 #include <QAction>
0011 #include <QApplication>
0012 #include <QClipboard>
0013 
0014 #include <KIO/OpenUrlJob>
0015 #include <KLocalizedString>
0016 #include <QIcon>
0017 
0018 #include "UrlFilter.h"
0019 #include "terminalDisplay/TerminalDisplay.h"
0020 // regexp matches:
0021 // full url:
0022 
0023 using namespace Konsole;
0024 
0025 UrlFilterHotSpot::~UrlFilterHotSpot() = default;
0026 
0027 UrlFilterHotSpot::UrlFilterHotSpot(int startLine, int startColumn, int endLine, int endColumn, const QStringList &capturedTexts)
0028     : RegExpFilterHotSpot(startLine, startColumn, endLine, endColumn, capturedTexts)
0029 {
0030     const UrlType kind = urlType();
0031     if (kind == Email) {
0032         setType(EMailAddress);
0033     } else {
0034         setType(Link);
0035     }
0036 }
0037 
0038 UrlFilterHotSpot::UrlType UrlFilterHotSpot::urlType() const
0039 {
0040     const QString url = capturedTexts().at(0);
0041 
0042     // Don't use a ternary here, it gets completely unreadable
0043     if (UrlFilter::FullUrlRegExp.match(url).hasMatch()) {
0044         return StandardUrl;
0045     } else if (UrlFilter::EmailAddressRegExp.match(url).hasMatch()) {
0046         return Email;
0047     } else {
0048         return Unknown;
0049     }
0050 }
0051 
0052 void UrlFilterHotSpot::activate(QObject *object)
0053 {
0054     QString url = capturedTexts().at(0);
0055 
0056     const UrlType kind = urlType();
0057 
0058     const QString &actionName = object != nullptr ? object->objectName() : QString();
0059 
0060     if (actionName == QLatin1String("copy-action")) {
0061         QApplication::clipboard()->setText(url);
0062         return;
0063     }
0064 
0065     if ((object == nullptr) || actionName == QLatin1String("open-action")) {
0066         if (kind == StandardUrl) {
0067             // if the URL path does not include the protocol ( eg. "www.kde.org" ) then
0068             // prepend https:// ( eg. "www.kde.org" --> "https://www.kde.org" )
0069             if (!url.contains(QLatin1String("://"))) {
0070                 url.prepend(QLatin1String("https://"));
0071             }
0072         } else if (kind == Email) {
0073             url.prepend(QLatin1String("mailto:"));
0074         }
0075 
0076         auto *job = new KIO::OpenUrlJob(QUrl(url));
0077         job->start();
0078     }
0079 }
0080 
0081 QList<QAction *> UrlFilterHotSpot::actions()
0082 {
0083     auto openAction = new QAction(this);
0084     auto copyAction = new QAction(this);
0085 
0086     const UrlType kind = urlType();
0087     Q_ASSERT(kind == StandardUrl || kind == Email);
0088 
0089     if (kind == StandardUrl) {
0090         openAction->setText(i18n("Open Link"));
0091         openAction->setIcon(QIcon::fromTheme(QStringLiteral("internet-services")));
0092         copyAction->setText(i18n("Copy Link Address"));
0093         copyAction->setIcon(QIcon::fromTheme(QStringLiteral("edit-copy-url")));
0094     } else if (kind == Email) {
0095         openAction->setText(i18n("Send Email To..."));
0096         openAction->setIcon(QIcon::fromTheme(QStringLiteral("mail-send")));
0097         copyAction->setText(i18n("Copy Email Address"));
0098         copyAction->setIcon(QIcon::fromTheme(QStringLiteral("edit-copy-mail")));
0099     }
0100 
0101     // object names are set here so that the hotspot performs the
0102     // correct action when activated() is called with the triggered
0103     // action passed as a parameter.
0104     openAction->setObjectName(QStringLiteral("open-action"));
0105     copyAction->setObjectName(QStringLiteral("copy-action"));
0106 
0107     QObject::connect(openAction, &QAction::triggered, this, [this, openAction] {
0108         activate(openAction);
0109     });
0110     QObject::connect(copyAction, &QAction::triggered, this, [this, copyAction] {
0111         activate(copyAction);
0112     });
0113 
0114     return {openAction, copyAction};
0115 }