nextcloud-desktop/src/mirall/owncloudinfo.cpp

273 lines
8.3 KiB
C++
Raw Normal View History

2011-10-05 19:49:03 +04:00
/*
* Copyright (C) by Klaas Freitag <freitag@kde.org>
*
* 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 <QtCore>
#include <QtGui>
#include <QAuthenticator>
#include "mirall/owncloudinfo.h"
#include "mirall/mirallconfigfile.h"
#include "mirall/sslerrordialog.h"
2012-03-14 13:02:52 +04:00
#include "mirall/version.h"
2012-04-12 13:37:48 +04:00
#include "mirall/sslerrordialog.h"
2011-10-05 19:49:03 +04:00
namespace Mirall
{
QNetworkAccessManager* ownCloudInfo::_manager = 0;
2012-04-12 13:37:48 +04:00
SslErrorDialog *ownCloudInfo::_sslErrorDialog = 0;
bool ownCloudInfo::_certsUntrusted = false;
ownCloudInfo::ownCloudInfo( const QString& connectionName, QObject *parent ) :
QObject(parent)
2011-10-05 19:49:03 +04:00
{
if( connectionName.isEmpty() )
_connection = QString::fromLocal8Bit( "ownCloud");
else
_connection = connectionName;
2012-03-16 18:16:45 +04:00
if( ! _manager ) {
qDebug() << "Creating static NetworkAccessManager";
_manager = new QNetworkAccessManager;
}
connect( _manager, SIGNAL( sslErrors(QNetworkReply*, QList<QSslError>)),
this, SLOT(slotSSLFailed(QNetworkReply*, QList<QSslError>)) );
connect( _manager, SIGNAL(authenticationRequired(QNetworkReply*, QAuthenticator*)),
SLOT(slotAuthentication(QNetworkReply*,QAuthenticator*)));
2012-03-16 18:16:45 +04:00
}
ownCloudInfo::~ownCloudInfo()
{
delete _manager;
2012-04-12 13:37:48 +04:00
delete _sslErrorDialog;
2011-10-05 19:49:03 +04:00
}
bool ownCloudInfo::isConfigured()
{
MirallConfigFile cfgFile;
return cfgFile.connectionExists( _connection );
2011-10-05 19:49:03 +04:00
}
void ownCloudInfo::checkInstallation()
{
getRequest( "status.php", false );
}
2011-10-05 19:49:03 +04:00
void ownCloudInfo::getWebDAVPath( const QString& path )
{
getRequest( path, true );
}
void ownCloudInfo::getRequest( const QString& path, bool webdav )
{
2012-03-16 18:16:45 +04:00
qDebug() << "Get Request to " << path;
MirallConfigFile cfgFile;
QString url = cfgFile.ownCloudUrl( _connection, webdav ) + path;
QNetworkRequest request;
request.setUrl( QUrl( url ) );
2012-04-15 15:54:16 +04:00
setupHeaders( request, 0 );
QNetworkReply *reply = _manager->get( request );
connect( reply, SIGNAL(finished()), SLOT(slotReplyFinished()));
_directories[reply] = path;
connect( reply, SIGNAL( error(QNetworkReply::NetworkError )),
this, SLOT(slotError( QNetworkReply::NetworkError )));
2011-10-05 19:49:03 +04:00
}
void ownCloudInfo::mkdirRequest( const QString& dir )
{
2012-04-15 16:36:24 +04:00
qDebug() << "OCInfo Making dir " << dir;
MirallConfigFile cfgFile;
QNetworkRequest req;
req.setUrl( QUrl( cfgFile.ownCloudUrl( _connection, true ) + dir ) );
QNetworkReply *reply = davRequest("MKCOL", req, 0);
connect( reply, SIGNAL(finished()), SLOT(slotMkdirFinished()) );
connect( reply, SIGNAL( error(QNetworkReply::NetworkError )),
2012-04-15 15:54:16 +04:00
this, SLOT(slotError(QNetworkReply::NetworkError )));
}
void ownCloudInfo::slotMkdirFinished()
{
QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
if( ! reply ) {
qDebug() << "ownCloudInfo: Reply empty!";
return;
}
emit webdavColCreated( reply );
qDebug() << "mkdir slot hit.";
reply->deleteLater();
}
void ownCloudInfo::slotAuthentication( QNetworkReply*, QAuthenticator *auth )
{
if( auth ) {
MirallConfigFile cfgFile;
qDebug() << "Authenticating request!";
auth->setUser( cfgFile.ownCloudUser( _connection ) );
auth->setPassword( cfgFile.ownCloudPasswd( _connection ));
}
}
2011-10-05 19:49:03 +04:00
void ownCloudInfo::slotSSLFailed( QNetworkReply *reply, QList<QSslError> errors )
{
2012-04-12 13:37:48 +04:00
qDebug() << "SSL-Warnings happened for url " << reply->url().toString();
2012-04-12 13:37:48 +04:00
if( _certsUntrusted ) {
// User decided once to untrust. Honor this decision.
return;
}
2012-04-12 13:37:48 +04:00
if( _sslErrorDialog == 0 ) {
_sslErrorDialog = new SslErrorDialog();
}
2012-04-12 13:37:48 +04:00
if( _sslErrorDialog->setErrorList( errors ) ) {
// all ssl certs are known and accepted. We can ignore the problems right away.
qDebug() << "Certs are already known and trusted, Warnings are not valid.";
reply->ignoreSslErrors();
} else {
if( _sslErrorDialog->exec() == QDialog::Accepted ) {
if( _sslErrorDialog->trustConnection() ) {
reply->ignoreSslErrors();
} else {
// User does not want to trust.
_certsUntrusted = true;
}
}
}
}
//
// There have been problems with the finish-signal coming from the networkmanager.
// To avoid that, the reply-signals were connected and the data is taken from the
// sender() method.
//
void ownCloudInfo::slotReplyFinished()
2011-10-05 19:49:03 +04:00
{
QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
if( ! reply ) {
qDebug() << "ownCloudInfo: Reply empty!";
return;
}
const QString version( reply->readAll() );
const QString url = reply->url().toString();
QString plainUrl(url);
plainUrl.remove("/status.php");
QString info( version );
if( url.endsWith("status.php") ) {
// it was a call to status.php
if( reply->error() == QNetworkReply::NoError && info.isEmpty() ) {
// This seems to be a bit strange behaviour of QNetworkAccessManager.
// It calls the finised slot multiple times but only the first read wins.
// That happend when the code connected the finished signal of the manager.
// It did not happen when the code connected to the reply finish signal.
qDebug() << "WRN: NetworkReply with not content but also no error! " << reply;
reply->deleteLater();
return;
}
qDebug() << "status.php returns: " << info << " " << reply->error() << " Reply: " << reply;
if( info.contains("installed") && info.contains("version") && info.contains("versionstring") ) {
info.remove(0,1); // remove first char which is a "{"
info.remove(-1,1); // remove the last char which is a "}"
QStringList li = info.split( QChar(',') );
QString infoString;
QString versionStr;
foreach ( infoString, li ) {
if( infoString.contains( "versionstring") ) {
// get the version string out.
versionStr = infoString.mid(17);
versionStr.remove(-1, 1);
}
}
emit ownCloudInfoFound( plainUrl, versionStr );
} else {
qDebug() << "No proper answer on " << url;
emit noOwncloudFound( reply );
}
} else {
// it was a general GET request.
QString dir("unknown");
if( _directories.contains(reply) ) {
dir = _directories[reply];
_directories.remove(reply);
}
emit ownCloudDirExists( dir, reply );
}
reply->deleteLater();
2011-10-05 19:49:03 +04:00
}
2012-04-12 13:37:48 +04:00
void ownCloudInfo::resetSSLUntrust()
{
_certsUntrusted = false;
}
2011-10-12 17:14:39 +04:00
void ownCloudInfo::slotError( QNetworkReply::NetworkError err)
2011-10-05 19:49:03 +04:00
{
qDebug() << "ownCloudInfo Network Error: " << err;
2011-10-05 19:49:03 +04:00
}
// ============================================================================
2012-04-15 15:54:16 +04:00
void ownCloudInfo::setupHeaders( QNetworkRequest & req, quint64 size )
{
2012-04-15 15:54:16 +04:00
MirallConfigFile cfgFile;
QUrl url( cfgFile.ownCloudUrl( QString(), false ) );
qDebug() << "Setting up host header: " << url.host();
req.setRawHeader( QByteArray("Host"), url.host().toUtf8() );
req.setRawHeader( QByteArray("User-Agent"), QString("mirall-%1").arg(MIRALL_STRINGIFY(MIRALL_VERSION)).toAscii());
req.setRawHeader( QByteArray("Authorization"), cfgFile.basicAuthHeader() );
if (size) {
req.setHeader( QNetworkRequest::ContentLengthHeader, QVariant(size));
req.setHeader( QNetworkRequest::ContentTypeHeader, QVariant("text/xml; charset=utf-8"));
}
}
QNetworkReply* ownCloudInfo::davRequest(const QString& reqVerb, QNetworkRequest& req, QByteArray *data)
{
MirallConfigFile cfgFile;
2012-04-15 15:54:16 +04:00
setupHeaders(req, quint64(data ? data->size() : 0));
if( data ) {
QBuffer iobuf( data );
return _manager->sendCustomRequest(req, reqVerb.toUtf8(), &iobuf );
} else {
return _manager->sendCustomRequest(req, reqVerb.toUtf8(), 0 );
}
2012-04-15 15:54:16 +04:00
}
2012-04-15 15:54:16 +04:00
2011-10-05 19:49:03 +04:00
}