summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJuha Vuolle <juha.vuolle@qt.io>2023-11-08 14:56:20 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2023-11-15 08:16:50 +0000
commit6bb114c0deff80246f8beabc1398e07471821a0f (patch)
tree9046021778fe2e685b202bc7f4fdb4c7f97965f9
parent7188524de807e0e57be43903dd27940463fcb672 (diff)
Redditclient example cleanups
Remove unused code + other smaller cleanups such as ASCII cast warnings, header inclusion and sorting Change-Id: I9bae13ff1b8655e5504b1eb2e24146bd0e6a58f5 Reviewed-by: Ivan Solovev <ivan.solovev@qt.io> (cherry picked from commit 8a75ee36cc38f9100964d20c462e00f4f57c697d) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r--examples/oauth/redditclient/main.cpp13
-rw-r--r--examples/oauth/redditclient/redditmodel.cpp22
-rw-r--r--examples/oauth/redditclient/redditmodel.h4
-rw-r--r--examples/oauth/redditclient/redditwrapper.cpp72
-rw-r--r--examples/oauth/redditclient/redditwrapper.h11
5 files changed, 42 insertions, 80 deletions
diff --git a/examples/oauth/redditclient/main.cpp b/examples/oauth/redditclient/main.cpp
index 06cbaeb..d449c1d 100644
--- a/examples/oauth/redditclient/main.cpp
+++ b/examples/oauth/redditclient/main.cpp
@@ -3,15 +3,20 @@
#include "redditmodel.h"
-#include <QtCore>
-#include <QtWidgets>
+#include <QtWidgets/qapplication.h>
+#include <QtWidgets/qlistview.h>
+
+#include <QtCore/qcommandlineparser.h>
+
+using namespace Qt::StringLiterals;
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QCommandLineParser parser;
- const QCommandLineOption clientId(QStringList() << "i" << "client-id",
- "Specifies the application client id", "client_id");
+
+ const QCommandLineOption clientId(QStringList() << "i"_L1 << "client-id"_L1,
+ "Specifies the application client id"_L1, "client_id"_L1);
parser.addOptions({clientId});
parser.process(app);
diff --git a/examples/oauth/redditclient/redditmodel.cpp b/examples/oauth/redditclient/redditmodel.cpp
index 45d754f..17469fe 100644
--- a/examples/oauth/redditclient/redditmodel.cpp
+++ b/examples/oauth/redditclient/redditmodel.cpp
@@ -3,8 +3,12 @@
#include "redditmodel.h"
-#include <QtCore>
-#include <QtNetwork>
+#include <QtNetwork/qnetworkreply.h>
+
+#include <QtCore/qjsonarray.h>
+#include <QtCore/qjsondocument.h>
+
+using namespace Qt::StringLiterals;
RedditModel::RedditModel(QObject *parent) : QAbstractTableModel(parent) {}
@@ -35,9 +39,9 @@ QVariant RedditModel::data(const QModelIndex &index, int role) const
if (role == Qt::DisplayRole) {
const auto childrenObject = threads.at(index.row());
- Q_ASSERT(childrenObject.value("data").isObject());
- const auto dataObject = childrenObject.value("data").toObject();
- return dataObject.value("title").toString();
+ Q_ASSERT(childrenObject.value("data"_L1).isObject());
+ const auto dataObject = childrenObject.value("data"_L1).toObject();
+ return dataObject.value("title"_L1).toString();
}
return QVariant();
}
@@ -52,7 +56,7 @@ void RedditModel::update()
{
auto reply = redditWrapper.requestHotThreads();
- connect(reply, &QNetworkReply::finished, [=]() {
+ connect(reply, &QNetworkReply::finished, this, [=]() {
reply->deleteLater();
if (reply->error() != QNetworkReply::NoError) {
emit error(reply->errorString());
@@ -62,11 +66,11 @@ void RedditModel::update()
const auto document = QJsonDocument::fromJson(json);
Q_ASSERT(document.isObject());
const auto rootObject = document.object();
- Q_ASSERT(rootObject.value("kind").toString() == "Listing");
- const auto dataValue = rootObject.value("data");
+ Q_ASSERT(rootObject.value("kind"_L1).toString() == "Listing"_L1);
+ const auto dataValue = rootObject.value("data"_L1);
Q_ASSERT(dataValue.isObject());
const auto dataObject = dataValue.toObject();
- const auto childrenValue = dataObject.value("children");
+ const auto childrenValue = dataObject.value("children"_L1);
Q_ASSERT(childrenValue.isArray());
const auto childrenArray = childrenValue.toArray();
diff --git a/examples/oauth/redditclient/redditmodel.h b/examples/oauth/redditclient/redditmodel.h
index ffc6eae..57e8c35 100644
--- a/examples/oauth/redditclient/redditmodel.h
+++ b/examples/oauth/redditclient/redditmodel.h
@@ -6,7 +6,9 @@
#include "redditwrapper.h"
-#include <QtCore>
+#include <QtCore/qabstractitemmodel.h>
+#include <QtCore/qjsonobject.h>
+#include <QtCore/qpointer.h>
QT_FORWARD_DECLARE_CLASS(QNetworkReply)
diff --git a/examples/oauth/redditclient/redditwrapper.cpp b/examples/oauth/redditclient/redditwrapper.cpp
index d2f5807..0723b49 100644
--- a/examples/oauth/redditclient/redditwrapper.cpp
+++ b/examples/oauth/redditclient/redditwrapper.cpp
@@ -3,32 +3,28 @@
#include "redditwrapper.h"
-#include <QtGui>
-#include <QtCore>
-#include <QtNetworkAuth>
+#include <QtNetworkAuth/qoauth2authorizationcodeflow.h>
+#include <QtNetworkAuth/qoauthhttpserverreplyhandler.h>
-const QUrl newUrl("https://oauth.reddit.com/new");
-const QUrl hotUrl("https://oauth.reddit.com/hot");
-const QUrl liveThreadsUrl("https://oauth.reddit.com/live/XXXX/about.json");
+#include <QtGui/qdesktopservices.h>
+
+using namespace Qt::StringLiterals;
+
+static constexpr auto hotUrl("https://oauth.reddit.com/hot"_L1);
+static constexpr auto authorizationUrl("https://www.reddit.com/api/v1/authorize"_L1);
+static constexpr auto accessTokenUrl("https://www.reddit.com/api/v1/access_token"_L1);
+static constexpr auto scope("identity read"_L1);
RedditWrapper::RedditWrapper(QObject *parent) : QObject(parent)
{
auto replyHandler = new QOAuthHttpServerReplyHandler(1337, this);
oauth2.setReplyHandler(replyHandler);
- oauth2.setAuthorizationUrl(QUrl("https://www.reddit.com/api/v1/authorize"));
- oauth2.setAccessTokenUrl(QUrl("https://www.reddit.com/api/v1/access_token"));
- oauth2.setScope("identity read");
+ oauth2.setAuthorizationUrl(QUrl(authorizationUrl));
+ oauth2.setAccessTokenUrl(QUrl(accessTokenUrl));
+ oauth2.setScope(scope);
- connect(&oauth2, &QOAuth2AuthorizationCodeFlow::statusChanged, [=](
- QAbstractOAuth::Status status) {
- if (status == QAbstractOAuth::Status::Granted)
- emit authenticated();
- });
- oauth2.setModifyParametersFunction([&](QAbstractOAuth::Stage stage, QMultiMap<QString, QVariant> *parameters) {
- if (stage == QAbstractOAuth::Stage::RequestingAuthorization && isPermanent())
- parameters->insert("duration", "permanent");
- });
- connect(&oauth2, &QOAuth2AuthorizationCodeFlow::authorizeWithBrowser,
+ connect(&oauth2, &QOAuth2AuthorizationCodeFlow::granted, this, &RedditWrapper::authenticated);
+ connect(&oauth2, &QOAuth2AuthorizationCodeFlow::authorizeWithBrowser, this,
&QDesktopServices::openUrl);
}
@@ -41,46 +37,10 @@ RedditWrapper::RedditWrapper(const QString &clientIdentifier, QObject *parent) :
QNetworkReply *RedditWrapper::requestHotThreads()
{
qDebug() << "Getting hot threads...";
- return oauth2.get(hotUrl);
-}
-
-bool RedditWrapper::isPermanent() const
-{
- return permanent;
-}
-
-void RedditWrapper::setPermanent(bool value)
-{
- permanent = value;
+ return oauth2.get(QUrl(hotUrl));
}
void RedditWrapper::grant()
{
oauth2.grant();
}
-
-void RedditWrapper::subscribeToLiveUpdates()
-{
- qDebug() << "Susbscribing...";
- QNetworkReply *reply = oauth2.get(liveThreadsUrl);
- connect(reply, &QNetworkReply::finished, [=]() {
- reply->deleteLater();
- if (reply->error() != QNetworkReply::NoError) {
- qCritical() << "Reddit error:" << reply->errorString();
- return;
- }
-
- const auto json = reply->readAll();
-
- const auto document = QJsonDocument::fromJson(json);
- Q_ASSERT(document.isObject());
- const auto rootObject = document.object();
- const auto dataValue = rootObject.value("data");
- Q_ASSERT(dataValue.isObject());
- const auto dataObject = dataValue.toObject();
- const auto websocketUrlValue = dataObject.value("websocket_url");
- Q_ASSERT(websocketUrlValue.isString() && websocketUrlValue.toString().size());
- const QUrl websocketUrl(websocketUrlValue.toString());
- emit subscribed(websocketUrl);
- });
-}
diff --git a/examples/oauth/redditclient/redditwrapper.h b/examples/oauth/redditclient/redditwrapper.h
index afabed3..23665c2 100644
--- a/examples/oauth/redditclient/redditwrapper.h
+++ b/examples/oauth/redditclient/redditwrapper.h
@@ -4,10 +4,7 @@
#ifndef REDDITWRAPPER_H
#define REDDITWRAPPER_H
-#include <QtCore>
-#include <QtNetwork>
-
-#include <QOAuth2AuthorizationCodeFlow>
+#include <QtNetworkAuth/qoauth2authorizationcodeflow.h>
class RedditWrapper : public QObject
{
@@ -19,20 +16,14 @@ public:
QNetworkReply *requestHotThreads();
- bool isPermanent() const;
- void setPermanent(bool value);
-
public slots:
void grant();
- void subscribeToLiveUpdates();
signals:
void authenticated();
- void subscribed(const QUrl &url);
private:
QOAuth2AuthorizationCodeFlow oauth2;
- bool permanent = false;
};
#endif // REDDITWRAPPER_H