Messagelib

scamexpandurljob.cpp
1 /*
2  SPDX-FileCopyrightText: 2016-2021 Laurent Montel <[email protected]>
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:
24  ScamExpandUrlJobPrivate() = default;
25 
26  ~ScamExpandUrlJobPrivate() = default;
27 
28  QNetworkAccessManager *mNetworkAccessManager = nullptr;
29 };
30 
31 ScamExpandUrlJob::ScamExpandUrlJob(QObject *parent)
32  : QObject(parent)
33  , d(new ScamExpandUrlJobPrivate)
34 {
35  d->mNetworkAccessManager = new QNetworkAccessManager(this);
36  d->mNetworkAccessManager->setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy);
37  d->mNetworkAccessManager->setStrictTransportSecurityEnabled(true);
38  d->mNetworkAccessManager->enableStrictTransportSecurityStore(true);
39 
40  connect(d->mNetworkAccessManager, &QNetworkAccessManager::finished, this, &ScamExpandUrlJob::slotExpandFinished);
41 }
42 
43 ScamExpandUrlJob::~ScamExpandUrlJob() = default;
44 
45 void ScamExpandUrlJob::expandedUrl(const QUrl &url)
46 {
47  if (!PimCommon::NetworkManager::self()->networkConfigureManager()->isOnline()) {
48  PimCommon::BroadcastStatus::instance()->setStatusMsg(i18n("No network connection detected, we cannot expand url."));
49  deleteLater();
50  return;
51  }
52  const QUrl newUrl(QStringLiteral("https://lengthenurl.info/api/longurl/shorturl/?inputURL=%1&format=json").arg(url.url()));
53 
54  qCDebug(MESSAGEVIEWER_LOG) << " newUrl " << newUrl;
55  QNetworkReply *reply = d->mNetworkAccessManager->get(QNetworkRequest(newUrl));
56  reply->setProperty("shortUrl", url.url());
57  connect(reply, qOverload<QNetworkReply::NetworkError>(&QNetworkReply::errorOccurred), this, &ScamExpandUrlJob::slotError);
58 }
59 
60 void ScamExpandUrlJob::slotExpandFinished(QNetworkReply *reply)
61 {
62  QUrl shortUrl;
63  if (!reply->property("shortUrl").isNull()) {
64  shortUrl.setUrl(reply->property("shortUrl").toString());
65  }
66  const QByteArray ba = reply->readAll();
67  // qDebug() << " reply->readAll()" << ba;
69  reply->deleteLater();
70  if (!jsonDoc.isNull()) {
71  const QMap<QString, QVariant> map = jsonDoc.toVariant().toMap();
72  QUrl longUrl;
73  const QVariant longUrlVar = map.value(QStringLiteral("LongURL"));
74  if (longUrlVar.isValid()) {
75  longUrl.setUrl(longUrlVar.toString());
76  } else {
77  qCWarning(MESSAGEVIEWER_LOG) << "JSon is not correct" << ba;
78  PimCommon::BroadcastStatus::instance()->setStatusMsg(i18n("Impossible to expand \'%1\'.", shortUrl.url()));
79  deleteLater();
80  return;
81  }
82  PimCommon::BroadcastStatus::instance()->setStatusMsg(i18n("Short url \'%1\' redirects to \'%2\'.", shortUrl.url(), longUrl.toDisplayString()));
83  } else {
84  PimCommon::BroadcastStatus::instance()->setStatusMsg(i18n("Impossible to expand \'%1\'.", shortUrl.url()));
85  }
86  deleteLater();
87 }
88 
89 void ScamExpandUrlJob::slotError(QNetworkReply::NetworkError error)
90 {
91  Q_EMIT expandUrlError(error);
92  deleteLater();
93 }
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
QString url(QUrl::FormattingOptions options) const const
QString toDisplayString(QUrl::FormattingOptions options) const const
bool isNull() const const
QVariant property(const char *name) const const
bool isNull() const const
void setUrl(const QString &url, QUrl::ParsingMode parsingMode)
QByteArray readAll()
void deleteLater()
QVariant toVariant() const const
void errorOccurred(QNetworkReply::NetworkError code)
QString i18n(const char *text, const TYPE &arg...)
QMap< QString, QVariant > toMap() const const
void finished(QNetworkReply *reply)
bool isValid() const const
bool setProperty(const char *name, const QVariant &value)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString toString() const const
QFuture< void > map(Sequence &sequence, MapFunctor function)
Q_EMITQ_EMIT
const T value(const Key &key, const T &defaultValue) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sun Dec 5 2021 23:04:55 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.