2013-10-18 14:24:29 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) by Klaas Freitag <freitag@owncloud.com>
|
|
|
|
* Copyright (C) by Daniel Molkentin <danimo@owncloud.com>
|
|
|
|
*
|
|
|
|
* 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; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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 <QNetworkRequest>
|
|
|
|
#include <QNetworkAccessManager>
|
2013-10-28 23:01:59 +04:00
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QNetworkRequest>
|
2013-10-24 02:29:08 +04:00
|
|
|
#include <QSslConfiguration>
|
2013-10-21 23:42:52 +04:00
|
|
|
#include <QBuffer>
|
|
|
|
#include <QXmlStreamReader>
|
|
|
|
#include <QStringList>
|
2013-10-23 16:48:44 +04:00
|
|
|
#include <QStack>
|
2013-11-14 20:02:41 +04:00
|
|
|
#include <QTimer>
|
2013-11-22 17:01:11 +04:00
|
|
|
#include <QMutex>
|
2013-10-21 23:42:52 +04:00
|
|
|
#include <QDebug>
|
2013-10-18 14:24:29 +04:00
|
|
|
|
|
|
|
#include "json.h"
|
|
|
|
|
|
|
|
#include "mirall/networkjobs.h"
|
2013-10-21 23:42:52 +04:00
|
|
|
#include "mirall/account.h"
|
2013-10-18 14:24:29 +04:00
|
|
|
|
2013-10-28 23:01:59 +04:00
|
|
|
#include "creds/credentialsfactory.h"
|
2013-11-22 17:01:11 +04:00
|
|
|
#include "creds/abstractcredentials.h"
|
2013-10-28 23:01:59 +04:00
|
|
|
|
2013-10-18 14:24:29 +04:00
|
|
|
namespace Mirall {
|
|
|
|
|
2013-10-23 16:48:44 +04:00
|
|
|
AbstractNetworkJob::AbstractNetworkJob(Account *account, const QString &path, QObject *parent)
|
2013-10-21 23:42:52 +04:00
|
|
|
: QObject(parent)
|
2013-11-22 17:01:11 +04:00
|
|
|
, _ignoreCredentialFailure(false)
|
2013-10-21 23:42:52 +04:00
|
|
|
, _reply(0)
|
2013-10-23 16:48:44 +04:00
|
|
|
, _account(account)
|
|
|
|
, _path(path)
|
2013-11-14 20:23:30 +04:00
|
|
|
, _timer(0)
|
2013-10-18 14:24:29 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractNetworkJob::setReply(QNetworkReply *reply)
|
|
|
|
{
|
2013-10-28 23:01:59 +04:00
|
|
|
if (_reply) {
|
|
|
|
_reply->deleteLater();
|
|
|
|
}
|
2013-10-18 14:24:29 +04:00
|
|
|
_reply = reply;
|
|
|
|
}
|
|
|
|
|
2013-11-14 20:02:41 +04:00
|
|
|
void AbstractNetworkJob::setTimeout(qint64 msec)
|
|
|
|
{
|
|
|
|
qDebug() << Q_FUNC_INFO << msec;
|
2013-11-14 20:23:30 +04:00
|
|
|
if (_timer)
|
|
|
|
_timer->deleteLater();
|
|
|
|
_timer = new QTimer(this);
|
|
|
|
_timer->setSingleShot(true);
|
|
|
|
connect(_timer, SIGNAL(timeout()), this, SLOT(slotTimeout()));
|
|
|
|
_timer->start(msec);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractNetworkJob::resetTimeout()
|
|
|
|
{
|
|
|
|
qint64 interval = _timer->interval();
|
|
|
|
_timer->stop();
|
|
|
|
_timer->start(interval);
|
2013-11-14 20:02:41 +04:00
|
|
|
}
|
|
|
|
|
2013-11-22 17:01:11 +04:00
|
|
|
void AbstractNetworkJob::setIgnoreCredentialFailure(bool ignore)
|
|
|
|
{
|
|
|
|
_ignoreCredentialFailure = ignore;
|
|
|
|
}
|
|
|
|
|
2013-10-21 23:42:52 +04:00
|
|
|
void AbstractNetworkJob::setAccount(Account *account)
|
|
|
|
{
|
|
|
|
_account = account;
|
|
|
|
}
|
|
|
|
|
2013-10-23 16:48:44 +04:00
|
|
|
void AbstractNetworkJob::setPath(const QString &path)
|
|
|
|
{
|
|
|
|
_path = path;
|
|
|
|
}
|
|
|
|
|
2013-11-22 17:01:11 +04:00
|
|
|
void AbstractNetworkJob::slotError(QNetworkReply::NetworkError)
|
2013-10-18 14:24:29 +04:00
|
|
|
{
|
|
|
|
qDebug() << metaObject()->className() << "Error:" << _reply->errorString();
|
2013-10-28 23:01:59 +04:00
|
|
|
emit networkError(_reply);
|
2013-10-18 14:24:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractNetworkJob::setupConnections(QNetworkReply *reply)
|
|
|
|
{
|
2013-10-28 23:01:59 +04:00
|
|
|
connect(reply, SIGNAL(finished()), SLOT(slotFinished()));
|
|
|
|
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
|
2013-11-04 19:36:23 +04:00
|
|
|
this, SLOT(slotError(QNetworkReply::NetworkError)));
|
2013-10-18 14:24:29 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 16:48:44 +04:00
|
|
|
QNetworkReply* AbstractNetworkJob::davRequest(const QByteArray &verb, const QString &relPath,
|
|
|
|
QNetworkRequest req, QIODevice *data)
|
2013-10-18 14:24:29 +04:00
|
|
|
{
|
2013-10-23 16:48:44 +04:00
|
|
|
return _account->davRequest(verb, relPath, req, data);
|
2013-10-18 14:24:29 +04:00
|
|
|
}
|
|
|
|
|
2013-10-28 23:01:59 +04:00
|
|
|
QNetworkReply *AbstractNetworkJob::davRequest(const QByteArray &verb, const QUrl &url, QNetworkRequest req, QIODevice *data)
|
|
|
|
{
|
|
|
|
return _account->davRequest(verb, url, req, data);
|
|
|
|
}
|
|
|
|
|
2013-10-23 16:48:44 +04:00
|
|
|
QNetworkReply* AbstractNetworkJob::getRequest(const QString &relPath)
|
2013-10-18 14:24:29 +04:00
|
|
|
{
|
2013-10-23 16:48:44 +04:00
|
|
|
return _account->getRequest(relPath);
|
2013-10-18 14:24:29 +04:00
|
|
|
}
|
|
|
|
|
2013-10-28 23:01:59 +04:00
|
|
|
QNetworkReply *AbstractNetworkJob::getRequest(const QUrl &url)
|
|
|
|
{
|
|
|
|
return _account->getRequest(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
QNetworkReply *AbstractNetworkJob::headRequest(const QString &relPath)
|
|
|
|
{
|
|
|
|
return _account->headRequest(relPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
QNetworkReply *AbstractNetworkJob::headRequest(const QUrl &url)
|
|
|
|
{
|
|
|
|
return _account->headRequest(url);
|
|
|
|
}
|
|
|
|
|
2013-11-22 17:01:11 +04:00
|
|
|
void AbstractNetworkJob::slotFinished()
|
|
|
|
{
|
|
|
|
static QMutex mutex;
|
|
|
|
AbstractCredentials *creds = _account->credentials();
|
|
|
|
if (creds->stillValid(_reply) || _ignoreCredentialFailure) {
|
|
|
|
finished();
|
|
|
|
} else {
|
|
|
|
// If other jobs that still were created from before
|
|
|
|
// the account was put offline by the code below,
|
|
|
|
// we do want them to fail silently while we
|
|
|
|
// query the user
|
|
|
|
if (mutex.tryLock()) {
|
|
|
|
Account *a = account();
|
2013-11-23 03:14:02 +04:00
|
|
|
bool fetched = creds->fetchFromUser(a);
|
2013-11-25 18:33:35 +04:00
|
|
|
if (fetched) {
|
|
|
|
a->setState(Account::Connected);
|
|
|
|
}
|
2013-11-22 17:01:11 +04:00
|
|
|
mutex.unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
deleteLater();
|
|
|
|
}
|
|
|
|
|
2013-10-18 14:24:29 +04:00
|
|
|
AbstractNetworkJob::~AbstractNetworkJob() {
|
|
|
|
_reply->deleteLater();
|
|
|
|
}
|
|
|
|
|
2013-11-22 17:01:11 +04:00
|
|
|
void AbstractNetworkJob::start()
|
|
|
|
{
|
|
|
|
qDebug() << "!!!" << metaObject()->className() << "created for" << account()->url() << "querying" << path();
|
|
|
|
}
|
|
|
|
|
2013-10-18 14:24:29 +04:00
|
|
|
/*********************************************************************************************/
|
|
|
|
|
2013-10-23 16:48:44 +04:00
|
|
|
RequestEtagJob::RequestEtagJob(Account *account, const QString &path, QObject *parent)
|
|
|
|
: AbstractNetworkJob(account, path, parent)
|
2013-11-14 22:20:09 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void RequestEtagJob::start()
|
2013-10-18 14:24:29 +04:00
|
|
|
{
|
2013-10-23 16:48:44 +04:00
|
|
|
QNetworkRequest req;
|
2013-11-14 22:20:09 +04:00
|
|
|
if (path().isEmpty() || path() == QLatin1String("/")) {
|
2013-10-18 14:24:29 +04:00
|
|
|
/* For the root directory, we need to query the etags of all the sub directories
|
|
|
|
* because, at the time I am writing this comment (Owncloud 5.0.9), the etag of the
|
|
|
|
* root directory is not updated when the sub directories changes */
|
|
|
|
req.setRawHeader("Depth", "1");
|
|
|
|
} else {
|
|
|
|
req.setRawHeader("Depth", "0");
|
|
|
|
}
|
|
|
|
QByteArray xml("<?xml version=\"1.0\" ?>\n"
|
|
|
|
"<d:propfind xmlns:d=\"DAV:\">\n"
|
|
|
|
" <d:prop>\n"
|
2013-10-23 16:48:44 +04:00
|
|
|
" <d:getetag/>\n"
|
2013-10-18 14:24:29 +04:00
|
|
|
" </d:prop>\n"
|
|
|
|
"</d:propfind>\n");
|
2013-11-14 22:20:09 +04:00
|
|
|
QBuffer *buf = new QBuffer(this);
|
2013-10-18 14:24:29 +04:00
|
|
|
buf->setData(xml);
|
|
|
|
buf->open(QIODevice::ReadOnly);
|
|
|
|
// assumes ownership
|
2013-11-14 22:20:09 +04:00
|
|
|
setReply(davRequest("PROPFIND", path(), req, buf));
|
2013-10-18 14:24:29 +04:00
|
|
|
buf->setParent(reply());
|
|
|
|
setupConnections(reply());
|
|
|
|
|
|
|
|
if( reply()->error() != QNetworkReply::NoError ) {
|
|
|
|
qDebug() << "getting etag: request network error: " << reply()->errorString();
|
|
|
|
}
|
2013-11-22 17:01:11 +04:00
|
|
|
AbstractNetworkJob::start();
|
2013-10-18 14:24:29 +04:00
|
|
|
}
|
|
|
|
|
2013-11-22 17:01:11 +04:00
|
|
|
void RequestEtagJob::finished()
|
2013-10-18 14:24:29 +04:00
|
|
|
{
|
|
|
|
if (reply()->attribute(QNetworkRequest::HttpStatusCodeAttribute) == 207) {
|
|
|
|
// Parse DAV response
|
|
|
|
QXmlStreamReader reader(reply());
|
|
|
|
reader.addExtraNamespaceDeclaration(QXmlStreamNamespaceDeclaration("d", "DAV:"));
|
|
|
|
QString etag;
|
|
|
|
while (!reader.atEnd()) {
|
|
|
|
QXmlStreamReader::TokenType type = reader.readNext();
|
|
|
|
if (type == QXmlStreamReader::StartElement &&
|
|
|
|
reader.namespaceUri() == QLatin1String("DAV:")) {
|
|
|
|
QString name = reader.name().toString();
|
|
|
|
if (name == QLatin1String("getetag")) {
|
|
|
|
etag += reader.readElementText();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
emit etagRetreived(etag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************/
|
|
|
|
|
2013-10-23 16:48:44 +04:00
|
|
|
MkColJob::MkColJob(Account *account, const QString &path, QObject *parent)
|
|
|
|
: AbstractNetworkJob(account, path, parent)
|
2013-10-18 14:24:29 +04:00
|
|
|
{
|
2013-11-14 22:20:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void MkColJob::start()
|
|
|
|
{
|
|
|
|
// assumes ownership
|
|
|
|
QNetworkReply *reply = davRequest("MKCOL", path());
|
|
|
|
setReply(reply);
|
|
|
|
setupConnections(reply);
|
2013-11-22 17:01:11 +04:00
|
|
|
AbstractNetworkJob::start();
|
2013-10-18 14:24:29 +04:00
|
|
|
}
|
|
|
|
|
2013-11-22 17:01:11 +04:00
|
|
|
void MkColJob::finished()
|
2013-10-18 14:24:29 +04:00
|
|
|
{
|
2013-10-28 23:01:59 +04:00
|
|
|
emit finished(reply()->error());
|
2013-10-18 14:24:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************/
|
|
|
|
|
2013-10-23 16:48:44 +04:00
|
|
|
LsColJob::LsColJob(Account *account, const QString &path, QObject *parent)
|
|
|
|
: AbstractNetworkJob(account, path, parent)
|
2013-11-14 22:20:09 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void LsColJob::start()
|
2013-10-18 14:24:29 +04:00
|
|
|
{
|
2013-10-23 16:48:44 +04:00
|
|
|
QNetworkRequest req;
|
2013-10-18 14:24:29 +04:00
|
|
|
req.setRawHeader("Depth", "1");
|
|
|
|
QByteArray xml("<?xml version=\"1.0\" ?>\n"
|
|
|
|
"<d:propfind xmlns:d=\"DAV:\">\n"
|
|
|
|
" <d:prop>\n"
|
|
|
|
" <d:resourcetype/>\n"
|
|
|
|
" </d:prop>\n"
|
|
|
|
"</d:propfind>\n");
|
2013-11-14 22:20:09 +04:00
|
|
|
QBuffer *buf = new QBuffer(this);
|
2013-10-18 14:24:29 +04:00
|
|
|
buf->setData(xml);
|
|
|
|
buf->open(QIODevice::ReadOnly);
|
2013-11-14 22:20:09 +04:00
|
|
|
QNetworkReply *reply = davRequest("PROPFIND", path(), req, buf);
|
2013-10-18 14:24:29 +04:00
|
|
|
buf->setParent(reply);
|
|
|
|
setReply(reply);
|
|
|
|
setupConnections(reply);
|
2013-11-22 17:01:11 +04:00
|
|
|
AbstractNetworkJob::start();
|
2013-10-18 14:24:29 +04:00
|
|
|
}
|
|
|
|
|
2013-11-22 17:01:11 +04:00
|
|
|
void LsColJob::finished()
|
2013-10-18 14:24:29 +04:00
|
|
|
{
|
|
|
|
if (reply()->attribute(QNetworkRequest::HttpStatusCodeAttribute) == 207) {
|
|
|
|
// Parse DAV response
|
|
|
|
QXmlStreamReader reader(reply());
|
|
|
|
reader.addExtraNamespaceDeclaration(QXmlStreamNamespaceDeclaration("d", "DAV:"));
|
|
|
|
|
|
|
|
QStringList folders;
|
|
|
|
QString currentItem;
|
|
|
|
|
|
|
|
while (!reader.atEnd()) {
|
|
|
|
QXmlStreamReader::TokenType type = reader.readNext();
|
|
|
|
if (type == QXmlStreamReader::StartElement &&
|
|
|
|
reader.namespaceUri() == QLatin1String("DAV:")) {
|
|
|
|
QString name = reader.name().toString();
|
|
|
|
if (name == QLatin1String("href")) {
|
|
|
|
currentItem = reader.readElementText();
|
|
|
|
} else if (name == QLatin1String("collection") &&
|
|
|
|
!currentItem.isEmpty()) {
|
|
|
|
folders.append(QUrl::fromEncoded(currentItem.toLatin1()).path());
|
|
|
|
currentItem.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
emit directoryListing(folders);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************/
|
|
|
|
|
2013-10-23 16:48:44 +04:00
|
|
|
CheckServerJob::CheckServerJob(Account *account, bool followRedirect, QObject *parent)
|
2013-10-28 23:01:59 +04:00
|
|
|
: AbstractNetworkJob(account, QLatin1String("status.php") , parent)
|
2013-10-23 16:48:44 +04:00
|
|
|
, _followRedirects(followRedirect)
|
|
|
|
, _redirectCount(0)
|
2013-10-18 14:24:29 +04:00
|
|
|
{
|
2013-11-14 22:20:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void CheckServerJob::start()
|
|
|
|
{
|
2013-10-23 16:48:44 +04:00
|
|
|
setReply(getRequest(path()));
|
2013-10-18 14:24:29 +04:00
|
|
|
setupConnections(reply());
|
2013-11-22 17:01:11 +04:00
|
|
|
AbstractNetworkJob::start();
|
2013-10-18 14:24:29 +04:00
|
|
|
}
|
|
|
|
|
2013-11-14 20:02:41 +04:00
|
|
|
void CheckServerJob::slotTimeout()
|
|
|
|
{
|
|
|
|
qDebug() << "TIMEOUT" << Q_FUNC_INFO;
|
|
|
|
if (reply()->isRunning())
|
|
|
|
emit timeout(reply()->url());
|
|
|
|
}
|
|
|
|
|
2013-10-23 16:48:44 +04:00
|
|
|
QString CheckServerJob::version(const QVariantMap &info)
|
|
|
|
{
|
|
|
|
return info.value(QLatin1String("version")).toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString CheckServerJob::versionString(const QVariantMap &info)
|
|
|
|
{
|
|
|
|
return info.value(QLatin1String("versionstring")).toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CheckServerJob::installed(const QVariantMap &info)
|
|
|
|
{
|
|
|
|
return info.value(QLatin1String("installed")).toBool();
|
|
|
|
}
|
|
|
|
|
2013-11-22 17:01:11 +04:00
|
|
|
void CheckServerJob::finished()
|
2013-10-18 14:24:29 +04:00
|
|
|
{
|
2013-10-24 02:29:08 +04:00
|
|
|
account()->setCertificateChain(reply()->sslConfiguration().peerCertificateChain());
|
|
|
|
|
2013-10-18 14:24:29 +04:00
|
|
|
// ### the qDebugs here should be exported via displayErrors() so they
|
|
|
|
// ### can be presented to the user if the job executor has a GUI
|
|
|
|
QUrl requestedUrl = reply()->request().url();
|
|
|
|
QUrl redirectUrl = reply()->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
|
|
|
|
if (!redirectUrl.isEmpty()) {
|
|
|
|
_redirectCount++;
|
|
|
|
if (requestedUrl.scheme() == QLatin1String("https") &&
|
|
|
|
redirectUrl.scheme() == QLatin1String("http")) {
|
|
|
|
qDebug() << Q_FUNC_INFO << "HTTPS->HTTP downgrade detected!";
|
2013-10-28 23:01:59 +04:00
|
|
|
} else if (requestedUrl == redirectUrl || _redirectCount >= maxRedirects()) {
|
2013-10-18 14:24:29 +04:00
|
|
|
qDebug() << Q_FUNC_INFO << "Redirect loop detected!";
|
|
|
|
} else {
|
2013-11-14 20:23:30 +04:00
|
|
|
resetTimeout();
|
2013-10-28 23:01:59 +04:00
|
|
|
setReply(getRequest(redirectUrl));
|
2013-10-18 14:24:29 +04:00
|
|
|
setupConnections(reply());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool success = false;
|
|
|
|
QVariantMap status = QtJson::parse(QString::fromUtf8(reply()->readAll()), success).toMap();
|
|
|
|
// empty or invalid response
|
|
|
|
if (!success || status.isEmpty()) {
|
|
|
|
qDebug() << "status.php from server is not valid JSON!";
|
|
|
|
}
|
|
|
|
|
|
|
|
qDebug() << "status.php returns: " << status << " " << reply()->error() << " Reply: " << reply();
|
|
|
|
if( status.contains("installed")
|
|
|
|
&& status.contains("version")
|
|
|
|
&& status.contains("versionstring") ) {
|
2013-10-28 23:01:59 +04:00
|
|
|
emit instanceFound(reply()->url(), status);
|
2013-10-18 14:24:29 +04:00
|
|
|
} else {
|
|
|
|
qDebug() << "No proper answer on " << requestedUrl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-04 19:36:23 +04:00
|
|
|
/*********************************************************************************************/
|
|
|
|
|
2013-11-14 22:20:09 +04:00
|
|
|
PropfindJob::PropfindJob(Account *account, const QString &path, QObject *parent)
|
2013-10-23 16:48:44 +04:00
|
|
|
: AbstractNetworkJob(account, path, parent)
|
2013-10-18 14:24:29 +04:00
|
|
|
{
|
2013-11-14 22:20:09 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void PropfindJob::start()
|
|
|
|
{
|
|
|
|
QList<QByteArray> properties = _properties;
|
|
|
|
|
2013-10-23 16:48:44 +04:00
|
|
|
if (properties.isEmpty()) {
|
|
|
|
properties << "allprop";
|
|
|
|
}
|
|
|
|
QNetworkRequest req;
|
2013-10-18 14:24:29 +04:00
|
|
|
req.setRawHeader("Depth", "0");
|
2013-10-23 16:48:44 +04:00
|
|
|
QByteArray propStr;
|
|
|
|
foreach (const QByteArray &prop, properties) {
|
|
|
|
propStr += " <d:" + prop + " />\n";
|
|
|
|
}
|
|
|
|
QByteArray xml = "<?xml version=\"1.0\" ?>\n"
|
|
|
|
"<d:propfind xmlns:d=\"DAV:\">\n"
|
|
|
|
" <d:prop>\n"
|
|
|
|
+ propStr +
|
|
|
|
" </d:prop>\n"
|
|
|
|
"</d:propfind>\n";
|
|
|
|
|
2013-11-14 22:20:09 +04:00
|
|
|
QBuffer *buf = new QBuffer(this);
|
2013-10-18 14:24:29 +04:00
|
|
|
buf->setData(xml);
|
|
|
|
buf->open(QIODevice::ReadOnly);
|
2013-11-14 22:20:09 +04:00
|
|
|
setReply(davRequest("PROPFIND", path(), req, buf));
|
2013-10-18 14:24:29 +04:00
|
|
|
buf->setParent(reply());
|
|
|
|
setupConnections(reply());
|
2013-11-22 17:01:11 +04:00
|
|
|
AbstractNetworkJob::start();
|
2013-10-18 14:24:29 +04:00
|
|
|
}
|
|
|
|
|
2013-11-14 22:20:09 +04:00
|
|
|
void PropfindJob::setProperties(QList<QByteArray> properties)
|
|
|
|
{
|
|
|
|
_properties = properties;
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<QByteArray> PropfindJob::properties() const
|
|
|
|
{
|
|
|
|
return _properties;
|
|
|
|
}
|
|
|
|
|
2013-11-22 17:01:11 +04:00
|
|
|
void PropfindJob::finished()
|
2013-10-18 14:24:29 +04:00
|
|
|
{
|
|
|
|
int http_result_code = reply()->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
|
|
|
|
|
|
|
if (http_result_code == 207) {
|
|
|
|
// Parse DAV response
|
|
|
|
QXmlStreamReader reader(reply());
|
|
|
|
reader.addExtraNamespaceDeclaration(QXmlStreamNamespaceDeclaration("d", "DAV:"));
|
|
|
|
|
2013-10-23 16:48:44 +04:00
|
|
|
QVariantMap items;
|
|
|
|
// introduced to nesting is ignored
|
|
|
|
QStack<QString> curElement;
|
2013-10-18 14:24:29 +04:00
|
|
|
|
|
|
|
while (!reader.atEnd()) {
|
|
|
|
QXmlStreamReader::TokenType type = reader.readNext();
|
|
|
|
if (type == QXmlStreamReader::StartElement &&
|
|
|
|
reader.namespaceUri() == QLatin1String("DAV:")) {
|
2013-10-23 16:48:44 +04:00
|
|
|
if (curElement.isEmpty()) {
|
|
|
|
curElement.push(reader.name().toString());
|
|
|
|
items.insert(reader.name().toString(), reader.text().toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (type == QXmlStreamReader::EndElement &&
|
|
|
|
reader.namespaceUri() == QLatin1String("DAV:")) {
|
|
|
|
if(curElement.top() == reader.name()) {
|
|
|
|
curElement.pop();
|
2013-10-18 14:24:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-23 16:48:44 +04:00
|
|
|
}
|
|
|
|
emit result(items);
|
2013-10-18 14:24:29 +04:00
|
|
|
} else {
|
|
|
|
qDebug() << "Quota request *not* successful, http result code is " << http_result_code;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-04 19:36:23 +04:00
|
|
|
/*********************************************************************************************/
|
|
|
|
|
2013-10-28 23:01:59 +04:00
|
|
|
EntityExistsJob::EntityExistsJob(Account *account, const QString &path, QObject *parent)
|
|
|
|
: AbstractNetworkJob(account, path, parent)
|
|
|
|
{
|
2013-11-14 22:20:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void EntityExistsJob::start()
|
|
|
|
{
|
|
|
|
setReply(headRequest(path()));
|
2013-10-28 23:01:59 +04:00
|
|
|
setupConnections(reply());
|
2013-11-22 17:01:11 +04:00
|
|
|
AbstractNetworkJob::start();
|
2013-10-28 23:01:59 +04:00
|
|
|
}
|
|
|
|
|
2013-11-22 17:01:11 +04:00
|
|
|
void EntityExistsJob::finished()
|
2013-10-28 23:01:59 +04:00
|
|
|
{
|
|
|
|
emit exists(reply());
|
|
|
|
}
|
|
|
|
|
2013-11-04 19:36:23 +04:00
|
|
|
/*********************************************************************************************/
|
|
|
|
|
|
|
|
CheckQuotaJob::CheckQuotaJob(Account *account, const QString &path, QObject *parent)
|
|
|
|
: AbstractNetworkJob(account, path, parent)
|
2013-11-14 22:20:09 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckQuotaJob::start()
|
2013-11-04 19:36:23 +04:00
|
|
|
{
|
|
|
|
QNetworkRequest req;
|
|
|
|
req.setRawHeader("Depth", "0");
|
|
|
|
QByteArray xml("<?xml version=\"1.0\" ?>\n"
|
|
|
|
"<d:propfind xmlns:d=\"DAV:\">\n"
|
|
|
|
" <d:prop>\n"
|
|
|
|
" <d:quota-available-bytes/>\n"
|
|
|
|
" <d:quota-used-bytes/>\n"
|
|
|
|
" </d:prop>\n"
|
|
|
|
"</d:propfind>\n");
|
2013-11-14 22:20:09 +04:00
|
|
|
QBuffer *buf = new QBuffer(this);
|
2013-11-04 19:36:23 +04:00
|
|
|
buf->setData(xml);
|
|
|
|
buf->open(QIODevice::ReadOnly);
|
|
|
|
// assumes ownership
|
2013-11-14 22:20:09 +04:00
|
|
|
setReply(davRequest("PROPFIND", path(), req, buf));
|
2013-11-04 19:36:23 +04:00
|
|
|
buf->setParent(reply());
|
|
|
|
setupConnections(reply());
|
2013-11-22 17:01:11 +04:00
|
|
|
AbstractNetworkJob::start();
|
2013-11-04 19:36:23 +04:00
|
|
|
}
|
|
|
|
|
2013-11-22 17:01:11 +04:00
|
|
|
void CheckQuotaJob::finished()
|
2013-11-04 19:36:23 +04:00
|
|
|
{
|
|
|
|
if (reply()->attribute(QNetworkRequest::HttpStatusCodeAttribute) == 207) {
|
|
|
|
// Parse DAV response
|
|
|
|
QXmlStreamReader reader(reply());
|
|
|
|
reader.addExtraNamespaceDeclaration(QXmlStreamNamespaceDeclaration("d", "DAV:"));
|
|
|
|
qint64 quotaAvailableBytes = 0;
|
|
|
|
qint64 quotaUsedBytes = 0;
|
|
|
|
while (!reader.atEnd()) {
|
|
|
|
QXmlStreamReader::TokenType type = reader.readNext();
|
|
|
|
if (type == QXmlStreamReader::StartElement &&
|
|
|
|
reader.namespaceUri() == QLatin1String("DAV:")) {
|
|
|
|
QString name = reader.name().toString();
|
|
|
|
if (name == QLatin1String("quota-available-bytes")) {
|
|
|
|
quotaAvailableBytes = reader.readElementText().toLongLong();
|
|
|
|
} else if (name == QLatin1String("quota-used-bytes")) {
|
|
|
|
quotaUsedBytes = reader.readElementText().toLongLong();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
qint64 total = quotaUsedBytes + quotaAvailableBytes;
|
|
|
|
emit quotaRetrieved(total, quotaUsedBytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-18 14:24:29 +04:00
|
|
|
} // namespace Mirall
|