annotate plugin/PluginScan.cpp @ 1235:7f4230e7d83a project-file-rework

Error handling
author Chris Cannam
date Thu, 27 Oct 2016 16:13:25 +0100
parents c0cec4659784
children c6bdf247016a
rev   line source
Chris@1178 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@1178 2
Chris@1178 3 /*
Chris@1178 4 Sonic Visualiser
Chris@1178 5 An audio file viewer and annotation editor.
Chris@1178 6 Centre for Digital Music, Queen Mary, University of London.
Chris@1178 7
Chris@1178 8 This program is free software; you can redistribute it and/or
Chris@1178 9 modify it under the terms of the GNU General Public License as
Chris@1178 10 published by the Free Software Foundation; either version 2 of the
Chris@1178 11 License, or (at your option) any later version. See the file
Chris@1178 12 COPYING included with this distribution for more information.
Chris@1178 13 */
Chris@1178 14
Chris@1178 15 #include "PluginScan.h"
Chris@1178 16
Chris@1178 17 #include "base/Debug.h"
Chris@1178 18
Chris@1180 19 #include "checker/knownplugins.h"
Chris@1180 20
Chris@1178 21 #include <QMutex>
Chris@1181 22 #include <QCoreApplication>
Chris@1178 23
Chris@1178 24 using std::string;
Chris@1178 25
cannam@1231 26 //#define DEBUG_PLUGIN_SCAN 1
cannam@1231 27
Chris@1180 28 class PluginScan::Logger : public PluginCandidates::LogCallback
Chris@1180 29 {
Chris@1180 30 protected:
Chris@1180 31 void log(std::string message) {
cannam@1231 32 #ifdef DEBUG_PLUGIN_SCAN
cannam@1231 33 cerr << "PluginScan: " << message;
cannam@1231 34 #endif
Chris@1180 35 SVDEBUG << "PluginScan: " << message;
Chris@1180 36 }
Chris@1180 37 };
Chris@1180 38
Chris@1180 39 PluginScan *PluginScan::getInstance()
Chris@1180 40 {
Chris@1178 41 static QMutex mutex;
Chris@1178 42 static PluginScan *m_instance = 0;
Chris@1178 43 mutex.lock();
Chris@1178 44 if (!m_instance) m_instance = new PluginScan();
Chris@1178 45 mutex.unlock();
Chris@1178 46 return m_instance;
Chris@1178 47 }
Chris@1178 48
Chris@1180 49 PluginScan::PluginScan() : m_kp(0), m_succeeded(false), m_logger(new Logger) {
Chris@1178 50 }
Chris@1178 51
Chris@1178 52 PluginScan::~PluginScan() {
Chris@1178 53 delete m_kp;
Chris@1180 54 delete m_logger;
Chris@1178 55 }
Chris@1178 56
Chris@1178 57 void
Chris@1181 58 PluginScan::scan(QString helperExecutablePath)
Chris@1178 59 {
Chris@1178 60 delete m_kp;
Chris@1179 61 m_succeeded = false;
Chris@1179 62 try {
Chris@1181 63 m_kp = new KnownPlugins(helperExecutablePath.toStdString(), m_logger);
Chris@1179 64 m_succeeded = true;
Chris@1179 65 } catch (const std::exception &e) {
Chris@1179 66 cerr << "ERROR: PluginScan::scan: " << e.what() << endl;
Chris@1179 67 m_kp = 0;
Chris@1179 68 }
Chris@1179 69 }
Chris@1179 70
Chris@1179 71 QStringList
Chris@1180 72 PluginScan::getCandidateLibrariesFor(PluginType type) const
Chris@1179 73 {
Chris@1180 74 KnownPlugins::PluginType kpt;
Chris@1180 75 switch (type) {
Chris@1180 76 case VampPlugin: kpt = KnownPlugins::VampPlugin; break;
Chris@1180 77 case LADSPAPlugin: kpt = KnownPlugins::LADSPAPlugin; break;
Chris@1180 78 case DSSIPlugin: kpt = KnownPlugins::DSSIPlugin; break;
Chris@1180 79 default: throw std::logic_error("Inconsistency in plugin type enums");
Chris@1180 80 }
Chris@1180 81
Chris@1179 82 QStringList candidates;
Chris@1179 83 if (!m_kp) return candidates;
Chris@1180 84 auto c = m_kp->getCandidateLibrariesFor(kpt);
Chris@1179 85 for (auto s: c) candidates.push_back(s.c_str());
Chris@1179 86 return candidates;
Chris@1178 87 }
Chris@1178 88
Chris@1178 89 QString
Chris@1178 90 PluginScan::getStartupFailureReport() const
Chris@1178 91 {
Chris@1179 92 if (!m_succeeded) {
Chris@1179 93 return QObject::tr("<b>Failed to scan for plugins</b>"
Chris@1181 94 "<p>Failed to scan for plugins at startup. Possibly "
Chris@1181 95 "the plugin checker helper program was not correctly "
Chris@1181 96 "installed alongside %1?</p>")
Chris@1181 97 .arg(QCoreApplication::applicationName());
Chris@1179 98 }
Chris@1179 99 if (!m_kp) {
Chris@1179 100 return QObject::tr("<b>Did not scan for plugins</b>"
Chris@1179 101 "<p>Apparently no scan for plugins was attempted "
Chris@1179 102 "(internal error?)</p>");
Chris@1179 103 }
Chris@1179 104
Chris@1178 105 string report = m_kp->getFailureReport();
Chris@1179 106 if (report == "") {
Chris@1179 107 return QString(report.c_str());
Chris@1179 108 }
Chris@1179 109
Chris@1179 110 return QObject::tr("<b>Failed to load plugins</b>"
Chris@1179 111 "<p>Failed to load one or more plugin libraries:</p>")
Chris@1181 112 + QString(report.c_str())
Chris@1181 113 + QObject::tr("<p>These plugins may be incompatible with the system, "
Chris@1181 114 "and will be ignored during this run of %1.</p>")
Chris@1181 115 .arg(QCoreApplication::applicationName());
Chris@1178 116 }
Chris@1178 117