Changeset View
Changeset View
Standalone View
Standalone View
libdiscover/backends/FlatpakBackend/FlatpakFetchDataJob.cpp
Show All 17 Lines | |||||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. * | 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. * | ||
19 | ***************************************************************************/ | 19 | ***************************************************************************/ | ||
20 | 20 | | |||
21 | #include "FlatpakFetchDataJob.h" | 21 | #include "FlatpakFetchDataJob.h" | ||
22 | #include "FlatpakResource.h" | 22 | #include "FlatpakResource.h" | ||
23 | 23 | | |||
24 | #include <QDebug> | 24 | #include <QDebug> | ||
25 | 25 | | |||
26 | FlatpakFetchDataJob::FlatpakFetchDataJob(FlatpakInstallation *installation, FlatpakResource *app, FlatpakFetchDataJob::DataKind kind) | 26 | static FlatpakRef * createFakeRef(FlatpakResource *resource) | ||
27 | : QThread() | | |||
28 | , m_app(app) | | |||
29 | , m_installation(installation) | | |||
30 | , m_kind(kind) | | |||
31 | { | 27 | { | ||
32 | m_cancellable = g_cancellable_new(); | 28 | FlatpakRef *ref = nullptr; | ||
33 | } | 29 | g_autoptr(GError) localError = nullptr; | ||
34 | 30 | | |||
35 | FlatpakFetchDataJob::~FlatpakFetchDataJob() | 31 | const QString id = QStringLiteral("%1/%2/%3/%4").arg(resource->typeAsString(), resource->flatpakName(), resource->arch(), resource->branch()); | ||
36 | { | 32 | ref = flatpak_ref_parse(id.toUtf8().constData(), &localError); | ||
37 | g_object_unref(m_cancellable); | 33 | | ||
34 | if (!ref) { | ||||
35 | qWarning() << "Failed to create fake ref: " << localError->message; | ||||
38 | } | 36 | } | ||
39 | 37 | | |||
40 | void FlatpakFetchDataJob::cancel() | 38 | return ref; | ||
41 | { | | |||
42 | g_cancellable_cancel(m_cancellable); | | |||
43 | } | 39 | } | ||
44 | 40 | | |||
45 | void FlatpakFetchDataJob::run() | 41 | namespace FlatpakRunnables | ||
46 | { | 42 | { | ||
43 | QByteArray fetchMetadata(FlatpakInstallation *installation, FlatpakResource *app) | ||||
44 | { | ||||
45 | g_autoptr(GCancellable) cancellable = g_cancellable_new(); | ||||
47 | g_autoptr(GError) localError = nullptr; | 46 | g_autoptr(GError) localError = nullptr; | ||
48 | 47 | | |||
49 | if (m_kind == FetchMetadata) { | 48 | if (app->origin().isEmpty()) { | ||
50 | QByteArray metadataContent; | | |||
51 | g_autoptr(GBytes) data = nullptr; | | |||
52 | g_autoptr(FlatpakRef) fakeRef = nullptr; | | |||
53 | | ||||
54 | if (m_app->origin().isEmpty()) { | | |||
55 | qWarning() << "Failed to get metadata file because of missing origin"; | 49 | qWarning() << "Failed to get metadata file because of missing origin"; | ||
56 | Q_EMIT jobFetchMetadataFailed(); | 50 | return {}; | ||
57 | return; | | |||
58 | } | 51 | } | ||
59 | 52 | | |||
60 | fakeRef = createFakeRef(m_app); | 53 | g_autoptr(FlatpakRef) fakeRef = createFakeRef(app); | ||
61 | if (!fakeRef) { | 54 | if (!fakeRef) { | ||
62 | Q_EMIT jobFetchMetadataFailed(); | 55 | return {}; | ||
63 | return; | | |||
64 | } | 56 | } | ||
65 | 57 | | |||
66 | data = flatpak_installation_fetch_remote_metadata_sync(m_installation, m_app->origin().toUtf8().constData(), fakeRef, m_cancellable, &localError); | 58 | QByteArray metadataContent; | ||
59 | g_autoptr(GBytes) data = flatpak_installation_fetch_remote_metadata_sync(installation, app->origin().toUtf8().constData(), fakeRef, cancellable, &localError); | ||||
67 | if (data) { | 60 | if (data) { | ||
68 | gsize len = 0; | 61 | gsize len = 0; | ||
69 | metadataContent = QByteArray((char *)g_bytes_get_data(data, &len)); | 62 | metadataContent = QByteArray((char *)g_bytes_get_data(data, &len)); | ||
70 | } else { | 63 | } else { | ||
71 | qWarning() << "Failed to get metadata file: " << localError->message; | 64 | qWarning() << "Failed to get metadata file: " << localError->message; | ||
72 | Q_EMIT jobFetchMetadataFailed(); | 65 | return {}; | ||
73 | return; | | |||
74 | } | 66 | } | ||
75 | 67 | | |||
76 | if (metadataContent.isEmpty()) { | 68 | if (metadataContent.isEmpty()) { | ||
77 | qWarning() << "Failed to get metadata file: empty metadata"; | 69 | qWarning() << "Failed to get metadata file: empty metadata"; | ||
78 | Q_EMIT jobFetchMetadataFailed(); | 70 | return {}; | ||
79 | return; | | |||
80 | } | | |||
81 | | ||||
82 | Q_EMIT jobFetchMetadataFinished(m_installation, m_app, metadataContent); | | |||
83 | } else if (m_kind == FetchSize) { | | |||
84 | guint64 downloadSize = 0; | | |||
85 | guint64 installedSize = 0; | | |||
86 | g_autoptr(FlatpakRef) ref = nullptr; | | |||
87 | | ||||
88 | ref = createFakeRef(m_app); | | |||
89 | if (!ref) { | | |||
90 | Q_EMIT jobFetchSizeFailed(); | | |||
91 | return; | | |||
92 | } | | |||
93 | | ||||
94 | if (!flatpak_installation_fetch_remote_size_sync(m_installation, m_app->origin().toUtf8().constData(), | | |||
95 | ref, &downloadSize, &installedSize, m_cancellable, &localError)) { | | |||
96 | qWarning() << "Failed to get remote size of " << m_app->name() << ": " << localError->message; | | |||
97 | Q_EMIT jobFetchSizeFailed(); | | |||
98 | return; | | |||
99 | } | 71 | } | ||
100 | 72 | | |||
101 | Q_EMIT jobFetchSizeFinished(m_app, downloadSize, installedSize); | 73 | return metadataContent; | ||
102 | } | | |||
103 | } | 74 | } | ||
104 | 75 | | |||
105 | FlatpakRef * FlatpakFetchDataJob::createFakeRef(FlatpakResource *resource) | 76 | SizeInformation fetchFlatpakSize(FlatpakInstallation *installation, FlatpakResource *app) | ||
106 | { | 77 | { | ||
107 | FlatpakRef *ref = nullptr; | 78 | g_autoptr(GCancellable) cancellable = g_cancellable_new(); | ||
108 | g_autoptr(GError) localError = nullptr; | 79 | g_autoptr(GError) localError = nullptr; | ||
109 | 80 | | |||
110 | const QString id = QString::fromUtf8("%1/%2/%3/%4").arg(resource->typeAsString()).arg(resource->flatpakName()).arg(resource->arch()).arg(resource->branch()); | 81 | SizeInformation ret; | ||
111 | ref = flatpak_ref_parse(id.toUtf8().constData(), &localError); | 82 | g_autoptr(FlatpakRef) ref = createFakeRef(app); | ||
112 | | ||||
113 | if (!ref) { | 83 | if (!ref) { | ||
114 | qWarning() << "Failed to create fake ref: " << localError->message; | 84 | return ret; | ||
85 | } | ||||
86 | | ||||
87 | if (!flatpak_installation_fetch_remote_size_sync(installation, app->origin().toUtf8().constData(), ref, &ret.downloadSize, &ret.installedSize, cancellable, &localError)) { | ||||
88 | qWarning() << "Failed to get remote size of " << app->name() << ": " << localError->message; | ||||
89 | return ret; | ||||
90 | } | ||||
91 | | ||||
92 | ret.valid = true; | ||||
93 | return ret; | ||||
115 | } | 94 | } | ||
116 | 95 | | |||
117 | return ref; | | |||
118 | } | 96 | } |