comparison plugin/PluginPathSetter.cpp @ 1472:5d7057af0c68 plugin-path-config

Add class to get/set/remember the plugin path for each plugin type
author Chris Cannam
date Fri, 25 May 2018 16:04:42 +0100
parents
children f52bf66b9096
comparison
equal deleted inserted replaced
1471:85e9b7b31a8d 1472:5d7057af0c68
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Sonic Visualiser
5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London.
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version. See the file
12 COPYING included with this distribution for more information.
13 */
14
15 #include "PluginPathSetter.h"
16
17 #include <vamp-hostsdk/PluginHostAdapter.h>
18
19 #include "RealTimePluginFactory.h"
20 #include "LADSPAPluginFactory.h"
21 #include "DSSIPluginFactory.h"
22
23 #include <QSettings>
24 #include <QMutexLocker>
25
26 QMutex
27 PluginPathSetter::m_mutex;
28
29 PluginPathSetter::Paths
30 PluginPathSetter::m_defaultPaths;
31
32 PluginPathSetter::Paths
33 PluginPathSetter::getDefaultPaths()
34 {
35 QMutexLocker locker(&m_mutex);
36
37 if (!m_defaultPaths.empty()) {
38 return m_defaultPaths;
39 }
40
41 Paths paths;
42
43 auto vampPath = Vamp::PluginHostAdapter::getPluginPath();
44
45 QStringList qVampPath;
46 for (auto s: vampPath) {
47 qVampPath.push_back(QString::fromStdString(s));
48 }
49 paths["Vamp"] = { qVampPath, "VAMP_PATH", true };
50
51 auto dssiPath = DSSIPluginFactory::getPluginPath();
52
53 QStringList qDssiPath;
54 for (auto s: dssiPath) {
55 qDssiPath.push_back(s);
56 }
57 paths["DSSI"] = { qDssiPath, "DSSI_PATH", true };
58
59 auto ladspaPath = LADSPAPluginFactory::getPluginPath();
60
61 QStringList qLadspaPath;
62 for (auto s: ladspaPath) {
63 qLadspaPath.push_back(s);
64 }
65 paths["LADSPA"] = { qLadspaPath, "LADSPA_PATH", true };
66
67 m_defaultPaths = paths;
68 return m_defaultPaths;
69 }
70
71 PluginPathSetter::Paths
72 PluginPathSetter::getPaths()
73 {
74 Paths paths = getDefaultPaths();
75
76 QSettings settings;
77 settings.beginGroup("Plugins");
78
79 for (auto p: paths) {
80
81 QString tag = p.first;
82
83 QStringList directories =
84 settings.value(QString("directories-%1").arg(tag),
85 p.second.directories)
86 .toStringList();
87 QString envVariable =
88 settings.value(QString("env-variable-%1").arg(tag),
89 p.second.envVariable)
90 .toString();
91 bool useEnvVariable =
92 settings.value(QString("use-env-variable-%1").arg(tag),
93 p.second.useEnvVariable)
94 .toBool();
95 std::string envVarStr = envVariable.toStdString();
96
97 QString currentValue = qEnvironmentVariable(envVarStr.c_str());
98 if (currentValue != QString() && useEnvVariable) {
99 directories = currentValue.split(
100 #ifdef Q_OS_WIN
101 ";"
102 #else
103 ":"
104 #endif
105 );
106 }
107
108 paths[tag] = { directories, envVariable, useEnvVariable };
109 }
110
111 settings.endGroup();
112
113 return paths;
114 }
115
116 void
117 PluginPathSetter::savePathSettings(Paths paths)
118 {
119 QSettings settings;
120 settings.beginGroup("Plugins");
121
122 for (auto p: paths) {
123 QString tag = p.first;
124 settings.setValue(QString("directories-%1").arg(tag),
125 p.second.directories);
126 settings.setValue(QString("env-variable-%1").arg(tag),
127 p.second.envVariable);
128 settings.setValue(QString("use-env-variable-%1").arg(tag),
129 p.second.useEnvVariable);
130 }
131
132 settings.endGroup();
133 }
134
135 void
136 PluginPathSetter::setEnvironmentVariables()
137 {
138 // Set the relevant environment variables from user configuration,
139 // so that later lookups through the standard APIs will follow the
140 // same paths as we have in the user config
141
142 // First ensure the default paths have been recorded for later, so
143 // we don't erroneously re-read them from the environment
144 // variables we've just set
145 (void)getDefaultPaths();
146
147 Paths paths = getPaths();
148
149 for (auto p: paths) {
150 QString envVariable = p.second.envVariable;
151 std::string envVarStr = envVariable.toStdString();
152 QString currentValue = qEnvironmentVariable(envVarStr.c_str());
153 if (currentValue != QString() && p.second.useEnvVariable) {
154 // don't override
155 continue;
156 }
157 QString separator =
158 #ifdef Q_OS_WIN
159 ";"
160 #else
161 ":"
162 #endif
163 ;
164 QString proposedValue = p.second.directories.join(separator);
165 qputenv(envVarStr.c_str(), proposedValue.toUtf8());
166 }
167 }
168