File indexing completed on 2024-05-05 17:33:22

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 AbstractResource;
0017 class AbstractReviewsBackend;
0018 class DISCOVERCOMMON_EXPORT ReviewsModel : public QAbstractListModel
0019 {
0020     Q_OBJECT
0021     Q_PROPERTY(AbstractReviewsBackend *backend READ backend NOTIFY resourceChanged)
0022     Q_PROPERTY(AbstractResource *resource READ resource WRITE setResource NOTIFY resourceChanged)
0023     Q_PROPERTY(int count READ rowCount NOTIFY rowsChanged)
0024     Q_PROPERTY(bool fetching READ isFetching NOTIFY fetchingChanged)
0025 public:
0026     enum Roles {
0027         ShouldShow = Qt::UserRole + 1,
0028         Reviewer,
0029         CreationDate,
0030         UsefulnessTotal,
0031         UsefulnessFavorable,
0032         UsefulChoice,
0033         Rating,
0034         Summary,
0035         Depth,
0036         PackageVersion,
0037     };
0038     enum UserChoice {
0039         None,
0040         Yes,
0041         No,
0042     };
0043     Q_ENUM(UserChoice)
0044 
0045     explicit ReviewsModel(QObject *parent = nullptr);
0046     ~ReviewsModel() override;
0047     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0048     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0049 
0050     AbstractReviewsBackend *backend() const;
0051     void setResource(AbstractResource *app);
0052     AbstractResource *resource() const;
0053     void fetchMore(const QModelIndex &parent = QModelIndex()) override;
0054     bool canFetchMore(const QModelIndex & /*parent*/) const override;
0055     QHash<int, QByteArray> roleNames() const override;
0056     bool isFetching() const;
0057 
0058 public Q_SLOTS:
0059     void deleteReview(int row);
0060     void flagReview(int row, const QString &reason, const QString &text);
0061     void markUseful(int row, bool useful);
0062 
0063 private Q_SLOTS:
0064     void addReviews(AbstractResource *app, const QVector<ReviewPtr> &reviews, bool canFetchMore);
0065     void restartFetching();
0066 
0067 Q_SIGNALS:
0068     void rowsChanged();
0069     void resourceChanged();
0070     void fetchingChanged(bool fetching);
0071 
0072 private:
0073     AbstractResource *m_app = nullptr;
0074     AbstractReviewsBackend *m_backend = nullptr;
0075     QVector<ReviewPtr> m_reviews;
0076     int m_lastPage;
0077     bool m_canFetchMore = true;
0078 };