41 typedef void fileMeshFilters_t( QString &fileMeshFiltersString, QString &fileMeshDatasetFiltersString );
59 QMutexLocker locker( &sMutex );
70 QgsProviderRegistry::QgsProviderRegistry(
const QString &pluginPath )
78 char **argv = qApp->argv();
79 QString appDir = argv[0];
80 int bin = appDir.findRev(
"/bin", -1,
false );
81 QString baseDir = appDir.left( bin );
82 QString mLibraryDirectory = baseDir +
"/lib";
84 mLibraryDirectory = pluginPath;
89 void QgsProviderRegistry::init()
92 mProviders[ QgsMemoryProvider::providerKey() ] =
new QgsProviderMetadata( QgsMemoryProvider::providerKey(), QgsMemoryProvider::providerDescription(), &QgsMemoryProvider::createProvider );
93 mProviders[ QgsMeshMemoryDataProvider::providerKey() ] =
new QgsProviderMetadata( QgsMeshMemoryDataProvider::providerKey(), QgsMeshMemoryDataProvider::providerDescription(), &QgsMeshMemoryDataProvider::createProvider );
95 mLibraryDirectory.setSorting( QDir::Name | QDir::IgnoreCase );
96 mLibraryDirectory.setFilter( QDir::Files | QDir::NoSymLinks );
98 #if defined(Q_OS_WIN) || defined(__CYGWIN__) 99 mLibraryDirectory.setNameFilters( QStringList(
"*.dll" ) );
100 #elif defined(ANDROID) 101 mLibraryDirectory.setNameFilters( QStringList(
"*provider.so" ) );
103 mLibraryDirectory.setNameFilters( QStringList( QStringLiteral(
"*.so" ) ) );
106 QgsDebugMsg( QStringLiteral(
"Checking %1 for provider plugins" ).arg( mLibraryDirectory.path() ) );
108 if ( mLibraryDirectory.count() == 0 )
110 QString msg = QObject::tr(
"No QGIS data provider plugins found in:\n%1\n" ).arg( mLibraryDirectory.path() );
111 msg += QObject::tr(
"No vector layers can be loaded. Check your QGIS installation" );
114 output->
setTitle( QObject::tr(
"No Data Providers" ) );
121 QString filePattern = getenv(
"QGIS_PROVIDER_FILE" );
123 if ( !filePattern.isEmpty() )
125 fileRegexp.setPattern( filePattern );
128 Q_FOREACH (
const QFileInfo &fi, mLibraryDirectory.entryInfoList() )
130 if ( !fileRegexp.isEmpty() )
132 if ( fileRegexp.indexIn( fi.fileName() ) == -1 )
134 QgsDebugMsg(
"provider " + fi.fileName() +
" skipped because doesn't match pattern " + filePattern );
139 QLibrary myLib( fi.filePath() );
142 QgsDebugMsg( QStringLiteral(
"Checking %1: ...invalid (lib not loadable): %2" ).arg( myLib.fileName(), myLib.errorString() ) );
151 QgsDebugMsg( QStringLiteral(
"Checking %1: ...invalid (has type method)" ).arg( myLib.fileName() ) );
159 QgsDebugMsg( QStringLiteral(
"Checking %1: ...invalid (no isProvider method)" ).arg( myLib.fileName() ) );
166 QgsDebugMsg( QStringLiteral(
"Checking %1: ...invalid (not a provider)" ).arg( myLib.fileName() ) );
174 QgsDebugMsg( QStringLiteral(
"Checking %1: ...invalid (no description method)" ).arg( myLib.fileName() ) );
181 QgsDebugMsg( QStringLiteral(
"Checking %1: ...invalid (no providerKey method)" ).arg( myLib.fileName() ) );
190 if ( pDatabaseDrivers )
192 mDatabaseDrivers = pDatabaseDrivers();
197 if ( pDirectoryDrivers )
199 mDirectoryDrivers = pDirectoryDrivers();
204 if ( pProtocolDrivers )
206 mProtocolDrivers = pProtocolDrivers();
211 if ( pFileVectorFilters )
215 if ( !fileVectorFilters.isEmpty() )
216 mVectorFileFilters += fileVectorFilters;
218 QgsDebugMsg( QStringLiteral(
"Checking %1: ...loaded OK (%2 file filters)" ).arg( myLib.fileName() ).arg( fileVectorFilters.split(
";;" ).count() ) );
228 pBuild( fileRasterFilters );
230 QgsDebugMsg(
"raster filters: " + fileRasterFilters );
231 if ( !fileRasterFilters.isEmpty() )
232 mRasterFileFilters += fileRasterFilters;
234 QgsDebugMsg( QStringLiteral(
"Checking %1: ...loaded OK (%2 file filters)" ).arg( myLib.fileName() ).arg( fileRasterFilters.split(
";;" ).count() ) );
239 if ( pFileMeshFilters )
244 pFileMeshFilters( fileMeshFilters, fileMeshDatasetFilters );
246 if ( !fileMeshFilters.isEmpty() )
247 mMeshFileFilters += fileMeshFilters;
248 if ( !fileMeshDatasetFilters.isEmpty() )
249 mMeshDatasetFileFilters += fileMeshDatasetFilters;
251 QgsDebugMsg( QStringLiteral(
"Checking %1: ...loaded OK (%2 file mesh filters, %3 file dataset filters)" ).arg( myLib.fileName() ).arg( mMeshFileFilters.split(
";;" ).count() ).arg( mMeshDatasetFileFilters.split(
";;" ).count() ) );
265 void QgsProviderRegistry::clean()
269 Providers::const_iterator it = mProviders.begin();
271 while ( it != mProviders.end() )
274 QString lib = it->second->library();
275 if ( !lib.isEmpty() )
277 QLibrary myLib( lib );
278 if ( myLib.isLoaded() )
294 if ( sInstance ==
this )
309 QString
const &providerKey )
311 QgsProviderRegistry::Providers::const_iterator i =
312 metaData.find( providerKey );
314 if ( i != metaData.end() )
339 Providers::const_iterator it = mProviders.begin();
341 if ( mProviders.empty() )
342 return QObject::tr(
"No data provider plugins are available. No vector layers can be loaded" );
347 list += QLatin1String(
"<ol>" );
349 while ( it != mProviders.end() )
352 list += QLatin1String(
"<li>" );
354 list += it->second->description();
357 list += QLatin1String(
"<br></li>" );
365 list += QLatin1String(
"</ol>" );
372 mLibraryDirectory = path;
379 return mLibraryDirectory;
413 QString lib =
library( providerKey );
415 #ifdef TESTPROVIDERLIB 416 const char *cLib = lib.toUtf8();
420 void *handle = dlopen( cOgrLib, RTLD_LAZY | RTLD_GLOBAL );
427 QgsDebugMsg( QStringLiteral(
"dlopen succeeded" ) );
433 QLibrary myLib( lib );
435 QgsDebugMsg(
"Library name is " + myLib.fileName() );
445 QgsDebugMsg( QStringLiteral(
"Failed to load %1: no classFactory method" ).arg( lib ) );
457 QgsDebugMsg( QStringLiteral(
"Instantiated the data provider plugin: %1" ).arg( dataProvider->
name() ) );
466 return QVariantMap();
472 return QVariantMap();
486 if ( !dataCapabilities )
491 return dataCapabilities();
503 if ( !selectFactory )
506 return selectFactory( parent, fl, widgetMode );
510 QString
const &functionName )
512 QString lib =
library( providerKey );
516 QLibrary myLib(
library( providerKey ) );
518 QgsDebugMsg(
"Library name is " + myLib.fileName() );
522 return myLib.resolve( functionName.toLatin1().data() );
526 QgsDebugMsg(
"Cannot load library: " + myLib.errorString() );
533 QString lib =
library( providerKey );
537 std::unique_ptr< QLibrary > myLib(
new QLibrary( lib ) );
539 QgsDebugMsg(
"Library name is " + myLib->fileName() );
542 return myLib.release();
544 QgsDebugMsg(
"Cannot load library: " + myLib->errorString() );
551 typedef void registerGui_function( QWidget * parent );
555 registerGui_function *registerGui =
reinterpret_cast< registerGui_function *
>(
cast_to_fptr(
function( provider,
"registerGui" ) ) );
560 registerGui( parent );
566 if ( providerMetadata )
568 if ( mProviders.find( providerMetadata->
key() ) == mProviders.end() )
575 QgsDebugMsgLevel( QStringLiteral(
"Cannot register provider metadata: a provider with the same key (%1) was already registered!" ).arg( providerMetadata->
key() ), 2 );
580 QgsDebugMsgLevel( QStringLiteral(
"Trying to register a null metadata provider!" ), 2 );
587 return mVectorFileFilters;
592 return mRasterFileFilters;
597 return mMeshFileFilters;
602 return mMeshDatasetFileFilters;
607 return mDatabaseDrivers;
612 return mDirectoryDrivers;
617 return mProtocolDrivers;
623 for ( Providers::const_iterator it = mProviders.begin(); it != mProviders.end(); ++it )
625 lst.append( it->first );
632 return findMetadata_( mProviders, providerKey );
bool registerProvider(QgsProviderMetadata *providerMetadata)
register a new vector data provider from its providerMetadata
WidgetMode
Different ways a source select dialog can be used (embedded is for the data source manager dialog) ...
QString databaseDrivers_t()
void cleanupProviderFunction_t()
virtual void setTitle(const QString &title)=0
Sets title for the messages.
QVariantMap decodeUri_t(const QString &uri)
static void warning(const QString &msg)
Goes to qWarning.
virtual QString protocolDrivers() const
Returns a string containing the available protocol drivers.
QString library(const QString &providerKey) const
Returns path for the library of the provider.
void registerGuis(QWidget *widget)
void fileMeshFilters_t(QString &fileMeshFiltersString, QString &fileMeshDatasetFiltersString)
virtual QString databaseDrivers() const
Returns a string containing the available database drivers.
QString directoryDrivers_t()
static QgsMessageOutput * createMessageOutput()
function that returns new class derived from QgsMessageOutput (don't forget to delete it then if show...
QgsDataProvider * classFactoryFunction_t(const QString *, const QgsDataProvider::ProviderOptions &options)
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.
virtual ~QgsProviderRegistry()
virtual QString name() const =0
Returns a provider name.
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.
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.
void initProviderFunction_t()
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
#define QgsDebugMsgLevel(str, level)
QLibrary * createProviderLibrary(const QString &providerKey) const
Returns a new QLibrary for the specified providerKey.
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).
virtual void setMessage(const QString &message, MessageType msgType)=0
Sets message, it won't be displayed until.
void setLibraryDirectory(const QDir &path)
Sets library directory where to search for plugins.
const QgsProviderMetadata * providerMetadata(const QString &providerKey) const
Returns metadata of the provider or NULL if not found.
QString pluginList(bool asHtml=false) const
Returns list of provider plugins found.
void removeAllMapLayers()
Removes all registered layers.
virtual QString fileMeshDatasetFilters() const
Returns mesh's dataset file filter string.
QString fileVectorFilters_t()
A registry / canonical manager of data providers.
int providerCapabilities(const QString &providerKey) const
Returns the provider capabilities.
virtual void showMessage(bool blocking=true)=0
display the message to the user and deletes itself
Setting options for creating vector data providers.
virtual QString fileMeshFilters() const
Returns mesh file filter string.
virtual QString directoryDrivers() const
Returns a string containing the available directory drivers.
std::map< QString, QgsProviderMetadata * > Providers
Open the given vector data source.
static QgsProject * instance()
Returns the QgsProject singleton instance.
QWidget * selectFactoryFunction_t(QWidget *parent, Qt::WindowFlags fl, QgsProviderRegistry::WidgetMode widgetMode)
QFunctionPointer function(const QString &providerKey, const QString &functionName)
Gets pointer to provider function.
virtual QString fileRasterFilters() const
Returns raster file filter string.
void buildsupportedrasterfilefilter_t(QString &fileFiltersString)
QString protocolDrivers_t()
QStringList providerList() const
Returns list of available providers by their keys.
Interface for showing messages from QGIS in GUI independent way.
QDir libraryDirectory() const
Returns the library directory where plugins are found.