Skip to content
Permalink
Browse files

make QgsLocalizedPathRegistry thread safe (#36865)

  • Loading branch information
3nids committed Jun 5, 2020
1 parent 58e17c6 commit 97862bbc15cd71517b3053cb941d05d925989d32
@@ -884,7 +884,7 @@ IF (WITH_CORE AND WITH_BINDINGS)
INCLUDE(SIPMacros)

SET(SIP_INCLUDES ${PYQT_SIP_DIR} ${CMAKE_SOURCE_DIR}/python)
SET(SIP_CONCAT_PARTS 4)
SET(SIP_CONCAT_PARTS 6)

IF (NOT BINDINGS_GLOBAL_INSTALL)
SET(PYTHON_SITE_PACKAGES_DIR ${QGIS_DATA_DIR}/python)
@@ -60,6 +60,8 @@ Since the paths are stored by order of preference, lower positions in the list t
Unregisters a localized path
%End

private:
QgsLocalizedDataPathRegistry( const QgsLocalizedDataPathRegistry &other );
};

/************************************************************************
@@ -28,6 +28,8 @@ QgsLocalizedDataPathRegistry::QgsLocalizedDataPathRegistry()

QString QgsLocalizedDataPathRegistry::globalPath( const QString &relativePath ) const
{
QgsReadWriteLocker locker( mLock, QgsReadWriteLocker::Read );

for ( const QDir &basePath : qgis::as_const( mPaths ) )
if ( basePath.exists( relativePath ) )
return basePath.absoluteFilePath( relativePath );
@@ -37,6 +39,8 @@ QString QgsLocalizedDataPathRegistry::globalPath( const QString &relativePath )

QString QgsLocalizedDataPathRegistry::localizedPath( const QString &fullPath ) const
{
QgsReadWriteLocker locker( mLock, QgsReadWriteLocker::Read );

for ( const QDir &basePath : qgis::as_const( mPaths ) )
if ( fullPath.startsWith( basePath.absolutePath() ) )
return basePath.relativeFilePath( fullPath );
@@ -47,6 +51,8 @@ QString QgsLocalizedDataPathRegistry::localizedPath( const QString &fullPath ) c

QStringList QgsLocalizedDataPathRegistry::paths() const
{
QgsReadWriteLocker locker( mLock, QgsReadWriteLocker::Read );

QStringList paths;
for ( const QDir &dir : mPaths )
paths << dir.absolutePath();
@@ -55,6 +61,8 @@ QStringList QgsLocalizedDataPathRegistry::paths() const

void QgsLocalizedDataPathRegistry::setPaths( const QStringList &paths )
{
QgsReadWriteLocker locker( mLock, QgsReadWriteLocker::Write );

mPaths.clear();
for ( const QString &path : paths )
{
@@ -63,26 +71,35 @@ void QgsLocalizedDataPathRegistry::setPaths( const QStringList &paths )
mPaths << dir;
}

locker.unlock();
writeToSettings();
}

void QgsLocalizedDataPathRegistry::registerPath( const QString &path, int position )
{
QgsReadWriteLocker locker( mLock, QgsReadWriteLocker::Read );

QDir dir( path );
if ( mPaths.contains( dir ) )
return;

locker.changeMode( QgsReadWriteLocker::Write );

if ( position >= 0 && position < mPaths.count() )
mPaths.insert( position, dir );
else
mPaths.append( dir );

locker.unlock();
writeToSettings();
}

void QgsLocalizedDataPathRegistry::unregisterPath( const QString &path )
{
QgsReadWriteLocker locker( mLock, QgsReadWriteLocker::Write );

mPaths.removeAll( QDir( path ) );
locker.unlock();
writeToSettings();
}

@@ -20,6 +20,7 @@

#include <QDir>
#include <QList>
#include <QReadWriteLock>

#include "qgis_core.h"
#include "qgis_sip.h"
@@ -65,10 +66,16 @@ class CORE_EXPORT QgsLocalizedDataPathRegistry
void unregisterPath( const QString &path );

private:
#ifdef SIP_RUN
QgsLocalizedDataPathRegistry( const QgsLocalizedDataPathRegistry &other )
{}
#endif

void readFromSettings();
void writeToSettings();

QList<QDir> mPaths;
mutable QReadWriteLock mLock;
};

#endif // QGSLOCALIZEDDATAPATHREGISTRY_H

0 comments on commit 97862bb

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