From 99df4c9e50c92fb5547c3d7ee90e5d40eccb5c7d Mon Sep 17 00:00:00 2001 From: Alexey Shmalko Date: Sun, 25 Nov 2012 01:17:28 +0200 Subject: [PATCH 1/2] Add scope flag in OAuthConnection --- src/oauth/oauthconnection.cpp | 57 +++++++++++++++++++++++------------ src/oauth/oauthconnection.h | 28 +++++++++++++++++ 2 files changed, 65 insertions(+), 20 deletions(-) diff --git a/src/oauth/oauthconnection.cpp b/src/oauth/oauthconnection.cpp index bf6a320..6081fb3 100644 --- a/src/oauth/oauthconnection.cpp +++ b/src/oauth/oauthconnection.cpp @@ -32,6 +32,7 @@ #include #include +#include #include #include @@ -44,6 +45,9 @@ namespace Vreen { const static QUrl authUrl("http://api.vk.com/oauth/authorize"); const static QUrl apiUrl("https://api.vk.com/method/"); +const static char *scopeNames[] = { "notify", "friends", "photos", "audio", + "video", "docs", "notes", "pages", "status", "offers", "questions", "wall", + "groups", "messages", "notifications", "stats", "ads", "offline" }; class OAuthConnection; class OAuthConnectionPrivate : public ConnectionPrivate @@ -53,24 +57,24 @@ class OAuthConnectionPrivate : public ConnectionPrivate OAuthConnectionPrivate(OAuthConnection *q, int appId) : ConnectionPrivate(q), connectionState(Client::StateOffline), clientId(appId), - scope(QStringList() << QLatin1String("notify") - << QLatin1String("friends") - << QLatin1String("photos") - << QLatin1String("audio") - << QLatin1String("video") - << QLatin1String("docs") - << QLatin1String("notes") - << QLatin1String("pages") - << QLatin1String("status") - << QLatin1String("offers") - << QLatin1String("questions") - << QLatin1String("wall") - << QLatin1String("groups") - << QLatin1String("messages") - << QLatin1String("notifications") - << QLatin1String("stats") - << QLatin1String("ads") - << QLatin1String("offline")), + scope(OAuthConnection::notify| + OAuthConnection::friends| + OAuthConnection::photos| + OAuthConnection::audio| + OAuthConnection::video| + OAuthConnection::docs| + OAuthConnection::notes| + OAuthConnection::pages| + OAuthConnection::status| + OAuthConnection::offers| + OAuthConnection::questions| + OAuthConnection::wall| + OAuthConnection::groups| + OAuthConnection::messages| + OAuthConnection::notifications| + OAuthConnection::stats| + OAuthConnection::ads| + OAuthConnection::offline), redirectUri(QLatin1String("http://oauth.vk.com/blank.html")), displayType(OAuthConnection::Touch), responseType(QLatin1String("token")), @@ -84,7 +88,7 @@ class OAuthConnectionPrivate : public ConnectionPrivate //OAuth settings int clientId; - QStringList scope; //settings + OAuthConnection::Scopes scope; //settings QString redirectUri; OAuthConnection::DisplayType displayType; QString responseType; @@ -238,6 +242,19 @@ void OAuthConnection::setDisplayType(OAuthConnection::DisplayType displayType) } } +OAuthConnection::Scopes OAuthConnection::scopes() const +{ + return d_func()->scope; +} + +void OAuthConnection::setScopes(OAuthConnection::Scopes scopes) +{ + Q_D(OAuthConnection); + if( d->scope != scopes ) { + d->scope = scopes; + } +} + void OAuthConnectionPrivate::requestToken() { Q_Q(OAuthConnection); @@ -248,7 +265,7 @@ void OAuthConnectionPrivate::requestToken() } QUrl url = authUrl; url.addQueryItem(QLatin1String("client_id"), QByteArray::number(clientId)); - url.addQueryItem(QLatin1String("scope"), scope.join(",")); + url.addQueryItem(QLatin1String("scope"), flagsToStrList(scope, scopeNames).join(",")); url.addQueryItem(QLatin1String("redirect_uri"), redirectUri); const char *type[] = { "page", diff --git a/src/oauth/oauthconnection.h b/src/oauth/oauthconnection.h index 1c246bf..f7e21f6 100644 --- a/src/oauth/oauthconnection.h +++ b/src/oauth/oauthconnection.h @@ -39,8 +39,10 @@ class OAuthConnection : public Connection Q_OBJECT Q_DECLARE_PRIVATE(OAuthConnection) Q_ENUMS(DisplayType) + Q_FLAGS(Scopes) Q_PROPERTY(int clientId READ clientId WRITE setClientId NOTIFY clientIdChanged) Q_PROPERTY(DisplayType displayType READ displayType WRITE setDisplayType) + Q_PROPERTY(Scopes scopes READ scopes WRITE setScopes) public: enum DisplayType { Page, @@ -48,6 +50,28 @@ class OAuthConnection : public Connection Touch, Wap }; + enum Scope { + notify = 0x1, + friends = 0x2, + photos = 0x4, + audio = 0x8, + video = 0x10, + docs = 0x20, + notes = 0x40, + pages = 0x80, + status = 0x100, + offers = 0x200, + questions = 0x400, + wall = 0x800, + groups = 0x1000, + messages = 0x2000, + notifications = 0x4000, + stats = 0x8000, + ads = 0x10000, + offline = 0x20000 + }; + Q_DECLARE_FLAGS(Scopes, Scope) + explicit OAuthConnection(int clientId, QObject *parent = 0); explicit OAuthConnection(QObject *parent = 0); virtual ~OAuthConnection(); @@ -67,6 +91,8 @@ class OAuthConnection : public Connection void setClientId(int clientId); DisplayType displayType() const; void setDisplayType(DisplayType displayType); + Scopes scopes() const; + void setScopes(Scopes scopes); signals: void authConfirmRequested(QWebPage *page); void accessTokenChanged(const QByteArray &token, time_t expiresIn); @@ -78,6 +104,8 @@ class OAuthConnection : public Connection Q_PRIVATE_SLOT(d_func(), void _q_loadFinished(bool)) }; +Q_DECLARE_OPERATORS_FOR_FLAGS(OAuthConnection::Scopes) + } // namespace Vreen #endif // VK_OAUTHCONNECTION_H From 55a3a3617dfa6acdb44f9369245a5231b067655a Mon Sep 17 00:00:00 2001 From: Alexey Shmalko Date: Sun, 25 Nov 2012 01:42:21 +0200 Subject: [PATCH 2/2] Add OAuthConnection::scopesChanged signal --- src/oauth/oauthconnection.cpp | 1 + src/oauth/oauthconnection.h | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/oauth/oauthconnection.cpp b/src/oauth/oauthconnection.cpp index 6081fb3..6b71462 100644 --- a/src/oauth/oauthconnection.cpp +++ b/src/oauth/oauthconnection.cpp @@ -252,6 +252,7 @@ void OAuthConnection::setScopes(OAuthConnection::Scopes scopes) Q_D(OAuthConnection); if( d->scope != scopes ) { d->scope = scopes; + emit scopesChanged(scopes); } } diff --git a/src/oauth/oauthconnection.h b/src/oauth/oauthconnection.h index f7e21f6..1935917 100644 --- a/src/oauth/oauthconnection.h +++ b/src/oauth/oauthconnection.h @@ -42,7 +42,7 @@ class OAuthConnection : public Connection Q_FLAGS(Scopes) Q_PROPERTY(int clientId READ clientId WRITE setClientId NOTIFY clientIdChanged) Q_PROPERTY(DisplayType displayType READ displayType WRITE setDisplayType) - Q_PROPERTY(Scopes scopes READ scopes WRITE setScopes) + Q_PROPERTY(Scopes scopes READ scopes WRITE setScopes NOTIFY scopesChanged) public: enum DisplayType { Page, @@ -97,6 +97,7 @@ class OAuthConnection : public Connection void authConfirmRequested(QWebPage *page); void accessTokenChanged(const QByteArray &token, time_t expiresIn); void clientIdChanged(int clientId); + void scopesChanged(Vreen::OAuthConnection::Scopes scopes); protected: QNetworkRequest makeRequest(const QString &method, const QVariantMap &args = QVariantMap()); void decorateRequest(QNetworkRequest &);