Skip to content

Commit 8ac56e6

Browse files
author
wonder
committedNov 17, 2008
Allow loading only compatible plugins, i.e. plugins implementing qgisMinimumVersion() metadata.
Incompatible plugins are disabled and can't be selected in plugin manager. Example of new metadata: (plugin's __init__.py) def qgisMinimumVersion(): return "1.0" git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@9652 c8812cc2-4d05-0410-92ff-de0c093fc19c

File tree

3 files changed

+82
-9
lines changed

3 files changed

+82
-9
lines changed
 

‎src/app/qgspluginmanager.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ void QgsPluginManager::getPythonPluginDescriptions()
147147
QString version = mPythonUtils->getPluginMetadata( packageName, "version" );
148148

149149
if ( pluginName == "???" || description == "???" || version == "???" ) continue;
150+
151+
bool isCompatible = QgsPluginRegistry::instance()->isPythonPluginCompatible( packageName );
152+
QString compatibleString; // empty by default
153+
if (!isCompatible)
154+
compatibleString = " " + tr("[ incompatible ]");
150155

151156
// filtering will be done on the display role so give it name and desription
152157
// user wont see this text since we are using a custome delegate
@@ -156,9 +161,11 @@ void QgsPluginManager::getPythonPluginDescriptions()
156161
mypDetailItem->setData( packageName, PLUGIN_BASE_NAME_ROLE ); //for matching in registry later
157162
mypDetailItem->setCheckable( false );
158163
mypDetailItem->setEditable( false );
164+
mypDetailItem->setEnabled( isCompatible );
159165
// setData in the delegate with a variantised QgsDetailedItemData
160166
QgsDetailedItemData myData;
161-
myData.setTitle( pluginName + " (" + version + ")" );
167+
myData.setTitle( pluginName + " (" + version + ")" + compatibleString );
168+
myData.setEnabled( isCompatible );
162169
myData.setDetail( description );
163170
//myData.setIcon(pixmap); //todo use a python logo here
164171
myData.setCheckable( true );
@@ -476,13 +483,9 @@ void QgsPluginManager::on_vwPlugins_clicked( const QModelIndex &theIndex )
476483
QStandardItem* mypItem = mModelPlugins->itemFromIndex( realIndex );
477484
QgsDetailedItemData myData =
478485
qVariantValue<QgsDetailedItemData>( mypItem->data( PLUGIN_DATA_ROLE ) );
479-
if ( myData.isChecked() )
486+
if ( myData.isEnabled() )
480487
{
481-
myData.setChecked( false );
482-
}
483-
else
484-
{
485-
myData.setChecked( true );
488+
myData.setChecked( ! myData.isChecked() );
486489
}
487490
QVariant myVariant = qVariantFromValue( myData );
488491
mypItem->setData( myVariant, PLUGIN_DATA_ROLE );

‎src/app/qgspluginregistry.cpp

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,48 @@ void QgsPluginRegistry::unloadAll()
152152
}
153153

154154

155+
bool QgsPluginRegistry::checkQgisVersion(QString minVersion)
156+
{
157+
QStringList minVersionParts = minVersion.split('.');
158+
// qgis version must be in form x.y.z or just x.y
159+
if (minVersionParts.count() != 2 && minVersionParts.count() != 3)
160+
return false;
161+
162+
int minVerMajor, minVerMinor, minVerBugfix=0;
163+
bool ok;
164+
minVerMajor = minVersionParts.at(0).toInt(&ok);
165+
if (!ok) return false;
166+
minVerMinor = minVersionParts.at(1).toInt(&ok);
167+
if (!ok) return false;
168+
if (minVersionParts.count() == 3)
169+
{
170+
minVerBugfix = minVersionParts.at(2).toInt(&ok);
171+
if (!ok) return false;
172+
}
173+
174+
// our qgis version - cut release name after version number
175+
QString qgisVersion = QString(QGis::QGIS_VERSION).section( '-', 0, 0 );
176+
QStringList qgisVersionParts = qgisVersion.split( "." );
177+
178+
int qgisMajor = qgisVersionParts.at( 0 ).toInt();
179+
int qgisMinor = qgisVersionParts.at( 1 ).toInt();
180+
int qgisBugfix= qgisVersionParts.at( 2 ).toInt();
181+
182+
// first check major version
183+
if (minVerMajor > qgisMajor) return false;
184+
if (minVerMajor < qgisMajor) return true;
185+
186+
// if same, check minor version
187+
if (minVerMinor > qgisMinor) return false;
188+
if (minVerMinor < qgisMinor) return true;
189+
190+
// if still same, check bugfix version
191+
if (minVerBugfix > qgisBugfix) return false;
192+
193+
// looks like min version is the same as our version - that's fine
194+
return true;
195+
}
196+
155197

156198
void QgsPluginRegistry::loadPythonPlugin( QString packageName )
157199
{
@@ -160,22 +202,30 @@ void QgsPluginRegistry::loadPythonPlugin( QString packageName )
160202
QgsDebugMsg( "Python is not enabled in QGIS." );
161203
return;
162204
}
205+
206+
QSettings settings;
163207

164208
// is loaded already?
165209
if ( ! isLoaded( packageName ) )
166210
{
211+
// if plugin is not compatible, disable it
212+
if ( ! isPythonPluginCompatible( packageName ) )
213+
{
214+
settings.setValue( "/PythonPlugins/" + packageName, false );
215+
return;
216+
}
217+
167218
mPythonUtils->loadPlugin( packageName );
168219
mPythonUtils->startPlugin( packageName );
169220

170221
// TODO: test success
171-
222+
172223
QString pluginName = mPythonUtils->getPluginMetadata( packageName, "name" );
173224

174225
// add to plugin registry
175226
addPlugin( packageName, QgsPluginMetadata( packageName, pluginName, NULL, true ) );
176227

177228
// add to settings
178-
QSettings settings;
179229
settings.setValue( "/PythonPlugins/" + packageName, true );
180230
std::cout << "Loaded : " << pluginName.toLocal8Bit().constData() << " (package: "
181231
<< packageName.toLocal8Bit().constData() << ")" << std::endl; // OK
@@ -364,3 +414,17 @@ bool QgsPluginRegistry::checkPythonPlugin( QString packageName )
364414

365415
return true;
366416
}
417+
418+
bool QgsPluginRegistry::isPythonPluginCompatible( QString packageName )
419+
{
420+
QString minVersion = mPythonUtils->getPluginMetadata( packageName, "qgisMinimumVersion" );
421+
if (minVersion == "__error__" || !checkQgisVersion(minVersion))
422+
{
423+
//QMessageBox::information(mQgisInterface->mainWindow(),
424+
// QObject::tr("Incompatible plugin"),
425+
// QObject::tr("Plugin \"%1\" is not compatible with this version of Quantum GIS.\nIt will be disabled.").arg(pluginName));
426+
//settings.setValue( "/PythonPlugins/" + packageName, false );
427+
return false;
428+
}
429+
return true;
430+
}

‎src/app/qgspluginregistry.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ class QgsPluginRegistry
8181
//! Load any plugins used in the last qgis session
8282
void restoreSessionPlugins( QString thePluginDirString );
8383

84+
//! Check whether plugin is compatible with current version of QGIS
85+
bool isPythonPluginCompatible( QString packageName );
86+
8487
protected:
8588
//! protected constructor
8689
QgsPluginRegistry();
@@ -90,6 +93,9 @@ class QgsPluginRegistry
9093
//! Try to load and get metadata from Python plugin, return true on success
9194
bool checkPythonPlugin( QString packageName );
9295

96+
//! Check current QGIS version against plugin's minimal requested QGIS version
97+
bool checkQgisVersion(QString minVersion);
98+
9399
private:
94100
static QgsPluginRegistry* _instance;
95101
QMap<QString, QgsPluginMetadata> mPlugins;

0 commit comments

Comments
 (0)
Please sign in to comment.