-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor ConnectionConfigurator out of PostgresConn. Introduce ConstL…
…atin1String, to create compile time String literals.
- Loading branch information
Alexey Karandashev
committed
Jun 13, 2023
1 parent
becd5b7
commit dbb2065
Showing
6 changed files
with
203 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
/*************************************************************************** | ||
qgspostgresconnectionconfigurator.h - connection configurator class for | ||
PostgeSQL based connectors. | ||
------------------- | ||
begin : 2023/06/06 | ||
copyright : (C) 2023 by Alexey Karandashev | ||
email : reflectored at pm dot me | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#ifndef QGSCONNECTIONCONFIGURATOR_H | ||
#define QGSCONNECTIONCONFIGURATOR_H | ||
|
||
#include <QString> | ||
#include <QtCore/qstring.h> | ||
|
||
#include "qgsdatasourceuri.h" | ||
#include "qgssettings.h" | ||
|
||
template <typename T> | ||
class QgsConnectionConfigurator | ||
{ | ||
static const QString connections_prefix; | ||
static const QString connection_name_prefix; | ||
|
||
public: | ||
static QStringList connectionList() | ||
{ | ||
QgsSettings settings; | ||
settings.beginGroup( connections_prefix ); | ||
return settings.childGroups(); | ||
} | ||
|
||
static QString selectedConnection() | ||
{ | ||
QgsSettings settings; | ||
return settings.value( connections_prefix + QStringLiteral( "/selected" ) ).toString(); | ||
} | ||
|
||
static void setSelectedConnection( const QString &name ) | ||
{ | ||
QgsSettings settings; | ||
return settings.setValue( connections_prefix + QStringLiteral( "/selected" ), name ); | ||
} | ||
|
||
static QgsDataSourceUri connUri( const QString &connName ) | ||
{ | ||
QgsDebugMsgLevel( "theConnName = " + connName, 2 ); | ||
|
||
QgsSettings settings; | ||
|
||
QString key = connections_prefix + QStringLiteral( "/%1" ).arg( connName ); | ||
|
||
QString service = settings.value( key + QLatin1String( "/service" ) ).toString(); | ||
QString host = settings.value( key + QLatin1String( "/host" ) ).toString(); | ||
QString port = settings.value( key + QLatin1String( "/port" ) ).toString(); | ||
if ( port.length() == 0 ) | ||
{ | ||
port = T::mConnectionTypePort; | ||
} | ||
QString database = settings.value( key + QLatin1String( "/database" ) ).toString(); | ||
|
||
bool estimatedMetadata = useEstimatedMetadata( connName ); | ||
QgsDataSourceUri::SslMode sslmode = settings.enumValue( key + QLatin1String( "/sslmode" ), QgsDataSourceUri::SslPrefer ); | ||
|
||
QString username; | ||
QString password; | ||
if ( settings.value( key + QLatin1String( "/saveUsername" ) ).toString() == QLatin1String( "true" ) ) | ||
{ | ||
username = settings.value( key + QLatin1String( "/username" ) ).toString(); | ||
} | ||
|
||
if ( settings.value( key + QLatin1String( "/savePassword" ) ).toString() == QLatin1String( "true" ) ) | ||
{ | ||
password = settings.value( key + QLatin1String( "/password" ) ).toString(); | ||
} | ||
|
||
// Old save setting | ||
if ( settings.contains( key + QLatin1String( "/save" ) ) ) | ||
{ | ||
username = settings.value( key + QLatin1String( "/username" ) ).toString(); | ||
|
||
if ( settings.value( key + QLatin1String( "/save" ) ).toString() == QLatin1String( "true" ) ) | ||
{ | ||
password = settings.value( key + QLatin1String( "/password" ) ).toString(); | ||
} | ||
} | ||
|
||
QString authcfg = settings.value( key + QLatin1String( "/authcfg" ) ).toString(); | ||
|
||
QgsDataSourceUri uri; | ||
if ( !service.isEmpty() ) | ||
{ | ||
uri.setConnection( service, database, username, password, sslmode, authcfg ); | ||
} | ||
else | ||
{ | ||
uri.setConnection( host, port, database, username, password, sslmode, authcfg ); | ||
} | ||
uri.setUseEstimatedMetadata( estimatedMetadata ); | ||
|
||
return uri; | ||
} | ||
|
||
|
||
static bool publicSchemaOnly( const QString &connectionName ) | ||
{ | ||
QgsSettings settings; | ||
return settings.value( connection_name_prefix.arg( connectionName ) + QStringLiteral( "/publicOnly" ), false ).toBool(); | ||
} | ||
|
||
static bool geometryColumnsOnly( const QString &connectionName ) | ||
{ | ||
QgsSettings settings; | ||
|
||
return settings.value( connection_name_prefix.arg( connectionName ) + QStringLiteral( "/geometryColumnsOnly" ), false ).toBool(); | ||
} | ||
|
||
static bool dontResolveType( const QString &connectionName ) | ||
{ | ||
QgsSettings settings; | ||
|
||
return settings.value( connection_name_prefix.arg( connectionName ) + QStringLiteral( "/dontResolveType" ), false ).toBool(); | ||
} | ||
|
||
static bool useEstimatedMetadata( const QString &connectionName ) | ||
{ | ||
QgsSettings settings; | ||
|
||
return settings.value( connection_name_prefix.arg( connectionName ) + QStringLiteral( "/estimatedMetadata" ), false ).toBool(); | ||
} | ||
|
||
static bool allowGeometrylessTables( const QString &connectionName ) | ||
{ | ||
QgsSettings settings; | ||
return settings.value( connection_name_prefix.arg( connectionName ) + QStringLiteral( "/allowGeometrylessTables" ), false ).toBool(); | ||
} | ||
|
||
static bool allowProjectsInDatabase( const QString &connectionName ) | ||
{ | ||
QgsSettings settings; | ||
return settings.value( connection_name_prefix.arg( connectionName ) + QStringLiteral( "/projectsInDatabase" ), false ).toBool(); | ||
} | ||
|
||
static void deleteConnection( const QString &connectionName ) | ||
{ | ||
QgsSettings settings; | ||
|
||
QString key = connection_name_prefix .arg( connectionName ); | ||
settings.remove( key + QLatin1String( "/service" ) ); | ||
settings.remove( key + QLatin1String( "/host" ) ); | ||
settings.remove( key + QLatin1String( "/port" ) ); | ||
settings.remove( key + QLatin1String( "/database" ) ); | ||
settings.remove( key + QLatin1String( "/username" ) ); | ||
settings.remove( key + QLatin1String( "/password" ) ); | ||
settings.remove( key + QLatin1String( "/sslmode" ) ); | ||
settings.remove( key + QLatin1String( "/publicOnly" ) ); | ||
settings.remove( key + QLatin1String( "/geometryColumnsOnly" ) ); | ||
settings.remove( key + QLatin1String( "/allowGeometrylessTables" ) ); | ||
settings.remove( key + QLatin1String( "/estimatedMetadata" ) ); | ||
settings.remove( key + QLatin1String( "/saveUsername" ) ); | ||
settings.remove( key + QLatin1String( "/savePassword" ) ); | ||
settings.remove( key + QLatin1String( "/save" ) ); | ||
settings.remove( key + QLatin1String( "/authcfg" ) ); | ||
settings.remove( key + QLatin1String( "/keys" ) ); | ||
settings.remove( key ); | ||
} | ||
|
||
static bool allowMetadataInDatabase( const QString &connectionName ) | ||
{ | ||
QgsSettings settings; | ||
return settings.value( connections_prefix + QStringLiteral( "/%1/metadataInDatabase" ).arg( connectionName ), false ).toBool(); | ||
} | ||
}; | ||
|
||
template <typename T> | ||
const QString QgsConnectionConfigurator<T>::connections_prefix = QStringLiteral( "/%1/connections" ).arg( T::mConnectionTypeName ); | ||
|
||
template <typename T> | ||
const QString QgsConnectionConfigurator<T>::connection_name_prefix = QStringLiteral( "%1/%2" ).arg( connections_prefix ); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.