-
Notifications
You must be signed in to change notification settings - Fork 4
/
settings.cpp
93 lines (84 loc) · 3.17 KB
/
settings.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "settings.h"
#include <QFile>
#include <QDir>
static QString pathConverter(const QString& fn)
{
return QDir().absoluteFilePath(fn).replace("/", QDir::separator());
}
QStringList Settings::parparBin(bool* isSystemExecutable) const
{
// check setting
auto pathParPar = settings.value("ParPar/Bin", "").toString();
auto pathNode = settings.value("ParPar/Node", "").toString();
if(!pathParPar.isEmpty()) {
if(isSystemExecutable) *isSystemExecutable = false; // well... I guess it's possibly true, but we wouldn't know
if(!pathNode.isEmpty()) {
if(pathNode != "node") // system executable - can't make absolute
pathNode = pathConverter(pathNode);
return {pathNode, pathConverter(pathParPar)};
} else {
if(pathParPar != "parpar")
pathParPar = pathConverter(pathParPar);
return {pathParPar};
}
}
// if not set, scan for it
QString exe = "";
QString curdir = "./";
#ifdef Q_OS_WINDOWS
exe = ".exe";
curdir = "";
#endif
if(isSystemExecutable) *isSystemExecutable = false;
// compiled binary
if(QFile::exists(QString("parpar") + exe))
return {pathConverter(QString("parpar") + exe)};
#ifdef Q_OS_WINDOWS
// TODO: test if this works
if(QFile::exists("parpar.cmd"))
return {pathConverter("parpar.cmd")};
#endif
if(QFile::exists("bin/parpar.js")) {
// find included node
if(QFile::exists(QString("bin/node") + exe))
return {pathConverter(QString("bin/node") + exe), pathConverter("bin/parpar.js")};
if(QFile::exists(QString("node") + exe))
return {pathConverter(QString("node") + exe), pathConverter("bin/parpar.js")};
// use system node
#ifdef Q_OS_WINDOWS
if(isSystemExecutable) *isSystemExecutable = true;
return {QString("node"), pathConverter("bin/parpar.js")};
#else
return {pathConverter("bin/parpar.js")};
#endif
}
if(isSystemExecutable) *isSystemExecutable = true;
return {"parpar"};
}
static QString relPathConverter(const QString& file)
{
if(file.isEmpty()) return file;
QDir cd;
QFileInfo info(file);
if(info.isAbsolute()) {
QString relPath = cd.relativeFilePath(file);
#ifdef Q_OS_WINDOWS
// on Windows, can't use relative paths if on different drives (or drive <> UNC path)
if(cd.absolutePath().left(2).compare(file.left(2), Qt::CaseInsensitive) == 0)
#else
// on *nix, path in current dir should have preceeding './'
// (we don't really require it, because we're not executing over a shell, but it distinguishes a local binary vs system binary)
if(!relPath.contains("/"))
return QString(".") + QDir::separator() + relPath;
#endif
if(!relPath.startsWith("../")) // only allow relative paths if in the same folder
return relPath.replace("/", QDir::separator());
}
return file;
}
void Settings::setParparBin(const QString& parpar, const QString& node)
{
// convert to relative paths to allow portability
settings.setValue("ParPar/Bin", relPathConverter(parpar));
settings.setValue("ParPar/Node", relPathConverter(node));
}