Commit 53c31b01 authored by Aleix Pol Gonzalez's avatar Aleix Pol Gonzalez 🐧

flatpak: Simplify the size fetching process

parent 2405b765
......@@ -1081,11 +1081,11 @@ bool FlatpakBackend::updateAppSizeFromRemote(FlatpakResource *resource)
return true;
}
auto futureWatcher = new QFutureWatcher<FlatpakRunnables::SizeInformation>(this);
connect(futureWatcher, &QFutureWatcher<FlatpakRunnables::SizeInformation>::finished, this, [this, resource, futureWatcher]() {
auto value = futureWatcher->result();
if (value.valid) {
onFetchSizeFinished(resource, value.downloadSize, value.installedSize);
auto futureWatcher = new QFutureWatcher<FlatpakRemoteRef*>(this);
connect(futureWatcher, &QFutureWatcher<FlatpakRemoteRef*>::finished, this, [this, resource, futureWatcher]() {
g_autoptr(FlatpakRemoteRef) remoteRef = futureWatcher->result();
if (remoteRef) {
onFetchSizeFinished(resource, flatpak_remote_ref_get_download_size(remoteRef), flatpak_remote_ref_get_installed_size(remoteRef));
} else {
resource->setPropertyState(FlatpakResource::DownloadSize, FlatpakResource::UnknownOrFailed);
resource->setPropertyState(FlatpakResource::InstalledSize, FlatpakResource::UnknownOrFailed);
......@@ -1095,7 +1095,7 @@ bool FlatpakBackend::updateAppSizeFromRemote(FlatpakResource *resource)
resource->setPropertyState(FlatpakResource::DownloadSize, FlatpakResource::Fetching);
resource->setPropertyState(FlatpakResource::InstalledSize, FlatpakResource::Fetching);
futureWatcher->setFuture(QtConcurrent::run(&m_threadPool, &FlatpakRunnables::fetchFlatpakSize, resource, m_cancellable));
futureWatcher->setFuture(QtConcurrent::run(&m_threadPool, &FlatpakRunnables::findRemoteRef, resource, m_cancellable));
}
return true;
......
......@@ -11,61 +11,42 @@
namespace FlatpakRunnables
{
static FlatpakRemoteRef* findRemoteRef(FlatpakResource *app, GCancellable* cancellable, GError **error)
FlatpakRemoteRef* findRemoteRef(FlatpakResource *app, GCancellable* cancellable)
{
if (app->origin().isEmpty()) {
qWarning() << "Failed to get metadata file because of missing origin";
return nullptr;
}
g_autoptr(GError) localError = nullptr;
const auto kind = app->resourceType() == FlatpakResource::DesktopApp ? FLATPAK_REF_KIND_APP : FLATPAK_REF_KIND_RUNTIME;
const QByteArray origin = app->origin().toUtf8(), name = app->flatpakName().toUtf8(), arch = app->arch().toUtf8(), branch = app->branch().toUtf8();
return flatpak_installation_fetch_remote_ref_sync_full(app->installation(), origin.constData(), kind, name.constData(), arch.constData(), branch.constData(), FLATPAK_QUERY_FLAGS_ONLY_CACHED, cancellable, error);
auto ret = flatpak_installation_fetch_remote_ref_sync_full(app->installation(), origin.constData(), kind, name.constData(), arch.constData(), branch.constData(), FLATPAK_QUERY_FLAGS_ONLY_CACHED, cancellable, &localError);
if (!ret) {
qWarning() << "Failed to find:" << app->ref() << "in" << origin << localError->message;
return {};
}
return ret;
}
QByteArray fetchMetadata(FlatpakResource *app, GCancellable* cancellable)
{
g_autoptr(GError) localError = nullptr;
FlatpakRemoteRef* remote = findRemoteRef(app, cancellable, &localError);
Q_ASSERT(remote);
Q_ASSERT(!localError);
QByteArray metadataContent;
g_autoptr(GBytes) data = flatpak_remote_ref_get_metadata(remote);
if (data) {
gsize len = 0;
auto buff = g_bytes_get_data(data, &len);
metadataContent = QByteArray((const char*) buff, len);
} else {
qWarning() << "Failed to get metadata file: " << localError->message;
FlatpakRemoteRef* remoteRef = findRemoteRef(app, cancellable);
if (!remoteRef) {
qDebug() << "failed to find the remote" << app->name();
return {};
}
g_autoptr(GBytes) data = flatpak_remote_ref_get_metadata(remoteRef);
gsize len = 0;
auto buff = g_bytes_get_data(data, &len);
const QByteArray metadataContent((const char*) buff, len);
if (metadataContent.isEmpty()) {
qWarning() << "Failed to get metadata file: empty metadata";
return {};
}
return metadataContent;
}
SizeInformation fetchFlatpakSize(FlatpakResource *app, GCancellable* cancellable)
{
g_autoptr(GError) localError = nullptr;
SizeInformation ret;
FlatpakRemoteRef* remote = findRemoteRef(app, cancellable, &localError);
if (!remote) {
qWarning() << "Failed to get remote size of" << app->name() << app->origin() << ":" << localError->message;
return ret;
}
ret.downloadSize = flatpak_remote_ref_get_download_size(remote);
ret.installedSize = flatpak_remote_ref_get_installed_size(remote);
ret.valid = true;
return ret;
}
}
......@@ -17,13 +17,7 @@ class FlatpakResource;
namespace FlatpakRunnables
{
struct SizeInformation {
bool valid = false;
guint64 downloadSize;
guint64 installedSize;
};
SizeInformation fetchFlatpakSize(FlatpakResource *app, GCancellable* cancellable);
FlatpakRemoteRef* findRemoteRef(FlatpakResource *app, GCancellable* cancellable);
QByteArray fetchMetadata(FlatpakResource *app, GCancellable* cancellable);
}
......
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