Skip to content

Commit

Permalink
[Plugin Manager] Fix inconsistent behaviour in --noplugins mode: don'…
Browse files Browse the repository at this point in the history
…t try to load plugins, just enable/disable them in QSettings.
  • Loading branch information
borysiasty committed Jun 27, 2013
1 parent fa9b215 commit 52bb2c0
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 20 deletions.
58 changes: 46 additions & 12 deletions src/app/pluginmanager/qgspluginmanager.cpp
Expand Up @@ -52,7 +52,7 @@
#endif


QgsPluginManager::QgsPluginManager( QWidget * parent, Qt::WFlags fl )
QgsPluginManager::QgsPluginManager( QWidget * parent, bool pluginsAreEnabled, Qt::WFlags fl )
: QgsOptionsDialogBase( "PluginManager", parent, fl )
{
// initialize pointer
Expand All @@ -75,6 +75,9 @@ QgsPluginManager::QgsPluginManager( QWidget * parent, Qt::WFlags fl )
// load translated description strings from qgspluginmanager_texts
initTabDescriptions();

// set internal variable
mPluginsAreEnabled = pluginsAreEnabled;

// Init models
mModelPlugins = new QStandardItemModel( 0, 1 );
mModelProxy = new QgsPluginSortFilterProxyModel( this );
Expand Down Expand Up @@ -248,6 +251,31 @@ void QgsPluginManager::unloadPlugin( QString id )



void QgsPluginManager::savePluginState( QString id, bool state )
{
const QMap<QString, QString>* plugin = pluginMetadata( id );
if ( ! plugin )
{
return;
}

QSettings settings;
if ( plugin->value( "pythonic" ) == "true" )
{
// Python plugin
settings.setValue( "/PythonPlugins/" + id, state );
}
else
{
// C++ plugin
// Trim "cpp:" prefix from cpp plugin id
id = id.mid( 4 );
settings.setValue( "/Plugins/" + id, state );
}
}



void QgsPluginManager::getCppPluginsMetadata()
{
QString sharedLibExtension;
Expand Down Expand Up @@ -493,7 +521,7 @@ void QgsPluginManager::reloadModelData()
mypDetailItem->setCheckState( Qt::Unchecked );
}

if ( isPluginLoaded( it->value( "id" ) ) )
if ( isPluginEnabled( it->value( "id" ) ) )
{
mypDetailItem->setCheckState( Qt::Checked );
}
Expand Down Expand Up @@ -535,10 +563,20 @@ void QgsPluginManager::reloadModelData()
void QgsPluginManager::pluginItemChanged( QStandardItem * item )
{
QString id = item->data( PLUGIN_BASE_NAME_ROLE ).toString();
if ( item->checkState() && ! isPluginLoaded( id ) )

if ( item->checkState() )
{
QgsDebugMsg( " Loading plugin: " + id );
loadPlugin( id );
if ( mPluginsAreEnabled && ! isPluginEnabled( id ))
{
QgsDebugMsg( " Loading plugin: " + id );
loadPlugin( id );
}
else
{
// only enable the plugin, as we're in --noplugins mode
QgsDebugMsg( " Enabling plugin: " + id );
savePluginState( id, true );
}
}
else if ( ! item->checkState() )
{
Expand Down Expand Up @@ -1194,7 +1232,7 @@ void QgsPluginManager::on_ckbExperimental_toggled( bool state )
// PRIVATE METHODS ///////////////////////////////////////////////////////////////////


bool QgsPluginManager::isPluginLoaded( QString key )
bool QgsPluginManager::isPluginEnabled( QString key )
{
const QMap<QString, QString>* plugin = pluginMetadata( key );
if ( plugin->isEmpty() )
Expand All @@ -1203,19 +1241,15 @@ bool QgsPluginManager::isPluginLoaded( QString key )
return false;
}

QSettings mySettings;
if ( plugin->value( "pythonic" ) != "true" )
{
// For C++ plugins, just check in the QgsPluginRegistry. If the plugin is broken, it was disabled quietly.
// Trim "cpp:" prefix from cpp plugin id
key = key.mid( 4 );
QgsPluginRegistry *pRegistry = QgsPluginRegistry::instance();
return pRegistry->isLoaded( key );
return ( mySettings.value( "/Plugins/" + key, QVariant( false ) ).toBool() );
}
else
{
// For Python plugins, check in QSettings if enabled rather than checking in QgsPluginRegistry if loaded.
// This will allow to turn off the plugin if broken.
QSettings mySettings;
return ( plugin->value( "installed" ) == "true" && mySettings.value( "/PythonPlugins/" + key, QVariant( false ) ).toBool() );
}
}
Expand Down
14 changes: 10 additions & 4 deletions src/app/pluginmanager/qgspluginmanager.h
Expand Up @@ -38,8 +38,8 @@ class QgsPluginManager : public QgsOptionsDialogBase, private Ui::QgsPluginManag
{
Q_OBJECT
public:
//! Constructor
QgsPluginManager( QWidget *parent = 0, Qt::WFlags fl = QgisGui::ModalDialogFlags );
//! Constructor; set pluginsAreEnabled to false in --noplugins mode
QgsPluginManager( QWidget *parent = 0, bool pluginsAreEnabled = true, Qt::WFlags fl = QgisGui::ModalDialogFlags );

//! Destructor
~QgsPluginManager();
Expand All @@ -53,6 +53,9 @@ class QgsPluginManager : public QgsOptionsDialogBase, private Ui::QgsPluginManag
//! Unload unselected plugin
void unloadPlugin( QString id );

//! Save plugin enabled/disabled state to QSettings
void savePluginState( QString id, bool state );

//! Get metadata of C++ plugins
void getCppPluginsMetadata();

Expand Down Expand Up @@ -167,8 +170,8 @@ class QgsPluginManager : public QgsOptionsDialogBase, private Ui::QgsPluginManag
//! Load translated descriptions. Source strings implemented in external qgspluginmanager_texts.cpp
void initTabDescriptions();

//! Return true if given plugin is present in QgsPluginRegistry (c++ plugins) or is enabled in QSettings (Python plugins)
bool isPluginLoaded( QString key );
//! Return true if given plugin is enabled in QSettings
bool isPluginEnabled( QString key );

//! Return true if there are plugins available for download in the metadata registry
bool hasAvailablePlugins( );
Expand All @@ -194,6 +197,9 @@ class QgsPluginManager : public QgsOptionsDialogBase, private Ui::QgsPluginManag

QgsPythonUtils* mPythonUtils;

//! true by default; false in --noplugins mode
bool mPluginsAreEnabled;

QMap<QString, QString> mTabDescriptions;

QMap< QString, QMap< QString, QString > > mPlugins;
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgisapp.cpp
Expand Up @@ -528,7 +528,7 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, QWidget * parent,
activateDeactivateLayerRelatedActions( NULL );

// initialize the plugin manager
mPluginManager = new QgsPluginManager( this );
mPluginManager = new QgsPluginManager( this, restorePlugins );

addDockWidget( Qt::LeftDockWidgetArea, mUndoWidget );
mUndoWidget->hide();
Expand Down
5 changes: 2 additions & 3 deletions src/app/qgspluginregistry.cpp
Expand Up @@ -428,17 +428,16 @@ void QgsPluginRegistry::unloadPythonPlugin( QString packageName )

void QgsPluginRegistry::unloadCppPlugin( QString theFullPathName )
{
QSettings settings;
QString baseName = QFileInfo( theFullPathName ).baseName();
// first check to see if it's loaded
settings.setValue( "/Plugins/" + baseName, false );
if ( isLoaded( baseName ) )
{
QgisPlugin * pluginInstance = plugin( baseName );
if ( pluginInstance )
{
pluginInstance->unload();
}
QSettings settings;
settings.setValue( "/Plugins/" + baseName, false );
// remove the plugin from the registry
removePlugin( baseName );
QgsDebugMsg( "Cpp plugin successfully unloaded: " + baseName );
Expand Down

0 comments on commit 52bb2c0

Please sign in to comment.