Messagelib

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

KDE's Doxygen guidelines are available online.