File indexing completed on 2021-12-21 14:36:50

0001 /*
0002     SPDX-FileCopyrightText: 2002 Jean-Baptiste Mardelle <bj@altern.org>
0003     SPDX-FileCopyrightText: 2007, 2008, 2012, 2013 Rolf Eike Beer <kde@opensource.sf-tec.de>
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "groupedit.h"
0008 
0009 #include "kgpgsettings.h"
0010 #include "core/kgpgkey.h"
0011 #include "model/groupeditproxymodel.h"
0012 #include "model/kgpgitemmodel.h"
0013 
0014 #include <QIcon>
0015 #include <QHeaderView>
0016 #include <QSortFilterProxyModel>
0017 
0018 groupEdit::groupEdit(QWidget *parent, QList<KGpgNode *> *ids, KGpgItemModel *md)
0019     : QWidget(parent),
0020     m_outFilter(new QSortFilterProxyModel(this)),
0021     members(ids)
0022 {
0023     Q_ASSERT(ids != nullptr);
0024     Q_ASSERT(md != nullptr);
0025 
0026     setupUi( this );
0027     KgpgCore::KgpgKeyTrust mintrust;
0028     if (KGpgSettings::allowUntrustedGroupMembers()) {
0029         mintrust = KgpgCore::TRUST_UNDEFINED;
0030         textLabelAvailable->setText(i18n("Available Keys"));
0031     } else {
0032         mintrust = KgpgCore::TRUST_FULL;
0033         textLabelAvailable->setText(i18n("Available Trusted Keys"));
0034     }
0035 
0036     m_in = new GroupEditProxyModel(this, false, members, KgpgCore::TRUST_MINIMUM);
0037     m_in->setKeyModel(md);
0038     m_out = new GroupEditProxyModel(this, true, members, mintrust);
0039     m_out->setKeyModel(md);
0040 
0041     m_outFilter->setSourceModel(m_out);
0042     m_outFilter->setFilterCaseSensitivity(Qt::CaseInsensitive);
0043     m_outFilter->setFilterKeyColumn(-1);
0044 
0045     connect(filterEdit, &QLineEdit::textChanged, m_outFilter, &QSortFilterProxyModel::setFilterFixedString);
0046 
0047     availableKeys->setModel(m_outFilter);
0048     groupKeys->setModel(m_in);
0049     buttonAdd->setIcon(QIcon::fromTheme( QLatin1String( "go-down" )));
0050     buttonRemove->setIcon(QIcon::fromTheme( QLatin1String( "go-up" )));
0051 
0052     availableKeys->setColumnWidth(0, 200);
0053     availableKeys->setColumnWidth(1, 200);
0054     availableKeys->setColumnWidth(2, 100);
0055     availableKeys->verticalHeader()->hide();
0056 
0057     groupKeys->setColumnWidth(0, 200);
0058     groupKeys->setColumnWidth(1, 200);
0059     groupKeys->setColumnWidth(2, 100);
0060     groupKeys->verticalHeader()->hide();
0061 
0062     setMinimumSize(sizeHint());
0063 
0064     connect(buttonAdd, &QPushButton::clicked, this, QOverload<>::of(&groupEdit::groupAdd));
0065     connect(buttonRemove, &QPushButton::clicked, this, QOverload<>::of(&groupEdit::groupRemove));
0066     connect(availableKeys, &QTableView::doubleClicked, this, QOverload<const QModelIndex&>::of(&groupEdit::groupAdd));
0067     connect(groupKeys, &QTableView::doubleClicked, this, QOverload<const QModelIndex&>::of(&groupEdit::groupRemove));
0068 }
0069 
0070 groupEdit::~groupEdit()
0071 {
0072     delete m_in;
0073     delete m_out;
0074 }
0075 
0076 void
0077 groupEdit::groupAdd()
0078 {
0079     QModelIndexList sel = availableKeys->selectionModel()->selectedIndexes();
0080     for (int i = 0; i < sel.count(); i++) {
0081         if (sel.at(i).column() != 0)
0082             continue;
0083         KGpgNode *nd = m_out->nodeForIndex(m_outFilter->mapToSource(sel.at(i)));
0084         members->append(nd);
0085     }
0086     m_in->invalidate();
0087     m_out->invalidate();
0088 }
0089 
0090 void
0091 groupEdit::groupRemove()
0092 {
0093     Q_ASSERT(!members->isEmpty());
0094     QModelIndexList sel = groupKeys->selectionModel()->selectedIndexes();
0095     for (int i = 0; i < sel.count(); i++) {
0096         if (sel.at(i).column() != 0)
0097             continue;
0098         KGpgNode *nd = m_in->nodeForIndex(sel.at(i));
0099         for (int j = 0; j < members->count(); j++)
0100             if (nd->toKeyNode()->compareId(members->at(j)->getId())) {
0101                 members->removeAt(j);
0102                 break;
0103             }
0104     }
0105     m_in->invalidate();
0106     m_out->invalidate();
0107 }
0108 
0109 void
0110 groupEdit::groupAdd(const QModelIndex &index)
0111 {
0112     KGpgNode *nd = m_out->nodeForIndex(m_outFilter->mapToSource(index));
0113     members->append(nd);
0114     m_in->invalidate();
0115     m_out->invalidate();
0116 }
0117 
0118 void
0119 groupEdit::groupRemove(const QModelIndex &index)
0120 {
0121     Q_ASSERT(!members->isEmpty());
0122     KGpgKeyNode *nd = m_in->nodeForIndex(index)->toKeyNode();
0123     for (int i = 0; i < members->count(); i++)
0124         if (nd->compareId(members->at(i)->getId())) {
0125             members->removeAt(i);
0126             break;
0127         }
0128     m_in->invalidate();
0129     m_out->invalidate();
0130 }