22
22
#include < QDir>
23
23
#include < QLibrary>
24
24
25
-
26
25
#include " qgis.h"
27
26
#include " qgsdataprovider.h"
28
27
#include " qgslogger.h"
@@ -65,13 +64,14 @@ QgsProviderRegistry::QgsProviderRegistry( QString pluginPath )
65
64
// check all libs in the current plugin directory and get name and descriptions
66
65
// TODO figure out how to register and identify data source plugin for a specific
67
66
// TODO layer type
68
- /* char **argv = qApp->argv();
69
- QString appDir = argv[0];
70
- int bin = appDir.findRev("/bin", -1, false);
71
- QString baseDir = appDir.left(bin);
72
- QString mLibraryDirectory = baseDir + "/lib"; */
67
+ #if 0
68
+ char **argv = qApp->argv();
69
+ QString appDir = argv[0];
70
+ int bin = appDir.findRev( "/bin", -1, false );
71
+ QString baseDir = appDir.left( bin );
72
+ QString mLibraryDirectory = baseDir + "/lib";
73
+ #endif
73
74
mLibraryDirectory = pluginPath;
74
-
75
75
mLibraryDirectory .setSorting ( QDir::Name | QDir::IgnoreCase );
76
76
mLibraryDirectory .setFilter ( QDir::Files | QDir::NoSymLinks );
77
77
@@ -94,134 +94,96 @@ QgsProviderRegistry::QgsProviderRegistry( QString pluginPath )
94
94
output->setTitle ( QObject::tr ( " No Data Providers" ) );
95
95
output->setMessage ( msg, QgsMessageOutput::MessageText );
96
96
output->showMessage ();
97
+ return ;
97
98
}
98
- else
99
+
100
+ QListIterator<QFileInfo> it ( mLibraryDirectory .entryInfoList () );
101
+ while ( it.hasNext () )
99
102
{
100
- const QFileInfoList list = mLibraryDirectory .entryInfoList ();
101
- QListIterator<QFileInfo> it ( list );
102
- QFileInfo fi;
103
+ QFileInfo fi ( it.next () );
103
104
104
- while ( it.hasNext () )
105
+ QLibrary myLib ( fi.filePath () );
106
+ if ( !myLib.load () )
105
107
{
106
- fi = it.next ();
107
-
108
- QLibrary *myLib = new QLibrary ( fi.filePath () );
109
-
110
- bool loaded = myLib->load ();
111
- // we will build up a debug message and print on one line to avoid terminal spam
112
- QString myMessage = " Checking " + myLib->fileName () + " : " ;
113
-
114
- if ( loaded )
115
- {
116
- // get the description and the key for the provider plugin
117
- isprovider_t *isProvider = ( isprovider_t * ) cast_to_fptr ( myLib->resolve ( " isProvider" ) );
118
-
119
- // MH: Added a further test to detect non-provider plugins linked to provider plugins.
120
- // Only pure provider plugins have 'type' not defined
121
- isprovider_t *hasType = ( isprovider_t * ) cast_to_fptr ( myLib->resolve ( " type" ) );
122
-
123
- if ( !hasType && isProvider )
124
- {
125
- // check to see if this is a provider plugin
126
- if ( isProvider () )
127
- {
128
- // looks like a provider. get the key and description
129
- description_t *pDesc = ( description_t * ) cast_to_fptr ( myLib->resolve ( " description" ) );
130
- providerkey_t *pKey = ( providerkey_t * ) cast_to_fptr ( myLib->resolve ( " providerKey" ) );
131
- if ( pDesc && pKey )
132
- {
133
- // add this provider to the provider map
134
- mProviders [pKey ()] =
135
- new QgsProviderMetadata ( pKey (), pDesc (), myLib->fileName () );
136
- // myMessage += "Loaded " + QString(pDesc()) + " ok";
137
-
138
- // now get vector file filters, if any
139
- fileVectorFilters_t *pFileVectorFilters =
140
- ( fileVectorFilters_t * ) cast_to_fptr ( myLib->resolve ( " fileVectorFilters" ) );
141
- // load database drivers
142
- databaseDrivers_t *pDatabaseDrivers =
143
- ( databaseDrivers_t * ) cast_to_fptr ( myLib->resolve ( " databaseDrivers" ) );
144
- if ( pDatabaseDrivers )
145
- {
146
- mDatabaseDrivers = pDatabaseDrivers ();
147
- }
148
- // load directory drivers
149
- directoryDrivers_t *pDirectoryDrivers =
150
- ( directoryDrivers_t * ) cast_to_fptr ( myLib->resolve ( " directoryDrivers" ) );
151
- if ( pDirectoryDrivers )
152
- {
153
- mDirectoryDrivers = pDirectoryDrivers ();
154
- }
155
- // load protocol drivers
156
- protocolDrivers_t *pProtocolDrivers =
157
- ( protocolDrivers_t * ) cast_to_fptr ( myLib->resolve ( " protocolDrivers" ) );
158
- if ( pProtocolDrivers )
159
- {
160
- mProtocolDrivers = pProtocolDrivers ();
161
- }
162
-
163
- if ( pFileVectorFilters )
164
- {
165
- QString vectorFileFilters = pFileVectorFilters ();
166
-
167
- // now get vector file filters, if any
168
- fileVectorFilters_t *pVectorFileFilters =
169
- ( fileVectorFilters_t * ) cast_to_fptr ( myLib->resolve ( " fileVectorFilters" ) );
170
-
171
- if ( pVectorFileFilters )
172
- {
173
- QString fileVectorFilters = pVectorFileFilters ();
174
-
175
- if ( ! fileVectorFilters.isEmpty () )
176
- {
177
- mVectorFileFilters += fileVectorFilters;
178
- myMessage += QString ( " ... loaded ok (and with %1 file filters)" ).
179
- arg ( fileVectorFilters.split ( " ;;" ).count () );
180
- }
181
- else
182
- {
183
- // myMessage += ", but it has no vector file filters for " + QString(pKey());
184
- myMessage += " ... loaded ok (0 file filters)" ;
185
- }
186
- }
187
- }
188
- else
189
- {
190
- // myMessage += ", but unable to invoke fileVectorFilters()";
191
- myMessage += " ... loaded ok (null file filters)" ;
192
- }
193
- }
194
- else
195
- {
196
- // myMessage += ", but unable to find one of the required provider functions (providerKey() or description()) in ";
197
- myMessage += " ...not usable" ;
198
-
199
- }
200
- }
201
- else
202
- {
203
- // myMessage += ", but this is not a valid provider, skipping.";
204
- myMessage += " ..invalid" ;
205
- }
206
- }
207
- else
208
- {
209
- // myMessage += ", but this is not a valid provider or has no type, skipping.";
210
- myMessage += " ..invalid (no type)" ;
211
- }
212
- }
213
- else
214
- {
215
- myMessage += " ...invalid (lib not loadable): " ;
216
- myMessage += myLib->errorString ();
217
- }
218
-
219
- QgsDebugMsg ( myMessage );
220
-
221
- delete myLib;
108
+ QgsDebugMsg ( QString ( " Checking %1: ...invalid (lib not loadable): %2" ).arg ( myLib.fileName () ).arg ( myLib.errorString () ) );
109
+ continue ;
222
110
}
223
- }
224
111
112
+ // MH: Added a further test to detect non-provider plugins linked to provider plugins.
113
+ // Only pure provider plugins have 'type' not defined
114
+ isprovider_t *hasType = ( isprovider_t * ) cast_to_fptr ( myLib.resolve ( " type" ) );
115
+ if ( hasType )
116
+ {
117
+ QgsDebugMsg ( QString ( " Checking %1: ...invalid (has type method)" ).arg ( myLib.fileName () ) );
118
+ continue ;
119
+ }
120
+
121
+ // get the description and the key for the provider plugin
122
+ isprovider_t *isProvider = ( isprovider_t * ) cast_to_fptr ( myLib.resolve ( " isProvider" ) );
123
+ if ( !isProvider )
124
+ {
125
+ QgsDebugMsg ( QString ( " Checking %1: ...invalid (no isProvider method)" ).arg ( myLib.fileName () ) );
126
+ continue ;
127
+ }
128
+
129
+ // check to see if this is a provider plugin
130
+ if ( !isProvider () )
131
+ {
132
+ QgsDebugMsg ( QString ( " Checking %1: ...invalid (not a provider)" ).arg ( myLib.fileName () ) );
133
+ continue ;
134
+ }
135
+
136
+ // looks like a provider. get the key and description
137
+ description_t *pDesc = ( description_t * ) cast_to_fptr ( myLib.resolve ( " description" ) );
138
+ if ( !pDesc )
139
+ {
140
+ QgsDebugMsg ( QString ( " Checking %1: ...invalid (no description method)" ).arg ( myLib.fileName () ) );
141
+ continue ;
142
+ }
143
+
144
+ providerkey_t *pKey = ( providerkey_t * ) cast_to_fptr ( myLib.resolve ( " providerKey" ) );
145
+ if ( !pKey )
146
+ {
147
+ QgsDebugMsg ( QString ( " Checking %1: ...invalid (no providerKey method)" ).arg ( myLib.fileName () ) );
148
+ continue ;
149
+ }
150
+
151
+ // add this provider to the provider map
152
+ mProviders [pKey ()] = new QgsProviderMetadata ( pKey (), pDesc (), myLib.fileName () );
153
+
154
+ // load database drivers
155
+ databaseDrivers_t *pDatabaseDrivers = ( databaseDrivers_t * ) cast_to_fptr ( myLib.resolve ( " databaseDrivers" ) );
156
+ if ( pDatabaseDrivers )
157
+ {
158
+ mDatabaseDrivers = pDatabaseDrivers ();
159
+ }
160
+
161
+ // load directory drivers
162
+ directoryDrivers_t *pDirectoryDrivers = ( directoryDrivers_t * ) cast_to_fptr ( myLib.resolve ( " directoryDrivers" ) );
163
+ if ( pDirectoryDrivers )
164
+ {
165
+ mDirectoryDrivers = pDirectoryDrivers ();
166
+ }
167
+
168
+ // load protocol drivers
169
+ protocolDrivers_t *pProtocolDrivers = ( protocolDrivers_t * ) cast_to_fptr ( myLib.resolve ( " protocolDrivers" ) );
170
+ if ( pProtocolDrivers )
171
+ {
172
+ mProtocolDrivers = pProtocolDrivers ();
173
+ }
174
+
175
+ // now get vector file filters, if any
176
+ fileVectorFilters_t *pFileVectorFilters = ( fileVectorFilters_t * ) cast_to_fptr ( myLib.resolve ( " fileVectorFilters" ) );
177
+ if ( pFileVectorFilters )
178
+ {
179
+ QString fileVectorFilters = pFileVectorFilters ();
180
+
181
+ if ( !fileVectorFilters.isEmpty () )
182
+ mVectorFileFilters += fileVectorFilters;
183
+
184
+ QgsDebugMsg ( QString ( " Checking %1: ...loaded ok (%2 file filters)" ).arg ( myLib.fileName () ).arg ( fileVectorFilters.split ( " ;;" ).count () ) );
185
+ }
186
+ }
225
187
} // QgsProviderRegistry ctor
226
188
227
189
@@ -270,39 +232,33 @@ QString QgsProviderRegistry::library( QString const & providerKey ) const
270
232
QString QgsProviderRegistry::pluginList ( bool asHTML ) const
271
233
{
272
234
Providers::const_iterator it = mProviders .begin ();
273
- QString list;
274
235
275
236
if ( mProviders .empty () )
276
- {
277
- list = QObject::tr ( " No data provider plugins are available. No vector layers can be loaded" );
278
- }
279
- else
237
+ return QObject::tr ( " No data provider plugins are available. No vector layers can be loaded" );
238
+
239
+ QString list;
240
+
241
+ if ( asHTML )
242
+ list += " <ol>" ;
243
+
244
+ while ( it != mProviders .end () )
280
245
{
281
246
if ( asHTML )
282
- {
283
- list += " <ol>" ;
284
- }
285
- while ( it != mProviders .end () )
286
- {
287
- QgsProviderMetadata *mp = ( *it ).second ;
288
-
289
- if ( asHTML )
290
- {
291
- list += " <li>" + mp->description () + " <br>" ;
292
- }
293
- else
294
- {
295
- list += mp->description () + " \n " ;
296
- }
297
-
298
- it++;
299
- }
247
+ list += " <li>" ;
248
+
249
+ list += it->second ->description ();
250
+
300
251
if ( asHTML )
301
- {
302
- list += " </ol>" ;
303
- }
252
+ list + " <br></li>" ;
253
+ else
254
+ list += " \n " ;
255
+
256
+ it++;
304
257
}
305
258
259
+ if ( asHTML )
260
+ list += " </ol>" ;
261
+
306
262
return list;
307
263
}
308
264
@@ -357,64 +313,33 @@ QgsDataProvider *QgsProviderRegistry::provider( QString const & providerKey, QSt
357
313
}
358
314
359
315
#endif
360
-
361
316
// load the data provider
362
- QLibrary* myLib = new QLibrary ( lib );
363
-
364
- QgsDebugMsg ( " Library name is " + myLib->fileName () );
365
-
366
- bool loaded = myLib->load ();
317
+ QLibrary myLib ( lib );
367
318
368
- if ( loaded )
319
+ QgsDebugMsg ( " Library name is " + myLib.fileName () );
320
+ if ( !myLib.load () )
369
321
{
370
- QgsDebugMsg ( " Loaded data provider library" );
371
- QgsDebugMsg ( " Attempting to resolve the classFactory function" );
372
-
373
- classFactoryFunction_t * classFactory =
374
- ( classFactoryFunction_t * ) cast_to_fptr ( myLib->resolve ( " classFactory" ) );
375
-
376
- if ( classFactory )
377
- {
378
- QgsDebugMsg ( " Getting pointer to a dataProvider object from the library" );
379
-
380
- // XXX - This was a dynamic cast but that kills the Windows
381
- // version big-time with an abnormal termination error
382
- // QgsDataProvider* dataProvider = (QgsDataProvider*)
383
- // (classFactory((const char*)(dataSource.utf8())));
384
-
385
- QgsDataProvider * dataProvider = ( *classFactory )( &dataSource );
386
-
387
- if ( dataProvider )
388
- {
389
- QgsDebugMsg ( " Instantiated the data provider plugin" );
390
- QgsDebugMsg ( " provider name: " + dataProvider->name () );
391
-
392
- delete myLib;
393
- return dataProvider;
394
- }
395
- else
396
- {
397
- QgsMessageLog::logMessage ( QObject::tr ( " Unable to instantiate the data provider plugin %1" ).arg ( lib ) );
398
-
399
- delete dataProvider;
400
-
401
- myLib->unload ();
402
- delete myLib;
403
- return 0 ;
404
- }
405
- }
322
+ QgsMessageLog::logMessage ( QObject::tr ( " Failed to load %1: %2" ).arg ( lib ).arg ( myLib.errorString () ) );
323
+ return 0 ;
406
324
}
407
- else
325
+
326
+ classFactoryFunction_t *classFactory = ( classFactoryFunction_t * ) cast_to_fptr ( myLib.resolve ( " classFactory" ) );
327
+ if ( !classFactory )
408
328
{
409
- QgsMessageLog::logMessage ( QObject::tr ( " Failed to load %1: %2" ).arg ( lib ).arg ( myLib->errorString () ) );
410
- delete myLib;
329
+ QgsDebugMsg ( QString ( " Failed to load %1: no classFactory method" ).arg ( lib ) );
411
330
return 0 ;
412
331
}
413
332
414
- QgsDebugMsg ( " exiting" );
415
-
416
- return 0 ; // factory didn't exist
333
+ QgsDataProvider *dataProvider = classFactory ( &dataSource );
334
+ if ( !dataProvider )
335
+ {
336
+ QgsMessageLog::logMessage ( QObject::tr ( " Unable to instantiate the data provider plugin %1" ).arg ( lib ) );
337
+ myLib.unload ();
338
+ return 0 ;
339
+ }
417
340
341
+ QgsDebugMsg ( QString ( " Instantiated the data provider plugin: %1" ).arg ( dataProvider->name () ) );
342
+ return dataProvider;
418
343
} // QgsProviderRegistry::setDataProvider
419
344
420
345
// This should be QWidget, not QDialog
@@ -432,44 +357,37 @@ QWidget* QgsProviderRegistry::selectWidget( const QString & providerKey,
432
357
return selectFactory ( parent, fl );
433
358
}
434
359
435
- void * QgsProviderRegistry::function ( QString const & providerKey,
436
- QString const & functionName )
360
+ void *QgsProviderRegistry::function ( QString const & providerKey,
361
+ QString const & functionName )
437
362
{
438
- QString lib = library ( providerKey );
439
-
440
- QLibrary* myLib = new QLibrary ( lib );
363
+ QLibrary myLib ( library ( providerKey ) );
441
364
442
- QgsDebugMsg ( " Library name is " + myLib-> fileName () );
365
+ QgsDebugMsg ( " Library name is " + myLib. fileName () );
443
366
444
- bool loaded = myLib->load ();
445
-
446
- if ( loaded )
367
+ if ( myLib.load () )
447
368
{
448
- void * ptr = myLib->resolve ( functionName.toAscii ().data () );
449
- delete myLib;
450
- return ptr;
369
+ return myLib.resolve ( functionName.toAscii ().data () );
370
+ }
371
+ else
372
+ {
373
+ QgsDebugMsg ( " Cannot load library: " + myLib.errorString () );
374
+ return 0 ;
451
375
}
452
- QgsDebugMsg ( " Cannot load library: " + myLib->errorString () );
453
- delete myLib;
454
- return 0 ;
455
376
}
456
377
457
378
QLibrary *QgsProviderRegistry::providerLibrary ( QString const & providerKey ) const
458
379
{
459
- QString lib = library ( providerKey );
460
-
461
- QLibrary *myLib = new QLibrary ( lib );
380
+ QLibrary *myLib = new QLibrary ( library ( providerKey ) );
462
381
463
382
QgsDebugMsg ( " Library name is " + myLib->fileName () );
464
383
465
- bool loaded = myLib->load ();
466
-
467
- if ( loaded )
468
- {
384
+ if ( myLib->load () )
469
385
return myLib;
470
- }
386
+
471
387
QgsDebugMsg ( " Cannot load library: " + myLib->errorString () );
388
+
472
389
delete myLib;
390
+
473
391
return 0 ;
474
392
}
475
393
@@ -508,7 +426,6 @@ QString QgsProviderRegistry::protocolDrivers() const
508
426
return mProtocolDrivers ;
509
427
}
510
428
511
-
512
429
QStringList QgsProviderRegistry::providerList () const
513
430
{
514
431
QStringList lst;
@@ -519,7 +436,6 @@ QStringList QgsProviderRegistry::providerList() const
519
436
return lst;
520
437
}
521
438
522
-
523
439
const QgsProviderMetadata* QgsProviderRegistry::providerMetadata ( const QString& providerKey ) const
524
440
{
525
441
return findMetadata_ ( mProviders , providerKey );
0 commit comments