-
Notifications
You must be signed in to change notification settings - Fork 0
/
tomboyserverauthenticatejob.cpp
127 lines (105 loc) · 4.09 KB
/
tomboyserverauthenticatejob.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
Copyright (c) 2016 Stefan Stäglich <[email protected]>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#include "tomboyserverauthenticatejob.h"
#include "debug.h"
#include <klocalizedstring.h>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
TomboyServerAuthenticateJob::TomboyServerAuthenticateJob(KIO::AccessManager *manager, QObject *parent)
: TomboyJobBase(manager, parent),
mWebView(new QWebEngineView(Q_NULLPTR))
{
// Connect the o2 authenfication signals
connect(mO1, &O1::linkingFailed, this, &TomboyServerAuthenticateJob::onLinkingFailed);
connect(mO1, &O1::linkingSucceeded, this, &TomboyServerAuthenticateJob::onLinkingSucceeded);
connect(mO1, &O1::openBrowser, this, &TomboyServerAuthenticateJob::onOpenBrowser);
connect(mO1, &O1::closeBrowser, mWebView, &QWebEngineView::close);
}
TomboyServerAuthenticateJob::~TomboyServerAuthenticateJob()
{
delete mWebView;
}
void TomboyServerAuthenticateJob::start()
{
mO1->link();
}
QString TomboyServerAuthenticateJob::getRequestToken() const
{
return mO1->getRequestToken();
}
QString TomboyServerAuthenticateJob::getRequestTokenSecret() const
{
return mO1->getRequestTokenSecret();
}
QString TomboyServerAuthenticateJob::getContentUrl() const
{
return mContentURL;
}
QString TomboyServerAuthenticateJob::getUserURL() const
{
return mUserURL;
}
void TomboyServerAuthenticateJob::onLinkingFailed()
{
setError(TomboyJobError::PermanentError);
setErrorText(i18n("Authorization has failed! It could be an SSL error!"));
emitResult();
}
void TomboyServerAuthenticateJob::onLinkingSucceeded()
{
QNetworkRequest request(mApiURL);
mReply = mRequestor->get(request, QList<O0RequestParameter>());
connect(mReply, &QNetworkReply::finished, this, &TomboyServerAuthenticateJob::onApiRequestFinished);
qCDebug(log_tomboynotesresource) << "TomboyServerAuthenticateJob: Start network request";
}
void TomboyServerAuthenticateJob::onOpenBrowser(const QUrl &url)
{
mWebView->setUrl(url);
mWebView->show();
}
void TomboyServerAuthenticateJob::onApiRequestFinished()
{
checkReplyError();
if (error() != TomboyJobError::NoError) {
setErrorText(mReply->errorString());
emitResult();
return;
}
// Parse received data as JSON and get user-href
const QJsonDocument document = QJsonDocument::fromJson(mReply->readAll(), Q_NULLPTR);
const QJsonObject jo = document.object();
mUserURL = jo[QLatin1String("user-ref")].toObject()[QLatin1String("api-ref")].toString();
QNetworkRequest request(mUserURL);
mReply = mRequestor->get(request, QList<O0RequestParameter>());
connect(mReply, &QNetworkReply::finished, this, &TomboyServerAuthenticateJob::onUserRequestFinished);
qCDebug(log_tomboynotesresource) << "TomboyServerAuthenticateJob: Start network request";
}
void TomboyServerAuthenticateJob::onUserRequestFinished()
{
checkReplyError();
if (error() != TomboyJobError::NoError) {
setErrorText(mReply->errorString());
emitResult();
return;
}
// Parse received data as JSON and get contentURL
QJsonDocument document = QJsonDocument::fromJson(mReply->readAll(), Q_NULLPTR);
QJsonObject jo = document.object();
mContentURL = jo[QLatin1String("notes-ref")].toObject()[QLatin1String("api-ref")].toString();
setError(TomboyJobError::NoError);
emitResult();
}