Skip to content
Permalink
Browse files

[afs] fix listing of afs services for saved servers starting in a sub…

…-folder
  • Loading branch information
nirvn committed Dec 7, 2018
1 parent be8438b commit d136e922fb6d3e00f968ecb81dfaf5f63a4e3f2a
@@ -96,15 +96,14 @@ void addFolderItems( QVector< QgsDataItem * > &items, const QVariantMap &service
}, serviceData, baseUrl );
}

void addServiceItems( QVector< QgsDataItem * > &items, const QVariantMap &serviceData, const QString &baseUrl, const QString &authcfg, QgsDataItem *parent,
const QString &parentName )
void addServiceItems( QVector< QgsDataItem * > &items, const QVariantMap &serviceData, const QString &baseUrl, const QString &authcfg, QgsDataItem *parent )
{
QgsArcGisRestUtils::visitServiceItems(
[&items, parent, authcfg]( const QString & name, const QString & url )
{
std::unique_ptr< QgsAfsServiceItem > serviceItem = qgis::make_unique< QgsAfsServiceItem >( parent, name, url, url, authcfg );
items.append( serviceItem.release() );
}, serviceData, baseUrl, parentName );
}, serviceData, baseUrl );
}

void addLayerItems( QVector< QgsDataItem * > &items, const QVariantMap &serviceData, const QString &parentUrl, const QString &authcfg, QgsDataItem *parent )
@@ -176,7 +175,7 @@ QVector<QgsDataItem *> QgsAfsConnectionItem::createChildren()
}

addFolderItems( items, serviceData, url, authcfg, this );
addServiceItems( items, serviceData, url, authcfg, this, QString() );
addServiceItems( items, serviceData, url, authcfg, this );
addLayerItems( items, serviceData, url, authcfg, this );

return items;
@@ -287,7 +286,7 @@ QVector<QgsDataItem *> QgsAfsFolderItem::createChildren()
}

addFolderItems( items, serviceData, mBaseUrl, mAuthCfg, this );
addServiceItems( items, serviceData, mBaseUrl, mAuthCfg, this, mName );
addServiceItems( items, serviceData, mBaseUrl, mAuthCfg, this );
addLayerItems( items, serviceData, mPath, mAuthCfg, this );
return items;
}
@@ -328,7 +327,7 @@ QVector<QgsDataItem *> QgsAfsServiceItem::createChildren()
}

addFolderItems( items, serviceData, mBaseUrl, mAuthCfg, this );
addServiceItems( items, serviceData, mBaseUrl, mAuthCfg, this, mName );
addServiceItems( items, serviceData, mBaseUrl, mAuthCfg, this );
addLayerItems( items, serviceData, mPath, mAuthCfg, this );
return items;
}
@@ -41,8 +41,8 @@ bool QgsAfsSourceSelect::connectToService( const QgsOwsConnection &connection )
const QString authcfg = connection.uri().param( QStringLiteral( "authcfg" ) );
const QString baseUrl = connection.uri().param( QStringLiteral( "url" ) );

std::function< bool( const QString &, QStandardItem *, const QString & )> visitItemsRecursive;
visitItemsRecursive = [this, &visitItemsRecursive, baseUrl, authcfg, &errorTitle, &errorMessage]( const QString & baseItemUrl, QStandardItem * parentItem, const QString & parentName ) -> bool
std::function< bool( const QString &, QStandardItem * )> visitItemsRecursive;
visitItemsRecursive = [this, &visitItemsRecursive, baseUrl, authcfg, &errorTitle, &errorMessage]( const QString & baseItemUrl, QStandardItem * parentItem ) -> bool
{
const QVariantMap serviceInfoMap = QgsArcGisRestUtils::getServiceInfo( baseItemUrl, authcfg, errorTitle, errorMessage );

@@ -62,7 +62,7 @@ bool QgsAfsSourceSelect::connectToService( const QgsOwsConnection &connection )
else
mModel->appendRow( QList<QStandardItem *>() << nameItem );

if ( !visitItemsRecursive( url, nameItem, name ) )
if ( !visitItemsRecursive( url, nameItem ) )
res = false;
}, serviceInfoMap, baseUrl );

@@ -76,9 +76,9 @@ bool QgsAfsSourceSelect::connectToService( const QgsOwsConnection &connection )
else
mModel->appendRow( QList<QStandardItem *>() << nameItem );

if ( !visitItemsRecursive( url, nameItem, name ) )
if ( !visitItemsRecursive( url, nameItem ) )
res = false;
}, serviceInfoMap, baseUrl, parentName );
}, serviceInfoMap, baseUrl );

QMap< QString, QList<QStandardItem *> > layerItems;
QMap< QString, QString > parents;
@@ -143,7 +143,7 @@ bool QgsAfsSourceSelect::connectToService( const QgsOwsConnection &connection )
return res;
};

if ( !visitItemsRecursive( baseUrl, nullptr, QString() ) )
if ( !visitItemsRecursive( baseUrl, nullptr ) )
QMessageBox::warning( this, tr( "Error" ), tr( "Failed to retrieve service capabilities:\n%1: %2" ).arg( errorTitle, errorMessage ) );

return true;
@@ -1056,18 +1056,50 @@ void QgsArcGisAsyncParallelQuery::handleReply()
}
}

void QgsArcGisRestUtils::adjustBaseUrl( QString &baseUrl, const QString name )
{
QStringList parts = name.split( '/' );
QString checkString;
for ( const QString &part : parts )
{
if ( !checkString.isEmpty() )
checkString += QString( '/' );

checkString += part;
if ( baseUrl.indexOf( QRegularExpression( checkString.replace( '/', QStringLiteral( "\\/" ) ) + QStringLiteral( "\\/?$" ) ) ) > -1 )
{
baseUrl = baseUrl.left( baseUrl.length() - checkString.length() - 1 );
break;
}
}
}

void QgsArcGisRestUtils::visitFolderItems( const std::function< void( const QString &, const QString & ) > &visitor, const QVariantMap &serviceData, const QString &baseUrl )
{
QString base( baseUrl );
bool baseChecked = false;
if ( !base.endsWith( '/' ) )
base += QStringLiteral( "/" );

const QStringList folderList = serviceData.value( QStringLiteral( "folders" ) ).toStringList();
for ( const QString &folder : folderList )
{
visitor( folder, baseUrl + ( baseUrl.endsWith( '/' ) ? QString() : QString( '/' ) ) + folder );
if ( !baseChecked )
{
adjustBaseUrl( base, folder );
baseChecked = true;
}
visitor( folder, base + folder );
}
}

void QgsArcGisRestUtils::visitServiceItems( const std::function< void( const QString &, const QString & ) > &visitor, const QVariantMap &serviceData, const QString &baseUrl,
const QString &parentName )
void QgsArcGisRestUtils::visitServiceItems( const std::function< void( const QString &, const QString & ) > &visitor, const QVariantMap &serviceData, const QString &baseUrl )
{
QString base( baseUrl );
bool baseChecked = false;
if ( !base.endsWith( '/' ) )
base += QStringLiteral( "/" );

const QVariantList serviceList = serviceData.value( QStringLiteral( "services" ) ).toList();
for ( const QVariant &service : serviceList )
{
@@ -1077,11 +1109,14 @@ void QgsArcGisRestUtils::visitServiceItems( const std::function< void( const QSt
continue;

const QString serviceName = serviceMap.value( QStringLiteral( "name" ) ).toString();
QString displayName = serviceName;
if ( displayName.startsWith( parentName + '/' ) )
displayName = displayName.mid( parentName.length() + 1 );
QString displayName = serviceName.split( '/' ).last();
if ( !baseChecked )
{
adjustBaseUrl( base, serviceName );
baseChecked = true;
}

visitor( displayName, baseUrl + ( baseUrl.endsWith( '/' ) ? QString() : QString( '/' ) ) + serviceName + '/' + serviceType );
visitor( displayName, base + serviceName + '/' + serviceType );
}
}

@@ -66,8 +66,9 @@ class QgsArcGisRestUtils
static QDateTime parseDateTime( const QVariant &value );

static QUrl parseUrl( const QUrl &url );
static void adjustBaseUrl( QString &baseUrl, const QString name );
static void visitFolderItems( const std::function<void ( const QString &folderName, const QString &url )> &visitor, const QVariantMap &serviceData, const QString &baseUrl );
static void visitServiceItems( const std::function<void ( const QString &serviceName, const QString &url )> &visitor, const QVariantMap &serviceData, const QString &baseUrl, const QString &parentName );
static void visitServiceItems( const std::function<void ( const QString &serviceName, const QString &url )> &visitor, const QVariantMap &serviceData, const QString &baseUrl );
static void addLayerItems( const std::function<void ( const QString &parentLayerId, const QString &layerId, const QString &name, const QString &description, const QString &url, bool isParentLayer, const QString &authid )> &visitor, const QVariantMap &serviceData, const QString &parentUrl );
};

0 comments on commit d136e92

Please sign in to comment.
You can’t perform that action at this time.