/*
* Copyright (C) by Klaas Freitag
")
.arg(url.toString())
.arg(Theme::instance()->appNameGUI())
.arg(CheckServerJob::versionString(info))
.arg(CheckServerJob::version(info)));
if (url.path().endsWith("/status.php")) {
// We might be redirected, update the account
QUrl redirectedUrl = url;
redirectedUrl.setPath(url.path().left(url.path().length() - 11));
_ocWizard->account()->setUrl(redirectedUrl);
qDebug() << Q_FUNC_INFO << " was redirected to" << redirectedUrl.toString();
}
DetermineAuthTypeJob *job = new DetermineAuthTypeJob(_ocWizard->account(), this);
connect(job, SIGNAL(authType(WizardCommon::AuthType)),
_ocWizard, SLOT(setAuthType(WizardCommon::AuthType)));
job->start();
}
void OwncloudSetupWizard::slotNoOwnCloudFoundAuth(QNetworkReply *reply)
{
_ocWizard->displayError(tr("Failed to connect to %1 at %2:
%3")
.arg(Theme::instance()->appNameGUI())
.arg(reply->url().toString())
.arg(reply->errorString()));
}
void OwncloudSetupWizard::slotNoOwnCloudFoundAuthTimeout(const QUrl&url)
{
_ocWizard->displayError(tr("Failed to connect to %1 at %2:
%3")
.arg(Theme::instance()->appNameGUI())
.arg(url.toString())
.arg("Timeout"));
}
void OwncloudSetupWizard::slotConnectToOCUrl( const QString& url )
{
qDebug() << "Connect to url: " << url;
_ocWizard->account()->setCredentials(_ocWizard->getCredentials());
_ocWizard->setField(QLatin1String("OCUrl"), url );
_ocWizard->appendToConfigurationLog(tr("Trying to connect to %1 at %2...")
.arg( Theme::instance()->appNameGUI() ).arg(url) );
testOwnCloudConnect();
}
void OwncloudSetupWizard::testOwnCloudConnect()
{
ValidateDavAuthJob *job = new ValidateDavAuthJob(_ocWizard->account(), this);
connect(job, SIGNAL(authResult(QNetworkReply*)), SLOT(slotConnectionCheck(QNetworkReply*)));
job->start();
}
void OwncloudSetupWizard::slotConnectionCheck(QNetworkReply* reply)
{
switch (reply->error()) {
case QNetworkReply::NoError:
case QNetworkReply::ContentNotFoundError:
_ocWizard->successfulStep();
break;
default:
_ocWizard->displayError(tr("Error: Wrong credentials."));
break;
}
}
void OwncloudSetupWizard::slotCreateLocalAndRemoteFolders(const QString& localFolder, const QString& remoteFolder)
{
qDebug() << "Setup local sync folder for new oC connection " << localFolder;
const QDir fi( localFolder );
bool nextStep = true;
if( fi.exists() ) {
// there is an existing local folder. If its non empty, it can only be synced if the
// ownCloud is newly created.
_ocWizard->appendToConfigurationLog( tr("Local sync folder %1 already exists, setting it up for sync.
").arg(localFolder));
} else {
QString res = tr("Creating local sync folder %1... ").arg(localFolder);
if( fi.mkpath( localFolder ) ) {
Utility::setupFavLink( localFolder );
// FIXME: Create a local sync folder.
res += tr("ok");
} else {
res += tr("failed.");
qDebug() << "Failed to create " << fi.path();
_ocWizard->displayError(tr("Could not create local folder %1").arg(localFolder));
nextStep = false;
}
_ocWizard->appendToConfigurationLog( res );
}
if (nextStep) {
EntityExistsJob *job = new EntityExistsJob(_ocWizard->account(), remoteFolder, this);
connect(job, SIGNAL(exists(QNetworkReply*)), SLOT(slotAuthCheckReply(QNetworkReply*)));
job->start();
} else {
finalizeSetup( false );
}
}
// ### TODO move into EntityExistsJob once we decide if/how to return gui strings from jobs
void OwncloudSetupWizard::slotAuthCheckReply(QNetworkReply *reply)
{
bool ok = true;
QString error;
QNetworkReply::NetworkError errId = reply->error();
if( errId == QNetworkReply::NoError ) {
qDebug() << "******** Remote folder found, all cool!";
} else if( errId == QNetworkReply::ContentNotFoundError ) {
if( _remoteFolder.isEmpty() ) {
error = tr("No remote folder specified!");
ok = false;
} else {
createRemoteFolder();
}
} else {
error = tr("Error: %1").arg(reply->errorString());
ok = false;
}
if( !ok ) {
_ocWizard->displayError(error);
}
finalizeSetup( ok );
}
void OwncloudSetupWizard::createRemoteFolder()
{
_ocWizard->appendToConfigurationLog( tr("creating folder on ownCloud: %1" ).arg( _remoteFolder ));
MkColJob *job = new MkColJob(_ocWizard->account(), _remoteFolder, this);
connect(job, SIGNAL(finished(QNetworkReply::NetworkError)), SLOT(slotCreateRemoteFolderFinished(QNetworkReply::NetworkError)));
job->start();
}
void OwncloudSetupWizard::slotCreateRemoteFolderFinished( QNetworkReply::NetworkError error )
{
qDebug() << "** webdav mkdir request finished " << error;
// disconnect(ownCloudInfo::instance(), SIGNAL(webdavColCreated(QNetworkReply::NetworkError)),
// this, SLOT(slotCreateRemoteFolderFinished(QNetworkReply::NetworkError)));
bool success = true;
if( error == QNetworkReply::NoError ) {
_ocWizard->appendToConfigurationLog( tr("Remote folder %1 created successfully.").arg(_remoteFolder));
} else if( error == 202 ) {
_ocWizard->appendToConfigurationLog( tr("The remote folder %1 already exists. Connecting it for syncing.").arg(_remoteFolder));
} else if( error > 202 && error < 300 ) {
_ocWizard->displayError( tr("The folder creation resulted in HTTP error code %1").arg((int)error ));
_ocWizard->appendToConfigurationLog( tr("The folder creation resulted in HTTP error code %1").arg((int)error) );
} else if( error == QNetworkReply::OperationCanceledError ) {
_ocWizard->displayError( tr("The remote folder creation failed because the provided credentials "
"are wrong!"
"
Please go back and check your credentials.
Remote folder creation failed probably because the provided credentials are wrong."
"
Please go back and check your credentials.
") + tr("Successfully connected to %1!") .arg(Theme::instance()->appNameGUI()) + QLatin1String("
")); _ocWizard->successfulStep(); } else { // ### this is not quite true, pass in the real problem as optional parameter _ocWizard->appendToConfigurationLog(QLatin1String("") + tr("Connection to %1 could not be established. Please check again.") .arg(Theme::instance()->appNameGUI()) + QLatin1String("
")); } } 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." "Please close the folder or file and hit retry or cancel the setup."), QMessageBox::Retry | QMessageBox::Abort, QMessageBox::Retry); if( but == QMessageBox::Abort ) { break; } } } return renameOk; } void OwncloudSetupWizard::replaceDefaultAccountWith(Account *newAccount) { // new Account AccountManager *mgr = AccountManager::instance(); if (mgr->account()) { mgr->account()->deleteLater(); } mgr->setAccount(newAccount); newAccount->save(); } // Method executed when the user ends the wizard, either with 'accept' or 'reject'. // accept the custom config to be the main one if Accepted. void OwncloudSetupWizard::slotAssistantFinished( int result ) { FolderMan *folderMan = FolderMan::instance(); if( result == QDialog::Rejected ) { // the old config remains valid. Remove the temporary one. _ocWizard->account()->deleteLater(); qDebug() << "Rejected the new config, use the old!"; } else if( result == QDialog::Accepted ) { Account *newAccount = _ocWizard->account(); Account *origAccount = AccountManager::instance()->account(); const QString localFolder = _ocWizard->localFolder(); bool isInitialSetup = (origAccount == 0); bool reinitRequired = newAccount->changed(origAccount, true /* ignoreProtocol, allows http->https */); bool startFromScratch = _ocWizard->field("OCSyncFromScratch").toBool(); // This distinguishes three possibilities: // 1. Initial setup, no prior account exists if (isInitialSetup) { folderMan->addFolderDefinition(Theme::instance()->appName(), localFolder, _remoteFolder ); replaceDefaultAccountWith(newAccount); } // 2. Server URL or user changed, requires reinit of folders else if (reinitRequired) { // 2.1: startFromScratch: (Re)move local data, clean slate sync if (startFromScratch) { if (ensureStartFromScratch(localFolder)) { folderMan->addFolderDefinition(Theme::instance()->appName(), localFolder, _remoteFolder ); _ocWizard->appendToConfigurationLog(tr("Local sync folder %1 successfully created!").arg(localFolder)); replaceDefaultAccountWith(newAccount); } } // 2.2: Reinit: Remove journal and start a sync else { folderMan->removeAllFolderDefinitions(); folderMan->addFolderDefinition(Theme::instance()->appName(), localFolder, _remoteFolder ); _ocWizard->appendToConfigurationLog(tr("Local sync folder %1 successfully created!").arg(localFolder)); replaceDefaultAccountWith(newAccount); } } // 3. Existing setup, http -> https or password changed else { replaceDefaultAccountWith(newAccount); qDebug() << "Only password was changed, no changes to folder configuration."; } } // notify others. emit ownCloudWizardDone( result ); } DetermineAuthTypeJob::DetermineAuthTypeJob(Account *account, QObject *parent) : AbstractNetworkJob(account, QString(), parent) , _redirects(0) { } void DetermineAuthTypeJob::start() { QNetworkReply *reply = getRequest(Account::davPath()); setReply(reply); setupConnections(reply); AbstractNetworkJob::start(); } void DetermineAuthTypeJob::finished() { QUrl redirection = reply()->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl(); qDebug() << redirection.toString(); if (_redirects >= maxRedirects()) { redirection.clear(); } if ((reply()->error() == QNetworkReply::AuthenticationRequiredError) || redirection.isEmpty()) { emit authType(WizardCommon::HttpCreds); } else if (redirection.toString().endsWith(Account::davPath())) { // do a new run _redirects++; setReply(getRequest(redirection)); setupConnections(reply()); } else { QRegExp shibbolethyWords("SAML|wayf"); shibbolethyWords.setCaseSensitivity(Qt::CaseInsensitive); if (redirection.toString().contains(shibbolethyWords)) { emit authType(WizardCommon::Shibboleth); } else { // TODO: Send an error. // eh? emit authType(WizardCommon::HttpCreds); } } } ValidateDavAuthJob::ValidateDavAuthJob(Account *account, QObject *parent) : AbstractNetworkJob(account, QString(), parent) { } void ValidateDavAuthJob::start() { QNetworkReply *reply = getRequest(Account::davPath()); setReply(reply); setupConnections(reply); AbstractNetworkJob::start(); } void ValidateDavAuthJob::finished() { emit authResult(reply()); } } // ns Mirall