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 "mirall/connectionvalidator.h"
|
|
|
|
#include "mirall/mirallconfigfile.h"
|
|
|
|
#include "mirall/theme.h"
|
2013-10-23 16:48:44 +04:00
|
|
|
#include "mirall/account.h"
|
|
|
|
#include "mirall/networkjobs.h"
|
2013-05-04 18:01:45 +04:00
|
|
|
|
|
|
|
namespace Mirall {
|
|
|
|
|
2013-10-23 16:48:44 +04:00
|
|
|
ConnectionValidator::ConnectionValidator(Account *account, QObject *parent)
|
2013-07-30 16:37:46 +04:00
|
|
|
: QObject(parent),
|
2013-10-23 16:48:44 +04:00
|
|
|
_account(account),
|
2013-10-11 13:43:23 +04:00
|
|
|
_networkError(QNetworkReply::NoError)
|
2013-05-04 18:01:45 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList ConnectionValidator::errors() const
|
|
|
|
{
|
|
|
|
return _errors;
|
|
|
|
}
|
|
|
|
|
2013-10-11 13:43:23 +04:00
|
|
|
bool ConnectionValidator::networkError() const
|
|
|
|
{
|
|
|
|
return _networkError;
|
|
|
|
}
|
|
|
|
|
2013-07-05 15:14:48 +04:00
|
|
|
QString ConnectionValidator::statusString( Status stat ) const
|
2013-05-04 18:01:45 +04:00
|
|
|
{
|
2013-07-05 15:14:48 +04:00
|
|
|
QString re;
|
|
|
|
|
|
|
|
switch( stat ) {
|
|
|
|
case Undefined:
|
|
|
|
re = QLatin1String("Undefined");
|
|
|
|
break;
|
|
|
|
case Connected:
|
|
|
|
re = QLatin1String("Connected");
|
|
|
|
break;
|
|
|
|
case NotConfigured:
|
|
|
|
re = QLatin1String("NotConfigured");
|
|
|
|
break;
|
|
|
|
case ServerVersionMismatch:
|
|
|
|
re = QLatin1String("Server Version Mismatch");
|
|
|
|
break;
|
|
|
|
case CredentialsTooManyAttempts:
|
|
|
|
re = QLatin1String("Credentials Too Many Attempts");
|
|
|
|
break;
|
|
|
|
case CredentialError:
|
|
|
|
re = QLatin1String("CredentialError");
|
|
|
|
break;
|
|
|
|
case CredentialsUserCanceled:
|
|
|
|
re = QLatin1String("Credential User Canceled");
|
|
|
|
break;
|
|
|
|
case CredentialsWrong:
|
|
|
|
re = QLatin1String("Credentials Wrong");
|
|
|
|
break;
|
|
|
|
case StatusNotFound:
|
|
|
|
re = QLatin1String("Status not found");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
re = QLatin1String("status undeclared.");
|
|
|
|
}
|
|
|
|
return re;
|
2013-05-04 18:01:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ConnectionValidator::checkConnection()
|
|
|
|
{
|
2013-11-04 19:36:23 +04:00
|
|
|
if( _account ) {
|
2013-10-23 16:48:44 +04:00
|
|
|
CheckServerJob *checkJob = new CheckServerJob(_account, false, this);
|
2013-10-28 23:01:59 +04:00
|
|
|
connect(checkJob, SIGNAL(instanceFound(QUrl,QVariantMap)), SLOT(slotStatusFound(QUrl,QVariantMap)));
|
|
|
|
connect(checkJob, SIGNAL(networkError(QNetworkReply*)), SLOT(slotNoStatusFound(QNetworkReply*)));
|
2013-11-14 22:20:09 +04:00
|
|
|
checkJob->start();
|
2013-05-04 18:01:45 +04:00
|
|
|
} else {
|
2013-10-23 16:48:44 +04:00
|
|
|
_errors << tr("No ownCloud account configured");
|
2013-05-04 18:01:45 +04:00
|
|
|
emit connectionResult( NotConfigured );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-28 23:01:59 +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: "
|
2013-10-28 23:01:59 +04:00
|
|
|
<< 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
|
|
|
// now check the authentication
|
|
|
|
|
2013-10-23 16:48:44 +04:00
|
|
|
if( CheckServerJob::version(info).startsWith("4.0") ) {
|
2013-09-11 12:24:31 +04:00
|
|
|
_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.") );
|
2013-05-04 18:01:45 +04:00
|
|
|
emit connectionResult( ServerVersionMismatch );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-29 16:28:19 +04:00
|
|
|
QTimer::singleShot( 0, this, SLOT( slotCheckAuthentication() ));
|
2013-05-04 18:01:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// status.php could not be loaded.
|
2013-10-28 23:01:59 +04:00
|
|
|
void ConnectionValidator::slotNoStatusFound(QNetworkReply *reply)
|
2013-05-04 18:01:45 +04:00
|
|
|
{
|
2013-11-07 15:22:17 +04:00
|
|
|
_account->setOnline(false);
|
|
|
|
|
2013-10-23 16:48:44 +04:00
|
|
|
// ### TODO
|
|
|
|
_errors.append(tr("Unable to connect to %1").arg(_account->url().toString()));
|
2013-10-28 23:01:59 +04:00
|
|
|
_errors.append( reply->errorString() );
|
|
|
|
_networkError = (reply->error() != QNetworkReply::NoError);
|
2013-05-04 18:01:45 +04:00
|
|
|
emit connectionResult( StatusNotFound );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConnectionValidator::slotCheckAuthentication()
|
|
|
|
{
|
|
|
|
// simply GET the webdav root, will fail if credentials are wrong.
|
|
|
|
// continue in slotAuthCheck here :-)
|
2013-11-14 22:20:09 +04:00
|
|
|
PropfindJob *propFind = new PropfindJob(_account, "/", this);
|
|
|
|
propFind->setProperties(QList<QByteArray>() << "getlastmodified");
|
2013-10-23 16:48:44 +04:00
|
|
|
connect(propFind, SIGNAL(result(QVariantMap)), SLOT(slotAuthSuccess()));
|
2013-10-28 23:01:59 +04:00
|
|
|
connect(propFind, SIGNAL(networkError(QNetworkReply*)), SLOT(slotAuthFailed(QNetworkReply*)));
|
2013-11-14 22:20:09 +04:00
|
|
|
propFind->start();
|
2013-10-23 16:48:44 +04:00
|
|
|
qDebug() << "# checking for authentication settings.";
|
2013-05-04 18:01:45 +04:00
|
|
|
}
|
|
|
|
|
2013-10-28 23:01:59 +04:00
|
|
|
void ConnectionValidator::slotAuthFailed(QNetworkReply *reply)
|
2013-05-04 18:01:45 +04:00
|
|
|
{
|
2013-10-23 16:48:44 +04:00
|
|
|
Status stat = StatusNotFound;
|
2013-05-04 18:01:45 +04:00
|
|
|
|
2013-10-28 23:01:59 +04:00
|
|
|
if( reply->error() == QNetworkReply::AuthenticationRequiredError ||
|
|
|
|
reply->error() == QNetworkReply::OperationCanceledError ) { // returned if the user is wrong.
|
2013-05-04 18:01:45 +04:00
|
|
|
qDebug() << "******** Password is wrong!";
|
2013-09-11 12:24:31 +04:00
|
|
|
_errors << tr("The provided credentials are not correct");
|
2013-05-04 18:01:45 +04:00
|
|
|
stat = CredentialsWrong;
|
2013-11-07 15:22:17 +04:00
|
|
|
_account->setOnline(false);
|
2013-10-28 23:01:59 +04:00
|
|
|
} else if( reply->error() != QNetworkReply::NoError ) {
|
|
|
|
_errors << reply->errorString();
|
2013-05-04 18:01:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
emit connectionResult( stat );
|
|
|
|
}
|
|
|
|
|
2013-10-23 16:48:44 +04:00
|
|
|
void ConnectionValidator::slotAuthSuccess()
|
|
|
|
{
|
2013-11-07 15:22:17 +04:00
|
|
|
_account->setOnline(true);
|
2013-10-23 16:48:44 +04:00
|
|
|
emit connectionResult(Connected);
|
2013-05-04 18:01:45 +04:00
|
|
|
}
|
2013-10-23 16:48:44 +04:00
|
|
|
|
|
|
|
} // namespace Mirall
|