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
17using namespace MessageViewer;
18DKIMManagerKeyTreeView::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);
28 setContextMenuPolicy(Qt::CustomContextMenu);
29 setSortingEnabled(true);
30 connect(this, &DKIMManagerKeyTreeView::customContextMenuRequested, this, &DKIMManagerKeyTreeView::slotCustomContextMenuRequested);
31}
32
33DKIMManagerKeyTreeView::~DKIMManagerKeyTreeView() = default;
34
35void DKIMManagerKeyTreeView::setFilterStr(const QString &str)
36{
37 mManagerKeyProxyModel->setFilterText(str);
38}
39
40QList<MessageViewer::KeyInfo> DKIMManagerKeyTreeView::keyInfos() const
41{
42 return mManagerKeyModel ? mManagerKeyModel->keyInfos() : QList<MessageViewer::KeyInfo>();
43}
44
45void DKIMManagerKeyTreeView::clear()
46{
47 if (mManagerKeyModel) {
48 mManagerKeyModel->clear();
49 }
50}
51
52void DKIMManagerKeyTreeView::setKeyModel(DKIMManagerKeyModel *model)
53{
54 mManagerKeyModel = model;
55 mManagerKeyProxyModel->setSourceModel(mManagerKeyModel);
56 setModel(mManagerKeyProxyModel);
57}
58
59void 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
108void 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"
QString i18np(const char *singular, const char *plural, const TYPE &arg...)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
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))
ButtonCode questionTwoActions(QWidget *parent, const QString &text, const QString &title, const KGuiItem &primaryAction, const KGuiItem &secondaryAction, const QString &dontAskAgainName=QString(), Options options=Notify)
KGuiItem cancel()
KGuiItem del()
QAbstractItemModel * model() const const
QItemSelectionModel * selectionModel() const const
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
void setText(const QString &text, Mode mode)
QPoint pos()
QClipboard * clipboard()
QIcon fromTheme(const QString &name)
QModelIndexList selectedRows(int column) const const
void append(QList< T > &&value)
QVariant data(int role) const const
bool isValid() const const
int row() const const
void setObjectName(QAnyStringView name)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const const override
virtual int rowCount(const QModelIndex &parent) const const override
virtual void setSourceModel(QAbstractItemModel *sourceModel) override
CustomContextMenu
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
virtual QModelIndex indexAt(const QPoint &point) const const override
virtual QModelIndexList selectedIndexes() const const override
virtual void setModel(QAbstractItemModel *model) override
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 1 2024 18:49:04 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.