1
0
Fork 0
mirror of https://github.com/nextcloud/desktop.git synced 2024-12-22 21:50:30 +03:00
nextcloud-desktop/src/libsync/connectionvalidator.cpp

162 lines
5.1 KiB
C++
Raw Normal View History

2013-05-04 18:01:45 +04:00
/*
* Copyright (C) by Klaas Freitag <freitag@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; 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 <QtCore>
2013-10-21 23:42:52 +04:00
#include <QNetworkReply>
2013-05-04 18:01:45 +04:00
#include "connectionvalidator.h"
#include "theme.h"
#include "account.h"
#include "networkjobs.h"
#include <creds/abstractcredentials.h>
2013-05-04 18:01:45 +04:00
2014-11-10 00:34:07 +03:00
namespace OCC {
2013-05-04 18:01:45 +04:00
ConnectionValidator::ConnectionValidator(AccountPtr account, QObject *parent)
: QObject(parent),
_account(account)
2013-05-04 18:01:45 +04:00
{
}
QString ConnectionValidator::statusString( Status stat )
2013-05-04 18:01:45 +04:00
{
switch( stat ) {
case Undefined:
return QLatin1String("Undefined");
case Connected:
return QLatin1String("Connected");
case NotConfigured:
return QLatin1String("NotConfigured");
case ServerVersionMismatch:
return QLatin1String("Server Version Mismatch");
case CredentialsWrong:
return QLatin1String("Credentials Wrong");
case StatusNotFound:
return QLatin1String("Status not found");
case Timeout:
return QLatin1String("Timeout");
}
return QLatin1String("status undeclared.");
2013-05-04 18:01:45 +04:00
}
bool ConnectionValidator::isNetworkError( Status status )
{
return status == Timeout;
}
2013-05-04 18:01:45 +04:00
void ConnectionValidator::checkServerAndAuth()
2013-05-04 18:01:45 +04:00
{
if( !_account ) {
_errors << tr("No ownCloud account configured");
reportResult( NotConfigured );
return;
}
CheckServerJob *checkJob = new CheckServerJob(_account, this);
checkJob->setIgnoreCredentialFailure(true);
connect(checkJob, SIGNAL(instanceFound(QUrl,QVariantMap)), SLOT(slotStatusFound(QUrl,QVariantMap)));
connect(checkJob, SIGNAL(networkError(QNetworkReply*)), SLOT(slotNoStatusFound(QNetworkReply*)));
connect(checkJob, SIGNAL(timeout(QUrl)), SLOT(slotJobTimeout(QUrl)));
checkJob->start();
2013-05-04 18:01:45 +04:00
}
void ConnectionValidator::slotStatusFound(const QUrl&url, const QVariantMap &info)
2013-05-04 18:01:45 +04:00
{
// status.php was found.
2013-10-23 16:48:44 +04:00
qDebug() << "** Application: ownCloud found: "
<< url << " with version "
2013-10-23 16:48:44 +04:00
<< CheckServerJob::versionString(info)
<< "(" << CheckServerJob::version(info) << ")";
2013-05-04 18:01:45 +04:00
2013-10-23 16:48:44 +04:00
if( CheckServerJob::version(info).startsWith("4.0") ) {
_errors.append( tr("The configured server for this client is too old") );
_errors.append( tr("Please update to the latest server and restart the client.") );
reportResult( ServerVersionMismatch );
2013-05-04 18:01:45 +04:00
return;
}
// now check the authentication
AbstractCredentials *creds = _account->credentials();
if (creds->ready()) {
QTimer::singleShot( 0, this, SLOT( checkAuthentication() ));
} else {
connect( creds, SIGNAL(fetched()),
this, SLOT(checkAuthentication()), Qt::UniqueConnection);
creds->fetch(_account);
}
2013-05-04 18:01:45 +04:00
}
// status.php could not be loaded.
void ConnectionValidator::slotNoStatusFound(QNetworkReply *reply)
2013-05-04 18:01:45 +04:00
{
2013-10-23 16:48:44 +04:00
_errors.append(tr("Unable to connect to %1").arg(_account->url().toString()));
_errors.append( reply->errorString() );
reportResult( StatusNotFound );
}
void ConnectionValidator::slotJobTimeout(const QUrl &url)
{
_errors.append(tr("Unable to connect to %1").arg(url.toString()));
_errors.append(tr("timeout"));
reportResult( Timeout );
2013-05-04 18:01:45 +04:00
}
void ConnectionValidator::checkAuthentication()
2013-05-04 18:01:45 +04:00
{
AbstractCredentials *creds = _account->credentials();
disconnect( creds, SIGNAL(fetched()),
this, SLOT(checkAuthentication()));
2013-05-04 18:01:45 +04:00
// simply GET the webdav root, will fail if credentials are wrong.
// continue in slotAuthCheck here :-)
qDebug() << "# Check whether authenticated propfind works.";
PropfindJob *job = new PropfindJob(_account, "/", this);
job->setProperties(QList<QByteArray>() << "getlastmodified");
connect(job, SIGNAL(result(QVariantMap)), SLOT(slotAuthSuccess()));
connect(job, SIGNAL(networkError(QNetworkReply*)), SLOT(slotAuthFailed(QNetworkReply*)));
job->start();
2013-05-04 18:01:45 +04:00
}
void ConnectionValidator::slotAuthFailed(QNetworkReply *reply)
2013-05-04 18:01:45 +04:00
{
Status stat = Timeout;
2013-05-04 18:01:45 +04:00
if( reply->error() == QNetworkReply::AuthenticationRequiredError ||
reply->error() == QNetworkReply::OperationCanceledError ) { // returned if the user/pwd is wrong.
qDebug() << reply->error() << reply->errorString();
2013-05-04 18:01:45 +04:00
qDebug() << "******** Password is wrong!";
_errors << tr("The provided credentials are not correct");
2013-05-04 18:01:45 +04:00
stat = CredentialsWrong;
} else if( reply->error() != QNetworkReply::NoError ) {
_errors << reply->errorString();
2013-05-04 18:01:45 +04:00
}
reportResult( stat );
2013-05-04 18:01:45 +04:00
}
2013-10-23 16:48:44 +04:00
void ConnectionValidator::slotAuthSuccess()
{
_errors.clear();
reportResult(Connected);
}
void ConnectionValidator::reportResult(Status status)
{
emit connectionResult(status, _errors);
deleteLater();
2013-05-04 18:01:45 +04:00
}
2013-10-23 16:48:44 +04:00
2014-11-10 00:34:07 +03:00
} // namespace OCC