HelperExecPath.cpp
Go to the documentation of this file.
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  This file copyright 2006-2016 Chris Cannam and QMUL.
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of the
12  License, or (at your option) any later version. See the file
13  COPYING included with this distribution for more information.
14 */
15 
16 #include "HelperExecPath.h"
17 
18 #include <QCoreApplication>
19 #include <QFile>
20 #include <QDir>
21 #include <QFileInfo>
22 
23 QStringList
25 {
26  if (sizeof(void *) == 8) {
28  return { "64", "" };
29  } else {
30  return { "64", "", "32" };
31  }
32  } else {
33  return { "", "32" };
34  }
35 }
36 
37 static bool
38 isGood(QString path)
39 {
40  return QFile(path).exists() && QFileInfo(path).isExecutable();
41 }
42 
43 QList<HelperExecPath::HelperExec>
45 {
46  QStringList dummy;
47  return search(basename, dummy);
48 }
49 
50 QString
52 {
53  auto execs = getHelperExecutables(basename);
54  if (execs.empty()) return "";
55  else return execs[0].executable;
56 }
57 
58 QStringList
60 {
61  // Helpers are expected to exist in one of the following, in order
62  // from most strongly preferred to least:
63  //
64  // 1. (on Mac only) in <mydir>/../Resources
65  //
66  // 2. (on non-Windows non-Mac platforms only) in
67  // <mydir>/../lib/application-name/
68  //
69  // 3. (on non-Mac platforms only) in <mydir>/helpers
70  //
71  // 4. in <mydir>
72 
73  QStringList dirs;
74  QString appName = QCoreApplication::applicationName();
75  QString myDir = QCoreApplication::applicationDirPath();
76  QString binaryName = QFileInfo(QCoreApplication::arguments().at(0))
77  .fileName();
78 
79 #ifdef Q_OS_MAC
80  dirs.push_back(myDir + "/../Resources");
81 #else
82 #ifndef Q_OS_WIN32
83  if (binaryName != "") {
84  dirs.push_back(myDir + "/../lib/" + binaryName);
85  }
86  dirs.push_back(myDir + "/../lib/" + appName);
87 #endif
88  dirs.push_back(myDir + "/helpers");
89 #endif
90  dirs.push_back(myDir);
91  return dirs;
92 }
93 
94 QStringList
96 {
97  QStringList candidates;
98  (void)search(basename, candidates);
99  return candidates;
100 }
101 
102 QList<HelperExecPath::HelperExec>
103 HelperExecPath::search(QString basename, QStringList &candidates)
104 {
105  QString extension = "";
106 #ifdef _WIN32
107  extension = ".exe";
108 #endif
109 
110  QList<HelperExec> executables;
111  QStringList dirs = getHelperDirPaths();
112 
113  for (QString t: getTags()) {
114  for (QString d: dirs) {
115  QString path = d + QDir::separator() + basename;
116  if (t != QString()) path += "-" + t;
117  path += extension;
118  candidates.push_back(path);
119  if (isGood(path)) {
120  executables.push_back({ path, t });
121  break;
122  }
123  }
124  }
125 
126  return executables;
127 }
128 
QString getHelperExecutable(QString basename)
Find a helper executable with the given base name in the bundle directory or installation location...
QStringList getHelperDirPaths()
Return the list of directories searched for helper executables.
QStringList getTags()
QList< HelperExec > search(QString, QStringList &)
static bool isGood(QString path)
QStringList getHelperCandidatePaths(QString basename)
Return the list of executable paths examined in the search for the helper executable with the given b...
QList< HelperExec > getHelperExecutables(QString basename)
Find all helper executables with the given base name in the bundle directory or installation location...
SearchType m_type