Commit ee3d6e9c authored by Bernardo Gomes Negri's avatar Bernardo Gomes Negri Committed by Aleix Pol Gonzalez
Browse files

Add option to change name in review dialog.

I only needed to change significantly the ODRS backend, since KNS appears to handle user names separately.

BUG: 405935
parent 14cc791d
Pipeline #225501 passed with stage
in 1 minute and 29 seconds
......@@ -10,6 +10,7 @@ Kirigami.OverlaySheet
property QtObject application
readonly property alias rating: ratingInput.rating
readonly property alias name: nameInput.text
readonly property alias summary: titleInput.text
readonly property alias review: reviewInput.text
property QtObject backend: null
......@@ -28,12 +29,13 @@ Kirigami.OverlaySheet
Kirigami.FormData.label: i18n("Rating:")
editable: true
}
QQC2.Label {
QQC2.TextField {
id: nameInput
Kirigami.FormData.label: i18n("Name:")
visible: page.reviewsBackend !== null && reviewDialog.backend.userName.length > 0
visible: page.reviewsBackend !== null && reviewDialog.backend.preferredUserName.length > 0
Layout.fillWidth: true
elide: Text.ElideRight
text: visible ? reviewDialog.backend.userName : ""
readOnly: !reviewDialog.backend.supportsNameChange
text: visible ? reviewDialog.backend.preferredUserName : ""
}
QQC2.TextField {
id: titleInput
......@@ -60,6 +62,7 @@ Kirigami.OverlaySheet
if (reviewInput.length === 0) return i18n("Write the review");
if (reviewInput.length < 15) return i18n("Keep writing…");
if (reviewInput.length > 3000) return i18n("Too long!");
if (nameInput.length < 1) return i18nc("@info:usagetip", "Insert a name");
return "";
}
wrapMode: Text.WordWrap
......
......@@ -25,7 +25,7 @@ Kirigami.OverlaySheet {
application: page.resource
backend: page.reviewsBackend
onAccepted: backend.submitReview(resource, summary, review, rating)
onAccepted: backend.submitReview(resource, summary, review, rating, name)
}
function openReviewDialog() {
......
......@@ -4,6 +4,8 @@
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#include <KConfigGroup>
#include <KSharedConfig>
#include "AbstractReviewsBackend.h"
AbstractReviewsBackend::AbstractReviewsBackend(QObject *parent)
......@@ -16,6 +18,34 @@ bool AbstractReviewsBackend::isReviewable() const
return true;
}
bool AbstractReviewsBackend::supportsNameChange() const
{
return false;
}
QString AbstractReviewsBackend::preferredUserName() const
{
if (!supportsNameChange()) {
return userName();
} else {
auto config = KSharedConfig::openConfig();
auto configGroup = KConfigGroup(config, "Identity");
const QString configName = configGroup.readEntry("Name", QString());
return configName.isEmpty() ? userName() : configName;
}
}
void AbstractReviewsBackend::submitReview(AbstractResource *app, const QString &summary, const QString &review_text, const QString &rating, const QString &userName)
{
if (supportsNameChange()) {
auto config = KSharedConfig::openConfig();
auto configGroup = KConfigGroup(config, "Identity");
configGroup.writeEntry("Name", userName);
configGroup.config()->sync();
}
sendReview(app, summary, review_text, rating, userName);
}
QString AbstractReviewsBackend::errorMessage() const
{
return QString();
......
......@@ -18,12 +18,12 @@ class DISCOVERCOMMON_EXPORT AbstractReviewsBackend : public QObject
{
Q_OBJECT
Q_PROPERTY(bool isReviewable READ isReviewable CONSTANT)
Q_PROPERTY(bool hasCredentials READ hasCredentials NOTIFY loginStateChanged)
Q_PROPERTY(QString userName READ userName NOTIFY loginStateChanged)
Q_PROPERTY(bool supportsNameChange READ supportsNameChange CONSTANT)
Q_PROPERTY(bool hasCredentials READ hasCredentials)
Q_PROPERTY(QString preferredUserName READ preferredUserName)
public:
explicit AbstractReviewsBackend(QObject *parent = nullptr);
virtual QString userName() const = 0;
virtual bool hasCredentials() const = 0;
Q_SCRIPTABLE virtual Rating *ratingForApplication(AbstractResource *app) const = 0;
......@@ -31,22 +31,33 @@ public:
Q_INVOKABLE virtual bool isResourceSupported(AbstractResource *res) const = 0;
virtual bool isFetching() const = 0;
virtual bool isReviewable() const;
virtual bool supportsNameChange() const;
public Q_SLOTS:
virtual void login() = 0;
virtual void registerAndLogin() = 0;
virtual void logout() = 0;
virtual void submitUsefulness(Review *r, bool useful) = 0;
virtual void submitReview(AbstractResource *app, const QString &summary, const QString &review_text, const QString &rating) = 0;
// About all the different "user_names": the user_name that is taken as input here
// is the user_name the user typed in the review dialog, which defaults to what
// the backend returns in userName() or the last username used
// if the backend supports changing it (that is what the preferredUserName is).
//If the backend returns true for "supportsNameChange()", then the review dialog won't let them change it,
// therefore making the user_name here the same as "userName()".
void submitReview(AbstractResource *app, const QString &summary, const QString &review_text, const QString &rating, const QString &userName);
QString preferredUserName() const;
virtual void deleteReview(Review *r) = 0;
virtual void flagReview(Review *r, const QString &reason, const QString &text) = 0;
virtual void fetchReviews(AbstractResource *app, int page = 1) = 0;
Q_SIGNALS:
void reviewsReady(AbstractResource *app, const QVector<ReviewPtr> &reviews, bool canFetchMore);
void loginStateChanged();
void error(const QString &message);
void fetchingChanged(bool fetching);
protected:
virtual void sendReview(AbstractResource *app, const QString &summary, const QString &review_text, const QString &rating, const QString &userName) = 0;
virtual QString userName() const = 0;
};
#endif // ABSTRACTREVIEWSBACKEND_H
......@@ -208,7 +208,7 @@ QString OdrsReviewsBackend::userName() const
return KUser().property(KUser::FullName).toString();
}
void OdrsReviewsBackend::submitReview(AbstractResource *res, const QString &summary, const QString &description, const QString &rating)
void OdrsReviewsBackend::sendReview(AbstractResource *res, const QString &summary, const QString &description, const QString &rating, const QString &userName)
{
Q_ASSERT(res);
QJsonObject map = {
......@@ -218,7 +218,7 @@ void OdrsReviewsBackend::submitReview(AbstractResource *res, const QString &summ
{QStringLiteral("version"), res->isInstalled() ? res->installedVersion() : res->availableVersion()},
{QStringLiteral("locale"), QLocale::system().name()},
{QStringLiteral("distro"), osName()},
{QStringLiteral("user_display"), QJsonValue::fromVariant(KUser().property(KUser::FullName))},
{QStringLiteral("user_display"), QJsonValue::fromVariant(userName)},
{QStringLiteral("summary"), summary},
{QStringLiteral("description"), description},
{QStringLiteral("rating"), rating.toInt() * 10},
......
......@@ -26,7 +26,6 @@ public:
explicit OdrsReviewsBackend();
~OdrsReviewsBackend() override;
QString userName() const override;
void login() override
{
}
......@@ -42,6 +41,10 @@ public:
{
return false;
}
bool supportsNameChange() const override
{
return true;
}
void deleteReview(Review *) override
{
}
......@@ -50,7 +53,6 @@ public:
{
return m_isFetching;
}
void submitReview(AbstractResource *, const QString &summary, const QString &description, const QString &rating) override;
void flagReview(Review *, const QString &, const QString &) override
{
}
......@@ -67,6 +69,10 @@ private Q_SLOTS:
Q_SIGNALS:
void ratingsReady();
protected:
void sendReview(AbstractResource *app, const QString &summary, const QString &review_text, const QString &rating, const QString &userName) override;
QString userName() const override;
private:
void setFetching(bool);
QNetworkAccessManager *nam();
......
......@@ -76,9 +76,9 @@ void DummyReviewsBackend::submitUsefulness(Review *r, bool useful)
r->setUsefulChoice(useful ? ReviewsModel::Yes : ReviewsModel::No);
}
void DummyReviewsBackend::submitReview(AbstractResource *res, const QString &a, const QString &b, const QString &c)
void DummyReviewsBackend::sendReview(AbstractResource *res, const QString &a, const QString &b, const QString &c, const QString &d)
{
qDebug() << "dummy submit review" << res->name() << a << b << c;
qDebug() << "dummy submit review" << res->name() << a << b << c << d;
}
bool DummyReviewsBackend::isResourceSupported(AbstractResource * /*res*/) const
......
......@@ -46,7 +46,7 @@ public:
{
return false;
}
void submitReview(AbstractResource *, const QString &, const QString &, const QString &) override;
void flagReview(Review *, const QString &, const QString &) override
{
}
......@@ -58,6 +58,9 @@ public:
Q_SIGNALS:
void ratingsReady();
protected:
void sendReview(AbstractResource *, const QString &, const QString &, const QString &, const QString &) override;
private:
QHash<AbstractResource *, Rating *> m_ratings;
};
......
......@@ -114,7 +114,7 @@ void KNSReviews::deleteReview(Review * /*r*/)
qWarning() << "cannot delete comments";
}
void KNSReviews::submitReview(AbstractResource *app, const QString &summary, const QString &review_text, const QString &rating)
void KNSReviews::sendReview(AbstractResource *app, const QString &summary, const QString &review_text, const QString &rating, const QString &userName)
{
provider().voteForContent(app->packageName(), rating.toUInt() * 20);
if (!summary.isEmpty())
......
......@@ -28,17 +28,19 @@ public:
bool isFetching() const override;
void flagReview(Review *r, const QString &reason, const QString &text) override;
void deleteReview(Review *r) override;
void submitReview(AbstractResource *app, const QString &summary, const QString &review_text, const QString &rating) override;
void submitUsefulness(Review *r, bool useful) override;
void logout() override;
void registerAndLogin() override;
void login() override;
Rating *ratingForApplication(AbstractResource *app) const override;
bool hasCredentials() const override;
QString userName() const override;
bool isResourceSupported(AbstractResource *res) const override;
protected:
void sendReview(AbstractResource *app, const QString &summary, const QString &review_text, const QString &rating, const QString &userName) override;
QString userName() const override;
private Q_SLOTS:
void commentsReceived(Attica::BaseJob *job);
void credentialsReceived(const QString &user, const QString &password);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment