19 #include <QPushButton>
33 , mDataProvider( dataprovider )
39 if ( !disabled && !authcfg.isEmpty() )
44 if ( disabled || !idok )
46 mAuthNotifyLayout =
new QVBoxLayout;
47 this->setLayout( mAuthNotifyLayout );
50 if ( !authcfg.isEmpty() )
52 msg +=
"\n\n" + tr(
"Authentication config id not loaded: %1" ).arg( authcfg );
54 mAuthNotify =
new QLabel( msg,
this );
55 mAuthNotifyLayout->addWidget( mAuthNotify );
58 buttonBox->button( QDialogButtonBox::Save )->setEnabled(
false );
63 connect( btnClear, &QToolButton::clicked,
this, &QgsAuthConfigEdit::btnClear_clicked );
64 connect( leName, &QLineEdit::textChanged,
this, &QgsAuthConfigEdit::leName_textChanged );
65 connect( buttonBox, &QDialogButtonBox::rejected,
this, &QWidget::close );
66 connect( buttonBox, &QDialogButtonBox::accepted,
this, &QgsAuthConfigEdit::saveConfig );
67 connect( buttonBox->button( QDialogButtonBox::Reset ), &QAbstractButton::clicked,
this, &QgsAuthConfigEdit::resetConfig );
69 populateAuthMethods();
71 connect( cmbAuthMethods,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
72 stkwAuthMethods, &QStackedWidget::setCurrentIndex );
73 connect( cmbAuthMethods,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
74 this, [ = ] { validateAuth(); } );
85 if ( cmbAuthMethods->count() > 0 )
87 cmbAuthMethods->setCurrentIndex( 0 );
88 stkwAuthMethods->setCurrentIndex( 0 );
100 void QgsAuthConfigEdit::populateAuthMethods()
105 QMap<QString, const QgsAuthMethodMetadata *> descmap;
106 const auto constAuthMethodKeys = authMethodKeys;
107 for (
const QString &authMethodKey : constAuthMethodKeys )
112 QgsDebugMsg( QStringLiteral(
"Load auth method instance FAILED for auth method key (%1)" ).arg( authMethodKey ) );
118 QMap<QString, const QgsAuthMethodMetadata *>::iterator it = descmap.begin();
119 for ( it = descmap.begin(); it != descmap.end(); ++it )
125 QgsDebugMsg( QStringLiteral(
"Load auth method edit widget FAILED for auth method key (%1)" ).arg( it.value()->key() ) );
130 cmbAuthMethods->addItem( it.key(), QVariant( it.value()->key() ) );
131 stkwAuthMethods->addWidget( editWidget );
135 void QgsAuthConfigEdit::loadConfig()
137 const bool emptyAuthCfg = mAuthCfg.isEmpty();
138 authCfgEdit->setAllowEmptyId( emptyAuthCfg );
154 QgsDebugMsg( QStringLiteral(
"Loading FAILED for authcfg: %1" ).arg( mAuthCfg ) );
158 if ( !mconfig.
isValid(
true ) )
160 QgsDebugMsg( QStringLiteral(
"Loading FAILED for authcfg (%1): invalid config" ).arg( mAuthCfg ) );
165 leName->setText( mconfig.
name() );
166 leResource->setText( mconfig.
uri() );
167 authCfgEdit->setAuthConfigId( mconfig.
id() );
171 QgsDebugMsgLevel( QStringLiteral(
"Loading authcfg: %1" ).arg( mAuthCfg ), 2 );
172 QgsDebugMsgLevel( QStringLiteral(
"Loading auth method: %1" ).arg( authMethodKey ), 2 );
174 if ( authMethodKey.isEmpty() )
176 QgsDebugMsg( QStringLiteral(
"Loading FAILED for authcfg (%1): no auth method found" ).arg( mAuthCfg ) );
180 if ( mconfig.
method() != authMethodKey )
182 QgsDebugMsg( QStringLiteral(
"Loading FAILED for authcfg (%1): auth method and key mismatch" ).arg( mAuthCfg ) );
186 const int indx = authMethodIndex( authMethodKey );
189 QgsDebugMsg( QStringLiteral(
"Loading FAILED for authcfg (%1): no edit widget loaded for auth method '%2'" )
190 .arg( mAuthCfg, authMethodKey ) );
191 if ( cmbAuthMethods->count() > 0 )
193 cmbAuthMethods->setCurrentIndex( 0 );
194 stkwAuthMethods->setCurrentIndex( 0 );
199 cmbAuthMethods->setCurrentIndex( indx );
200 stkwAuthMethods->setCurrentIndex( indx );
205 QgsDebugMsg( QStringLiteral(
"Cast to edit widget FAILED for authcfg (%1) and auth method key (%2)" )
206 .arg( mAuthCfg, authMethodKey ) );
213 void QgsAuthConfigEdit::resetConfig()
220 void QgsAuthConfigEdit::saveConfig()
225 const QString authMethodKey = cmbAuthMethods->currentData().toString();
230 QgsDebugMsg( QStringLiteral(
"Cast to edit widget FAILED)" ) );
237 QgsDebugMsg( QStringLiteral(
"Save auth config FAILED when loading auth method instance from key (%1)" ).arg( authMethodKey ) );
242 mconfig.
setName( leName->text() );
243 mconfig.
setUri( leResource->text() );
250 QgsDebugMsg( QStringLiteral(
"Save auth config FAILED: config invalid" ) );
254 const QString authCfgId( authCfgEdit->configId() );
255 if ( !mAuthCfg.isEmpty() )
257 if ( authCfgId == mAuthCfg )
259 mconfig.
setId( mAuthCfg );
266 QgsDebugMsg( QStringLiteral(
"Updating auth config FAILED for authcfg: %1" ).arg( mAuthCfg ) );
271 mconfig.
setId( authCfgId );
277 QgsDebugMsg( QStringLiteral(
"Removal of older auth config FAILED" ) );
279 mAuthCfg = authCfgId;
283 QgsDebugMsg( QStringLiteral(
"Storing new auth config with user-created unique ID FAILED" ) );
287 else if ( mAuthCfg.isEmpty() )
289 if ( authCfgId.isEmpty() )
293 mAuthCfg = mconfig.
id();
298 QgsDebugMsg( QStringLiteral(
"Storing new auth config FAILED" ) );
303 mconfig.
setId( authCfgId );
306 mAuthCfg = authCfgId;
311 QgsDebugMsg( QStringLiteral(
"Storing new auth config with user-created unique ID FAILED" ) );
319 void QgsAuthConfigEdit::btnClear_clicked()
324 QgsDebugMsg( QStringLiteral(
"Cast to edit widget FAILED)" ) );
333 void QgsAuthConfigEdit::clearAll()
337 authCfgEdit->clear();
339 for (
int i = 0; i < stkwAuthMethods->count(); i++ )
341 QgsAuthMethodEdit *editWidget = qobject_cast<QgsAuthMethodEdit *>( stkwAuthMethods->widget( i ) );
351 void QgsAuthConfigEdit::validateAuth()
353 bool authok = !leName->text().isEmpty();
358 QgsDebugMsg( QStringLiteral(
"Cast to edit widget FAILED" ) );
364 authok = authok && authCfgEdit->validate();
366 buttonBox->button( QDialogButtonBox::Save )->setEnabled( authok );
369 void QgsAuthConfigEdit::leName_textChanged(
const QString &txt )
375 int QgsAuthConfigEdit::authMethodIndex(
const QString &authMethodKey )
377 return cmbAuthMethods->findData( QVariant( authMethodKey ) );
382 return qobject_cast<QgsAuthMethodEdit *>( stkwAuthMethods->currentWidget() );