22
22
#include " qgsconfig.h"
23
23
24
24
#include " qgsapplication.h"
25
+ #include " qgsauthmanager.h"
25
26
#include " qgscoordinatetransform.h"
26
27
#include " qgsdataitem.h"
27
28
#include " qgsdatasourceuri.h"
@@ -255,6 +256,33 @@ QgsGdalProvider::QgsGdalProvider( const QgsGdalProvider &other )
255
256
copyBaseSettings ( other );
256
257
}
257
258
259
+ QString QgsGdalProvider::dataSourceUri ( bool expandAuthConfig ) const
260
+ {
261
+ if ( expandAuthConfig && QgsDataProvider::dataSourceUri ( ).contains ( QLatin1String ( " authcfg" ) ) )
262
+ {
263
+ QString uri ( QgsDataProvider::dataSourceUri () );
264
+ // Check for authcfg
265
+ QRegularExpression authcfgRe ( " authcfg='([^']+)'" );
266
+ QRegularExpressionMatch match;
267
+ if ( uri.contains ( authcfgRe, &match ) )
268
+ {
269
+ uri = uri.replace ( match.captured ( 0 ), QString () );
270
+ QString configId ( match.captured ( 1 ) );
271
+ QStringList connectionItems;
272
+ connectionItems << uri;
273
+ if ( QgsApplication::authManager ()->updateDataSourceUriItems ( connectionItems, configId, QStringLiteral ( " gdal" ) ) )
274
+ {
275
+ uri = connectionItems.first ( );
276
+ }
277
+ }
278
+ return uri;
279
+ }
280
+ else
281
+ {
282
+ return QgsDataProvider::dataSourceUri ();
283
+ }
284
+ }
285
+
258
286
QgsGdalProvider *QgsGdalProvider::clone () const
259
287
{
260
288
return new QgsGdalProvider ( *this );
@@ -1723,7 +1751,7 @@ QString QgsGdalProvider::buildPyramids( const QList<QgsRasterPyramid> &rasterPyr
1723
1751
1724
1752
// test if the file is writable
1725
1753
// QFileInfo myQFile( mDataSource );
1726
- QFileInfo myQFile ( dataSourceUri () );
1754
+ QFileInfo myQFile ( dataSourceUri ( true ) );
1727
1755
1728
1756
if ( !myQFile.isWritable () )
1729
1757
{
@@ -1753,12 +1781,12 @@ QString QgsGdalProvider::buildPyramids( const QList<QgsRasterPyramid> &rasterPyr
1753
1781
GDALClose ( mGdalDataset );
1754
1782
// mGdalBaseDataset = GDALOpen( QFile::encodeName( dataSourceUri() ).constData(), GA_Update );
1755
1783
1756
- mGdalBaseDataset = gdalOpen ( dataSourceUri ().toUtf8 ().constData (), GA_Update );
1784
+ mGdalBaseDataset = gdalOpen ( dataSourceUri ( true ).toUtf8 ().constData (), GA_Update );
1757
1785
1758
1786
// if the dataset couldn't be opened in read / write mode, tell the user
1759
1787
if ( !mGdalBaseDataset )
1760
1788
{
1761
- mGdalBaseDataset = gdalOpen ( dataSourceUri ().toUtf8 ().constData (), GA_ReadOnly );
1789
+ mGdalBaseDataset = gdalOpen ( dataSourceUri ( true ).toUtf8 ().constData (), GA_ReadOnly );
1762
1790
// Since we are not a virtual warped dataset, mGdalDataSet and mGdalBaseDataset are supposed to be the same
1763
1791
mGdalDataset = mGdalBaseDataset ;
1764
1792
return QStringLiteral ( " ERROR_WRITE_FORMAT" );
@@ -1859,7 +1887,7 @@ QString QgsGdalProvider::buildPyramids( const QList<QgsRasterPyramid> &rasterPyr
1859
1887
// something bad happenend
1860
1888
// QString myString = QString (CPLGetLastError());
1861
1889
GDALClose ( mGdalBaseDataset );
1862
- mGdalBaseDataset = gdalOpen ( dataSourceUri ().toUtf8 ().constData (), mUpdate ? GA_Update : GA_ReadOnly );
1890
+ mGdalBaseDataset = gdalOpen ( dataSourceUri ( true ).toUtf8 ().constData (), mUpdate ? GA_Update : GA_ReadOnly );
1863
1891
// Since we are not a virtual warped dataset, mGdalDataSet and mGdalBaseDataset are supposed to be the same
1864
1892
mGdalDataset = mGdalBaseDataset ;
1865
1893
@@ -1911,7 +1939,7 @@ QString QgsGdalProvider::buildPyramids( const QList<QgsRasterPyramid> &rasterPyr
1911
1939
QgsDebugMsg ( " Reopening dataset ..." );
1912
1940
// close the gdal dataset and reopen it in read only mode
1913
1941
GDALClose ( mGdalBaseDataset );
1914
- mGdalBaseDataset = gdalOpen ( dataSourceUri ().toUtf8 ().constData (), mUpdate ? GA_Update : GA_ReadOnly );
1942
+ mGdalBaseDataset = gdalOpen ( dataSourceUri ( true ).toUtf8 ().constData (), mUpdate ? GA_Update : GA_ReadOnly );
1915
1943
// Since we are not a virtual warped dataset, mGdalDataSet and mGdalBaseDataset are supposed to be the same
1916
1944
mGdalDataset = mGdalBaseDataset ;
1917
1945
}
@@ -2625,7 +2653,7 @@ bool QgsGdalProvider::initIfNeeded()
2625
2653
2626
2654
mHasInit = true ;
2627
2655
2628
- QString gdalUri = dataSourceUri ();
2656
+ QString gdalUri = dataSourceUri ( true );
2629
2657
2630
2658
// Try to open using VSIFileHandler (see qgsogrprovider.cpp)
2631
2659
QString vsiPrefix = QgsZipItem::vsiPrefix ( gdalUri );
@@ -2636,7 +2664,7 @@ bool QgsGdalProvider::initIfNeeded()
2636
2664
QgsDebugMsg ( QString ( " Trying %1 syntax, uri= %2" ).arg ( vsiPrefix, dataSourceUri () ) );
2637
2665
}
2638
2666
2639
- gdalUri = dataSourceUri ();
2667
+ gdalUri = dataSourceUri ( true );
2640
2668
2641
2669
CPLErrorReset ();
2642
2670
mGdalBaseDataset = gdalOpen ( gdalUri.toUtf8 ().constData (), mUpdate ? GA_Update : GA_ReadOnly );
@@ -3041,7 +3069,7 @@ bool QgsGdalProvider::remove()
3041
3069
mGdalDataset = nullptr ;
3042
3070
3043
3071
CPLErrorReset ();
3044
- CPLErr err = GDALDeleteDataset ( driver, dataSourceUri ().toUtf8 ().constData () );
3072
+ CPLErr err = GDALDeleteDataset ( driver, dataSourceUri ( true ).toUtf8 ().constData () );
3045
3073
if ( err != CPLE_None )
3046
3074
{
3047
3075
QgsLogger::warning ( " RasterIO error: " + QString::fromUtf8 ( CPLGetLastErrorMsg () ) );
@@ -3240,7 +3268,7 @@ bool QgsGdalProvider::setEditable( bool enabled )
3240
3268
mUpdate = enabled;
3241
3269
3242
3270
// reopen the dataset
3243
- mGdalBaseDataset = gdalOpen ( dataSourceUri ().toUtf8 ().constData (), mUpdate ? GA_Update : GA_ReadOnly );
3271
+ mGdalBaseDataset = gdalOpen ( dataSourceUri ( true ).toUtf8 ().constData (), mUpdate ? GA_Update : GA_ReadOnly );
3244
3272
if ( !mGdalBaseDataset )
3245
3273
{
3246
3274
QString msg = QStringLiteral ( " Cannot reopen GDAL dataset %1:\n %2" ).arg ( dataSourceUri (), QString::fromUtf8 ( CPLGetLastErrorMsg () ) );
0 commit comments