Skip to content

Commit c74ddf1

Browse files
committed
[auth] Add widget for editing an authcfg within a data source URI
1 parent 2f8e684 commit c74ddf1

5 files changed

Lines changed: 518 additions & 33 deletions

File tree

‎python/gui/auth/qgsauthconfigselect.sip‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,38 @@ class QgsAuthConfigSelect : QWidget
99
~QgsAuthConfigSelect();
1010

1111
void setConfigId( const QString& authcfg );
12+
1213
const QString configId() const;
14+
15+
void setDataProviderKey( const QString &key );
16+
17+
signals:
18+
void selectedConfigIdChanged( const QString& authcfg );
19+
20+
void selectedConfigIdRemoved( const QString& authcfg );
21+
22+
public slots:
23+
void showMessage( const QString &msg );
24+
25+
void clearMessage();
26+
};
27+
28+
29+
class QgsAuthConfigUriEdit : QDialog
30+
{
31+
%TypeHeaderCode
32+
#include <qgsauthconfigselect.h>
33+
%End
34+
35+
public:
36+
explicit QgsAuthConfigUriEdit( QWidget *parent /TransferThis/ = 0,
37+
const QString &datauri = QString(),
38+
const QString &dataprovider = QString() );
39+
~QgsAuthConfigUriEdit();
40+
41+
void setDataSourceUri( const QString &datauri );
42+
43+
QString dataSourceUri();
44+
45+
static bool hasConfigID( const QString &txt );
1346
};

‎src/gui/auth/qgsauthconfigselect.cpp‎

Lines changed: 212 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@
1919

2020
#include <QHash>
2121
#include <QMessageBox>
22+
#include <QTimer>
2223

2324
#include "qgsauthconfig.h"
25+
#include "qgsauthguiutils.h"
2426
#include "qgsauthmanager.h"
2527
#include "qgsauthconfigedit.h"
28+
#include "qgslogger.h"
2629

2730

2831
QgsAuthConfigSelect::QgsAuthConfigSelect( QWidget *parent, const QString &dataprovider )
@@ -44,7 +47,11 @@ QgsAuthConfigSelect::QgsAuthConfigSelect( QWidget *parent, const QString &datapr
4447
{
4548
setupUi( this );
4649

50+
leConfigMsg->setStyleSheet( QString( "QLineEdit{background-color: %1}" )
51+
.arg( QgsAuthGuiUtils::yellowColor().name() ) );
52+
4753
clearConfig();
54+
clearMessage();
4855
populateConfigSelector();
4956
}
5057
}
@@ -63,12 +70,20 @@ void QgsAuthConfigSelect::setConfigId( const QString& authcfg )
6370
else
6471
{
6572
if ( mAuthCfg != authcfg )
73+
{
6674
mAuthCfg = authcfg;
75+
}
6776
populateConfigSelector();
6877
loadConfig();
6978
}
7079
}
7180

81+
void QgsAuthConfigSelect::setDataProviderKey( const QString &key )
82+
{
83+
mDataProvider = key;
84+
populateConfigSelector();
85+
}
86+
7287
void QgsAuthConfigSelect::loadConfig()
7388
{
7489
clearConfig();
@@ -87,6 +102,7 @@ void QgsAuthConfigSelect::loadConfig()
87102
btnConfigEdit->setEnabled( true );
88103
btnConfigRemove->setEnabled( true );
89104
}
105+
emit selectedConfigIdChanged( mAuthCfg );
90106
}
91107

92108
void QgsAuthConfigSelect::clearConfig()
@@ -101,6 +117,7 @@ void QgsAuthConfigSelect::validateConfig()
101117
{
102118
if ( !mAuthCfg.isEmpty() && !mConfigs.contains( mAuthCfg ) )
103119
{
120+
showMessage( tr( "Configuration '%1' not in database" ).arg( mAuthCfg ) );
104121
mAuthCfg.clear();
105122
}
106123
}
@@ -137,6 +154,18 @@ void QgsAuthConfigSelect::populateConfigSelector()
137154
cmbConfigSelect->setCurrentIndex( indx > 0 ? indx : 0 );
138155
}
139156

157+
void QgsAuthConfigSelect::showMessage( const QString &msg )
158+
{
159+
leConfigMsg->setText( msg );
160+
frConfigMsg->setVisible( true );
161+
}
162+
163+
void QgsAuthConfigSelect::clearMessage()
164+
{
165+
leConfigMsg->clear();
166+
frConfigMsg->setVisible( false );
167+
}
168+
140169
void QgsAuthConfigSelect::loadAvailableConfigs()
141170
{
142171
mConfigs.clear();
@@ -173,7 +202,7 @@ void QgsAuthConfigSelect::on_btnConfigEdit_clicked()
173202
ace->setWindowModality( Qt::WindowModal );
174203
if ( ace->exec() )
175204
{
176-
qDebug( "Edit returned config Id: %s", ace->configId().toAscii().constData() );
205+
//qDebug( "Edit returned config Id: %s", ace->configId().toAscii().constData() );
177206
setConfigId( ace->configId() );
178207
}
179208
ace->deleteLater();
@@ -192,6 +221,188 @@ void QgsAuthConfigSelect::on_btnConfigRemove_clicked()
192221

193222
if ( QgsAuthManager::instance()->removeAuthenticationConfig( mAuthCfg ) )
194223
{
224+
emit selectedConfigIdRemoved( mAuthCfg );
195225
setConfigId( QString() );
196226
}
197227
}
228+
229+
void QgsAuthConfigSelect::on_btnConfigMsgClear_clicked()
230+
{
231+
clearMessage();
232+
}
233+
234+
235+
//////////////// Embed in dialog ///////////////////
236+
237+
#include <QPushButton>
238+
239+
QgsAuthConfigUriEdit::QgsAuthConfigUriEdit( QWidget *parent, const QString &datauri, const QString &dataprovider )
240+
: QDialog( parent )
241+
, mAuthCfg( QString() )
242+
, mDataUri( QString() )
243+
, mDataUriOrig( QString() )
244+
{
245+
setupUi( this );
246+
247+
setWindowTitle( tr( "Authentication Config ID String Editor" ) );
248+
249+
buttonBox->button( QDialogButtonBox::Close )->setDefault( true );
250+
connect( buttonBox, SIGNAL( rejected() ), this, SLOT( close() ) );
251+
connect( buttonBox, SIGNAL( accepted() ), this, SLOT( saveChanges() ) );
252+
253+
connect( buttonBox->button( QDialogButtonBox::Reset ), SIGNAL( clicked() ), this, SLOT( resetChanges() ) );
254+
255+
connect( wdgtAuthSelect, SIGNAL( selectedConfigIdChanged( QString ) ), this , SLOT( authCfgUpdated( QString ) ) );
256+
connect( wdgtAuthSelect, SIGNAL( selectedConfigIdRemoved( QString ) ), this , SLOT( authCfgRemoved( QString ) ) );
257+
258+
wdgtAuthSelect->setDataProviderKey( dataprovider );
259+
setDataSourceUri( datauri );
260+
}
261+
262+
QgsAuthConfigUriEdit::~QgsAuthConfigUriEdit()
263+
{
264+
}
265+
266+
void QgsAuthConfigUriEdit::setDataSourceUri( const QString &datauri )
267+
{
268+
if ( datauri.isEmpty() )
269+
return;
270+
271+
mDataUri = mDataUriOrig = datauri;
272+
273+
teDataUri->setPlainText( mDataUri );
274+
275+
if ( authCfgIndex() == -1 )
276+
{
277+
wdgtAuthSelect->showMessage( tr( "No authcfg in Data Source URI" ) );
278+
return;
279+
}
280+
281+
selectAuthCfgInUri();
282+
283+
mAuthCfg = authCfgFromUri();
284+
285+
QgsDebugMsg( QString( "Parsed authcfg ID: %1" ).arg( mAuthCfg ) );
286+
287+
wdgtAuthSelect->blockSignals( true );
288+
wdgtAuthSelect->setConfigId( mAuthCfg );
289+
wdgtAuthSelect->blockSignals( false );
290+
}
291+
292+
QString QgsAuthConfigUriEdit::dataSourceUri()
293+
{
294+
return mDataUri;
295+
}
296+
297+
bool QgsAuthConfigUriEdit::hasConfigID( const QString &txt )
298+
{
299+
return QgsAuthManager::instance()->hasConfigId( txt );
300+
}
301+
302+
void QgsAuthConfigUriEdit::saveChanges()
303+
{
304+
this->accept();
305+
}
306+
307+
void QgsAuthConfigUriEdit::resetChanges()
308+
{
309+
wdgtAuthSelect->clearMessage();
310+
setDataSourceUri( mDataUriOrig );
311+
}
312+
313+
void QgsAuthConfigUriEdit::authCfgUpdated( const QString &authcfg )
314+
{
315+
mAuthCfg = authcfg;
316+
317+
if ( mAuthCfg.size() != 7 )
318+
{
319+
mAuthCfg.clear();
320+
removeAuthCfgFromUri();
321+
}
322+
else
323+
{
324+
updateUriWithAuthCfg();
325+
}
326+
teDataUri->clear();
327+
teDataUri->setPlainText( mDataUri );
328+
selectAuthCfgInUri();
329+
}
330+
331+
void QgsAuthConfigUriEdit::authCfgRemoved( const QString &authcfg )
332+
{
333+
if ( authCfgFromUri() == authcfg )
334+
{
335+
removeAuthCfgFromUri();
336+
}
337+
}
338+
339+
int QgsAuthConfigUriEdit::authCfgIndex()
340+
{
341+
QRegExp rx( QgsAuthManager::instance()->configIdRegex() );
342+
return rx.indexIn( mDataUri );
343+
}
344+
345+
QString QgsAuthConfigUriEdit::authCfgFromUri()
346+
{
347+
int startindex = authCfgIndex();
348+
if ( startindex == -1 )
349+
return QString();
350+
351+
return mDataUri.mid( startindex + 8, 7 );
352+
}
353+
354+
void QgsAuthConfigUriEdit::selectAuthCfgInUri()
355+
{
356+
int startindex = authCfgIndex();
357+
if ( startindex == -1 )
358+
return;
359+
360+
// authcfg=.{7} will always be 15 chars
361+
QTextCursor tc = teDataUri->textCursor();
362+
tc.setPosition( startindex );
363+
tc.setPosition( startindex + 15, QTextCursor::KeepAnchor );
364+
teDataUri->setTextCursor( tc );
365+
teDataUri->setFocus();
366+
}
367+
368+
void QgsAuthConfigUriEdit::updateUriWithAuthCfg()
369+
{
370+
int startindex = authCfgIndex();
371+
if ( startindex == -1 )
372+
{
373+
if ( mAuthCfg.size() == 7 )
374+
{
375+
wdgtAuthSelect->showMessage( tr( "Adding authcfg to URI not supported" ) );
376+
}
377+
return;
378+
}
379+
380+
mDataUri = mDataUri.replace( startindex + 8, 7, mAuthCfg );
381+
}
382+
383+
void QgsAuthConfigUriEdit::removeAuthCfgFromUri()
384+
{
385+
int startindex = authCfgIndex();
386+
if ( startindex == -1 )
387+
return;
388+
389+
// add any preceding space so two spaces will not result after removal
390+
int rmvlen = 15;
391+
if ( startindex - 1 >= 0
392+
&& ( mDataUri.at( startindex - 1 ).isSpace()
393+
|| mDataUri.at( startindex - 1 ) == QChar( '&' ) ) )
394+
{
395+
startindex -= 1;
396+
rmvlen += 1;
397+
}
398+
399+
// trim any leftover spaces or & from ends
400+
mDataUri = mDataUri.remove( startindex, rmvlen ).trimmed();
401+
if ( mDataUri.at( 0 ) == QChar( '&' ) )
402+
mDataUri = mDataUri.remove( 0, 1 );
403+
404+
// trim any & from
405+
406+
mAuthCfg.clear();
407+
}
408+

0 commit comments

Comments
 (0)