Messagelib

dkimmanagerkeywidget.cpp
1 /*
2  SPDX-FileCopyrightText: 2018-2023 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "dkimmanagerkeywidget.h"
8 #include "dkimmanagerkey.h"
9 
10 #include <KLocalizedString>
11 #include <KMessageBox>
12 #include <KTreeWidgetSearchLine>
13 #include <QApplication>
14 #include <QClipboard>
15 #include <QHeaderView>
16 #include <QMenu>
17 #include <QTreeWidget>
18 #include <QVBoxLayout>
19 
20 using namespace MessageViewer;
21 DKIMManagerKeyWidget::DKIMManagerKeyWidget(QWidget *parent)
22  : QWidget(parent)
23  , mTreeWidget(new QTreeWidget(this))
24 {
25  auto mainLayout = new QVBoxLayout(this);
26  mainLayout->setObjectName(QStringLiteral("mainlayout"));
27  mainLayout->setContentsMargins({});
28 
29  mTreeWidget->setObjectName(QStringLiteral("treewidget"));
30  mTreeWidget->setRootIsDecorated(false);
31  mTreeWidget->setHeaderLabels({i18n("SDID"), i18n("Selector"), i18n("DKIM Key"), i18n("Inserted"), i18n("Last Used")});
32  mTreeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
33  mTreeWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
34  mTreeWidget->setAlternatingRowColors(true);
35 
36  auto searchLineEdit = new KTreeWidgetSearchLine(this, mTreeWidget);
37  searchLineEdit->setObjectName(QStringLiteral("searchlineedit"));
38  searchLineEdit->setClearButtonEnabled(true);
39  mainLayout->addWidget(searchLineEdit);
40 
41  mainLayout->addWidget(mTreeWidget);
42  connect(mTreeWidget, &QTreeWidget::customContextMenuRequested, this, &DKIMManagerKeyWidget::slotCustomContextMenuRequested);
43 }
44 
45 DKIMManagerKeyWidget::~DKIMManagerKeyWidget() = default;
46 
47 QByteArray DKIMManagerKeyWidget::saveHeaders() const
48 {
49  return mTreeWidget->header()->saveState();
50 }
51 
52 void DKIMManagerKeyWidget::restoreHeaders(const QByteArray &header)
53 {
54  mTreeWidget->header()->restoreState(header);
55 }
56 
57 void DKIMManagerKeyWidget::slotCustomContextMenuRequested(const QPoint &pos)
58 {
59  QTreeWidgetItem *item = mTreeWidget->itemAt(pos);
60  QMenu menu(this);
61  if (item) {
62  const auto selectedItemsCount{mTreeWidget->selectedItems().count()};
63  if (selectedItemsCount == 1) {
64  menu.addAction(QIcon::fromTheme(QStringLiteral("edit-copy")), i18n("Copy Key"), this, [item]() {
66  });
67  menu.addSeparator();
68  }
69  menu.addAction(QIcon::fromTheme(QStringLiteral("edit-delete")),
70  i18np("Remove Key", "Remove Keys", selectedItemsCount),
71  this,
72  [this, selectedItemsCount]() {
73  const int answer = KMessageBox::questionTwoActions(
74  this,
75  i18np("Do you want to delete this key?", "Do you want to delete these keys?", selectedItemsCount),
76  i18np("Delete Key", "Delete Keys", selectedItemsCount),
79  if (answer == KMessageBox::ButtonCode::PrimaryAction) {
80  const auto selectedItems = mTreeWidget->selectedItems();
81  for (QTreeWidgetItem *item : selectedItems) {
82  delete item;
83  }
84  }
85  });
86  menu.addSeparator();
87  }
88  if (mTreeWidget->topLevelItemCount() > 0) {
89  menu.addAction(i18n("Delete All"), this, [this]() {
90  const int answer = KMessageBox::warningTwoActions(this,
91  i18n("Do you want to delete all keys?"),
92  i18n("Delete Keys"),
95  if (answer == KMessageBox::ButtonCode::PrimaryAction) {
96  mTreeWidget->clear();
97  }
98  });
99  }
100  if (!menu.isEmpty()) {
101  menu.exec(QCursor::pos());
102  }
103 }
104 
105 void DKIMManagerKeyWidget::loadKeys()
106 {
107  const QVector<MessageViewer::KeyInfo> lst = DKIMManagerKey::self()->keys();
108  for (const MessageViewer::KeyInfo &key : lst) {
109  auto item = new DKIMManagerKeyTreeWidgetItem(mTreeWidget);
110  item->setStoredAtDateTime(key.storedAtDateTime);
111  item->setLastUsedDateTime(key.lastUsedDateTime);
112  item->setText(ManagerKeyTreeWidget::Domain, key.domain);
113  item->setText(ManagerKeyTreeWidget::Selector, key.selector);
114  item->setText(ManagerKeyTreeWidget::KeyValue, key.keyValue);
115  item->setText(ManagerKeyTreeWidget::InsertDate, key.storedAtDateTime.toString());
116  item->setText(ManagerKeyTreeWidget::LastUsedDate, key.lastUsedDateTime.toString());
117  item->setToolTip(ManagerKeyTreeWidget::KeyValue, key.keyValue);
118  }
119  mTreeWidget->setSortingEnabled(true);
120  mTreeWidget->header()->setSortIndicatorShown(true);
121  mTreeWidget->header()->setSectionsClickable(true);
122  mTreeWidget->sortByColumn(ManagerKeyTreeWidget::Domain, Qt::AscendingOrder);
123 }
124 
125 void DKIMManagerKeyWidget::saveKeys()
126 {
128  lst.reserve(mTreeWidget->topLevelItemCount());
129  for (int i = 0, total = mTreeWidget->topLevelItemCount(); i < total; ++i) {
130  QTreeWidgetItem *item = mTreeWidget->topLevelItem(i);
131  const MessageViewer::KeyInfo info{item->text(ManagerKeyTreeWidget::KeyValue),
132  item->text(ManagerKeyTreeWidget::Selector),
133  item->text(ManagerKeyTreeWidget::Domain),
134  QDateTime::fromString(item->text(ManagerKeyTreeWidget::InsertDate)),
135  QDateTime::fromString(item->text(ManagerKeyTreeWidget::LastUsedDate))};
136  lst.append(info);
137  }
138  DKIMManagerKey::self()->saveKeys(lst);
139 }
140 
141 void DKIMManagerKeyWidget::resetKeys()
142 {
143  mTreeWidget->clear();
144  loadKeys();
145 }
146 
147 DKIMManagerKeyTreeWidgetItem::DKIMManagerKeyTreeWidgetItem(QTreeWidget *parent)
148  : QTreeWidgetItem(parent)
149 {
150 }
151 
152 DKIMManagerKeyTreeWidgetItem::~DKIMManagerKeyTreeWidgetItem() = default;
153 
154 bool DKIMManagerKeyTreeWidgetItem::operator<(const QTreeWidgetItem &other) const
155 {
156  const int column = treeWidget()->sortColumn();
157  if (column == DKIMManagerKeyWidget::ManagerKeyTreeWidget::InsertDate) {
158  return storedAtDateTime() < static_cast<const DKIMManagerKeyTreeWidgetItem *>(&other)->storedAtDateTime();
159  }
160  if (column == DKIMManagerKeyWidget::ManagerKeyTreeWidget::LastUsedDate) {
161  return lastUsedDateTime() < static_cast<const DKIMManagerKeyTreeWidgetItem *>(&other)->lastUsedDateTime();
162  }
163  return QTreeWidgetItem::operator<(other);
164 }
165 
166 const QDateTime &DKIMManagerKeyTreeWidgetItem::storedAtDateTime() const
167 {
168  return mStoredAtDateTime;
169 }
170 
171 void DKIMManagerKeyTreeWidgetItem::setStoredAtDateTime(const QDateTime &newStoredAtDateTime)
172 {
173  mStoredAtDateTime = newStoredAtDateTime;
174 }
175 
176 const QDateTime &DKIMManagerKeyTreeWidgetItem::lastUsedDateTime() const
177 {
178  return mLastUsedDateTime;
179 }
180 
181 void DKIMManagerKeyTreeWidgetItem::setLastUsedDateTime(const QDateTime &newLastUsedDateTime)
182 {
183  mLastUsedDateTime = newLastUsedDateTime;
184 }
void setToolTip(int column, const QString &toolTip)
void customContextMenuRequested(const QPoint &pos)
The KeyInfo struct.
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)
KGuiItem cancel()
QHeaderView * header() const const
QClipboard * clipboard()
bool restoreState(const QByteArray &state)
void clear()
QString i18n(const char *text, const TYPE &arg...)
void setText(int column, const QString &text)
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))
QDateTime fromString(const QString &string, Qt::DateFormat format)
QPoint pos()
void setText(const QString &text, QClipboard::Mode mode)
ButtonCode questionTwoActions(QWidget *parent, const QString &text, const QString &title, const KGuiItem &primaryAction, const KGuiItem &secondaryAction, const QString &dontAskAgainName=QString(), Options options=Notify)
void reserve(int size)
QList< QTreeWidgetItem * > selectedItems() const const
QTreeWidgetItem * itemAt(const QPoint &p) const const
QString i18np(const char *singular, const char *plural, const TYPE &arg...)
void setSortingEnabled(bool enable)
KGuiItem del()
void setSortIndicatorShown(bool show)
QTreeWidgetItem * topLevelItem(int index) const const
virtual bool operator<(const QTreeWidgetItem &other) const const
QString text(int column) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Mar 26 2023 04:08:11 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.