1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
|
// SPDX-FileCopyrightText: 2018 Daniel Vrátil <dvratil@kde.org>
//
// SPDX-License-Identifier: LGPL-2.1-or-later
#include "passwordfiltermodel.h"
#include "abbreviations.h"
#include "passwordsmodel.h"
#include <KDescendantsProxyModel>
#include <QFutureWatcher>
#include <QtConcurrent>
#include <chrono>
#include <iterator>
using namespace PlasmaPass;
namespace
{
constexpr const auto invalidateDelay = std::chrono::milliseconds(100);
constexpr const char *newFilterProperty = "newFilter";
class ModelIterator
{
public:
using reference = const QModelIndex &;
using pointer = QModelIndex *;
using value_type = QModelIndex;
using difference_type = int;
using iterator_category = std::forward_iterator_tag;
static ModelIterator begin(QAbstractItemModel *model)
{
return ModelIterator{model, model->index(0, 0)};
}
static ModelIterator end(QAbstractItemModel *model)
{
return ModelIterator{model, {}};
}
bool operator==(const ModelIterator &other) const
{
return mModel == other.mModel && mIndex == other.mIndex;
}
bool operator!=(const ModelIterator &other) const
{
return !(*this == other);
}
QModelIndex operator*() const
{
return mIndex;
}
const QModelIndex *operator->() const
{
return &mIndex;
}
ModelIterator &operator++()
{
if (mIndex.row() < mModel->rowCount() - 1) {
mIndex = mModel->index(mIndex.row() + 1, mIndex.column());
} else {
mIndex = {};
}
return *this;
}
ModelIterator operator++(int)
{
ModelIterator it = *this;
++*this;
return it;
}
private:
ModelIterator(QAbstractItemModel *model, const QModelIndex &index)
: mModel(model)
, mIndex(index)
{
}
private:
QAbstractItemModel *mModel = nullptr;
QModelIndex mIndex;
};
} // namespace
PasswordFilterModel::PathFilter::PathFilter(QString filter)
: filter(std::move(filter))
{
}
PasswordFilterModel::PathFilter::PathFilter(const PathFilter &other)
: filter(other.filter)
{
updateParts();
}
PasswordFilterModel::PathFilter &PasswordFilterModel::PathFilter::operator=(const PathFilter &other)
{
filter = other.filter;
updateParts();
return *this;
}
PasswordFilterModel::PathFilter::PathFilter(PathFilter &&other) noexcept
: filter(std::move(other.filter))
{
updateParts();
}
PasswordFilterModel::PathFilter &PasswordFilterModel::PathFilter::operator=(PathFilter &&other) noexcept
{
filter = std::move(other.filter);
updateParts();
return *this;
}
void PasswordFilterModel::PathFilter::updateParts()
{
mParts = QStringView(filter).split(QLatin1Char('/'), Qt::SkipEmptyParts);
}
PasswordFilterModel::PathFilter::result_type PasswordFilterModel::PathFilter::operator()(const QModelIndex &index) const
{
const auto path = index.model()->data(index, PasswordsModel::FullNameRole).toString();
const auto weight = matchPathFilter(QStringView(path).split(QLatin1Char('/')), mParts);
return std::make_pair(index, weight);
}
PasswordFilterModel::PasswordFilterModel(QObject *parent)
: QSortFilterProxyModel(parent)
, mFlatModel(new KDescendantsProxyModel(this))
{
mFlatModel->setDisplayAncestorData(false);
sort(0); // enable sorting
mUpdateTimer.setSingleShot(true);
connect(&mUpdateTimer, &QTimer::timeout, this, &PasswordFilterModel::delayedUpdateFilter);
connect(&mUpdateTimer, &QTimer::timeout, this, []() {
qDebug() << "Update timer timeout, will calculate results lazily.";
});
}
void PasswordFilterModel::setSourceModel(QAbstractItemModel *sourceModel)
{
mFlatModel->setSourceModel(sourceModel);
if (this->sourceModel() == nullptr) {
QSortFilterProxyModel::setSourceModel(mFlatModel);
}
}
QString PasswordFilterModel::passwordFilter() const
{
return mFilter.filter;
}
void PasswordFilterModel::setPasswordFilter(const QString &filter)
{
if (mFilter.filter != filter) {
if (mUpdateTimer.isActive()) {
mUpdateTimer.stop();
}
mUpdateTimer.setProperty(newFilterProperty, filter);
mUpdateTimer.start(invalidateDelay);
if (mFuture.isRunning()) {
mFuture.cancel();
}
if (!filter.isEmpty()) {
mFuture = QtConcurrent::mappedReduced<QHash<QModelIndex, int>>(ModelIterator::begin(sourceModel()),
ModelIterator::end(sourceModel()),
PathFilter{filter},
[](QHash<QModelIndex, int> &result, const std::pair<QModelIndex, int> &value) {
result.insert(value.first, value.second);
});
auto watcher = new QFutureWatcher<QHash<QModelIndex, int>>();
connect(watcher, &QFutureWatcherBase::finished, this, [this, watcher]() {
mSortingLookup = mFuture.result();
watcher->deleteLater();
// If the timer is not active it means we were to slow, so don't invoke
// delayedUpdateFilter() again, just update mSortingLookup with our
// results.
if (mUpdateTimer.isActive()) {
mUpdateTimer.stop();
delayedUpdateFilter();
}
});
connect(watcher, &QFutureWatcherBase::canceled, watcher, &QObject::deleteLater);
watcher->setFuture(mFuture);
}
}
}
void PasswordFilterModel::delayedUpdateFilter()
{
mFilter = PathFilter(mUpdateTimer.property(newFilterProperty).toString());
Q_EMIT passwordFilterChanged();
if (mFuture.isRunning()) {
// If the future is still running, clear up the lookup table, we will have to
// calculate the intermediate results ourselves
mSortingLookup.clear();
}
invalidate();
}
QVariant PasswordFilterModel::data(const QModelIndex &index, int role) const
{
if (role == Qt::DisplayRole) {
return data(index, PasswordsModel::FullNameRole);
}
return QSortFilterProxyModel::data(index, role);
}
bool PasswordFilterModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
{
const auto src_index = sourceModel()->index(source_row, 0, source_parent);
const auto type = static_cast<PasswordsModel::EntryType>(sourceModel()->data(src_index, PasswordsModel::EntryTypeRole).toInt());
if (type == PasswordsModel::FolderEntry) {
return false;
}
if (mFilter.filter.isEmpty()) {
return true;
}
// Try to lookup the weight in the lookup table, the worker thread may have put it in there
// while the updateTimer was ticking
auto weight = mSortingLookup.find(src_index);
if (weight == mSortingLookup.end()) {
// It's not there, let's calculate the value now
const auto result = mFilter(src_index);
weight = mSortingLookup.insert(result.first, result.second);
}
return *weight > -1;
}
bool PasswordFilterModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
{
const auto weightLeft = mSortingLookup.value(source_left, -1);
const auto weightRight = mSortingLookup.value(source_right, -1);
if (weightLeft == weightRight) {
const auto nameLeft = source_left.data(PasswordsModel::FullNameRole).toString();
const auto nameRight = source_right.data(PasswordsModel::FullNameRole).toString();
return QString::localeAwareCompare(nameLeft, nameRight) < 0;
}
return weightLeft < weightRight;
}
#include "moc_passwordfiltermodel.cpp"
|