Messagelib

dkimmanagerkeytreeview.cpp
1 /*
2  SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "dkimmanagerkeytreeview.h"
8 #include "dkimmanagerkeymodel.h"
9 #include "dkimmanagerkeyproxymodel.h"
10 #include <KLocalizedString>
11 #include <KMessageBox>
12 
13 #include <QApplication>
14 #include <QClipboard>
15 #include <QMenu>
16 
17 using namespace MessageViewer;
18 DKIMManagerKeyTreeView::DKIMManagerKeyTreeView(QWidget *parent)
19  : QTreeView(parent)
20  , mManagerKeyProxyModel(new DKIMManagerKeyProxyModel(this))
21 {
22  mManagerKeyProxyModel->setObjectName(QLatin1StringView("mManagerKeyProxyModel"));
23  setRootIsDecorated(false);
24  setAlternatingRowColors(true);
25  setSelectionBehavior(QAbstractItemView::SelectRows);
26  setSelectionMode(ExtendedSelection);
27  setUniformRowHeights(true);
29  setSortingEnabled(true);
30  connect(this, &DKIMManagerKeyTreeView::customContextMenuRequested, this, &DKIMManagerKeyTreeView::slotCustomContextMenuRequested);
31 }
32 
33 DKIMManagerKeyTreeView::~DKIMManagerKeyTreeView() = default;
34 
35 void DKIMManagerKeyTreeView::setFilterStr(const QString &str)
36 {
37  mManagerKeyProxyModel->setFilterText(str);
38 }
39 
40 QList<MessageViewer::KeyInfo> DKIMManagerKeyTreeView::keyInfos() const
41 {
42  return mManagerKeyModel ? mManagerKeyModel->keyInfos() : QList<MessageViewer::KeyInfo>();
43 }
44 
45 void DKIMManagerKeyTreeView::clear()
46 {
47  if (mManagerKeyModel) {
48  mManagerKeyModel->clear();
49  }
50 }
51 
52 void DKIMManagerKeyTreeView::setKeyModel(DKIMManagerKeyModel *model)
53 {
54  mManagerKeyModel = model;
55  mManagerKeyProxyModel->setSourceModel(mManagerKeyModel);
56  setModel(mManagerKeyProxyModel);
57 }
58 
59 void DKIMManagerKeyTreeView::slotCustomContextMenuRequested(const QPoint &pos)
60 {
61  const QModelIndex idx = indexAt(pos);
62  const QModelIndex index = mManagerKeyProxyModel->mapToSource(idx);
63  QMenu menu(this);
64  const QModelIndexList selectedIndexes = selectionModel()->selectedRows();
65  const auto selectedItemCount{selectedIndexes.count()};
66 
67  if (index.isValid()) {
68  if (selectedItemCount == 1) {
69  menu.addAction(QIcon::fromTheme(QStringLiteral("edit-copy")), i18n("Copy Key"), this, [index, this]() {
70  QApplication::clipboard()->setText(mManagerKeyModel->index(index.row()).data(DKIMManagerKeyModel::KeyRole).toString());
71  });
72  menu.addSeparator();
73  }
74 
75  menu.addAction(QIcon::fromTheme(QStringLiteral("edit-delete")),
76  i18np("Remove Key", "Remove Keys", selectedItemCount),
77  this,
78  [this, selectedItemCount]() {
79  const int answer =
81  i18np("Do you want to delete this key?", "Do you want to delete these keys?", selectedItemCount),
82  i18np("Delete Key", "Delete Keys", selectedItemCount),
85  if (answer == KMessageBox::ButtonCode::PrimaryAction) {
86  deleteSelectedItems();
87  }
88  });
89  menu.addSeparator();
90  }
91  if (mManagerKeyProxyModel->rowCount() > 0) {
92  menu.addAction(i18n("Delete All"), this, [this]() {
93  const int answer = KMessageBox::warningTwoActions(this,
94  i18n("Do you want to delete all keys?"),
95  i18nc("@title:window", "Delete Keys"),
98  if (answer == KMessageBox::ButtonCode::PrimaryAction) {
99  mManagerKeyModel->clear();
100  }
101  });
102  }
103  if (!menu.isEmpty()) {
104  menu.exec(QCursor::pos());
105  }
106 }
107 
108 void DKIMManagerKeyTreeView::deleteSelectedItems()
109 {
110  const QModelIndexList selectedIndexes = selectionModel()->selectedRows();
111  if (selectedIndexes.isEmpty()) {
112  return;
113  }
114  QStringList lst;
115  for (const auto &index : selectedIndexes) {
116  const auto info = mManagerKeyProxyModel->mapToSource(mManagerKeyProxyModel->index(index.row(), DKIMManagerKeyModel::KeyRole));
117  lst.append(info.data().toString());
118  }
119  mManagerKeyModel->removeKeyInfos(lst);
120 }
121 
122 #include "moc_dkimmanagerkeytreeview.cpp"
void append(const T &value)
QIcon fromTheme(const QString &name)
CustomContextMenu
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
KGuiItem cancel()
QClipboard * clipboard()
QString i18n(const char *text, const TYPE &arg...)
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))
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)
bool isValid() const const
int row() const const
void setContextMenuPolicy(Qt::ContextMenuPolicy policy)
QString i18np(const char *singular, const char *plural, const TYPE &arg...)
KGuiItem del()
void setObjectName(const QString &name)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Thu Feb 15 2024 03:55:20 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.