summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleix Pol <aleixpol@kde.org>2017-04-12 14:50:22 (GMT)
committerAleix Pol <aleixpol@kde.org>2017-04-12 14:50:22 (GMT)
commit1efd2873eace2ba23d2a00d717822579ef86fd19 (patch)
tree2ec435642912d965ef97aa4ec44b3f2e08594cf4
parent62a3b1f1c252eb56d33309b436a04c5df5de2505 (diff)
Fix crash
Make sure we don't crash when two ReviewsModel fetch reviews for the same resource.
-rw-r--r--libdiscover/ReviewsBackend/AbstractReviewsBackend.h5
-rw-r--r--libdiscover/ReviewsBackend/ReviewsModel.cpp14
-rw-r--r--libdiscover/ReviewsBackend/ReviewsModel.h6
-rw-r--r--libdiscover/backends/DummyBackend/DummyReviewsBackend.cpp6
-rw-r--r--libdiscover/backends/KNSBackend/KNSReviews.cpp6
-rw-r--r--libdiscover/backends/KNSBackend/tests/KNSBackendTest.cpp2
-rw-r--r--libdiscover/backends/KNSBackend/tests/KNSBackendTest.h6
7 files changed, 21 insertions, 24 deletions
diff --git a/libdiscover/ReviewsBackend/AbstractReviewsBackend.h b/libdiscover/ReviewsBackend/AbstractReviewsBackend.h
index bcece0c..cea830d 100644
--- a/libdiscover/ReviewsBackend/AbstractReviewsBackend.h
+++ b/libdiscover/ReviewsBackend/AbstractReviewsBackend.h
@@ -23,11 +23,10 @@
#include <QObject>
-#include "discovercommon_export.h"
+#include "ReviewsModel.h"
class Rating;
class AbstractResource;
-class Review;
class DISCOVERCOMMON_EXPORT AbstractReviewsBackend : public QObject
{
@@ -57,7 +56,7 @@ class DISCOVERCOMMON_EXPORT AbstractReviewsBackend : public QObject
virtual bool isReviewable() const;
Q_SIGNALS:
- void reviewsReady(AbstractResource *app, const QList<Review *> &);
+ void reviewsReady(AbstractResource *app, const QVector<ReviewPtr> &);
void loginStateChanged();
};
diff --git a/libdiscover/ReviewsBackend/ReviewsModel.cpp b/libdiscover/ReviewsBackend/ReviewsModel.cpp
index a674a12..61308c6 100644
--- a/libdiscover/ReviewsBackend/ReviewsModel.cpp
+++ b/libdiscover/ReviewsBackend/ReviewsModel.cpp
@@ -34,10 +34,7 @@ ReviewsModel::ReviewsModel(QObject* parent)
, m_canFetchMore(true)
{}
-ReviewsModel::~ReviewsModel()
-{
- qDeleteAll(m_reviews);
-}
+ReviewsModel::~ReviewsModel() = default;
QHash< int, QByteArray > ReviewsModel::roleNames() const
{
@@ -101,7 +98,6 @@ void ReviewsModel::setResource(AbstractResource* app)
{
if(m_app!=app) {
beginResetModel();
- qDeleteAll(m_reviews);
m_reviews.clear();
m_lastPage = 0;
endResetModel();
@@ -142,7 +138,7 @@ void ReviewsModel::fetchMore(const QModelIndex& parent)
// qDebug() << "fetching reviews... " << m_lastPage;
}
-void ReviewsModel::addReviews(AbstractResource* app, const QList<Review*>& reviews)
+void ReviewsModel::addReviews(AbstractResource* app, const QVector<ReviewPtr>& reviews)
{
if(app!=m_app)
return;
@@ -165,7 +161,7 @@ bool ReviewsModel::canFetchMore(const QModelIndex& /*parent*/) const
void ReviewsModel::markUseful(int row, bool useful)
{
- Review* r = m_reviews[row];
+ Review* r = m_reviews[row].data();
r->setUsefulChoice(useful ? Yes : No);
// qDebug() << "submitting usefulness" << r->applicationName() << r->id() << useful;
m_backend->submitUsefulness(r, useful);
@@ -175,12 +171,12 @@ void ReviewsModel::markUseful(int row, bool useful)
void ReviewsModel::deleteReview(int row)
{
- Review* r = m_reviews[row];
+ Review* r = m_reviews[row].data();
m_backend->deleteReview(r);
}
void ReviewsModel::flagReview(int row, const QString& reason, const QString& text)
{
- Review* r = m_reviews[row];
+ Review* r = m_reviews[row].data();
m_backend->flagReview(r, reason, text);
}
diff --git a/libdiscover/ReviewsBackend/ReviewsModel.h b/libdiscover/ReviewsBackend/ReviewsModel.h
index bd25cf2..e7bc39e 100644
--- a/libdiscover/ReviewsBackend/ReviewsModel.h
+++ b/libdiscover/ReviewsBackend/ReviewsModel.h
@@ -25,6 +25,8 @@
#include "discovercommon_export.h"
class Review;
+typedef QSharedPointer<Review> ReviewPtr;
+
class AbstractResource;
class AbstractReviewsBackend;
class DISCOVERCOMMON_EXPORT ReviewsModel : public QAbstractListModel
@@ -69,7 +71,7 @@ class DISCOVERCOMMON_EXPORT ReviewsModel : public QAbstractListModel
void markUseful(int row, bool useful);
private Q_SLOTS:
- void addReviews(AbstractResource* app, const QList<Review*>& reviews);
+ void addReviews(AbstractResource* app, const QVector<ReviewPtr>& reviews);
void restartFetching();
Q_SIGNALS:
@@ -79,7 +81,7 @@ class DISCOVERCOMMON_EXPORT ReviewsModel : public QAbstractListModel
private:
AbstractResource* m_app;
AbstractReviewsBackend* m_backend;
- QList<Review*> m_reviews;
+ QVector<ReviewPtr> m_reviews;
int m_lastPage;
bool m_canFetchMore;
};
diff --git a/libdiscover/backends/DummyBackend/DummyReviewsBackend.cpp b/libdiscover/backends/DummyBackend/DummyReviewsBackend.cpp
index bb0a9df..b804572 100644
--- a/libdiscover/backends/DummyBackend/DummyReviewsBackend.cpp
+++ b/libdiscover/backends/DummyBackend/DummyReviewsBackend.cpp
@@ -36,10 +36,10 @@ void DummyReviewsBackend::fetchReviews(AbstractResource* app, int page)
if (page>=5)
return;
- QList<Review*> review;
+ QVector<ReviewPtr> review;
for(int i=0; i<33; i++) {
- review += new Review(app->name(), app->packageName(), QStringLiteral("en_US"), QStringLiteral("good morning"), QStringLiteral("the morning is very good"), QStringLiteral("dummy"),
- QDateTime(), true, page+i, i%5, 1, 1, app->packageName());
+ review += ReviewPtr(new Review(app->name(), app->packageName(), QStringLiteral("en_US"), QStringLiteral("good morning"), QStringLiteral("the morning is very good"), QStringLiteral("dummy"),
+ QDateTime(), true, page+i, i%5, 1, 1, app->packageName()));
}
emit reviewsReady(app, review);
}
diff --git a/libdiscover/backends/KNSBackend/KNSReviews.cpp b/libdiscover/backends/KNSBackend/KNSReviews.cpp
index ae768b2..6b3130e 100644
--- a/libdiscover/backends/KNSBackend/KNSReviews.cpp
+++ b/libdiscover/backends/KNSBackend/KNSReviews.cpp
@@ -92,13 +92,13 @@ void KNSReviews::commentsReceived(Attica::BaseJob* j)
Attica::ListJob<Attica::Comment>* job = static_cast<Attica::ListJob<Attica::Comment>*>(j);
Attica::Comment::List comments = job->itemList();
- QList<Review*> reviews;
+ QVector<ReviewPtr> reviews;
AbstractResource* app = job->property("app").value<AbstractResource*>();
foreach(const Attica::Comment& comment, comments) {
//TODO: language lookup?
- Review* r = new Review(app->name(), app->packageName(), QStringLiteral("en"), comment.subject(), comment.text(), comment.user(),
+ ReviewPtr r(new Review(app->name(), app->packageName(), QStringLiteral("en"), comment.subject(), comment.text(), comment.user(),
comment.date(), true, comment.id().toInt(), comment.score()/10, 0, 0, QString()
- );
+ ));
reviews += r;
}
diff --git a/libdiscover/backends/KNSBackend/tests/KNSBackendTest.cpp b/libdiscover/backends/KNSBackend/tests/KNSBackendTest.cpp
index 69b3897..84b7e0f 100644
--- a/libdiscover/backends/KNSBackend/tests/KNSBackendTest.cpp
+++ b/libdiscover/backends/KNSBackend/tests/KNSBackendTest.cpp
@@ -125,7 +125,7 @@ void KNSBackendTest::testReviews()
QVERIFY(spy.count() || spy.wait());
}
-void KNSBackendTest::reviewsArrived(AbstractResource* r, const QList< Review* >& revs)
+void KNSBackendTest::reviewsArrived(AbstractResource* r, const QVector<ReviewPtr>& revs)
{
m_r = r;
m_revs = revs;
diff --git a/libdiscover/backends/KNSBackend/tests/KNSBackendTest.h b/libdiscover/backends/KNSBackend/tests/KNSBackendTest.h
index 17ad7af..38bf8f0 100644
--- a/libdiscover/backends/KNSBackend/tests/KNSBackendTest.h
+++ b/libdiscover/backends/KNSBackend/tests/KNSBackendTest.h
@@ -23,11 +23,11 @@
#include <QtCore/QObject>
#include <QtCore/QPointer>
+#include "ReviewsBackend/ReviewsModel.h"
class AbstractResourcesBackend;
class AbstractResource;
class ResultsStream;
-class Review;
class Category;
class KNSBackendTest : public QObject
@@ -42,14 +42,14 @@ class KNSBackendTest : public QObject
void testResourceByUrl();
public Q_SLOTS:
- void reviewsArrived(AbstractResource *r, const QList<Review *>& revs);
+ void reviewsArrived(AbstractResource *r, const QVector<ReviewPtr>& revs);
private:
QVector<AbstractResource*> getResources(ResultsStream* stream);
QVector<AbstractResource*> getAllResources(AbstractResourcesBackend* backend);
QPointer<AbstractResourcesBackend> m_backend;
QPointer<AbstractResource> m_r;
- QList<Review*> m_revs;
+ QVector<ReviewPtr> m_revs;
};
#endif // KNSBACKENDTEST_H