Messagelib

scamdetectiondetailsdialog.cpp
1/*
2 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5
6*/
7
8#include "scamdetectiondetailsdialog.h"
9#include "messageviewer_debug.h"
10#include "settings/messageviewersettings.h"
11
12#include <TextCustomEditor/RichTextEditorWidget>
13
14#include <KLocalizedString>
15
16#include <QUrl>
17
18#include <KStandardGuiItem>
19#include <QFileDialog>
20
21#include <KConfigGroup>
22#include <KGuiItem>
23#include <KWindowConfig>
24#include <QDialogButtonBox>
25#include <QPushButton>
26#include <QTextStream>
27#include <QVBoxLayout>
28#include <QWindow>
29#include <memory>
30
31using namespace MessageViewer;
32namespace
33{
34static const char myScamDetectionDetailsDialogConfigGroupName[] = "ScamDetectionDetailsDialog";
35}
36ScamDetectionDetailsDialog::ScamDetectionDetailsDialog(QWidget *parent)
37 : QDialog(parent)
38 , mDetails(new TextCustomEditor::RichTextEditorWidget(this))
39{
40 setWindowTitle(i18nc("@title:window", "Details"));
41 setAttribute(Qt::WA_DeleteOnClose);
42 auto mainLayout = new QVBoxLayout(this);
43
44 auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, this);
45 auto user1Button = new QPushButton(this);
46 buttonBox->addButton(user1Button, QDialogButtonBox::ActionRole);
47 connect(buttonBox, &QDialogButtonBox::accepted, this, &ScamDetectionDetailsDialog::accept);
48 connect(buttonBox, &QDialogButtonBox::rejected, this, &ScamDetectionDetailsDialog::reject);
50 setModal(false);
51 mainLayout->addWidget(mDetails);
52 mainLayout->addWidget(buttonBox);
53 mDetails->setReadOnly(true);
54 connect(user1Button, &QPushButton::clicked, this, &ScamDetectionDetailsDialog::slotSaveAs);
55 readConfig();
56}
57
58ScamDetectionDetailsDialog::~ScamDetectionDetailsDialog()
59{
60 writeConfig();
61}
62
63void ScamDetectionDetailsDialog::slotSaveAs()
64{
65 QUrl url;
66 std::unique_ptr<QFileDialog> fdlg(new QFileDialog(this, QString(), url.path()));
67 fdlg->setAcceptMode(QFileDialog::AcceptSave);
68 fdlg->setFileMode(QFileDialog::AnyFile);
69 fdlg->selectFile(QStringLiteral("scam-detection.html"));
70 if (fdlg->exec() == QDialog::Accepted) {
71 const QStringList fileNames = fdlg->selectedFiles();
72 if (!fileNames.isEmpty()) {
73 QFile file(fileNames.at(0));
74 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
75 qCDebug(MESSAGEVIEWER_LOG) << "We can't save in file :" << fileNames.at(0);
76 return;
77 }
78 QTextStream ts(&file);
79 QString htmlStr = mDetails->toHtml();
80 htmlStr.replace(QLatin1StringView(R"(meta name="qrichtext" content="1")"),
81 QLatin1StringView(R"(meta http-equiv="Content-Type" content="text/html; charset=UTF-8")"));
82 ts << htmlStr;
83 file.close();
84 }
85 }
86}
87
88void ScamDetectionDetailsDialog::setDetails(const QString &details)
89{
90 mDetails->setHtml(details);
91}
92
93void ScamDetectionDetailsDialog::readConfig()
94{
95 create(); // ensure a window is created
96 windowHandle()->resize(QSize(600, 200));
99 resize(windowHandle()->size()); // workaround for QTBUG-40584
100}
101
102void ScamDetectionDetailsDialog::writeConfig()
103{
106}
107
108#include "moc_scamdetectiondetailsdialog.cpp"
static void assign(QPushButton *button, const KGuiItem &item)
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
QString i18nc(const char *context, const char *text, const TYPE &arg...)
KGuiItem saveAs()
KCONFIGGUI_EXPORT void saveWindowSize(const QWindow *window, KConfigGroup &config, KConfigGroup::WriteConfigFlags options=KConfigGroup::Normal)
KCONFIGGUI_EXPORT void restoreWindowSize(QWindow *window, const KConfigGroup &config)
KEDUVOCDOCUMENT_EXPORT QStringList fileNames(const QString &language=QString())
void clicked(bool checked)
const T & at(int i) const const
bool isEmpty() const const
T qobject_cast(QObject *object)
WA_DeleteOnClose
QString path(QUrl::ComponentFormattingOptions options) const const
void create(WId window, bool initializeWindow, bool destroyOldWindow)
void resize(int w, int h)
QWindow * windowHandle() const const
void resize(const QSize &newSize)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sun Feb 25 2024 18:37:31 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.