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