|
| 1 | +/*************************************************************************** |
| 2 | + qgsauthsettingswidget.cpp - QgsAuthSettingsWidget |
| 3 | +
|
| 4 | + --------------------- |
| 5 | + begin : 28.9.2017 |
| 6 | + copyright : (C) 2017 by Alessandro Pasotti |
| 7 | + email : apasotti at boundlessgeo dot com |
| 8 | + *************************************************************************** |
| 9 | + * * |
| 10 | + * This program is free software; you can redistribute it and/or modify * |
| 11 | + * it under the terms of the GNU General Public License as published by * |
| 12 | + * the Free Software Foundation; either version 2 of the License, or * |
| 13 | + * (at your option) any later version. * |
| 14 | + * * |
| 15 | + ***************************************************************************/ |
| 16 | +#include "qgsauthsettingswidget.h" |
| 17 | +#include "qgsauthmanager.h" |
| 18 | +#include "qgsauthconfig.h" |
| 19 | + |
| 20 | +#include <QDateTime> |
| 21 | + |
| 22 | +QgsAuthSettingsWidget::QgsAuthSettingsWidget( QWidget *parent, |
| 23 | + const QString &configId, |
| 24 | + const QString &username, |
| 25 | + const QString &password, |
| 26 | + const QString &dataprovider ) |
| 27 | + : QWidget( parent ) |
| 28 | +{ |
| 29 | + setupUi( this ); |
| 30 | + txtPassword->setText( password ); |
| 31 | + txtUserName->setText( username ); |
| 32 | + if ( ! dataprovider.isEmpty( ) ) |
| 33 | + { |
| 34 | + mAuthConfigSelect->setDataProviderKey( dataprovider ); |
| 35 | + } |
| 36 | + if ( ! configId.isEmpty( ) ) |
| 37 | + { |
| 38 | + mAuthConfigSelect->setConfigId( configId ); |
| 39 | + tabAuth->setCurrentIndex( tabAuth->indexOf( tabConfigurations ) ); |
| 40 | + } |
| 41 | + else if ( !( username.isEmpty() && password.isEmpty( ) ) ) |
| 42 | + { |
| 43 | + tabAuth->setCurrentIndex( tabAuth->indexOf( tabBasic ) ); |
| 44 | + } |
| 45 | + updateConvertBtnState(); |
| 46 | +} |
| 47 | + |
| 48 | +void QgsAuthSettingsWidget::setWarningText( const QString &warningText ) |
| 49 | +{ |
| 50 | + lblWarning->setText( warningText ); |
| 51 | +} |
| 52 | + |
| 53 | +void QgsAuthSettingsWidget::setBasicText( const QString &basicText ) |
| 54 | +{ |
| 55 | + lblBasic->setText( basicText ); |
| 56 | +} |
| 57 | + |
| 58 | +const QString QgsAuthSettingsWidget::username() const |
| 59 | +{ |
| 60 | + return txtUserName->text(); |
| 61 | +} |
| 62 | + |
| 63 | +const QString QgsAuthSettingsWidget::password() const |
| 64 | +{ |
| 65 | + return txtPassword->text(); |
| 66 | +} |
| 67 | + |
| 68 | +const QString QgsAuthSettingsWidget::configId() const |
| 69 | +{ |
| 70 | + return mAuthConfigSelect->configId(); |
| 71 | +} |
| 72 | + |
| 73 | +int QgsAuthSettingsWidget::currentTabIndex() const |
| 74 | +{ |
| 75 | + return tabAuth->currentIndex( ); |
| 76 | +} |
| 77 | + |
| 78 | +bool QgsAuthSettingsWidget::btnConvertToEncryptedIsEnabled() const |
| 79 | +{ |
| 80 | + return btnConvertToEncrypted->isEnabled( ); |
| 81 | +} |
| 82 | + |
| 83 | +bool QgsAuthSettingsWidget::on_btnConvertToEncrypted_clicked() |
| 84 | +{ |
| 85 | + tabAuth->setCurrentIndex( tabAuth->indexOf( tabConfigurations ) ); |
| 86 | + QgsAuthMethodConfig config( QStringLiteral( "Basic" ) ); |
| 87 | + config.setName( tr( "Converted config %1" ).arg( QDateTime::currentDateTime().toString( ) ) ); |
| 88 | + config.setConfig( QStringLiteral( "username" ), txtUserName->text() ); |
| 89 | + config.setConfig( QStringLiteral( "password" ), txtPassword->text() ); |
| 90 | + if ( ! QgsAuthManager::instance()->storeAuthenticationConfig( config ) ) |
| 91 | + { |
| 92 | + mAuthConfigSelect->showMessage( tr( "Couldn't create a Basic authentication configuration!" ) ); |
| 93 | + return false; |
| 94 | + } |
| 95 | + else |
| 96 | + { |
| 97 | + txtUserName->setText( QString( ) ); |
| 98 | + txtPassword->setText( QString( ) ); |
| 99 | + mAuthConfigSelect->setConfigId( config.id( ) ); |
| 100 | + return true; |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +void QgsAuthSettingsWidget::on_txtUserName_textChanged( const QString &text ) |
| 105 | +{ |
| 106 | + Q_UNUSED( text ); |
| 107 | + updateConvertBtnState(); |
| 108 | +} |
| 109 | + |
| 110 | +void QgsAuthSettingsWidget::on_txtPassword_textChanged( const QString &text ) |
| 111 | +{ |
| 112 | + Q_UNUSED( text ); |
| 113 | + updateConvertBtnState(); |
| 114 | +} |
| 115 | + |
| 116 | +void QgsAuthSettingsWidget::updateConvertBtnState() |
| 117 | +{ |
| 118 | + btnConvertToEncrypted->setEnabled( ! txtUserName->text().isEmpty() || ! txtPassword->text().isEmpty() ); |
| 119 | +} |
0 commit comments