2011-09-30 19:42:28 +04:00
/*
2013-07-30 12:48:26 +04:00
* Copyright ( C ) by Klaas Freitag < freitag @ owncloud . com >
2013-07-22 16:54:14 +04:00
* Copyright ( C ) by Krzesimir Nowak < krzesimir @ endocode . com >
2011-09-30 19:42:28 +04:00
*
* 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 .
*/
2013-07-22 16:54:14 +04:00
# include <QAbstractButton>
2013-04-28 00:24:32 +04:00
# include <QtCore>
# include <QProcess>
# include <QMessageBox>
# include <QDesktopServices>
2017-10-19 14:49:40 +03:00
# include <QApplication>
2013-04-28 00:24:32 +04:00
2013-07-30 13:19:22 +04:00
# include "wizard/owncloudwizardcommon.h"
# include "wizard/owncloudwizard.h"
2014-07-11 02:31:24 +04:00
# include "owncloudsetupwizard.h"
2014-11-10 01:25:57 +03:00
# include "configfile.h"
2014-07-11 02:31:24 +04:00
# include "folderman.h"
2014-11-10 01:25:57 +03:00
# include "accessmanager.h"
2014-07-11 02:31:24 +04:00
# include "account.h"
# include "networkjobs.h"
# include "sslerrordialog.h"
2015-04-09 17:19:17 +03:00
# include "accountmanager.h"
2016-09-26 16:45:30 +03:00
# include "clientproxy.h"
2016-11-23 13:05:41 +03:00
# include "filesystem.h"
2017-04-20 09:55:44 +03:00
# include "owncloudgui.h"
2013-10-23 16:48:44 +04:00
2013-10-28 23:01:59 +04:00
# include "creds/credentialsfactory.h"
2013-07-30 13:19:22 +04:00
# include "creds/abstractcredentials.h"
# include "creds/dummycredentials.h"
2011-09-28 20:14:48 +04:00
2014-11-10 00:34:07 +03:00
namespace OCC {
2011-09-28 20:14:48 +04:00
2013-07-31 17:46:41 +04:00
OwncloudSetupWizard : : OwncloudSetupWizard ( QObject * parent )
2012-02-17 18:54:39 +04:00
: QObject ( parent )
2013-07-31 17:46:41 +04:00
, _ocWizard ( new OwncloudWizard )
, _remoteFolder ( )
2011-09-28 20:14:48 +04:00
{
2017-09-20 11:14:48 +03:00
connect ( _ocWizard , & OwncloudWizard : : determineAuthType ,
2017-10-19 11:54:46 +03:00
this , & OwncloudSetupWizard : : slotCheckServer ) ;
2017-09-20 11:14:48 +03:00
connect ( _ocWizard , & OwncloudWizard : : connectToOCUrl ,
this , & OwncloudSetupWizard : : slotConnectToOCUrl ) ;
connect ( _ocWizard , & OwncloudWizard : : createLocalAndRemoteFolders ,
this , & OwncloudSetupWizard : : slotCreateLocalAndRemoteFolders ) ;
2013-08-19 14:43:52 +04:00
/* basicSetupFinished might be called from a reply from the network.
slotAssistantFinished might destroy the temporary QNetworkAccessManager .
Therefore Qt : : QueuedConnection is required */
2017-09-20 11:14:48 +03:00
connect ( _ocWizard , & OwncloudWizard : : basicSetupFinished ,
this , & OwncloudSetupWizard : : slotAssistantFinished , Qt : : QueuedConnection ) ;
connect ( _ocWizard , & QDialog : : finished , this , & QObject : : deleteLater ) ;
connect ( _ocWizard , & OwncloudWizard : : skipFolderConfiguration , this , & OwncloudSetupWizard : : slotSkipFolderConfiguration ) ;
2012-04-12 13:37:48 +04:00
}
OwncloudSetupWizard : : ~ OwncloudSetupWizard ( )
{
2013-05-29 03:20:43 +04:00
_ocWizard - > deleteLater ( ) ;
2013-04-28 00:24:32 +04:00
}
2012-07-26 12:16:18 +04:00
2017-04-20 09:55:44 +03:00
static QPointer < OwncloudSetupWizard > wiz = 0 ;
2013-07-22 15:59:52 +04:00
void OwncloudSetupWizard : : runWizard ( QObject * obj , const char * amember , QWidget * parent )
2013-07-04 21:59:40 +04:00
{
2013-09-19 13:51:02 +04:00
if ( ! wiz . isNull ( ) ) {
2017-10-20 12:19:23 +03:00
bringWizardToFrontIfVisible ( ) ;
2013-09-19 13:51:02 +04:00
return ;
}
wiz = new OwncloudSetupWizard ( parent ) ;
2013-07-04 21:59:40 +04:00
connect ( wiz , SIGNAL ( ownCloudWizardDone ( int ) ) , obj , amember ) ;
2013-07-22 15:59:52 +04:00
FolderMan : : instance ( ) - > setSyncEnabled ( false ) ;
2013-07-04 21:59:40 +04:00
wiz - > startWizard ( ) ;
}
2017-04-20 09:55:44 +03:00
bool OwncloudSetupWizard : : bringWizardToFrontIfVisible ( )
{
if ( wiz . isNull ( ) ) {
return false ;
}
2017-10-19 14:49:40 +03:00
if ( wiz - > _ocWizard - > currentId ( ) = = WizardCommon : : Page_ShibbolethCreds ) {
// Try to find if there is a browser open and raise that instead (Issue #6105)
const auto allWindow = qApp - > topLevelWidgets ( ) ;
auto it = std : : find_if ( allWindow . cbegin ( ) , allWindow . cend ( ) , [ ] ( QWidget * w )
{ return QLatin1String ( w - > metaObject ( ) - > className ( ) ) = = QLatin1String ( " OCC::ShibbolethWebView " ) ; } ) ;
if ( it ! = allWindow . cend ( ) ) {
ownCloudGui : : raiseDialog ( * it ) ;
return true ;
}
}
2017-04-20 09:55:44 +03:00
ownCloudGui : : raiseDialog ( wiz - > _ocWizard ) ;
return true ;
}
2013-03-02 00:30:22 +04:00
void OwncloudSetupWizard : : startWizard ( )
{
2015-07-16 15:21:51 +03:00
AccountPtr account = AccountManager : : createAccount ( ) ;
2015-04-09 17:19:17 +03:00
account - > setCredentials ( CredentialsFactory : : create ( " dummy " ) ) ;
2015-08-29 19:49:10 +03:00
account - > setUrl ( Theme : : instance ( ) - > overrideServerUrl ( ) ) ;
2013-10-28 23:01:59 +04:00
_ocWizard - > setAccount ( account ) ;
2013-11-04 19:36:23 +04:00
_ocWizard - > setOCUrl ( account - > url ( ) . toString ( ) ) ;
2013-05-26 16:04:11 +04:00
2013-04-28 00:24:32 +04:00
_remoteFolder = Theme : : instance ( ) - > defaultServerFolder ( ) ;
// remoteFolder may be empty, which means /
2013-05-23 03:51:05 +04:00
QString localFolder = Theme : : instance ( ) - > defaultClientFolder ( ) ;
2013-04-28 00:24:32 +04:00
// if its a relative path, prepend with users home dir, otherwise use as absolute path
2013-11-26 03:00:27 +04:00
2013-07-31 17:46:41 +04:00
if ( ! QDir ( localFolder ) . isAbsolute ( ) ) {
localFolder = QDir : : homePath ( ) + QDir : : separator ( ) + localFolder ;
2013-04-28 00:24:32 +04:00
}
2013-11-26 03:00:27 +04:00
2013-05-23 03:51:05 +04:00
_ocWizard - > setProperty ( " localFolder " , localFolder ) ;
2014-07-08 20:32:43 +04:00
// remember the local folder to compare later if it changed, but clean first
QString lf = QDir : : fromNativeSeparators ( localFolder ) ;
if ( ! lf . endsWith ( QLatin1Char ( ' / ' ) ) ) {
lf . append ( QLatin1Char ( ' / ' ) ) ;
}
_initLocalFolder = lf ;
2013-05-23 03:51:05 +04:00
_ocWizard - > setRemoteFolder ( _remoteFolder ) ;
2013-03-02 00:30:22 +04:00
2013-07-31 17:46:41 +04:00
_ocWizard - > setStartId ( WizardCommon : : Page_ServerSetup ) ;
2013-03-02 00:30:22 +04:00
_ocWizard - > restart ( ) ;
2013-05-28 22:25:31 +04:00
2013-07-04 21:59:40 +04:00
_ocWizard - > open ( ) ;
2013-06-24 17:15:46 +04:00
_ocWizard - > raise ( ) ;
2013-03-02 00:30:22 +04:00
}
2013-10-28 23:01:59 +04:00
// also checks if an installation is valid and determines auth type in a second step
2017-10-19 11:54:46 +03:00
void OwncloudSetupWizard : : slotCheckServer ( const QString & urlString )
2012-06-12 14:30:05 +04:00
{
2013-10-28 23:01:59 +04:00
QString fixedUrl = urlString ;
QUrl url = QUrl : : fromUserInput ( fixedUrl ) ;
// fromUserInput defaults to http, not http if no scheme is specified
2013-10-30 19:31:47 +04:00
if ( ! fixedUrl . startsWith ( " http:// " ) & & ! fixedUrl . startsWith ( " https:// " ) ) {
2013-10-28 23:01:59 +04:00
url . setScheme ( " https " ) ;
2013-07-31 17:46:41 +04:00
}
2014-12-18 14:09:48 +03:00
AccountPtr account = _ocWizard - > account ( ) ;
2013-10-28 23:01:59 +04:00
account - > setUrl ( url ) ;
2015-03-24 23:30:42 +03:00
// Reset the proxy which might had been determined previously in ConnectionValidator::checkServerAndAuth()
// when there was a previous account.
2016-09-26 16:45:30 +03:00
account - > networkAccessManager ( ) - > setProxy ( QNetworkProxy ( QNetworkProxy : : NoProxy ) ) ;
// Lookup system proxy in a thread https://github.com/owncloud/client/issues/2993
if ( ClientProxy : : isUsingSystemDefault ( ) ) {
2017-05-09 15:24:11 +03:00
qCDebug ( lcWizard ) < < " Trying to look up system proxy " ;
2016-09-26 16:45:30 +03:00
ClientProxy : : lookupSystemProxyAsync ( account - > url ( ) ,
this , SLOT ( slotSystemProxyLookupDone ( QNetworkProxy ) ) ) ;
} else {
// We want to reset the QNAM proxy so that the global proxy settings are used (via ClientProxy settings)
account - > networkAccessManager ( ) - > setProxy ( QNetworkProxy ( QNetworkProxy : : DefaultProxy ) ) ;
// use a queued invocation so we're as asynchronous as with the other code path
2017-10-19 11:54:46 +03:00
QMetaObject : : invokeMethod ( this , " slotFindServer " , Qt : : QueuedConnection ) ;
2016-09-26 16:45:30 +03:00
}
}
void OwncloudSetupWizard : : slotSystemProxyLookupDone ( const QNetworkProxy & proxy )
{
if ( proxy . type ( ) ! = QNetworkProxy : : NoProxy ) {
2017-03-30 14:46:20 +03:00
qCInfo ( lcWizard ) < < " Setting QNAM proxy to be system proxy " < < printQNetworkProxy ( proxy ) ;
2016-09-26 16:45:30 +03:00
} else {
2017-03-30 14:46:20 +03:00
qCInfo ( lcWizard ) < < " No system proxy set by OS " ;
2016-09-26 16:45:30 +03:00
}
AccountPtr account = _ocWizard - > account ( ) ;
account - > networkAccessManager ( ) - > setProxy ( proxy ) ;
2017-10-19 11:54:46 +03:00
slotFindServer ( ) ;
2016-09-26 16:45:30 +03:00
}
2017-10-19 11:54:46 +03:00
void OwncloudSetupWizard : : slotFindServer ( )
2016-09-26 16:45:30 +03:00
{
AccountPtr account = _ocWizard - > account ( ) ;
2015-10-05 07:21:19 +03:00
// Set fake credentials before we check what credential it actually is.
2014-07-23 16:38:50 +04:00
account - > setCredentials ( CredentialsFactory : : create ( " dummy " ) ) ;
2017-09-11 14:45:01 +03:00
2017-10-19 11:54:46 +03:00
// Determining the actual server URL can be a multi-stage process
// 1. Check url/status.php with CheckServerJob
// If that works we're done. In that case we don't check the
// url directly for redirects, see #5954.
// 2. Check the url for permanent redirects (like url shorteners)
// 3. Check redirected-url/status.php with CheckServerJob
// Step 1: Check url/status.php
CheckServerJob * job = new CheckServerJob ( account , this ) ;
job - > setIgnoreCredentialFailure ( true ) ;
connect ( job , & CheckServerJob : : instanceFound , this , & OwncloudSetupWizard : : slotFoundServer ) ;
connect ( job , & CheckServerJob : : instanceNotFound , this , & OwncloudSetupWizard : : slotFindServerBehindRedirect ) ;
connect ( job , & CheckServerJob : : timeout , this , & OwncloudSetupWizard : : slotNoServerFoundTimeout ) ;
job - > setTimeout ( ( account - > url ( ) . scheme ( ) = = " https " ) ? 30 * 1000 : 10 * 1000 ) ;
job - > start ( ) ;
// Step 2 and 3 are in slotFindServerBehindRedirect()
}
void OwncloudSetupWizard : : slotFindServerBehindRedirect ( )
{
AccountPtr account = _ocWizard - > account ( ) ;
// Step 2: Resolve any permanent redirect chains on the base url
2017-09-11 14:45:01 +03:00
auto redirectCheckJob = account - > sendRequest ( " GET " , account - > url ( ) ) ;
2017-09-15 10:11:52 +03:00
// Use a significantly reduced timeout for this redirect check:
// the 5-minute default is inappropriate.
redirectCheckJob - > setTimeout ( qMin ( 2000ll , redirectCheckJob - > timeoutMsec ( ) ) ) ;
2017-09-11 14:45:01 +03:00
// Grab the chain of permanent redirects and adjust the account url
// accordingly
auto permanentRedirects = std : : make_shared < int > ( 0 ) ;
connect ( redirectCheckJob , & AbstractNetworkJob : : redirected , this ,
[ permanentRedirects , account ] ( QNetworkReply * reply , const QUrl & targetUrl , int count ) {
int httpCode = reply - > attribute ( QNetworkRequest : : HttpStatusCodeAttribute ) . toInt ( ) ;
if ( count = = * permanentRedirects & & ( httpCode = = 301 | | httpCode = = 308 ) ) {
qCInfo ( lcWizard ) < < account - > url ( ) < < " was redirected to " < < targetUrl ;
account - > setUrl ( targetUrl ) ;
* permanentRedirects + = 1 ;
}
} ) ;
2017-10-19 11:54:46 +03:00
// Step 3: When done, start checking status.php.
2017-09-11 14:45:01 +03:00
connect ( redirectCheckJob , & SimpleNetworkJob : : finishedSignal , this ,
[ this , account ] ( ) {
CheckServerJob * job = new CheckServerJob ( account , this ) ;
job - > setIgnoreCredentialFailure ( true ) ;
2017-10-19 11:54:46 +03:00
connect ( job , & CheckServerJob : : instanceFound , this , & OwncloudSetupWizard : : slotFoundServer ) ;
connect ( job , & CheckServerJob : : instanceNotFound , this , & OwncloudSetupWizard : : slotNoServerFound ) ;
connect ( job , & CheckServerJob : : timeout , this , & OwncloudSetupWizard : : slotNoServerFoundTimeout ) ;
2017-09-11 14:45:01 +03:00
job - > setTimeout ( ( account - > url ( ) . scheme ( ) = = " https " ) ? 30 * 1000 : 10 * 1000 ) ;
job - > start ( ) ;
2017-10-19 11:54:46 +03:00
} ) ;
2013-07-31 17:46:41 +04:00
}
2013-05-26 16:04:11 +04:00
2017-10-19 11:54:46 +03:00
void OwncloudSetupWizard : : slotFoundServer ( const QUrl & url , const QJsonObject & info )
2013-07-31 17:46:41 +04:00
{
2016-06-28 13:25:04 +03:00
auto serverVersion = CheckServerJob : : version ( info ) ;
2013-07-31 17:46:41 +04:00
_ocWizard - > appendToConfigurationLog ( tr ( " <font color= \" green \" >Successfully connected to %1: %2 version %3 (%4)</font><br/><br/> " )
2017-02-23 16:54:17 +03:00
. arg ( Utility : : escape ( url . toString ( ) ) ,
Utility : : escape ( Theme : : instance ( ) - > appNameGUI ( ) ) ,
Utility : : escape ( CheckServerJob : : versionString ( info ) ) ,
Utility : : escape ( serverVersion ) ) ) ;
2016-06-28 13:25:04 +03:00
2017-04-19 12:02:03 +03:00
// Note with newer servers we get the version actually only later in capabilities
// https://github.com/owncloud/core/pull/27473/files
2016-06-28 13:25:04 +03:00
_ocWizard - > account ( ) - > setServerVersion ( serverVersion ) ;
2013-07-31 17:46:41 +04:00
2017-09-08 12:59:45 +03:00
if ( url ! = _ocWizard - > account ( ) - > url ( ) ) {
2013-11-14 20:54:38 +04:00
// We might be redirected, update the account
2017-09-08 12:59:45 +03:00
_ocWizard - > account ( ) - > setUrl ( url ) ;
qCInfo ( lcWizard ) < < " was redirected to " < < url . toString ( ) ;
2013-11-14 20:54:38 +04:00
}
2017-10-19 11:54:46 +03:00
slotDetermineAuthType ( ) ;
2013-07-31 17:46:41 +04:00
}
2012-06-12 14:30:05 +04:00
2017-10-19 11:54:46 +03:00
void OwncloudSetupWizard : : slotNoServerFound ( QNetworkReply * reply )
2013-07-31 17:46:41 +04:00
{
2017-03-23 17:53:22 +03:00
auto job = qobject_cast < CheckServerJob * > ( sender ( ) ) ;
2016-11-23 13:43:15 +03:00
int resultCode = reply - > attribute ( QNetworkRequest : : HttpStatusCodeAttribute ) . toInt ( ) ;
QString contentType = reply - > header ( QNetworkRequest : : ContentTypeHeader ) . toString ( ) ;
// Do this early because reply might be deleted in message box event loop
2016-12-21 11:34:54 +03:00
QString msg ;
if ( ! _ocWizard - > account ( ) - > url ( ) . isValid ( ) ) {
msg = tr ( " Invalid URL " ) ;
} else {
msg = tr ( " Failed to connect to %1 at %2:<br/>%3 " )
2017-02-23 16:54:17 +03:00
. arg ( Utility : : escape ( Theme : : instance ( ) - > appNameGUI ( ) ) ,
Utility : : escape ( reply - > url ( ) . toString ( ) ) ,
2017-03-23 17:53:22 +03:00
Utility : : escape ( job - > errorString ( ) ) ) ;
2016-12-21 11:34:54 +03:00
}
2016-11-23 13:43:15 +03:00
bool isDowngradeAdvised = checkDowngradeAdvised ( reply ) ;
// If a client cert is needed, nginx sends:
// 400 "<html>\r\n<head><title>400 No required SSL certificate was sent</title></head>\r\n<body bgcolor=\"white\">\r\n<center><h1>400 Bad Request</h1></center>\r\n<center>No required SSL certificate was sent</center>\r\n<hr><center>nginx/1.10.0</center>\r\n</body>\r\n</html>\r\n"
// If the IP needs to be added as "trusted domain" in oC, oC sends:
// https://gist.github.com/guruz/ab6d11df1873c2ad3932180de92e7d82
if ( resultCode ! = 200 & & contentType . startsWith ( " text/ " ) ) {
// FIXME: Synchronous dialogs are not so nice because of event loop recursion
// (we already create a dialog further below)
QString serverError = reply - > peek ( 1024 * 20 ) ;
2017-05-09 15:24:11 +03:00
qCDebug ( lcWizard ) < < serverError ;
2016-11-23 13:43:15 +03:00
QMessageBox messageBox ( _ocWizard ) ;
messageBox . setText ( serverError ) ;
messageBox . addButton ( QMessageBox : : Ok ) ;
messageBox . setTextFormat ( Qt : : RichText ) ;
messageBox . exec ( ) ;
}
// Displays message inside wizard and possibly also another message box
_ocWizard - > displayError ( msg , isDowngradeAdvised ) ;
2015-05-08 11:17:21 +03:00
// Allow the credentials dialog to pop up again for the same URL.
// Maybe the user just clicked 'Cancel' by accident or changed his mind.
2016-05-27 13:08:42 +03:00
_ocWizard - > account ( ) - > resetRejectedCertificates ( ) ;
2012-06-12 14:30:05 +04:00
}
2017-10-19 11:54:46 +03:00
void OwncloudSetupWizard : : slotNoServerFoundTimeout ( const QUrl & url )
2013-11-14 20:02:41 +04:00
{
2017-02-23 16:54:17 +03:00
_ocWizard - > displayError (
tr ( " Timeout while trying to connect to %1 at %2. " )
. arg ( Utility : : escape ( Theme : : instance ( ) - > appNameGUI ( ) ) , Utility : : escape ( url . toString ( ) ) ) ,
2017-10-19 11:54:46 +03:00
false ) ;
}
void OwncloudSetupWizard : : slotDetermineAuthType ( )
{
DetermineAuthTypeJob * job = new DetermineAuthTypeJob ( _ocWizard - > account ( ) , this ) ;
job - > setIgnoreCredentialFailure ( true ) ;
connect ( job , & DetermineAuthTypeJob : : authType ,
_ocWizard , & OwncloudWizard : : setAuthType ) ;
job - > start ( ) ;
2013-11-14 20:02:41 +04:00
}
2012-02-17 14:11:18 +04:00
void OwncloudSetupWizard : : slotConnectToOCUrl ( const QString & url )
2011-09-28 20:14:48 +04:00
{
2017-03-30 14:46:20 +03:00
qCInfo ( lcWizard ) < < " Connect to url: " < < url ;
2014-05-15 11:45:26 +04:00
AbstractCredentials * creds = _ocWizard - > getCredentials ( ) ;
_ocWizard - > account ( ) - > setCredentials ( creds ) ;
2013-10-30 19:31:47 +04:00
_ocWizard - > setField ( QLatin1String ( " OCUrl " ) , url ) ;
_ocWizard - > appendToConfigurationLog ( tr ( " Trying to connect to %1 at %2... " )
. arg ( Theme : : instance ( ) - > appNameGUI ( ) )
. arg ( url ) ) ;
2013-10-28 23:01:59 +04:00
2013-10-30 19:31:47 +04:00
testOwnCloudConnect ( ) ;
2012-02-17 18:54:39 +04:00
}
void OwncloudSetupWizard : : testOwnCloudConnect ( )
{
2014-12-18 14:09:48 +03:00
AccountPtr account = _ocWizard - > account ( ) ;
2014-05-15 11:45:26 +04:00
2015-03-03 18:01:41 +03:00
auto * job = new PropfindJob ( account , " / " , this ) ;
2013-11-25 18:50:19 +04:00
job - > setIgnoreCredentialFailure ( true ) ;
2017-02-22 15:04:26 +03:00
// There is custom redirect handling in the error handler,
// so don't automatically follow redirects.
job - > setFollowRedirects ( false ) ;
2015-03-03 18:01:41 +03:00
job - > setProperties ( QList < QByteArray > ( ) < < " getlastmodified " ) ;
2017-09-20 11:14:48 +03:00
connect ( job , & PropfindJob : : result , _ocWizard , & OwncloudWizard : : successfulStep ) ;
connect ( job , & PropfindJob : : finishedWithError , this , & OwncloudSetupWizard : : slotAuthError ) ;
2013-11-14 22:20:09 +04:00
job - > start ( ) ;
2012-02-17 18:54:39 +04:00
}
2015-04-09 14:38:52 +03:00
void OwncloudSetupWizard : : slotAuthError ( )
{
QString errorMsg ;
PropfindJob * job = qobject_cast < PropfindJob * > ( sender ( ) ) ;
if ( ! job ) {
2017-05-09 15:24:11 +03:00
qCWarning ( lcWizard ) < < " Can't check for authed redirects. This slot should be invoked from PropfindJob! " ;
2015-04-09 14:38:52 +03:00
return ;
}
2015-04-24 16:20:13 +03:00
QNetworkReply * reply = job - > reply ( ) ;
2015-04-09 14:38:52 +03:00
// If there were redirects on the *authed* requests, also store
// the updated server URL, similar to redirects on status.php.
2015-04-24 16:20:13 +03:00
QUrl redirectUrl = reply - > attribute ( QNetworkRequest : : RedirectionTargetAttribute ) . toUrl ( ) ;
2015-04-09 14:38:52 +03:00
if ( ! redirectUrl . isEmpty ( ) ) {
2017-03-30 14:46:20 +03:00
qCInfo ( lcWizard ) < < " Authed request was redirected to " < < redirectUrl . toString ( ) ;
2015-04-09 14:38:52 +03:00
// strip the expected path
QString path = redirectUrl . path ( ) ;
2015-04-10 10:24:25 +03:00
static QString expectedPath = " / " + _ocWizard - > account ( ) - > davPath ( ) ;
2015-04-09 14:38:52 +03:00
if ( path . endsWith ( expectedPath ) ) {
path . chop ( expectedPath . size ( ) ) ;
redirectUrl . setPath ( path ) ;
2017-03-30 14:46:20 +03:00
qCInfo ( lcWizard ) < < " Setting account url to " < < redirectUrl . toString ( ) ;
2015-04-09 14:38:52 +03:00
_ocWizard - > account ( ) - > setUrl ( redirectUrl ) ;
testOwnCloudConnect ( ) ;
return ;
}
2015-04-24 16:20:13 +03:00
errorMsg = tr ( " The authenticated request to the server was redirected to "
" '%1'. The URL is bad, the server is misconfigured. " )
2017-02-23 16:54:17 +03:00
. arg ( Utility : : escape ( redirectUrl . toString ( ) ) ) ;
2015-04-24 16:20:13 +03:00
// A 404 is actually a success: we were authorized to know that the folder does
// not exist. It will be created later...
} else if ( reply - > error ( ) = = QNetworkReply : : ContentNotFoundError ) {
_ocWizard - > successfulStep ( ) ;
return ;
// Provide messages for other errors, such as invalid credentials.
} else if ( reply - > error ( ) ! = QNetworkReply : : NoError ) {
if ( ! _ocWizard - > account ( ) - > credentials ( ) - > stillValid ( reply ) ) {
errorMsg = tr ( " Access forbidden by server. To verify that you have proper access, "
" <a href= \" %1 \" >click here</a> to access the service with your browser. " )
2017-02-23 16:54:17 +03:00
. arg ( Utility : : escape ( _ocWizard - > account ( ) - > url ( ) . toString ( ) ) ) ;
2015-06-03 16:35:26 +03:00
} else {
2017-03-23 17:53:22 +03:00
errorMsg = job - > errorStringParsingBody ( ) ;
2015-04-24 16:20:13 +03:00
}
2015-04-09 14:38:52 +03:00
2015-04-24 16:20:13 +03:00
// Something else went wrong, maybe the response was 200 but with invalid data.
} else {
2015-04-09 14:38:52 +03:00
errorMsg = tr ( " There was an invalid response to an authenticated webdav request " ) ;
}
2015-04-24 16:20:13 +03:00
2015-04-09 14:38:52 +03:00
_ocWizard - > show ( ) ;
2017-06-07 17:49:38 +03:00
if ( _ocWizard - > currentId ( ) = = WizardCommon : : Page_ShibbolethCreds | | _ocWizard - > currentId ( ) = = WizardCommon : : Page_OAuthCreds ) {
2015-04-24 16:20:13 +03:00
_ocWizard - > back ( ) ;
}
_ocWizard - > displayError ( errorMsg , _ocWizard - > currentId ( ) = = WizardCommon : : Page_ServerSetup & & checkDowngradeAdvised ( reply ) ) ;
2015-04-09 14:38:52 +03:00
}
2014-12-17 21:14:42 +03:00
bool OwncloudSetupWizard : : checkDowngradeAdvised ( QNetworkReply * reply )
{
2015-01-21 17:07:24 +03:00
if ( reply - > url ( ) . scheme ( ) ! = QLatin1String ( " https " ) ) {
2015-01-07 20:22:44 +03:00
return false ;
}
2014-12-17 21:14:42 +03:00
switch ( reply - > error ( ) ) {
case QNetworkReply : : NoError :
case QNetworkReply : : ContentNotFoundError :
2015-01-21 17:07:24 +03:00
case QNetworkReply : : AuthenticationRequiredError :
2014-12-17 21:14:42 +03:00
case QNetworkReply : : HostNotFoundError :
2015-01-07 20:22:44 +03:00
return false ;
2014-12-17 21:14:42 +03:00
default :
2015-01-08 11:42:26 +03:00
break ;
2014-12-17 21:14:42 +03:00
}
2015-01-07 20:22:44 +03:00
// Adhere to HSTS, even though we do not parse it properly
if ( reply - > hasRawHeader ( " Strict-Transport-Security " ) ) {
return false ;
}
return true ;
2014-12-17 21:14:42 +03:00
}
2013-07-31 17:46:41 +04:00
void OwncloudSetupWizard : : slotCreateLocalAndRemoteFolders ( const QString & localFolder , const QString & remoteFolder )
2011-09-30 16:19:26 +04:00
{
2017-03-30 14:46:20 +03:00
qCInfo ( lcWizard ) < < " Setup local sync folder for new oC connection " < < localFolder ;
2013-07-31 17:46:41 +04:00
const QDir fi ( localFolder ) ;
2013-10-30 19:31:47 +04:00
bool nextStep = true ;
2012-03-14 14:26:00 +04:00
if ( fi . exists ( ) ) {
2017-04-05 17:55:36 +03:00
FileSystem : : setFolderMinimumPermissions ( localFolder ) ;
2017-08-03 20:54:20 +03:00
Utility : : setupFavLink ( localFolder ) ;
2012-03-14 14:26:00 +04:00
// there is an existing local folder. If its non empty, it can only be synced if the
// ownCloud is newly created.
2017-02-23 16:54:17 +03:00
_ocWizard - > appendToConfigurationLog (
tr ( " Local sync folder %1 already exists, setting it up for sync.<br/><br/> " )
. arg ( Utility : : escape ( localFolder ) ) ) ;
2011-10-05 14:11:10 +04:00
} else {
2015-02-06 00:00:13 +03:00
QString res = tr ( " Creating local sync folder %1... " ) . arg ( localFolder ) ;
2013-05-23 03:51:05 +04:00
if ( fi . mkpath ( localFolder ) ) {
2016-11-23 13:05:41 +03:00
FileSystem : : setFolderMinimumPermissions ( localFolder ) ;
2013-05-23 03:51:05 +04:00
Utility : : setupFavLink ( localFolder ) ;
2012-03-14 14:26:00 +04:00
res + = tr ( " ok " ) ;
} else {
res + = tr ( " failed. " ) ;
2017-03-30 14:46:20 +03:00
qCWarning ( lcWizard ) < < " Failed to create " < < fi . path ( ) ;
2017-02-23 16:54:17 +03:00
_ocWizard - > displayError ( tr ( " Could not create local folder %1 " ) . arg ( Utility : : escape ( localFolder ) ) , false ) ;
2013-10-30 19:31:47 +04:00
nextStep = false ;
2012-03-14 14:26:00 +04:00
}
2013-04-28 00:24:32 +04:00
_ocWizard - > appendToConfigurationLog ( res ) ;
2012-03-14 14:26:00 +04:00
}
2013-10-30 19:31:47 +04:00
if ( nextStep ) {
2015-02-05 14:48:23 +03:00
EntityExistsJob * job = new EntityExistsJob ( _ocWizard - > account ( ) , _ocWizard - > account ( ) - > davPath ( ) + remoteFolder , this ) ;
2017-09-20 11:14:48 +03:00
connect ( job , & EntityExistsJob : : exists , this , & OwncloudSetupWizard : : slotRemoteFolderExists ) ;
2013-11-14 22:20:09 +04:00
job - > start ( ) ;
2013-10-30 19:31:47 +04:00
} else {
finalizeSetup ( false ) ;
}
2012-12-04 20:27:59 +04:00
}
2012-03-14 14:26:00 +04:00
2013-10-28 23:01:59 +04:00
// ### TODO move into EntityExistsJob once we decide if/how to return gui strings from jobs
2015-04-09 14:38:52 +03:00
void OwncloudSetupWizard : : slotRemoteFolderExists ( QNetworkReply * reply )
2013-04-28 00:24:32 +04:00
{
2017-03-23 17:53:22 +03:00
auto job = qobject_cast < EntityExistsJob * > ( sender ( ) ) ;
2013-04-28 00:24:32 +04:00
bool ok = true ;
QString error ;
QNetworkReply : : NetworkError errId = reply - > error ( ) ;
if ( errId = = QNetworkReply : : NoError ) {
2017-03-30 14:46:20 +03:00
qCInfo ( lcWizard ) < < " Remote folder found, all cool! " ;
2013-04-28 00:24:32 +04:00
} else if ( errId = = QNetworkReply : : ContentNotFoundError ) {
2013-10-28 23:01:59 +04:00
if ( _remoteFolder . isEmpty ( ) ) {
error = tr ( " No remote folder specified! " ) ;
2013-04-28 00:24:32 +04:00
ok = false ;
2013-10-28 23:01:59 +04:00
} else {
createRemoteFolder ( ) ;
2013-04-28 00:24:32 +04:00
}
} else {
2017-03-23 17:53:22 +03:00
error = tr ( " Error: %1 " ) . arg ( job - > errorString ( ) ) ;
2013-04-28 00:24:32 +04:00
ok = false ;
2012-12-09 01:56:48 +04:00
}
2013-04-28 00:24:32 +04:00
if ( ! ok ) {
2017-02-23 16:54:17 +03:00
_ocWizard - > displayError ( Utility : : escape ( error ) , false ) ;
2011-10-05 14:11:10 +04:00
}
2013-04-28 00:24:32 +04:00
finalizeSetup ( ok ) ;
2011-10-05 14:11:10 +04:00
}
2013-10-28 23:01:59 +04:00
void OwncloudSetupWizard : : createRemoteFolder ( )
2011-10-05 14:11:10 +04:00
{
2013-04-28 00:24:32 +04:00
_ocWizard - > appendToConfigurationLog ( tr ( " creating folder on ownCloud: %1 " ) . arg ( _remoteFolder ) ) ;
2012-04-18 13:59:56 +04:00
2013-10-28 23:01:59 +04:00
MkColJob * job = new MkColJob ( _ocWizard - > account ( ) , _remoteFolder , this ) ;
connect ( job , SIGNAL ( finished ( QNetworkReply : : NetworkError ) ) , SLOT ( slotCreateRemoteFolderFinished ( QNetworkReply : : NetworkError ) ) ) ;
2013-11-14 22:20:09 +04:00
job - > start ( ) ;
2011-10-05 14:11:10 +04:00
}
2012-04-21 01:58:09 +04:00
void OwncloudSetupWizard : : slotCreateRemoteFolderFinished ( QNetworkReply : : NetworkError error )
2012-03-14 14:26:00 +04:00
{
2017-05-09 15:24:11 +03:00
qCDebug ( lcWizard ) < < " ** webdav mkdir request finished " < < error ;
2013-10-30 19:31:47 +04:00
// disconnect(ownCloudInfo::instance(), SIGNAL(webdavColCreated(QNetworkReply::NetworkError)),
// this, SLOT(slotCreateRemoteFolderFinished(QNetworkReply::NetworkError)));
2013-04-28 00:24:32 +04:00
2012-08-06 18:43:24 +04:00
bool success = true ;
2012-03-14 14:26:00 +04:00
2012-04-21 01:58:09 +04:00
if ( error = = QNetworkReply : : NoError ) {
2013-04-28 00:24:32 +04:00
_ocWizard - > appendToConfigurationLog ( tr ( " Remote folder %1 created successfully. " ) . arg ( _remoteFolder ) ) ;
2012-04-21 01:58:09 +04:00
} else if ( error = = 202 ) {
2013-04-28 00:24:32 +04:00
_ocWizard - > appendToConfigurationLog ( tr ( " The remote folder %1 already exists. Connecting it for syncing. " ) . arg ( _remoteFolder ) ) ;
2012-08-06 18:43:24 +04:00
} else if ( error > 202 & & error < 300 ) {
2014-12-17 21:14:42 +03:00
_ocWizard - > displayError ( tr ( " The folder creation resulted in HTTP error code %1 " ) . arg ( ( int ) error ) , false ) ;
2017-05-17 11:55:42 +03:00
2013-04-28 00:24:32 +04:00
_ocWizard - > appendToConfigurationLog ( tr ( " The folder creation resulted in HTTP error code %1 " ) . arg ( ( int ) error ) ) ;
2012-04-21 01:58:09 +04:00
} else if ( error = = QNetworkReply : : OperationCanceledError ) {
2013-04-28 00:24:32 +04:00
_ocWizard - > displayError ( tr ( " The remote folder creation failed because the provided credentials "
" are wrong! "
2014-12-17 21:14:42 +03:00
" <br/>Please go back and check your credentials.</p> " ) ,
false ) ;
2013-04-28 00:24:32 +04:00
_ocWizard - > appendToConfigurationLog ( tr ( " <p><font color= \" red \" >Remote folder creation failed probably because the provided credentials are wrong.</font> "
2013-10-30 19:31:47 +04:00
" <br/>Please go back and check your credentials.</p> " ) ) ;
2012-06-12 14:30:05 +04:00
_remoteFolder . clear ( ) ;
2012-08-06 18:43:24 +04:00
success = false ;
2012-03-14 14:26:00 +04:00
} else {
2017-02-23 16:54:17 +03:00
_ocWizard - > appendToConfigurationLog ( tr ( " Remote folder %1 creation failed with error <tt>%2</tt>. " ) . arg ( Utility : : escape ( _remoteFolder ) ) . arg ( error ) ) ;
_ocWizard - > displayError ( tr ( " Remote folder %1 creation failed with error <tt>%2</tt>. " ) . arg ( Utility : : escape ( _remoteFolder ) ) . arg ( error ) , false ) ;
2012-06-12 14:30:05 +04:00
_remoteFolder . clear ( ) ;
2012-08-06 18:43:24 +04:00
success = false ;
2012-06-12 14:30:05 +04:00
}
2012-08-06 18:43:24 +04:00
finalizeSetup ( success ) ;
2012-06-12 14:30:05 +04:00
}
void OwncloudSetupWizard : : finalizeSetup ( bool success )
{
2013-02-06 13:21:50 +04:00
// enable/disable the finish button.
_ocWizard - > enableFinishOnResultWidget ( success ) ;
2013-05-23 03:51:05 +04:00
const QString localFolder = _ocWizard - > property ( " localFolder " ) . toString ( ) ;
2012-06-12 14:30:05 +04:00
if ( success ) {
2013-05-23 03:51:05 +04:00
if ( ! ( localFolder . isEmpty ( ) | | _remoteFolder . isEmpty ( ) ) ) {
2017-02-23 16:54:17 +03:00
_ocWizard - > appendToConfigurationLog (
tr ( " A sync connection from %1 to remote directory %2 was set up. " )
. arg ( localFolder , _remoteFolder ) ) ;
2012-06-12 14:30:05 +04:00
}
2013-04-28 00:24:32 +04:00
_ocWizard - > appendToConfigurationLog ( QLatin1String ( " " ) ) ;
_ocWizard - > appendToConfigurationLog ( QLatin1String ( " <p><font color= \" green \" ><b> " )
2013-10-30 19:31:47 +04:00
+ tr ( " Successfully connected to %1! " )
. arg ( Theme : : instance ( ) - > appNameGUI ( ) )
+ QLatin1String ( " </b></font></p> " ) ) ;
2013-07-31 17:46:41 +04:00
_ocWizard - > successfulStep ( ) ;
2012-06-12 14:30:05 +04:00
} else {
2013-10-30 19:31:47 +04:00
// ### this is not quite true, pass in the real problem as optional parameter
2013-04-28 00:24:32 +04:00
_ocWizard - > appendToConfigurationLog ( QLatin1String ( " <p><font color= \" red \" > " )
2013-10-30 19:31:47 +04:00
+ tr ( " Connection to %1 could not be established. Please check again. " )
. arg ( Theme : : instance ( ) - > appNameGUI ( ) )
+ QLatin1String ( " </font></p> " ) ) ;
}
}
bool OwncloudSetupWizard : : ensureStartFromScratch ( const QString & localFolder )
{
// first try to rename (backup) the current local dir.
bool renameOk = false ;
while ( ! renameOk ) {
renameOk = FolderMan : : instance ( ) - > startFromScratch ( localFolder ) ;
if ( ! renameOk ) {
QMessageBox : : StandardButton but ;
but = QMessageBox : : question ( 0 , tr ( " Folder rename failed " ) ,
tr ( " Can't remove and back up the folder because the folder or a file in it is open in another program. "
2014-04-20 16:54:40 +04:00
" Please close the folder or file and hit retry or cancel the setup. " ) ,
QMessageBox : : Retry | QMessageBox : : Abort , QMessageBox : : Retry ) ;
2013-10-30 19:31:47 +04:00
if ( but = = QMessageBox : : Abort ) {
break ;
}
}
}
return renameOk ;
}
2015-10-05 07:21:19 +03:00
// Method executed when the user end has finished the basic setup.
2013-07-31 17:46:41 +04:00
void OwncloudSetupWizard : : slotAssistantFinished ( int result )
2013-07-22 16:54:14 +04:00
{
2013-07-31 17:46:41 +04:00
FolderMan * folderMan = FolderMan : : instance ( ) ;
2013-07-22 16:54:14 +04:00
2013-07-31 17:46:41 +04:00
if ( result = = QDialog : : Rejected ) {
2017-03-30 14:46:20 +03:00
qCInfo ( lcWizard ) < < " Rejected the new config, use the old! " ;
2014-12-18 15:18:24 +03:00
2013-07-31 17:46:41 +04:00
} else if ( result = = QDialog : : Accepted ) {
2014-12-18 15:18:24 +03:00
// This may or may not wipe all folder definitions, depending
// on whether a new account is activated or the existing one
// is changed.
2015-04-27 18:43:07 +03:00
auto account = applyAccountChanges ( ) ;
2014-12-18 15:18:24 +03:00
2015-09-17 13:14:35 +03:00
QString localFolder = FolderDefinition : : prepareLocalPath ( _ocWizard - > localFolder ( ) ) ;
2013-07-22 16:54:14 +04:00
2013-10-30 19:31:47 +04:00
bool startFromScratch = _ocWizard - > field ( " OCSyncFromScratch " ) . toBool ( ) ;
2014-12-18 15:18:24 +03:00
if ( ! startFromScratch | | ensureStartFromScratch ( localFolder ) ) {
2017-03-30 14:46:20 +03:00
qCInfo ( lcWizard ) < < " Adding folder definition for " < < localFolder < < _remoteFolder ;
2015-04-24 11:18:33 +03:00
FolderDefinition folderDefinition ;
folderDefinition . localPath = localFolder ;
2016-11-23 12:40:17 +03:00
folderDefinition . targetPath = FolderDefinition : : prepareTargetPath ( _remoteFolder ) ;
2015-08-10 12:03:57 +03:00
folderDefinition . ignoreHiddenFiles = folderMan - > ignoreHiddenFiles ( ) ;
2015-06-10 17:22:14 +03:00
auto f = folderMan - > addFolder ( account , folderDefinition ) ;
if ( f ) {
f - > journalDb ( ) - > setSelectiveSyncList ( SyncJournalDb : : SelectiveSyncBlackList ,
_ocWizard - > selectiveSyncBlacklist ( ) ) ;
2017-01-26 12:14:30 +03:00
if ( ! _ocWizard - > isConfirmBigFolderChecked ( ) ) {
// The user already accepted the selective sync dialog. everything is in the white list
f - > journalDb ( ) - > setSelectiveSyncList ( SyncJournalDb : : SelectiveSyncWhiteList ,
2015-08-05 17:11:59 +03:00
QStringList ( ) < < QLatin1String ( " / " ) ) ;
2017-01-26 12:14:30 +03:00
}
2015-06-10 17:22:14 +03:00
}
2014-12-18 15:18:24 +03:00
_ocWizard - > appendToConfigurationLog ( tr ( " <font color= \" green \" ><b>Local sync folder %1 successfully created!</b></font> " ) . arg ( localFolder ) ) ;
2013-08-15 18:35:03 +04:00
}
2013-07-22 16:54:14 +04:00
}
2013-07-31 17:46:41 +04:00
// notify others.
emit ownCloudWizardDone ( result ) ;
2013-07-22 16:54:14 +04:00
}
2014-12-18 15:18:24 +03:00
void OwncloudSetupWizard : : slotSkipFolderConfiguration ( )
2014-08-12 19:33:17 +04:00
{
2014-12-18 15:18:24 +03:00
applyAccountChanges ( ) ;
2017-09-20 11:14:48 +03:00
disconnect ( _ocWizard , & OwncloudWizard : : basicSetupFinished ,
this , & OwncloudSetupWizard : : slotAssistantFinished ) ;
2014-08-12 19:33:17 +04:00
_ocWizard - > close ( ) ;
emit ownCloudWizardDone ( QDialog : : Accepted ) ;
}
2015-04-27 18:43:07 +03:00
AccountState * OwncloudSetupWizard : : applyAccountChanges ( )
2014-12-18 15:18:24 +03:00
{
AccountPtr newAccount = _ocWizard - > account ( ) ;
2016-12-21 17:04:37 +03:00
// Detach the account that is going to be saved from the
// wizard to ensure it doesn't accidentally get modified
// later (such as from running cleanup such as
// AbstractCredentialsWizardPage::cleanupPage())
_ocWizard - > setAccount ( AccountManager : : createAccount ( ) ) ;
2015-04-17 18:56:17 +03:00
auto manager = AccountManager : : instance ( ) ;
2014-12-18 15:18:24 +03:00
2015-04-27 18:43:07 +03:00
auto newState = manager - > addAccount ( newAccount ) ;
2015-04-27 18:19:46 +03:00
manager - > save ( ) ;
2015-04-27 18:43:07 +03:00
return newState ;
2014-12-18 15:18:24 +03:00
}
2014-11-10 00:34:07 +03:00
} // namespace OCC