Messagelib

dkimmanageruleswidget.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 "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::loadSettings()
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  mTreeWidget->setSortingEnabled(true);
102  mTreeWidget->header()->setSortIndicatorShown(true);
103  mTreeWidget->header()->setSectionsClickable(true);
104  mTreeWidget->sortByColumn(0, Qt::AscendingOrder);
105 }
106 
107 QVector<MessageViewer::DKIMRule> DKIMManageRulesWidget::rules() const
108 {
110  const int total = mTreeWidget->topLevelItemCount();
111  rules.reserve(total);
112  for (int i = 0; i < total; ++i) {
113  QTreeWidgetItem *item = mTreeWidget->topLevelItem(i);
114  auto ruleItem = static_cast<DKIMManageRulesWidgetItem *>(item);
115  rules.append(ruleItem->rule());
116  }
117  return rules;
118 }
119 
120 void DKIMManageRulesWidget::saveSettings()
121 {
122  MessageViewer::DKIMManagerRules::self()->saveRules(rules());
123 }
124 
125 QByteArray DKIMManageRulesWidget::saveHeaders() const
126 {
127  return mTreeWidget->header()->saveState();
128 }
129 
130 void DKIMManageRulesWidget::restoreHeaders(const QByteArray &header)
131 {
132  mTreeWidget->header()->restoreState(header);
133 }
134 
135 void DKIMManageRulesWidget::addRule()
136 {
137  QPointer<DKIMRuleDialog> dlg = new DKIMRuleDialog(this);
138  if (dlg->exec()) {
139  const MessageViewer::DKIMRule rule = dlg->rule();
140  if (rule.isValid()) {
141  auto item = new DKIMManageRulesWidgetItem(mTreeWidget);
142  item->setRule(rule);
143  } else {
144  qCDebug(MESSAGEVIEWER_DKIMCHECKER_LOG) << "Rule is not valid";
145  }
146  }
147  delete dlg;
148 }
149 
150 void DKIMManageRulesWidget::duplicateRule(DKIMManageRulesWidgetItem *rulesItem)
151 {
152  QPointer<DKIMRuleDialog> dlg = new DKIMRuleDialog(this);
153  dlg->loadRule(rulesItem->rule());
154  if (dlg->exec()) {
155  const MessageViewer::DKIMRule rule = dlg->rule();
156  if (rule.isValid()) {
157  auto item = new DKIMManageRulesWidgetItem(mTreeWidget);
158  item->setRule(rule);
159  }
160  }
161  delete dlg;
162 }
163 
164 void DKIMManageRulesWidget::modifyRule(DKIMManageRulesWidgetItem *rulesItem)
165 {
166  QPointer<DKIMRuleDialog> dlg = new DKIMRuleDialog(this);
167  dlg->loadRule(rulesItem->rule());
168  if (dlg->exec()) {
169  const MessageViewer::DKIMRule rule = dlg->rule();
170  if (rule.isValid()) {
171  rulesItem->setRule(rule);
172  }
173  }
174  delete dlg;
175 }
176 
177 void DKIMManageRulesWidget::slotCustomContextMenuRequested(const QPoint &pos)
178 {
179  QTreeWidgetItem *item = mTreeWidget->itemAt(pos);
180  QMenu menu(this);
181  menu.addAction(QIcon::fromTheme(QStringLiteral("list-add")), i18n("Add..."), this, [this]() {
182  addRule();
183  });
184  auto rulesItem = dynamic_cast<DKIMManageRulesWidgetItem *>(item);
185  if (rulesItem) {
186  menu.addAction(QIcon::fromTheme(QStringLiteral("document-edit")), i18n("Modify..."), this, [this, rulesItem]() {
187  modifyRule(rulesItem);
188  });
189  menu.addSeparator();
190  menu.addAction(QIcon::fromTheme(QStringLiteral("edit-duplicate")), i18n("Duplicate Rule"), this, [this, rulesItem]() {
191  duplicateRule(rulesItem);
192  });
193  menu.addSeparator();
194  menu.addAction(QIcon::fromTheme(QStringLiteral("edit-delete")), i18n("Remove Rule"), this, [this, item]() {
195  const int answer = KMessageBox::warningYesNo(this,
196  i18n("Do you want to delete this rule?"),
197  i18n("Delete Rule"),
200  if (answer == KMessageBox::Yes) {
201  delete item;
202  }
203  });
204  }
205  if (mTreeWidget->topLevelItemCount() > 0) {
206  menu.addSeparator();
207  menu.addAction(i18n("Delete All"), this, [this]() {
208  const int answer = KMessageBox::warningYesNo(this,
209  i18n("Do you want to delete all the rules?"),
210  i18n("Delete Rules"),
213  if (answer == KMessageBox::Yes) {
214  mTreeWidget->clear();
215  }
216  });
217  }
218  menu.exec(QCursor::pos());
219 }
void customContextMenuRequested(const QPoint &pos)
void append(const T &value)
KGuiItem cancel()
CustomContextMenu
KGuiItem del()
QString number(int n, int base)
QPoint pos() const const
void reserve(int size)
QString i18n(const char *text, const TYPE &arg...)
The DKIMManageRulesWidgetItem class.
QPoint pos()
AscendingOrder
QIcon fromTheme(const QString &name)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void itemDoubleClicked(QTreeWidgetItem *item, int column)
The DKIMRule class.
Definition: dkimrule.h:17
ButtonCode warningYesNo(QWidget *parent, const QString &text, const QString &caption=QString(), const KGuiItem &buttonYes=KStandardGuiItem::yes(), const KGuiItem &buttonNo=KStandardGuiItem::no(), const QString &dontAskAgainName=QString(), Options options=Options(Notify|Dangerous))
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Dec 4 2021 23:12:53 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.