Alkimia API

alkenvironment.cpp
1/*
2 SPDX-FileCopyrightText: 2023 Thomas Baumgart tbaumgart @kde.org
3
4 This file is part of libalkimia.
5
6 SPDX-License-Identifier: LGPL-2.1-or-later
7*/
8
9// #include <QtGlobal>
10#include <QProcess>
11
12#include "alkdebug.h"
13#include <alkimia/alkenvironment.h>
14
15void AlkEnvironment::checkForAppImageEnvironment(const char* applicationPath)
16{
17#ifdef Q_OS_UNIX
18#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
19 if (qEnvironmentVariableIsSet("APPDIR")) {
20 QByteArray appFullPath(applicationPath);
21 auto lastDirSeparator = appFullPath.lastIndexOf('/');
22 auto appDir = appFullPath.left(lastDirSeparator + 1);
23 auto appName = QString::fromUtf8(appFullPath.mid(lastDirSeparator + 1));
24 alkDebug() << "AppImageInfo:" << appFullPath << appDir << appName;
25 if (appName == QStringLiteral("AppRun.wrapped")) {
26 appDir.append("usr/lib");
27 const auto libPath = qgetenv("LD_LIBRARY_PATH");
28 auto newLibPath = appDir;
29 if (!libPath.isEmpty()) {
30 newLibPath.append(':');
31 newLibPath.append(libPath);
32 }
33 qputenv("RUNNING_AS_APPIMAGE", "true");
34 qputenv("LD_LIBRARY_PATH", newLibPath);
35 alkDebug() << "LD_LIBRARY_PATH set to" << newLibPath;
36 }
37 }
38 #endif
39#endif
40}
41
42bool AlkEnvironment::isRunningAsAppImage()
43{
44#ifdef Q_OS_UNIX
45#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
46 return qEnvironmentVariableIsSet("RUNNING_AS_APPIMAGE");
47 #else
48 return false;
49 #endif
50#else
51 return false;
52#endif
53}
54
55void AlkEnvironment::removeAppImagePathFromLinkLoaderLibPath(QProcess* process)
56{
57#ifdef Q_OS_UNIX
58#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
59 if (isRunningAsAppImage() && process) {
60 auto environment = QProcessEnvironment::systemEnvironment();
61 auto ld_library_path = environment.value(QLatin1String("LD_LIBRARY_PATH"));
62 if (!ld_library_path.isEmpty()) {
63 const auto appdir = environment.value(QLatin1String("APPDIR"));
64 auto path_list = ld_library_path.split(QLatin1Char(':'));
65 while (!path_list.isEmpty() && path_list.at(0).startsWith(appdir)) {
66 path_list.removeAt(0);
67 ld_library_path.clear();
68 if (!path_list.isEmpty()) {
69 ld_library_path = path_list.join(QLatin1Char(':'));
70 }
71 if (!ld_library_path.isEmpty()) {
72 environment.insert(QLatin1String("LD_LIBRARY_PATH"), ld_library_path);
73 } else {
74 environment.remove(QLatin1String("LD_LIBRARY_PATH"));
75 }
76 process->setProcessEnvironment(environment);
77 }
78 }
79 }
80 #endif
81#endif
82}
Wrapper for debug output.
QCA_EXPORT QString appName()
void setProcessEnvironment(const QProcessEnvironment &environment)
QProcessEnvironment systemEnvironment()
QString fromUtf8(QByteArrayView str)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:01:13 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.