File indexing completed on 2024-12-01 04:36:53

0001 /*
0002    SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "myaccountpersonalaccesstokentreeview.h"
0008 #include "model/personalaccesstokeninfosmodel.h"
0009 
0010 #include <KLocalizedString>
0011 #include <KMessageBox>
0012 #include <QMenu>
0013 
0014 MyAccountPersonalAccessTokenTreeView::MyAccountPersonalAccessTokenTreeView(QWidget *parent)
0015     : QTreeView(parent)
0016 {
0017     setAlternatingRowColors(true);
0018     setSelectionMode(SingleSelection);
0019     setContextMenuPolicy(Qt::CustomContextMenu);
0020     setSelectionBehavior(QAbstractItemView::SelectRows);
0021     setRootIsDecorated(false);
0022     setSortingEnabled(true);
0023     connect(this,
0024             &MyAccountPersonalAccessTokenTreeView::customContextMenuRequested,
0025             this,
0026             &MyAccountPersonalAccessTokenTreeView::slotCustomContextMenuRequested);
0027 }
0028 
0029 MyAccountPersonalAccessTokenTreeView::~MyAccountPersonalAccessTokenTreeView() = default;
0030 
0031 void MyAccountPersonalAccessTokenTreeView::slotCustomContextMenuRequested(const QPoint &pos)
0032 {
0033     const QModelIndex index = indexAt(pos);
0034     QMenu menu(this);
0035     menu.addAction(QIcon::fromTheme(QStringLiteral("list-add")), i18n("Add..."), this, &MyAccountPersonalAccessTokenTreeView::createToken);
0036     if (index.isValid()) {
0037         menu.addAction(QIcon::fromTheme(QStringLiteral("view-refresh")), i18n("Regenerate..."), this, [this, index]() {
0038             const QModelIndex modelIndex = model()->index(index.row(), PersonalAccessTokenInfosModel::Name);
0039             Q_EMIT regenerateToken(modelIndex.data().toString());
0040         });
0041         menu.addSeparator();
0042         menu.addAction(QIcon::fromTheme(QStringLiteral("list-remove")), i18n("Remove"), this, [this, index]() {
0043             const QModelIndex modelIndex = model()->index(index.row(), PersonalAccessTokenInfosModel::Name);
0044             removeClicked(modelIndex.data().toString());
0045         });
0046     }
0047     menu.exec(viewport()->mapToGlobal(pos));
0048 }
0049 
0050 void MyAccountPersonalAccessTokenTreeView::removeClicked(const QString &tokenName)
0051 {
0052     if (KMessageBox::ButtonCode::PrimaryAction
0053         == KMessageBox::questionTwoActions(this,
0054                                            i18n("Are you sure that you want to delete \"%1\" Token?", tokenName),
0055                                            i18nc("@title:window", "Remove Token"),
0056                                            KStandardGuiItem::remove(),
0057                                            KStandardGuiItem::cancel())) {
0058         Q_EMIT removeToken(tokenName);
0059     }
0060 }
0061 
0062 #include "moc_myaccountpersonalaccesstokentreeview.cpp"