Commit ba61b469 authored by Aleix Pol Gonzalez's avatar Aleix Pol Gonzalez 🐧

flatpak: simplify internal identification of resources into a hash

Instead of building artificial strings.
parent 4c65d034
......@@ -61,7 +61,7 @@
DISCOVER_BACKEND_PLUGIN(FlatpakBackend)
static QString idForInstalledRef(FlatpakInstallation *installation, FlatpakInstalledRef *ref)
static FlatpakResource::Id idForInstalledRef(FlatpakInstallation *installation, FlatpakInstalledRef *ref)
{
const FlatpakResource::ResourceType appType = flatpak_ref_get_kind(FLATPAK_REF(ref)) == FLATPAK_REF_KIND_APP ? FlatpakResource::DesktopApp : FlatpakResource::Runtime;
const QString name = QLatin1String(flatpak_ref_get_name(FLATPAK_REF(ref)));
......@@ -70,8 +70,7 @@ static QString idForInstalledRef(FlatpakInstallation *installation, FlatpakInsta
const QString arch = QString::fromUtf8(flatpak_ref_get_arch(FLATPAK_REF(ref)));
const QString branch = QString::fromUtf8(flatpak_ref_get_branch(FLATPAK_REF(ref)));
return QStringLiteral("%1/%2/%3/%4/%5/%6").arg(FlatpakResource::installationPath(installation), QLatin1String("flatpak"), QString::fromUtf8(flatpak_installed_ref_get_origin(ref)),
FlatpakResource::typeAsString(appType), appId, branch);
return { installation, QString::fromUtf8(flatpak_installed_ref_get_origin(ref)), appType, appId, branch, arch };
}
FlatpakBackend::FlatpakBackend(QObject* parent)
......@@ -226,10 +225,9 @@ FlatpakResource * FlatpakBackend::getRuntimeForApp(FlatpakResource *resource) co
return runtime;
}
const QString runtimeId = QStringLiteral("runtime/") + runtimeInfo.at(0) + QLatin1Char('/') + runtimeInfo.at(2);
for(auto it = m_resources.constBegin(), itEnd = m_resources.constEnd(); it!=itEnd; ++it) {
if (it.key().endsWith(runtimeId)) {
const auto id = it.key();
if (id.type == FlatpakResource::Runtime && id.id == runtimeInfo.at(0) && id.branch == runtimeInfo.at(2)) {
runtime = *it;
break;
}
......@@ -237,7 +235,7 @@ FlatpakResource * FlatpakBackend::getRuntimeForApp(FlatpakResource *resource) co
// TODO if runtime wasn't found, create a new one from available info
if (!runtime) {
qWarning() << "could not find runtime" << runtimeId << resource;
qWarning() << "could not find runtime" << runtimeInfo << resource;
}
return runtime;
......
......@@ -104,7 +104,7 @@ private:
void acquireFetching(bool f);
QHash<QString, FlatpakResource*> m_resources;
QHash<FlatpakResource::Id, FlatpakResource*> m_resources;
StandardBackendUpdater *m_updater;
FlatpakSourcesBackend *m_sources = nullptr;
QSharedPointer<OdrsReviewsBackend> m_reviews;
......
......@@ -387,12 +387,9 @@ QString FlatpakResource::typeAsString() const
}
}
QString FlatpakResource::uniqueId() const
FlatpakResource::Id FlatpakResource::uniqueId() const
{
return installationPath() + QStringLiteral("/flatpak/") + origin()
+ QLatin1Char('/') + typeAsString()
+ QLatin1Char('/') + m_appdata.id()
+ QLatin1Char('/') + branch();
return {m_installation, m_origin, m_type, m_appdata.id(), branch(), arch() };
}
void FlatpakResource::invokeApplication() const
......
......@@ -58,6 +58,24 @@ public:
Source
};
struct Id {
const FlatpakInstallation * installation;
const QString origin;
const FlatpakResource::ResourceType type;
const QString id;
const QString branch;
const QString arch;
bool operator!=(const Id& other) const { return !operator==(other); }
bool operator==(const Id& other) const { return &other == this || (
other.installation == installation
&& other.origin == origin
&& other.type == type
&& other.id == id
&& other.branch == branch
&& other.arch == arch
); }
};
static QString typeAsString(ResourceType type) {
if (type == DesktopApp) {
return QLatin1String("app");
......@@ -103,7 +121,7 @@ public:
AbstractResource::State state() override;
ResourceType type() const;
QString typeAsString() const;
QString uniqueId() const;
FlatpakResource::Id uniqueId() const;
QUrl url() const override;
QDate releaseDate() const override;
......@@ -158,4 +176,14 @@ public:
ResourceType m_type;
};
inline uint qHash(const FlatpakResource::Id &key)
{
return qHash(key.installation)
^ qHash(key.origin)
^ qHash(key.type)
^ qHash(key.id)
^ qHash(key.branch)
;
}
#endif // FLATPAKRESOURCE_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment