Chris@66: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@66: Chris@66: /* Chris@66: Sonic Visualiser Chris@66: An audio file viewer and annotation editor. Chris@66: Centre for Digital Music, Queen Mary, University of London. Chris@66: This file copyright 2006 Chris Cannam. Chris@66: Chris@66: This program is free software; you can redistribute it and/or Chris@66: modify it under the terms of the GNU General Public License as Chris@66: published by the Free Software Foundation; either version 2 of the Chris@66: License, or (at your option) any later version. See the file Chris@66: COPYING included with this distribution for more information. Chris@66: */ Chris@66: Chris@66: #include "PluginXml.h" Chris@66: Chris@66: #include Chris@66: #include Chris@66: Chris@66: #include Chris@66: #include Chris@66: #include Chris@66: #include Chris@66: Chris@66: #include "vamp-sdk/PluginBase.h" Chris@75: #include "RealTimePluginInstance.h" Chris@66: Chris@66: #include Chris@66: Chris@66: PluginXml::PluginXml(Vamp::PluginBase *plugin) : Chris@66: m_plugin(plugin) Chris@66: { Chris@66: } Chris@66: Chris@66: PluginXml::~PluginXml() { } Chris@66: Chris@66: QString Chris@66: PluginXml::toXmlString(QString indent, QString extraAttributes) const Chris@66: { Chris@66: QString s; Chris@66: s += indent; Chris@66: Chris@66: s += QString("getName().c_str()))) Chris@66: .arg(encodeEntities(QString(m_plugin->getDescription().c_str()))) Chris@66: .arg(encodeEntities(QString(m_plugin->getMaker().c_str()))) Chris@66: .arg(m_plugin->getPluginVersion()) Chris@66: .arg(encodeEntities(QString(m_plugin->getCopyright().c_str()))) Chris@66: .arg(extraAttributes); Chris@66: Chris@66: if (!m_plugin->getPrograms().empty()) { Chris@66: s += QString("program=\"%1\" ") Chris@66: .arg(encodeEntities(m_plugin->getCurrentProgram().c_str())); Chris@66: } Chris@66: Chris@66: Vamp::PluginBase::ParameterList parameters = Chris@66: m_plugin->getParameterDescriptors(); Chris@66: Chris@66: for (Vamp::PluginBase::ParameterList::const_iterator i = parameters.begin(); Chris@66: i != parameters.end(); ++i) { Chris@66: s += QString("param-%1=\"%2\" ") Chris@66: .arg(stripInvalidParameterNameCharacters(QString(i->name.c_str()))) Chris@66: .arg(m_plugin->getParameter(i->name)); Chris@66: } Chris@66: Chris@75: RealTimePluginInstance *rtpi = Chris@75: dynamic_cast(m_plugin); Chris@75: if (rtpi) { Chris@75: std::map configurePairs = Chris@75: rtpi->getConfigurePairs(); Chris@75: QString config; Chris@75: for (std::map::iterator i = configurePairs.begin(); Chris@75: i != configurePairs.end(); ++i) { Chris@75: QString key = i->first.c_str(); Chris@75: QString value = i->second.c_str(); Chris@75: key.replace(";", "[[SEMICOLON]]"); Chris@75: key.replace("=", "[[EQUALS]]"); Chris@75: value.replace(";", "[[SEMICOLON]]"); Chris@75: value.replace("=", "[[EQUALS]]"); Chris@75: if (config != "") config += ";"; Chris@75: config += QString("%1=%2").arg(key).arg(value); Chris@75: } Chris@75: if (config != "") { Chris@75: s += QString("configuration=\"%1\" ") Chris@75: .arg(encodeEntities(config)); Chris@75: } Chris@75: } Chris@75: Chris@66: s += "/>\n"; Chris@66: return s; Chris@66: } Chris@66: Chris@66: #define CHECK_ATTRIBUTE(ATTRIBUTE, ACCESSOR) \ Chris@66: QString ATTRIBUTE = attrs.value(#ATTRIBUTE); \ Chris@66: if (ATTRIBUTE != "" && ATTRIBUTE != ACCESSOR().c_str()) { \ Chris@66: std::cerr << "WARNING: PluginXml::setParameters: Plugin " \ Chris@66: << #ATTRIBUTE << " does not match (attributes have \"" \ Chris@66: << ATTRIBUTE.toStdString() << "\", my " \ Chris@66: << #ATTRIBUTE << " is \"" << ACCESSOR() << "\")" << std::endl; \ Chris@66: } Chris@66: Chris@66: void Chris@66: PluginXml::setParameters(const QXmlAttributes &attrs) Chris@66: { Chris@66: CHECK_ATTRIBUTE(name, m_plugin->getName); Chris@66: CHECK_ATTRIBUTE(description, m_plugin->getDescription); Chris@66: CHECK_ATTRIBUTE(maker, m_plugin->getMaker); Chris@66: CHECK_ATTRIBUTE(copyright, m_plugin->getCopyright); Chris@66: Chris@66: bool ok; Chris@66: int version = attrs.value("version").trimmed().toInt(&ok); Chris@66: if (ok && version != m_plugin->getPluginVersion()) { Chris@66: std::cerr << "WARNING: PluginXml::setParameters: Plugin version does not match (attributes have " << version << ", my version is " << m_plugin->getPluginVersion() << ")" << std::endl; Chris@66: } Chris@66: Chris@75: RealTimePluginInstance *rtpi = Chris@75: dynamic_cast(m_plugin); Chris@75: if (rtpi) { Chris@75: QString config = attrs.value("configuration"); Chris@75: if (config != "") { Chris@75: QStringList configList = config.split(";"); Chris@75: for (QStringList::iterator i = configList.begin(); Chris@75: i != configList.end(); ++i) { Chris@75: QStringList kv = i->split("="); Chris@75: if (kv.count() < 2) { Chris@75: std::cerr << "WARNING: PluginXml::setParameters: Malformed configure pair string: \"" << i->toStdString() << "\"" << std::endl; Chris@75: continue; Chris@75: } Chris@75: QString key(kv[0]), value(kv[1]); Chris@75: key.replace("[[SEMICOLON]]", ";"); Chris@75: key.replace("[[EQUALS]]", ";"); Chris@75: value.replace("[[SEMICOLON]]", ";"); Chris@75: value.replace("[[SEMICOLON]]", ";"); Chris@75: rtpi->configure(key.toStdString(), value.toStdString()); Chris@75: } Chris@75: } Chris@75: } Chris@75: Chris@66: if (!m_plugin->getPrograms().empty()) { Chris@66: m_plugin->selectProgram(attrs.value("program").toStdString()); Chris@66: } Chris@66: Chris@66: Vamp::PluginBase::ParameterList parameters = Chris@66: m_plugin->getParameterDescriptors(); Chris@66: Chris@66: for (Vamp::PluginBase::ParameterList::const_iterator i = Chris@66: parameters.begin(); i != parameters.end(); ++i) { Chris@66: Chris@66: QString name = QString("param-%1") Chris@66: .arg(stripInvalidParameterNameCharacters Chris@66: (QString(i->name.c_str()))); Chris@66: Chris@66: if (attrs.value(name) == "") { Chris@66: std::cerr << "PluginXml::setParameters: no parameter \"" << i->name << "\" (attribute \"" << name.toStdString() << "\")" << std::endl; Chris@66: continue; Chris@66: } Chris@66: Chris@66: bool ok; Chris@66: float value = attrs.value(name).trimmed().toFloat(&ok); Chris@66: if (ok) { Chris@66: m_plugin->setParameter(i->name, value); Chris@66: } else { Chris@66: std::cerr << "WARNING: PluginXml::setParameters: Invalid value \"" << attrs.value(name).toStdString() << "\" for parameter \"" << i->name << "\" (attribute \"" << name.toStdString() << "\")" << std::endl; Chris@66: } Chris@66: } Chris@66: } Chris@66: Chris@66: void Chris@66: PluginXml::setParametersFromXml(QString xml) Chris@66: { Chris@66: QDomDocument doc; Chris@66: Chris@66: QString error; Chris@66: int errorLine; Chris@66: int errorColumn; Chris@66: Chris@66: if (!doc.setContent(xml, false, &error, &errorLine, &errorColumn)) { Chris@66: std::cerr << "PluginXml::setParametersFromXml: Error in parsing XML: " << error.toStdString() << " at line " << errorLine << ", column " << errorColumn << std::endl; Chris@66: std::cerr << "Input follows:" << std::endl; Chris@66: std::cerr << xml.toStdString() << std::endl; Chris@66: std::cerr << "Input ends." << std::endl; Chris@66: return; Chris@66: } Chris@66: Chris@66: QDomElement pluginElt = doc.firstChildElement("plugin"); Chris@66: QDomNamedNodeMap attrNodes = pluginElt.attributes(); Chris@66: QXmlAttributes attrs; Chris@66: Chris@66: for (unsigned int i = 0; i < attrNodes.length(); ++i) { Chris@66: QDomAttr attr = attrNodes.item(i).toAttr(); Chris@66: if (attr.isNull()) continue; Chris@66: std::cerr << "Adding attribute \"" << attr.name().toStdString() Chris@66: << "\" with value \"" << attr.value().toStdString() << "\"" << std::endl; Chris@66: attrs.append(attr.name(), "", "", attr.value()); Chris@66: } Chris@66: Chris@66: setParameters(attrs); Chris@66: } Chris@66: Chris@66: QString Chris@66: PluginXml::stripInvalidParameterNameCharacters(QString s) const Chris@66: { Chris@66: s.replace(QRegExp("[^a-zA-Z0-9_]*"), ""); Chris@66: return s; Chris@66: } Chris@66: