File indexing completed on 2024-05-19 09:21:26

0001 /*
0002  *   SPDX-FileCopyrightText: 2012 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
0003  *
0004  *   SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #pragma once
0008 
0009 #include "discovercommon_export.h"
0010 #include <QModelIndex>
0011 #include <QSharedPointer>
0012 
0013 class Review;
0014 typedef QSharedPointer<Review> ReviewPtr;
0015 
0016 class StarsCount
0017 {
0018     Q_GADGET
0019     Q_PROPERTY(int one READ one CONSTANT)
0020     Q_PROPERTY(int two READ two CONSTANT)
0021     Q_PROPERTY(int three READ three CONSTANT)
0022     Q_PROPERTY(int four READ four CONSTANT)
0023     Q_PROPERTY(int five READ five CONSTANT)
0024 public:
0025     int one() const;
0026     int two() const;
0027     int three() const;
0028     int four() const;
0029     int five() const;
0030 
0031     void addRating(int rating);
0032     void clear();
0033 
0034 private:
0035     int m_one = 0;
0036     int m_two = 0;
0037     int m_three = 0;
0038     int m_four = 0;
0039     int m_five = 0;
0040 };
0041 
0042 class AbstractResource;
0043 class AbstractReviewsBackend;
0044 class DISCOVERCOMMON_EXPORT ReviewsModel : public QAbstractListModel
0045 {
0046     Q_OBJECT
0047     Q_PROPERTY(AbstractReviewsBackend *backend READ backend NOTIFY resourceChanged)
0048     Q_PROPERTY(AbstractResource *resource READ resource WRITE setResource NOTIFY resourceChanged)
0049     Q_PROPERTY(int count READ rowCount NOTIFY rowsChanged)
0050     Q_PROPERTY(StarsCount starsCount READ starsCount NOTIFY rowsChanged)
0051     Q_PROPERTY(bool fetching READ isFetching NOTIFY fetchingChanged)
0052     Q_PROPERTY(QString preferredSortRole READ preferredSortRole WRITE setPreferredSortRole NOTIFY preferredSortRoleChanged)
0053 public:
0054     enum Roles {
0055         ShouldShow = Qt::UserRole + 1,
0056         Reviewer,
0057         CreationDate,
0058         UsefulnessTotal,
0059         UsefulnessFavorable,
0060         WilsonScore,
0061         UsefulChoice,
0062         Rating,
0063         Summary,
0064         Depth,
0065         PackageVersion,
0066     };
0067     enum UserChoice {
0068         None,
0069         Yes,
0070         No,
0071     };
0072     Q_ENUM(UserChoice)
0073 
0074     explicit ReviewsModel(QObject *parent = nullptr);
0075     ~ReviewsModel() override;
0076     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0077     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0078 
0079     QString preferredSortRole() const;
0080     void setPreferredSortRole(const QString &sorting);
0081 
0082     AbstractReviewsBackend *backend() const;
0083     void setResource(AbstractResource *app);
0084     AbstractResource *resource() const;
0085     void fetchMore(const QModelIndex &parent = QModelIndex()) override;
0086     bool canFetchMore(const QModelIndex & /*parent*/) const override;
0087     QHash<int, QByteArray> roleNames() const override;
0088     StarsCount starsCount() const;
0089     bool isFetching() const;
0090 
0091 public Q_SLOTS:
0092     void deleteReview(int row);
0093     void flagReview(int row, const QString &reason, const QString &text);
0094     void markUseful(int row, bool useful);
0095 
0096 private Q_SLOTS:
0097     void addReviews(AbstractResource *app, const QVector<ReviewPtr> &reviews, bool canFetchMore);
0098     void restartFetching();
0099 
0100 Q_SIGNALS:
0101     void rowsChanged();
0102     void resourceChanged();
0103     void fetchingChanged(bool fetching);
0104     void preferredSortRoleChanged();
0105 
0106 private:
0107     AbstractResource *m_app = nullptr;
0108     AbstractReviewsBackend *m_backend = nullptr;
0109     QVector<ReviewPtr> m_reviews;
0110     QString m_preferredSortRole;
0111     StarsCount m_starsCount;
0112     int m_lastPage;
0113     bool m_canFetchMore = true;
0114 };