Messagelib

dkimmanagerulesdialog.cpp
1 /*
2  SPDX-FileCopyrightText: 2019-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "dkimmanagerulesdialog.h"
8 #include "dkimmanagerrules.h"
9 #include "dkimmanageruleswidget.h"
10 
11 #include <KConfigGroup>
12 #include <KLocalizedString>
13 #include <KMessageBox>
14 #include <KSharedConfig>
15 #include <QDialogButtonBox>
16 #include <QFileDialog>
17 #include <QPushButton>
18 #include <QVBoxLayout>
19 
20 namespace
21 {
22 static const char myDKIMManageRulesDialogGroupName[] = "DKIMManageRulesDialog";
23 }
24 using namespace MessageViewer;
25 
26 DKIMManageRulesDialog::DKIMManageRulesDialog(QWidget *parent)
27  : QDialog(parent)
28  , mRulesWidget(new DKIMManageRulesWidget(this))
29 {
30  setWindowTitle(i18nc("@title:window", "Configure Rule"));
31  auto mainLayout = new QVBoxLayout(this);
32  mainLayout->setObjectName(QStringLiteral("mainlayout"));
33 
34  mRulesWidget->setObjectName(QStringLiteral("mRulesWidget"));
35  mainLayout->addWidget(mRulesWidget);
36 
38  buttonBox->setObjectName(QStringLiteral("buttonBox"));
39  mainLayout->addWidget(buttonBox);
40 
41  auto addButton = new QPushButton(i18n("Add Rule..."), this);
42  buttonBox->addButton(addButton, QDialogButtonBox::ActionRole);
43  connect(addButton, &QPushButton::clicked, mRulesWidget, &DKIMManageRulesWidget::addRule);
44 
45  auto importButton = new QPushButton(i18n("Import..."), this);
46  buttonBox->addButton(importButton, QDialogButtonBox::ActionRole);
47  connect(importButton, &QPushButton::clicked, this, &DKIMManageRulesDialog::slotImport);
48 
49  auto exportButton = new QPushButton(i18n("Export..."), this);
50  buttonBox->addButton(exportButton, QDialogButtonBox::ActionRole);
51  connect(exportButton, &QPushButton::clicked, this, &DKIMManageRulesDialog::slotExport);
52 
53  connect(buttonBox, &QDialogButtonBox::accepted, this, &DKIMManageRulesDialog::slotAccepted);
55  mRulesWidget->loadSettings();
56  readConfig();
57 }
58 
59 DKIMManageRulesDialog::~DKIMManageRulesDialog()
60 {
61  writeConfig();
62 }
63 
64 void DKIMManageRulesDialog::slotAccepted()
65 {
66  mRulesWidget->saveSettings();
67  accept();
68 }
69 
70 void DKIMManageRulesDialog::slotExport()
71 {
72  const auto rules = mRulesWidget->rules();
73  if (rules.isEmpty()) {
74  KMessageBox::error(this, i18n("No rules found"), i18n("Export Rules"));
75  return;
76  }
77  const QString fileName = QFileDialog::getSaveFileName(this, i18n("Export Rules"));
78  if (!fileName.isEmpty()) {
79  MessageViewer::DKIMManagerRules::self()->exportRules(fileName, rules);
80  }
81 }
82 
83 void DKIMManageRulesDialog::slotImport()
84 {
85  const QString fileName = QFileDialog::getOpenFileName(this, i18n("Import Rules"));
86  if (!fileName.isEmpty()) {
87  if (MessageViewer::DKIMManagerRules::self()->importRules(fileName) == 0) {
88  KMessageBox::error(this, i18n("No rules imported."), i18n("Import Rules"));
89  }
90  }
91 }
92 
93 void DKIMManageRulesDialog::readConfig()
94 {
95  KConfigGroup group(KSharedConfig::openStateConfig(), myDKIMManageRulesDialogGroupName);
96  const QSize size = group.readEntry("Size", QSize(600, 400));
97  mRulesWidget->restoreHeaders(group.readEntry("Header", QByteArray()));
98  if (size.isValid()) {
99  resize(size);
100  }
101 }
102 
103 void DKIMManageRulesDialog::writeConfig()
104 {
105  KConfigGroup group(KSharedConfig::openStateConfig(), myDKIMManageRulesDialogGroupName);
106  group.writeEntry("Header", mRulesWidget->saveHeaders());
107  group.writeEntry("Size", size());
108  group.sync();
109 }
bool isValid() const const
virtual void reject()
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)
void error(QWidget *parent, const QString &text, const QString &caption=QString(), Options options=Notify)
QString i18n(const char *text, const TYPE &arg...)
void readConfig()
QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options)
The DKIMManageRulesWidget class.
QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options)
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 Thu Dec 2 2021 23:06:08 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.