Skip to content
This repository was archived by the owner on Dec 12, 2024. It is now read-only.

Commit 819e74f

Browse files
author
niwakame
committed
Cleanup and QSettings fix
1 parent bf9d01c commit 819e74f

File tree

6 files changed

+99
-35
lines changed

6 files changed

+99
-35
lines changed

Diff for: main.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ int main(int argc, char *argv[])
2222
Blog blog_req;
2323
Settings settings;
2424
Oauth oauth;
25+
oauth.setSettings(&settings);
2526

2627

2728
/* DEBUG VALUES */

Diff for: meemblr.pro

+4-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ SOURCES += main.cpp \
4444
kqoauth/kqoauthutils.cpp \
4545
kqoauth/kqoauthrequest_xauth.cpp \
4646
kqoauth/kqoauthrequest_1.cpp \
47-
kqoauth/kqoauthauthreplyserver.cpp
47+
kqoauth/kqoauthauthreplyserver.cpp \
48+
tumblrapi.cpp
4849

4950
# Please do not modify the following two lines. Required for deployment.
5051
include(qmlapplicationviewer/qmlapplicationviewer.pri)
@@ -80,6 +81,7 @@ HEADERS += \
8081
kqoauth/kqoauthmanager_p.h \
8182
kqoauth/kqoauthglobals.h \
8283
kqoauth/kqoauthauthreplyserver_p.h \
83-
kqoauth/kqoauthauthreplyserver.h
84+
kqoauth/kqoauthauthreplyserver.h \
85+
tumblrapi.h
8486

8587
RESOURCES +=

Diff for: oauth.cpp

+51-20
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
#include "oauth.h"
22

3-
Oauth::Oauth(QObject *parent) : QObject(parent)
4-
{
5-
/* seed random generator */
6-
qsrand(QTime::currentTime().msec());
3+
Oauth::Oauth(QObject *parent) : QObject(parent) {
4+
oauthRequest = new KQOAuthRequest;
5+
oauthManager = new KQOAuthManager;
6+
oauthRequest->setEnableDebugOutput(true);
7+
}
8+
9+
void Oauth::setSettings(Settings *settings) {
10+
m_sets = settings;
711
}
812

913
void Oauth::startAuthentication(QString username) {
@@ -31,7 +35,6 @@ void Oauth::startAuthentication(QString username) {
3135
data.setRequestEndpoint("http://www.tumblr.com/oauth/request_token");
3236
data.setTokenEndpoint("http://www.tumblr.com/oauth/access_token");
3337

34-
3538
/* Please read this! ###############
3639
* these are actually our keys ! if you wish to use this code, please register your key / application at: http://www.tumblr.com/oauth/apps
3740
*/
@@ -40,9 +43,8 @@ void Oauth::startAuthentication(QString username) {
4043
//data.setConsumerKey("7zSQ06s60K8PHwXYK1Hw2fTSqgQQkLzPPS14BQQjsXRvQMMWyP");
4144
//data.setConsumerSecret("8rRNTXMWxe6p5AYumqnZYSDDfofBehF1cjuT05w7nqMHpGy0YZ");
4245
/* Niwakame's keys */
43-
data.setConsumerKey("KhJM28EMcP1iEeUomSnFbpwI1YmzPVEYxh1xl07Rlr5AQfbKEj");
44-
data.setConsumerSecret("ouPTiEiAU7mUXbkRlDZkD3FlXEAOREWyffmtVuo5bQSanf7UFb");
45-
46+
data.setConsumerKey(m_sets->getConsumerKey());
47+
data.setConsumerSecret(m_sets->getConsumerSecret());
4648

4749
data.setCallback("http://www.tumblr.com/oauth/access_token");
4850
data.setDisplayCallback("true");
@@ -70,33 +72,62 @@ void Oauth::onResponse(const SignOn::SessionData &sessionData)
7072
qDebug() << "Access token: " << response.AccessToken();
7173
qDebug() << "Toke nSecret: " << response.TokenSecret();
7274

75+
m_sets->setAccessToken(response.AccessToken());
76+
m_sets->setAccessTokenSecret(response.TokenSecret());
7377
}
7478

7579
void Oauth::onApiResponse(QByteArray response) {
7680
qDebug() << response;
7781
}
82+
void Oauth::onApiError(QByteArray response) {
7883

84+
}
7985

8086
/* test function to get a profile */
8187
void Oauth::testCall(QString test) {
82-
oauthRequest = new KQOAuthRequest;
83-
oauthManager = new KQOAuthManager;
84-
oauthRequest->setEnableDebugOutput(true);
88+
oauthRequest->initRequest(KQOAuthRequest::AuthorizedRequest, QUrl("http://api.tumblr.com/v2/user/info"));
89+
//oauthRequest->initRequest(KQOAuthRequest::AuthorizedRequest, QUrl("http://api.tumblr.com/v2/blog/niwakame.tumblr.com/post"));
8590

86-
//oauthRequest->initRequest(KQOAuthRequest::AuthorizedRequest, QUrl("http://api.tumblr.com/v2/user/info"));
87-
oauthRequest->initRequest(KQOAuthRequest::AuthorizedRequest, QUrl("http://api.tumblr.com/v2/blog/niwakame.tumblr.com/post"));
88-
oauthRequest->setConsumerKey("KhJM28EMcP1iEeUomSnFbpwI1YmzPVEYxh1xl07Rlr5AQfbKEj");
89-
oauthRequest->setConsumerSecretKey("ouPTiEiAU7mUXbkRlDZkD3FlXEAOREWyffmtVuo5bQSanf7UFb");
90-
oauthRequest->setToken("");
91-
oauthRequest->setTokenSecret("");
92-
oauthRequest->setHttpMethod(KQOAuthRequest::POST);
91+
oauthRequest->setConsumerKey(m_sets->getConsumerKey());
92+
oauthRequest->setConsumerSecretKey(m_sets->getConsumerSecret());
93+
oauthRequest->setToken(m_sets->getAccessToken());
94+
oauthRequest->setTokenSecret(m_sets->getAccessTokenSecret());
95+
//oauthRequest->setHttpMethod(KQOAuthRequest::POST);
9396

94-
KQOAuthParameters params;
97+
/*KQOAuthParameters params;
9598
params.insert("type", "text");
9699
params.insert("body", "This is a testpost from Meemblr. Fear us :>");
97-
oauthRequest->setAdditionalParameters(params);
100+
oauthRequest->setAdditionalParameters(params);*/
98101

99102
connect(oauthManager, SIGNAL(requestReady(QByteArray)), this, SLOT(onApiResponse(QByteArray)));
100103

101104
oauthManager->executeRequest(oauthRequest);
102105
}
106+
107+
void Oauth::callApi(QUrl url, QList< QPair<QString, QString> > params, QString method) {
108+
oauthRequest->initRequest(KQOAuthRequest::AuthorizedRequest, url);
109+
oauthRequest->setConsumerKey(m_sets->getConsumerKey());
110+
oauthRequest->setConsumerSecretKey(m_sets->getConsumerSecret());
111+
oauthRequest->setToken(m_sets->getAccessToken());
112+
oauthRequest->setTokenSecret(m_sets->getAccessTokenSecret());
113+
114+
if (method == "POST") {
115+
oauthRequest->setHttpMethod(KQOAuthRequest::POST);
116+
} else {
117+
oauthRequest->setHttpMethod(KQOAuthRequest::GET);
118+
}
119+
120+
if (params.size() > 0) {
121+
KQOAuthParameters parameters;
122+
QPair<QString, QString> pair;
123+
foreach(pair, params) {
124+
QString key = pair.first;
125+
QString val = pair.second;
126+
parameters.insert(key, val);
127+
}
128+
oauthRequest->setAdditionalParameters(parameters);
129+
}
130+
131+
connect(oauthManager, SIGNAL(requestReady(QByteArray)), this, SLOT(onApiResponse(QByteArray)));
132+
oauthManager->executeRequest(oauthRequest);
133+
}

Diff for: oauth.h

+7-10
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,10 @@
44
#include <QObject>
55
#include <QString>
66
#include <QDebug>
7-
#include <QStringList>
8-
#include <QMap>
97
#include <QUrl>
10-
#include <QList>
8+
#include <settings.h>
119
#include <QPair>
12-
#include <QCryptographicHash>
13-
#include <QNetworkRequest>
14-
#include <QNetworkReply>
15-
#include <QNetworkAccessManager>
16-
#include <QTime>
17-
#include <QtAlgorithms>
10+
#include <QList>
1811

1912
#include <kqoauth/kqoauthmanager.h>
2013
#include <kqoauth/kqoauthrequest.h>
@@ -31,21 +24,25 @@ class Oauth : public QObject
3124
public:
3225
Oauth(QObject *parent = 0);
3326
Q_INVOKABLE void startAuthentication(QString username);
34-
Q_INVOKABLE void testCall(QString test);
27+
void testCall(QString test);
28+
void setSettings(Settings *settings);
29+
void callApi(QUrl url, QList< QPair<QString, QString> >, QString method);
3530

3631

3732
public slots:
3833
void onResponse(const SignOn::SessionData &sessionData);
3934
void onError(const SignOn::Error &error);
4035
void identityMethodsAvailable(const QStringList &);
4136
void onApiResponse(QByteArray response);
37+
void onApiError(QByteArray response);
4238

4339
private:
4440
SignOn::AuthService *authService;
4541
SignOn::IdentityInfo *m_info;
4642

4743
KQOAuthManager *oauthManager;
4844
KQOAuthRequest *oauthRequest;
45+
Settings *m_sets;
4946
};
5047

5148
#endif // OAUTH_H

Diff for: settings.cpp

+27-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@
33
Settings::Settings(QObject *parent)
44
: QObject(parent)
55
{
6-
sets = new QSettings("meemblr", "cyrus-ZogG-niwakame");
6+
productName = QString("meemblr");
7+
configName = QString("config");
8+
sets = new QSettings(productName, configName, this);
79
}
810

911
void Settings::setUsername(QString username) {
1012
sets->setValue("username", username);
1113
}
1214

1315
QVariant Settings::getUsername() {
14-
return sets->value("username","");
16+
return sets->value("username","").toString();
1517
}
1618

1719
void Settings::setThemeInverted(QVariant inverted) {
@@ -21,3 +23,26 @@ void Settings::setThemeInverted(QVariant inverted) {
2123
QVariant Settings::getThemeInverted() {
2224
return sets->value("themeInverted", false);
2325
}
26+
27+
void Settings::setAccessToken(QString token) {
28+
sets->setValue("token", token);
29+
}
30+
31+
QString Settings::getAccessToken() {
32+
return sets->value("token", "").toString();
33+
}
34+
35+
void Settings::setAccessTokenSecret(QString tokenSecret) {
36+
sets->setValue("tokenSecret", tokenSecret);
37+
}
38+
39+
QString Settings::getAccessTokenSecret() {
40+
return sets->value("tokenSecret", "").toString();
41+
}
42+
43+
QString Settings::getConsumerKey() {
44+
return "KhJM28EMcP1iEeUomSnFbpwI1YmzPVEYxh1xl07Rlr5AQfbKEj";
45+
}
46+
QString Settings::getConsumerSecret() {
47+
return "ouPTiEiAU7mUXbkRlDZkD3FlXEAOREWyffmtVuo5bQSanf7UFb";
48+
}

Diff for: settings.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,19 @@ class Settings : public QObject
1111

1212
public:
1313
Settings(QObject *parent = 0);
14-
1514
Q_INVOKABLE void setUsername(QString username);
1615
Q_INVOKABLE QVariant getUsername();
1716
Q_INVOKABLE void setThemeInverted(QVariant inverted);
1817
Q_INVOKABLE QVariant getThemeInverted();
18+
void setAccessToken(QString token);
19+
QString getAccessToken();
20+
void setAccessTokenSecret(QString tokenSecret);
21+
QString getAccessTokenSecret();
22+
QString getConsumerKey();
23+
QString getConsumerSecret();
24+
QString productName;
25+
QString configName;
26+
1927
private:
2028
QSettings *sets;
2129

0 commit comments

Comments
 (0)