55 QMutexLocker locker( &sMutex );
66 QgsProviderRegistry::QgsProviderRegistry(
const QString &pluginPath )
74 char **argv = qApp->argv();
75 QString appDir = argv[0];
76 int bin = appDir.findRev(
"/bin", -1,
false );
77 QString baseDir = appDir.left( bin );
78 QString mLibraryDirectory = baseDir +
"/lib";
80 mLibraryDirectory = pluginPath;
85 void QgsProviderRegistry::init()
88 mProviders[ QgsMemoryProvider::providerKey() ] =
new QgsProviderMetadata( QgsMemoryProvider::providerKey(), QgsMemoryProvider::providerDescription(), &QgsMemoryProvider::createProvider );
90 mLibraryDirectory.setSorting( QDir::Name | QDir::IgnoreCase );
91 mLibraryDirectory.setFilter( QDir::Files | QDir::NoSymLinks );
93 #if defined(Q_OS_WIN) || defined(__CYGWIN__) 94 mLibraryDirectory.setNameFilters( QStringList(
"*.dll" ) );
95 #elif defined(ANDROID) 96 mLibraryDirectory.setNameFilters( QStringList(
"*provider.so" ) );
98 mLibraryDirectory.setNameFilters( QStringList( QStringLiteral(
"*.so" ) ) );
101 QgsDebugMsg( QString(
"Checking %1 for provider plugins" ).arg( mLibraryDirectory.path() ) );
103 if ( mLibraryDirectory.count() == 0 )
105 QString msg = QObject::tr(
"No QGIS data provider plugins found in:\n%1\n" ).arg( mLibraryDirectory.path() );
106 msg += QObject::tr(
"No vector layers can be loaded. Check your QGIS installation" );
109 output->
setTitle( QObject::tr(
"No Data Providers" ) );
116 QString filePattern = getenv(
"QGIS_PROVIDER_FILE" );
118 if ( !filePattern.isEmpty() )
120 fileRegexp.setPattern( filePattern );
123 Q_FOREACH (
const QFileInfo &fi, mLibraryDirectory.entryInfoList() )
125 if ( !fileRegexp.isEmpty() )
127 if ( fileRegexp.indexIn( fi.fileName() ) == -1 )
129 QgsDebugMsg(
"provider " + fi.fileName() +
" skipped because doesn't match pattern " + filePattern );
134 QLibrary myLib( fi.filePath() );
137 QgsDebugMsg( QString(
"Checking %1: ...invalid (lib not loadable): %2" ).arg( myLib.fileName(), myLib.errorString() ) );
146 QgsDebugMsg( QString(
"Checking %1: ...invalid (has type method)" ).arg( myLib.fileName() ) );
154 QgsDebugMsg( QString(
"Checking %1: ...invalid (no isProvider method)" ).arg( myLib.fileName() ) );
161 QgsDebugMsg( QString(
"Checking %1: ...invalid (not a provider)" ).arg( myLib.fileName() ) );
169 QgsDebugMsg( QString(
"Checking %1: ...invalid (no description method)" ).arg( myLib.fileName() ) );
176 QgsDebugMsg( QString(
"Checking %1: ...invalid (no providerKey method)" ).arg( myLib.fileName() ) );
185 if ( pDatabaseDrivers )
187 mDatabaseDrivers = pDatabaseDrivers();
192 if ( pDirectoryDrivers )
194 mDirectoryDrivers = pDirectoryDrivers();
199 if ( pProtocolDrivers )
201 mProtocolDrivers = pProtocolDrivers();
206 if ( pFileVectorFilters )
210 if ( !fileVectorFilters.isEmpty() )
211 mVectorFileFilters += fileVectorFilters;
213 QgsDebugMsg( QString(
"Checking %1: ...loaded OK (%2 file filters)" ).arg( myLib.fileName() ).arg( fileVectorFilters.split(
";;" ).count() ) );
223 pBuild( fileRasterFilters );
225 QgsDebugMsg(
"raster filters: " + fileRasterFilters );
226 if ( !fileRasterFilters.isEmpty() )
227 mRasterFileFilters += fileRasterFilters;
229 QgsDebugMsg( QString(
"Checking %1: ...loaded OK (%2 file filters)" ).arg( myLib.fileName() ).arg( fileRasterFilters.split(
";;" ).count() ) );
238 void QgsProviderRegistry::clean()
242 Providers::const_iterator it = mProviders.begin();
244 while ( it != mProviders.end() )
247 QString lib = it->second->library();
248 if ( !lib.isEmpty() )
250 QLibrary myLib( lib );
251 if ( myLib.isLoaded() )
267 if ( sInstance ==
this )
282 QString
const &providerKey )
284 QgsProviderRegistry::Providers::const_iterator i =
285 metaData.find( providerKey );
287 if ( i != metaData.end() )
312 Providers::const_iterator it = mProviders.begin();
314 if ( mProviders.empty() )
315 return QObject::tr(
"No data provider plugins are available. No vector layers can be loaded" );
320 list += QLatin1String(
"<ol>" );
322 while ( it != mProviders.end() )
325 list += QLatin1String(
"<li>" );
327 list += it->second->description();
330 list += QLatin1String(
"<br></li>" );
338 list += QLatin1String(
"</ol>" );
345 mLibraryDirectory = path;
352 return mLibraryDirectory;
386 QString lib =
library( providerKey );
388 #ifdef TESTPROVIDERLIB 389 const char *cLib = lib.toUtf8();
393 void *handle = dlopen( cOgrLib, RTLD_LAZY | RTLD_GLOBAL );
406 QLibrary myLib( lib );
408 QgsDebugMsg(
"Library name is " + myLib.fileName() );
418 QgsDebugMsg( QString(
"Failed to load %1: no classFactory method" ).arg( lib ) );
430 QgsDebugMsg( QString(
"Instantiated the data provider plugin: %1" ).arg( dataProvider->
name() ) );
443 if ( !dataCapabilities )
448 return dataCapabilities();
460 if ( !selectFactory )
463 return selectFactory( parent, fl, widgetMode );
467 QString
const &functionName )
469 QString lib =
library( providerKey );
473 QLibrary myLib(
library( providerKey ) );
475 QgsDebugMsg(
"Library name is " + myLib.fileName() );
479 return myLib.resolve( functionName.toLatin1().data() );
483 QgsDebugMsg(
"Cannot load library: " + myLib.errorString() );
490 QString lib =
library( providerKey );
494 std::unique_ptr< QLibrary > myLib(
new QLibrary( lib ) );
496 QgsDebugMsg(
"Library name is " + myLib->fileName() );
499 return myLib.release();
501 QgsDebugMsg(
"Cannot load library: " + myLib->errorString() );
508 typedef void registerGui_function( QWidget * parent );
512 registerGui_function *registerGui =
reinterpret_cast< registerGui_function *
>(
cast_to_fptr(
function( provider,
"registerGui" ) ) );
517 registerGui( parent );
523 return mVectorFileFilters;
528 return mRasterFileFilters;
533 return mDatabaseDrivers;
538 return mDirectoryDrivers;
543 return mProtocolDrivers;
549 for ( Providers::const_iterator it = mProviders.begin(); it != mProviders.end(); ++it )
551 lst.append( it->first );
558 return findMetadata_( mProviders, providerKey );
WidgetMode
Different ways a source select dialog can be used (embedded is for the data source manager dialog) ...
QgsDataProvider * classFactoryFunction_t(const QString *)
QString databaseDrivers_t()
void cleanupProviderFunction_t()
virtual void setTitle(const QString &title)=0
set title for the messages
static void warning(const QString &msg)
Goes to qWarning.
virtual QString protocolDrivers() const
Return a string containing the available protocol drivers.
QString library(const QString &providerKey) const
Return path for the library of the provider.
void registerGuis(QWidget *widget)
virtual QString databaseDrivers() const
Return a string containing the available database drivers.
QgsDataProvider * createProvider(const QString &providerKey, const QString &dataSource)
Creates a new instance of a provider.
QString directoryDrivers_t()
static QgsMessageOutput * createMessageOutput()
function that returns new class derived from QgsMessageOutput (don't forget to delete it then if show...
Abstract base class for spatial data provider implementations.
virtual ~QgsProviderRegistry()
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning)
add a message to the instance (and create it if necessary)
virtual QString name() const =0
Return a provider name.
virtual QString fileVectorFilters() const
Return vector file filter string.
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.
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.
virtual void setMessage(const QString &message, MessageType msgType)=0
set message, it won't be displayed until
void setLibraryDirectory(const QDir &path)
Set library directory where to search for plugins.
const QgsProviderMetadata * providerMetadata(const QString &providerKey) const
Return metadata of the provider or NULL if not found.
QString pluginList(bool asHtml=false) const
Return 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
Return the provider capabilities.
virtual void showMessage(bool blocking=true)=0
display the message to the user and deletes itself
virtual QString directoryDrivers() const
Return 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)
Get pointer to provider function.
virtual QString fileRasterFilters() const
Return raster file filter string.
void buildsupportedrasterfilefilter_t(QString &fileFiltersString)
QString protocolDrivers_t()
QStringList providerList() const
Return 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.