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 BOOKMODEL_H
0023 #define BOOKMODEL_H
0024 
0025 #include <QAbstractListModel>
0026 /**
0027  * \brief Base Class to handle books, their pages and their metadata
0028  * 
0029  * BookModel is an QAbstractListModel, holding the pages as a list of objects.
0030  * 
0031  * It also holds metadata for the following entries as Q Properties:
0032  * 
0033  * - filename.
0034  * - author
0035  * - publisher
0036  * - title
0037  * - page count.
0038  * - current page.
0039  * - acbf data
0040  * - processing
0041  * 
0042  * The book model in turn is extended by ArchiveBookModel and FolderBookModel
0043  * to provide specialised functionality for archives(zip, rar, cbz, cbr) with
0044  * a book and Folders with a book and a description file.
0045  */
0046 class BookModel : public QAbstractListModel
0047 {
0048     Q_OBJECT
0049     /**
0050      * \brief The filename of the archive that describes this book.
0051      */
0052     Q_PROPERTY(QString filename READ filename WRITE setFilename NOTIFY filenameChanged)
0053     /**
0054      * \brief The main author of this book.
0055      */
0056     Q_PROPERTY(QString author READ author WRITE setAuthor NOTIFY authorChanged)
0057     /**
0058      * \brief the name of the publisher of this book.
0059      */
0060     Q_PROPERTY(QString publisher READ publisher WRITE setPublisher NOTIFY publisherChanged)
0061     /**
0062      * \brief The title of the book.
0063      */
0064     Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged)
0065     /**
0066      * \brief The page count of the book.
0067      */
0068     Q_PROPERTY(int pageCount READ pageCount NOTIFY pageCountChanged)
0069     /**
0070      * \brief The page currently being read of the book.
0071      */
0072     Q_PROPERTY(int currentPage READ currentPage WRITE setCurrentPage NOTIFY currentPageChanged)
0073     /**
0074      * The Advanced Comic Book Format data management instance associated with this book
0075      * This may be null
0076      */
0077     Q_PROPERTY(QObject* acbfData READ acbfData NOTIFY acbfDataChanged)
0078     /**
0079      * \brief Whether or not the book is still being processed.
0080      */
0081     Q_PROPERTY(bool processing READ processing WRITE setProcessing NOTIFY processingChanged)
0082     /**
0083      * \brief When processing, this string will commonly be set to something informational
0084      */
0085     Q_PROPERTY(QString processingDescription READ processingDescription WRITE setProcessingDescription NOTIFY processingDescriptionChanged)
0086 public:
0087     explicit BookModel(QObject* parent = nullptr);
0088     ~BookModel() override;
0089 
0090     /**
0091      * Extra roles for the page data access.
0092      */
0093     enum Roles {
0094         UrlRole = Qt::UserRole + 1, // This allows access to the resource location of the page.
0095         TitleRole, // This allows access to the title of the page, if it has one.
0096     };
0097 
0098     /**
0099      * \brief This gives names for the Roles enum.
0100      */
0101     QHash<int, QByteArray> roleNames() const override;
0102     /**
0103      * \brief Access the data inside the BookModel.
0104      * @param index The QModelIndex at which you wish to access the data.
0105      * @param role An enumerator of the type of data you want to access.
0106      * Is extended by the Roles enum.
0107      * 
0108      * @return a QVariant with the page data.
0109      */
0110     QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
0111     /**
0112      * @param parent The QModel index of the parent, not used here.
0113      * @returns the number of total pages there are in the Book.
0114      */
0115     int rowCount(const QModelIndex& parent = QModelIndex()) const override;
0116 
0117     /**
0118      * \brief add a page to this book.
0119      * @param url The resource location of the page as an url.
0120      * @param title The title of the page. This is shown in a table of contents.
0121      */
0122     virtual void addPage(QString url, QString title);
0123 
0124     /**
0125      * @brief removePage
0126      * Remove this page from the book.
0127      * @param index the index of the page to be removed.
0128      */
0129     virtual void removePage(int pageNumber);
0130 
0131     /**
0132      * \brief remove all pages from the book.
0133      */
0134     virtual void clearPages();
0135 
0136     /**
0137      * @return the filename of the file that describes this book.
0138      */
0139     QString filename() const;
0140     /**
0141      * \brief set the filename of the file that describes this book.
0142      */
0143     virtual void setFilename(QString newFilename);
0144     /**
0145      * \brief Fires when the filename is changed via setfilename.
0146      */
0147     Q_SIGNAL void filenameChanged();
0148     /**
0149      * @returns the main author of the book as a QString.
0150      */
0151     virtual QString author() const;
0152     /**
0153      * \brief set the main author of the book as a single string.
0154      * @param newAuthor The new name associated with the author
0155      * as a single string.
0156      */
0157     virtual void setAuthor(QString newAuthor);
0158     /**
0159      * \brief Fires when the author has changed via setAuthor.
0160      */
0161     Q_SIGNAL void authorChanged();
0162     /**
0163      * @return the name of the publisher as a QString.
0164      */
0165     virtual QString publisher() const;
0166     /**
0167      * \brief Set the name of the publisher.
0168      * @param newPublisher String that describes the publisher's name.
0169      */
0170     virtual void setPublisher(QString newPublisher);
0171     /**
0172      * \brief Fires when publisher's name has changed with setPublisher.
0173      */
0174     Q_SIGNAL void publisherChanged();
0175     /**
0176      * @return The proper title of the book as a Qstring.
0177      */
0178     virtual QString title() const;
0179     /**
0180      * \brief Set the title of the book.
0181      * @param newTitle A QString describing the new title.
0182      */
0183     virtual void setTitle(QString newTitle);
0184     /**
0185      * \brief Fires when the book's title has changed via SetTitle
0186      */
0187     Q_SIGNAL void titleChanged();
0188     /**
0189      * @return the total pages in the book as an int.
0190      */
0191     virtual int pageCount() const;
0192     /**
0193      * \brief Fires when the page count has changed, via for example pages
0194      * being added or removed.
0195      */
0196     Q_SIGNAL void pageCountChanged();
0197 
0198     /**
0199      * @return the number of the current page being viewed as an int.
0200      */
0201     int currentPage() const;
0202     /**
0203      * \brief Set the current page.
0204      * @param newCurrentPage Int with the index of the page to switch to.
0205      * @param updateFilesystem If this is set to false, the attributes do not get written back to the filesystem. Useful for when the information is first filled out
0206      */
0207     virtual void setCurrentPage(int newCurrentPage, bool updateFilesystem = true);
0208     /**
0209      * \brief Fires when the current page has changed.
0210      */
0211     Q_SIGNAL void currentPageChanged();
0212 
0213     /**
0214      * @return an object with the acbf data, might be null.
0215      */
0216     QObject* acbfData() const;
0217     /**
0218      * This is used by subclasses who want to create one such. Until this is called
0219      * with a valid object, acbfData is null. This function causes BookModel to take
0220      * ownership of the object. It will further delete any previous objects set as
0221      * acbfData.
0222      */
0223     void setAcbfData(QObject* obj);
0224     /**
0225      * \brief Fires when the ACBF data has changed.
0226      */
0227     Q_SIGNAL void acbfDataChanged();
0228 
0229     /**
0230      * @return Whether or not the any processing is currently going on
0231      */
0232     bool processing() const;
0233     /**
0234      * \brief Set whether it is processing or done.
0235      * @param processing Whether this model is being processed.
0236      */
0237     void setProcessing(bool processing);
0238     /**
0239      * \brief Fires when the state of processing has changed.
0240      */
0241     Q_SIGNAL void processingChanged();
0242     /**
0243      * \brief Fires when the book is done loading, and informs whether it was
0244      * successful.
0245      * @param success Whether the book's loading was successful
0246      * TODO: This isn't triggered by anything right now?
0247      */
0248     Q_SIGNAL void loadingCompleted(bool success);
0249 
0250     /**
0251      * \brief A human readable description of what is currently happening when processing is ongoing
0252      * \return The description of what's going on
0253      */
0254     QString processingDescription() const;
0255     /**
0256      * \brief Set the human readable description of what is currently happening
0257      * \param description The description of what's going on
0258      */
0259     void setProcessingDescription(const QString& description);
0260     /**
0261      * \brief Fires when the description of what's going on changes
0262      */
0263     Q_SIGNAL void processingDescriptionChanged();
0264 
0265     /**
0266      * @brief Swap the two pages at the specified indices
0267      * 
0268      * @param swapThisIndex The index of the first page to be swapped
0269      * @param withThisIndex The index of the page you want the first to be swapped with
0270      */
0271     Q_INVOKABLE virtual void swapPages(int swapThisIndex, int withThisIndex);
0272 private:
0273     class Private;
0274     Private* d;
0275 };
0276 
0277 #endif//BOOKMODEL_H