Commit f4709427 authored by Ingo Klöcker's avatar Ingo Klöcker
Browse files

Add support for groups of keys to HierarchicalKeyListModel

GnuPG-bug-id: 5175, 5238
parent 06efd641
......@@ -11,5 +11,6 @@ endif()
ecm_add_tests(
flatkeylistmodeltest.cpp
hierarchicalkeylistmodeltest.cpp
LINK_LIBRARIES KF5::Libkleo Qt5::Test
)
/*
autotests/hierarchicalkeylistmodeltest.cpp
This file is part of libkleopatra's test suite.
SPDX-FileCopyrightText: 2021 g10 Code GmbH
SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "kleo/keygroup.h"
#include "models/keylistmodel.h"
#include <gpgme++/key.h>
#include <QTest>
class HierarchicalKeyListModelTest: public QObject
{
Q_OBJECT
private Q_SLOTS:
void testCreation();
void testSetGroups();
void testClear();
};
using namespace Kleo;
using namespace GpgME;
void HierarchicalKeyListModelTest::testCreation()
{
QScopedPointer<AbstractKeyListModel> model(AbstractKeyListModel::createHierarchicalKeyListModel(this));
QCOMPARE( model->rowCount(), 0 );
}
void HierarchicalKeyListModelTest::testSetGroups()
{
QScopedPointer<AbstractKeyListModel> model(AbstractKeyListModel::createHierarchicalKeyListModel(this));
const std::vector<KeyGroup> groups = {
KeyGroup("test1", std::vector<Key>())
};
model->setGroups(groups);
QCOMPARE( model->rowCount(), 1 );
QVERIFY( model->index(groups[0]).isValid() );
const std::vector<KeyGroup> otherGroups = {
KeyGroup("test2", std::vector<Key>()),
KeyGroup("test3", std::vector<Key>())
};
model->setGroups(otherGroups);
QCOMPARE( model->rowCount(), 2 );
QVERIFY( model->index(otherGroups[0]).isValid() );
QVERIFY( model->index(otherGroups[1]).isValid() );
QVERIFY( !model->index(groups[0]).isValid() );
}
void HierarchicalKeyListModelTest::testClear()
{
QScopedPointer<AbstractKeyListModel> model(AbstractKeyListModel::createHierarchicalKeyListModel(this));
const KeyGroup group("test", std::vector<Key>());
model->setGroups({group});
model->clear(AbstractKeyListModel::Keys);
QCOMPARE( model->rowCount(), 1 );
model->clear(AbstractKeyListModel::Groups);
QCOMPARE( model->rowCount(), 0 );
}
QTEST_MAIN(HierarchicalKeyListModelTest)
#include "hierarchicalkeylistmodeltest.moc"
......@@ -542,6 +542,9 @@ private:
mKeysByExistingParent.clear();
mKeysByNonExistingParent.clear();
}
if (types & Groups) {
mGroups.clear();
}
}
private:
......@@ -554,6 +557,7 @@ private:
std::vector<Key> mKeysByFingerprint; // all keys
Map mKeysByExistingParent, mKeysByNonExistingParent; // parent->child map
std::vector<Key> mTopLevels; // all roots + parent-less
std::vector<KeyGroup> mGroups;
};
static const char *cleanChainID(const Key &key)
......@@ -694,7 +698,7 @@ int HierarchicalKeyListModel::rowCount(const QModelIndex &pidx) const
// toplevel item:
if (!pidx.isValid()) {
return mTopLevels.size();
return mTopLevels.size() + mGroups.size();
}
if (pidx.column() != 0) {
......@@ -1091,19 +1095,41 @@ void HierarchicalKeyListModel::doRemoveKey(const Key &key)
KeyGroup HierarchicalKeyListModel::doMapToGroup(const QModelIndex &idx) const
{
Q_ASSERT(!"not implemented");
return KeyGroup();
Q_ASSERT(idx.isValid());
if (idx.parent().isValid()) {
// groups are always top-level
return KeyGroup();
}
if (static_cast<unsigned>(idx.row()) >= mTopLevels.size()
&& static_cast<unsigned>(idx.row()) < mTopLevels.size() + mGroups.size()
&& idx.column() < NumColumns) {
return mGroups[ idx.row() - mTopLevels.size() ];
} else {
return KeyGroup();
}
}
QModelIndex HierarchicalKeyListModel::doMapFromGroup(const KeyGroup &group, int column) const
{
Q_ASSERT(!"not implemented");
return QModelIndex();
Q_ASSERT(!group.isNull());
const QString name = group.name();
const auto it = std::find_if(mGroups.begin(), mGroups.end(), [name](const KeyGroup &g) { return g.name() == name; });
if (it == mGroups.end()) {
return QModelIndex();
} else {
return createIndex(it - mGroups.begin() + mTopLevels.size(), column);
}
}
void HierarchicalKeyListModel::doSetGroups(const std::vector<KeyGroup> &groups)
{
Q_ASSERT(!"not implemented");
Q_ASSERT(mGroups.empty()); // ensure that groups have been cleared
const int first = mTopLevels.size();
const int last = first + groups.size() - 1;
beginInsertRows(QModelIndex(), first, last);
mGroups = groups;
endInsertRows();
}
void AbstractKeyListModel::useKeyCache(bool value, bool secretOnly)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment