File indexing completed on 2024-04-21 05:50:40

0001 /*
0002     SPDX-FileCopyrightText: 2009-2022 Rolf Eike Beer <kde@opensource.sf-tec.de>
0003     SPDX-FileCopyrightText: 2016 David Zaslavsky <diazona@ellipsix.net>
0004 
0005     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006 */
0007 #ifndef KGPGSEARCHRESULTMODEL_H
0008 #define KGPGSEARCHRESULTMODEL_H
0009 
0010 #include <QAbstractItemModel>
0011 #include <QSortFilterProxyModel>
0012 #include <QStringList>
0013 
0014 class SearchResult;
0015 class KGpgSearchResultModelPrivate;
0016 
0017 /**
0018  * @brief A model to store the results of a keyserver search.
0019  *
0020  * This model parses and stores the results of a search on a keyserver.
0021  * It is never used directly, only by `KGpgSearchResultModel`.
0022  *
0023  * @author Rolf Eike Beer
0024  * @author David Zaslavsky
0025  */
0026 class KGpgSearchResultBackingModel : public QAbstractItemModel {
0027     // The moc complains if I put this class definition in the .cpp file
0028     Q_OBJECT
0029 public:
0030     explicit KGpgSearchResultBackingModel(QObject *parent = nullptr);
0031     ~KGpgSearchResultBackingModel() override;
0032 
0033     int columnCount(const QModelIndex &parent = QModelIndex()) const override;
0034     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0035     QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
0036     QModelIndex parent(const QModelIndex &index) const override;
0037     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0038     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
0039 
0040     /**
0041      * @brief get the key fingerprint for the given index
0042      * @param index valid index of any item in the model
0043      * @return fingerprint of the corresponding key
0044      */
0045     QString idForIndex(const QModelIndex &index) const;
0046 
0047     typedef enum {ROOT_LEVEL, KEY_LEVEL, ATTRIBUTE_LEVEL} NodeLevel;
0048 
0049     /**
0050      * @brief Returns the level corresponding to a `QModelIndex` associated
0051      * with this model.
0052      *
0053      * There are three levels of nodes. The top level, level 0, is the
0054      * root node. Each first-level subnode corresponds to a key, and each
0055      * second-level subnode corresponds to an attribute of that key: a UID,
0056      * or the number of UATs, or the summary description of the key.
0057      *
0058      * @param index a `QModelIndex` representing the position of a node in the model
0059      * @return the level of the node
0060      */
0061     static NodeLevel nodeLevel(const QModelIndex &index);
0062 
0063     /**
0064      * @brief Find the `SearchResult` associated with an index.
0065      *
0066      * This returns the `SearchResult` instance corresponding to an
0067      * index regardless of whether the index represents a key (first-level)
0068      * or an attribute (second-level). It also works for the root index,
0069      * for which it returns `nullptr`.
0070      */
0071     SearchResult *resultForIndex(const QModelIndex &index) const;
0072 
0073 public Q_SLOTS:
0074     void slotAddKey(const QStringList &lines);
0075 
0076 private:
0077     KGpgSearchResultModelPrivate * const d;
0078 };
0079 
0080 /**
0081  * @brief A model to parse, store, and display the results of
0082  * a keyserver search.
0083  *
0084  * This model manages the results returned by a keyserver search.
0085  * It is a proxy model, backed by a source model which parses
0086  * and stores the list of keys yielded by the search. The proxy
0087  * model exposes a sorted and/or filtered version of that list
0088  * to the view. On top of the sorting and regexp-based filtering
0089  * allowed by a basic `QSortFilterProxyModel`, this adds the
0090  * ability to filter out invalid keys.
0091  *
0092  * Unlike a generic `QSortFilterProxyModel`, this manages its
0093  * own source model internally. Don't set the source model with
0094  * `setSourceModel()` yourself.
0095  *
0096  * @author David Zaslavsky
0097  */
0098 class KGpgSearchResultModel : public QSortFilterProxyModel {
0099     Q_OBJECT
0100 public:
0101     explicit KGpgSearchResultModel(QObject *parent = nullptr);
0102     ~KGpgSearchResultModel() override = default;
0103 
0104     bool filterByValidity() const;
0105     bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
0106 
0107     /**
0108      * @brief get the key fingerprint for the given index
0109      * @param index valid index of any item in the model
0110      * @return fingerprint of the corresponding key
0111      */
0112     QString idForIndex(const QModelIndex &index) const;
0113 
0114     /**
0115      * @brief Return the total number of rows in the source model.
0116      */
0117     int sourceRowCount(const QModelIndex &parent = QModelIndex()) const;
0118 
0119     /**
0120      * Don't use this. The filter model manages its own source
0121      * internally. Use `resetSourceModel()` if you want to clear the
0122      * source model, and `slotAddKey()` to populate it.
0123      */
0124     void setSourceModel(QAbstractItemModel *sourceModel) override;
0125 
0126 public Q_SLOTS:
0127     /**
0128      * @brief Control whether validity filtering of keys is enabled.
0129      *
0130      * @param filter `true` to hide expired/revoked keys, `false` to show them
0131      */
0132     void setFilterByValidity(bool filter);
0133     /**
0134      * @brief Adds a key to the underlying source model.
0135      */
0136     void slotAddKey(const QStringList &lines);
0137     /**
0138      * @brief Resets the source model to be empty.
0139      */
0140     void resetSourceModel();
0141 
0142 private:
0143     bool m_filterByValidity;
0144 };
0145 
0146 #endif