QGIS API Documentation  3.23.0-Master (dd0cd13a00)
qgslocalizeddatapathregistry.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgslocalizeddatapathregistry.cpp
3  --------------------------------------
4  Date : May 2020
5  Copyright : (C) 2020 by Denis Rouzaud
6  Email : denis.rouzaud
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include <QDir>
17 
19 #include "qgssettings.h"
20 #include "qgis.h"
21 #include "qgsreadwritelocker.h"
22 
23 
25 {
26  readFromSettings();
27 }
28 
29 QString QgsLocalizedDataPathRegistry::globalPath( const QString &relativePath ) const
30 {
32 
33  for ( const QDir &basePath : std::as_const( mPaths ) )
34  if ( basePath.exists( relativePath ) )
35  return basePath.absoluteFilePath( relativePath );
36 
37  return QString();
38 }
39 
40 QString QgsLocalizedDataPathRegistry::localizedPath( const QString &fullPath ) const
41 {
43 
44  for ( const QDir &basePath : std::as_const( mPaths ) )
45  if ( fullPath.startsWith( basePath.absolutePath() ) )
46  return basePath.relativeFilePath( fullPath );
47 
48  return QString();
49 
50 }
51 
53 {
55 
56  QStringList paths;
57  for ( const QDir &dir : mPaths )
58  paths << dir.absolutePath();
59  return paths;
60 }
61 
62 void QgsLocalizedDataPathRegistry::setPaths( const QStringList &paths )
63 {
65 
66  mPaths.clear();
67  for ( const QString &path : paths )
68  {
69  QDir dir( path );
70  if ( !mPaths.contains( dir ) )
71  mPaths << dir;
72  }
73 
74  locker.unlock();
75  writeToSettings();
76 }
77 
78 void QgsLocalizedDataPathRegistry::registerPath( const QString &path, int position )
79 {
81 
82  QDir dir( path );
83  if ( mPaths.contains( dir ) )
84  return;
85 
87 
88  if ( position >= 0 && position < mPaths.count() )
89  mPaths.insert( position, dir );
90  else
91  mPaths.append( dir );
92 
93  locker.unlock();
94  writeToSettings();
95 }
96 
98 {
100 
101  mPaths.removeAll( QDir( path ) );
102  locker.unlock();
103  writeToSettings();
104 }
105 
106 void QgsLocalizedDataPathRegistry::readFromSettings()
107 {
109 }
110 
111 void QgsLocalizedDataPathRegistry::writeToSettings()
112 {
113  settingsLocalizedDataPaths.setValue( paths() );
114 }
QString globalPath(const QString &localizedPath) const
Returns the global path if the file has been found in one of the paths, an empty string otherwise.
void setPaths(const QStringList &paths)
Sets the complete list of localized path.
void unregisterPath(const QString &path)
Unregisters a localized path.
QString localizedPath(const QString &globalPath) const
Returns the localized path if the file has been found in one of the path, an empty string otherwise.
void registerPath(const QString &path, int position=-1)
Registers a localized path If position is given, the path is inserted at the given position in the li...
QStringList paths() const
Returns a list of registered localized paths.
static const QgsSettingsEntryStringList settingsLocalizedDataPaths
Settings entry localized data paths.
The QgsReadWriteLocker class is a convenience class that simplifies locking and unlocking QReadWriteL...
@ Write
Lock for write.
@ Read
Lock for read.
void unlock()
Unlocks the lock.
void changeMode(Mode mode)
Change the mode of the lock to mode.