changeset 1473:f52bf66b9096 plugin-path-config

Provide means of getting the initial environment values
author Chris Cannam
date Mon, 04 Jun 2018 15:43:27 +0100
parents 5d7057af0c68
children dcff44a76573
files plugin/PluginPathSetter.cpp plugin/PluginPathSetter.h
diffstat 2 files changed, 90 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/plugin/PluginPathSetter.cpp	Fri May 25 16:04:42 2018 +0100
+++ b/plugin/PluginPathSetter.cpp	Mon Jun 04 15:43:27 2018 +0100
@@ -30,14 +30,14 @@
 PluginPathSetter::m_defaultPaths;
 
 PluginPathSetter::Paths
-PluginPathSetter::getDefaultPaths()
+PluginPathSetter::m_environmentPaths;
+
+std::map<QString, QString>
+PluginPathSetter::m_originalEnvValues;
+
+PluginPathSetter::Paths
+PluginPathSetter::getEnvironmentPathsUncached()
 {
-    QMutexLocker locker(&m_mutex);
-
-    if (!m_defaultPaths.empty()) {
-        return m_defaultPaths;
-    }
-        
     Paths paths;
 
     auto vampPath = Vamp::PluginHostAdapter::getPluginPath();
@@ -64,14 +64,53 @@
     }
     paths["LADSPA"] = { qLadspaPath, "LADSPA_PATH", true };
 
+    return paths;
+}
+
+PluginPathSetter::Paths
+PluginPathSetter::getDefaultPaths()
+{
+    QMutexLocker locker(&m_mutex);
+
+    if (!m_defaultPaths.empty()) {
+        return m_defaultPaths;
+    }
+        
+    QString savedPathVamp = qEnvironmentVariable("VAMP_PATH");
+    QString savedPathDssi = qEnvironmentVariable("DSSI_PATH");
+    QString savedPathLadspa = qEnvironmentVariable("LADSPA_PATH");
+
+    qunsetenv("VAMP_PATH");
+    qunsetenv("DSSI_PATH");
+    qunsetenv("LADSPA_PATH");
+
+    Paths paths = getEnvironmentPathsUncached();
+
+    qputenv("VAMP_PATH", savedPathVamp.toUtf8());
+    qputenv("DSSI_PATH", savedPathDssi.toUtf8());
+    qputenv("LADSPA_PATH", savedPathLadspa.toUtf8());
+
     m_defaultPaths = paths;
     return m_defaultPaths;
 }
 
 PluginPathSetter::Paths
+PluginPathSetter::getEnvironmentPaths()
+{
+    QMutexLocker locker(&m_mutex);
+
+    if (!m_environmentPaths.empty()) {
+        return m_environmentPaths;
+    }
+        
+    m_environmentPaths = getEnvironmentPathsUncached();
+    return m_environmentPaths;
+}
+
+PluginPathSetter::Paths
 PluginPathSetter::getPaths()
 {
-    Paths paths = getDefaultPaths();
+    Paths paths = getEnvironmentPaths();
        
     QSettings settings;
     settings.beginGroup("Plugins");
@@ -132,8 +171,18 @@
     settings.endGroup();
 }
 
+QString
+PluginPathSetter::getOriginalEnvironmentValue(QString envVariable)
+{
+    if (m_originalEnvValues.find(envVariable) != m_originalEnvValues.end()) {
+        return m_originalEnvValues.at(envVariable);
+    } else {
+        return QString();
+    }
+}
+
 void
-PluginPathSetter::setEnvironmentVariables()
+PluginPathSetter::initialiseEnvironmentVariables()
 {
     // Set the relevant environment variables from user configuration,
     // so that later lookups through the standard APIs will follow the
@@ -143,6 +192,7 @@
     // we don't erroneously re-read them from the environment
     // variables we've just set
     (void)getDefaultPaths();
+    (void)getEnvironmentPaths();
     
     Paths paths = getPaths();
 
@@ -150,6 +200,7 @@
         QString envVariable = p.second.envVariable;
         std::string envVarStr = envVariable.toStdString();
         QString currentValue = qEnvironmentVariable(envVarStr.c_str());
+        m_originalEnvValues[envVariable] = currentValue;
         if (currentValue != QString() && p.second.useEnvVariable) {
             // don't override
             continue;
--- a/plugin/PluginPathSetter.h	Fri May 25 16:04:42 2018 +0100
+++ b/plugin/PluginPathSetter.h	Mon Jun 04 15:43:27 2018 +0100
@@ -28,31 +28,51 @@
     typedef QString PluginTypeLabel;
 
     struct PathConfig {
-        QStringList directories;
-        QString envVariable; // e.g. "LADSPA_PATH" etc
-        bool useEnvVariable; // true if env variable overrides directories list
+        QStringList directories; // Actual list of directories arising
+                                 // from user settings, environment
+                                 // variables, and defaults as
+                                 // appropriate
+        
+        QString envVariable; // Name of env var, e.g. LADSPA_PATH
+        
+        bool useEnvVariable; // True if env variable should override
+                             // any user settings for this
     };
 
     typedef std::map<PluginTypeLabel, PathConfig> Paths;
 
-    /// Return paths arising from environment variables only, without
-    /// any user-defined preferences
+    /// Update *_PATH environment variables from the settings, on
+    /// application startup. Must be called exactly once, before any
+    /// of the other functions in this class has been called
+    static void initialiseEnvironmentVariables();
+
+    /// Return default values of paths only, without any environment
+    /// variables or user-defined preferences
     static Paths getDefaultPaths();
 
+    /// Return paths arising from environment variables only, falling
+    /// back to the defaults, without any user-defined preferences
+    static Paths getEnvironmentPaths();
+
     /// Return paths arising from user settings + environment
-    /// variables as appropriate
+    /// variables + defaults as appropriate
     static Paths getPaths();
 
     /// Save the given paths to the settings
     static void savePathSettings(Paths paths);
 
-    /// Update *_PATH environment variables from the settings, on
-    /// application startup
-    static void setEnvironmentVariables();
-
+    /// Return the original value observed on startup for the given
+    /// environment variable, if it is one of the variables used by a
+    /// known path config.
+    static QString getOriginalEnvironmentValue(QString envVariable);
+    
 private:
     static Paths m_defaultPaths;
+    static Paths m_environmentPaths;
+    static std::map<QString, QString> m_originalEnvValues;
     static QMutex m_mutex;
+
+    static Paths getEnvironmentPathsUncached();
 };
 
 #endif