Skip to content

Commit

Permalink
move connection name generation to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Aug 8, 2024
1 parent 698074c commit cf9166f
Show file tree
Hide file tree
Showing 14 changed files with 38 additions and 92 deletions.
8 changes: 1 addition & 7 deletions src/gui/providers/gdal/qgsgdalclouddataitemguiprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,7 @@ void QgsGdalCloudDataItemGuiProvider::duplicateConnection( QgsGdalCloudConnectio
const QgsGdalCloudProviderConnection::Data connection = QgsGdalCloudProviderConnection::connection( connectionName );
const QStringList connections = QgsGdalCloudProviderConnection::sTreeConnectionCloud->items();

int i = 0;
QString newConnectionName( connectionName );
while ( connections.contains( newConnectionName ) )
{
++i;
newConnectionName = QString( "%1 - copy %2" ).arg( connectionName ) .arg( i );
}
const QString newConnectionName = QgsDataItemGuiProviderUtils::uniqueName( connectionName, connections );

QgsGdalCloudProviderConnection::addConnection( newConnectionName, connection );
cloudItem->refresh();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,7 @@ void QgsSensorThingsDataItemGuiProvider::duplicateConnection( QgsDataItem *item
const QgsSensorThingsProviderConnection::Data connection = QgsSensorThingsProviderConnection::connection( connectionName );
const QStringList connections = QgsSensorThingsProviderConnection::sTreeSensorThingsConnections->items();

int i = 0;
QString newConnectionName( connectionName );
while ( connections.contains( newConnectionName ) )
{
++i;
newConnectionName = QString( "%1 - copy %2" ).arg( connectionName ).arg( i );
}
const QString newConnectionName = QgsDataItemGuiProviderUtils::uniqueName( connectionName, connections );

QgsSensorThingsProviderConnection::addConnection( newConnectionName, connection );
item->parent()->refreshConnections();
Expand Down
13 changes: 13 additions & 0 deletions src/gui/qgsdataitemguiproviderutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,16 @@ void QgsDataItemGuiProviderUtils::deleteConnectionsPrivate( const QStringList &c
if ( firstParent )
firstParent->refreshConnections();
}

const QString QgsDataItemGuiProviderUtils::uniqueName( const QString &name, const QStringList &connectionNames )
{
int i = 0;
QString newConnectionName( name );
while ( connectionNames.contains( newConnectionName ) )
{
++i;
newConnectionName = QString( "%1 (copy %2)" ).arg( name ) .arg( i );
}

return newConnectionName;
}
11 changes: 11 additions & 0 deletions src/gui/qgsdataitemguiproviderutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ class GUI_EXPORT QgsDataItemGuiProviderUtils
QPointer< QgsDataItem > firstParent( items.at( 0 )->parent() );
deleteConnectionsPrivate( connectionNames, deleteConnection, firstParent );
}

/**
* Check if connection with \a name exists in \a connectionNames list and then try to
* append a number to it to get a unique name.
*
* \note Not available in Python bindings
*
* \since QGIS 3.40
*/
static const QString uniqueName( const QString &name, const QStringList &connectionNames );

#endif

private:
Expand Down
8 changes: 1 addition & 7 deletions src/gui/tiledscene/qgstiledscenedataitemguiprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,7 @@ void QgsTiledSceneDataItemGuiProvider::duplicateConnection( QgsDataItem *item )
const QgsTiledSceneProviderConnection::Data connection = QgsTiledSceneProviderConnection::connection( connectionName );
const QStringList connections = QgsTiledSceneProviderConnection::sTreeConnectionTiledScene->items();

int i = 0;
QString newConnectionName( connectionName );
while ( connections.contains( newConnectionName ) )
{
++i;
newConnectionName = QString( "%1 - copy %2" ).arg( connectionName ).arg( i );
}
const QString newConnectionName = QgsDataItemGuiProviderUtils::uniqueName( connectionName, connections );

QgsTiledSceneProviderConnection::addConnection( newConnectionName, connection );
item->parent()->refreshConnections();
Expand Down
8 changes: 1 addition & 7 deletions src/gui/vectortile/qgsvectortiledataitemguiprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,7 @@ void QgsVectorTileDataItemGuiProvider::duplicateConnection( QgsDataItem *item )
const QgsVectorTileProviderConnection::Data connection = QgsVectorTileProviderConnection::connection( connectionName );
const QStringList connections = QgsVectorTileProviderConnection::sTreeConnectionVectorTile->items();

int i = 0;
QString newConnectionName( connectionName );
while ( connections.contains( newConnectionName ) )
{
++i;
newConnectionName = QString( "%1 - copy %2" ).arg( connectionName ).arg( i );
}
const QString newConnectionName = QgsDataItemGuiProviderUtils::uniqueName( connectionName, connections );

QgsVectorTileProviderConnection::addConnection( newConnectionName, connection );
item->parent()->refreshConnections();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,7 @@ void QgsArcGisRestDataItemGuiProvider::duplicateConnection( QgsDataItem *item )
const QString connectionName = item->name();
const QStringList connections = QgsArcGisConnectionSettings::sTreeConnectionArcgis->items();

int i = 0;
QString newConnectionName( connectionName );
while ( connections.contains( newConnectionName ) )
{
++i;
newConnectionName = QString( "%1 - copy %2" ).arg( connectionName ).arg( i );
}
const QString newConnectionName = QgsDataItemGuiProviderUtils::uniqueName( connectionName, connections );

QgsArcGisConnectionSettings::settingsUrl->setValue( QgsArcGisConnectionSettings::settingsUrl->value( connectionName ), newConnectionName );

Expand All @@ -203,7 +197,6 @@ void QgsArcGisRestDataItemGuiProvider::duplicateConnection( QgsDataItem *item )
item->parent()->refreshConnections();
}


void QgsArcGisRestDataItemGuiProvider::refreshConnection( QgsDataItem *item )
{
item->refresh();
Expand Down
8 changes: 1 addition & 7 deletions src/providers/hana/qgshanadataitemguiprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,7 @@ void QgsHanaDataItemGuiProvider::duplicateConnection( QgsDataItem *item )
const QStringList connections = settings.childGroups();
settings.endGroup();

int i = 0;
QString newConnectionName( connectionName );
while ( connections.contains( newConnectionName ) )
{
++i;
newConnectionName = QString( "%1 - copy %2" ).arg( connectionName ).arg( i );
}
const QString newConnectionName = QgsDataItemGuiProviderUtils::uniqueName( connectionName, connections );

QgsHanaSettings hanaSettings( connectionName, true );
QgsHanaSettings newHanaSettings( newConnectionName );
Expand Down
8 changes: 1 addition & 7 deletions src/providers/mssql/qgsmssqldataitemguiprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,7 @@ void QgsMssqlDataItemGuiProvider::duplicateConnection( QgsDataItem *item )
const QStringList connections = settings.childGroups();
settings.endGroup();

int i = 0;
QString newConnectionName( connectionName );
while ( connections.contains( newConnectionName ) )
{
++i;
newConnectionName = QString( "%1 - copy %2" ).arg( connectionName ).arg( i );
}
const QString newConnectionName = QgsDataItemGuiProviderUtils::uniqueName( connectionName, connections );

const QString key = "/MSSQL/connections/" + connectionName;
const QString newKey = "/MSSQL/connections/" + newConnectionName;
Expand Down
9 changes: 2 additions & 7 deletions src/providers/oracle/qgsoracledataitems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "qgsdbquerylog.h"
#include "qgsdbquerylog_p.h"
#include "qgsvectorlayerexporter.h"
#include "qgsdataitemguiproviderutils.h"

#include <QMessageBox>
#include <QProgressDialog>
Expand Down Expand Up @@ -311,13 +312,7 @@ void QgsOracleConnectionItem::duplicateConnection()
const QStringList connections = settings.childGroups();
settings.endGroup();

int i = 0;
QString newConnectionName( mName );
while ( connections.contains( newConnectionName ) )
{
++i;
newConnectionName = QString( "%1 - copy %2" ).arg( mName ).arg( i );
}
const QString newConnectionName = QgsDataItemGuiProviderUtils::uniqueName( connectionName, connections );

QString key = QStringLiteral( "/Oracle/connections/" ) + mName;
QString newKey = QStringLiteral( "/Oracle/connections/" ) + newConnectionName;
Expand Down
8 changes: 1 addition & 7 deletions src/providers/postgres/qgspostgresdataitemguiprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,7 @@ void QgsPostgresDataItemGuiProvider::duplicateConnection( QgsDataItem *item )
const QStringList connections = settings.childGroups();
settings.endGroup();

int i = 0;
QString newConnectionName( connectionName );
while ( connections.contains( newConnectionName ) )
{
++i;
newConnectionName = QString( "%1 - copy %2" ).arg( connectionName ).arg( i );
}
const QString newConnectionName = QgsDataItemGuiProviderUtils::uniqueName( connectionName, connections );

QString baseKey = QStringLiteral( "/PostgreSQL/connections/%1" ).arg( connectionName );
QString newBaseKey = QStringLiteral( "/PostgreSQL/connections/%1" ).arg( newConnectionName );
Expand Down
8 changes: 1 addition & 7 deletions src/providers/wcs/qgswcsdataitemguiprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,7 @@ void QgsWcsDataItemGuiProvider::duplicateConnection( QgsDataItem *item )
const QString connectionName = item->name();
const QStringList connections = QgsOwsConnection::sTreeOwsConnections->items( {QStringLiteral( "wcs" )} );

int i = 0;
QString newConnectionName( connectionName );
while ( connections.contains( newConnectionName ) )
{
++i;
newConnectionName = QString( "%1 - copy %2" ).arg( connectionName ).arg( i );
}
const QString newConnectionName = QgsDataItemGuiProviderUtils::uniqueName( connectionName, connections );

const QStringList detailsParameters { QStringLiteral( "wcs" ), connectionName };
const QStringList newDetailsParameters { QStringLiteral( "wcs" ), newConnectionName };
Expand Down
8 changes: 1 addition & 7 deletions src/providers/wfs/qgswfsdataitemguiprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,7 @@ void QgsWfsDataItemGuiProvider::duplicateConnection( QgsDataItem *item )
const QString connectionName = item->name();
const QStringList connections = QgsOwsConnection::sTreeOwsConnections->items( {QStringLiteral( "wfs" )} );

int i = 0;
QString newConnectionName( connectionName );
while ( connections.contains( newConnectionName ) )
{
++i;
newConnectionName = QString( "%1 - copy %2" ).arg( connectionName ).arg( i );
}
const QString newConnectionName = QgsDataItemGuiProviderUtils::uniqueName( connectionName, connections );

const QStringList detailsParameters { QStringLiteral( "wfs" ), connectionName };
const QStringList newDetailsParameters { QStringLiteral( "wfs" ), newConnectionName };
Expand Down
16 changes: 2 additions & 14 deletions src/providers/wms/qgswmsdataitemguiproviders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,7 @@ void QgsWmsDataItemGuiProvider::duplicateConnection( QgsDataItem *item )
const QString connectionName = item->name();
const QStringList connections = QgsOwsConnection::sTreeOwsConnections->items( {QStringLiteral( "wms" )} );

int i = 0;
QString newConnectionName( connectionName );
while ( connections.contains( newConnectionName ) )
{
++i;
newConnectionName = QString( "%1 - copy %2" ).arg( connectionName ).arg( i );
}
const QString newConnectionName = QgsDataItemGuiProviderUtils::uniqueName( connectionName, connections );

const QStringList detailsParameters { QStringLiteral( "wms" ), connectionName };
const QStringList newDetailsParameters { QStringLiteral( "wms" ), newConnectionName };
Expand Down Expand Up @@ -244,13 +238,7 @@ void QgsXyzDataItemGuiProvider::duplicateConnection( QgsDataItem *item )
const QString connectionName = item->name();
const QStringList connections = QgsXyzConnectionSettings::sTreeXyzConnections->items();

int i = 0;
QString newConnectionName( connectionName );
while ( connections.contains( newConnectionName ) )
{
++i;
newConnectionName = QString( "%1 - copy %2" ).arg( connectionName ).arg( i );
}
const QString newConnectionName = QgsDataItemGuiProviderUtils::uniqueName( connectionName, connections );

QgsXyzConnection connection = QgsXyzConnectionUtils::connection( connectionName );
connection.name = newConnectionName;
Expand Down

0 comments on commit cf9166f

Please sign in to comment.