annotate plugin/PluginPathSetter.cpp @ 1480:5ac102155409 plugin-path-config

Environment variable lookup fixes, & use this implementation throughout
author Chris Cannam
date Fri, 08 Jun 2018 11:27:40 +0100
parents f52bf66b9096
children 75fe1c1e003f
rev   line source
Chris@1472 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@1472 2
Chris@1472 3 /*
Chris@1472 4 Sonic Visualiser
Chris@1472 5 An audio file viewer and annotation editor.
Chris@1472 6 Centre for Digital Music, Queen Mary, University of London.
Chris@1472 7
Chris@1472 8 This program is free software; you can redistribute it and/or
Chris@1472 9 modify it under the terms of the GNU General Public License as
Chris@1472 10 published by the Free Software Foundation; either version 2 of the
Chris@1472 11 License, or (at your option) any later version. See the file
Chris@1472 12 COPYING included with this distribution for more information.
Chris@1472 13 */
Chris@1472 14
Chris@1472 15 #include "PluginPathSetter.h"
Chris@1472 16
Chris@1472 17 #include <vamp-hostsdk/PluginHostAdapter.h>
Chris@1472 18
Chris@1472 19 #include "RealTimePluginFactory.h"
Chris@1472 20 #include "LADSPAPluginFactory.h"
Chris@1472 21 #include "DSSIPluginFactory.h"
Chris@1472 22
Chris@1472 23 #include <QSettings>
Chris@1472 24 #include <QMutexLocker>
Chris@1472 25
Chris@1480 26 #include "system/System.h"
Chris@1480 27
Chris@1472 28 QMutex
Chris@1472 29 PluginPathSetter::m_mutex;
Chris@1472 30
Chris@1472 31 PluginPathSetter::Paths
Chris@1472 32 PluginPathSetter::m_defaultPaths;
Chris@1472 33
Chris@1472 34 PluginPathSetter::Paths
Chris@1473 35 PluginPathSetter::m_environmentPaths;
Chris@1473 36
Chris@1473 37 std::map<QString, QString>
Chris@1473 38 PluginPathSetter::m_originalEnvValues;
Chris@1473 39
Chris@1480 40 using std::string;
Chris@1480 41
Chris@1473 42 PluginPathSetter::Paths
Chris@1473 43 PluginPathSetter::getEnvironmentPathsUncached()
Chris@1472 44 {
Chris@1472 45 Paths paths;
Chris@1472 46
Chris@1472 47 auto vampPath = Vamp::PluginHostAdapter::getPluginPath();
Chris@1472 48
Chris@1472 49 QStringList qVampPath;
Chris@1472 50 for (auto s: vampPath) {
Chris@1472 51 qVampPath.push_back(QString::fromStdString(s));
Chris@1472 52 }
Chris@1472 53 paths["Vamp"] = { qVampPath, "VAMP_PATH", true };
Chris@1472 54
Chris@1472 55 auto dssiPath = DSSIPluginFactory::getPluginPath();
Chris@1472 56
Chris@1472 57 QStringList qDssiPath;
Chris@1472 58 for (auto s: dssiPath) {
Chris@1472 59 qDssiPath.push_back(s);
Chris@1472 60 }
Chris@1472 61 paths["DSSI"] = { qDssiPath, "DSSI_PATH", true };
Chris@1472 62
Chris@1472 63 auto ladspaPath = LADSPAPluginFactory::getPluginPath();
Chris@1472 64
Chris@1472 65 QStringList qLadspaPath;
Chris@1472 66 for (auto s: ladspaPath) {
Chris@1472 67 qLadspaPath.push_back(s);
Chris@1472 68 }
Chris@1472 69 paths["LADSPA"] = { qLadspaPath, "LADSPA_PATH", true };
Chris@1472 70
Chris@1473 71 return paths;
Chris@1473 72 }
Chris@1473 73
Chris@1473 74 PluginPathSetter::Paths
Chris@1473 75 PluginPathSetter::getDefaultPaths()
Chris@1473 76 {
Chris@1473 77 QMutexLocker locker(&m_mutex);
Chris@1473 78
Chris@1473 79 if (!m_defaultPaths.empty()) {
Chris@1473 80 return m_defaultPaths;
Chris@1473 81 }
Chris@1473 82
Chris@1480 83 string savedPathVamp, savedPathDssi, savedPathLadspa;
Chris@1480 84 (void)getEnvUtf8("VAMP_PATH", savedPathVamp);
Chris@1480 85 (void)getEnvUtf8("DSSI_PATH", savedPathDssi);
Chris@1480 86 (void)getEnvUtf8("LADSPA_PATH", savedPathLadspa);
Chris@1480 87
Chris@1480 88 putEnvUtf8("VAMP_PATH", "");
Chris@1480 89 putEnvUtf8("DSSI_PATH", "");
Chris@1480 90 putEnvUtf8("LADSPA_PATH", "");
Chris@1473 91
Chris@1473 92 Paths paths = getEnvironmentPathsUncached();
Chris@1473 93
Chris@1480 94 putEnvUtf8("VAMP_PATH", savedPathVamp);
Chris@1480 95 putEnvUtf8("DSSI_PATH", savedPathDssi);
Chris@1480 96 putEnvUtf8("LADSPA_PATH", savedPathLadspa);
Chris@1473 97
Chris@1472 98 m_defaultPaths = paths;
Chris@1472 99 return m_defaultPaths;
Chris@1472 100 }
Chris@1472 101
Chris@1472 102 PluginPathSetter::Paths
Chris@1473 103 PluginPathSetter::getEnvironmentPaths()
Chris@1473 104 {
Chris@1473 105 QMutexLocker locker(&m_mutex);
Chris@1473 106
Chris@1473 107 if (!m_environmentPaths.empty()) {
Chris@1473 108 return m_environmentPaths;
Chris@1473 109 }
Chris@1473 110
Chris@1473 111 m_environmentPaths = getEnvironmentPathsUncached();
Chris@1473 112 return m_environmentPaths;
Chris@1473 113 }
Chris@1473 114
Chris@1473 115 PluginPathSetter::Paths
Chris@1472 116 PluginPathSetter::getPaths()
Chris@1472 117 {
Chris@1473 118 Paths paths = getEnvironmentPaths();
Chris@1472 119
Chris@1472 120 QSettings settings;
Chris@1472 121 settings.beginGroup("Plugins");
Chris@1472 122
Chris@1472 123 for (auto p: paths) {
Chris@1472 124
Chris@1472 125 QString tag = p.first;
Chris@1472 126
Chris@1472 127 QStringList directories =
Chris@1472 128 settings.value(QString("directories-%1").arg(tag),
Chris@1472 129 p.second.directories)
Chris@1472 130 .toStringList();
Chris@1472 131 QString envVariable =
Chris@1472 132 settings.value(QString("env-variable-%1").arg(tag),
Chris@1472 133 p.second.envVariable)
Chris@1472 134 .toString();
Chris@1472 135 bool useEnvVariable =
Chris@1472 136 settings.value(QString("use-env-variable-%1").arg(tag),
Chris@1472 137 p.second.useEnvVariable)
Chris@1472 138 .toBool();
Chris@1480 139
Chris@1480 140 string envVarStr = envVariable.toStdString();
Chris@1480 141 string currentValue;
Chris@1480 142 (void)getEnvUtf8(envVarStr, currentValue);
Chris@1480 143
Chris@1480 144 if (currentValue != "" && useEnvVariable) {
Chris@1480 145 directories = QString::fromStdString(currentValue).split(
Chris@1472 146 #ifdef Q_OS_WIN
Chris@1472 147 ";"
Chris@1472 148 #else
Chris@1472 149 ":"
Chris@1472 150 #endif
Chris@1472 151 );
Chris@1472 152 }
Chris@1472 153
Chris@1472 154 paths[tag] = { directories, envVariable, useEnvVariable };
Chris@1472 155 }
Chris@1472 156
Chris@1472 157 settings.endGroup();
Chris@1472 158
Chris@1472 159 return paths;
Chris@1472 160 }
Chris@1472 161
Chris@1472 162 void
Chris@1472 163 PluginPathSetter::savePathSettings(Paths paths)
Chris@1472 164 {
Chris@1472 165 QSettings settings;
Chris@1472 166 settings.beginGroup("Plugins");
Chris@1472 167
Chris@1472 168 for (auto p: paths) {
Chris@1472 169 QString tag = p.first;
Chris@1472 170 settings.setValue(QString("directories-%1").arg(tag),
Chris@1472 171 p.second.directories);
Chris@1472 172 settings.setValue(QString("env-variable-%1").arg(tag),
Chris@1472 173 p.second.envVariable);
Chris@1472 174 settings.setValue(QString("use-env-variable-%1").arg(tag),
Chris@1472 175 p.second.useEnvVariable);
Chris@1472 176 }
Chris@1472 177
Chris@1472 178 settings.endGroup();
Chris@1472 179 }
Chris@1472 180
Chris@1473 181 QString
Chris@1473 182 PluginPathSetter::getOriginalEnvironmentValue(QString envVariable)
Chris@1473 183 {
Chris@1473 184 if (m_originalEnvValues.find(envVariable) != m_originalEnvValues.end()) {
Chris@1473 185 return m_originalEnvValues.at(envVariable);
Chris@1473 186 } else {
Chris@1473 187 return QString();
Chris@1473 188 }
Chris@1473 189 }
Chris@1473 190
Chris@1472 191 void
Chris@1473 192 PluginPathSetter::initialiseEnvironmentVariables()
Chris@1472 193 {
Chris@1472 194 // Set the relevant environment variables from user configuration,
Chris@1472 195 // so that later lookups through the standard APIs will follow the
Chris@1472 196 // same paths as we have in the user config
Chris@1472 197
Chris@1472 198 // First ensure the default paths have been recorded for later, so
Chris@1472 199 // we don't erroneously re-read them from the environment
Chris@1472 200 // variables we've just set
Chris@1472 201 (void)getDefaultPaths();
Chris@1473 202 (void)getEnvironmentPaths();
Chris@1472 203
Chris@1472 204 Paths paths = getPaths();
Chris@1472 205
Chris@1472 206 for (auto p: paths) {
Chris@1472 207 QString envVariable = p.second.envVariable;
Chris@1480 208 string envVarStr = envVariable.toStdString();
Chris@1472 209 QString currentValue = qEnvironmentVariable(envVarStr.c_str());
Chris@1473 210 m_originalEnvValues[envVariable] = currentValue;
Chris@1472 211 if (currentValue != QString() && p.second.useEnvVariable) {
Chris@1472 212 // don't override
Chris@1472 213 continue;
Chris@1472 214 }
Chris@1472 215 QString separator =
Chris@1472 216 #ifdef Q_OS_WIN
Chris@1472 217 ";"
Chris@1472 218 #else
Chris@1472 219 ":"
Chris@1472 220 #endif
Chris@1472 221 ;
Chris@1472 222 QString proposedValue = p.second.directories.join(separator);
Chris@1480 223 putEnvUtf8(envVarStr, proposedValue.toStdString());
Chris@1472 224 }
Chris@1472 225 }
Chris@1472 226