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

Add support for groups of keys to FlatKeyListModel

GnuPG-bug-id: 5175, 5238
parent 6f4cfbeb
......@@ -105,6 +105,10 @@ endif()
add_subdirectory(src)
if (BUILD_TESTING)
add_subdirectory(autotests)
endif()
ecm_qt_install_logging_categories(
EXPORT LIBKLEO
FILE libkleo.categories
......
remove_definitions(-DQT_NO_CAST_FROM_ASCII)
include(ECMAddTests)
find_package(Qt5Test ${REQUIRED_QT_VERSION} CONFIG QUIET)
if(NOT Qt5Test_FOUND)
message(STATUS "Qt5Test not found, autotests will not be built.")
return()
endif()
ecm_add_tests(
flatkeylistmodeltest.cpp
LINK_LIBRARIES KF5::Libkleo Qt5::Test
)
/*
autotests/flatkeylistmodeltest.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 FlatKeyListModelTest: public QObject
{
Q_OBJECT
private Q_SLOTS:
void testCreation();
void testSetGroups();
void testClear();
};
using namespace Kleo;
using namespace GpgME;
void FlatKeyListModelTest::testCreation()
{
QScopedPointer<AbstractKeyListModel> model(AbstractKeyListModel::createFlatKeyListModel(this));
QCOMPARE( model->rowCount(), 0 );
}
void FlatKeyListModelTest::testSetGroups()
{
QScopedPointer<AbstractKeyListModel> model(AbstractKeyListModel::createFlatKeyListModel(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 FlatKeyListModelTest::testClear()
{
QScopedPointer<AbstractKeyListModel> model(AbstractKeyListModel::createFlatKeyListModel(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(FlatKeyListModelTest)
#include "flatkeylistmodeltest.moc"
......@@ -479,7 +479,7 @@ public:
int rowCount(const QModelIndex &pidx) const override
{
return pidx.isValid() ? 0 : mKeysByFingerprint.size();
return pidx.isValid() ? 0 : mKeysByFingerprint.size() + mGroups.size();
}
private:
......@@ -497,10 +497,14 @@ private:
if (types & Keys) {
mKeysByFingerprint.clear();
}
if (types & Groups) {
mGroups.clear();
}
}
private:
std::vector<Key> mKeysByFingerprint;
std::vector<KeyGroup> mGroups;
};
class HierarchicalKeyListModel : public AbstractKeyListModel
......@@ -566,10 +570,8 @@ static const char *cleanChainID(const Key &key)
}
FlatKeyListModel::FlatKeyListModel(QObject *p)
: TableModelMixin<AbstractKeyListModel>(p),
mKeysByFingerprint()
: TableModelMixin<AbstractKeyListModel>(p)
{
}
FlatKeyListModel::~FlatKeyListModel() {}
......@@ -643,19 +645,36 @@ void FlatKeyListModel::doRemoveKey(const Key &key)
KeyGroup FlatKeyListModel::doMapToGroup(const QModelIndex &idx) const
{
Q_ASSERT(!"not implemented");
return KeyGroup();
Q_ASSERT(idx.isValid());
if (static_cast<unsigned>(idx.row()) >= mKeysByFingerprint.size()
&& static_cast<unsigned>(idx.row()) < mKeysByFingerprint.size() + mGroups.size()
&& idx.column() < NumColumns) {
return mGroups[ idx.row() - mKeysByFingerprint.size() ];
} else {
return KeyGroup();
}
}
QModelIndex FlatKeyListModel::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() + mKeysByFingerprint.size(), column);
}
}
void FlatKeyListModel::doSetGroups(const std::vector<KeyGroup> &groups)
{
Q_ASSERT(!"not implemented");
Q_ASSERT(mGroups.empty()); // ensure that groups have been cleared
const int first = mKeysByFingerprint.size();
const int last = first + groups.size() - 1;
beginInsertRows(QModelIndex(), first, last);
mGroups = groups;
endInsertRows();
}
HierarchicalKeyListModel::HierarchicalKeyListModel(QObject *p)
......
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