Messagelib

dkimmanagerulesdialog.cpp
1 /*
2  SPDX-FileCopyrightText: 2019-2022 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  addButton->setObjectName(QStringLiteral("addButton"));
43  buttonBox->addButton(addButton, QDialogButtonBox::ActionRole);
44  connect(addButton, &QPushButton::clicked, mRulesWidget, &DKIMManageRulesWidget::addRule);
45 
46  auto importButton = new QPushButton(i18n("Import..."), this);
47  importButton->setObjectName(QStringLiteral("importButton"));
48  buttonBox->addButton(importButton, QDialogButtonBox::ActionRole);
49  connect(importButton, &QPushButton::clicked, this, &DKIMManageRulesDialog::slotImport);
50 
51  mExportButton = new QPushButton(i18n("Export..."), this);
52  mExportButton->setObjectName(QStringLiteral("mExportButton"));
53  buttonBox->addButton(mExportButton, QDialogButtonBox::ActionRole);
54  connect(mExportButton, &QPushButton::clicked, this, &DKIMManageRulesDialog::slotExport);
55 
56  connect(buttonBox, &QDialogButtonBox::accepted, this, &DKIMManageRulesDialog::slotAccepted);
58  connect(mRulesWidget, &DKIMManageRulesWidget::updateExportButton, mExportButton, &QPushButton::setEnabled);
59  mExportButton->setEnabled(false);
60  mRulesWidget->loadSettings();
61  readConfig();
62 }
63 
64 DKIMManageRulesDialog::~DKIMManageRulesDialog()
65 {
66  writeConfig();
67 }
68 
69 void DKIMManageRulesDialog::slotAccepted()
70 {
71  mRulesWidget->saveSettings();
72  accept();
73 }
74 
75 void DKIMManageRulesDialog::slotExport()
76 {
77  const auto rules = mRulesWidget->rules();
78  if (rules.isEmpty()) {
79  KMessageBox::error(this, i18n("No rules found"), i18n("Export Rules"));
80  return;
81  }
82  const QString fileName = QFileDialog::getSaveFileName(this, i18n("Export Rules"));
83  if (!fileName.isEmpty()) {
84  MessageViewer::DKIMManagerRules::self()->exportRules(fileName, rules);
85  }
86 }
87 
88 void DKIMManageRulesDialog::slotImport()
89 {
90  const QString fileName = QFileDialog::getOpenFileName(this, i18n("Import Rules"));
91  if (!fileName.isEmpty()) {
92  if (MessageViewer::DKIMManagerRules::self()->importRules(fileName) == 0) {
93  KMessageBox::error(this, i18n("No rules imported."), i18n("Import Rules"));
94  } else {
95  mRulesWidget->updateRules();
96  }
97  }
98 }
99 
100 void DKIMManageRulesDialog::readConfig()
101 {
102  KConfigGroup group(KSharedConfig::openStateConfig(), myDKIMManageRulesDialogGroupName);
103  const QSize size = group.readEntry("Size", QSize(600, 400));
104  mRulesWidget->restoreHeaders(group.readEntry("Header", QByteArray()));
105  if (size.isValid()) {
106  resize(size);
107  }
108 }
109 
110 void DKIMManageRulesDialog::writeConfig()
111 {
112  KConfigGroup group(KSharedConfig::openStateConfig(), myDKIMManageRulesDialogGroupName);
113  group.writeEntry("Header", mRulesWidget->saveHeaders());
114  group.writeEntry("Size", size());
115  group.sync();
116 }
virtual void reject()
void clicked(bool checked)
void readConfig()
void error(QWidget *parent, const QString &text, const QString &caption=QString(), Options options=Notify)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
The DKIMManageRulesWidget class.
QString i18n(const char *text, const TYPE &arg...)
bool isEmpty() const const
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
virtual void accept()
void setEnabled(bool)
void resize(int w, int h)
QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options)
QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu May 19 2022 03:53:25 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.