QGIS API Documentation  3.13.0-Master (740be229cb)
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"
27 
28 #ifdef HAVE_STATIC_PROVIDERS
29 #include "qgswmsprovidergui.h"
30 #include "qgspostgresprovidergui.h"
31 #endif
32 
41 static
42 QgsProviderGuiMetadata *findMetadata_( QgsProviderGuiRegistry::GuiProviders const &metaData,
43  QString const &providerKey )
44 {
45  QgsProviderGuiRegistry::GuiProviders::const_iterator i = metaData.find( providerKey );
46  if ( i != metaData.end() )
47  {
48  return i->second;
49  }
50 
51  return nullptr;
52 } // findMetadata_
53 
55 {
56  loadStaticProviders();
57  loadDynamicProviders( pluginPath );
58 }
59 
60 void QgsProviderGuiRegistry::loadStaticProviders( )
61 {
62  // Register static providers
63  QgsProviderGuiMetadata *gdal = new QgsGdalGuiProviderMetadata();
64  mProviders[ gdal->key() ] = gdal;
65 
66  QgsProviderGuiMetadata *ogr = new QgsOgrGuiProviderMetadata();
67  mProviders[ ogr->key() ] = ogr;
68 
69 #ifdef HAVE_STATIC_PROVIDERS
70  QgsProviderGuiMetadata *wms = new QgsWmsProviderGuiMetadata();
71  mProviders[ wms->key() ] = wms;
72 
73  QgsProviderGuiMetadata *postgres = new QgsPostgresProviderGuiMetadata();
74  mProviders[ postgres->key() ] = postgres;
75 #endif
76 }
77 
78 void QgsProviderGuiRegistry::loadDynamicProviders( const QString &pluginPath )
79 {
80 #ifdef HAVE_STATIC_PROVIDERS
81  QgsDebugMsg( QStringLiteral( "Forced only static GUI providers" ) );
82 #else
83  typedef QgsProviderGuiMetadata *factory_function( );
84 
85  // add dynamic providers
86  QDir mLibraryDirectory( pluginPath );
87  mLibraryDirectory.setSorting( QDir::Name | QDir::IgnoreCase );
88  mLibraryDirectory.setFilter( QDir::Files | QDir::NoSymLinks );
89 
90 #if defined(Q_OS_WIN) || defined(__CYGWIN__)
91  mLibraryDirectory.setNameFilters( QStringList( "*.dll" ) );
92 #elif defined(ANDROID)
93  mLibraryDirectory.setNameFilters( QStringList( "*provider.so" ) );
94 #else
95  mLibraryDirectory.setNameFilters( QStringList( QStringLiteral( "*.so" ) ) );
96 #endif
97 
98  QgsDebugMsg( QStringLiteral( "Checking %1 for GUI provider plugins" ).arg( mLibraryDirectory.path() ) );
99 
100  if ( mLibraryDirectory.count() == 0 )
101  {
102  QgsDebugMsg( QStringLiteral( "No dynamic QGIS GUI provider plugins found in:\n%1\n" ).arg( mLibraryDirectory.path() ) );
103  }
104 
105  // provider file regex pattern, only files matching the pattern are loaded if the variable is defined
106  QString filePattern = getenv( "QGIS_PROVIDER_FILE" );
107  QRegExp fileRegexp;
108  if ( !filePattern.isEmpty() )
109  {
110  fileRegexp.setPattern( filePattern );
111  }
112 
113  const auto constEntryInfoList = mLibraryDirectory.entryInfoList();
114  for ( const QFileInfo &fi : constEntryInfoList )
115  {
116  if ( !fileRegexp.isEmpty() )
117  {
118  if ( fileRegexp.indexIn( fi.fileName() ) == -1 )
119  {
120  QgsDebugMsg( "provider " + fi.fileName() + " skipped because doesn't match pattern " + filePattern );
121  continue;
122  }
123  }
124 
125  QLibrary myLib( fi.filePath() );
126  if ( myLib.load() )
127  {
128  QFunctionPointer func = myLib.resolve( QStringLiteral( "providerGuiMetadataFactory" ).toLatin1().data() );
129  factory_function *function = reinterpret_cast< factory_function * >( cast_to_fptr( func ) );
130  if ( !function )
131  continue;
132 
133  QgsProviderGuiMetadata *meta = function( );
134 
135  if ( !meta )
136  continue;
137 
138  const QString providerKey = meta->key();
139 
140  // check if such providers is already registered
141  if ( findMetadata_( mProviders, providerKey ) )
142  continue;
143 
144  mProviders[providerKey] = meta;
145  }
146  }
147 #endif
148 }
149 
151 {
152  GuiProviders::const_iterator it = mProviders.begin();
153  while ( it != mProviders.end() )
154  {
155  delete it->second;
156  ++it;
157  }
158  mProviders.clear();
159 }
160 
161 void QgsProviderGuiRegistry::registerGuis( QMainWindow *parent )
162 {
163  GuiProviders::const_iterator it = mProviders.begin();
164  while ( it != mProviders.end() )
165  {
166  it->second->registerGui( parent );
167  ++it;
168  }
169 }
170 
171 const QList<QgsDataItemGuiProvider *> QgsProviderGuiRegistry::dataItemGuiProviders( const QString &providerKey )
172 {
173  QgsProviderGuiMetadata *meta = findMetadata_( mProviders, providerKey );
174  if ( meta )
175  return meta->dataItemGuiProviders();
176  return QList<QgsDataItemGuiProvider *>();
177 }
178 
179 QList<QgsSourceSelectProvider *> QgsProviderGuiRegistry::sourceSelectProviders( const QString &providerKey )
180 {
181  QgsProviderGuiMetadata *meta = findMetadata_( mProviders, providerKey );
182  if ( meta )
183  return meta->sourceSelectProviders();
184  return QList<QgsSourceSelectProvider *> ();
185 }
186 
187 QList<QgsProjectStorageGuiProvider *> QgsProviderGuiRegistry::projectStorageGuiProviders( const QString &providerKey )
188 {
189  QgsProviderGuiMetadata *meta = findMetadata_( mProviders, providerKey );
190  if ( meta )
191  return meta->projectStorageGuiProviders();
192  return QList<QgsProjectStorageGuiProvider *>();
193 }
194 
196 {
197  QStringList lst;
198  for ( GuiProviders::const_iterator it = mProviders.begin(); it != mProviders.end(); ++it )
199  {
200  lst.append( it->first );
201  }
202  return lst;
203 }
204 
205 const QgsProviderGuiMetadata *QgsProviderGuiRegistry::providerMetadata( const QString &providerKey ) const
206 {
207  return findMetadata_( mProviders, providerKey );
208 }
virtual QList< QgsSourceSelectProvider * > sourceSelectProviders()
Returns source select providers.
std::map< QString, QgsProviderGuiMetadata * > GuiProviders
Type for data provider metadata associative container.
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
virtual QList< QgsProjectStorageGuiProvider * > projectStorageGuiProviders(const QString &providerKey)
Returns all project storage gui providers registered in provider with providerKey.
virtual const QList< QgsDataItemGuiProvider * > dataItemGuiProviders(const QString &providerKey)
Returns all data item gui providers registered in provider with providerKey.
virtual QList< QgsDataItemGuiProvider * > dataItemGuiProviders()
Returns data item gui providers.
QgsProviderGuiRegistry(const QString &pluginPath)
Creates registry and loads static provider plugins.
const QgsProviderGuiMetadata * providerMetadata(const QString &providerKey) const
Returns metadata of the provider or nullptr if not found.
QString key() const
Returns unique provider key.
virtual QList< QgsProjectStorageGuiProvider * > projectStorageGuiProviders()
Returns project storage gui providers.
#define cast_to_fptr(f)
Definition: qgis.h:208
virtual QList< QgsSourceSelectProvider * > sourceSelectProviders(const QString &providerKey)
Returns all source select providers registered in provider with providerKey.
Holds data for GUI part of the data providers.
QStringList providerList() const
Returns list of available providers by their keys.
void registerGuis(QMainWindow *widget)
Called during GUI initialization - allows providers to do its internal initialization of GUI componen...