QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
qgsproviderguiregistry.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsproviderrguiegistry.cpp
3  -------------------
4  begin : June 2019
5  copyright : (C) 2019 by Peter Petrik
6  email : zilolv at google dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgsproviderguiregistry.h"
19 
20 #include <QString>
21 #include <QDir>
22 #include <QLibrary>
23 
24 #include "qgslogger.h"
25 #include "qgsgdalguiprovider.h"
26 #include "qgsogrguiprovider.h"
28 #include "qgspointcloudproviderguimetadata.h"
29 
30 #ifdef HAVE_EPT
31 #include "qgseptproviderguimetadata.h"
32 #endif
33 
34 #ifdef HAVE_STATIC_PROVIDERS
35 #include "qgswmsprovidergui.h"
36 #include "qgspostgresprovidergui.h"
37 #endif
38 
47 static
48 QgsProviderGuiMetadata *findMetadata_( QgsProviderGuiRegistry::GuiProviders const &metaData,
49  QString const &providerKey )
50 {
51  QgsProviderGuiRegistry::GuiProviders::const_iterator i = metaData.find( providerKey );
52  if ( i != metaData.end() )
53  {
54  return i->second;
55  }
56 
57  return nullptr;
58 } // findMetadata_
59 
61 {
62  loadStaticProviders();
63  loadDynamicProviders( pluginPath );
64 }
65 
66 void QgsProviderGuiRegistry::loadStaticProviders( )
67 {
68  // Register static providers
69  QgsProviderGuiMetadata *gdal = new QgsGdalGuiProviderMetadata();
70  mProviders[ gdal->key() ] = gdal;
71 
72  QgsProviderGuiMetadata *ogr = new QgsOgrGuiProviderMetadata();
73  mProviders[ ogr->key() ] = ogr;
74 
75  QgsProviderGuiMetadata *vt = new QgsVectorTileProviderGuiMetadata();
76  mProviders[ vt->key() ] = vt;
77 
78 #ifdef HAVE_EPT
79  QgsProviderGuiMetadata *ept = new QgsEptProviderGuiMetadata();
80  mProviders[ ept->key() ] = ept;
81 #endif
82 
83  // only show point cloud option if we have at least one point cloud provider available!
84  if ( !QgsProviderRegistry::instance()->filePointCloudFilters().isEmpty() )
85  {
86  QgsProviderGuiMetadata *pointcloud = new QgsPointCloudProviderGuiMetadata();
87  mProviders[ pointcloud->key() ] = pointcloud;
88  }
89 
90 #ifdef HAVE_STATIC_PROVIDERS
91  QgsProviderGuiMetadata *wms = new QgsWmsProviderGuiMetadata();
92  mProviders[ wms->key() ] = wms;
93 
94  QgsProviderGuiMetadata *postgres = new QgsPostgresProviderGuiMetadata();
95  mProviders[ postgres->key() ] = postgres;
96 #endif
97 }
98 
99 void QgsProviderGuiRegistry::loadDynamicProviders( const QString &pluginPath )
100 {
101 #ifdef HAVE_STATIC_PROVIDERS
102  QgsDebugMsg( QStringLiteral( "Forced only static GUI providers" ) );
103 #else
104  typedef QgsProviderGuiMetadata *factory_function( );
105 
106  // add dynamic providers
107  QDir mLibraryDirectory( pluginPath );
108  mLibraryDirectory.setSorting( QDir::Name | QDir::IgnoreCase );
109  mLibraryDirectory.setFilter( QDir::Files | QDir::NoSymLinks );
110 
111 #if defined(Q_OS_WIN) || defined(__CYGWIN__)
112  mLibraryDirectory.setNameFilters( QStringList( "*.dll" ) );
113 #elif defined(ANDROID)
114  mLibraryDirectory.setNameFilters( QStringList( "*provider.so" ) );
115 #else
116  mLibraryDirectory.setNameFilters( QStringList( QStringLiteral( "*.so" ) ) );
117 #endif
118 
119  QgsDebugMsgLevel( QStringLiteral( "Checking %1 for GUI provider plugins" ).arg( mLibraryDirectory.path() ), 2 );
120 
121  if ( mLibraryDirectory.count() == 0 )
122  {
123  QgsDebugMsg( QStringLiteral( "No dynamic QGIS GUI provider plugins found in:\n%1\n" ).arg( mLibraryDirectory.path() ) );
124  }
125 
126  // provider file regex pattern, only files matching the pattern are loaded if the variable is defined
127  QString filePattern = getenv( "QGIS_PROVIDER_FILE" );
128  QRegExp fileRegexp;
129  if ( !filePattern.isEmpty() )
130  {
131  fileRegexp.setPattern( filePattern );
132  }
133 
134  const auto constEntryInfoList = mLibraryDirectory.entryInfoList();
135  for ( const QFileInfo &fi : constEntryInfoList )
136  {
137  if ( !fileRegexp.isEmpty() )
138  {
139  if ( fileRegexp.indexIn( fi.fileName() ) == -1 )
140  {
141  QgsDebugMsg( "provider " + fi.fileName() + " skipped because doesn't match pattern " + filePattern );
142  continue;
143  }
144  }
145 
146  QLibrary myLib( fi.filePath() );
147  if ( myLib.load() )
148  {
149  QFunctionPointer func = myLib.resolve( QStringLiteral( "providerGuiMetadataFactory" ).toLatin1().data() );
150  factory_function *function = reinterpret_cast< factory_function * >( cast_to_fptr( func ) );
151  if ( !function )
152  continue;
153 
154  QgsProviderGuiMetadata *meta = function( );
155 
156  if ( !meta )
157  continue;
158 
159  const QString providerKey = meta->key();
160 
161  // check if such providers is already registered
162  if ( findMetadata_( mProviders, providerKey ) )
163  continue;
164 
165  mProviders[providerKey] = meta;
166  }
167  }
168 #endif
169 }
170 
172 {
173  GuiProviders::const_iterator it = mProviders.begin();
174  while ( it != mProviders.end() )
175  {
176  delete it->second;
177  ++it;
178  }
179  mProviders.clear();
180 }
181 
182 void QgsProviderGuiRegistry::registerGuis( QMainWindow *parent )
183 {
184  GuiProviders::const_iterator it = mProviders.begin();
185  while ( it != mProviders.end() )
186  {
187  it->second->registerGui( parent );
188  ++it;
189  }
190 }
191 
192 const QList<QgsDataItemGuiProvider *> QgsProviderGuiRegistry::dataItemGuiProviders( const QString &providerKey )
193 {
194  QgsProviderGuiMetadata *meta = findMetadata_( mProviders, providerKey );
195  if ( meta )
196  return meta->dataItemGuiProviders();
197  return QList<QgsDataItemGuiProvider *>();
198 }
199 
200 QList<QgsSourceSelectProvider *> QgsProviderGuiRegistry::sourceSelectProviders( const QString &providerKey )
201 {
202  QgsProviderGuiMetadata *meta = findMetadata_( mProviders, providerKey );
203  if ( meta )
204  return meta->sourceSelectProviders();
205  return QList<QgsSourceSelectProvider *> ();
206 }
207 
208 QList<QgsProjectStorageGuiProvider *> QgsProviderGuiRegistry::projectStorageGuiProviders( const QString &providerKey )
209 {
210  QgsProviderGuiMetadata *meta = findMetadata_( mProviders, providerKey );
211  if ( meta )
212  return meta->projectStorageGuiProviders();
213  return QList<QgsProjectStorageGuiProvider *>();
214 }
215 
216 QList<QgsSubsetStringEditorProvider *> QgsProviderGuiRegistry::subsetStringEditorProviders( const QString &providerKey )
217 {
218  QgsProviderGuiMetadata *meta = findMetadata_( mProviders, providerKey );
219  if ( meta )
220  return meta->subsetStringEditorProviders();
221  return QList<QgsSubsetStringEditorProvider *>();
222 }
223 
224 QList<QgsProviderSourceWidgetProvider *> QgsProviderGuiRegistry::sourceWidgetProviders( const QString &providerKey )
225 {
226  QgsProviderGuiMetadata *meta = findMetadata_( mProviders, providerKey );
227  if ( meta )
228  return meta->sourceWidgetProviders();
229  return QList<QgsProviderSourceWidgetProvider *>();
230 }
231 
233 {
234  QStringList lst;
235  for ( GuiProviders::const_iterator it = mProviders.begin(); it != mProviders.end(); ++it )
236  {
237  lst.append( it->first );
238  }
239  return lst;
240 }
241 
242 const QgsProviderGuiMetadata *QgsProviderGuiRegistry::providerMetadata( const QString &providerKey ) const
243 {
244  return findMetadata_( mProviders, providerKey );
245 }
Holds data for GUI part of the data providers.
virtual QList< QgsSubsetStringEditorProvider * > subsetStringEditorProviders()
Returns subset string editor providers.
virtual QList< QgsDataItemGuiProvider * > dataItemGuiProviders()
Returns data item gui providers.
virtual QList< QgsProjectStorageGuiProvider * > projectStorageGuiProviders()
Returns project storage gui providers.
QString key() const
Returns unique provider key.
virtual QList< QgsProviderSourceWidgetProvider * > sourceWidgetProviders()
Returns source widget providers.
virtual QList< QgsSourceSelectProvider * > sourceSelectProviders()
Returns source select providers.
virtual const QList< QgsDataItemGuiProvider * > dataItemGuiProviders(const QString &providerKey)
Returns all data item gui providers registered in provider with providerKey.
virtual QList< QgsSubsetStringEditorProvider * > subsetStringEditorProviders(const QString &providerKey)
Returns all subset string editor providers registered in provider with providerKey.
const QgsProviderGuiMetadata * providerMetadata(const QString &providerKey) const
Returns metadata of the provider or nullptr if not found.
virtual QList< QgsProjectStorageGuiProvider * > projectStorageGuiProviders(const QString &providerKey)
Returns all project storage gui providers registered in provider with providerKey.
std::map< QString, QgsProviderGuiMetadata * > GuiProviders
Type for data provider metadata associative container.
QgsProviderGuiRegistry(const QString &pluginPath)
Creates registry and loads static provider plugins.
virtual QList< QgsSourceSelectProvider * > sourceSelectProviders(const QString &providerKey)
Returns all source select providers registered in provider with providerKey.
virtual QList< QgsProviderSourceWidgetProvider * > sourceWidgetProviders(const QString &providerKey)
Returns all source widget providers registered in provider with providerKey.
void registerGuis(QMainWindow *widget)
Called during GUI initialization - allows providers to do its internal initialization of GUI componen...
QStringList providerList() const
Returns list of available providers by their keys.
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
#define cast_to_fptr(f)
Definition: qgis.h:209
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
#define QgsDebugMsg(str)
Definition: qgslogger.h:38