File indexing completed on 2025-05-04 04:59:33

0001 /*
0002    SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "searchfullhashgui.h"
0008 
0009 #include <QApplication>
0010 #include <QLabel>
0011 #include <QLineEdit>
0012 #include <QPlainTextEdit>
0013 #include <QPushButton>
0014 #include <QStandardPaths>
0015 #include <QVBoxLayout>
0016 
0017 extern WEBENGINEVIEWER_EXPORT bool webengineview_useCompactJson_SearchFullHashJob;
0018 
0019 SearchFullHashGui::SearchFullHashGui(QWidget *parent)
0020     : QWidget(parent)
0021 {
0022     webengineview_useCompactJson_SearchFullHashJob = false;
0023     auto layout = new QVBoxLayout(this);
0024 
0025     auto checkHashLayout = new QHBoxLayout;
0026     layout->addLayout(checkHashLayout);
0027     auto lab = new QLabel(QStringLiteral("Hash from Url to Check:"), this);
0028     checkHashLayout->addWidget(lab);
0029     mCheckHashLineEdit = new QLineEdit(this);
0030     checkHashLayout->addWidget(mCheckHashLineEdit);
0031 
0032     auto databaseHashLayout = new QHBoxLayout;
0033     layout->addLayout(databaseHashLayout);
0034     lab = new QLabel(QStringLiteral("Database hash:"), this);
0035     checkHashLayout->addWidget(lab);
0036     mDataBaseHashLineEdit = new QLineEdit(this);
0037     checkHashLayout->addWidget(mDataBaseHashLineEdit);
0038 
0039     auto button = new QPushButton(QStringLiteral("Check"), this);
0040     checkHashLayout->addWidget(button);
0041     connect(button, &QPushButton::clicked, this, &SearchFullHashGui::slotCheckUrl);
0042     connect(mCheckHashLineEdit, &QLineEdit::returnPressed, this, &SearchFullHashGui::slotCheckUrl);
0043 
0044     mResult = new QPlainTextEdit(this);
0045     mResult->setReadOnly(true);
0046     layout->addWidget(mResult);
0047 
0048     mJson = new QPlainTextEdit(this);
0049     mJson->setReadOnly(true);
0050     layout->addWidget(mJson);
0051 }
0052 
0053 SearchFullHashGui::~SearchFullHashGui() = default;
0054 
0055 void SearchFullHashGui::slotCheckUrl()
0056 {
0057     const QString hashStr = mCheckHashLineEdit->text().trimmed();
0058     if (hashStr.isEmpty()) {
0059         return;
0060     }
0061     const QString databaseHashStr = mDataBaseHashLineEdit->text().trimmed();
0062     if (databaseHashStr.isEmpty()) {
0063         return;
0064     }
0065 
0066     mResult->clear();
0067     auto job = new WebEngineViewer::SearchFullHashJob(this);
0068     connect(job, &WebEngineViewer::SearchFullHashJob::result, this, &SearchFullHashGui::slotGetResult);
0069     connect(job, &WebEngineViewer::SearchFullHashJob::debugJson, this, &SearchFullHashGui::slotJSonDebug);
0070     job->setDatabaseState(QStringList() << databaseHashStr);
0071     QByteArray ba = hashStr.toLatin1();
0072     QByteArray baShort = ba;
0073     baShort.truncate(4);
0074     QHash<QByteArray, QByteArray> lst;
0075     lst.insert(ba, baShort);
0076     job->setSearchHashs(lst);
0077 
0078     job->start();
0079 }
0080 
0081 void SearchFullHashGui::slotJSonDebug(const QByteArray &debug)
0082 {
0083     mJson->setPlainText(QString::fromLatin1(debug));
0084 }
0085 
0086 void SearchFullHashGui::slotGetResult(WebEngineViewer::CheckPhishingUrlUtil::UrlStatus result)
0087 {
0088     QString resultStr;
0089     switch (result) {
0090     case WebEngineViewer::CheckPhishingUrlUtil::Ok:
0091         resultStr = QStringLiteral("Url ok");
0092         break;
0093     case WebEngineViewer::CheckPhishingUrlUtil::MalWare:
0094         resultStr = QStringLiteral("Url MalWare");
0095         break;
0096     case WebEngineViewer::CheckPhishingUrlUtil::Unknown:
0097         resultStr = QStringLiteral("Url Unknown state");
0098         break;
0099     case WebEngineViewer::CheckPhishingUrlUtil::BrokenNetwork:
0100         resultStr = QStringLiteral("Broken Network");
0101         break;
0102     case WebEngineViewer::CheckPhishingUrlUtil::InvalidUrl:
0103         resultStr = QStringLiteral("Invalid Url");
0104         break;
0105     }
0106     mResult->setPlainText(resultStr);
0107 }
0108 
0109 int main(int argc, char **argv)
0110 {
0111     QApplication app(argc, argv);
0112     QStandardPaths::setTestModeEnabled(true);
0113     auto w = new SearchFullHashGui;
0114 
0115     w->show();
0116     app.exec();
0117     delete w;
0118     return 0;
0119 }
0120 
0121 #include "moc_searchfullhashgui.cpp"