File indexing completed on 2024-04-28 05:34:17

0001 // SPDX-FileCopyrightText: 2018 Daniel Vrátil <dvratil@kde.org>
0002 //
0003 // SPDX-License-Identifier: LGPL-2.1-or-later
0004 
0005 #ifndef PASSWORDFILTERMODEL_H_
0006 #define PASSWORDFILTERMODEL_H_
0007 
0008 #include <QFuture>
0009 #include <QSortFilterProxyModel>
0010 #include <QTimer>
0011 #include <QVector>
0012 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0013 class QStringRef;
0014 #endif
0015 class KDescendantsProxyModel;
0016 
0017 namespace PlasmaPass
0018 {
0019 class PasswordFilterModel : public QSortFilterProxyModel
0020 {
0021     Q_OBJECT
0022 
0023     Q_PROPERTY(QString passwordFilter READ passwordFilter WRITE setPasswordFilter NOTIFY passwordFilterChanged)
0024 public:
0025     explicit PasswordFilterModel(QObject *parent = nullptr);
0026 
0027     void setSourceModel(QAbstractItemModel *sourceModel) override;
0028 
0029     QString passwordFilter() const;
0030     void setPasswordFilter(const QString &filter);
0031 
0032     QVariant data(const QModelIndex &index, int role) const override;
0033 
0034 Q_SIGNALS:
0035     void passwordFilterChanged();
0036 
0037 protected:
0038     bool lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const override;
0039     bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override;
0040 
0041 private:
0042     struct PathFilter {
0043         using result_type = std::pair<QModelIndex, int>;
0044 
0045         explicit PathFilter() = default;
0046         PathFilter(QString filter);
0047 
0048         PathFilter(const PathFilter &);
0049         PathFilter(PathFilter &&) noexcept;
0050         PathFilter &operator=(const PathFilter &);
0051         PathFilter &operator=(PathFilter &&) noexcept;
0052 
0053         result_type operator()(const QModelIndex &index) const;
0054 
0055         QString filter;
0056 
0057     private:
0058         void updateParts();
0059 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0060         QVector<QStringRef> mParts;
0061 #else
0062         QVector<QStringView> mParts;
0063 #endif
0064     };
0065 
0066     void delayedUpdateFilter();
0067 
0068     KDescendantsProxyModel *mFlatModel = nullptr;
0069     PathFilter mFilter;
0070     mutable QHash<QModelIndex, int> mSortingLookup;
0071     QTimer mUpdateTimer;
0072     QFuture<QHash<QModelIndex, int>> mFuture;
0073 };
0074 
0075 }
0076 
0077 #endif