Skip to content

Commit

Permalink
make QgsLocalizedPathRegistry thread safe (#36865)
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed Jun 5, 2020
1 parent 58e17c6 commit 97862bb
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Expand Up @@ -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)
Expand Down
Expand Up @@ -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 );
};

/************************************************************************
Expand Down
17 changes: 17 additions & 0 deletions src/core/qgslocalizeddatapathregistry.cpp
Expand Up @@ -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 );
Expand All @@ -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 );
Expand All @@ -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();
Expand All @@ -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 )
{
Expand All @@ -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();
}

Expand Down
7 changes: 7 additions & 0 deletions src/core/qgslocalizeddatapathregistry.h
Expand Up @@ -20,6 +20,7 @@

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

#include "qgis_core.h"
#include "qgis_sip.h"
Expand Down Expand Up @@ -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.