File indexing completed on 2025-01-26 04:14:59

0001 /*
0002  * Copyright (C) 2015 Dan Leinir Turthra Jensen <admin@leinir.dk>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Lesser General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2.1 of the License, or (at your option) version 3, or any
0008  * later version accepted by the membership of KDE e.V. (or its
0009  * successor approved by the membership of KDE e.V.), which shall
0010  * act as a proxy defined in Section 6 of version 3 of the license.
0011  *
0012  * This library is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015  * Lesser General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU Lesser General Public
0018  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0019  *
0020  */
0021 
0022 #ifndef BOOKLISTMODEL_H
0023 #define BOOKLISTMODEL_H
0024 
0025 #include "CategoryEntriesModel.h"
0026 #include <QQmlParserStatus>
0027 /**
0028  * \brief Main catalogue model class.
0029  * 
0030  * BookListModel extends CategoryEntriesModel, and is the main model that
0031  * handles book entries and the different categories that books can be in.
0032  * 
0033  * It also extends QQmlParseStatus to ensure that the loading the cache of
0034  * books is postponed until the application UI has been painted at least once.
0035  * 
0036  * BookListModel keeps track of which books there are, how they can be sorted
0037  * and how far the reader is in reading a specific book.
0038  * 
0039  * It caches its entries in the BookDataBase.
0040  * 
0041  * ContentModel is the model used to enable searching the collection, it is
0042  * typically a ContentList.
0043  */
0044 class BookListModel : public CategoryEntriesModel, public QQmlParserStatus
0045 {
0046     Q_OBJECT
0047     /**
0048      * \brief count holds how many entries there are in the catalogue.
0049      */
0050     Q_PROPERTY(int count READ count NOTIFY countChanged)
0051     /**
0052      * \brief The content model is an abstract list model that holds data to search through.
0053      */
0054     Q_PROPERTY(QObject* contentModel READ contentModel WRITE setContentModel NOTIFY contentModelChanged)
0055     /**
0056      * \brief The "newly added" category entries model manages the newly added entries.
0057      */
0058     Q_PROPERTY(QObject* newlyAddedCategoryModel READ newlyAddedCategoryModel NOTIFY newlyAddedCategoryModelChanged)
0059     /**
0060      * \brief The "title" category entries model manages the sorting of entries by title.
0061      */
0062     Q_PROPERTY(QObject* titleCategoryModel READ titleCategoryModel NOTIFY titleCategoryModelChanged)
0063     /**
0064      * \brief The "author" category entries model manages the sorting of entries by author.
0065      */
0066     Q_PROPERTY(QObject* authorCategoryModel READ authorCategoryModel NOTIFY authorCategoryModelChanged)
0067     /**
0068      * \brief The "series" category entries model managed the sorting of entry by series.
0069      */
0070     Q_PROPERTY(QObject* seriesCategoryModel READ seriesCategoryModel NOTIFY seriesCategoryModelChanged)
0071     /**
0072      * \brief The "publisher" category entries model managed the sorting of entry by publisher.
0073      */
0074     Q_PROPERTY(QObject* publisherCategoryModel READ publisherCategoryModel NOTIFY publisherCategoryModelChanged)
0075     /**
0076      * \brief The "keyword" category entries model managed the sorting of entry by keyword.
0077      */
0078     Q_PROPERTY(QObject* keywordCategoryModel READ keywordCategoryModel NOTIFY keywordCategoryModelChanged)
0079     /**
0080      * \brief The "folder" category entries model managed the sorting of entry by file system folder.
0081      */
0082     Q_PROPERTY(QObject* folderCategoryModel READ folderCategoryModel NOTIFY folderCategoryModelChanged)
0083     /**
0084      * \brief cacheLoaded holds whether the database cache has been loaded..
0085      */
0086     Q_PROPERTY(bool cacheLoaded READ cacheLoaded NOTIFY cacheLoadedChanged)
0087     Q_ENUMS(Grouping)
0088     Q_INTERFACES(QQmlParserStatus)
0089 public:
0090     explicit BookListModel(QObject* parent = nullptr);
0091     ~BookListModel() override;
0092 
0093     /**
0094      * Inherited from QmlParserStatus, not implemented.
0095      */
0096     void classBegin() override {};
0097     /**
0098      * \brief triggers the loading of the cache.
0099      * Inherited from QmlParserStatus
0100      */
0101     void componentComplete() override;
0102 
0103     /**
0104      * \brief Enum holding the different categories implemented.
0105      */
0106     enum Grouping {
0107         GroupByNone = 0,
0108         GroupByRecentlyAdded,
0109         GroupByRecentlyRead,
0110         GroupByTitle,
0111         GroupByAuthor,
0112         GroupByPublisher
0113     };
0114 
0115     /**
0116      * @return the contentModel. Used for searching.
0117      */
0118     QObject* contentModel() const;
0119     /**
0120      * \brief set the ContentModel.
0121      * @param newModel The new content model.
0122      */
0123     void setContentModel(QObject* newModel);
0124     /**
0125      * \brief Fires when the content model has changed.
0126      */
0127     Q_SIGNAL void contentModelChanged();
0128 
0129     /**
0130      * @returns how many entries there are in the catalogue.
0131      */
0132     int count() const;
0133     /**
0134      * \brief Fires when the count has changed.
0135      */
0136     Q_SIGNAL void countChanged();
0137 
0138     /**
0139      * @return The categoryEntriesModel that manages the sorting of entries by title.
0140      */
0141     QObject* titleCategoryModel() const;
0142     /**
0143      * \brief Fires when the titleCategoryModel has changed or finished initializing.
0144      */
0145     Q_SIGNAL void titleCategoryModelChanged();
0146 
0147     /**
0148      * @return The categoryEntriesModel that manages the recently added entries.
0149      */
0150     QObject* newlyAddedCategoryModel() const;
0151     /**
0152      * \brief Fires when the newlyAddedCategoryModel has changed or finished initializing.
0153      */
0154     Q_SIGNAL void newlyAddedCategoryModelChanged();
0155 
0156     /**
0157      * @return The categoryEntriesModel that manages the sorting of entries by author.
0158      */
0159     QObject* authorCategoryModel() const;
0160     /**
0161      * \brief Fires when the authorCategoryModel has changed or finished initializing.
0162      */
0163     Q_SIGNAL void authorCategoryModelChanged();
0164 
0165     /**
0166      * @return The categoryEntriesModel that manages the sorting of entries by series.
0167      */
0168     QObject* seriesCategoryModel() const;
0169     /**
0170      * \brief Fires when the seriesCategoryModel has changed or finished initializing.
0171      */
0172     Q_SIGNAL void seriesCategoryModelChanged();
0173     
0174     /**
0175      * Returns the leaf model representing the series the entry with the passed URL is a part of
0176      * Base assumption: A book is only part of one series. This is not always true, but not sure how
0177      * to sensibly represent that.
0178      * 
0179      * @param fileName the File Name of the entry to get the series of.
0180      */
0181     Q_INVOKABLE QObject* seriesModelForEntry(QString fileName);
0182 
0183     /**
0184      * @return The categoryEntriesModel that manages the sorting of entries by publisher.
0185      */
0186     QObject* publisherCategoryModel() const;
0187     /**
0188      * \brief Fires when the publisherCategoryModel has changed or finished initializing.
0189      */
0190     Q_SIGNAL void publisherCategoryModelChanged();
0191     /**
0192      * @return The categoryEntriesModel that manages the sorting of entries by keywords, names and genres.
0193      */
0194     QObject* keywordCategoryModel() const;
0195     /**
0196      * \brief Fires when the keywordCategoryModel has changed or finished initializing.
0197      */
0198     Q_SIGNAL void keywordCategoryModelChanged();
0199     /**
0200      * @return The categoryEntriesModel that manages the sorting of entries by folder.
0201      */
0202     QObject* folderCategoryModel() const;
0203     /**
0204      * \brief Fires when the folderCategoryModel has changed or finished initializing.
0205      */
0206     Q_SIGNAL void folderCategoryModelChanged();
0207 
0208     /**
0209      * @returns whether the cache is loaded from the database.
0210      */
0211     bool cacheLoaded() const;
0212     /**
0213      * \brief Fires when the cache is done loading.
0214      */
0215     Q_SIGNAL void cacheLoadedChanged();
0216 
0217     /**
0218      * \brief Update the data of a book at runtime
0219      * 
0220      * This is used in to update totalPages and currentPage.
0221      * 
0222      * @param fileName The filename to update the page for.
0223      * @param property The property to update, can be "currentPage" or
0224      * "totalPages".
0225      * @param value The value to set it to.
0226      */
0227     Q_INVOKABLE void setBookData(QString fileName, QString property, QString value);
0228 
0229     /**
0230      * Delete a book from the model, and optionally delete the entry from file storage.
0231      * @param fileName The filename of the book to remove.
0232      * @param deleteFile Whether to also delete the file from the disk.
0233      */
0234     Q_INVOKABLE void removeBook(QString fileName, bool deleteFile = false);
0235 
0236     /**
0237      * \brief A list of the files currently known by the applications
0238      * @returns a QStringList with paths to known books.
0239      */
0240     Q_INVOKABLE QStringList knownBookFiles() const;
0241 private:
0242     class Private;
0243     Private* d;
0244 
0245     Q_SLOT void contentModelItemsInserted(QModelIndex index,int first, int last);
0246 };
0247 
0248 #endif//BOOKLISTMODEL_H