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.
|
|
|
|
*/
|
|
|
|
|
2011-10-06 18:55:28 +04:00
|
|
|
|
|
|
|
#include <QtCore>
|
|
|
|
#include <QtGui>
|
|
|
|
#include <QAuthenticator>
|
|
|
|
|
2012-04-06 10:53:57 +04:00
|
|
|
|
2012-02-17 18:54:39 +04:00
|
|
|
#include "mirall/owncloudinfo.h"
|
|
|
|
#include "mirall/mirallconfigfile.h"
|
2012-04-06 10:53:57 +04:00
|
|
|
#include "mirall/sslerrordialog.h"
|
2012-03-14 13:02:52 +04:00
|
|
|
#include "mirall/version.h"
|
2011-10-05 19:49:03 +04:00
|
|
|
|
|
|
|
namespace Mirall
|
|
|
|
{
|
|
|
|
|
2012-04-06 10:53:57 +04:00
|
|
|
QNetworkAccessManager* ownCloudInfo::_manager = 0;
|
|
|
|
|
|
|
|
|
2012-02-17 18:54:39 +04:00
|
|
|
ownCloudInfo::ownCloudInfo( const QString& connectionName, QObject *parent ) :
|
2012-04-06 10:53:57 +04:00
|
|
|
QObject(parent)
|
2011-10-05 19:49:03 +04:00
|
|
|
{
|
2012-02-17 18:54:39 +04:00
|
|
|
if( connectionName.isEmpty() )
|
|
|
|
_connection = QString::fromLocal8Bit( "ownCloud");
|
|
|
|
else
|
|
|
|
_connection = connectionName;
|
2012-03-16 18:16:45 +04:00
|
|
|
|
2012-04-06 10:53:57 +04:00
|
|
|
if( ! _manager ) {
|
|
|
|
qDebug() << "Creating static NetworkAccessManager";
|
|
|
|
_manager = new QNetworkAccessManager;
|
|
|
|
}
|
|
|
|
connect( _manager, SIGNAL(finished(QNetworkReply*)),
|
|
|
|
this, SLOT(slotReplyFinished(QNetworkReply*)));
|
|
|
|
connect( _manager, SIGNAL( sslErrors(QNetworkReply*, QList<QSslError>)),
|
|
|
|
this, SLOT(slotSSLFailed(QNetworkReply*, QList<QSslError>)) );
|
2012-03-16 18:16:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ownCloudInfo::~ownCloudInfo()
|
|
|
|
{
|
|
|
|
delete _manager;
|
2011-10-05 19:49:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ownCloudInfo::isConfigured()
|
|
|
|
{
|
2012-02-17 18:54:39 +04:00
|
|
|
MirallConfigFile cfgFile;
|
|
|
|
return cfgFile.connectionExists( _connection );
|
2011-10-05 19:49:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ownCloudInfo::checkInstallation()
|
|
|
|
{
|
2012-02-18 20:02:10 +04:00
|
|
|
getRequest( "status.php", false );
|
|
|
|
}
|
2011-10-05 19:49:03 +04:00
|
|
|
|
2012-02-18 20:02:10 +04:00
|
|
|
void ownCloudInfo::getWebDAVPath( const QString& path )
|
|
|
|
{
|
|
|
|
getRequest( path, true );
|
|
|
|
}
|
2012-02-17 18:54:39 +04:00
|
|
|
|
2012-02-18 20:02:10 +04:00
|
|
|
void ownCloudInfo::getRequest( const QString& path, bool webdav )
|
|
|
|
{
|
2012-03-16 18:16:45 +04:00
|
|
|
qDebug() << "Get Request to " << path;
|
2012-04-06 10:53:57 +04:00
|
|
|
|
2012-02-18 20:02:10 +04:00
|
|
|
// this is not a status call.
|
|
|
|
if( !webdav && path == "status.php") {
|
|
|
|
_versionInfoCall = true;
|
|
|
|
_directory.clear();
|
|
|
|
} else {
|
|
|
|
_directory = path;
|
|
|
|
_versionInfoCall = false;
|
|
|
|
}
|
2011-10-06 12:54:54 +04:00
|
|
|
|
2012-02-18 20:02:10 +04:00
|
|
|
MirallConfigFile cfgFile;
|
|
|
|
QString url = cfgFile.ownCloudUrl( _connection, webdav ) + path;
|
|
|
|
QNetworkRequest request;
|
|
|
|
request.setUrl( QUrl( url ) );
|
2012-03-14 13:02:52 +04:00
|
|
|
request.setRawHeader( "User-Agent", QString("mirall-%1").arg(MIRALL_STRINGIFY(MIRALL_VERSION)).toAscii());
|
2012-02-18 20:02:10 +04:00
|
|
|
request.setRawHeader( "Authorization", cfgFile.basicAuthHeader() );
|
2012-04-06 10:53:57 +04:00
|
|
|
QNetworkReply *reply = _manager->get( request );
|
2012-02-18 20:02:10 +04:00
|
|
|
|
2012-04-06 10:53:57 +04:00
|
|
|
connect( reply, SIGNAL( error(QNetworkReply::NetworkError )),
|
2012-02-18 20:02:10 +04:00
|
|
|
this, SLOT(slotError( QNetworkReply::NetworkError )));
|
2012-04-06 10:53:57 +04:00
|
|
|
|
2011-10-05 19:49:03 +04:00
|
|
|
}
|
|
|
|
|
2012-02-18 20:02:10 +04:00
|
|
|
|
2011-10-06 18:55:28 +04:00
|
|
|
void ownCloudInfo::slotAuthentication( QNetworkReply*, QAuthenticator *auth )
|
|
|
|
{
|
2012-02-17 18:54:39 +04:00
|
|
|
if( auth ) {
|
|
|
|
MirallConfigFile cfgFile;
|
|
|
|
qDebug() << "Authenticating request!";
|
|
|
|
auth->setUser( cfgFile.ownCloudUser( _connection ) );
|
|
|
|
auth->setPassword( cfgFile.ownCloudPasswd( _connection ));
|
|
|
|
}
|
2011-10-06 18:55:28 +04:00
|
|
|
}
|
2011-10-05 19:49:03 +04:00
|
|
|
|
2012-04-06 10:53:57 +04:00
|
|
|
void ownCloudInfo::slotSSLFailed( QNetworkReply *reply, QList<QSslError> errors )
|
|
|
|
{
|
|
|
|
qDebug() << "SSL-Errors happened for url " << reply->url().toString();
|
|
|
|
|
|
|
|
SslErrorDialog dialog;
|
|
|
|
dialog.setErrorList( errors );
|
|
|
|
|
|
|
|
if( dialog.exec() == QDialog::Accepted ) {
|
|
|
|
reply->ignoreSslErrors();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-10-05 19:49:03 +04:00
|
|
|
void ownCloudInfo::slotReplyFinished( QNetworkReply *reply )
|
|
|
|
{
|
2012-04-10 16:51:45 +04:00
|
|
|
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( _versionInfoCall ) {
|
|
|
|
// it was a call to status.php
|
|
|
|
|
|
|
|
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.
|
|
|
|
emit ownCloudDirExists( _directory, reply );
|
|
|
|
}
|
|
|
|
reply->deleteLater();
|
2011-10-05 19:49:03 +04:00
|
|
|
}
|
|
|
|
|
2011-10-12 17:14:39 +04:00
|
|
|
void ownCloudInfo::slotError( QNetworkReply::NetworkError err)
|
2011-10-05 19:49:03 +04:00
|
|
|
{
|
2012-03-30 22:42:40 +04:00
|
|
|
qDebug() << "ownCloudInfo Network Error: " << err;
|
2011-10-05 19:49:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|