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 "scamdetectiondetailsdialog.h"
0009 #include "messageviewer_debug.h"
0010 #include "settings/messageviewersettings.h"
0011 
0012 #include <TextCustomEditor/RichTextEditorWidget>
0013 
0014 #include <KLocalizedString>
0015 
0016 #include <QUrl>
0017 
0018 #include <KStandardGuiItem>
0019 #include <QFileDialog>
0020 
0021 #include <KConfigGroup>
0022 #include <KGuiItem>
0023 #include <KWindowConfig>
0024 #include <QDialogButtonBox>
0025 #include <QPushButton>
0026 #include <QTextStream>
0027 #include <QVBoxLayout>
0028 #include <QWindow>
0029 #include <memory>
0030 
0031 using namespace MessageViewer;
0032 namespace
0033 {
0034 static const char myScamDetectionDetailsDialogConfigGroupName[] = "ScamDetectionDetailsDialog";
0035 }
0036 ScamDetectionDetailsDialog::ScamDetectionDetailsDialog(QWidget *parent)
0037     : QDialog(parent)
0038     , mDetails(new TextCustomEditor::RichTextEditorWidget(this))
0039 {
0040     setWindowTitle(i18nc("@title:window", "Details"));
0041     setAttribute(Qt::WA_DeleteOnClose);
0042     auto mainLayout = new QVBoxLayout(this);
0043 
0044     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, this);
0045     auto user1Button = new QPushButton(this);
0046     buttonBox->addButton(user1Button, QDialogButtonBox::ActionRole);
0047     connect(buttonBox, &QDialogButtonBox::accepted, this, &ScamDetectionDetailsDialog::accept);
0048     connect(buttonBox, &QDialogButtonBox::rejected, this, &ScamDetectionDetailsDialog::reject);
0049     KGuiItem::assign(user1Button, KStandardGuiItem::saveAs());
0050     setModal(false);
0051     mainLayout->addWidget(mDetails);
0052     mainLayout->addWidget(buttonBox);
0053     mDetails->setReadOnly(true);
0054     connect(user1Button, &QPushButton::clicked, this, &ScamDetectionDetailsDialog::slotSaveAs);
0055     readConfig();
0056 }
0057 
0058 ScamDetectionDetailsDialog::~ScamDetectionDetailsDialog()
0059 {
0060     writeConfig();
0061 }
0062 
0063 void ScamDetectionDetailsDialog::slotSaveAs()
0064 {
0065     QUrl url;
0066     std::unique_ptr<QFileDialog> fdlg(new QFileDialog(this, QString(), url.path()));
0067     fdlg->setAcceptMode(QFileDialog::AcceptSave);
0068     fdlg->setFileMode(QFileDialog::AnyFile);
0069     fdlg->selectFile(QStringLiteral("scam-detection.html"));
0070     if (fdlg->exec() == QDialog::Accepted) {
0071         const QStringList fileNames = fdlg->selectedFiles();
0072         if (!fileNames.isEmpty()) {
0073             QFile file(fileNames.at(0));
0074             if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
0075                 qCDebug(MESSAGEVIEWER_LOG) << "We can't save in file :" << fileNames.at(0);
0076                 return;
0077             }
0078             QTextStream ts(&file);
0079             QString htmlStr = mDetails->toHtml();
0080             htmlStr.replace(QLatin1StringView(R"(meta name="qrichtext" content="1")"),
0081                             QLatin1StringView(R"(meta http-equiv="Content-Type" content="text/html; charset=UTF-8")"));
0082             ts << htmlStr;
0083             file.close();
0084         }
0085     }
0086 }
0087 
0088 void ScamDetectionDetailsDialog::setDetails(const QString &details)
0089 {
0090     mDetails->setHtml(details);
0091 }
0092 
0093 void ScamDetectionDetailsDialog::readConfig()
0094 {
0095     create(); // ensure a window is created
0096     windowHandle()->resize(QSize(600, 200));
0097     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myScamDetectionDetailsDialogConfigGroupName));
0098     KWindowConfig::restoreWindowSize(windowHandle(), group);
0099     resize(windowHandle()->size()); // workaround for QTBUG-40584
0100 }
0101 
0102 void ScamDetectionDetailsDialog::writeConfig()
0103 {
0104     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myScamDetectionDetailsDialogConfigGroupName));
0105     KWindowConfig::saveWindowSize(windowHandle(), group);
0106 }
0107 
0108 #include "moc_scamdetectiondetailsdialog.cpp"