diff options
author | Roeland Jago Douma <rullzer@owncloud.com> | 2015-09-07 14:50:01 +0300 |
---|---|---|
committer | Roeland Jago Douma <rullzer@owncloud.com> | 2015-10-15 21:05:47 +0300 |
commit | b293aa762c3b32fe00527647ab5b18b8dbc28541 (patch) | |
tree | 5dcab7749603a9f66a75d098051f978100b5ebce /src/gui/ocsjob.cpp | |
parent | b5e75afc171f79a3fa97dee438dde997e27a5558 (diff) |
Split sharing code
There is now a generic OCSJob which must be inherited by other jobs. This is in
prepartion for the other OCS job that will come (for the Sharee API endpoint
for example).
More logic is moved from the sharedialog to the OcsShareJob. So in the GUI code
we now only say what we want (a new share, set the password etc). And the code
in libsync will make that happen. Error handling is for now still done in the
GUI part.
For now the ocsjob and ocssharejob live in gui but probabaly we should
create a libshare or libocs at some point.
Diffstat (limited to 'src/gui/ocsjob.cpp')
-rw-r--r-- | src/gui/ocsjob.cpp | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/src/gui/ocsjob.cpp b/src/gui/ocsjob.cpp new file mode 100644 index 000000000..19bf1e46d --- /dev/null +++ b/src/gui/ocsjob.cpp @@ -0,0 +1,115 @@ +/* + * 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 { + +OCSJob::OCSJob(AccountPtr account, QObject* parent) +: AbstractNetworkJob(account, "", parent) +{ + _passStatusCodes.append(100); + setIgnoreCredentialFailure(true); +} + +void OCSJob::setVerb(const QByteArray &verb) +{ + _verb = verb; +} + +void OCSJob::setUrl(const QUrl &url) +{ + _url = url; +} + +void OCSJob::setGetParams(const QList<QPair<QString, QString> >& getParams) +{ + _url.setQueryItems(getParams); +} + +void OCSJob::setPostParams(const QList<QPair<QString, QString> >& postParams) +{ + _postParams = postParams; +} + +void OCSJob::addPassStatusCode(int code) +{ + _passStatusCodes.append(code); +} + +void OCSJob::start() +{ + QNetworkRequest req; + req.setRawHeader("OCS-APIREQUEST", "true"); + 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(); +} + +bool OCSJob::finished() +{ + 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; +} + +int OCSJob::getJsonReturnCode(const QVariantMap &json, QString &message) +{ + //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; +} + +} |