diff --git a/src/core/webdavconnection.cpp b/src/core/webdavconnection.cpp index 3c23868861..8fe6ff95e1 100644 --- a/src/core/webdavconnection.cpp +++ b/src/core/webdavconnection.cpp @@ -83,6 +83,15 @@ void WebdavConnection::setPassword( const QString &password ) mWebdavConnection.clearAccessCache(); } +void WebdavConnection::setStorePassword( bool storePassword ) +{ + if ( mStorePassword == storePassword ) + return; + + mStorePassword = storePassword; + emit storePasswordChanged(); +} + void WebdavConnection::checkStoredPassword() { mStoredPassword.clear(); @@ -90,12 +99,18 @@ void WebdavConnection::checkStoredPassword() if ( !mUrl.isEmpty() && !mUsername.isEmpty() ) { QgsAuthManager *authManager = QgsApplication::instance()->authManager(); - const QgsAuthMethodConfigsMap configs = authManager->availableAuthMethodConfigs(); - for ( const QgsAuthMethodConfig &config : configs ) + QgsAuthMethodConfigsMap configs = authManager->availableAuthMethodConfigs(); + for ( QgsAuthMethodConfig &config : configs ) { - if ( config.uri() == mUrl && config.config( QStringLiteral( "username" ) ) == mUsername ) + qDebug() << config.name(); + qDebug() << config.uri(); + if ( config.uri() == mUrl ) { - mStoredPassword = config.config( QStringLiteral( "password" ) ); + authManager->loadAuthenticationConfig( config.id(), config, true ); + if ( config.config( QStringLiteral( "username" ) ) == mUsername ) + { + mStoredPassword = config.config( QStringLiteral( "password" ) ); + } } } } @@ -103,6 +118,69 @@ void WebdavConnection::checkStoredPassword() emit isPasswordStoredChanged(); } +void WebdavConnection::applyStoredPassword() +{ + QgsAuthManager *authManager = QgsApplication::instance()->authManager(); + QgsAuthMethodConfigsMap configs = authManager->availableAuthMethodConfigs(); + if ( mStorePassword ) + { + if ( !mPassword.isEmpty() ) + { + bool found = false; + for ( QgsAuthMethodConfig &config : configs ) + { + if ( config.uri() == mUrl ) + { + authManager->loadAuthenticationConfig( config.id(), config, true ); + if ( config.config( QStringLiteral( "username" ) ) == mUsername ) + { + if ( config.config( QStringLiteral( "password" ) ) != mPassword ) + { + config.setConfig( "password", mPassword ); + authManager->updateAuthenticationConfig( config ); + + mStoredPassword = mPassword; + emit isPasswordStoredChanged(); + } + + found = true; + break; + } + } + } + + if ( !found ) + { + QgsAuthMethodConfig config( QStringLiteral( "Basic" ) ); + config.setName( QStringLiteral( "WebDAV created on %1" ).arg( QDateTime::currentDateTime().toString() ) ); + config.setUri( mUrl ); + config.setConfig( "username", mUsername ); + config.setConfig( "password", mPassword ); + authManager->storeAuthenticationConfig( config ); + + mStoredPassword = mPassword; + emit isPasswordStoredChanged(); + } + } + } + else + { + for ( const QgsAuthMethodConfig &config : configs ) + { + if ( config.uri() == mUrl && config.config( QStringLiteral( "username" ) ) == mUsername ) + { + authManager->removeAuthenticationConfig( config.id() ); + } + } + + if ( !mStoredPassword.isEmpty() ) + { + mStoredPassword = mPassword; + emit isPasswordStoredChanged(); + } + } +} + void WebdavConnection::setupConnection() { QUrl connectionUrl( mUrl ); @@ -133,6 +211,8 @@ void WebdavConnection::processDirParserFinished() { if ( !list.isEmpty() ) { + applyStoredPassword(); + mAvailablePaths << QStringLiteral( "/" ); for ( const QWebdavItem &item : list ) { @@ -151,20 +231,25 @@ void WebdavConnection::processDirParserFinished() } else if ( mIsImportingPath ) { - QDir importLocalDir( mImportLocalPath ); - for ( const QWebdavItem &item : list ) + if ( !list.isEmpty() ) { - if ( item.isDir() ) - { - importLocalDir.mkpath( item.path().mid( mImportRemotePath.size() ) ); - } - else + applyStoredPassword(); + + QDir importLocalDir( mImportLocalPath ); + for ( const QWebdavItem &item : list ) { - mImportItems << item.path(); - mImportingBytesTotal += item.size(); + if ( item.isDir() ) + { + importLocalDir.mkpath( item.path().mid( mImportRemotePath.size() ) ); + } + else + { + mImportItems << item.path(); + mImportingBytesTotal += item.size(); + } } + emit progressChanged(); } - emit progressChanged(); processImportItems(); } diff --git a/src/core/webdavconnection.h b/src/core/webdavconnection.h index 64a2fcdbac..bd26220e5f 100644 --- a/src/core/webdavconnection.h +++ b/src/core/webdavconnection.h @@ -34,15 +34,15 @@ class WebdavConnection : public QObject Q_PROPERTY( QString url READ url WRITE setUrl NOTIFY urlChanged ); Q_PROPERTY( QString username READ username WRITE setUsername NOTIFY usernameChanged ) Q_PROPERTY( QString password READ password WRITE setPassword NOTIFY passwordChanged ) - Q_PROPERTY( bool isPasswordStored READ isPasswordStored NOTIFY isPasswordStoredChanged ) - Q_PROPERTY( QStringList availablePaths READ availablePaths NOTIFY availablePathsChanged ) + Q_PROPERTY( bool storePassword READ storePassword WRITE setStorePassword NOTIFY storePasswordChanged ) + Q_PROPERTY( bool isPasswordStored READ isPasswordStored NOTIFY isPasswordStoredChanged ) Q_PROPERTY( bool isFetchingAvailablePaths READ isFetchingAvailablePaths NOTIFY isFetchingAvailablePathsChanged ) Q_PROPERTY( bool isImportingPath READ isImportingPath NOTIFY isImportingPathChanged ) + Q_PROPERTY( QStringList availablePaths READ availablePaths NOTIFY availablePathsChanged ) Q_PROPERTY( double progress READ progress NOTIFY progressChanged ) - Q_PROPERTY( QString lastError READ lastError NOTIFY lastErrorChanged ) public: @@ -61,6 +61,10 @@ class WebdavConnection : public QObject void setPassword( const QString &password ); + bool storePassword() const { return mStorePassword; } + + void setStorePassword( bool storePassword ); + bool isPasswordStored() const { return !mStoredPassword.isEmpty(); } QStringList availablePaths() const { return mIsFetchingAvailablePaths ? QStringList() : mAvailablePaths; } @@ -81,10 +85,11 @@ class WebdavConnection : public QObject void urlChanged(); void usernameChanged(); void passwordChanged(); + void storePasswordChanged(); void isPasswordStoredChanged(); - void availablePathsChanged(); void isFetchingAvailablePathsChanged(); void isImportingPathChanged(); + void availablePathsChanged(); void progressChanged(); void lastErrorChanged(); @@ -95,12 +100,15 @@ class WebdavConnection : public QObject private: void checkStoredPassword(); + void applyStoredPassword(); void setupConnection(); void processImportItems(); QString mUrl; QString mUsername; QString mPassword; + + bool mStorePassword = false; QString mStoredPassword; bool mIsFetchingAvailablePaths = false; diff --git a/src/qml/QFieldLocalDataPickerScreen.qml b/src/qml/QFieldLocalDataPickerScreen.qml index 9b1b852c2f..66f7fb9c02 100644 --- a/src/qml/QFieldLocalDataPickerScreen.qml +++ b/src/qml/QFieldLocalDataPickerScreen.qml @@ -706,7 +706,9 @@ Page { id: webdavConnection url: importWebdavUrlInput.text + username: importWebdavUserInput.text password: importWebdavPasswordInput.text + storePassword: importWebdavStorePasswordCheck.checked onIsImportingPathChanged: { if (isImportingPath) { @@ -727,6 +729,10 @@ Page { onLastErrorChanged: { displayToast(qsTr("WebDAV error: ") + lastError); } + + onIsPasswordStoredChanged: { + console.log(isPasswordStored ? "stored" : "not stored"); + } } } @@ -772,12 +778,6 @@ Page { enabled: !webdavConnectionLoader.item || !webdavConnectionLoader.item.isFetchingAvailablePaths width: importWebdavUrlLabel.width placeholderText: qsTr("WebDAV server URL") - - onDisplayTextChanged: { - if (webdavConnectionLoader.item) { - webdavConnectionLoader.item.url = displayText; - } - } } QfTextField { @@ -785,26 +785,23 @@ Page { enabled: !webdavConnectionLoader.item || !webdavConnectionLoader.item.isFetchingAvailablePaths width: importWebdavUrlLabel.width placeholderText: qsTr("User") - - onDisplayTextChanged: { - if (webdavConnectionLoader.item) { - webdavConnectionLoader.item.username = displayText; - } - } } QfTextField { id: importWebdavPasswordInput enabled: !webdavConnectionLoader.item || !webdavConnectionLoader.item.isFetchingAvailablePaths width: importWebdavUrlLabel.width - placeholderText: qsTr("Password") + placeholderText: text === "" && webdavConnectionLoader.item && webdavConnectionLoader.item.isPasswordStored ? qsTr("Password (leave empty to use remembered)") : qsTr("Password") echoMode: TextInput.Password + } - onDisplayTextChanged: { - if (webdavConnectionLoader.item) { - webdavConnectionLoader.item.password = text; - } - } + CheckBox { + id: importWebdavStorePasswordCheck + width: importWebdavUrlLabel.width + enabled: !webdavConnectionLoader.item || !webdavConnectionLoader.item.isFetchingAvailablePaths + text: qsTr('Remember password') + font: Theme.defaultFont + checked: true } Row {