Messagelib

dkimmanagerulesdialog.cpp
1/*
2 SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org>
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
20namespace
21{
22static const char myDKIMManageRulesDialogGroupName[] = "DKIMManageRulesDialog";
23}
24using namespace MessageViewer;
25
26DKIMManageRulesDialog::DKIMManageRulesDialog(QWidget *parent)
27 : QDialog(parent)
28 , mRulesWidget(new DKIMManageRulesWidget(this))
29 , mExportButton(new QPushButton(i18nc("@action:button", "Export..."), this))
30{
31 setWindowTitle(i18nc("@title:window", "Configure Rule"));
32 auto mainLayout = new QVBoxLayout(this);
33 mainLayout->setObjectName(QLatin1StringView("mainlayout"));
34
35 mRulesWidget->setObjectName(QLatin1StringView("mRulesWidget"));
36 mainLayout->addWidget(mRulesWidget);
37
39 buttonBox->setObjectName(QLatin1StringView("buttonBox"));
40 mainLayout->addWidget(buttonBox);
41
42 auto addButton = new QPushButton(i18nc("@action:button", "Add Rule..."), this);
43 addButton->setObjectName(QLatin1StringView("addButton"));
44 buttonBox->addButton(addButton, QDialogButtonBox::ActionRole);
45 connect(addButton, &QPushButton::clicked, mRulesWidget, &DKIMManageRulesWidget::addRule);
46
47 auto importButton = new QPushButton(i18nc("@action:button", "Import..."), this);
48 importButton->setObjectName(QLatin1StringView("importButton"));
49 buttonBox->addButton(importButton, QDialogButtonBox::ActionRole);
50 connect(importButton, &QPushButton::clicked, this, &DKIMManageRulesDialog::slotImport);
51
52 mExportButton->setObjectName(QLatin1StringView("mExportButton"));
53 buttonBox->addButton(mExportButton, QDialogButtonBox::ActionRole);
54 connect(mExportButton, &QPushButton::clicked, this, &DKIMManageRulesDialog::slotExport);
55
56 connect(buttonBox, &QDialogButtonBox::accepted, this, &DKIMManageRulesDialog::slotAccepted);
57 connect(buttonBox, &QDialogButtonBox::rejected, this, &DKIMManageRulesDialog::reject);
58 connect(mRulesWidget, &DKIMManageRulesWidget::updateExportButton, mExportButton, &QPushButton::setEnabled);
59 mExportButton->setEnabled(false);
60 mRulesWidget->loadSettings();
61 readConfig();
62}
63
64DKIMManageRulesDialog::~DKIMManageRulesDialog()
65{
66 writeConfig();
67}
68
69void DKIMManageRulesDialog::slotAccepted()
70{
71 mRulesWidget->saveSettings();
72 accept();
73}
74
75void DKIMManageRulesDialog::slotExport()
76{
77 const auto rules = mRulesWidget->rules();
78 if (rules.isEmpty()) {
79 KMessageBox::error(this, i18n("No rules found"), i18nc("@title:window", "Export Rules"));
80 return;
81 }
82 const QString fileName = QFileDialog::getSaveFileName(this, i18nc("@title:window", "Export Rules"));
83 if (!fileName.isEmpty()) {
84 MessageViewer::DKIMManagerRules::self()->exportRules(fileName, rules);
85 }
86}
87
88void DKIMManageRulesDialog::slotImport()
89{
90 const QString fileName = QFileDialog::getOpenFileName(this, i18nc("@title:window", "Import Rules"));
91 if (!fileName.isEmpty()) {
92 if (MessageViewer::DKIMManagerRules::self()->importRules(fileName) == 0) {
93 KMessageBox::error(this, i18n("No rules imported."), i18nc("@title:window", "Import Rules"));
94 } else {
95 mRulesWidget->updateRules();
96 }
97 }
98}
99
100void DKIMManageRulesDialog::readConfig()
101{
102 KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(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
110void DKIMManageRulesDialog::writeConfig()
111{
112 KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myDKIMManageRulesDialogGroupName));
113 group.writeEntry("Header", mRulesWidget->saveHeaders());
114 group.writeEntry("Size", size());
115 group.sync();
116}
117
118#include "moc_dkimmanagerulesdialog.cpp"
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
The DKIMManageRulesWidget class.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
void clicked(bool checked)
virtual void accept()
QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, Options options)
QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, Options options)
bool isEmpty() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void setEnabled(bool)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:50:55 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.