File indexing completed on 2025-03-09 04:54:30
0001 /* 0002 SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "dkimmanagerkeytreeview.h" 0008 #include "dkimmanagerkeymodel.h" 0009 #include "dkimmanagerkeyproxymodel.h" 0010 #include <KLocalizedString> 0011 #include <KMessageBox> 0012 0013 #include <QApplication> 0014 #include <QClipboard> 0015 #include <QMenu> 0016 0017 using namespace MessageViewer; 0018 DKIMManagerKeyTreeView::DKIMManagerKeyTreeView(QWidget *parent) 0019 : QTreeView(parent) 0020 , mManagerKeyProxyModel(new DKIMManagerKeyProxyModel(this)) 0021 { 0022 mManagerKeyProxyModel->setObjectName(QLatin1StringView("mManagerKeyProxyModel")); 0023 setRootIsDecorated(false); 0024 setAlternatingRowColors(true); 0025 setSelectionBehavior(QAbstractItemView::SelectRows); 0026 setSelectionMode(ExtendedSelection); 0027 setUniformRowHeights(true); 0028 setContextMenuPolicy(Qt::CustomContextMenu); 0029 setSortingEnabled(true); 0030 connect(this, &DKIMManagerKeyTreeView::customContextMenuRequested, this, &DKIMManagerKeyTreeView::slotCustomContextMenuRequested); 0031 } 0032 0033 DKIMManagerKeyTreeView::~DKIMManagerKeyTreeView() = default; 0034 0035 void DKIMManagerKeyTreeView::setFilterStr(const QString &str) 0036 { 0037 mManagerKeyProxyModel->setFilterText(str); 0038 } 0039 0040 QList<MessageViewer::KeyInfo> DKIMManagerKeyTreeView::keyInfos() const 0041 { 0042 return mManagerKeyModel ? mManagerKeyModel->keyInfos() : QList<MessageViewer::KeyInfo>(); 0043 } 0044 0045 void DKIMManagerKeyTreeView::clear() 0046 { 0047 if (mManagerKeyModel) { 0048 mManagerKeyModel->clear(); 0049 } 0050 } 0051 0052 void DKIMManagerKeyTreeView::setKeyModel(DKIMManagerKeyModel *model) 0053 { 0054 mManagerKeyModel = model; 0055 mManagerKeyProxyModel->setSourceModel(mManagerKeyModel); 0056 setModel(mManagerKeyProxyModel); 0057 } 0058 0059 void DKIMManagerKeyTreeView::slotCustomContextMenuRequested(const QPoint &pos) 0060 { 0061 const QModelIndex idx = indexAt(pos); 0062 const QModelIndex index = mManagerKeyProxyModel->mapToSource(idx); 0063 QMenu menu(this); 0064 const QModelIndexList selectedIndexes = selectionModel()->selectedRows(); 0065 const auto selectedItemCount{selectedIndexes.count()}; 0066 0067 if (index.isValid()) { 0068 if (selectedItemCount == 1) { 0069 menu.addAction(QIcon::fromTheme(QStringLiteral("edit-copy")), i18n("Copy Key"), this, [index, this]() { 0070 QApplication::clipboard()->setText(mManagerKeyModel->index(index.row()).data(DKIMManagerKeyModel::KeyRole).toString()); 0071 }); 0072 menu.addSeparator(); 0073 } 0074 0075 menu.addAction(QIcon::fromTheme(QStringLiteral("edit-delete")), 0076 i18np("Remove Key", "Remove Keys", selectedItemCount), 0077 this, 0078 [this, selectedItemCount]() { 0079 const int answer = 0080 KMessageBox::questionTwoActions(this, 0081 i18np("Do you want to delete this key?", "Do you want to delete these keys?", selectedItemCount), 0082 i18np("Delete Key", "Delete Keys", selectedItemCount), 0083 KStandardGuiItem::del(), 0084 KStandardGuiItem::cancel()); 0085 if (answer == KMessageBox::ButtonCode::PrimaryAction) { 0086 deleteSelectedItems(); 0087 } 0088 }); 0089 menu.addSeparator(); 0090 } 0091 if (mManagerKeyProxyModel->rowCount() > 0) { 0092 menu.addAction(i18n("Delete All"), this, [this]() { 0093 const int answer = KMessageBox::warningTwoActions(this, 0094 i18n("Do you want to delete all keys?"), 0095 i18nc("@title:window", "Delete Keys"), 0096 KStandardGuiItem::del(), 0097 KStandardGuiItem::cancel()); 0098 if (answer == KMessageBox::ButtonCode::PrimaryAction) { 0099 mManagerKeyModel->clear(); 0100 } 0101 }); 0102 } 0103 if (!menu.isEmpty()) { 0104 menu.exec(QCursor::pos()); 0105 } 0106 } 0107 0108 void DKIMManagerKeyTreeView::deleteSelectedItems() 0109 { 0110 const QModelIndexList selectedIndexes = selectionModel()->selectedRows(); 0111 if (selectedIndexes.isEmpty()) { 0112 return; 0113 } 0114 QStringList lst; 0115 for (const auto &index : selectedIndexes) { 0116 const auto info = mManagerKeyProxyModel->mapToSource(mManagerKeyProxyModel->index(index.row(), DKIMManagerKeyModel::KeyRole)); 0117 lst.append(info.data().toString()); 0118 } 0119 mManagerKeyModel->removeKeyInfos(lst); 0120 } 0121 0122 #include "moc_dkimmanagerkeytreeview.cpp"