| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2010 Ryan Rix <ry@n.rix.si> |
| 3 | |
| 4 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 | */ |
| 6 | |
| 7 | #include "packageloader.h" |
| 8 | #include "private/packageloader_p.h" |
| 9 | #include "private/utils.h" |
| 10 | |
| 11 | #include "kpackage_debug.h" |
| 12 | #include <QCoreApplication> |
| 13 | #include <QDateTime> |
| 14 | #include <QDirIterator> |
| 15 | #include <QList> |
| 16 | #include <QStandardPaths> |
| 17 | |
| 18 | #include <KLazyLocalizedString> |
| 19 | #include <KPluginFactory> |
| 20 | #include <KPluginMetaData> |
| 21 | #include <unordered_set> |
| 22 | |
| 23 | #include "config-package.h" |
| 24 | |
| 25 | #include "package.h" |
| 26 | #include "packagestructure.h" |
| 27 | #include "private/packages_p.h" |
| 28 | |
| 29 | namespace KPackage |
| 30 | { |
| 31 | PackageLoader::PackageLoader() |
| 32 | : d(new PackageLoaderPrivate) |
| 33 | { |
| 34 | } |
| 35 | |
| 36 | PackageLoader::~PackageLoader() |
| 37 | { |
| 38 | for (auto &wp : std::as_const(t&: d->structures)) { |
| 39 | delete wp.data(); |
| 40 | } |
| 41 | delete d; |
| 42 | } |
| 43 | |
| 44 | PackageLoader *PackageLoader::self() |
| 45 | { |
| 46 | static PackageLoader *s_packageTrader = new PackageLoader; |
| 47 | return s_packageTrader; |
| 48 | } |
| 49 | |
| 50 | Package PackageLoader::loadPackage(const QString &packageFormat, const QString &packagePath) |
| 51 | { |
| 52 | if (packageFormat.isEmpty()) { |
| 53 | return Package(); |
| 54 | } |
| 55 | |
| 56 | if (PackageStructure *structure = loadPackageStructure(packageFormat)) { |
| 57 | Package p(structure); |
| 58 | if (!packagePath.isEmpty()) { |
| 59 | p.setPath(packagePath); |
| 60 | } |
| 61 | return p; |
| 62 | } |
| 63 | |
| 64 | return Package(); |
| 65 | } |
| 66 | |
| 67 | QList<Package> PackageLoader::listKPackages(const QString &packageFormat, const QString &packageRoot) |
| 68 | { |
| 69 | QList<Package> lst; |
| 70 | |
| 71 | // has been a root specified? |
| 72 | QString actualRoot = packageRoot; |
| 73 | |
| 74 | PackageStructure *structure = d->structures.value(key: packageFormat).data(); |
| 75 | // try to take it from the package structure |
| 76 | if (actualRoot.isEmpty()) { |
| 77 | if (!structure) { |
| 78 | if (packageFormat == QLatin1String("KPackage/Generic" )) { |
| 79 | structure = new GenericPackage(); |
| 80 | } else if (packageFormat == QLatin1String("KPackage/GenericQML" )) { |
| 81 | structure = new GenericQMLPackage(); |
| 82 | } else { |
| 83 | structure = loadPackageStructure(packageFormat); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | if (structure) { |
| 88 | d->structures.insert(key: packageFormat, value: structure); |
| 89 | actualRoot = Package(structure).defaultPackageRoot(); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | if (actualRoot.isEmpty()) { |
| 94 | actualRoot = packageFormat; |
| 95 | } |
| 96 | |
| 97 | QStringList paths; |
| 98 | if (QDir::isAbsolutePath(path: actualRoot)) { |
| 99 | paths = QStringList(actualRoot); |
| 100 | } else { |
| 101 | const auto listPath = QStandardPaths::standardLocations(type: QStandardPaths::GenericDataLocation); |
| 102 | for (const QString &path : listPath) { |
| 103 | paths += path + QLatin1Char('/') + actualRoot; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | for (auto const &plugindir : std::as_const(t&: paths)) { |
| 108 | QDirIterator it(plugindir, QDir::Dirs | QDir::NoDotAndDotDot); |
| 109 | std::unordered_set<QString> dirs; |
| 110 | while (it.hasNext()) { |
| 111 | it.next(); |
| 112 | |
| 113 | const QString dir = it.filePath(); |
| 114 | if (!dirs.insert(x: it.fileInfo().fileName()).second) { |
| 115 | continue; |
| 116 | } |
| 117 | Package package(structure); |
| 118 | package.setPath(dir); |
| 119 | if (package.isValid()) { |
| 120 | // Ignore packages with empty metadata here |
| 121 | if (packageFormat.isEmpty() || !package.metadata().isValid() || readKPackageType(metaData: package.metadata()) == packageFormat) { |
| 122 | lst << package; |
| 123 | } else { |
| 124 | qInfo() << "KPackage in" << package.path() << readKPackageType(metaData: package.metadata()) << "does not match requested format" << packageFormat; |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | return lst; |
| 130 | } |
| 131 | QList<KPluginMetaData> PackageLoader::listPackages(const QString &packageFormat, const QString &packageRoot) |
| 132 | { |
| 133 | // Note: Use QDateTime::currentSecsSinceEpoch() once we can depend on Qt 5.8 |
| 134 | const qint64 now = qRound64(d: QDateTime::currentMSecsSinceEpoch() / 1000.0); |
| 135 | bool useRuntimeCache = true; |
| 136 | if (now - d->pluginCacheAge > d->maxCacheAge && d->pluginCacheAge != 0) { |
| 137 | // cache is old and we're not within a few seconds of startup anymore |
| 138 | useRuntimeCache = false; |
| 139 | d->pluginCache.clear(); |
| 140 | } |
| 141 | |
| 142 | const QString cacheKey = packageFormat + QLatin1Char('.') + packageRoot; |
| 143 | if (useRuntimeCache) { |
| 144 | auto it = d->pluginCache.constFind(key: cacheKey); |
| 145 | if (it != d->pluginCache.constEnd()) { |
| 146 | return *it; |
| 147 | } |
| 148 | } |
| 149 | if (d->pluginCacheAge == 0) { |
| 150 | d->pluginCacheAge = now; |
| 151 | } |
| 152 | |
| 153 | QList<KPluginMetaData> lst; |
| 154 | |
| 155 | // has been a root specified? |
| 156 | QString actualRoot = packageRoot; |
| 157 | |
| 158 | // try to take it from the package structure |
| 159 | if (actualRoot.isEmpty()) { |
| 160 | PackageStructure *structure = d->structures.value(key: packageFormat).data(); |
| 161 | if (!structure) { |
| 162 | if (packageFormat == QLatin1String("KPackage/Generic" )) { |
| 163 | structure = new GenericPackage(); |
| 164 | } else if (packageFormat == QLatin1String("KPackage/GenericQML" )) { |
| 165 | structure = new GenericQMLPackage(); |
| 166 | } else { |
| 167 | structure = loadPackageStructure(packageFormat); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | if (structure) { |
| 172 | d->structures.insert(key: packageFormat, value: structure); |
| 173 | Package p(structure); |
| 174 | actualRoot = p.defaultPackageRoot(); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | if (actualRoot.isEmpty()) { |
| 179 | actualRoot = packageFormat; |
| 180 | } |
| 181 | |
| 182 | QSet<QString> uniqueIds; |
| 183 | QStringList paths; |
| 184 | if (QDir::isAbsolutePath(path: actualRoot)) { |
| 185 | paths = QStringList(actualRoot); |
| 186 | } else { |
| 187 | const auto listPath = QStandardPaths::standardLocations(type: QStandardPaths::GenericDataLocation); |
| 188 | for (const QString &path : listPath) { |
| 189 | paths += path + QLatin1Char('/') + actualRoot; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | for (auto const &plugindir : std::as_const(t&: paths)) { |
| 194 | QDirIterator it(plugindir, QStringList{QStringLiteral("metadata.json" )}, QDir::Files, QDirIterator::Subdirectories | QDirIterator::FollowSymlinks); |
| 195 | std::unordered_set<QString> dirs; |
| 196 | while (it.hasNext()) { |
| 197 | it.next(); |
| 198 | |
| 199 | const QString dir = it.fileInfo().absoluteDir().path(); |
| 200 | if (!dirs.insert(x: dir).second) { |
| 201 | continue; |
| 202 | } |
| 203 | |
| 204 | const QString metadataPath = it.fileInfo().absoluteFilePath(); |
| 205 | KPluginMetaData info = KPluginMetaData::fromJsonFile(jsonFile: metadataPath); |
| 206 | |
| 207 | if (!info.isValid() || uniqueIds.contains(value: info.pluginId())) { |
| 208 | continue; |
| 209 | } |
| 210 | |
| 211 | if (packageFormat.isEmpty() || readKPackageType(metaData: info) == packageFormat) { |
| 212 | uniqueIds << info.pluginId(); |
| 213 | lst << info; |
| 214 | } else { |
| 215 | qInfo() << "KPackageStructure of" << info << "does not match requested format" << packageFormat; |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | if (useRuntimeCache) { |
| 221 | d->pluginCache.insert(key: cacheKey, value: lst); |
| 222 | } |
| 223 | return lst; |
| 224 | } |
| 225 | |
| 226 | QList<KPluginMetaData> PackageLoader::listPackagesMetadata(const QString &packageFormat, const QString &packageRoot) |
| 227 | { |
| 228 | return listPackages(packageFormat, packageRoot); |
| 229 | } |
| 230 | |
| 231 | QList<KPluginMetaData> |
| 232 | PackageLoader::findPackages(const QString &packageFormat, const QString &packageRoot, std::function<bool(const KPluginMetaData &)> filter) |
| 233 | { |
| 234 | QList<KPluginMetaData> lst; |
| 235 | const auto lstPlugins = listPackages(packageFormat, packageRoot); |
| 236 | for (auto const &plugin : lstPlugins) { |
| 237 | if (!filter || filter(plugin)) { |
| 238 | lst << plugin; |
| 239 | } |
| 240 | } |
| 241 | return lst; |
| 242 | } |
| 243 | |
| 244 | KPackage::PackageStructure *PackageLoader::loadPackageStructure(const QString &packageFormat) |
| 245 | { |
| 246 | PackageStructure *structure = d->structures.value(key: packageFormat).data(); |
| 247 | if (!structure) { |
| 248 | if (packageFormat == QLatin1String("KPackage/Generic" )) { |
| 249 | structure = new GenericPackage(); |
| 250 | d->structures.insert(key: packageFormat, value: structure); |
| 251 | } else if (packageFormat == QLatin1String("KPackage/GenericQML" )) { |
| 252 | structure = new GenericQMLPackage(); |
| 253 | d->structures.insert(key: packageFormat, value: structure); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | if (structure) { |
| 258 | return structure; |
| 259 | } |
| 260 | |
| 261 | const KPluginMetaData metaData = structureForKPackageType(packageFormat); |
| 262 | if (!metaData.isValid()) { |
| 263 | qCWarning(KPACKAGE_LOG) << "Invalid metadata for package structure" << packageFormat; |
| 264 | return nullptr; |
| 265 | } |
| 266 | |
| 267 | auto result = KPluginFactory::instantiatePlugin<PackageStructure>(data: metaData); |
| 268 | if (!result) { |
| 269 | qCWarning(KPACKAGE_LOG).noquote() << "Could not load installer for package of type" << packageFormat << "Error reported was: " << result.errorString; |
| 270 | return nullptr; |
| 271 | } |
| 272 | |
| 273 | structure = result.plugin; |
| 274 | |
| 275 | d->structures.insert(key: packageFormat, value: structure); |
| 276 | |
| 277 | return structure; |
| 278 | } |
| 279 | |
| 280 | void PackageLoader::addKnownPackageStructure(const QString &packageFormat, KPackage::PackageStructure *structure) |
| 281 | { |
| 282 | d->structures.insert(key: packageFormat, value: structure); |
| 283 | } |
| 284 | |
| 285 | void PackageLoader::invalidateCache() |
| 286 | { |
| 287 | self()->d->maxCacheAge = -1; |
| 288 | } |
| 289 | |
| 290 | } // KPackage Namespace |
| 291 | |