1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
2 /*
3     Copyright (c) 2016-2018 Queen Mary, University of London
4 
5     Permission is hereby granted, free of charge, to any person
6     obtaining a copy of this software and associated documentation
7     files (the "Software"), to deal in the Software without
8     restriction, including without limitation the rights to use, copy,
9     modify, merge, publish, distribute, sublicense, and/or sell copies
10     of the Software, and to permit persons to whom the Software is
11     furnished to do so, subject to the following conditions:
12 
13     The above copyright notice and this permission notice shall be
14     included in all copies or substantial portions of the Software.
15 
16     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17     EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18     MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20     CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
21     CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22     WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 
24     Except as contained in this notice, the names of the Centre for
25     Digital Music and Queen Mary, University of London shall not be
26     used in advertising or otherwise to promote the sale, use or other
27     dealings in this Software without prior written authorization.
28 */
29 
30 #include "knownplugincandidates.h"
31 
32 #include <sstream>
33 
34 using namespace std;
35 
36 /** This returns true if the helper has a name ending in "-32". By our
37  *  convention, this means that it is a 32-bit helper found on a
38  *  64-bit system, so (depending on the OS) we may need to look in
39  *  32-bit-specific paths. Note that is32bit() is *not* usually true
40  *  on 32-bit systems; it's used specifically to indicate a
41  *  "non-native" 32-bit helper.
42  */
43 static
44 bool
is32bit(string helperExecutableName)45 is32bit(string helperExecutableName)
46 {
47     return helperExecutableName.find("-32") != string::npos;
48 }
49 
KnownPluginCandidates(string helperExecutableName,PluginCandidates::LogCallback * cb)50 KnownPluginCandidates::KnownPluginCandidates(string helperExecutableName,
51                                              PluginCandidates::LogCallback *cb) :
52     m_known(is32bit(helperExecutableName) ?
53             KnownPlugins::FormatNonNative32Bit :
54             KnownPlugins::FormatNative),
55     m_candidates(helperExecutableName),
56     m_helperExecutableName(helperExecutableName)
57 {
58     m_candidates.setLogCallback(cb);
59 
60     auto knownTypes = m_known.getKnownPluginTypes();
61 
62     for (auto type: knownTypes) {
63         m_candidates.scan(m_known.getTagFor(type),
64                           m_known.getPathFor(type),
65                           m_known.getDescriptorFor(type));
66     }
67 }
68 
69 vector<PluginCandidates::FailureRec>
getFailures() const70 KnownPluginCandidates::getFailures() const
71 {
72     vector<PluginCandidates::FailureRec> failures;
73 
74     for (auto t: getKnownPluginTypes()) {
75         auto ff = m_candidates.getFailedLibrariesFor(m_known.getTagFor(t));
76         failures.insert(failures.end(), ff.begin(), ff.end());
77     }
78 
79     return failures;
80 }
81 
82 string
getFailureReport() const83 KnownPluginCandidates::getFailureReport() const
84 {
85     auto failures = getFailures();
86     if (failures.empty()) return "";
87 
88     int n = int(failures.size());
89     int i = 0;
90 
91     ostringstream os;
92 
93     os << "<ul>";
94     for (auto f: failures) {
95         os << "<li>" + f.library;
96         if (f.message != "") {
97             os << "<br><i>" + f.message + "</i>";
98         } else {
99             os << "<br><i>unknown error</i>";
100         }
101         os << "</li>";
102 
103         if (n > 10) {
104             if (++i == 5) {
105                 os << "<li>(... and " << (n - i) << " further failures)</li>";
106                 break;
107             }
108         }
109     }
110     os << "</ul>";
111 
112     return os.str();
113 }
114