a-la-karte/src/platformimporter.cpp

47 lines
1.2 KiB
C++
Raw Normal View History

// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: 2024 A-La-Karte Contributors
#include "platformimporter.h"
#include <QDir>
#include <QRegularExpression>
#include <QStandardPaths>
PlatformImporter::PlatformImporter(QObject *parent)
: QObject(parent)
{
}
QString PlatformImporter::findExecutable(const QString &name) const
{
return QStandardPaths::findExecutable(name);
}
bool PlatformImporter::directoryExists(const QString &path) const
{
return QDir(expandPath(path)).exists();
}
QString PlatformImporter::expandPath(const QString &path) const
{
QString result = path;
// Expand ~ to home directory
if (result.startsWith(QLatin1Char('~'))) {
result.replace(0, 1, QDir::homePath());
}
// Expand environment variables
static QRegularExpression envVarRegex(QStringLiteral("\\$\\{?([A-Za-z_][A-Za-z0-9_]*)\\}?"));
QRegularExpressionMatchIterator it = envVarRegex.globalMatch(result);
while (it.hasNext()) {
QRegularExpressionMatch match = it.next();
QString varName = match.captured(1);
QString varValue = QString::fromLocal8Bit(qgetenv(varName.toLocal8Bit().constData()));
result.replace(match.captured(0), varValue);
}
return result;
}