Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

Commit 6ea64d5e authored by Aleix Pol Gonzalez's avatar Aleix Pol Gonzalez 🐧

Drop virtual AbstractResourcesBackend::resourceForFile

We already have it in ::search() with async API which always gives a a
slightly better API.
parent d23440ff
......@@ -236,22 +236,26 @@ void DiscoverObject::openLocalPackage(const QUrl& localfile)
setRootObjectProperty("defaultStartup", false);
auto action = new OneTimeAction(
[this, localfile]() {
auto res = ResourcesModel::global()->resourceForFile(localfile);
qCDebug(DISCOVER_LOG) << "all initialized..." << res;
if (res) {
emit openApplicationInternal(res);
} else {
QMimeDatabase db;
auto mime = db.mimeTypeForUrl(localfile);
auto fIsFlatpakBackend = [](AbstractResourcesBackend* backend) { return backend->metaObject()->className() == QByteArray("FlatpakBackend"); };
if (mime.name().startsWith(QLatin1String("application/vnd.flatpak")) && !kContains(ResourcesModel::global()->backends(), fIsFlatpakBackend)) {
openApplication(QUrl(QLatin1String("appstream://org.kde.discover.flatpak")));
showPassiveNotification(i18n("Cannot interact with flatpak resources without the flatpak backend %1. Please install it first.", localfile.toDisplayString()));
AbstractResourcesBackend::Filters f;
f.resourceUrl = localfile;
auto stream = new StoredResultsStream({ResourcesModel::global()->search(f)});
connect(stream, &StoredResultsStream::finished, this, [this, localfile, stream]() {
const auto res = stream->resources();
if (res.count() == 1) {
emit openApplicationInternal(res.first());
} else {
setRootObjectProperty("defaultStartup", true);
showPassiveNotification(i18n("Couldn't open %1", localfile.toDisplayString()));
QMimeDatabase db;
auto mime = db.mimeTypeForUrl(localfile);
auto fIsFlatpakBackend = [](AbstractResourcesBackend* backend) { return backend->metaObject()->className() == QByteArray("FlatpakBackend"); };
if (mime.name().startsWith(QLatin1String("application/vnd.flatpak")) && !kContains(ResourcesModel::global()->backends(), fIsFlatpakBackend)) {
openApplication(QUrl(QLatin1String("appstream://org.kde.discover.flatpak")));
showPassiveNotification(i18n("Cannot interact with flatpak resources without the flatpak backend %1. Please install it first.", localfile.toDisplayString()));
} else {
setRootObjectProperty("defaultStartup", true);
showPassiveNotification(i18n("Couldn't open %1", localfile.toDisplayString()));
}
}
}
});
}
, this);
......
......@@ -123,6 +123,15 @@ ResultsStream* DummyBackend::search(const AbstractResourcesBackend::Filters& fil
ResultsStream * DummyBackend::findResourceByPackageName(const QUrl& search)
{
if(search.isLocalFile()) {
DummyResource* res = new DummyResource(search.fileName(), AbstractResource::Technical, this);
res->setSize(666);
res->setState(AbstractResource::None);
m_resources.insert(res->packageName(), res);
connect(res, &DummyResource::stateChanged, this, &DummyBackend::updatesCountChanged);
return new ResultsStream(QStringLiteral("DummyStream-local"), { res });
}
auto res = search.scheme() == QLatin1String("dummy") ? m_resources.value(search.host().replace(QLatin1Char('.'), QLatin1Char(' '))) : nullptr;
if (!res) {
return new ResultsStream(QStringLiteral("DummyStream"), {});
......@@ -165,16 +174,6 @@ void DummyBackend::checkForUpdates()
qDebug() << "DummyBackend::checkForUpdates";
}
AbstractResource * DummyBackend::resourceForFile(const QUrl& path)
{
DummyResource* res = new DummyResource(path.fileName(), AbstractResource::Technical, this);
res->setSize(666);
res->setState(AbstractResource::None);
m_resources.insert(res->packageName(), res);
connect(res, &DummyResource::stateChanged, this, &DummyBackend::updatesCountChanged);
return res;
}
QString DummyBackend::displayName() const
{
return QStringLiteral("Dummy");
......
......@@ -47,7 +47,6 @@ public:
Transaction* installApplication(AbstractResource* app, const AddonList& addons) override;
Transaction* removeApplication(AbstractResource* app) override;
bool isFetching() const override { return m_fetching; }
AbstractResource * resourceForFile(const QUrl & ) override;
void checkForUpdates() override;
QString displayName() const override;
bool hasApplications() const override;
......
......@@ -152,21 +152,34 @@ public:
const QUrl fileUrl = QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::TempLocation) + QLatin1Char('/') + originalUrl.fileName());
auto replyPut = put(QNetworkRequest(fileUrl), replyGet->readAll());
connect(replyPut, &QNetworkReply::finished, this, [this, originalUrl, fileUrl, replyPut]() {
if (replyPut->error() == QNetworkReply::NoError) {
auto res = m_backend->resourceForFile(fileUrl);
if (res) {
FlatpakResource *resource = qobject_cast<FlatpakResource*>(res);
resource->setResourceFile(originalUrl);
Q_EMIT jobFinished(true, resource);
} else {
qWarning() << "couldn't create resource from" << fileUrl.toLocalFile();
Q_EMIT jobFinished(false, nullptr);
}
} else {
if (replyPut->error() != QNetworkReply::NoError) {
qWarning() << "couldn't save" << originalUrl << replyPut->errorString();
Q_EMIT jobFinished(false, nullptr);
return;
}
if (!fileUrl.isLocalFile()) {
Q_EMIT jobFinished(false, nullptr);
return;
}
FlatpakResource *resource = nullptr;
if (fileUrl.path().endsWith(QLatin1String(".flatpak"))) {
resource = m_backend->addAppFromFlatpakBundle(fileUrl);
} else if (fileUrl.path().endsWith(QLatin1String(".flatpakref"))) {
resource = m_backend->addAppFromFlatpakRef(fileUrl);
} else if (fileUrl.path().endsWith(QLatin1String(".flatpakrepo"))) {
resource = m_backend->addSourceFromFlatpakRepo(fileUrl);
}
if (resource) {
resource->setResourceFile(originalUrl);
Q_EMIT jobFinished(true, resource);
} else {
qWarning() << "couldn't create resource from" << fileUrl.toLocalFile();
Q_EMIT jobFinished(false, nullptr);
}
});
}
);
});
}
......@@ -386,7 +399,7 @@ FlatpakResource * FlatpakBackend::addAppFromFlatpakRef(const QUrl &url)
remoteRef = flatpak_installation_install_ref_file (preferredInstallation(), bytes, m_cancellable, &error);
if (!remoteRef) {
qWarning() << "Failed to create install ref file:" << error->message;
const auto resources = resourcesByAppstreamName(settings.value(QStringLiteral("Flatpak Ref/Name")).toString());
const auto resources = resourcesByAppstreamName(name);
if (!resources.isEmpty()) {
return qobject_cast<FlatpakResource*>(resources.constFirst());
}
......@@ -403,7 +416,7 @@ FlatpakResource * FlatpakBackend::addAppFromFlatpakRef(const QUrl &url)
asComponent.setDescription(settings.value(QStringLiteral("Flatpak Ref/Description")).toString());
asComponent.setName(settings.value(QStringLiteral("Flatpak Ref/Title")).toString());
asComponent.setSummary(settings.value(QStringLiteral("Flatpak Ref/Comment")).toString());
asComponent.setId(settings.value(QStringLiteral("Flatpak Ref/Name")).toString());
asComponent.setId(name);
const QString iconUrl = settings.value(QStringLiteral("Flatpak Ref/Icon")).toString();
if (!iconUrl.isEmpty()) {
......@@ -1084,7 +1097,7 @@ bool FlatpakBackend::flatpakResourceLessThan(AbstractResource* l, AbstractResour
ResultsStream * FlatpakBackend::search(const AbstractResourcesBackend::Filters &filter)
{
if (filter.resourceUrl.fileName().endsWith(QLatin1String(".flatpakrepo")) || filter.resourceUrl.fileName().endsWith(QLatin1String(".flatpakref"))) {
if (filter.resourceUrl.fileName().endsWith(QLatin1String(".flatpakrepo")) || filter.resourceUrl.fileName().endsWith(QLatin1String(".flatpakref")) || filter.resourceUrl.fileName().endsWith(QLatin1String(".flatpak"))) {
auto stream = new ResultsStream(QStringLiteral("FlatpakStream-http-")+filter.resourceUrl.fileName());
FlatpakFetchRemoteResourceJob *fetchResourceJob = new FlatpakFetchRemoteResourceJob(filter.resourceUrl, this);
......@@ -1250,24 +1263,6 @@ void FlatpakBackend::checkForUpdates()
}
}
AbstractResource * FlatpakBackend::resourceForFile(const QUrl &url)
{
if (!url.isLocalFile()) {
return nullptr;
}
FlatpakResource *resource = nullptr;
if (url.path().endsWith(QLatin1String(".flatpak"))) {
resource = addAppFromFlatpakBundle(url);
} else if (url.path().endsWith(QLatin1String(".flatpakref"))) {
resource = addAppFromFlatpakRef(url);
} else if (url.path().endsWith(QLatin1String(".flatpakrepo"))) {
resource = addSourceFromFlatpakRepo(url);
}
return resource;
}
QString FlatpakBackend::displayName() const
{
return QStringLiteral("Flatpak");
......
......@@ -58,13 +58,15 @@ public:
Transaction* installApplication(AbstractResource* app, const AddonList& addons) override;
Transaction* removeApplication(AbstractResource* app) override;
bool isFetching() const override { return m_isFetching>0; }
AbstractResource * resourceForFile(const QUrl & ) override;
void checkForUpdates() override;
QString displayName() const override;
bool hasApplications() const override { return true; }
FlatpakResource * addSourceFromFlatpakRepo(const QUrl &url);
QStringList extends() const override { return m_extends; }
FlatpakResource * addAppFromFlatpakBundle(const QUrl &url);
FlatpakResource * addAppFromFlatpakRef(const QUrl &url);
private Q_SLOTS:
void onFetchMetadataFinished(FlatpakInstallation *flatpakInstallation, FlatpakResource *resource, const QByteArray &metadata);
void onFetchSizeFinished(FlatpakResource *resource, guint64 downloadSize, guint64 installedSize);
......@@ -83,8 +85,6 @@ private:
FlatpakResource * getAppForInstalledRef(FlatpakInstallation *flatpakInstallation, FlatpakInstalledRef *ref) const;
FlatpakResource * getRuntimeForApp(FlatpakResource *resource) const;
FlatpakResource * addAppFromFlatpakBundle(const QUrl &url);
FlatpakResource * addAppFromFlatpakRef(const QUrl &url);
void addResource(FlatpakResource *resource);
void loadAppsFromAppstreamData();
bool loadAppsFromAppstreamData(FlatpakInstallation *flatpakInstallation);
......
......@@ -459,7 +459,7 @@ ResultsStream* FwupdBackend::search(const AbstractResourcesBackend::Filters& fil
if (filter.resourceUrl.scheme() == QLatin1String("fwupd")) {
return findResourceByPackageName(filter.resourceUrl);
} else if (!filter.resourceUrl.isEmpty()) {
return new ResultsStream(QStringLiteral("FwupdStream-void"), {});
return resourceForFile(filter.resourceUrl);
}
auto stream = new ResultsStream(QStringLiteral("FwupdStream"));
......@@ -523,7 +523,7 @@ Transaction* FwupdBackend::removeApplication(AbstractResource* /*app*/)
return nullptr;
}
AbstractResource * FwupdBackend::resourceForFile(const QUrl& path)
ResultsStream* FwupdBackend::resourceForFile(const QUrl& path)
{
g_autoptr(GError) error = nullptr;
......@@ -549,13 +549,14 @@ AbstractResource * FwupdBackend::resourceForFile(const QUrl& path)
}
addResourceToList(app);
connect(app, &FwupdResource::stateChanged, this, &FwupdBackend::updatesCountChanged);
return new ResultsStream(QStringLiteral("FwupdStream-file"), {app});
}
else
{
handleError(error);
}
}
return app;
return new ResultsStream(QStringLiteral("FwupdStream-void"), {});
}
QString FwupdBackend::displayName() const
......
......@@ -71,7 +71,6 @@ public:
Transaction* installApplication(AbstractResource* app, const AddonList& addons) override;
Transaction* removeApplication(AbstractResource* app) override;
bool isFetching() const override { return m_fetching; }
AbstractResource * resourceForFile(const QUrl & ) override;
void checkForUpdates() override;
QString displayName() const override;
bool hasApplications() const override;
......@@ -82,6 +81,7 @@ Q_SIGNALS:
void initialized();
private:
ResultsStream* resourceForFile(const QUrl & );
void refreshRemotes();
void addUpdates();
void addResourceToList(FwupdResource *res);
......
......@@ -442,7 +442,17 @@ ResultsStream* PackageKitBackend::search(const AbstractResourcesBackend::Filters
ResultsStream * PackageKitBackend::findResourceByPackageName(const QUrl& url)
{
AbstractResource* pkg = nullptr;
if (url.host().isEmpty())
if (url.isLocalFile()) {
QMimeDatabase db;
const auto mime = db.mimeTypeForUrl(url);
if ( mime.inherits(QLatin1String("application/vnd.debian.binary-package"))
|| mime.inherits(QLatin1String("application/x-rpm"))
|| mime.inherits(QLatin1String("application/x-tar"))
|| mime.inherits(QLatin1String("application/x-xz-compressed-tar"))
) {
pkg = new LocalFilePKResource(url, this);
}
} else if (url.host().isEmpty())
Q_EMIT passiveMessage(i18n("Malformed appstream url '%1'", url.toDisplayString()));
else if (url.scheme() == QLatin1String("appstream")) {
static const QMap<QString, QString> deprecatedAppstreamIds = {
......@@ -639,20 +649,6 @@ AbstractReviewsBackend* PackageKitBackend::reviewsBackend() const
return m_reviews.data();
}
AbstractResource * PackageKitBackend::resourceForFile(const QUrl& file)
{
QMimeDatabase db;
const auto mime = db.mimeTypeForUrl(file);
if ( mime.inherits(QLatin1String("application/vnd.debian.binary-package"))
|| mime.inherits(QLatin1String("application/x-rpm"))
|| mime.inherits(QLatin1String("application/x-tar"))
|| mime.inherits(QLatin1String("application/x-xz-compressed-tar"))
) {
return new LocalFilePKResource(file, this);
}
return nullptr;
}
static QString readDistroName()
{
const QStringList osreleasenames = (QStringList() << QStringLiteral("/etc/os-release")
......
......@@ -67,7 +67,6 @@ class DISCOVERCOMMON_EXPORT PackageKitBackend : public AbstractResourcesBackend
void fetchDetails(const QString& pkgid) { fetchDetails(QSet<QString>{pkgid}); }
void fetchDetails(const QSet<QString>& pkgid);
AbstractResource * resourceForFile(const QUrl & ) override;
void checkForUpdates() override;
QString displayName() const override;
......
......@@ -159,8 +159,6 @@ class DISCOVERCOMMON_EXPORT AbstractResourcesBackend : public QObject
*/
void emitRatingsReady();
virtual AbstractResource* resourceForFile(const QUrl &/*url*/) { return nullptr; }
/**
* @returns the root category tree
*/
......
......@@ -336,17 +336,6 @@ AggregatedResultsStream* ResourcesModel::search(const AbstractResourcesBackend::
return new AggregatedResultsStream(streams);
}
AbstractResource* ResourcesModel::resourceForFile(const QUrl& file)
{
AbstractResource* ret = nullptr;
foreach(auto backend, m_backends) {
ret = backend->resourceForFile(file);
if (ret)
break;
}
return ret;
}
void ResourcesModel::checkForUpdates()
{
for(auto backend: qAsConst(m_backends))
......
......@@ -78,7 +78,6 @@ class DISCOVERCOMMON_EXPORT ResourcesModel : public QObject
Q_SCRIPTABLE bool isExtended(const QString &id);
AggregatedResultsStream* search(const AbstractResourcesBackend::Filters &search);
AbstractResource* resourceForFile(const QUrl &/*url*/);
void checkForUpdates();
QVariantList applicationBackendsVariant() const;
......
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