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 CATEGORYENTRIESMODEL_H
0023 #define CATEGORYENTRIESMODEL_H
0024 
0025 #include <QAbstractListModel>
0026 #include <QDateTime>
0027 
0028 class CategoryEntriesModel;
0029 /**
0030  * \brief A struct for an Entry to the Book Database.
0031  */
0032 struct BookEntry {
0033     explicit BookEntry()
0034         : totalPages(0)
0035         , currentPage(0)
0036         , rating(0)
0037     {}
0038     QString filename;
0039     QString filetitle;
0040     QString title;
0041     QStringList genres;
0042     QStringList keywords;
0043     QStringList characters;
0044     QStringList series;
0045     QStringList seriesNumbers;
0046     QStringList seriesVolumes;
0047     QStringList author;
0048     QString publisher;
0049     QDateTime created;
0050     QDateTime lastOpenedTime;
0051     int totalPages;
0052     int currentPage;
0053     QString thumbnail;
0054     QStringList description;
0055     QString comment;
0056     QStringList tags;
0057     int rating;
0058 };
0059 
0060 /**
0061  * \brief Model to handle the filter categories.
0062  * 
0063  * This model in specific handles which categories there are
0064  * and which books are assigned to a category, if so, which.
0065  * 
0066  * Used to handle sorting by author, title and so forth.
0067  * Is extended by BookListModel.
0068  * 
0069  * categories and book entries are both in the same model
0070  * because there can be books that are not assigned categories.
0071  * Similarly, categories can contain categories, like in the case
0072  * of folder category.
0073  */
0074 class CategoryEntriesModel : public QAbstractListModel
0075 {
0076     Q_OBJECT
0077     /**
0078      * \brief count holds how many entries there are in the model - equivalent to rowCount, except as a property
0079      */
0080     Q_PROPERTY(int count READ count NOTIFY countChanged)
0081 public:
0082     explicit CategoryEntriesModel(QObject* parent = nullptr);
0083     ~CategoryEntriesModel() override;
0084 
0085     /**
0086      * \brief Extra roles for the book entry access.
0087      */
0088     enum Roles {
0089         UnknownRole = Qt::UserRole,
0090         FilenameRole = Qt::UserRole + 1, /// For getting a string with the full path to the book.
0091         FiletitleRole, /// For getting a string with the basename of the book.
0092         TitleRole, /// For getting a string with the proper title of the book.
0093         SeriesRole, /// For getting a stringlist of series this book is part of.
0094         SeriesNumbersRole, /// For getting a stringlist of numbers, which represent the sequence number the book has within each series.
0095         SeriesVolumesRole, /// For getting a stringlist of numbers, which represent the volume number the book has within a series. This is optional.
0096         AuthorRole, /// For getting a stringlist of all the authors.
0097         PublisherRole, /// For getting a string with the publisher name.
0098         CreatedRole, /// For getting the creation date of the book as a QDateTime.
0099         LastOpenedTimeRole, /// For getting the last time the book was opened as a QDateTime.
0100         TotalPagesRole, /// For getting the total amount of pages in this book.
0101         CurrentPageRole, /// For getting the current page as an int.
0102         CategoryEntriesModelRole, /// For getting the model of this category.
0103         CategoryEntryCountRole, /// For getting the an int with the number of books within this category.
0104         ThumbnailRole, /// For getting a thumbnail url for this book.
0105         DescriptionRole, /// For getting a stringlist with a book description.
0106         CommentRole, /// For getting a string with user assigned comment.
0107         TagsRole, /// For getting a stringlist with user assigned tags.
0108         RatingRole, /// For getting an int with the rating of the comic. This is gotten from KFileMeta and thus goes from 1-10 with 0 being no rating.
0109         GenreRole, /// For getting a stringlist with genres assigned to this book.
0110         KeywordRole, /// For getting a stringlist with keywords assigned to this book. Where tags are user assigned, keywords come from the book itself.
0111         CharacterRole /// For getting a stringlist with names of characters in this book.
0112     };
0113     Q_ENUMS(Roles)
0114 
0115     /**
0116      * @returns names for the extra roles defined.
0117      */
0118     QHash<int, QByteArray> roleNames() const override;
0119     /**
0120      * \brief Access the data inside the CategoryEntriesModel.
0121      * @param index The QModelIndex at which you wish to access the data.
0122      * @param role An enumerator of the type of data you want to access.
0123      * Is extended by the Roles enum.
0124      * 
0125      * @return a QVariant with the book entry's data.
0126      */
0127     QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
0128     /**
0129      * @param parent The QModel index of the parent. This only counts for
0130      * tree like page structures, and thus defaults to a freshly constructed
0131      * QModelIndex. A wellformed QModelIndex will cause this function to return 0
0132      * @returns the number of total rows(bookentries and categories) there are.
0133      */
0134     int rowCount(const QModelIndex& parent = QModelIndex()) const override;
0135 
0136     /**
0137      * @returns how many entries there are in the catalogue.
0138      */
0139     int count() const;
0140     /**
0141      * \brief Fires when the count has changed.
0142      */
0143     Q_SIGNAL void countChanged();
0144 
0145     /**
0146      * \brief Add a book entry to the CategoryEntriesModel.
0147      *
0148      * @param entry The BookEntry to add.
0149      * @param compareRole The role that determines the data to sort the entry into.
0150      * Defaults to the Book title.
0151      */
0152     Q_INVOKABLE void append(BookEntry* entry, Roles compareRole = TitleRole);
0153 
0154     /**
0155      * \brief Add a book entry to the model, using a fake book
0156      *
0157      * @param book The fake book (such as returned by get(int))
0158      * @param compareRole The role that determines the data used to sort the entry (defaults to TitleRole)
0159      */
0160     Q_INVOKABLE void appendFakeBook(QObject* book, Roles compareRole = TitleRole);
0161 
0162     /**
0163      * \brief Remove all entries from the model
0164      */
0165     Q_INVOKABLE void clear();
0166 
0167     /**
0168      * \brief Add a book entry to a category.
0169      * 
0170      * This also adds it to the model's list of entries.
0171      */
0172     void addCategoryEntry(const QString& categoryName, BookEntry* entry, Roles compareRole = TitleRole);
0173 
0174     /**
0175      * @param index an integer index pointing at the desired book.
0176      * @returns a QObject wrapper around a BookEntry struct for the given index.
0177      */
0178     Q_INVOKABLE QObject* get(int index);
0179     /**
0180      * @param index an integer index pointing at the desired book.
0181      * @returns the BookEntry struct for the given index (owned by this model, do not delete)
0182      */
0183     Q_INVOKABLE BookEntry* getBookEntry(int index);
0184     /**
0185      * TODO: This is backwards... need to fox this to make get return the actual thing, not just a book, and create a getter for books...
0186      * @return an entry object. This can be either a category or a book.
0187      * @param index the index of the object.
0188      */
0189     Q_INVOKABLE QObject* getEntry(int index);
0190     /**
0191      * @return an entry object for the given filename. Used to get the recently
0192      * read books.
0193      * @param filename the filename associated with an entry object.
0194      */
0195     Q_INVOKABLE QObject* bookFromFile(const QString &filename);
0196     /**
0197      * @return an entry index for the given filename.
0198      * @param filename the filename associated with an entry object.
0199      */
0200     Q_INVOKABLE int indexOfFile(const QString &filename);
0201     /**
0202      * @return whether the entry is a bookentry or a category entry.
0203      * @param index the index of the entry.
0204      */
0205     Q_INVOKABLE bool indexIsBook(int index);
0206     /**
0207      * @return an integer with the total books in the model.
0208      */
0209     int bookCount() const;
0210 
0211     /**
0212      * \brief Fires when a book entry is updated.
0213      * @param entry The updated entry
0214      * 
0215      * Used in the BookListModel::setBookData()
0216      */
0217     Q_SIGNAL void entryDataUpdated(BookEntry* entry);
0218     /**
0219      * \brief set a book entry as changed.
0220      * @param entry The changed entry.
0221      */
0222     Q_SLOT void entryDataChanged(BookEntry* entry);
0223     /**
0224      * \brief Fires when a book entry is removed.
0225      * @param entry The removed entry
0226      */ 
0227     Q_SIGNAL void entryRemoved(BookEntry* entry);
0228     /**
0229      * \brief Remove a book entry.
0230      * @param entry The entry to remove.
0231      */
0232     Q_SLOT void entryRemove(BookEntry* entry);
0233 
0234     // This will iterate over all sub-models and find the model which contains the entry, or null if not found
0235     QObject* leafModelForEntry(BookEntry* entry);
0236 protected:
0237     /**
0238      * @return the name of the model.
0239      */
0240     const QString &name() const;
0241     /**
0242      * \brief set the name of the model.
0243      * @param newName QString with the name.
0244      */
0245     void setName(const QString& newName);
0246 private:
0247     class Private;
0248     Private* d;
0249 };
0250 
0251 #endif//CATEGORYENTRIESMODEL_H