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

Add support for groups of keys to KeyListModelInterface

GnuPG-bug-id: 5175, 5238
parent d3127adf
/* -*- mode: c++; c-basic-offset:4 -*-
models/keylistmodel.cpp
This file is part of Kleopatra, the KDE keymanager
This file is part of libkleopatra, the KDE keymanagement library
SPDX-FileCopyrightText: 2007 Klarälvdalens Datakonsult AB
SPDX-FileCopyrightText: 2021 g10 Code GmbH
SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
......@@ -11,6 +13,7 @@
#include "keycache.h"
#include "keylist.h"
#include "kleo/keygroup.h"
#include "kleo/predicates.h"
#include "kleo/keyfiltermanager.h"
#include "kleo/keyfilter.h"
......@@ -113,6 +116,15 @@ std::vector<Key> AbstractKeyListModel::keys(const QList<QModelIndex> &indexes) c
return result;
}
KeyGroup AbstractKeyListModel::group(const QModelIndex &idx) const
{
if (idx.isValid()) {
return doMapToGroup(idx);
} else {
return KeyGroup();
}
}
QModelIndex AbstractKeyListModel::index(const Key &key) const
{
return index(key, 0);
......@@ -139,6 +151,20 @@ QList<QModelIndex> AbstractKeyListModel::indexes(const std::vector<Key> &keys) c
return result;
}
QModelIndex AbstractKeyListModel::index(const KeyGroup &group) const
{
return index(group, 0);
}
QModelIndex AbstractKeyListModel::index(const KeyGroup &group, int col) const
{
if (group.isNull() || col < 0 || col >= NumColumns) {
return {};
} else {
return doMapFromGroup(group, col);
}
}
void AbstractKeyListModel::setKeys(const std::vector<Key> &keys)
{
clear();
......@@ -391,6 +417,10 @@ private:
QModelIndex doMapFromKey(const Key &key, int col) const override;
QList<QModelIndex> doAddKeys(const std::vector<Key> &keys) override;
void doRemoveKey(const Key &key) override;
KeyGroup doMapToGroup(const QModelIndex &index) const override;
QModelIndex doMapFromGroup(const KeyGroup &group, int column) const override;
void doClear() override {
mKeysByFingerprint.clear();
}
......@@ -421,6 +451,10 @@ private:
QModelIndex doMapFromKey(const Key &key, int col) const override;
QList<QModelIndex> doAddKeys(const std::vector<Key> &keys) override;
void doRemoveKey(const Key &key) override;
KeyGroup doMapToGroup(const QModelIndex &index) const override;
QModelIndex doMapFromGroup(const KeyGroup &group, int column) const override;
void doClear() override {
mTopLevels.clear();
mKeysByFingerprint.clear();
......@@ -529,6 +563,18 @@ void FlatKeyListModel::doRemoveKey(const Key &key)
endRemoveRows();
}
KeyGroup FlatKeyListModel::doMapToGroup(const QModelIndex &idx) const
{
Q_ASSERT(!"not implemented");
return KeyGroup();
}
QModelIndex FlatKeyListModel::doMapFromGroup(const KeyGroup &group, int column) const
{
Q_ASSERT(!"not implemented");
return QModelIndex();
}
HierarchicalKeyListModel::HierarchicalKeyListModel(QObject *p)
: AbstractKeyListModel(p),
mKeysByFingerprint(),
......@@ -941,6 +987,18 @@ void HierarchicalKeyListModel::doRemoveKey(const Key &key)
endRemoveRows();
}
KeyGroup HierarchicalKeyListModel::doMapToGroup(const QModelIndex &idx) const
{
Q_ASSERT(!"not implemented");
return KeyGroup();
}
QModelIndex HierarchicalKeyListModel::doMapFromGroup(const KeyGroup &group, int column) const
{
Q_ASSERT(!"not implemented");
return QModelIndex();
}
void AbstractKeyListModel::useKeyCache(bool value, bool secretOnly)
{
d->m_secretOnly = secretOnly;
......
/* -*- mode: c++; c-basic-offset:4 -*-
models/keylistmodel.h
This file is part of Kleopatra, the KDE keymanager
This file is part of libkleopatra, the KDE keymanagement library
SPDX-FileCopyrightText: 2007 Klarälvdalens Datakonsult AB
SPDX-FileCopyrightText: 2021 g10 Code GmbH
SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
......@@ -40,11 +42,16 @@ public:
GpgME::Key key(const QModelIndex &idx) const override;
std::vector<GpgME::Key> keys(const QList<QModelIndex> &indexes) const override;
KeyGroup group(const QModelIndex &idx) const override;
using QAbstractItemModel::index;
QModelIndex index(const GpgME::Key &key) const override;
QModelIndex index(const GpgME::Key &key, int col) const;
QList<QModelIndex> indexes(const std::vector<GpgME::Key> &keys) const override;
QModelIndex index(const KeyGroup &group) const override;
QModelIndex index(const KeyGroup &group, int col) const;
Q_SIGNALS:
void rowAboutToBeMoved(const QModelIndex &old_parent, int old_row);
void rowMoved(const QModelIndex &new_parent, int new_row);
......@@ -84,6 +91,10 @@ private:
virtual QModelIndex doMapFromKey(const GpgME::Key &key, int column) const = 0;
virtual QList<QModelIndex> doAddKeys(const std::vector<GpgME::Key> &keys) = 0;
virtual void doRemoveKey(const GpgME::Key &key) = 0;
virtual KeyGroup doMapToGroup(const QModelIndex &index) const = 0;
virtual QModelIndex doMapFromGroup(const KeyGroup &group, int column) const = 0;
virtual void doClear() = 0;
private:
......
/* -*- mode: c++; c-basic-offset:4 -*-
models/keylistmodelinterface.h
This file is part of Kleopatra, the KDE keymanager
This file is part of libkleopatra, the KDE keymanagement library
SPDX-FileCopyrightText: 2008 Klarälvdalens Datakonsult AB
SPDX-FileCopyrightText: 2021 g10 Code GmbH
SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
......@@ -24,6 +26,7 @@ template <typename T> class QList;
namespace Kleo
{
class KeyGroup;
class KLEO_EXPORT KeyListModelInterface
{
......@@ -35,6 +38,9 @@ public:
virtual QModelIndex index(const GpgME::Key &key) const = 0;
virtual QList<QModelIndex> indexes(const std::vector<GpgME::Key> &keys) const = 0;
virtual KeyGroup group(const QModelIndex &idx) const = 0;
virtual QModelIndex index(const KeyGroup &group) const = 0;
};
}
......
......@@ -12,6 +12,7 @@
#include "keylist.h"
#include "keylistmodel.h"
#include "kleo/keyfilter.h"
#include "kleo/keygroup.h"
#include "kleo/stl_util.h"
#include <libkleo_debug.h>
......@@ -75,13 +76,20 @@ std::vector<Key> AbstractKeyListSortFilterProxyModel::keys(const QList<QModelInd
return klmi->keys(mapped);
}
KeyGroup AbstractKeyListSortFilterProxyModel::group(const QModelIndex &idx) const
{
if (const KeyListModelInterface *const klmi = dynamic_cast<KeyListModelInterface *>(sourceModel())) {
return klmi->group(mapToSource(idx));
}
return KeyGroup();
}
QModelIndex AbstractKeyListSortFilterProxyModel::index(const Key &key) const
{
if (const KeyListModelInterface *const klmi = dynamic_cast<KeyListModelInterface *>(sourceModel())) {
return mapFromSource(klmi->index(key));
} else {
return {};
}
return {};
}
QList<QModelIndex> AbstractKeyListSortFilterProxyModel::indexes(const std::vector<Key> &keys) const
......@@ -96,9 +104,16 @@ QList<QModelIndex> AbstractKeyListSortFilterProxyModel::indexes(const std::vecto
return mapFromSource(idx);
});
return mapped;
} else {
return QList<QModelIndex>();
}
return QList<QModelIndex>();
}
QModelIndex AbstractKeyListSortFilterProxyModel::index(const Kleo::KeyGroup& group) const
{
if (const KeyListModelInterface *const klmi = dynamic_cast<KeyListModelInterface *>(sourceModel())) {
return mapFromSource(klmi->index(group));
}
return {};
}
class KeyListSortFilterProxyModel::Private
......@@ -148,7 +163,6 @@ void KeyListSortFilterProxyModel::setKeyFilter(const std::shared_ptr<const KeyFi
bool KeyListSortFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
{
//
// 0. Keep parents of matching children:
//
......@@ -169,6 +183,8 @@ bool KeyListSortFilterProxyModel::filterAcceptsRow(int source_row, const QModelI
const KeyListModelInterface *const klm = dynamic_cast<KeyListModelInterface *>(sourceModel());
Q_ASSERT(klm);
const Key key = klm->key(nameIndex);
const KeyGroup group = klm->group(nameIndex);
Q_ASSERT(!key.isNull() || !group.isNull());
if (col) {
const QModelIndex colIdx = sourceModel()->index(source_row, col, source_parent);
......@@ -176,7 +192,7 @@ bool KeyListSortFilterProxyModel::filterAcceptsRow(int source_row, const QModelI
if (!content.contains(rx)) {
return false;
}
} else {
} else if (!key.isNull()) {
// By default match against the full uid data (name / email / comment / dn)
bool match = false;
for (const auto &uid: key.userIDs()) {
......@@ -200,12 +216,18 @@ bool KeyListSortFilterProxyModel::filterAcceptsRow(int source_row, const QModelI
if (!match) {
return false;
}
} else if (!group.isNull()) {
if (!group.name().contains(rx)) {
return false;
}
} else {
return false;
}
//
// 2. Check that key filters match (if any are defined)
// 2. For keys check that key filters match (if any are defined)
//
if (d->keyFilter) { // avoid artifacts when no filters are defined
if (d->keyFilter && !key.isNull()) { // avoid artifacts when no filters are defined
return d->keyFilter->matches(key, KeyFilter::Filtering);
}
......
......@@ -43,10 +43,14 @@ public:
GpgME::Key key(const QModelIndex &idx) const override;
std::vector<GpgME::Key> keys(const QList<QModelIndex> &indexes) const override;
KeyGroup group(const QModelIndex &idx) const override;
using QAbstractItemModel::index;
QModelIndex index(const GpgME::Key &key) const override;
QList<QModelIndex> indexes(const std::vector<GpgME::Key> &keys) const override;
QModelIndex index(const KeyGroup &group) const override;
private:
void init();
};
......
......@@ -9,6 +9,8 @@
#include "keyrearrangecolumnsproxymodel.h"
#include "kleo/keygroup.h"
#include <gpgme++/key.h>
using namespace Kleo;
......@@ -43,6 +45,10 @@ std::vector<GpgME::Key> KeyRearrangeColumnsProxyModel::keys(const QList<QModelIn
return klm()->keys(srcIdxs);
}
KeyGroup KeyRearrangeColumnsProxyModel::group(const QModelIndex &idx) const
{
return klm()->group(mapToSource(idx));
}
QModelIndex KeyRearrangeColumnsProxyModel::index(const GpgME::Key &key) const
{
......@@ -60,6 +66,11 @@ QList<QModelIndex> KeyRearrangeColumnsProxyModel::indexes(const std::vector<GpgM
return myIdxs;
}
QModelIndex KeyRearrangeColumnsProxyModel::index(const KeyGroup &group) const
{
return mapFromSource(klm()->index(group));
}
void KeyRearrangeColumnsProxyModel::sort(int column, Qt::SortOrder order)
{
const auto fakeIdx = createIndex(0, column);
......
......@@ -28,11 +28,15 @@ public:
GpgME::Key key(const QModelIndex &idx) const override;
std::vector<GpgME::Key> keys(const QList<QModelIndex> &idxs) const override;
KeyGroup group(const QModelIndex &idx) const override;
using KRearrangeColumnsProxyModel::index;
QModelIndex index(const GpgME::Key &key) const override;
QList<QModelIndex> indexes(const std::vector<GpgME::Key> &keys) const override;
QModelIndex index(const KeyGroup &group) const override;
void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override;
private:
KeyListModelInterface *klm() const;
......
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