-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
qgsproviderregistry.cpp
420 lines (327 loc) · 11.5 KB
/
qgsproviderregistry.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/***************************************************************************
qgsproviderregistry.cpp - Singleton class for
registering data providers.
-------------------
begin : Sat Jan 10 2004
copyright : (C) 2004 by Gary E.Sherman
email : sherman at mrcc.com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
/* $Id$ */
#include "qgsproviderregistry.h"
#include <iostream>
using namespace std;
#include <qmessagebox.h>
#include <qstring.h>
#include <qdir.h>
#include <qlibrary.h>
#include <qapplication.h>
#include "qgis.h"
#include "qgsdataprovider.h"
#include "qgsprovidermetadata.h"
// typedefs for provider plugin functions of interest
typedef QString providerkey_t();
typedef QString description_t();
typedef bool isprovider_t();
typedef QString fileVectorFilters_t();
QgsProviderRegistry *QgsProviderRegistry::_instance = 0;
QgsProviderRegistry *QgsProviderRegistry::instance(QString pluginPath)
{
if (_instance == 0)
{
_instance = new QgsProviderRegistry(pluginPath);
}
return _instance;
} // QgsProviderRegistry::instance
QgsProviderRegistry::QgsProviderRegistry(QString pluginPath)
{
// At startup, examine the libs in the qgis/lib dir and store those that
// are a provider shared lib
// check all libs in the current plugin directory and get name and descriptions
//TODO figure out how to register and identify data source plugin for a specific
//TODO layer type
/* char **argv = qApp->argv();
QString appDir = argv[0];
int bin = appDir.findRev("/bin", -1, false);
QString baseDir = appDir.left(bin);
QString mLibraryDirectory = baseDir + "/lib"; */
mLibraryDirectory = pluginPath;
mLibraryDirectory.setSorting( QDir::Name | QDir::IgnoreCase );
mLibraryDirectory.setFilter( QDir::Files | QDir::NoSymLinks );
#ifdef WIN32
mLibraryDirectory.setNameFilter( "*.dll" );
#else
mLibraryDirectory.setNameFilter( "*.so" );
#endif
#ifdef QGISDEBUG
cerr << "Checking " << mLibraryDirectory.path().data() << " for provider plugins\n";
#endif
if (mLibraryDirectory.count() == 0)
{
QString msg = QObject::tr("No Data Provider Plugins",
"No QGIS data provider plugins found in:");
msg += "\n" + mLibraryDirectory.path() + "\n\n";
msg += QObject::tr("No vector layers can be loaded. Check your QGIS installation");
QMessageBox::critical(0, QObject::tr("No Data Providers"), msg);
}
else
{
const QFileInfoList list = mLibraryDirectory.entryInfoList();
QListIterator<QFileInfo> it( list );
QFileInfo fi;
while (it.hasNext())
{
fi = it.next();
QLibrary *myLib = new QLibrary( fi.filePath() );
bool loaded = myLib->load();
if (loaded)
{
#ifdef QGISDEBUG
std::cout << "Checking " << myLib->library().toLocal8Bit().data() << std::endl;
#endif
// get the description and the key for the provider plugin
isprovider_t *isProvider = (isprovider_t *) myLib->resolve("isProvider");
if (isProvider)
{
// check to see if this is a provider plugin
if (isProvider())
{
// looks like a provider. get the key and description
description_t *pDesc = (description_t *) myLib->resolve("description");
providerkey_t *pKey = (providerkey_t *) myLib->resolve("providerKey");
if (pDesc && pKey)
{
// add this provider to the provider map
mProviders[pKey()] =
new QgsProviderMetadata(pKey(), pDesc(), myLib->library());
#ifdef QGISDEBUG
std::cout << "Loaded " << pDesc().toLocal8Bit().data() << std::endl;
#endif
// now get vector file filters, if any
fileVectorFilters_t *pFileVectorFilters =
(fileVectorFilters_t *) myLib->resolve("fileVectorFilters");
if ( pFileVectorFilters )
{
QString vectorFileFilters = pFileVectorFilters();
// now get vector file filters, if any
fileVectorFilters_t *pVectorFileFilters =
(fileVectorFilters_t *) myLib->resolve("fileVectorFilters");
if ( pVectorFileFilters )
{
QString fileVectorFilters = pVectorFileFilters();
if ( ! fileVectorFilters.isEmpty() )
{
mVectorFileFilters += fileVectorFilters;
}
else
{
QgsDebug( QString("No vector file filters for " + pKey()).ascii() );
}
}
else
{
QgsDebug( "Unable to invoke fileVectorFilters()" );
}
}
else
{
cout << myLib->library().data()
<< " Unable to find one of the required provider functions:\n\tproviderKey() or description()"
<< endl;
}
}
}
else
{
QgsDebug( "Unable to invoke fileVectorFilters()" );
}
}
else
{
cout << myLib->library().data()
<< " Unable to find one of the required provider functions:\n\tproviderKey() or description()"
<< endl;
}
}
//++it;
delete myLib;
}
}
} // QgsProviderRegistry ctor
/** convenience function for finding any existing data providers that match "providerKey"
Necessary because [] map operator will create a QgsProviderMetadata
instance. Also you cannot use the map [] operator in const members for that
very reason. So there needs to be a convenient way to find a data provider
without accidentally adding a null meta data item to the metadata map.
*/
static
QgsProviderMetadata * findMetadata_( QgsProviderRegistry::Providers const & metaData,
QString const & providerKey )
{
QgsProviderRegistry::Providers::const_iterator i =
metaData.find( providerKey );
if ( i != metaData.end() )
{
return i->second;
}
return 0x0;
} // findMetadata_
QString QgsProviderRegistry::library(QString const & providerKey) const
{
QgsProviderMetadata * md = findMetadata_( mProviders, providerKey );
if (md)
{
return md->library();
}
return QString();
}
QString QgsProviderRegistry::pluginList(bool asHTML) const
{
Providers::const_iterator it = mProviders.begin();
QString list;
if ( mProviders.empty() )
{
list = QObject::tr("No data provider plugins are available. No vector layers can be loaded");
}
else
{
if (asHTML)
{
list += "<ol>";
}
while (it != mProviders.end())
{
QgsProviderMetadata *mp = (*it).second;
if (asHTML)
{
list += "<li>" + mp->description() + "<br>";
}
else
{
list += mp->description() + "\n";
}
it++;
}
if (asHTML)
{
list += "</ol>";
}
}
return list;
}
void QgsProviderRegistry::setLibraryDirectory(QDir const & path)
{
mLibraryDirectory = path;
}
QDir const & QgsProviderRegistry::libraryDirectory() const
{
return mLibraryDirectory;
}
// typedef for the QgsDataProvider class factory
typedef QgsDataProvider * classFactoryFunction_t( const QString * );
/** Copied from QgsVectorLayer::setDataProvider
* TODO: Make it work in the generic environment
*
* TODO: Is this class really the best place to put a data provider loader?
* It seems more sensible to provide the code in one place rather than
* in qgsrasterlayer, qgsvectorlayer, serversourceselect, etc.
*/
QgsDataProvider* QgsProviderRegistry::getProvider( QString const & providerKey,
QString const & dataSource )
{
// XXX should I check for and possibly delete any pre-existing providers?
// XXX How often will that scenario occur?
#ifdef QGISDEBUG
const char * providerStr = providerKey.ascii(); // debugger probe
const char * dataSourceStr = dataSource.ascii(); // ditto
#endif
// load the plugin
QString lib = library(providerKey);
#ifdef TESTPROVIDERLIB
const char *cLib = (const char *) lib;
// test code to help debug provider loading problems
// void *handle = dlopen(cLib, RTLD_LAZY);
void *handle = dlopen(cOgrLib, RTLD_LAZY | RTLD_GLOBAL);
if (!handle)
{
cout << "Error in dlopen: " << dlerror() << endl;
}
else
{
cout << "dlopen suceeded" << endl;
dlclose(handle);
}
#endif
// load the data provider
QLibrary* myLib = new QLibrary((const char *) lib);
#ifdef QGISDEBUG
std::cout << "QgsProviderRegistry::getRasterProvider: Library name is "
<< myLib->library().toLocal8Bit().data()
<< std::endl;
#endif
bool loaded = myLib->load();
if (loaded)
{
QgsDebug( "Loaded data provider library" );
QgsDebug( "Attempting to resolve the classFactory function" );
classFactoryFunction_t * classFactory =
(classFactoryFunction_t *) myLib->resolve("classFactory");
if (classFactory)
{
QgsDebug( "Getting pointer to a dataProvider object from the library" );
//XXX - This was a dynamic cast but that kills the Windows
// version big-time with an abnormal termination error
// QgsDataProvider* dataProvider = (QgsDataProvider*)
// (classFactory((const char*)(dataSource.utf8())));
QgsDataProvider * dataProvider = (*classFactory)(&dataSource);
if (dataProvider)
{
#ifdef QGISDEBUG
QgsDebug( "Instantiated the data provider plugin" );
cerr << "provider name: " << dataProvider->name().data() << "\n";
//cout << "provider description: " << dataProvider->description() << "\n";
#endif
if (dataProvider->isValid())
{
return dataProvider;
}
else
{ // this is likely because the dataSource is invalid, and isn't
// necessarily a reflection on the data provider itself
QgsDebug( "Invalid data provider" );
myLib->unload();
return 0;
}
}
else
{
QgsDebug( "Unable to instantiate the data provider plugin" );
myLib->unload();
return 0;
}
}
}
else
{
QgsDebug( "Failed to load ../providers/libproviders.so" );
return 0;
}
QgsDebug( "exiting" );
return 0; // factory didn't exist
} // QgsProviderRegistry::setDataProvider
QString QgsProviderRegistry::fileVectorFilters() const
{
return mVectorFileFilters;
} // QgsProviderRegistry::fileVectorFilters
QgsDataProvider *
QgsProviderRegistry::openVector( QString const & dataSource, QString const & providerKey )
{
return getProvider( providerKey, dataSource );
} // QgsProviderRegistry::openVector