1919#include < QPushButton>
2020
2121#include " qgsauthconfig.h"
22+ #include " qgsauthconfigidedit.h"
2223#include " qgsauthmanager.h"
2324#include " qgsauthmethodedit.h"
2425#include " qgslogger.h"
@@ -53,6 +54,7 @@ QgsAuthConfigEdit::QgsAuthConfigEdit( QWidget *parent , const QString& authcfg ,
5354 mAuthNotifyLayout ->addWidget ( mAuthNotify );
5455
5556 mAuthCfg .clear (); // otherwise will contiue to try authenticate (and fail) after save
57+ buttonBox->button ( QDialogButtonBox::Save )->setEnabled ( false );
5658 }
5759 else
5860 {
@@ -68,6 +70,8 @@ QgsAuthConfigEdit::QgsAuthConfigEdit( QWidget *parent , const QString& authcfg ,
6870 connect ( cmbAuthMethods, SIGNAL ( currentIndexChanged ( int ) ),
6971 this , SLOT ( validateAuth () ) );
7072
73+ connect ( authCfgEdit, SIGNAL ( validityChanged ( bool ) ), this , SLOT ( validateAuth () ) );
74+
7175 // needed (if only combobox is ever changed)?
7276 // connect( stkwAuthMethods, SIGNAL( currentChanged( int ) ),
7377 // cmbAuthMethods, SLOT( setCurrentIndex( int ) ) );
@@ -128,7 +132,9 @@ void QgsAuthConfigEdit::populateAuthMethods()
128132
129133void QgsAuthConfigEdit::loadConfig ()
130134{
131- if ( mAuthCfg .isEmpty () )
135+ bool emptyAuthCfg = mAuthCfg .isEmpty ();
136+ authCfgEdit->setAllowEmptyId ( emptyAuthCfg );
137+ if ( emptyAuthCfg )
132138 {
133139 return ;
134140 }
@@ -156,7 +162,7 @@ void QgsAuthConfigEdit::loadConfig()
156162 // load basic info
157163 leName->setText ( mconfig.name () );
158164 leResource->setText ( mconfig.uri () );
159- leAuthCfg-> setText ( mconfig.id () );
165+ authCfgEdit-> setAuthConfigId ( mconfig.id () );
160166
161167 QString authMethodKey = QgsAuthManager::instance ()->configAuthMethodKey ( mAuthCfg );
162168
@@ -243,28 +249,65 @@ void QgsAuthConfigEdit::saveConfig()
243249 return ;
244250 }
245251
246- if ( !mAuthCfg .isEmpty () ) // update
252+ QString authCfgId ( authCfgEdit->configId () );
253+ if ( !mAuthCfg .isEmpty () )
247254 {
248- mconfig.setId ( mAuthCfg );
249- if ( QgsAuthManager::instance ()->updateAuthenticationConfig ( mconfig ) )
255+ if ( authCfgId == mAuthCfg ) // update
250256 {
251- emit authenticationConfigUpdated ( mAuthCfg );
257+ mconfig.setId ( mAuthCfg );
258+ if ( QgsAuthManager::instance ()->updateAuthenticationConfig ( mconfig ) )
259+ {
260+ emit authenticationConfigUpdated ( mAuthCfg );
261+ }
262+ else
263+ {
264+ QgsDebugMsg ( QString ( " Updating auth config FAILED for authcfg: %1" ).arg ( mAuthCfg ) );
265+ }
252266 }
253- else
267+ else // store new with unique ID, then delete previous
254268 {
255- QgsDebugMsg ( QString ( " Updating auth config FAILED for authcfg: %1" ).arg ( mAuthCfg ) );
269+ mconfig.setId ( authCfgId );
270+ if ( QgsAuthManager::instance ()->storeAuthenticationConfig ( mconfig ) )
271+ {
272+ emit authenticationConfigStored ( authCfgId );
273+ if ( !QgsAuthManager::instance ()->removeAuthenticationConfig ( mAuthCfg ) )
274+ {
275+ QgsDebugMsg ( QString ( " Removal of older auth config FAILED" ) );
276+ }
277+ mAuthCfg = authCfgId;
278+ }
279+ else
280+ {
281+ QgsDebugMsg ( QString ( " Storing new auth config with user-created unique ID FAILED" ) );
282+ }
256283 }
257284 }
258- else // create new
285+ else if ( mAuthCfg . isEmpty () )
259286 {
260- if ( QgsAuthManager::instance ()-> storeAuthenticationConfig ( mconfig ) )
287+ if ( authCfgId. isEmpty () ) // create new with generated ID
261288 {
262- mAuthCfg = mconfig.id ();
263- emit authenticationConfigStored ( mAuthCfg );
289+ if ( QgsAuthManager::instance ()->storeAuthenticationConfig ( mconfig ) )
290+ {
291+ mAuthCfg = mconfig.id ();
292+ emit authenticationConfigStored ( mAuthCfg );
293+ }
294+ else
295+ {
296+ QgsDebugMsg ( QString ( " Storing new auth config FAILED" ) );
297+ }
264298 }
265- else
299+ else // create new with user-created unique ID
266300 {
267- QgsDebugMsg ( QString ( " Storing new auth config FAILED" ) );
301+ mconfig.setId ( authCfgId );
302+ if ( QgsAuthManager::instance ()->storeAuthenticationConfig ( mconfig ) )
303+ {
304+ mAuthCfg = authCfgId;
305+ emit authenticationConfigStored ( mAuthCfg );
306+ }
307+ else
308+ {
309+ QgsDebugMsg ( QString ( " Storing new auth config with user-created unique ID FAILED" ) );
310+ }
268311 }
269312 }
270313
@@ -289,7 +332,7 @@ void QgsAuthConfigEdit::clearAll()
289332{
290333 leName->clear ();
291334 leResource->clear ();
292- leAuthCfg ->clear ();
335+ authCfgEdit ->clear ();
293336
294337 for ( int i = 0 ; i < stkwAuthMethods->count (); i++ )
295338 {
@@ -315,6 +358,8 @@ void QgsAuthConfigEdit::validateAuth()
315358
316359 authok = authok && editWidget->validateConfig ();
317360
361+ authok = authok && authCfgEdit->validate ();
362+
318363 buttonBox->button ( QDialogButtonBox::Save )->setEnabled ( authok );
319364}
320365
0 commit comments