diff --git a/src/app/main.cpp b/src/app/main.cpp index c2c035f10a..30a2a9346e 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -201,6 +201,60 @@ int main( int argc, char **argv ) delete[] newPaths; } +#if defined( Q_OS_ANDROID ) || defined( Q_OS_IOS ) + for ( const QString &dataDir : dataDirs ) + { + const QFileInfo pgServiceFileInfo( QStringLiteral( "%1/pg_service.conf" ).arg( dataDir ) ); + if ( pgServiceFileInfo.exists() && pgServiceFileInfo.isReadable() ) + { + const QString systemLocalDataPath = platformUtils->systemLocalDataLocation( QString() ); + + QFile pgServiceFile( QStringLiteral( "%1/pg_service.conf" ).arg( dataDir ) ); + pgServiceFile.open( QFile::ReadOnly | QFile::Text ); + QTextStream textStream( &pgServiceFile ); + QString psServiceFileContent = textStream.readAll(); + pgServiceFile.close(); + + const QStringList keys = QStringList() << QStringLiteral( "sslrootcert" ) << QStringLiteral( "sslcert" ) << QStringLiteral( "sslkey" ); + for ( const QString &key : keys ) + { + const QRegularExpression rx( QStringLiteral( "%1=(.*)" ).arg( key ) ); + QRegularExpressionMatchIterator matchIt = rx.globalMatch( psServiceFileContent ); + while ( matchIt.hasNext() ) + { + const QRegularExpressionMatch match = matchIt.next(); + const QString fileName = match.captured( 1 ).trimmed(); + + // Check if the file is relative to the pg_service.conf, in which case copy to user-owned location, use absolute path, and change permissions + const QString filePath = QStringLiteral( "%1/%2" ).arg( dataDir, fileName ); + const QFileInfo fileInfo( filePath ); + if ( QFileInfo::exists( filePath ) ) + { + const QString newFilePath = QStringLiteral( "%1/%2" ).arg( systemLocalDataPath, fileName ); + if ( QFileInfo::exists( newFilePath ) ) + { + QFile newFile( newFilePath ); + newFile.remove(); + } + QFile::copy( filePath, newFilePath ); + QFile::setPermissions( newFilePath, QFileDevice::ReadOwner | QFileDevice::WriteOwner ); + psServiceFileContent.replace( QStringLiteral( "%1=%2" ).arg( key, match.captured( 1 ) ), QStringLiteral( "%1=%2" ).arg( key, newFilePath ) ); + } + } + } + + const QString localPgServiceFileName = QStringLiteral( "%1/pg_service.conf" ).arg( systemLocalDataPath ); + QFile localPgServiceFile( localPgServiceFileName ); + localPgServiceFile.open( QFile::WriteOnly ); + localPgServiceFile.write( psServiceFileContent.toUtf8() ); + localPgServiceFile.close(); + + setenv( "PGSYSCONFDIR", systemLocalDataPath.toUtf8(), true ); + break; + } + } +#endif + #if WITH_SENTRY sentry_wrapper::install_message_handler(); #endif diff --git a/src/core/platforms/platformutilities.cpp b/src/core/platforms/platformutilities.cpp index 5d172fe706..21bdfef0db 100644 --- a/src/core/platforms/platformutilities.cpp +++ b/src/core/platforms/platformutilities.cpp @@ -140,7 +140,7 @@ QString PlatformUtilities::systemSharedDataLocation() const QString PlatformUtilities::systemLocalDataLocation( const QString &subDir ) const { - return QStandardPaths::writableLocation( QStandardPaths::AppDataLocation ) + '/' + subDir; + return QStandardPaths::writableLocation( QStandardPaths::AppDataLocation ) + ( !subDir.isEmpty() ? '/' + subDir : QString() ); } bool PlatformUtilities::hasQgsProject() const diff --git a/src/core/platforms/platformutilities.h b/src/core/platforms/platformutilities.h index f52b65fe91..313d46a2a0 100644 --- a/src/core/platforms/platformutilities.h +++ b/src/core/platforms/platformutilities.h @@ -92,7 +92,7 @@ class QFIELD_CORE_EXPORT PlatformUtilities : public QObject * this includes local qfieldcloud data or sample projects. * A \a subDir is appended to the path. */ - virtual QString systemLocalDataLocation( const QString &subDir ) const; + virtual QString systemLocalDataLocation( const QString &subDir = QString() ) const; /** * Returns TRUE is a project file has been provided and should be opened at launch. diff --git a/src/core/qgismobileapp.cpp b/src/core/qgismobileapp.cpp index cb277f427b..767bd79fcb 100644 --- a/src/core/qgismobileapp.cpp +++ b/src/core/qgismobileapp.cpp @@ -287,18 +287,6 @@ QgisMobileapp::QgisMobileapp( QgsApplication *app, QObject *parent ) if ( !dataDirs.isEmpty() ) { -#if defined( Q_OS_ANDROID ) || defined( Q_OS_IOS ) - for ( const QString &dataDir : dataDirs ) - { - QFileInfo pgServiceFile( QStringLiteral( "%1/pg_service.conf" ).arg( dataDir ) ); - if ( pgServiceFile.exists() && pgServiceFile.isReadable() ) - { - setenv( "PGSYSCONFDIR", dataDir.toUtf8(), true ); - break; - } - } -#endif - QgsApplication::instance()->authManager()->setPasswordHelperEnabled( false ); QgsApplication::instance()->authManager()->setMasterPassword( QString( "qfield" ) ); // import authentication method configurations