Messagelib

scamcheckshorturl.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2023 Laurent Montel <[email protected]>
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 
17 using namespace MessageViewer;
18 QStringList ScamCheckShortUrl::sSupportedServices = QStringList();
19 
20 ScamCheckShortUrl::ScamCheckShortUrl(QObject *parent)
21  : QObject(parent)
22 {
23  loadLongUrlServices();
24 }
25 
26 ScamCheckShortUrl::~ScamCheckShortUrl() = default;
27 
28 void ScamCheckShortUrl::expandedUrl(const QUrl &url)
29 {
30  auto job = new MessageViewer::ScamExpandUrlJob(this);
31  job->expandedUrl(url);
32 }
33 
34 bool 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 
43 void 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 }
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
bool isNull() const const
QVariant toVariant() const const
QString locate(QStandardPaths::StandardLocation type, const QString &fileName, QStandardPaths::LocateOptions options)
bool isEmpty() const const
The ScamExpandUrlJob class.
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
QMap< QString, QVariant > toMap() const const
QString host(QUrl::ComponentFormattingOptions options) const const
QString path(QUrl::ComponentFormattingOptions options) const const
QList< Key > keys() const const
int compare(const QString &other, Qt::CaseSensitivity cs) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Fri Mar 24 2023 04:08:32 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.