QGIS API Documentation  3.4.15-Madeira (e83d02e274)
qgssourceselectproviderregistry.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgssourceselectproviderregistry.cpp - QgsSourceSelectProviderRegistry
3 
4  ---------------------
5  begin : 1.9.2017
6  copyright : (C) 2017 by Alessandro Pasotti
7  email : apasotti at boundlessgeo dot com
8  ***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
18 #include "qgsproviderregistry.h"
19 
20 #include <memory>
21 
22 typedef QList<QgsSourceSelectProvider *> *sourceSelectProviders_t();
23 
25 {
26  qDeleteAll( mProviders );
27 }
28 
29 QList<QgsSourceSelectProvider *> QgsSourceSelectProviderRegistry::providers()
30 {
31  init();
32  return mProviders;
33 }
34 
36 {
37  mProviders.append( provider );
38  std::sort( mProviders.begin(), mProviders.end(), [ ]( const QgsSourceSelectProvider * first, const QgsSourceSelectProvider * second ) -> bool
39  {
40  return first->ordering() < second->ordering();
41  } );
42 }
43 
45 {
46  int index = mProviders.indexOf( provider );
47  if ( index >= 0 )
48  {
49  delete mProviders.takeAt( index );
50  return true;
51  }
52  return false;
53 }
54 
56 {
57  const QList<QgsSourceSelectProvider *> providerList = providers();
58  for ( const auto provider : providerList )
59  {
60  if ( provider->name() == name )
61  {
62  return provider;
63  }
64  }
65  return nullptr;
66 }
67 
68 QList<QgsSourceSelectProvider *> QgsSourceSelectProviderRegistry::providersByKey( const QString &providerKey )
69 {
70  QList<QgsSourceSelectProvider *> result;
71  const QList<QgsSourceSelectProvider *> providerList = providers();
72  for ( const auto provider : providerList )
73  {
74  if ( provider->providerKey() == providerKey )
75  {
76  result << provider;
77  }
78  }
79  return result;
80 }
81 
82 void QgsSourceSelectProviderRegistry::init()
83 {
84  if ( mInitialized )
85  {
86  return;
87  }
88  const QStringList providersList = QgsProviderRegistry::instance()->providerList();
89  for ( const QString &key : providersList )
90  {
91  std::unique_ptr< QLibrary > library( QgsProviderRegistry::instance()->createProviderLibrary( key ) );
92  if ( !library )
93  continue;
94 
95  sourceSelectProviders_t *sourceSelectProvidersFn = reinterpret_cast< sourceSelectProviders_t * >( cast_to_fptr( library->resolve( "sourceSelectProviders" ) ) );
96  if ( sourceSelectProvidersFn )
97  {
98  QList<QgsSourceSelectProvider *> *providerList = sourceSelectProvidersFn();
99  // the function is a factory - we keep ownership of the returned providers
100  for ( auto provider : qgis::as_const( *providerList ) )
101  {
102  addProvider( provider );
103  }
104  delete providerList;
105  }
106  }
107  mInitialized = true;
108 }
109 
QList< QgsSourceSelectProvider * > providers()
Gets list of available providers.
QList< QgsSourceSelectProvider * > * sourceSelectProviders_t()
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:171
QgsSourceSelectProvider * providerByName(const QString &name)
Returns a provider by name or nullptr if not found.
This is the interface for those who want to add entries to the QgsDataSourceManagerDialog.
bool removeProvider(QgsSourceSelectProvider *provider)
Remove provider implementation from the list (provider object is deleted)
void addProvider(QgsSourceSelectProvider *provider)
Add a provider implementation. Takes ownership of the object.
virtual int ordering() const
Ordering: the source select provider registry will be able to sort the source selects (ascending) usi...
QList< QgsSourceSelectProvider * > providersByKey(const QString &providerKey)
Returns a (possibly empty) list of providers by data providerkey.