Messagelib

dkimmanageruleswidget.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 "dkimmanageruleswidget.h"
8#include "dkimmanagerulescombobox.h"
9#include "dkimruledialog.h"
10#include "messageviewer_dkimcheckerdebug.h"
11#include <KLineEditEventHandler>
12#include <KLocalizedString>
13#include <KMessageBox>
14#include <KTreeWidgetSearchLine>
15#include <MessageViewer/DKIMManagerRules>
16#include <QHeaderView>
17#include <QMenu>
18#include <QPointer>
19#include <QTreeWidget>
20#include <QVBoxLayout>
21using namespace MessageViewer;
22DKIMManageRulesWidgetItem::DKIMManageRulesWidgetItem(QTreeWidget *parent)
23 : QTreeWidgetItem(parent)
24 , mRuleTypeCombobox(new DKIMManageRulesComboBox)
25{
26 treeWidget()->setItemWidget(this, ColumnType::RuleType, mRuleTypeCombobox);
27}
28
29DKIMManageRulesWidgetItem::~DKIMManageRulesWidgetItem() = default;
30
31MessageViewer::DKIMRule DKIMManageRulesWidgetItem::rule() const
32{
34 rule.setEnabled(checkState(ColumnType::Enabled) == Qt::Checked);
35 rule.setDomain(text(ColumnType::Domain));
36 rule.setFrom(text(ColumnType::From));
37 rule.setListId(text(ColumnType::ListId));
38 rule.setPriority(text(ColumnType::Priority).toInt());
39 rule.setRuleType(mRuleTypeCombobox->ruleType());
40 rule.setSignedDomainIdentifier(text(ColumnType::SDid).split(QLatin1Char(' ')));
41 return rule;
42}
43
44void DKIMManageRulesWidgetItem::setRule(const MessageViewer::DKIMRule &rule)
45{
46 if (mRule != rule) {
47 mRule = rule;
48 updateInfo();
49 }
50}
51
52void DKIMManageRulesWidgetItem::updateInfo()
53{
54 setCheckState(ColumnType::Enabled, mRule.enabled() ? Qt::Checked : Qt::Unchecked);
55 setText(ColumnType::Domain, mRule.domain());
56 setText(ColumnType::ListId, mRule.listId());
57 setText(ColumnType::From, mRule.from());
58 setText(ColumnType::SDid, mRule.signedDomainIdentifier().join(QLatin1Char(' ')));
59 setText(ColumnType::Priority, QString::number(mRule.priority()));
60 mRuleTypeCombobox->setRuleType(mRule.ruleType());
61}
62
63DKIMManageRulesWidget::DKIMManageRulesWidget(QWidget *parent)
64 : QWidget(parent)
65 , mTreeWidget(new QTreeWidget(this))
66{
67 auto mainLayout = new QVBoxLayout(this);
68 mainLayout->setObjectName(QLatin1StringView("mainLayout"));
69 mainLayout->setContentsMargins({});
70
71 mTreeWidget->setObjectName(QLatin1StringView("treewidget"));
72 mTreeWidget->setRootIsDecorated(false);
73 mTreeWidget->setHeaderLabels({i18n("Active"), i18n("Domain"), i18n("List-ID"), i18n("From"), i18n("SDID"), i18n("Rule type"), i18n("Priority")});
74 mTreeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
75 mTreeWidget->setAlternatingRowColors(true);
76
77 auto searchLineEdit = new KTreeWidgetSearchLine(this, mTreeWidget);
78 searchLineEdit->setObjectName(QLatin1StringView("searchlineedit"));
79 searchLineEdit->setClearButtonEnabled(true);
80 mainLayout->addWidget(searchLineEdit);
82
83 mainLayout->addWidget(mTreeWidget);
84 connect(mTreeWidget, &QTreeWidget::customContextMenuRequested, this, &DKIMManageRulesWidget::slotCustomContextMenuRequested);
85 connect(mTreeWidget, &QTreeWidget::itemDoubleClicked, this, [this](QTreeWidgetItem *item) {
86 if (item) {
87 auto rulesItem = static_cast<DKIMManageRulesWidgetItem *>(item);
88 modifyRule(rulesItem);
89 }
90 });
91}
92
93DKIMManageRulesWidget::~DKIMManageRulesWidget() = default;
94
95void DKIMManageRulesWidget::updateRules()
96{
97 const QList<MessageViewer::DKIMRule> rules = MessageViewer::DKIMManagerRules::self()->rules();
98
99 for (const MessageViewer::DKIMRule &rule : rules) {
100 auto item = new DKIMManageRulesWidgetItem(mTreeWidget);
101 item->setRule(rule);
102 }
103 emitUpdateExportButton();
104}
105
106void DKIMManageRulesWidget::loadSettings()
107{
108 mTreeWidget->setSortingEnabled(true);
109 mTreeWidget->header()->setSortIndicatorShown(true);
110 mTreeWidget->header()->setSectionsClickable(true);
111 mTreeWidget->sortByColumn(0, Qt::AscendingOrder);
112 updateRules();
113}
114
115QList<MessageViewer::DKIMRule> DKIMManageRulesWidget::rules() const
116{
118 const int total = mTreeWidget->topLevelItemCount();
119 rules.reserve(total);
120 for (int i = 0; i < total; ++i) {
121 QTreeWidgetItem *item = mTreeWidget->topLevelItem(i);
122 auto ruleItem = static_cast<DKIMManageRulesWidgetItem *>(item);
123 rules.append(ruleItem->rule());
124 }
125 return rules;
126}
127
128void DKIMManageRulesWidget::saveSettings()
129{
130 MessageViewer::DKIMManagerRules::self()->saveRules(rules());
131}
132
133QByteArray DKIMManageRulesWidget::saveHeaders() const
134{
135 return mTreeWidget->header()->saveState();
136}
137
138void DKIMManageRulesWidget::restoreHeaders(const QByteArray &header)
139{
140 mTreeWidget->header()->restoreState(header);
141}
142
143void DKIMManageRulesWidget::addRule()
144{
145 QPointer<DKIMRuleDialog> dlg = new DKIMRuleDialog(this);
146 if (dlg->exec()) {
147 const MessageViewer::DKIMRule rule = dlg->rule();
148 if (rule.isValid()) {
149 auto item = new DKIMManageRulesWidgetItem(mTreeWidget);
150 item->setRule(rule);
151 emitUpdateExportButton();
152 } else {
153 qCDebug(MESSAGEVIEWER_DKIMCHECKER_LOG) << "Rule is not valid";
154 }
155 }
156 delete dlg;
157}
158
159void DKIMManageRulesWidget::duplicateRule(DKIMManageRulesWidgetItem *rulesItem)
160{
161 QPointer<DKIMRuleDialog> dlg = new DKIMRuleDialog(this);
162 dlg->loadRule(rulesItem->rule());
163 if (dlg->exec()) {
164 const MessageViewer::DKIMRule rule = dlg->rule();
165 if (rule.isValid()) {
166 auto item = new DKIMManageRulesWidgetItem(mTreeWidget);
167 item->setRule(rule);
168 emitUpdateExportButton();
169 }
170 }
171 delete dlg;
172}
173
174void DKIMManageRulesWidget::emitUpdateExportButton()
175{
176 Q_EMIT updateExportButton(mTreeWidget->topLevelItemCount() > 0);
177}
178
179void DKIMManageRulesWidget::modifyRule(DKIMManageRulesWidgetItem *rulesItem)
180{
181 QPointer<DKIMRuleDialog> dlg = new DKIMRuleDialog(this);
182 dlg->loadRule(rulesItem->rule());
183 if (dlg->exec()) {
184 const MessageViewer::DKIMRule rule = dlg->rule();
185 if (rule.isValid()) {
186 rulesItem->setRule(rule);
187 }
188 }
189 delete dlg;
190}
191
192void DKIMManageRulesWidget::slotCustomContextMenuRequested(const QPoint &pos)
193{
194 QTreeWidgetItem *item = mTreeWidget->itemAt(pos);
195 QMenu menu(this);
196 menu.addAction(QIcon::fromTheme(QStringLiteral("list-add")), i18n("Add..."), this, [this]() {
197 addRule();
198 });
199 auto rulesItem = dynamic_cast<DKIMManageRulesWidgetItem *>(item);
200 if (rulesItem) {
201 menu.addAction(QIcon::fromTheme(QStringLiteral("document-edit")), i18n("Modify..."), this, [this, rulesItem]() {
202 modifyRule(rulesItem);
203 });
204 menu.addSeparator();
205 menu.addAction(QIcon::fromTheme(QStringLiteral("edit-duplicate")), i18n("Duplicate Rule"), this, [this, rulesItem]() {
206 duplicateRule(rulesItem);
207 });
208 menu.addSeparator();
209 menu.addAction(QIcon::fromTheme(QStringLiteral("edit-delete")), i18n("Remove Rule"), this, [this, item]() {
210 const int answer = KMessageBox::warningTwoActions(this,
211 i18n("Do you want to delete this rule?"),
212 i18nc("@title:window", "Delete Rule"),
215 if (answer == KMessageBox::ButtonCode::PrimaryAction) {
216 delete item;
217 emitUpdateExportButton();
218 }
219 });
220 }
221 if (mTreeWidget->topLevelItemCount() > 0) {
222 menu.addSeparator();
223 menu.addAction(i18n("Delete All"), this, [this]() {
224 const int answer = KMessageBox::warningTwoActions(this,
225 i18n("Do you want to delete all the rules?"),
226 i18nc("@title:window", "Delete Rules"),
229 if (answer == KMessageBox::ButtonCode::PrimaryAction) {
230 mTreeWidget->clear();
231 emitUpdateExportButton();
232 }
233 });
234 }
235 menu.exec(QCursor::pos());
236}
237
238#include "moc_dkimmanageruleswidget.cpp"
The DKIMManageRulesWidgetItem class.
The DKIMRule class.
Definition dkimrule.h:18
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void catchReturnKey(QObject *lineEdit)
ButtonCode warningTwoActions(QWidget *parent, const QString &text, const QString &title, const KGuiItem &primaryAction, const KGuiItem &secondaryAction, const QString &dontAskAgainName=QString(), Options options=Options(Notify|Dangerous))
KGuiItem cancel()
KGuiItem del()
QPoint pos()
bool restoreState(const QByteArray &state)
QByteArray saveState() const const
void setSectionsClickable(bool clickable)
void setSortIndicatorShown(bool show)
QIcon fromTheme(const QString &name)
void append(QList< T > &&value)
void reserve(qsizetype size)
Q_EMITQ_EMIT
T qobject_cast(QObject *object)
QString number(double n, char format, int precision)
QString join(QChar separator) const const
CustomContextMenu
AscendingOrder
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QHeaderView * header() const const
void sortByColumn(int column, Qt::SortOrder order)
void setSortingEnabled(bool enable)
void clear()
QTreeWidgetItem * itemAt(const QPoint &p) const const
void itemDoubleClicked(QTreeWidgetItem *item, int column)
QTreeWidgetItem * topLevelItem(int index) const const
Qt::CheckState checkState(int column) const const
void setCheckState(int column, Qt::CheckState state)
void setText(int column, const QString &text)
QString text(int column) const const
void customContextMenuRequested(const QPoint &pos)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 7 2024 11:58:34 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.