view 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
line wrap: on
line source
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */

/*
    Sonic Visualiser
    An audio file viewer and annotation editor.
    Centre for Digital Music, Queen Mary, University of London.
    
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.  See the file
    COPYING included with this distribution for more information.
*/

#include "PluginPathSetter.h"

#include <vamp-hostsdk/PluginHostAdapter.h>

#include "RealTimePluginFactory.h"
#include "LADSPAPluginFactory.h"
#include "DSSIPluginFactory.h"

#include <QSettings>
#include <QMutexLocker>

QMutex
PluginPathSetter::m_mutex;

PluginPathSetter::Paths
PluginPathSetter::m_defaultPaths;

PluginPathSetter::Paths
PluginPathSetter::getDefaultPaths()
{
    QMutexLocker locker(&m_mutex);

    if (!m_defaultPaths.empty()) {
        return m_defaultPaths;
    }
        
    Paths paths;

    auto vampPath = Vamp::PluginHostAdapter::getPluginPath();

    QStringList qVampPath;
    for (auto s: vampPath) {
        qVampPath.push_back(QString::fromStdString(s));
    }
    paths["Vamp"] = { qVampPath, "VAMP_PATH", true };

    auto dssiPath = DSSIPluginFactory::getPluginPath();

    QStringList qDssiPath;
    for (auto s: dssiPath) {
        qDssiPath.push_back(s);
    }
    paths["DSSI"] = { qDssiPath, "DSSI_PATH", true };
            
    auto ladspaPath = LADSPAPluginFactory::getPluginPath();

    QStringList qLadspaPath;
    for (auto s: ladspaPath) {
        qLadspaPath.push_back(s);
    }
    paths["LADSPA"] = { qLadspaPath, "LADSPA_PATH", true };

    m_defaultPaths = paths;
    return m_defaultPaths;
}

PluginPathSetter::Paths
PluginPathSetter::getPaths()
{
    Paths paths = getDefaultPaths();
       
    QSettings settings;
    settings.beginGroup("Plugins");

    for (auto p: paths) {

        QString tag = p.first;

        QStringList directories =
            settings.value(QString("directories-%1").arg(tag),
                           p.second.directories)
            .toStringList();
        QString envVariable =
            settings.value(QString("env-variable-%1").arg(tag),
                           p.second.envVariable)
            .toString();
        bool useEnvVariable =
            settings.value(QString("use-env-variable-%1").arg(tag),
                           p.second.useEnvVariable)
            .toBool();
        std::string envVarStr = envVariable.toStdString();
        
        QString currentValue = qEnvironmentVariable(envVarStr.c_str());
        if (currentValue != QString() && useEnvVariable) {
            directories = currentValue.split(
#ifdef Q_OS_WIN
               ";"
#else
               ":"
#endif
                );
        }
        
        paths[tag] = { directories, envVariable, useEnvVariable };
    }

    settings.endGroup();

    return paths;
}

void
PluginPathSetter::savePathSettings(Paths paths)
{
    QSettings settings;
    settings.beginGroup("Plugins");

    for (auto p: paths) {
        QString tag = p.first;
        settings.setValue(QString("directories-%1").arg(tag),
                          p.second.directories);
        settings.setValue(QString("env-variable-%1").arg(tag),
                          p.second.envVariable);
        settings.setValue(QString("use-env-variable-%1").arg(tag),
                          p.second.useEnvVariable);
    }

    settings.endGroup();
}

void
PluginPathSetter::setEnvironmentVariables()
{
    // Set the relevant environment variables from user configuration,
    // so that later lookups through the standard APIs will follow the
    // same paths as we have in the user config

    // First ensure the default paths have been recorded for later, so
    // we don't erroneously re-read them from the environment
    // variables we've just set
    (void)getDefaultPaths();
    
    Paths paths = getPaths();

    for (auto p: paths) {
        QString envVariable = p.second.envVariable;
        std::string envVarStr = envVariable.toStdString();
        QString currentValue = qEnvironmentVariable(envVarStr.c_str());
        if (currentValue != QString() && p.second.useEnvVariable) {
            // don't override
            continue;
        }
        QString separator =
#ifdef Q_OS_WIN
            ";"
#else
            ":"
#endif
            ;
        QString proposedValue = p.second.directories.join(separator);
        qputenv(envVarStr.c_str(), proposedValue.toUtf8());
    }
}