1 /*
2   SPDX-FileCopyrightText: 2016-2021 Laurent Montel <montel@kde.org>
3 
4   SPDX-License-Identifier: LGPL-2.0-or-later
5 
6 */
7 
8 #include "scamexpandurljob.h"
9 #include "messageviewer_debug.h"
10 
11 #include <PimCommon/BroadcastStatus>
12 #include <PimCommon/NetworkManager>
13 
14 #include <KLocalizedString>
15 
16 #include <QJsonDocument>
17 #include <QNetworkAccessManager>
18 #include <QNetworkConfigurationManager>
19 
20 using namespace MessageViewer;
21 class MessageViewer::ScamExpandUrlJobPrivate
22 {
23 public:
ScamExpandUrlJobPrivate()24     ScamExpandUrlJobPrivate()
25     {
26     }
27 
~ScamExpandUrlJobPrivate()28     ~ScamExpandUrlJobPrivate()
29     {
30     }
31 
32     QNetworkAccessManager *mNetworkAccessManager = nullptr;
33 };
34 
ScamExpandUrlJob(QObject * parent)35 ScamExpandUrlJob::ScamExpandUrlJob(QObject *parent)
36     : QObject(parent)
37     , d(new ScamExpandUrlJobPrivate)
38 {
39     d->mNetworkAccessManager = new QNetworkAccessManager(this);
40     d->mNetworkAccessManager->setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy);
41     d->mNetworkAccessManager->setStrictTransportSecurityEnabled(true);
42     d->mNetworkAccessManager->enableStrictTransportSecurityStore(true);
43 
44     connect(d->mNetworkAccessManager, &QNetworkAccessManager::finished, this, &ScamExpandUrlJob::slotExpandFinished);
45 }
46 
47 ScamExpandUrlJob::~ScamExpandUrlJob() = default;
48 
expandedUrl(const QUrl & url)49 void ScamExpandUrlJob::expandedUrl(const QUrl &url)
50 {
51     if (!PimCommon::NetworkManager::self()->networkConfigureManager()->isOnline()) {
52         PimCommon::BroadcastStatus::instance()->setStatusMsg(i18n("No network connection detected, we cannot expand url."));
53         deleteLater();
54         return;
55     }
56     const QUrl newUrl(QStringLiteral("https://lengthenurl.info/api/longurl/shorturl/?inputURL=%1&format=json").arg(url.url()));
57 
58     qCDebug(MESSAGEVIEWER_LOG) << " newUrl " << newUrl;
59     QNetworkReply *reply = d->mNetworkAccessManager->get(QNetworkRequest(newUrl));
60     reply->setProperty("shortUrl", url.url());
61     connect(reply, qOverload<QNetworkReply::NetworkError>(&QNetworkReply::errorOccurred), this, &ScamExpandUrlJob::slotError);
62 }
63 
slotExpandFinished(QNetworkReply * reply)64 void ScamExpandUrlJob::slotExpandFinished(QNetworkReply *reply)
65 {
66     QUrl shortUrl;
67     if (!reply->property("shortUrl").isNull()) {
68         shortUrl.setUrl(reply->property("shortUrl").toString());
69     }
70     const QByteArray ba = reply->readAll();
71     // qDebug() << " reply->readAll()" << ba;
72     QJsonDocument jsonDoc = QJsonDocument::fromJson(ba);
73     reply->deleteLater();
74     if (!jsonDoc.isNull()) {
75         const QMap<QString, QVariant> map = jsonDoc.toVariant().toMap();
76         QUrl longUrl;
77         const QVariant longUrlVar = map.value(QStringLiteral("LongURL"));
78         if (longUrlVar.isValid()) {
79             longUrl.setUrl(longUrlVar.toString());
80         } else {
81             qCWarning(MESSAGEVIEWER_LOG) << "JSon is not correct" << ba;
82             PimCommon::BroadcastStatus::instance()->setStatusMsg(i18n("Impossible to expand \'%1\'.", shortUrl.url()));
83             deleteLater();
84             return;
85         }
86         PimCommon::BroadcastStatus::instance()->setStatusMsg(i18n("Short url \'%1\' redirects to \'%2\'.", shortUrl.url(), longUrl.toDisplayString()));
87     } else {
88         PimCommon::BroadcastStatus::instance()->setStatusMsg(i18n("Impossible to expand \'%1\'.", shortUrl.url()));
89     }
90     deleteLater();
91 }
92 
slotError(QNetworkReply::NetworkError error)93 void ScamExpandUrlJob::slotError(QNetworkReply::NetworkError error)
94 {
95     Q_EMIT expandUrlError(error);
96     deleteLater();
97 }
98