File indexing completed on 2024-05-12 15:51:16

0001 // SPDX-FileCopyrightText: 2015 Dan Leinir Turthra Jensen <admin@leinir.dk>
0002 // SPDX-License-Identifier: LGPL-2.1-only or LGPL-3.0-only or LicenseRef-KDE-Accepted-LGPL
0003 
0004 #pragma once
0005 
0006 #include <QAbstractListModel>
0007 #include <QQmlListProperty>
0008 #include <QQmlParserStatus>
0009 
0010 #include "contentquery.h"
0011 
0012 /**
0013  * \brief Class to handle searching all the book entries.
0014  *
0015  * This class handles the queries and the contentlisterbase
0016  * that in turn handles the actual searching.
0017  *
0018  * When searching is done, the entries are filled into this model.
0019  */
0020 class ContentList : public QAbstractListModel, public QQmlParserStatus
0021 {
0022     Q_OBJECT
0023     Q_CLASSINFO("DefaultProperty", "queries")
0024     Q_INTERFACES(QQmlParserStatus)
0025     /**
0026      * \brief The list of query parameters that make up this search.
0027      */
0028     Q_PROPERTY(QQmlListProperty<ContentQuery> queries READ queries CONSTANT)
0029     /**
0030      * TODO: No idea.
0031      */
0032     Q_PROPERTY(bool autoSearch READ autoSearch WRITE setAutoSearch NOTIFY autoSearchChanged)
0033     /**
0034      * \brief Whether to cache the search results for later.
0035      */
0036     Q_PROPERTY(bool cacheResults READ cacheResults WRITE setCacheResults NOTIFY cacheResultsChanged)
0037 public:
0038     explicit ContentList(QObject *parent = nullptr);
0039     ~ContentList() override;
0040 
0041     /**
0042      * Extra roles for the different kinds of data that can be searched.
0043      */
0044     enum Roles {
0045         FilenameRole = Qt::UserRole + 1,
0046         FilePathRole,
0047         MetadataRole,
0048     };
0049 
0050     /**
0051      * @returns the list of search parameters as a list of content queries.
0052      */
0053     QQmlListProperty<ContentQuery> queries();
0054 
0055     /**
0056      * @return whether to do auto search.
0057      */
0058     bool autoSearch() const;
0059 
0060     /**
0061      * @return whether to cache the results.
0062      */
0063     bool cacheResults() const;
0064 
0065     /**
0066      * \brief QStrings with names for the extra roles.
0067      */
0068     QHash<int, QByteArray> roleNames() const override;
0069     /**
0070      * \brief Access the search results inside the model.
0071      * @param index The QModelIndex at which you wish to access the data.
0072      * @param role An enumerator of the type of data you want to access.
0073      * Is extended by the Roles enum.
0074      *
0075      * @return a QVariant with the search entry data.
0076      */
0077     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0078     /**
0079      * @param parent The QModel index of the parent. This only counts for
0080      * tree like page structures, and thus defaults to a freshly constructed
0081      * QModelIndex. A wellformed QModelIndex will cause this function to return 0
0082      * @returns the number of total rows(search results) there are in this model.
0083      */
0084     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0085 
0086     /**
0087      * Inherited from QmlParserStatus, not implemented.
0088      */
0089     void classBegin() override;
0090     /**
0091      * \brief loads the cached results or autosearch
0092      * into the model if either is enabled.
0093      *
0094      * Inherited from QmlParserStatus.
0095      */
0096     void componentComplete() override;
0097 
0098     /**
0099      * \brief Set whether to do autoSearch.
0100      * @param autoSearch whether to do autoSearch.
0101      */
0102     Q_SLOT void setAutoSearch(bool autoSearch);
0103     /**
0104      * \brief Set whether to cache the results.
0105      * @param cacheResults whether to cache the results.
0106      */
0107     Q_SLOT void setCacheResults(bool cacheResults);
0108 
0109     /**
0110      * \brief Fill the model with the results.
0111      *
0112      * This clears the model of search entries and then
0113      * fills it up with the new entries.
0114      *
0115      * @param results a stringlist with paths to the new
0116      * search results.
0117      */
0118     Q_SLOT void setKnownFiles(const QStringList &results);
0119     /**
0120      * \brief Start searching with the current queries list.
0121      */
0122     Q_SLOT void startSearch();
0123     /**
0124      * \brief Fires when a search starts
0125      */
0126     Q_SIGNAL void searchStarted();
0127 
0128     Q_SIGNAL void autoSearchChanged();
0129     Q_SIGNAL void cacheResultsChanged();
0130     /**
0131      * \brief Fires when the search is completed.
0132      */
0133     Q_SIGNAL void searchCompleted();
0134 
0135     Q_INVOKABLE static QString getMimetype(const QString &filePath);
0136 
0137     Q_INVOKABLE void addFile(const QUrl &filePath);
0138 
0139 private:
0140     bool isComplete() const;
0141     Q_SLOT void fileFound(const QString &filePath, const QVariantMap &metaData);
0142 
0143     class Private;
0144     std::unique_ptr<Private> d;
0145 };