Messagelib

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

KDE's Doxygen guidelines are available online.