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