Messagelib

scamcheckshorturl.cpp
1/*
2 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5
6*/
7
8#include "scamcheckshorturl.h"
9#include "messageviewer_debug.h"
10#include "scamexpandurljob.h"
11
12#include <QFile>
13
14#include <QJsonDocument>
15#include <QStandardPaths>
16
17using namespace MessageViewer;
18QStringList ScamCheckShortUrl::sSupportedServices = QStringList();
19
20ScamCheckShortUrl::ScamCheckShortUrl(QObject *parent)
21 : QObject(parent)
22{
23 loadLongUrlServices();
24}
25
26ScamCheckShortUrl::~ScamCheckShortUrl() = default;
27
28void ScamCheckShortUrl::expandedUrl(const QUrl &url)
29{
30 auto job = new MessageViewer::ScamExpandUrlJob(this);
31 job->expandedUrl(url);
32}
33
34bool ScamCheckShortUrl::isShortUrl(const QUrl &url)
35{
36 if (!url.path().isEmpty() && QString::compare(url.path(), QStringLiteral("/")) && sSupportedServices.contains(url.host())) {
37 return true;
38 } else {
39 return false;
40 }
41}
42
43void ScamCheckShortUrl::loadLongUrlServices()
44{
45 QFile servicesFile(QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("messageviewer/longurlServices.json")));
46 if (servicesFile.open(QIODevice::ReadOnly)) {
48 const QJsonDocument json = QJsonDocument::fromJson(servicesFile.readAll(), &error);
49 if (error.error != QJsonParseError::NoError || json.isNull()) {
50 qCDebug(MESSAGEVIEWER_LOG) << " Error during read longurlServices.json";
51 return;
52 }
53 const QMap<QString, QVariant> response = json.toVariant().toMap();
54 sSupportedServices = response.keys();
55 } else {
56 qCDebug(MESSAGEVIEWER_LOG) << " json file \'longurlServices.json\' not found";
57 }
58}
59
60#include "moc_scamcheckshorturl.cpp"
The ScamExpandUrlJob class.
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
bool isNull() const const
QVariant toVariant() const const
QList< Key > keys() const const
QString locate(StandardLocation type, const QString &fileName, LocateOptions options)
int compare(QLatin1StringView s1, const QString &s2, Qt::CaseSensitivity cs)
bool isEmpty() const const
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
QString host(ComponentFormattingOptions options) const const
QString path(ComponentFormattingOptions options) const const
QMap< QString, QVariant > toMap() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:05:10 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.