Messagelib

scamdetectiondetailsdialog.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
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 <KPIMTextEdit/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 <QDialogButtonBox>
24 #include <QPushButton>
25 #include <QTextStream>
26 #include <QVBoxLayout>
27 #include <memory>
28 
29 using namespace MessageViewer;
30 namespace
31 {
32 static const char myScamDetectionDetailsDialogConfigGroupName[] = "ScamDetectionDetailsDialog";
33 }
34 ScamDetectionDetailsDialog::ScamDetectionDetailsDialog(QWidget *parent)
35  : QDialog(parent)
36  , mDetails(new KPIMTextEdit::RichTextEditorWidget(this))
37 {
38  setWindowTitle(i18nc("@title:window", "Details"));
39  setAttribute(Qt::WA_DeleteOnClose);
40  auto mainLayout = new QVBoxLayout(this);
41 
42  auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, this);
43  auto user1Button = new QPushButton(this);
44  buttonBox->addButton(user1Button, QDialogButtonBox::ActionRole);
45  connect(buttonBox, &QDialogButtonBox::accepted, this, &ScamDetectionDetailsDialog::accept);
46  connect(buttonBox, &QDialogButtonBox::rejected, this, &ScamDetectionDetailsDialog::reject);
48  setModal(false);
49  mainLayout->addWidget(mDetails);
50  mainLayout->addWidget(buttonBox);
51  mDetails->setReadOnly(true);
52  connect(user1Button, &QPushButton::clicked, this, &ScamDetectionDetailsDialog::slotSaveAs);
53  readConfig();
54 }
55 
56 ScamDetectionDetailsDialog::~ScamDetectionDetailsDialog()
57 {
58  writeConfig();
59 }
60 
61 void ScamDetectionDetailsDialog::slotSaveAs()
62 {
63  QUrl url;
64  std::unique_ptr<QFileDialog> fdlg(new QFileDialog(this, QString(), url.path()));
65  fdlg->setAcceptMode(QFileDialog::AcceptSave);
66  fdlg->setFileMode(QFileDialog::AnyFile);
67  fdlg->selectFile(QStringLiteral("scam-detection.html"));
68  if (fdlg->exec() == QDialog::Accepted) {
69  const QStringList fileNames = fdlg->selectedFiles();
70  if (!fileNames.isEmpty()) {
71  QFile file(fileNames.at(0));
72  if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
73  qCDebug(MESSAGEVIEWER_LOG) << "We can't save in file :" << fileNames.at(0);
74  return;
75  }
76  QTextStream ts(&file);
77 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
78  ts.setCodec("UTF-8");
79 #endif
80  QString htmlStr = mDetails->toHtml();
81  htmlStr.replace(QLatin1String(R"(meta name="qrichtext" content="1")"),
82  QLatin1String(R"(meta http-equiv="Content-Type" content="text/html; charset=UTF-8")"));
83  ts << htmlStr;
84  file.close();
85  }
86  }
87 }
88 
89 void ScamDetectionDetailsDialog::setDetails(const QString &details)
90 {
91  mDetails->setHtml(details);
92 }
93 
94 void ScamDetectionDetailsDialog::readConfig()
95 {
96  KConfigGroup group(KSharedConfig::openStateConfig(), myScamDetectionDetailsDialogConfigGroupName);
97  const QSize size = group.readEntry("Size", QSize(600, 400));
98  if (size.isValid()) {
99  resize(size);
100  }
101 }
102 
103 void ScamDetectionDetailsDialog::writeConfig()
104 {
105  KConfigGroup group(KSharedConfig::openStateConfig(), myScamDetectionDetailsDialogConfigGroupName);
106  group.writeEntry("Size", size());
107  group.sync();
108 }
bool isValid() const const
const T & at(int i) const const
KGuiItem saveAs()
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
QString i18nc(const char *context, const char *text, const TYPE &arg...)
bool isEmpty() const const
void clicked(bool checked)
static void assign(QPushButton *button, const KGuiItem &item)
QString path(QUrl::ComponentFormattingOptions options) const const
WA_DeleteOnClose
QString & replace(int position, int n, QChar after)
void readConfig()
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Nov 30 2021 23:05:47 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.