diff options
author | Jesus Fernandez <jesus.fernandez@qt.io> | 2016-12-05 15:38:19 +0100 |
---|---|---|
committer | Jani Heikkinen <jani.heikkinen@qt.io> | 2016-12-07 13:20:06 +0000 |
commit | ce47c7b0562f61af20df0aca306b88efa700b562 (patch) | |
tree | 4dba8203050408a4753315c1b9ff165fe8e0f441 /examples | |
parent | a493cacda6365ba29e5d3e1cf373c61329c00d77 (diff) |
Reddit Client Example
Basic Reddit client which shows the user posts in a QListView.
Change-Id: I16c23bac2d7dc709c6223f6c6cf22a63e057a545
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'examples')
-rw-r--r-- | examples/oauth/oauth.pro | 4 | ||||
-rw-r--r-- | examples/oauth/redditclient/doc/images/redditclient-example.png | bin | 0 -> 40526 bytes | |||
-rw-r--r-- | examples/oauth/redditclient/doc/src/qtnetworkauth-redditclient.qdoc | 44 | ||||
-rw-r--r-- | examples/oauth/redditclient/main.cpp | 65 | ||||
-rw-r--r-- | examples/oauth/redditclient/redditclient.pro | 16 | ||||
-rw-r--r-- | examples/oauth/redditclient/redditmodel.cpp | 118 | ||||
-rw-r--r-- | examples/oauth/redditclient/redditmodel.h | 75 | ||||
-rw-r--r-- | examples/oauth/redditclient/redditwrapper.cpp | 121 | ||||
-rw-r--r-- | examples/oauth/redditclient/redditwrapper.h | 74 |
9 files changed, 516 insertions, 1 deletions
diff --git a/examples/oauth/oauth.pro b/examples/oauth/oauth.pro index 06a388d..6124292 100644 --- a/examples/oauth/oauth.pro +++ b/examples/oauth/oauth.pro @@ -1,2 +1,4 @@ TEMPLATE = subdirs -SUBDIRS += twittertimeline +SUBDIRS += \ + twittertimeline \ + redditclient diff --git a/examples/oauth/redditclient/doc/images/redditclient-example.png b/examples/oauth/redditclient/doc/images/redditclient-example.png Binary files differnew file mode 100644 index 0000000..013450a --- /dev/null +++ b/examples/oauth/redditclient/doc/images/redditclient-example.png diff --git a/examples/oauth/redditclient/doc/src/qtnetworkauth-redditclient.qdoc b/examples/oauth/redditclient/doc/src/qtnetworkauth-redditclient.qdoc new file mode 100644 index 0000000..773f2db --- /dev/null +++ b/examples/oauth/redditclient/doc/src/qtnetworkauth-redditclient.qdoc @@ -0,0 +1,44 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:FDL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Free Documentation License Usage +** Alternatively, this file may be used under the terms of the GNU Free +** Documentation License version 1.3 as published by the Free Software +** Foundation and appearing in the file included in the packaging of +** this file. Please review the following information to ensure +** the GNU Free Documentation License version 1.3 requirements +** will be met: http://www.gnu.org/copyleft/fdl.html. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/*! + \example redditclient + \title Reddit Example + \ingroup examples-qtnetworkauth + \brief Demonstrates authenticating with OAuth 2 to access Reddit. + + \image redditclient-example.png Screenshot of the example + + The \e {Reddit} example uses OAuth 2, as supported by + \l {Qt Network Authorization}, to sign in to Reddit and display the + Reddit posts (in text format) associated with the authenticated user. + + To use this example, a consumer key from Reddit is needed. + To register the application visit https://www.reddit.com/prefs/apps/. + + \include examples-run.qdocinc +*/ diff --git a/examples/oauth/redditclient/main.cpp b/examples/oauth/redditclient/main.cpp new file mode 100644 index 0000000..b5e1f6e --- /dev/null +++ b/examples/oauth/redditclient/main.cpp @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtNetwork module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "redditmodel.h" + +#include <QtCore> +#include <QtWidgets> + +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"); + + parser.addOptions({clientId}); + parser.process(app); + + if (parser.isSet(clientId)) { + QListView view; + RedditModel model(parser.value(clientId)); + view.setModel(&model); + view.show(); + return app.exec(); + } else { + parser.showHelp(); + } + return 0; +} diff --git a/examples/oauth/redditclient/redditclient.pro b/examples/oauth/redditclient/redditclient.pro new file mode 100644 index 0000000..c507bed --- /dev/null +++ b/examples/oauth/redditclient/redditclient.pro @@ -0,0 +1,16 @@ +QT += widgets network networkauth + +TARGET = redditclient + +# Input +SOURCES += main.cpp \ + redditmodel.cpp \ + redditwrapper.cpp + +HEADERS += \ + redditmodel.h \ + redditwrapper.h + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/oauth/redditclient +INSTALLS += target diff --git a/examples/oauth/redditclient/redditmodel.cpp b/examples/oauth/redditclient/redditmodel.cpp new file mode 100644 index 0000000..077ed18 --- /dev/null +++ b/examples/oauth/redditclient/redditmodel.cpp @@ -0,0 +1,118 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtNetwork module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "redditmodel.h" + +#include <QtCore> +#include <QtNetwork> + +RedditModel::RedditModel(QObject *parent) : QAbstractTableModel(parent) {} + +RedditModel::RedditModel(const QString &clientId, QObject *parent) : + QAbstractTableModel(parent), + redditWrapper(clientId) +{ + grant(); +} + +int RedditModel::rowCount(const QModelIndex &parent) const +{ + Q_UNUSED(parent); + return threads.size(); +} + +int RedditModel::columnCount(const QModelIndex &parent) const +{ + Q_UNUSED(parent); + return threads.size() ? 1 : 0; +} + +QVariant RedditModel::data(const QModelIndex &index, int role) const +{ + Q_UNUSED(role); + if (!index.isValid()) + return QVariant(); + + 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(); + } + return QVariant(); +} + +void RedditModel::grant() +{ + redditWrapper.grant(); + connect(&redditWrapper, &RedditWrapper::authenticated, this, &RedditModel::update); +} + +void RedditModel::update() +{ + auto reply = redditWrapper.requestHotThreads(); + + connect(reply, &QNetworkReply::finished, [=]() { + if (reply->error() != QNetworkReply::NoError) { + emit error(reply->errorString()); + return; + } + const auto json = reply->readAll(); + 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(dataValue.isObject()); + const auto dataObject = dataValue.toObject(); + const auto childrenValue = dataObject.value("children"); + Q_ASSERT(childrenValue.isArray()); + const auto childrenArray = childrenValue.toArray(); + + if (childrenArray.isEmpty()) + return; + + beginInsertRows(QModelIndex(), threads.size(), childrenArray.size() + threads.size() - 1); + for (const auto childValue : qAsConst(childrenArray)) { + Q_ASSERT(childValue.isObject()); + threads.append(childValue.toObject()); + } + endInsertRows(); + }); +} diff --git a/examples/oauth/redditclient/redditmodel.h b/examples/oauth/redditclient/redditmodel.h new file mode 100644 index 0000000..8ee0cab --- /dev/null +++ b/examples/oauth/redditclient/redditmodel.h @@ -0,0 +1,75 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtNetwork module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef REDDITMODEL_H +#define REDDITMODEL_H + +#include "redditwrapper.h" + +#include <QtCore> + +class QNetworkReply; + +class RedditModel : public QAbstractTableModel +{ + Q_OBJECT + +public: + RedditModel(QObject *parent = nullptr); + RedditModel(const QString &clientId, QObject *parent = nullptr); + + virtual int rowCount(const QModelIndex &parent) const override; + virtual int columnCount(const QModelIndex &parent) const override; + virtual QVariant data(const QModelIndex &index, int role) const override; + + void grant(); + +signals: + void error(const QString &errorString); + +private slots: + void update(); + +private: + RedditWrapper redditWrapper; + QPointer<QNetworkReply> liveThreadReply; + QList<QJsonObject> threads; +}; + +#endif // REDDITMODEL_H diff --git a/examples/oauth/redditclient/redditwrapper.cpp b/examples/oauth/redditclient/redditwrapper.cpp new file mode 100644 index 0000000..ec98d5c --- /dev/null +++ b/examples/oauth/redditclient/redditwrapper.cpp @@ -0,0 +1,121 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtNetwork module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "redditwrapper.h" + +#include <QtGui> +#include <QtCore> +#include <QtNetworkAuth> + +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"); + +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"); + + connect(&oauth2, &QOAuth2AuthorizationCodeFlow::statusChanged, [=]( + QAbstractOAuth::Status status) { + if (status == QAbstractOAuth::Status::Granted) + emit authenticated(); + }); + oauth2.setModifyParametersFunction([&](QAbstractOAuth::Stage stage, QVariantMap *parameters) { + if (stage == QAbstractOAuth::Stage::RequestingAuthorization && isPermanent()) + parameters->insert("duration", "permanent"); + }); + connect(&oauth2, &QOAuth2AuthorizationCodeFlow::authorizeWithBrowser, + &QDesktopServices::openUrl); +} + +RedditWrapper::RedditWrapper(const QString &clientIdentifier, QObject *parent) : + RedditWrapper(parent) +{ + oauth2.setClientIdentifier(clientIdentifier); +} + +QNetworkReply *RedditWrapper::requestHotThreads() +{ + qDebug() << "Getting hot threads..."; + return oauth2.get(hotUrl); +} + +bool RedditWrapper::isPermanent() const +{ + return permanent; +} + +void RedditWrapper::setPermanent(bool value) +{ + permanent = value; +} + +void RedditWrapper::grant() +{ + oauth2.grant(); +} + +void RedditWrapper::subscribeToLiveUpdates() +{ + qDebug() << "Susbscribing..."; + QNetworkReply *reply = oauth2.get(liveThreadsUrl); + connect(reply, &QNetworkReply::finished, [=]() { + 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 new file mode 100644 index 0000000..cc416e1 --- /dev/null +++ b/examples/oauth/redditclient/redditwrapper.h @@ -0,0 +1,74 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtNetwork module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef REDDITWRAPPER_H +#define REDDITWRAPPER_H + +#include <QtCore> +#include <QtNetwork> + +#include <QOAuth2AuthorizationCodeFlow> + +class RedditWrapper : public QObject +{ + Q_OBJECT + +public: + RedditWrapper(QObject *parent = nullptr); + RedditWrapper(const QString &clientIdentifier, QObject *parent = nullptr); + + 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 |