Skip to content

Commit 1c2c079

Browse files
committedJun 2, 2013
else cascade cleanups
1 parent 81ff2a3 commit 1c2c079

File tree

3 files changed

+247
-351
lines changed

3 files changed

+247
-351
lines changed
 

‎src/app/qgspluginmanager.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -475,14 +475,11 @@ void QgsPluginManager::unload()
475475

476476
if ( pRegistry->isPythonPlugin( baseName ) )
477477
{
478-
if ( mPythonUtils && mPythonUtils->isEnabled() )
478+
if ( mPythonUtils && mPythonUtils->isEnabled() && mPythonUtils->canUninstallPlugin( baseName ) )
479479
{
480-
if ( mPythonUtils->canUninstallPlugin( baseName ) )
481-
{
482-
mPythonUtils->unloadPlugin( baseName );
483-
//disable it to the qsettings file
484-
settings.setValue( "/PythonPlugins/" + baseName, false );
485-
}
480+
mPythonUtils->unloadPlugin( baseName );
481+
//disable it to the qsettings file
482+
settings.setValue( "/PythonPlugins/" + baseName, false );
486483
}
487484
}
488485
else // C++ plugin
@@ -537,8 +534,8 @@ std::vector < QgsPluginItem > QgsPluginManager::getSelectedPlugins()
537534
// bool python=false);
538535
pis.push_back( QgsPluginItem( pluginName, library, 0, pythonic ) );
539536
}
540-
541537
}
538+
542539
return pis;
543540
}
544541

‎src/app/qgspluginregistry.cpp

Lines changed: 97 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ QgisPlugin *QgsPluginRegistry::plugin( QString key )
100100
{
101101
QMap<QString, QgsPluginMetadata>::iterator it = mPlugins.find( key );
102102
if ( it == mPlugins.end() )
103-
return NULL;
103+
return 0;
104104

105105
// note: not used by python plugins
106106

@@ -109,12 +109,7 @@ QgisPlugin *QgsPluginRegistry::plugin( QString key )
109109

110110
bool QgsPluginRegistry::isPythonPlugin( QString key )
111111
{
112-
if ( mPythonUtils && mPythonUtils->isEnabled() )
113-
{
114-
if ( mPythonUtils->isPluginLoaded( key ) )
115-
return true;
116-
}
117-
return false;
112+
return mPythonUtils && mPythonUtils->isEnabled() && mPythonUtils->isPluginLoaded( key );
118113
}
119114

120115
void QgsPluginRegistry::addPlugin( QString key, QgsPluginMetadata metadata )
@@ -272,30 +267,20 @@ void QgsPluginRegistry::loadPythonPlugin( QString packageName )
272267

273268
void QgsPluginRegistry::loadCppPlugin( QString theFullPathName )
274269
{
275-
QSettings settings;
276-
277-
QString baseName = QFileInfo( theFullPathName ).baseName();
278-
279270
// first check to see if its already loaded
271+
QString baseName = QFileInfo( theFullPathName ).baseName();
280272
if ( isLoaded( baseName ) )
281-
{
282-
// plugin is loaded
283-
// QMessageBox::warning(this, "Already Loaded", description + " is already loaded");
284273
return;
285-
}
286274

287275
QLibrary myLib( theFullPathName );
288-
289-
QString myError; //we will only show detailed diagnostics if something went wrong
290-
myError += QObject::tr( "Library name is %1\n" ).arg( myLib.fileName() );
291-
292-
bool loaded = myLib.load();
293-
if ( !loaded )
276+
if ( !myLib.load() )
294277
{
295278
QgsMessageLog::logMessage( QObject::tr( "Failed to load %1 (Reason: %2)" ).arg( myLib.fileName() ).arg( myLib.errorString() ), QObject::tr( "Plugins" ) );
296279
return;
297280
}
298281

282+
QString myError( QObject::tr( "Library name is %1\n" ).arg( myLib.fileName() ) );
283+
299284
myError += QObject::tr( "Attempting to resolve the classFactory function\n" );
300285

301286
type_t *pType = ( type_t * ) cast_to_fptr( myLib.resolve( "type" ) );
@@ -309,59 +294,57 @@ void QgsPluginRegistry::loadCppPlugin( QString theFullPathName )
309294
{
310295
// UI only -- doesn't use mapcanvas
311296
create_ui *cf = ( create_ui * ) cast_to_fptr( myLib.resolve( "classFactory" ) );
312-
if ( cf )
297+
if ( !cf )
298+
{
299+
QgsMessageLog::logMessage( QObject::tr( "Unable to find the class factory for %1." ).arg( theFullPathName ), QObject::tr( "Plugins" ) );
300+
break;
301+
}
302+
303+
QSettings settings;
304+
QgisPlugin *pl = cf( mQgisInterface );
305+
if ( !pl )
306+
{
307+
// something went wrong
308+
QMessageBox::warning( mQgisInterface->mainWindow(), QObject::tr( "Error Loading Plugin" ),
309+
QObject::tr( "There was an error loading a plugin."
310+
"The following diagnostic information may help the QGIS developers resolve the issue:\n%1." )
311+
.arg( myError ) );
312+
//disable it to the qsettings file [ts]
313+
settings.setValue( "/Plugins/" + baseName, false );
314+
break;
315+
}
316+
317+
pl->initGui();
318+
// add it to the plugin registry
319+
addPlugin( baseName, QgsPluginMetadata( myLib.fileName(), pName(), pl ) );
320+
//add it to the qsettings file [ts]
321+
settings.setValue( "/Plugins/" + baseName, true );
322+
QgsMessageLog::logMessage( QObject::tr( "Loaded %1 (Path: %2)" ).arg( pName() ).arg( myLib.fileName() ), QObject::tr( "Plugins" ), QgsMessageLog::INFO );
323+
324+
QObject *o = dynamic_cast<QObject *>( pl );
325+
if ( !o )
326+
break;
327+
328+
QgsDebugMsg( QString( "plugin object name: %1" ).arg( o->objectName() ) );
329+
if ( o->objectName().isEmpty() )
313330
{
314-
QgisPlugin *pl = cf( mQgisInterface );
315-
if ( pl )
316-
{
317-
pl->initGui();
318-
// add it to the plugin registry
319-
addPlugin( baseName, QgsPluginMetadata( myLib.fileName(), pName(), pl ) );
320-
//add it to the qsettings file [ts]
321-
settings.setValue( "/Plugins/" + baseName, true );
322-
QgsMessageLog::logMessage( QObject::tr( "Loaded %1 (Path: %2)" ).arg( pName() ).arg( myLib.fileName() ), QObject::tr( "Plugins" ), QgsMessageLog::INFO );
323-
324-
QObject *o = dynamic_cast<QObject *>( pl );
325-
if ( o )
326-
{
327-
QgsDebugMsg( QString( "plugin object name: %1" ).arg( o->objectName() ) );
328-
if ( o->objectName().isEmpty() )
329-
{
330331
#ifndef WIN32
331-
baseName = baseName.mid( 3 );
332+
baseName = baseName.mid( 3 );
332333
#endif
333-
QgsDebugMsg( QString( "object name to %1" ).arg( baseName ) );
334-
o->setObjectName( QString( "qgis_plugin_%1" ).arg( baseName ) );
335-
QgsDebugMsg( QString( "plugin object name now: %1" ).arg( o->objectName() ) );
336-
}
337-
338-
if ( !o->parent() )
339-
{
340-
QgsDebugMsg( QString( "setting plugin parent" ) );
341-
o->setParent( QgisApp::instance() );
342-
}
343-
else
344-
{
345-
QgsDebugMsg( QString( "plugin parent already set" ) );
346-
}
347-
}
348-
}
349-
else
350-
{
351-
// something went wrong
352-
QMessageBox::warning( mQgisInterface->mainWindow(), QObject::tr( "Error Loading Plugin" ),
353-
QObject::tr( "There was an error loading a plugin."
354-
"The following diagnostic information may help the QGIS developers resolve the issue:\n%1." )
355-
.arg( myError ) );
356-
//disable it to the qsettings file [ts]
357-
settings.setValue( "/Plugins/" + baseName, false );
358-
}
334+
QgsDebugMsg( QString( "object name to %1" ).arg( baseName ) );
335+
o->setObjectName( QString( "qgis_plugin_%1" ).arg( baseName ) );
336+
QgsDebugMsg( QString( "plugin object name now: %1" ).arg( o->objectName() ) );
337+
}
338+
339+
if ( !o->parent() )
340+
{
341+
QgsDebugMsg( QString( "setting plugin parent" ) );
342+
o->setParent( QgisApp::instance() );
359343
}
360344
else
361345
{
362-
QgsMessageLog::logMessage( QObject::tr( "Unable to find the class factory for %1." ).arg( theFullPathName ), QObject::tr( "Plugins" ) );
346+
QgsDebugMsg( QString( "plugin parent already set" ) );
363347
}
364-
365348
}
366349
break;
367350
default:
@@ -411,73 +394,73 @@ void QgsPluginRegistry::restoreSessionPlugins( QString thePluginDirString )
411394
}
412395
}
413396

414-
if ( mPythonUtils && mPythonUtils->isEnabled() )
397+
if ( !mPythonUtils || !mPythonUtils->isEnabled() )
398+
return;
399+
400+
// check for python plugins system-wide
401+
QStringList pluginList = mPythonUtils->pluginList();
402+
QgsDebugMsg( "Loading python plugins" );
403+
404+
QStringList corePlugins = QStringList();
405+
corePlugins << "plugin_installer";
406+
corePlugins << "fTools";
407+
corePlugins << "GdalTools";
408+
corePlugins << "db_manager";
409+
410+
// make the required core plugins enabled by default:
411+
for ( int i = 0; i < corePlugins.size(); i++ )
415412
{
416-
// check for python plugins system-wide
417-
QStringList pluginList = mPythonUtils->pluginList();
418-
QgsDebugMsg( "Loading python plugins" );
419-
420-
QStringList corePlugins = QStringList();
421-
corePlugins << "plugin_installer";
422-
corePlugins << "fTools";
423-
corePlugins << "GdalTools";
424-
corePlugins << "db_manager";
425-
426-
// make the required core plugins enabled by default:
427-
for ( int i = 0; i < corePlugins.size(); i++ )
413+
if ( !mySettings.contains( "/PythonPlugins/" + corePlugins[i] ) )
428414
{
429-
if ( !mySettings.contains( "/PythonPlugins/" + corePlugins[i] ) )
430-
{
431-
mySettings.setValue( "/PythonPlugins/" + corePlugins[i], true );
432-
}
415+
mySettings.setValue( "/PythonPlugins/" + corePlugins[i], true );
433416
}
417+
}
434418

435-
for ( int i = 0; i < pluginList.size(); i++ )
436-
{
437-
QString packageName = pluginList[i];
419+
for ( int i = 0; i < pluginList.size(); i++ )
420+
{
421+
QString packageName = pluginList[i];
438422

439-
// TODO: apply better solution for #5879
440-
// start - temporary fix for issue #5879
441-
if ( QgsApplication::isRunningFromBuildDir() )
423+
// TODO: apply better solution for #5879
424+
// start - temporary fix for issue #5879
425+
if ( QgsApplication::isRunningFromBuildDir() )
426+
{
427+
if ( corePlugins.contains( packageName ) )
442428
{
443-
if ( corePlugins.contains( packageName ) )
444-
{
445-
QgsApplication::setPkgDataPath( QString( "" ) );
446-
}
447-
else
448-
{
449-
QgsApplication::setPkgDataPath( QgsApplication::buildSourcePath() );
450-
}
429+
QgsApplication::setPkgDataPath( QString( "" ) );
451430
}
452-
// end - temporary fix for issue #5879, more below
453-
454-
if ( checkPythonPlugin( packageName ) )
431+
else
455432
{
456-
// check if the plugin was active on last session
457-
458-
if ( mySettings.value( "/PythonPlugins/" + packageName ).toBool() )
459-
{
460-
loadPythonPlugin( packageName );
461-
}
433+
QgsApplication::setPkgDataPath( QgsApplication::buildSourcePath() );
462434
}
463435
}
464-
// start - temporary fix for issue #5879, more above
465-
if ( QgsApplication::isRunningFromBuildDir() )
436+
// end - temporary fix for issue #5879, more below
437+
438+
if ( checkPythonPlugin( packageName ) )
466439
{
467-
QgsApplication::setPkgDataPath( QgsApplication::buildSourcePath() );
440+
// check if the plugin was active on last session
441+
442+
if ( mySettings.value( "/PythonPlugins/" + packageName ).toBool() )
443+
{
444+
loadPythonPlugin( packageName );
445+
}
468446
}
469-
// end - temporary fix for issue #5879
470447
}
471448

449+
// start - temporary fix for issue #5879, more above
450+
if ( QgsApplication::isRunningFromBuildDir() )
451+
{
452+
QgsApplication::setPkgDataPath( QgsApplication::buildSourcePath() );
453+
}
454+
// end - temporary fix for issue #5879
455+
472456
QgsDebugMsg( "Plugin loading completed" );
473457
}
474458

475459

476460
bool QgsPluginRegistry::checkCppPlugin( QString pluginFullPath )
477461
{
478462
QLibrary myLib( pluginFullPath );
479-
bool loaded = myLib.load();
480-
if ( ! loaded )
463+
if ( !myLib.load() )
481464
{
482465
QgsMessageLog::logMessage( QObject::tr( "Failed to load %1 (Reason: %2)" ).arg( myLib.fileName() ).arg( myLib.errorString() ), QObject::tr( "Plugins" ) );
483466
return false;
@@ -488,7 +471,7 @@ bool QgsPluginRegistry::checkCppPlugin( QString pluginFullPath )
488471
category_t * myCategory = ( category_t * ) cast_to_fptr( myLib.resolve( "category" ) );
489472
version_t * myVersion = ( version_t * ) cast_to_fptr( myLib.resolve( "version" ) );
490473

491-
if ( myName && myDescription && myVersion && myCategory )
474+
if ( myName && myDescription && myVersion && myCategory )
492475
return true;
493476

494477
QgsDebugMsg( "Failed to get name, description, category or type for " + myLib.fileName() );

‎src/core/qgsproviderregistry.cpp

Lines changed: 145 additions & 229 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include <QDir>
2323
#include <QLibrary>
2424

25-
2625
#include "qgis.h"
2726
#include "qgsdataprovider.h"
2827
#include "qgslogger.h"
@@ -65,13 +64,14 @@ QgsProviderRegistry::QgsProviderRegistry( QString pluginPath )
6564
// check all libs in the current plugin directory and get name and descriptions
6665
//TODO figure out how to register and identify data source plugin for a specific
6766
//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
7374
mLibraryDirectory = pluginPath;
74-
7575
mLibraryDirectory.setSorting( QDir::Name | QDir::IgnoreCase );
7676
mLibraryDirectory.setFilter( QDir::Files | QDir::NoSymLinks );
7777

@@ -94,134 +94,96 @@ QgsProviderRegistry::QgsProviderRegistry( QString pluginPath )
9494
output->setTitle( QObject::tr( "No Data Providers" ) );
9595
output->setMessage( msg, QgsMessageOutput::MessageText );
9696
output->showMessage();
97+
return;
9798
}
98-
else
99+
100+
QListIterator<QFileInfo> it( mLibraryDirectory.entryInfoList() );
101+
while ( it.hasNext() )
99102
{
100-
const QFileInfoList list = mLibraryDirectory.entryInfoList();
101-
QListIterator<QFileInfo> it( list );
102-
QFileInfo fi;
103+
QFileInfo fi( it.next() );
103104

104-
while ( it.hasNext() )
105+
QLibrary myLib( fi.filePath() );
106+
if ( !myLib.load() )
105107
{
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;
222110
}
223-
}
224111

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+
}
225187
} // QgsProviderRegistry ctor
226188

227189

@@ -270,39 +232,33 @@ QString QgsProviderRegistry::library( QString const & providerKey ) const
270232
QString QgsProviderRegistry::pluginList( bool asHTML ) const
271233
{
272234
Providers::const_iterator it = mProviders.begin();
273-
QString list;
274235

275236
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() )
280245
{
281246
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+
300251
if ( asHTML )
301-
{
302-
list += "</ol>";
303-
}
252+
list + "<br></li>";
253+
else
254+
list += "\n";
255+
256+
it++;
304257
}
305258

259+
if ( asHTML )
260+
list += "</ol>";
261+
306262
return list;
307263
}
308264

@@ -357,64 +313,33 @@ QgsDataProvider *QgsProviderRegistry::provider( QString const & providerKey, QSt
357313
}
358314

359315
#endif
360-
361316
// 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 );
367318

368-
if ( loaded )
319+
QgsDebugMsg( "Library name is " + myLib.fileName() );
320+
if ( !myLib.load() )
369321
{
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;
406324
}
407-
else
325+
326+
classFactoryFunction_t *classFactory = ( classFactoryFunction_t * ) cast_to_fptr( myLib.resolve( "classFactory" ) );
327+
if ( !classFactory )
408328
{
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 ) );
411330
return 0;
412331
}
413332

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+
}
417340

341+
QgsDebugMsg( QString( "Instantiated the data provider plugin: %1" ).arg( dataProvider->name() ) );
342+
return dataProvider;
418343
} // QgsProviderRegistry::setDataProvider
419344

420345
// This should be QWidget, not QDialog
@@ -432,44 +357,37 @@ QWidget* QgsProviderRegistry::selectWidget( const QString & providerKey,
432357
return selectFactory( parent, fl );
433358
}
434359

435-
void * QgsProviderRegistry::function( QString const & providerKey,
436-
QString const & functionName )
360+
void *QgsProviderRegistry::function( QString const & providerKey,
361+
QString const & functionName )
437362
{
438-
QString lib = library( providerKey );
439-
440-
QLibrary* myLib = new QLibrary( lib );
363+
QLibrary myLib( library( providerKey ) );
441364

442-
QgsDebugMsg( "Library name is " + myLib->fileName() );
365+
QgsDebugMsg( "Library name is " + myLib.fileName() );
443366

444-
bool loaded = myLib->load();
445-
446-
if ( loaded )
367+
if ( myLib.load() )
447368
{
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;
451375
}
452-
QgsDebugMsg( "Cannot load library: " + myLib->errorString() );
453-
delete myLib;
454-
return 0;
455376
}
456377

457378
QLibrary *QgsProviderRegistry::providerLibrary( QString const & providerKey ) const
458379
{
459-
QString lib = library( providerKey );
460-
461-
QLibrary *myLib = new QLibrary( lib );
380+
QLibrary *myLib = new QLibrary( library( providerKey ) );
462381

463382
QgsDebugMsg( "Library name is " + myLib->fileName() );
464383

465-
bool loaded = myLib->load();
466-
467-
if ( loaded )
468-
{
384+
if ( myLib->load() )
469385
return myLib;
470-
}
386+
471387
QgsDebugMsg( "Cannot load library: " + myLib->errorString() );
388+
472389
delete myLib;
390+
473391
return 0;
474392
}
475393

@@ -508,7 +426,6 @@ QString QgsProviderRegistry::protocolDrivers() const
508426
return mProtocolDrivers;
509427
}
510428

511-
512429
QStringList QgsProviderRegistry::providerList() const
513430
{
514431
QStringList lst;
@@ -519,7 +436,6 @@ QStringList QgsProviderRegistry::providerList() const
519436
return lst;
520437
}
521438

522-
523439
const QgsProviderMetadata* QgsProviderRegistry::providerMetadata( const QString& providerKey ) const
524440
{
525441
return findMetadata_( mProviders, providerKey );

0 commit comments

Comments
 (0)
Please sign in to comment.