File indexing completed on 2024-09-15 04:49:25
0001 /** 0002 * SPDX-FileCopyrightText: 2022 Suhaas Joshi <joshiesuhaas0@gmail.com> 0003 * SPDX-FileCopyrightText: 2023 ivan tkachenko <me@ratijas.tk> 0004 * SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "flatpakhelper.h" 0008 #include "config.h" 0009 0010 #include <QDir> 0011 #include <QFileInfo> 0012 #include <QStandardPaths> 0013 #include <QUrl> 0014 0015 #include <gio/gio.h> 0016 0017 namespace FlatpakHelper 0018 { 0019 0020 QString userBaseDirectory() 0021 { 0022 static const QString dir = []() { 0023 QString dir; 0024 const auto env = qEnvironmentVariable("FLATPAK_USER_DIR"); 0025 if (!env.isEmpty()) { 0026 dir = QStringLiteral("%1").arg(env); 0027 } else { 0028 // Takes care of XDG_DATA_HOME 0029 const auto userDataDir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation); 0030 dir = QStringLiteral("%1/flatpak").arg(userDataDir); 0031 } 0032 return dir; 0033 }(); 0034 return dir; 0035 } 0036 0037 QString systemBaseDirectory() 0038 { 0039 static const QString dir = []() { 0040 const auto env = qEnvironmentVariable("FLATPAK_SYSTEM_DIR"); 0041 if (!env.isEmpty()) { 0042 return env; 0043 } 0044 return QStringLiteral(FLATPAK_SYSTEMDIR); 0045 }(); 0046 return dir; 0047 } 0048 0049 QString userOverridesDirectory() 0050 { 0051 static const QString dir = []() { 0052 const auto base = userBaseDirectory(); 0053 const auto dir = QStringLiteral("%1/overrides").arg(base); 0054 return dir; 0055 }(); 0056 return dir; 0057 } 0058 0059 QString systemOverridesDirectory() 0060 { 0061 static const QString dir = []() { 0062 const auto base = systemBaseDirectory(); 0063 const auto dir = QStringLiteral("%1/overrides").arg(base); 0064 return dir; 0065 }(); 0066 return dir; 0067 } 0068 0069 static QString metadataPathForInstallation(const QString &flatpakBaseDirectory, const QString &flatpakName) 0070 { 0071 return QStringLiteral("%1/app/%2/current/active/metadata").arg(flatpakBaseDirectory, flatpakName); 0072 } 0073 0074 QString metadataPathForUserInstallation(const QString &flatpakName) 0075 { 0076 const auto base = userBaseDirectory(); 0077 return metadataPathForInstallation(base, flatpakName); 0078 } 0079 0080 QString metadataPathForSystemInstallation(const QString &flatpakName) 0081 { 0082 const auto base = systemBaseDirectory(); 0083 return metadataPathForInstallation(base, flatpakName); 0084 } 0085 0086 QUrl iconSourceUrl(const QString &displayName, const QString &flatpakName, const QString &appBaseDirectory) 0087 { 0088 QString dirPath = appBaseDirectory + QStringLiteral("/files/share/icons/hicolor/"); 0089 QDir dir(dirPath); 0090 dir.setFilter(QDir::NoDotAndDotDot | QDir::AllDirs); 0091 0092 QString nextDir; 0093 if (dir.exists(QStringLiteral("scalable"))) { 0094 nextDir = QStringLiteral("scalable"); 0095 } else if (dir.exists(QStringLiteral("symbolic"))) { 0096 nextDir = QStringLiteral("symbolic"); 0097 } else if (!dir.isEmpty()) { 0098 nextDir = dir.entryList().at(0); 0099 } else { 0100 return QUrl(); 0101 } 0102 dir.cd(nextDir + QStringLiteral("/apps")); 0103 0104 QString file = flatpakName + QStringLiteral(".png"); 0105 if (!dir.exists(file)) { 0106 file = flatpakName + QStringLiteral(".svg"); 0107 if (!dir.exists(file)) { 0108 file = displayName.toLower() + QStringLiteral(".png"); 0109 if (!dir.exists(file)) { 0110 file = displayName.toLower() + QStringLiteral(".svg"); 0111 if (!dir.exists(file)) { 0112 return QUrl(); 0113 } 0114 } 0115 } 0116 } 0117 return QUrl::fromLocalFile(dir.absoluteFilePath(file)); 0118 } 0119 0120 bool verifyDBusName(QStringView name) 0121 { 0122 if (name.endsWith(QLatin1String(".*"))) { 0123 name.chop(2); 0124 name = name.mid(-2); 0125 } 0126 0127 const auto ownedName = name.toString(); 0128 const auto arrayName = ownedName.toUtf8(); 0129 const auto cName = arrayName.constData(); 0130 0131 return g_dbus_is_name(cName) && !g_dbus_is_unique_name(cName); 0132 } 0133 }