File indexing completed on 2025-03-09 04:54:38

0001 /*
0002   SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: LGPL-2.0-or-later
0005 
0006 */
0007 
0008 #include "scamcheckshorturl.h"
0009 #include "messageviewer_debug.h"
0010 #include "scamexpandurljob.h"
0011 
0012 #include <QFile>
0013 
0014 #include <QJsonDocument>
0015 #include <QStandardPaths>
0016 
0017 using namespace MessageViewer;
0018 QStringList ScamCheckShortUrl::sSupportedServices = QStringList();
0019 
0020 ScamCheckShortUrl::ScamCheckShortUrl(QObject *parent)
0021     : QObject(parent)
0022 {
0023     loadLongUrlServices();
0024 }
0025 
0026 ScamCheckShortUrl::~ScamCheckShortUrl() = default;
0027 
0028 void ScamCheckShortUrl::expandedUrl(const QUrl &url)
0029 {
0030     auto job = new MessageViewer::ScamExpandUrlJob(this);
0031     job->expandedUrl(url);
0032 }
0033 
0034 bool ScamCheckShortUrl::isShortUrl(const QUrl &url)
0035 {
0036     if (!url.path().isEmpty() && QString::compare(url.path(), QStringLiteral("/")) && sSupportedServices.contains(url.host())) {
0037         return true;
0038     } else {
0039         return false;
0040     }
0041 }
0042 
0043 void ScamCheckShortUrl::loadLongUrlServices()
0044 {
0045     QFile servicesFile(QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("messageviewer/longurlServices.json")));
0046     if (servicesFile.open(QIODevice::ReadOnly)) {
0047         QJsonParseError error;
0048         const QJsonDocument json = QJsonDocument::fromJson(servicesFile.readAll(), &error);
0049         if (error.error != QJsonParseError::NoError || json.isNull()) {
0050             qCDebug(MESSAGEVIEWER_LOG) << " Error during read longurlServices.json";
0051             return;
0052         }
0053         const QMap<QString, QVariant> response = json.toVariant().toMap();
0054         sSupportedServices = response.keys();
0055     } else {
0056         qCDebug(MESSAGEVIEWER_LOG) << " json file \'longurlServices.json\' not found";
0057     }
0058 }
0059 
0060 #include "moc_scamcheckshorturl.cpp"