19 #include <QPushButton>
32 , mDataProvider( dataprovider )
38 if ( !disabled && !authcfg.isEmpty() )
43 if ( disabled || !idok )
45 mAuthNotifyLayout =
new QVBoxLayout;
46 this->setLayout( mAuthNotifyLayout );
49 if ( !authcfg.isEmpty() )
51 msg +=
"\n\n" + tr(
"Authentication config id not loaded: %1" ).arg( authcfg );
53 mAuthNotify =
new QLabel( msg,
this );
54 mAuthNotifyLayout->addWidget( mAuthNotify );
57 buttonBox->button( QDialogButtonBox::Save )->setEnabled(
false );
62 connect( btnClear, &QToolButton::clicked,
this, &QgsAuthConfigEdit::btnClear_clicked );
63 connect( leName, &QLineEdit::textChanged,
this, &QgsAuthConfigEdit::leName_textChanged );
64 connect( buttonBox, &QDialogButtonBox::rejected,
this, &QWidget::close );
65 connect( buttonBox, &QDialogButtonBox::accepted,
this, &QgsAuthConfigEdit::saveConfig );
66 connect( buttonBox->button( QDialogButtonBox::Reset ), &QAbstractButton::clicked,
this, &QgsAuthConfigEdit::resetConfig );
68 populateAuthMethods();
70 connect( cmbAuthMethods,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
71 stkwAuthMethods, &QStackedWidget::setCurrentIndex );
72 connect( cmbAuthMethods,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
73 this, [ = ] { validateAuth(); } );
84 if ( cmbAuthMethods->count() > 0 )
86 cmbAuthMethods->setCurrentIndex( 0 );
87 stkwAuthMethods->setCurrentIndex( 0 );
99 void QgsAuthConfigEdit::populateAuthMethods()
104 QMap<QString, QgsAuthMethod *> descmap;
105 const auto constAuthMethodKeys = authMethodKeys;
106 for (
const QString &authMethodKey : constAuthMethodKeys )
111 QgsDebugMsg( QStringLiteral(
"Load auth method instance FAILED for auth method key (%1)" ).arg( authMethodKey ) );
117 QMap<QString, QgsAuthMethod *>::iterator it = descmap.begin();
118 for ( it = descmap.begin(); it != descmap.end(); ++it )
124 QgsDebugMsg( QStringLiteral(
"Load auth method edit widget FAILED for auth method key (%1)" ).arg( it.value()->key() ) );
129 cmbAuthMethods->addItem( it.key(), QVariant( it.value()->key() ) );
130 stkwAuthMethods->addWidget( editWidget );
134 void QgsAuthConfigEdit::loadConfig()
136 bool emptyAuthCfg = mAuthCfg.isEmpty();
137 authCfgEdit->setAllowEmptyId( emptyAuthCfg );
153 QgsDebugMsg( QStringLiteral(
"Loading FAILED for authcfg: %1" ).arg( mAuthCfg ) );
157 if ( !mconfig.
isValid(
true ) )
159 QgsDebugMsg( QStringLiteral(
"Loading FAILED for authcfg (%1): invalid config" ).arg( mAuthCfg ) );
164 leName->setText( mconfig.
name() );
165 leResource->setText( mconfig.
uri() );
166 authCfgEdit->setAuthConfigId( mconfig.
id() );
170 QgsDebugMsg( QStringLiteral(
"Loading authcfg: %1" ).arg( mAuthCfg ) );
171 QgsDebugMsg( QStringLiteral(
"Loading auth method: %1" ).arg( authMethodKey ) );
173 if ( authMethodKey.isEmpty() )
175 QgsDebugMsg( QStringLiteral(
"Loading FAILED for authcfg (%1): no auth method found" ).arg( mAuthCfg ) );
179 if ( mconfig.
method() != authMethodKey )
181 QgsDebugMsg( QStringLiteral(
"Loading FAILED for authcfg (%1): auth method and key mismatch" ).arg( mAuthCfg ) );
185 int indx = authMethodIndex( authMethodKey );
188 QgsDebugMsg( QStringLiteral(
"Loading FAILED for authcfg (%1): no edit widget loaded for auth method '%2'" )
189 .arg( mAuthCfg, authMethodKey ) );
190 if ( cmbAuthMethods->count() > 0 )
192 cmbAuthMethods->setCurrentIndex( 0 );
193 stkwAuthMethods->setCurrentIndex( 0 );
198 cmbAuthMethods->setCurrentIndex( indx );
199 stkwAuthMethods->setCurrentIndex( indx );
204 QgsDebugMsg( QStringLiteral(
"Cast to edit widget FAILED for authcfg (%1) and auth method key (%2)" )
205 .arg( mAuthCfg, authMethodKey ) );
212 void QgsAuthConfigEdit::resetConfig()
219 void QgsAuthConfigEdit::saveConfig()
224 QString authMethodKey = cmbAuthMethods->currentData().toString();
229 QgsDebugMsg( QStringLiteral(
"Cast to edit widget FAILED)" ) );
236 QgsDebugMsg( QStringLiteral(
"Save auth config FAILED when loading auth method instance from key (%1)" ).arg( authMethodKey ) );
241 mconfig.
setName( leName->text() );
242 mconfig.
setUri( leResource->text() );
249 QgsDebugMsg( QStringLiteral(
"Save auth config FAILED: config invalid" ) );
253 QString authCfgId( authCfgEdit->configId() );
254 if ( !mAuthCfg.isEmpty() )
256 if ( authCfgId == mAuthCfg )
258 mconfig.
setId( mAuthCfg );
265 QgsDebugMsg( QStringLiteral(
"Updating auth config FAILED for authcfg: %1" ).arg( mAuthCfg ) );
270 mconfig.
setId( authCfgId );
276 QgsDebugMsg( QStringLiteral(
"Removal of older auth config FAILED" ) );
278 mAuthCfg = authCfgId;
282 QgsDebugMsg( QStringLiteral(
"Storing new auth config with user-created unique ID FAILED" ) );
286 else if ( mAuthCfg.isEmpty() )
288 if ( authCfgId.isEmpty() )
292 mAuthCfg = mconfig.
id();
297 QgsDebugMsg( QStringLiteral(
"Storing new auth config FAILED" ) );
302 mconfig.
setId( authCfgId );
305 mAuthCfg = authCfgId;
310 QgsDebugMsg( QStringLiteral(
"Storing new auth config with user-created unique ID FAILED" ) );
318 void QgsAuthConfigEdit::btnClear_clicked()
323 QgsDebugMsg( QStringLiteral(
"Cast to edit widget FAILED)" ) );
332 void QgsAuthConfigEdit::clearAll()
336 authCfgEdit->clear();
338 for (
int i = 0; i < stkwAuthMethods->count(); i++ )
340 QgsAuthMethodEdit *editWidget = qobject_cast<QgsAuthMethodEdit *>( stkwAuthMethods->widget( i ) );
350 void QgsAuthConfigEdit::validateAuth()
352 bool authok = !leName->text().isEmpty();
357 QgsDebugMsg( QStringLiteral(
"Cast to edit widget FAILED" ) );
363 authok = authok && authCfgEdit->validate();
365 buttonBox->button( QDialogButtonBox::Save )->setEnabled( authok );
368 void QgsAuthConfigEdit::leName_textChanged(
const QString &txt )
374 int QgsAuthConfigEdit::authMethodIndex(
const QString &authMethodKey )
376 return cmbAuthMethods->findData( QVariant( authMethodKey ) );
381 return qobject_cast<QgsAuthMethodEdit *>( stkwAuthMethods->currentWidget() );