35 #include "providers/gdal/qgsgdalprovider.h" 36 #include "providers/ogr/qgsogrprovider.h" 37 #include "providers/meshmemory/qgsmeshmemorydataprovider.h" 38 #ifdef HAVE_STATIC_PROVIDERS 39 #include "qgswmsprovider.h" 49 QMutexLocker locker( &sMutex );
69 QString
const &providerKey )
71 QgsProviderRegistry::Providers::const_iterator i =
72 metaData.find( providerKey );
74 if ( i != metaData.end() )
82 QgsProviderRegistry::QgsProviderRegistry(
const QString &pluginPath )
90 char **argv = qApp->argv();
91 QString appDir = argv[0];
92 int bin = appDir.findRev(
"/bin", -1,
false );
93 QString baseDir = appDir.left( bin );
94 QString mLibraryDirectory = baseDir +
"/lib";
96 mLibraryDirectory = pluginPath;
101 void QgsProviderRegistry::init()
105 mProviders[ QgsMemoryProvider::providerKey() ] =
new QgsProviderMetadata( QgsMemoryProvider::providerKey(), QgsMemoryProvider::providerDescription(), &QgsMemoryProvider::createProvider );
106 mProviders[ QgsMeshMemoryDataProvider::providerKey() ] =
new QgsProviderMetadata( QgsMeshMemoryDataProvider::providerKey(), QgsMeshMemoryDataProvider::providerDescription(), &QgsMeshMemoryDataProvider::createProvider );
108 mProviders[ QgsGdalProvider::providerKey() ] =
new QgsGdalProviderMetadata();
109 mProviders[ QgsOgrProvider::providerKey() ] =
new QgsOgrProviderMetadata();
110 #ifdef HAVE_STATIC_PROVIDERS 111 mProviders[ QgsWmsProvider::providerKey() ] =
new QgsWmsProviderMetadata();
115 #ifdef HAVE_STATIC_PROVIDERS 116 QgsDebugMsg( QStringLiteral(
"Forced only static providers" ) );
120 mLibraryDirectory.setSorting( QDir::Name | QDir::IgnoreCase );
121 mLibraryDirectory.setFilter( QDir::Files | QDir::NoSymLinks );
123 #if defined(Q_OS_WIN) || defined(__CYGWIN__) 124 mLibraryDirectory.setNameFilters( QStringList(
"*.dll" ) );
125 #elif defined(ANDROID) 126 mLibraryDirectory.setNameFilters( QStringList(
"*provider.so" ) );
128 mLibraryDirectory.setNameFilters( QStringList( QStringLiteral(
"*.so" ) ) );
131 QgsDebugMsg( QStringLiteral(
"Checking %1 for provider plugins" ).arg( mLibraryDirectory.path() ) );
133 if ( mLibraryDirectory.count() == 0 )
135 QgsDebugMsg( QStringLiteral(
"No dynamic QGIS data provider plugins found in:\n%1\n" ).arg( mLibraryDirectory.path() ) );
139 QString filePattern = getenv(
"QGIS_PROVIDER_FILE" );
141 if ( !filePattern.isEmpty() )
143 fileRegexp.setPattern( filePattern );
146 const auto constEntryInfoList = mLibraryDirectory.entryInfoList();
147 for (
const QFileInfo &fi : constEntryInfoList )
149 if ( !fileRegexp.isEmpty() )
151 if ( fileRegexp.indexIn( fi.fileName() ) == -1 )
153 QgsDebugMsg(
"provider " + fi.fileName() +
" skipped because doesn't match pattern " + filePattern );
158 QLibrary myLib( fi.filePath() );
161 QgsDebugMsg( QStringLiteral(
"Checking %1: ...invalid (lib not loadable): %2" ).arg( myLib.fileName(), myLib.errorString() ) );
165 QFunctionPointer func = myLib.resolve( QStringLiteral(
"providerMetadataFactory" ).toLatin1().data() );
166 factory_function *
function =
reinterpret_cast< factory_function *
>(
cast_to_fptr( func ) );
169 QgsDebugMsg( QStringLiteral(
"Checking %1: ...invalid (no providerMetadataFactory method)" ).arg( myLib.fileName() ) );
176 QgsDebugMsg( QStringLiteral(
"Checking %1: ...invalid (no metadata returned)" ).arg( myLib.fileName() ) );
180 if ( findMetadata_( mProviders, meta->
key() ) )
182 QgsDebugMsg( QStringLiteral(
"Checking %1: ...invalid (key %2 already registered)" ).arg( myLib.fileName() ).arg( meta->
key() ) );
187 mProviders[meta->
key()] = meta;
190 QgsDebugMsg( QStringLiteral(
"Loaded %1 providers (%2) " ).arg( mProviders.size() ).arg(
providerList().join(
';' ) ) );
193 for ( Providers::const_iterator it = mProviders.begin(); it != mProviders.end(); ++it )
195 const QString &key = it->first;
201 if ( !fileVectorFilters.isEmpty() )
204 QgsDebugMsgLevel( QStringLiteral(
"Checking %1: ...loaded OK (%2 file filters)" ).arg( key ).arg( fileVectorFilters.split(
";;" ).count() ), 2 );
209 if ( !fileRasterFilters.isEmpty() )
213 QgsDebugMsgLevel( QStringLiteral(
"Checking %1: ...loaded OK (%2 file filters)" ).arg( key ).arg( fileRasterFilters.split(
";;" ).count() ), 2 );
218 if ( !fileMeshFilters.isEmpty() )
221 QgsDebugMsgLevel( QStringLiteral(
"Checking %1: ...loaded OK (%2 file mesh filters)" ).arg( key ).arg( mMeshFileFilters.split(
";;" ).count() ), 2 );
226 if ( !fileMeshDatasetFilters.isEmpty() )
229 QgsDebugMsgLevel( QStringLiteral(
"Checking %1: ...loaded OK (%2 file dataset filters)" ).arg( key ).arg( mMeshDatasetFileFilters.split(
";;" ).count() ), 2 );
237 mDatabaseDrivers = QgsOgrProviderUtils::databaseDrivers();
240 mDirectoryDrivers = QgsOgrProviderUtils::directoryDrivers();
243 mProtocolDrivers = QgsOgrProviderUtils::protocolDrivers();
250 void QgsProviderRegistry::clean()
253 if ( QgsProject::sProject )
256 Providers::const_iterator it = mProviders.begin();
258 while ( it != mProviders.end() )
261 it->second->cleanupProvider();
268 bool QgsProviderRegistry::exists()
270 return static_cast< bool >( sInstance );
276 if ( sInstance ==
this )
296 Providers::const_iterator it = mProviders.begin();
298 if ( mProviders.empty() )
299 return QObject::tr(
"No data provider plugins are available. No vector layers can be loaded" );
304 list += QLatin1String(
"<ol>" );
306 while ( it != mProviders.end() )
309 list += QLatin1String(
"<li>" );
311 list += it->second->description();
314 list += QLatin1String(
"<br></li>" );
322 list += QLatin1String(
"</ol>" );
329 mLibraryDirectory = path;
336 return mLibraryDirectory;
364 const QList< QgsDataItemProvider * > itemProviders =
dataItemProviders( providerKey );
369 ret = ret | itemProvider->capabilities();
380 return QVariantMap();
388 bool overwrite, QMap<int, int> &oldToNewAttrIdxMap,
389 QString &errorMessage,
390 const QMap<QString, QVariant> *options )
396 return meta->
createEmptyLayer( uri, fields, wkbType, srs, overwrite, oldToNewAttrIdxMap, errorMessage, options );
400 errorMessage = QObject::tr(
"Unable to load %1 provider" ).arg( providerKey );
409 const QStringList &createOptions )
413 return meta->
createRasterDataProvider( uri, format, nBands, type, width, height, geoTransform, crs, createOptions );
424 return QList<QPair<QString, QString> >();
433 return QList<QgsDataItemProvider *>();
436 int QgsProviderRegistry::listStyles(
const QString &providerKey,
const QString &uri, QStringList &ids, QStringList &names, QStringList &descriptions, QString &errCause )
442 res = meta->
listStyles( uri, ids, names, descriptions, errCause );
446 errCause = QObject::tr(
"Unable to load %1 provider" ).arg( providerKey );
461 errCause = QObject::tr(
"Unable to load %1 provider" ).arg( providerKey );
475 errCause = QObject::tr(
"Unable to load %1 provider" ).arg( providerKey );
481 const QString &sldStyle,
const QString &styleName,
const QString &styleDescription,
482 const QString &uiFileContent,
bool useAsDefault, QString &errCause )
487 ret = meta->
saveStyle( uri, qmlStyle, sldStyle, styleName, styleDescription,
488 uiFileContent, useAsDefault, errCause );
491 errCause = QObject::tr(
"Unable to load %1 provider" ).arg( providerKey );
504 errCause = QObject::tr(
"Unable to load %1 provider" ).arg( providerKey );
513 return meta->
createDb( dbPath, errCause );
516 errCause = QStringLiteral(
"Resolving createDb(...) failed" );
533 Q_UNUSED( providerKey );
536 Q_UNUSED( widgetMode );
537 QgsDebugMsg(
"deprecated call - use QgsGui::providerGuiRegistry()->createDataSourceWidget() instead" );
542 QString
const &functionName )
545 QString lib =
library( providerKey );
550 QLibrary myLib( lib );
552 QgsDebugMsg(
"Library name is " + myLib.fileName() );
556 return myLib.resolve( functionName.toLatin1().data() );
560 QgsDebugMsg(
"Cannot load library: " + myLib.errorString() );
568 QString lib =
library( providerKey );
573 std::unique_ptr< QLibrary > myLib(
new QLibrary( lib ) );
575 QgsDebugMsg(
"Library name is " + myLib->fileName() );
578 return myLib.release();
580 QgsDebugMsg(
"Cannot load library: " + myLib->errorString() );
587 QgsDebugMsg(
"deprecated - use QgsGui::providerGuiRegistry() instead." );
592 if ( providerMetadata )
594 if ( mProviders.find( providerMetadata->
key() ) == mProviders.end() )
601 QgsDebugMsgLevel( QStringLiteral(
"Cannot register provider metadata: a provider with the same key (%1) was already registered!" ).arg( providerMetadata->
key() ), 2 );
606 QgsDebugMsgLevel( QStringLiteral(
"Trying to register a null metadata provider!" ), 2 );
613 return mVectorFileFilters;
618 return mRasterFileFilters;
623 return mMeshFileFilters;
628 return mMeshDatasetFileFilters;
633 return mDatabaseDrivers;
638 return mDirectoryDrivers;
643 return mProtocolDrivers;
649 for ( Providers::const_iterator it = mProviders.begin(); it != mProviders.end(); ++it )
651 lst.append( it->first );
658 return findMetadata_( mProviders, providerKey );
bool registerProvider(QgsProviderMetadata *providerMetadata)
register a new vector data provider from its providerMetadata
bool saveStyle(const QString &providerKey, const QString &uri, const QString &qmlStyle, const QString &sldStyle, const QString &styleName, const QString &styleDescription, const QString &uiFileContent, bool useAsDefault, QString &errCause)
Saves a layer style to provider.
WidgetMode
Different ways a source select dialog can be used.
QgsProviderMetadata * providerMetadata(const QString &providerKey) const
Returns metadata of the provider or nullptr if not found.
QList< QgsDataItemProvider *> dataItemProviders(const QString &providerKey) const
Returns list of data item providers of the provider.
void cleanupProviderFunction_t()
virtual QString protocolDrivers() const
Returns a string containing the available protocol drivers.
Q_DECL_DEPRECATED QString library(const QString &providerKey) const
Returns path for the library of the provider.
#define Q_NOWARN_DEPRECATED_PUSH
Q_DECL_DEPRECATED void registerGuis(QWidget *widget)
DataType
Raster data types.
virtual QString databaseDrivers() const
Returns a string containing the available database drivers.
Container of fields for a vector layer.
QVariantMap decodeUri(const QString &providerKey, const QString &uri)
Breaks a provider data source URI into its component paths (e.g.
Abstract base class for spatial data provider implementations.
QString getStyleById(const QString &providerKey, const QString &uri, QString styleId, QString &errCause)
Gets a layer style defined by styleId.
virtual ~QgsProviderRegistry()
const QgsCoordinateReferenceSystem & crs
Could not find a matching provider key.
QgsVectorLayerExporter::ExportError createEmptyLayer(const QString &providerKey, const QString &uri, const QgsFields &fields, QgsWkbTypes::Type wkbType, const QgsCoordinateReferenceSystem &srs, bool overwrite, QMap< int, int > &oldToNewAttrIdxMap, QString &errorMessage, const QMap< QString, QVariant > *options)
Creates new empty vector layer.
virtual QString fileVectorFilters() const
Returns vector file filter string.
QgsDataProvider * createProvider(const QString &providerKey, const QString &dataSource, const QgsDataProvider::ProviderOptions &options=QgsDataProvider::ProviderOptions())
Creates a new instance of a provider.
Q_DECL_DEPRECATED QWidget * createSelectionWidget(const QString &providerKey, QWidget *parent=nullptr, Qt::WindowFlags fl=Qt::WindowFlags(), QgsProviderRegistry::WidgetMode widgetMode=QgsProviderRegistry::WidgetMode::None)
Returns a new widget for selecting layers from a provider.
Type
The WKB type describes the number of dimensions a geometry has.
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
#define QgsDebugMsgLevel(str, level)
Q_DECL_DEPRECATED QLibrary * createProviderLibrary(const QString &providerKey) const
Returns a new QLibrary for the specified providerKey.
QgsTransaction * createTransaction(const QString &providerKey, const QString &connString)
Returns new instance of transaction.
QList< QPair< QString, QString > > pyramidResamplingMethods(const QString &providerKey)
Returns list of raster pyramid resampling methods.
int listStyles(const QString &providerKey, const QString &uri, QStringList &ids, QStringList &names, QStringList &descriptions, QString &errCause)
Lists stored layer styles in the provider defined by providerKey and uri.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
void setLibraryDirectory(const QDir &path)
Sets library directory where to search for plugins.
QString loadStyle(const QString &providerKey, const QString &uri, QString &errCause)
Loads a layer style defined by uri.
QString pluginList(bool asHtml=false) const
Returns list of provider plugins found.
void removeAllMapLayers()
Removes all registered layers.
virtual QgsRasterDataProvider * createRasterDataProvider(const QString &providerKey, const QString &uri, const QString &format, int nBands, Qgis::DataType type, int width, int height, double *geoTransform, const QgsCoordinateReferenceSystem &crs, const QStringList &createOptions=QStringList())
Creates new instance of raster data provider.
virtual QString fileMeshDatasetFilters() const
Returns mesh's dataset file filter string.
A registry / canonical manager of data providers.
Q_DECL_DEPRECATED int providerCapabilities(const QString &providerKey) const
Returns the provider capabilities.
#define Q_NOWARN_DEPRECATED_POP
Setting options for creating vector data providers.
virtual QString fileMeshFilters() const
Returns mesh file filter string.
bool deleteStyleById(const QString &providerKey, const QString &uri, QString styleId, QString &errCause)
Deletes a layer style defined by styleId.
virtual QString directoryDrivers() const
Returns a string containing the available directory drivers.
This class allows including a set of layers in a database-side transaction, provided the layer data p...
std::map< QString, QgsProviderMetadata * > Providers
Type for data provider metadata associative container.
static QgsProject * instance()
Returns the QgsProject singleton instance.
bool createDb(const QString &providerKey, const QString &dbPath, QString &errCause)
Creates database by the provider on the path.
This class represents a coordinate reference system (CRS).
Q_DECL_DEPRECATED QFunctionPointer function(const QString &providerKey, const QString &functionName)
Gets pointer to provider function.
virtual QString fileRasterFilters() const
Returns raster file filter string.
QStringList providerList() const
Returns list of available providers by their keys.
QDir libraryDirectory() const
Returns the library directory where plugins are found.
This is the interface for those who want to add custom data items to the browser tree.
Base class for raster data providers.