Commit 4b755e94 authored by Aleix Pol Gonzalez's avatar Aleix Pol Gonzalez 🐧

Make it possible to pass remote flatpakref and flatpakrepo files

They will be resolved during the ResultsStream look-up
parent 36095ea6
......@@ -53,6 +53,7 @@
#include <QTimer>
#include <QTextStream>
#include <QTemporaryFile>
#include <QNetworkAccessManager>
#include <glib.h>
......@@ -364,6 +365,7 @@ FlatpakResource * FlatpakBackend::addAppFromFlatpakRef(const QUrl &url)
FlatpakResource * FlatpakBackend::addSourceFromFlatpakRepo(const QUrl &url)
{
Q_ASSERT(url.isLocalFile());
QSettings settings(url.toLocalFile(), QSettings::NativeFormat);
const QString gpgKey = settings.value(QStringLiteral("Flatpak Repo/GPGKey")).toString();
......@@ -899,7 +901,39 @@ int FlatpakBackend::updatesCount() const
ResultsStream * FlatpakBackend::search(const AbstractResourcesBackend::Filters &filter)
{
if (!filter.resourceUrl.isEmpty() && filter.resourceUrl.scheme() != QLatin1String("appstream"))
if (filter.resourceUrl.fileName().endsWith(QLatin1String(".flatpakrepo")) || filter.resourceUrl.fileName().endsWith(QLatin1String(".flatpakref"))) {
auto stream = new ResultsStream(QStringLiteral("FlatpakStream-http-")+filter.resourceUrl.fileName());
QNetworkAccessManager* manager = new QNetworkAccessManager;
auto replyGet = manager->get(QNetworkRequest(filter.resourceUrl));
connect(replyGet, &QNetworkReply::finished, this, [this, manager, replyGet, stream] {
const QUrl originalUrl = replyGet->request().url();
if (replyGet->error() != QNetworkReply::NoError) {
qWarning() << "couldn't download" << originalUrl << replyGet->errorString();
stream->finish();
return;
}
const QUrl fileUrl = QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation) + QLatin1Char('/') + originalUrl.fileName());
auto replyPut = manager->put(QNetworkRequest(fileUrl), replyGet->readAll());
connect(replyPut, &QNetworkReply::finished, this, [this, originalUrl, fileUrl, replyPut, stream, manager]() {
if (replyPut->error() == QNetworkReply::NoError) {
auto res = resourceForFile(fileUrl);
if (res) {
FlatpakResource* fres = qobject_cast<FlatpakResource*>(res);
fres->setResourceFile(originalUrl);
stream->resourcesFound({ res });
} else {
qWarning() << "couldn't download" << originalUrl << "into" << fileUrl << replyPut->errorString();
}
}
stream->finish();
manager->deleteLater();
});
});
return stream;
} else if (!filter.resourceUrl.isEmpty() && filter.resourceUrl.scheme() != QLatin1String("appstream"))
return new ResultsStream(QStringLiteral("FlatpakStream-void"), {});
QVector<AbstractResource*> ret;
......
......@@ -510,3 +510,8 @@ QString FlatpakResource::installationPath(FlatpakInstallation* flatpakInstallati
g_autoptr(GFile) path = flatpak_installation_get_path(flatpakInstallation);
return QString::fromUtf8(g_file_get_path(path));
}
QUrl FlatpakResource::url() const
{
return m_resourceFile.isEmpty() ? QUrl(QStringLiteral("appstream://") + appstreamId()) : m_resourceFile;
}
......@@ -101,6 +101,7 @@ public:
ResourceType type() const;
QString typeAsString() const;
QString uniqueId() const;
QUrl url() const override;
FlatpakInstallation* installation() const { return m_installation; }
......
......@@ -30,6 +30,7 @@
#include <glib.h>
#include <QTemporaryFile>
#include <QStandardPaths>
#include <resources/StoredResultsStream.h>
class FlatpakSourceItem : public QStandardItem
{
......@@ -64,39 +65,29 @@ QAbstractItemModel* FlatpakSourcesBackend::sources()
return m_sources;
}
static void addRepo(FlatpakBackend* backend, const QUrl &fileUrl)
{
Q_ASSERT(backend);
auto res = backend->addSourceFromFlatpakRepo(fileUrl);
if (!res) {
qWarning() << "Couldn't add" << fileUrl;
return;
}
backend->installApplication(res);
}
bool FlatpakSourcesBackend::addSource(const QString &id)
{
FlatpakBackend* backend = qobject_cast<FlatpakBackend*>(parent());
const QUrl flatpakrepoUrl = QUrl::fromUserInput(id);
if (flatpakrepoUrl.isLocalFile()) {
addRepo(backend, flatpakrepoUrl);
auto res = backend->addSourceFromFlatpakRepo(flatpakrepoUrl);
if (res)
backend->installApplication(res);
else
backend->passiveMessage(i18n("Could not add the source %1", flatpakrepoUrl.toDisplayString()));
} else {
const QUrl fileUrl = QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation) + QLatin1Char('/') + flatpakrepoUrl.fileName());
QNetworkAccessManager* manager = new QNetworkAccessManager(this);
auto replyGet = manager->get(QNetworkRequest(flatpakrepoUrl));
connect(replyGet, &QNetworkReply::finished, this, [this, manager, replyGet, fileUrl, backend] {
if (replyGet->error() != QNetworkReply::NoError)
return;
auto replyPut = manager->put(QNetworkRequest(fileUrl), replyGet->readAll());
connect(replyPut, &QNetworkReply::finished, this, [fileUrl, backend, manager]() {
addRepo(backend, fileUrl);
delete manager;
});
AbstractResourcesBackend::Filters filter;
filter.resourceUrl = flatpakrepoUrl;
auto stream = new StoredResultsStream ({backend->search(filter)});
connect(stream, &StoredResultsStream::finished, this, [backend, stream, flatpakrepoUrl]() {
const auto res = stream->resources();
if (!res.isEmpty()) {
Q_ASSERT(res.count() == 1);
backend->installApplication(res.first());
} else {
backend->passiveMessage(i18n("Could not add the source %1", flatpakrepoUrl.toDisplayString()));
}
});
}
return true;
......
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