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 "createphishingurldatabasegui.h"
0008 
0009 #include <QApplication>
0010 #include <QComboBox>
0011 #include <QFileDialog>
0012 #include <QInputDialog>
0013 #include <QPlainTextEdit>
0014 #include <QPushButton>
0015 #include <QStandardPaths>
0016 #include <QVBoxLayout>
0017 
0018 extern WEBENGINEVIEWER_EXPORT bool webengineview_useCompactJson_CreatePhishingUrlDataBaseJob;
0019 
0020 CreatePhisingUrlDataBaseGui::CreatePhisingUrlDataBaseGui(QWidget *parent)
0021     : QWidget(parent)
0022 {
0023     webengineview_useCompactJson_CreatePhishingUrlDataBaseJob = false;
0024     auto layout = new QVBoxLayout(this);
0025 
0026     mCompressionType = new QComboBox(this);
0027     mCompressionType->addItem(QStringLiteral("RAW"));
0028     mCompressionType->addItem(QStringLiteral("RICE"));
0029     layout->addWidget(mCompressionType);
0030 
0031     mResult = new QPlainTextEdit(this);
0032     mResult->setReadOnly(true);
0033     layout->addWidget(mResult);
0034 
0035     mJson = new QPlainTextEdit(this);
0036     mJson->setReadOnly(true);
0037     layout->addWidget(mJson);
0038 
0039     auto buttonLayout = new QHBoxLayout;
0040     layout->addLayout(buttonLayout);
0041     auto button = new QPushButton(QStringLiteral("DownLoad full database"), this);
0042     connect(button, &QPushButton::clicked, this, &CreatePhisingUrlDataBaseGui::slotDownloadFullDatabase);
0043     buttonLayout->addWidget(button);
0044 
0045     auto button2 = new QPushButton(QStringLiteral("DownLoad partial database"), this);
0046     connect(button2, &QPushButton::clicked, this, &CreatePhisingUrlDataBaseGui::slotDownloadPartialDatabase);
0047     buttonLayout->addWidget(button2);
0048 
0049     auto save = new QPushButton(QStringLiteral("Save result to disk"), this);
0050     connect(save, &QPushButton::clicked, this, &CreatePhisingUrlDataBaseGui::slotSaveResultToDisk);
0051     buttonLayout->addWidget(save);
0052 }
0053 
0054 CreatePhisingUrlDataBaseGui::~CreatePhisingUrlDataBaseGui() = default;
0055 
0056 void CreatePhisingUrlDataBaseGui::clear()
0057 {
0058     mJson->clear();
0059     mResult->clear();
0060 }
0061 
0062 WebEngineViewer::CreatePhishingUrlDataBaseJob::ContraintsCompressionType CreatePhisingUrlDataBaseGui::compressionType()
0063 {
0064     WebEngineViewer::CreatePhishingUrlDataBaseJob::ContraintsCompressionType type = WebEngineViewer::CreatePhishingUrlDataBaseJob::RawCompression;
0065     if (mCompressionType->currentText() == QLatin1StringView("RICE")) {
0066         type = WebEngineViewer::CreatePhishingUrlDataBaseJob::RiceCompression;
0067     } else if (mCompressionType->currentText() == QLatin1StringView("RAW")) {
0068         type = WebEngineViewer::CreatePhishingUrlDataBaseJob::RawCompression;
0069     }
0070     return type;
0071 }
0072 
0073 void CreatePhisingUrlDataBaseGui::slotDownloadPartialDatabase()
0074 {
0075     const QString newValue = QInputDialog::getText(this, QStringLiteral("Define database newClientState"), QStringLiteral("newClientState:"));
0076     if (!newValue.isEmpty()) {
0077         clear();
0078         auto job = new WebEngineViewer::CreatePhishingUrlDataBaseJob(this);
0079         job->setContraintsCompressionType(compressionType());
0080         job->setDataBaseDownloadNeeded(WebEngineViewer::CreatePhishingUrlDataBaseJob::UpdateDataBase);
0081         job->setDataBaseState(newValue);
0082         connect(job, &WebEngineViewer::CreatePhishingUrlDataBaseJob::debugJsonResult, this, &CreatePhisingUrlDataBaseGui::slotResult);
0083         connect(job, &WebEngineViewer::CreatePhishingUrlDataBaseJob::debugJson, this, &CreatePhisingUrlDataBaseGui::slotDebugJSon);
0084         job->start();
0085     }
0086 }
0087 
0088 void CreatePhisingUrlDataBaseGui::slotDownloadFullDatabase()
0089 {
0090     clear();
0091     auto job = new WebEngineViewer::CreatePhishingUrlDataBaseJob(this);
0092     job->setContraintsCompressionType(compressionType());
0093     connect(job, &WebEngineViewer::CreatePhishingUrlDataBaseJob::debugJsonResult, this, &CreatePhisingUrlDataBaseGui::slotResult);
0094     connect(job, &WebEngineViewer::CreatePhishingUrlDataBaseJob::debugJson, this, &CreatePhisingUrlDataBaseGui::slotDebugJSon);
0095     job->start();
0096 }
0097 
0098 void CreatePhisingUrlDataBaseGui::slotSaveResultToDisk()
0099 {
0100     if (!mResult->document()->isEmpty()) {
0101         const QString filename = QFileDialog::getSaveFileName(this, QStringLiteral("save result to disk"));
0102         QTextStream ds;
0103         QFile file;
0104         file.setFileName(filename);
0105         if (!file.open(QIODevice::WriteOnly)) {
0106             qWarning() << " impossible to open file " << filename;
0107             return;
0108         }
0109         ds.setDevice(&file);
0110         ds << mResult->toPlainText();
0111         file.close();
0112     }
0113 }
0114 
0115 void CreatePhisingUrlDataBaseGui::slotDebugJSon(const QByteArray &data)
0116 {
0117     mJson->setPlainText(QString::fromLatin1(data));
0118 }
0119 
0120 void CreatePhisingUrlDataBaseGui::slotResult(const QByteArray &data)
0121 {
0122     mResult->setPlainText(QString::fromLatin1(data));
0123 }
0124 
0125 int main(int argc, char **argv)
0126 {
0127     QApplication app(argc, argv);
0128     QStandardPaths::setTestModeEnabled(true);
0129     auto w = new CreatePhisingUrlDataBaseGui;
0130 
0131     w->show();
0132     app.exec();
0133     delete w;
0134     return 0;
0135 }
0136 
0137 #include "moc_createphishingurldatabasegui.cpp"