Messagelib

scamexpandurljob.cpp
1/*
2 SPDX-FileCopyrightText: 2016-2024 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
19using namespace MessageViewer;
20class MessageViewer::ScamExpandUrlJobPrivate
21{
22public:
23 ScamExpandUrlJobPrivate() = default;
24
25 ~ScamExpandUrlJobPrivate() = default;
26
27 QNetworkAccessManager *mNetworkAccessManager = nullptr;
28};
29
30ScamExpandUrlJob::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
42ScamExpandUrlJob::~ScamExpandUrlJob() = default;
43
44void 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."));
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, &QNetworkReply::errorOccurred, this, &ScamExpandUrlJob::slotError);
57}
58
59void ScamExpandUrlJob::slotExpandFinished(QNetworkReply *reply)
60{
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();
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()));
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 }
86}
87
88void ScamExpandUrlJob::slotError(QNetworkReply::NetworkError error)
89{
90 Q_EMIT expandUrlError(error);
92}
93
94#include "moc_scamexpandurljob.cpp"
QString i18n(const char *text, const TYPE &arg...)
QByteArray readAll()
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
void finished(QNetworkReply *reply)
void errorOccurred(QNetworkReply::NetworkError code)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
QVariant property(const char *name) const const
T qobject_cast(QObject *object)
bool setProperty(const char *name, QVariant &&value)
QFuture< void > map(Iterator begin, Iterator end, MapFunctor &&function)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QString url(FormattingOptions options) const const
bool isNull() const const
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 24 2024 11:55:44 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.