56 QMutexLocker locker( &sMutex );
67 QgsProviderRegistry::QgsProviderRegistry(
const QString &pluginPath )
75 char **argv = qApp->argv();
76 QString appDir = argv[0];
77 int bin = appDir.findRev(
"/bin", -1,
false );
78 QString baseDir = appDir.left( bin );
79 QString mLibraryDirectory = baseDir +
"/lib";
81 mLibraryDirectory = pluginPath;
86 void QgsProviderRegistry::init()
89 mProviders[ QgsMemoryProvider::providerKey() ] =
new QgsProviderMetadata( QgsMemoryProvider::providerKey(), QgsMemoryProvider::providerDescription(), &QgsMemoryProvider::createProvider );
90 mProviders[ QgsMeshMemoryDataProvider::providerKey() ] =
new QgsProviderMetadata( QgsMeshMemoryDataProvider::providerKey(), QgsMeshMemoryDataProvider::providerDescription(), &QgsMeshMemoryDataProvider::createProvider );
92 mLibraryDirectory.setSorting( QDir::Name | QDir::IgnoreCase );
93 mLibraryDirectory.setFilter( QDir::Files | QDir::NoSymLinks );
95 #if defined(Q_OS_WIN) || defined(__CYGWIN__) 96 mLibraryDirectory.setNameFilters( QStringList(
"*.dll" ) );
97 #elif defined(ANDROID) 98 mLibraryDirectory.setNameFilters( QStringList(
"*provider.so" ) );
100 mLibraryDirectory.setNameFilters( QStringList( QStringLiteral(
"*.so" ) ) );
103 QgsDebugMsg( QString(
"Checking %1 for provider plugins" ).arg( mLibraryDirectory.path() ) );
105 if ( mLibraryDirectory.count() == 0 )
107 QString msg = QObject::tr(
"No QGIS data provider plugins found in:\n%1\n" ).arg( mLibraryDirectory.path() );
108 msg += QObject::tr(
"No vector layers can be loaded. Check your QGIS installation" );
111 output->
setTitle( QObject::tr(
"No Data Providers" ) );
118 QString filePattern = getenv(
"QGIS_PROVIDER_FILE" );
120 if ( !filePattern.isEmpty() )
122 fileRegexp.setPattern( filePattern );
125 Q_FOREACH (
const QFileInfo &fi, mLibraryDirectory.entryInfoList() )
127 if ( !fileRegexp.isEmpty() )
129 if ( fileRegexp.indexIn( fi.fileName() ) == -1 )
131 QgsDebugMsg(
"provider " + fi.fileName() +
" skipped because doesn't match pattern " + filePattern );
136 QLibrary myLib( fi.filePath() );
139 QgsDebugMsg( QString(
"Checking %1: ...invalid (lib not loadable): %2" ).arg( myLib.fileName(), myLib.errorString() ) );
148 QgsDebugMsg( QString(
"Checking %1: ...invalid (has type method)" ).arg( myLib.fileName() ) );
156 QgsDebugMsg( QString(
"Checking %1: ...invalid (no isProvider method)" ).arg( myLib.fileName() ) );
163 QgsDebugMsg( QString(
"Checking %1: ...invalid (not a provider)" ).arg( myLib.fileName() ) );
171 QgsDebugMsg( QString(
"Checking %1: ...invalid (no description method)" ).arg( myLib.fileName() ) );
178 QgsDebugMsg( QString(
"Checking %1: ...invalid (no providerKey method)" ).arg( myLib.fileName() ) );
187 if ( pDatabaseDrivers )
189 mDatabaseDrivers = pDatabaseDrivers();
194 if ( pDirectoryDrivers )
196 mDirectoryDrivers = pDirectoryDrivers();
201 if ( pProtocolDrivers )
203 mProtocolDrivers = pProtocolDrivers();
208 if ( pFileVectorFilters )
212 if ( !fileVectorFilters.isEmpty() )
213 mVectorFileFilters += fileVectorFilters;
215 QgsDebugMsg( QString(
"Checking %1: ...loaded OK (%2 file filters)" ).arg( myLib.fileName() ).arg( fileVectorFilters.split(
";;" ).count() ) );
225 pBuild( fileRasterFilters );
227 QgsDebugMsg(
"raster filters: " + fileRasterFilters );
228 if ( !fileRasterFilters.isEmpty() )
229 mRasterFileFilters += fileRasterFilters;
231 QgsDebugMsg( QString(
"Checking %1: ...loaded OK (%2 file filters)" ).arg( myLib.fileName() ).arg( fileRasterFilters.split(
";;" ).count() ) );
245 void QgsProviderRegistry::clean()
249 Providers::const_iterator it = mProviders.begin();
251 while ( it != mProviders.end() )
254 QString lib = it->second->library();
255 if ( !lib.isEmpty() )
257 QLibrary myLib( lib );
258 if ( myLib.isLoaded() )
274 if ( sInstance ==
this )
289 QString
const &providerKey )
291 QgsProviderRegistry::Providers::const_iterator i =
292 metaData.find( providerKey );
294 if ( i != metaData.end() )
319 Providers::const_iterator it = mProviders.begin();
321 if ( mProviders.empty() )
322 return QObject::tr(
"No data provider plugins are available. No vector layers can be loaded" );
327 list += QLatin1String(
"<ol>" );
329 while ( it != mProviders.end() )
332 list += QLatin1String(
"<li>" );
334 list += it->second->description();
337 list += QLatin1String(
"<br></li>" );
345 list += QLatin1String(
"</ol>" );
352 mLibraryDirectory = path;
359 return mLibraryDirectory;
393 QString lib =
library( providerKey );
395 #ifdef TESTPROVIDERLIB 396 const char *cLib = lib.toUtf8();
400 void *handle = dlopen( cOgrLib, RTLD_LAZY | RTLD_GLOBAL );
413 QLibrary myLib( lib );
415 QgsDebugMsg(
"Library name is " + myLib.fileName() );
425 QgsDebugMsg( QString(
"Failed to load %1: no classFactory method" ).arg( lib ) );
437 QgsDebugMsg( QString(
"Instantiated the data provider plugin: %1" ).arg( dataProvider->
name() ) );
450 if ( !dataCapabilities )
455 return dataCapabilities();
467 if ( !selectFactory )
470 return selectFactory( parent, fl, widgetMode );
474 QString
const &functionName )
476 QString lib =
library( providerKey );
480 QLibrary myLib(
library( providerKey ) );
482 QgsDebugMsg(
"Library name is " + myLib.fileName() );
486 return myLib.resolve( functionName.toLatin1().data() );
490 QgsDebugMsg(
"Cannot load library: " + myLib.errorString() );
497 QString lib =
library( providerKey );
501 std::unique_ptr< QLibrary > myLib(
new QLibrary( lib ) );
503 QgsDebugMsg(
"Library name is " + myLib->fileName() );
506 return myLib.release();
508 QgsDebugMsg(
"Cannot load library: " + myLib->errorString() );
515 typedef void registerGui_function( QWidget * parent );
519 registerGui_function *registerGui =
reinterpret_cast< registerGui_function *
>(
cast_to_fptr(
function( provider,
"registerGui" ) ) );
524 registerGui( parent );
530 if ( providerMetadata )
532 if ( mProviders.find( providerMetadata->
key() ) == mProviders.end() )
539 QgsDebugMsgLevel( QStringLiteral(
"Cannot register provider metadata: a provider with the same key (%1) was already registered!" ).arg( providerMetadata->
key() ), 2 );
544 QgsDebugMsgLevel( QStringLiteral(
"Trying to register a null metadata provider!" ), 2 );
551 return mVectorFileFilters;
556 return mRasterFileFilters;
561 return mDatabaseDrivers;
566 return mDirectoryDrivers;
571 return mProtocolDrivers;
577 for ( Providers::const_iterator it = mProviders.begin(); it != mProviders.end(); ++it )
579 lst.append( it->first );
586 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.
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)
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)
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.
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 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.