QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 : qgis::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 : qgis::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 {
108  setPaths( QgsSettings().value( QStringLiteral( "/qgis/localized_data_paths" ) ).toStringList() );
109 }
110 
111 void QgsLocalizedDataPathRegistry::writeToSettings()
112 {
113  QgsSettings().setValue( QStringLiteral( "/qgis/localized_data_paths" ), paths() );
114 }
QgsReadWriteLocker::changeMode
void changeMode(Mode mode)
Change the mode of the lock to mode.
Definition: qgsreadwritelocker.cpp:30
qgsreadwritelocker.h
QgsReadWriteLocker::Read
@ Read
Lock for read.
Definition: qgsreadwritelocker.h:75
qgslocalizeddatapathregistry.h
QgsLocalizedDataPathRegistry::registerPath
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...
Definition: qgslocalizeddatapathregistry.cpp:78
qgis.h
QgsSettings
Definition: qgssettings.h:61
QgsLocalizedDataPathRegistry::unregisterPath
void unregisterPath(const QString &path)
Unregisters a localized path.
Definition: qgslocalizeddatapathregistry.cpp:97
QgsLocalizedDataPathRegistry::localizedPath
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.
Definition: qgslocalizeddatapathregistry.cpp:40
QgsReadWriteLocker::unlock
void unlock()
Unlocks the lock.
Definition: qgsreadwritelocker.cpp:45
QgsReadWriteLocker
Definition: qgsreadwritelocker.h:40
QgsLocalizedDataPathRegistry::QgsLocalizedDataPathRegistry
QgsLocalizedDataPathRegistry()
Definition: qgslocalizeddatapathregistry.cpp:24
QgsReadWriteLocker::Write
@ Write
Lock for write.
Definition: qgsreadwritelocker.h:76
QgsSettings::setValue
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
Definition: qgssettings.cpp:289
QgsLocalizedDataPathRegistry::paths
QStringList paths() const
Returns a list of registered localized paths.
Definition: qgslocalizeddatapathregistry.cpp:52
QgsLocalizedDataPathRegistry::globalPath
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.
Definition: qgslocalizeddatapathregistry.cpp:29
QgsLocalizedDataPathRegistry::setPaths
void setPaths(const QStringList &paths)
Sets the complete list of localized path.
Definition: qgslocalizeddatapathregistry.cpp:62
qgssettings.h