diff --git a/libdiscover/backends/SnapBackend/SnapBackend.cpp b/libdiscover/backends/SnapBackend/SnapBackend.cpp index ea3dfb1e..b0bcbf5c 100644 --- a/libdiscover/backends/SnapBackend/SnapBackend.cpp +++ b/libdiscover/backends/SnapBackend/SnapBackend.cpp @@ -1,228 +1,228 @@ /*************************************************************************** * Copyright © 2013 Aleix Pol Gonzalez * * * * This program is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License as * * published by the Free Software Foundation; either version 2 of * * the License or (at your option) version 3 or any later version * * accepted by the membership of KDE e.V. (or its successor approved * * by the membership of KDE e.V.), which shall act as a proxy * * defined in Section 14 of version 3 of the license. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program. If not, see . * ***************************************************************************/ #include "SnapBackend.h" #include "SnapTransaction.h" #include "SnapResource.h" #include "appstream/AppStreamIntegration.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "utils.h" DISCOVER_BACKEND_PLUGIN(SnapBackend) class SnapSourcesBackend : public AbstractSourcesBackend { public: explicit SnapSourcesBackend(AbstractResourcesBackend * parent) : AbstractSourcesBackend(parent), m_model(new QStandardItemModel(this)) { auto it = new QStandardItem(i18n("Snap")); it->setData(QStringLiteral("Snap"), IdRole); m_model->appendRow(it); } QAbstractItemModel* sources() override { return m_model; } bool addSource(const QString& /*id*/) override { return false; } bool removeSource(const QString& /*id*/) override { return false;} QString idDescription() override { return QStringLiteral("Snap"); } QVariantList actions() const override { return {}; } bool supportsAdding() const override { return false; } bool canMoveSources() const override { return false; } private: QStandardItemModel* const m_model; }; SnapBackend::SnapBackend(QObject* parent) : AbstractResourcesBackend(parent) , m_updater(new StandardBackendUpdater(this)) , m_reviews(AppStreamIntegration::global()->reviews()) { connect(m_reviews.data(), &OdrsReviewsBackend::ratingsReady, this, [this] { m_reviews->emitRatingFetched(this, kTransform>(m_resources.values(), [] (AbstractResource* r) { return r; })); }); //make sure we populate the installed resources first refreshStates(); SourcesModel::global()->addSourcesBackend(new SnapSourcesBackend(this)); } SnapBackend::~SnapBackend() = default; int SnapBackend::updatesCount() const { return m_updater->updatesCount(); } static ResultsStream* voidStream() { return new ResultsStream(QStringLiteral("Snap-void"), {}); } ResultsStream * SnapBackend::search(const AbstractResourcesBackend::Filters& filters) { if (!filters.extends.isEmpty()) { return voidStream(); } else if (!filters.resourceUrl.isEmpty()) { return findResourceByPackageName(filters.resourceUrl); } else if (filters.category && filters.category->isAddons()) { return voidStream(); } else if (filters.state >= AbstractResource::Installed || filters.origin == QLatin1String("Snap")) { std::function&)> f = [filters](const QSharedPointer& s) { return filters.search.isEmpty() || s->name().contains(filters.search, Qt::CaseInsensitive) || s->description().contains(filters.search, Qt::CaseInsensitive); }; return populateWithFilter(m_client.getSnaps(), f); } else if (!filters.search.isEmpty()) { return populate(m_client.find(QSnapdClient::FindFlag::None, filters.search)); } return voidStream(); } ResultsStream * SnapBackend::findResourceByPackageName(const QUrl& search) { - Q_ASSERT(!search.host().isEmpty()); + Q_ASSERT(!search.host().isEmpty() || !AppStreamUtils::appstreamId(search).isEmpty()); return search.scheme() == QLatin1String("snap") ? populate(m_client.find(QSnapdClient::MatchName, search.host())) : #ifdef SNAP_FIND_COMMON_ID search.scheme() == QLatin1String("appstream") ? populate(m_client.find(QSnapdClient::MatchCommonId, AppStreamUtils::appstreamId(search))) : #endif voidStream(); } template ResultsStream* SnapBackend::populate(T* snaps) { std::function&)> acceptAll = [](const QSharedPointer&){ return true; }; return populateWithFilter(snaps, acceptAll); } template ResultsStream* SnapBackend::populateWithFilter(T* job, std::function& s)>& filter) { auto stream = new ResultsStream(QStringLiteral("Snap-populate")); connect(job, &T::complete, stream, [stream, this, job, filter]() { if (job->error()) { qDebug() << "error:" << job->error() << job->errorString(); stream->finish(); return; } QVector ret; QVector resources; ret.reserve(job->snapCount()); resources.reserve(job->snapCount()); for (int i=0, c=job->snapCount(); i snap(job->snap(i)); if (!filter(snap)) continue; const auto snapname = snap->name(); SnapResource* res = m_resources.value(snapname); if (!res) { res = new SnapResource(snap, AbstractResource::None, this); Q_ASSERT(res->packageName() == snapname); resources += res; } else { res->setSnap(snap); } ret += res; } foreach(SnapResource* res, resources) m_resources[res->packageName()] = res; if (!ret.isEmpty()) Q_EMIT stream->resourcesFound(ret); stream->finish(); }); job->runAsync(); return stream; } void SnapBackend::setFetching(bool fetching) { if (m_fetching != fetching) { m_fetching = fetching; Q_EMIT fetchingChanged(); } else { qWarning() << "fetching already on state" << fetching; } } AbstractBackendUpdater* SnapBackend::backendUpdater() const { return m_updater; } AbstractReviewsBackend* SnapBackend::reviewsBackend() const { return m_reviews.data(); } Transaction* SnapBackend::installApplication(AbstractResource* app, const AddonList& addons) { Q_ASSERT(addons.isEmpty()); return installApplication(app); } Transaction* SnapBackend::installApplication(AbstractResource* _app) { auto app = qobject_cast(_app); return new SnapTransaction(&m_client, app, Transaction::InstallRole, AbstractResource::Installed); } Transaction* SnapBackend::removeApplication(AbstractResource* _app) { auto app = qobject_cast(_app); return new SnapTransaction(&m_client, app, Transaction::RemoveRole, AbstractResource::None); } QString SnapBackend::displayName() const { return QStringLiteral("Snap"); } void SnapBackend::refreshStates() { auto ret = new StoredResultsStream({populate(m_client.getSnaps())}); connect(ret, &StoredResultsStream::finishedResources, this, [this] (const QVector& resources){ for (auto res: qAsConst(m_resources)) { if (resources.contains(res)) res->setState(AbstractResource::Installed); else res->setState(AbstractResource::None); } }); } #include "SnapBackend.moc"