58 QMutexLocker locker( &sMutex );
69 QgsProviderRegistry::QgsProviderRegistry(
const QString &pluginPath )
77 char **argv = qApp->argv();
78 QString appDir = argv[0];
79 int bin = appDir.findRev(
"/bin", -1,
false );
80 QString baseDir = appDir.left( bin );
81 QString mLibraryDirectory = baseDir +
"/lib";
83 mLibraryDirectory = pluginPath;
88 void QgsProviderRegistry::init()
91 mProviders[ QgsMemoryProvider::providerKey() ] =
new QgsProviderMetadata( QgsMemoryProvider::providerKey(), QgsMemoryProvider::providerDescription(), &QgsMemoryProvider::createProvider );
92 mProviders[ QgsMeshMemoryDataProvider::providerKey() ] =
new QgsProviderMetadata( QgsMeshMemoryDataProvider::providerKey(), QgsMeshMemoryDataProvider::providerDescription(), &QgsMeshMemoryDataProvider::createProvider );
94 mLibraryDirectory.setSorting( QDir::Name | QDir::IgnoreCase );
95 mLibraryDirectory.setFilter( QDir::Files | QDir::NoSymLinks );
97 #if defined(Q_OS_WIN) || defined(__CYGWIN__) 98 mLibraryDirectory.setNameFilters( QStringList(
"*.dll" ) );
99 #elif defined(ANDROID) 100 mLibraryDirectory.setNameFilters( QStringList(
"*provider.so" ) );
102 mLibraryDirectory.setNameFilters( QStringList( QStringLiteral(
"*.so" ) ) );
105 QgsDebugMsg( QStringLiteral(
"Checking %1 for provider plugins" ).arg( mLibraryDirectory.path() ) );
107 if ( mLibraryDirectory.count() == 0 )
109 QString msg = QObject::tr(
"No QGIS data provider plugins found in:\n%1\n" ).arg( mLibraryDirectory.path() );
110 msg += QObject::tr(
"No vector layers can be loaded. Check your QGIS installation" );
113 output->
setTitle( QObject::tr(
"No Data Providers" ) );
120 QString filePattern = getenv(
"QGIS_PROVIDER_FILE" );
122 if ( !filePattern.isEmpty() )
124 fileRegexp.setPattern( filePattern );
127 Q_FOREACH (
const QFileInfo &fi, mLibraryDirectory.entryInfoList() )
129 if ( !fileRegexp.isEmpty() )
131 if ( fileRegexp.indexIn( fi.fileName() ) == -1 )
133 QgsDebugMsg(
"provider " + fi.fileName() +
" skipped because doesn't match pattern " + filePattern );
138 QLibrary myLib( fi.filePath() );
141 QgsDebugMsg( QStringLiteral(
"Checking %1: ...invalid (lib not loadable): %2" ).arg( myLib.fileName(), myLib.errorString() ) );
150 QgsDebugMsg( QStringLiteral(
"Checking %1: ...invalid (has type method)" ).arg( myLib.fileName() ) );
158 QgsDebugMsg( QStringLiteral(
"Checking %1: ...invalid (no isProvider method)" ).arg( myLib.fileName() ) );
165 QgsDebugMsg( QStringLiteral(
"Checking %1: ...invalid (not a provider)" ).arg( myLib.fileName() ) );
173 QgsDebugMsg( QStringLiteral(
"Checking %1: ...invalid (no description method)" ).arg( myLib.fileName() ) );
180 QgsDebugMsg( QStringLiteral(
"Checking %1: ...invalid (no providerKey method)" ).arg( myLib.fileName() ) );
189 if ( pDatabaseDrivers )
191 mDatabaseDrivers = pDatabaseDrivers();
196 if ( pDirectoryDrivers )
198 mDirectoryDrivers = pDirectoryDrivers();
203 if ( pProtocolDrivers )
205 mProtocolDrivers = pProtocolDrivers();
210 if ( pFileVectorFilters )
214 if ( !fileVectorFilters.isEmpty() )
215 mVectorFileFilters += fileVectorFilters;
217 QgsDebugMsg( QStringLiteral(
"Checking %1: ...loaded OK (%2 file filters)" ).arg( myLib.fileName() ).arg( fileVectorFilters.split(
";;" ).count() ) );
227 pBuild( fileRasterFilters );
229 QgsDebugMsg(
"raster filters: " + fileRasterFilters );
230 if ( !fileRasterFilters.isEmpty() )
231 mRasterFileFilters += fileRasterFilters;
233 QgsDebugMsg( QStringLiteral(
"Checking %1: ...loaded OK (%2 file filters)" ).arg( myLib.fileName() ).arg( fileRasterFilters.split(
";;" ).count() ) );
247 void QgsProviderRegistry::clean()
250 if ( QgsProject::sProject )
253 Providers::const_iterator it = mProviders.begin();
255 while ( it != mProviders.end() )
258 QString lib = it->second->library();
259 if ( !lib.isEmpty() )
261 QLibrary myLib( lib );
262 if ( myLib.isLoaded() )
278 if ( sInstance ==
this )
293 QString
const &providerKey )
295 QgsProviderRegistry::Providers::const_iterator i =
296 metaData.find( providerKey );
298 if ( i != metaData.end() )
323 Providers::const_iterator it = mProviders.begin();
325 if ( mProviders.empty() )
326 return QObject::tr(
"No data provider plugins are available. No vector layers can be loaded" );
331 list += QLatin1String(
"<ol>" );
333 while ( it != mProviders.end() )
336 list += QLatin1String(
"<li>" );
338 list += it->second->description();
341 list += QLatin1String(
"<br></li>" );
349 list += QLatin1String(
"</ol>" );
356 mLibraryDirectory = path;
363 return mLibraryDirectory;
397 QString lib =
library( providerKey );
399 #ifdef TESTPROVIDERLIB 400 const char *cLib = lib.toUtf8();
404 void *handle = dlopen( cOgrLib, RTLD_LAZY | RTLD_GLOBAL );
411 QgsDebugMsg( QStringLiteral(
"dlopen succeeded" ) );
417 QLibrary myLib( lib );
419 QgsDebugMsg(
"Library name is " + myLib.fileName() );
429 QgsDebugMsg( QStringLiteral(
"Failed to load %1: no classFactory method" ).arg( lib ) );
441 QgsDebugMsg( QStringLiteral(
"Instantiated the data provider plugin: %1" ).arg( dataProvider->
name() ) );
450 return QVariantMap();
456 return QVariantMap();
470 if ( !dataCapabilities )
475 return dataCapabilities();
487 if ( !selectFactory )
490 return selectFactory( parent, fl, widgetMode );
494 QString
const &functionName )
496 QString lib =
library( providerKey );
500 QLibrary myLib(
library( providerKey ) );
502 QgsDebugMsg(
"Library name is " + myLib.fileName() );
506 return myLib.resolve( functionName.toLatin1().data() );
510 QgsDebugMsg(
"Cannot load library: " + myLib.errorString() );
517 QString lib =
library( providerKey );
521 std::unique_ptr< QLibrary > myLib(
new QLibrary( lib ) );
523 QgsDebugMsg(
"Library name is " + myLib->fileName() );
526 return myLib.release();
528 QgsDebugMsg(
"Cannot load library: " + myLib->errorString() );
535 typedef void registerGui_function( QWidget * parent );
539 registerGui_function *registerGui =
reinterpret_cast< registerGui_function *
>(
cast_to_fptr(
function( provider,
"registerGui" ) ) );
544 registerGui( parent );
550 if ( providerMetadata )
552 if ( mProviders.find( providerMetadata->
key() ) == mProviders.end() )
559 QgsDebugMsgLevel( QStringLiteral(
"Cannot register provider metadata: a provider with the same key (%1) was already registered!" ).arg( providerMetadata->
key() ), 2 );
564 QgsDebugMsgLevel( QStringLiteral(
"Trying to register a null metadata provider!" ), 2 );
571 return mVectorFileFilters;
576 return mRasterFileFilters;
581 return mDatabaseDrivers;
586 return mDirectoryDrivers;
591 return mProtocolDrivers;
597 for ( Providers::const_iterator it = mProviders.begin(); it != mProviders.end(); ++it )
599 lst.append( it->first );
606 return findMetadata_( mProviders, providerKey );
bool registerProvider(QgsProviderMetadata *providerMetadata)
register a new vector data provider from its providerMetadata
QDir libraryDirectory() const
Returns the library directory where plugins are found.
WidgetMode
Different ways a source select dialog can be used (embedded is for the data source manager dialog) ...
QString databaseDrivers_t()
virtual QString databaseDrivers() const
Returns a string containing the available database drivers.
void cleanupProviderFunction_t()
virtual void setTitle(const QString &title)=0
Sets title for the messages.
QVariantMap decodeUri_t(const QString &uri)
QString library(const QString &providerKey) const
Returns path for the library of the provider.
static void warning(const QString &msg)
Goes to qWarning.
const QgsProviderMetadata * providerMetadata(const QString &providerKey) const
Returns metadata of the provider or NULL if not found.
void registerGuis(QWidget *widget)
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)
Returns the components (e.g.
Abstract base class for spatial data provider implementations.
virtual ~QgsProviderRegistry()
virtual QString name() const =0
Returns a provider name.
QgsDataProvider * createProvider(const QString &providerKey, const QString &dataSource, const QgsDataProvider::ProviderOptions &options=QgsDataProvider::ProviderOptions())
Creates a new instance of a provider.
QStringList providerList() const
Returns list of available providers by their keys.
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)
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 QString protocolDrivers() const
Returns a string containing the available protocol drivers.
int providerCapabilities(const QString &providerKey) const
Returns the provider capabilities.
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.
void removeAllMapLayers()
Removes all registered layers.
QLibrary * createProviderLibrary(const QString &providerKey) const
Returns a new QLibrary for the specified providerKey.
virtual QString directoryDrivers() const
Returns a string containing the available directory drivers.
virtual QString fileRasterFilters() const
Returns raster file filter string.
QString fileVectorFilters_t()
QString pluginList(bool asHtml=false) const
Returns list of provider plugins found.
A registry / canonical manager of data providers.
virtual void showMessage(bool blocking=true)=0
display the message to the user and deletes itself
virtual QString fileVectorFilters() const
Returns vector file filter string.
Setting options for creating vector data providers.
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.
void buildsupportedrasterfilefilter_t(QString &fileFiltersString)
QString protocolDrivers_t()
Interface for showing messages from QGIS in GUI independent way.