2015-09-07 14:50:01 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) by Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; version 2 of the License.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ocsjob.h"
|
|
|
|
#include "networkjobs.h"
|
|
|
|
#include "account.h"
|
|
|
|
#include "json.h"
|
|
|
|
|
|
|
|
#include <QBuffer>
|
|
|
|
|
|
|
|
namespace OCC {
|
|
|
|
|
2015-10-15 21:54:52 +03:00
|
|
|
OcsJob::OcsJob(AccountPtr account, QObject* parent)
|
2015-09-07 14:50:01 +03:00
|
|
|
: AbstractNetworkJob(account, "", parent)
|
|
|
|
{
|
|
|
|
_passStatusCodes.append(100);
|
|
|
|
setIgnoreCredentialFailure(true);
|
|
|
|
}
|
|
|
|
|
2015-10-15 21:54:52 +03:00
|
|
|
void OcsJob::setVerb(const QByteArray &verb)
|
2015-09-07 14:50:01 +03:00
|
|
|
{
|
|
|
|
_verb = verb;
|
|
|
|
}
|
|
|
|
|
2015-10-15 21:54:52 +03:00
|
|
|
void OcsJob::setUrl(const QUrl &url)
|
2015-09-07 14:50:01 +03:00
|
|
|
{
|
|
|
|
_url = url;
|
|
|
|
}
|
|
|
|
|
2015-10-15 21:54:52 +03:00
|
|
|
void OcsJob::setGetParams(const QList<QPair<QString, QString> >& getParams)
|
2015-09-07 14:50:01 +03:00
|
|
|
{
|
|
|
|
_url.setQueryItems(getParams);
|
|
|
|
}
|
|
|
|
|
2015-10-15 21:54:52 +03:00
|
|
|
void OcsJob::setPostParams(const QList<QPair<QString, QString> >& postParams)
|
2015-09-07 14:50:01 +03:00
|
|
|
{
|
|
|
|
_postParams = postParams;
|
|
|
|
}
|
|
|
|
|
2015-10-15 21:54:52 +03:00
|
|
|
void OcsJob::addPassStatusCode(int code)
|
2015-09-07 14:50:01 +03:00
|
|
|
{
|
|
|
|
_passStatusCodes.append(code);
|
|
|
|
}
|
|
|
|
|
2015-10-15 21:54:52 +03:00
|
|
|
void OcsJob::start()
|
2015-09-07 14:50:01 +03:00
|
|
|
{
|
|
|
|
QNetworkRequest req;
|
2015-10-15 21:54:52 +03:00
|
|
|
req.setRawHeader("Ocs-APIREQUEST", "true");
|
2015-09-07 14:50:01 +03:00
|
|
|
req.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
|
|
|
|
|
|
|
|
// Url encode the _postParams and put them in a buffer.
|
|
|
|
QByteArray postData;
|
|
|
|
Q_FOREACH(auto tmp2, _postParams) {
|
|
|
|
if (! postData.isEmpty()) {
|
|
|
|
postData.append("&");
|
|
|
|
}
|
|
|
|
postData.append(QUrl::toPercentEncoding(tmp2.first));
|
|
|
|
postData.append("=");
|
|
|
|
postData.append(QUrl::toPercentEncoding(tmp2.second));
|
|
|
|
}
|
|
|
|
QBuffer *buffer = new QBuffer;
|
|
|
|
buffer->setData(postData);
|
|
|
|
|
|
|
|
auto queryItems = _url.queryItems();
|
|
|
|
queryItems.append(qMakePair(QString::fromLatin1("format"), QString::fromLatin1("json")));
|
|
|
|
_url.setQueryItems(queryItems);
|
|
|
|
|
|
|
|
setReply(davRequest(_verb, _url, req, buffer));
|
|
|
|
setupConnections(reply());
|
|
|
|
buffer->setParent(reply());
|
|
|
|
AbstractNetworkJob::start();
|
|
|
|
}
|
|
|
|
|
2015-10-15 21:54:52 +03:00
|
|
|
bool OcsJob::finished()
|
2015-09-07 14:50:01 +03:00
|
|
|
{
|
|
|
|
const QString replyData = reply()->readAll();
|
|
|
|
|
|
|
|
bool success;
|
|
|
|
QVariantMap json = QtJson::parse(replyData, success).toMap();
|
|
|
|
if (!success) {
|
|
|
|
qDebug() << "Could not parse reply to" << _verb << _url << _postParams
|
|
|
|
<< ":" << replyData;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString message;
|
|
|
|
const int statusCode = getJsonReturnCode(json, message);
|
|
|
|
if (!_passStatusCodes.contains(statusCode)) {
|
|
|
|
qDebug() << "Reply to" << _verb << _url << _postParams
|
|
|
|
<< "has unexpected status code:" << statusCode << replyData;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit jobFinished(json);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-15 21:54:52 +03:00
|
|
|
int OcsJob::getJsonReturnCode(const QVariantMap &json, QString &message)
|
2015-09-07 14:50:01 +03:00
|
|
|
{
|
|
|
|
//TODO proper checking
|
|
|
|
int code = json.value("ocs").toMap().value("meta").toMap().value("statuscode").toInt();
|
|
|
|
message = json.value("ocs").toMap().value("meta").toMap().value("message").toString();
|
|
|
|
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|