File indexing completed on 2024-07-14 04:55:00

0001 /*
0002     SPDX-FileCopyrightText: 2023 Thomas Baumgart tbaumgart @kde.org
0003 
0004     This file is part of libalkimia.
0005 
0006     SPDX-License-Identifier: LGPL-2.1-or-later
0007 */
0008 
0009 // #include <QtGlobal>
0010 #include <QProcess>
0011 #include <QDebug>
0012 
0013 #include <alkimia/alkenvironment.h>
0014 
0015 void AlkEnvironment::checkForAppImageEnvironment(const char* applicationPath)
0016 {
0017 #ifdef Q_OS_UNIX
0018 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
0019     if (qEnvironmentVariableIsSet("APPDIR")) {
0020         QByteArray appFullPath(applicationPath);
0021         auto lastDirSeparator = appFullPath.lastIndexOf('/');
0022         auto appDir = appFullPath.left(lastDirSeparator + 1);
0023         auto appName = QString::fromUtf8(appFullPath.mid(lastDirSeparator + 1));
0024         qDebug() << "AppImageInfo:" << appFullPath << appDir << appName;
0025         if (appName == QStringLiteral("AppRun.wrapped")) {
0026             appDir.append("usr/lib");
0027             const auto libPath = qgetenv("LD_LIBRARY_PATH");
0028             auto newLibPath = appDir;
0029             if (!libPath.isEmpty()) {
0030                 newLibPath.append(':');
0031                 newLibPath.append(libPath);
0032             }
0033             qputenv("RUNNING_AS_APPIMAGE", "true");
0034             qputenv("LD_LIBRARY_PATH", newLibPath);
0035             qDebug() << "LD_LIBRARY_PATH set to" << newLibPath;
0036         }
0037     }
0038     #endif
0039 #endif
0040 }
0041 
0042 bool AlkEnvironment::isRunningAsAppImage()
0043 {
0044 #ifdef Q_OS_UNIX
0045 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
0046     return qEnvironmentVariableIsSet("RUNNING_AS_APPIMAGE");
0047     #else
0048     return false;
0049     #endif
0050 #else
0051     return false;
0052 #endif
0053 }
0054 
0055 
0056 void AlkEnvironment::removeAppImagePathFromLinkLoaderLibPath(QProcess* process)
0057 {
0058 #ifdef Q_OS_UNIX
0059 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
0060     if (isRunningAsAppImage() && process) {
0061         auto environment = QProcessEnvironment::systemEnvironment();
0062         auto ld_library_path = environment.value(QLatin1String("LD_LIBRARY_PATH"));
0063         if (!ld_library_path.isEmpty()) {
0064             const auto appdir = environment.value(QLatin1String("APPDIR"));
0065             auto path_list = ld_library_path.split(QLatin1Char(':'));
0066             while (!path_list.isEmpty() && path_list.at(0).startsWith(appdir)) {
0067                 path_list.removeAt(0);
0068                 ld_library_path.clear();
0069                 if (!path_list.isEmpty()) {
0070                     ld_library_path = path_list.join(QLatin1Char(':'));
0071                 }
0072                 if (!ld_library_path.isEmpty()) {
0073                     environment.insert(QLatin1String("LD_LIBRARY_PATH"), ld_library_path);
0074                 } else {
0075                     environment.remove(QLatin1String("LD_LIBRARY_PATH"));
0076                 }
0077                 process->setProcessEnvironment(environment);
0078             }
0079         }
0080     }
0081     #endif
0082 #endif
0083 }