Messagelib

scamcheckshorturl.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 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)
QString host(QUrl::ComponentFormattingOptions options) const const
bool isNull() const const
QList< Key > keys() const const
bool isEmpty() const const
void error(QWidget *parent, const QString &text, const QString &caption=QString(), Options options=Notify)
QString path(QUrl::ComponentFormattingOptions options) const const
QVariant toVariant() const const
QMap< QString, QVariant > toMap() const const
The ScamExpandUrlJob class.
int compare(const QString &other, Qt::CaseSensitivity cs) const const
QString locate(QStandardPaths::StandardLocation type, const QString &fileName, QStandardPaths::LocateOptions options)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Dec 4 2021 23:12:54 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.