diff --git a/libdiscover/backends/SnapBackend/SnapBackend.cpp b/libdiscover/backends/SnapBackend/SnapBackend.cpp index c605aafc..464fa028 100644 --- a/libdiscover/backends/SnapBackend/SnapBackend.cpp +++ b/libdiscover/backends/SnapBackend/SnapBackend.cpp @@ -1,213 +1,215 @@ /*************************************************************************** * 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 "SnapReviewsBackend.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include "utils.h" DISCOVER_BACKEND_PLUGIN(SnapBackend) SnapBackend::SnapBackend(QObject* parent) : AbstractResourcesBackend(parent) , m_updater(new StandardBackendUpdater(this)) , m_reviews(new SnapReviewsBackend(this)) { { auto request = m_client.connect(); request->runSync(); m_valid = request->error() == QSnapdRequest::NoError; if (!m_valid) { qWarning() << "snap problem at initialize:" << request->errorString(); return; } } connect(m_reviews, &SnapReviewsBackend::ratingsReady, this, &AbstractResourcesBackend::emitRatingsReady); //make sure we populate the installed resources first populate(m_client.list(), AbstractResource::Installed); SourcesModel::global()->addBackend(this); } 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) { return populate(m_client.list(), AbstractResource::Installed); } else { return populate(m_client.find(QSnapdClient::FindFlag::None, filters.search), AbstractResource::None); } return voidStream(); } ResultsStream * SnapBackend::findResourceByPackageName(const QUrl& search) { return search.scheme() == QLatin1String("snap") ? populate(m_client.listOne(search.host()), AbstractResource::None) : voidStream(); } ResultsStream * SnapBackend::populate(QSnapdListOneRequest* job, AbstractResource::State state) { auto stream = new ResultsStream(QStringLiteral("Snap-populateOne")); connect(job, &QSnapdFindRequest::complete, stream, [stream, this, state, job]() { if (job->error()) { qDebug() << "error:" << job->error() << job->errorString(); stream->finish(); return; } QSet resources; QSharedPointer snap(job->snap()); const auto snapname = snap->name(); SnapResource* res = m_resources.value(snapname); if (!res) { res = new SnapResource(snap, state, this); Q_ASSERT(res->packageName() == snapname); resources += res; } else if (res->state() < state) { res->setState(state); + res->setSnap(snap); } m_resources[res->packageName()] = res; stream->resourcesFound({res}); stream->finish(); }); job->runAsync(); return stream; } template ResultsStream* SnapBackend::populate(T* job, AbstractResource::State state) { auto stream = new ResultsStream(QStringLiteral("Snap-populate")); connect(job, &QSnapdFindRequest::complete, stream, [stream, this, state, job]() { if (job->error()) { qDebug() << "error:" << job->error() << job->errorString(); stream->finish(); return; } QVector ret; QSet resources; for (int i=0, c=job->snapCount(); i snap(job->snap(i)); const auto snapname = snap->name(); SnapResource* res = m_resources.value(snapname); if (!res) { res = new SnapResource(snap, state, this); Q_ASSERT(res->packageName() == snapname); resources += res; } else if (res->state() < state) { res->setState(state); + res->setSnap(snap); } ret += res; } foreach(SnapResource* res, resources) m_resources[res->packageName()] = res; if (!ret.isEmpty()) 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; } 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(app, m_client.install(app->packageName()), Transaction::InstallRole, AbstractResource::Installed); } Transaction* SnapBackend::removeApplication(AbstractResource* _app) { auto app = qobject_cast(_app); return new SnapTransaction(app, m_client.remove(app->packageName()), Transaction::RemoveRole, AbstractResource::None); } QString SnapBackend::displayName() const { return QStringLiteral("Snap"); } void SnapBackend::refreshStates() { populate(m_client.list(), AbstractResource::Installed); } #include "SnapBackend.moc" diff --git a/libdiscover/backends/SnapBackend/SnapResource.cpp b/libdiscover/backends/SnapBackend/SnapResource.cpp index 80a44a39..8a603e41 100644 --- a/libdiscover/backends/SnapBackend/SnapResource.cpp +++ b/libdiscover/backends/SnapBackend/SnapResource.cpp @@ -1,165 +1,177 @@ /*************************************************************************** * 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 "SnapResource.h" #include "SnapBackend.h" #include #include #include #include SnapResource::SnapResource(QSharedPointer snap, AbstractResource::State state, SnapBackend* parent) : AbstractResource(parent) , m_state(state) , m_snap(snap) { } QString SnapResource::availableVersion() const { return installedVersion(); } QStringList SnapResource::categories() { return { QStringLiteral("Application") }; } QString SnapResource::comment() { return m_snap->summary(); } int SnapResource::size() { return isInstalled() ? m_snap->installedSize() : m_snap->downloadSize(); } QUrl SnapResource::homepage() { return {}; } QVariant SnapResource::icon() const { const auto iconPath = m_snap->icon(); if (iconPath.isEmpty()) return QStringLiteral("package-x-generic"); if (!iconPath.startsWith(QLatin1Char('/'))) return QUrl(iconPath); auto backend = qobject_cast(parent()); auto req = backend->client()->getIcon(packageName()); req->runSync(); if (req->error()) { qWarning() << "icon error" << req->errorString() << iconPath; return {}; } auto icon = req->icon(); QBuffer buffer; buffer.setData(icon->data()); QImageReader reader(&buffer); return QVariant::fromValue(reader.read()); } QString SnapResource::installedVersion() const { return m_snap->version(); } QString SnapResource::license() { return m_snap->license(); } QString SnapResource::longDescription() { return m_snap->description(); } QString SnapResource::name() { return m_snap->title().isEmpty() ? m_snap->name() : m_snap->title(); } QString SnapResource::origin() const { return QStringLiteral("snappy:") + m_snap->channel(); } QString SnapResource::packageName() const { return m_snap->name(); } QString SnapResource::section() { return QStringLiteral("snap"); } AbstractResource::State SnapResource::state() { return m_state; } void SnapResource::setState(AbstractResource::State state) { if (m_state != state) { m_state = state; Q_EMIT stateChanged(); } } void SnapResource::fetchChangelog() { QString log; emit changelogFetched(log); } void SnapResource::fetchScreenshots() { QList screenshots; for(int i = 0, c = m_snap->screenshotCount(); i screenshot(m_snap->screenshot(i)); screenshots << QUrl(screenshot->url()); } Q_EMIT screenshotsFetched(screenshots, screenshots); } void SnapResource::invokeApplication() const { // QProcess::startDetached(m_snap->price()); } bool SnapResource::isTechnical() const { return m_snap->snapType() != QLatin1String("app"); } QUrl SnapResource::url() const { //FIXME interim, until it has an appstreamId return QUrl(QStringLiteral("snap://") + packageName()); } + +void SnapResource::setSnap(const QSharedPointer& snap) +{ + Q_ASSERT(snap->name() == m_snap->name()); + if (m_snap == snap) + return; + + const bool newSize = m_snap->installedSize() != snap->installedSize() || m_snap->downloadSize() != snap->downloadSize(); + m_snap = snap; + if (newSize) + Q_EMIT sizeChanged(); +} diff --git a/libdiscover/backends/SnapBackend/SnapResource.h b/libdiscover/backends/SnapBackend/SnapResource.h index 60d51085..ac20311c 100644 --- a/libdiscover/backends/SnapBackend/SnapResource.h +++ b/libdiscover/backends/SnapBackend/SnapResource.h @@ -1,68 +1,69 @@ /*************************************************************************** * 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 . * ***************************************************************************/ #ifndef SNAPRESOURCE_H #define SNAPRESOURCE_H #include #include #include #include class SnapBackend; class SnapResource : public AbstractResource { Q_OBJECT public: explicit SnapResource(QSharedPointer snap, AbstractResource::State state, SnapBackend* parent); ~SnapResource() override = default; QString section() override; QString origin() const override; QString longDescription() override; QString availableVersion() const override; QString installedVersion() const override; QString license() override; int size() override; QUrl homepage() override; QStringList categories() override; AbstractResource::State state() override; QVariant icon() const override; QString comment() override; QString name() override; QString packageName() const override; bool isTechnical() const override; bool canExecute() const override { return true; } void invokeApplication() const override; void fetchChangelog() override; void fetchScreenshots() override; QList addonsInformation() override { return {}; } QUrl url() const override; + void setSnap(const QSharedPointer &snap); void setState(AbstractResource::State state); public: AbstractResource::State m_state; QSharedPointer m_snap; }; #endif // SNAPRESOURCE_H diff --git a/libdiscover/backends/SnapBackend/SnapTransaction.cpp b/libdiscover/backends/SnapBackend/SnapTransaction.cpp index 701591fc..d90b2ebc 100644 --- a/libdiscover/backends/SnapBackend/SnapTransaction.cpp +++ b/libdiscover/backends/SnapBackend/SnapTransaction.cpp @@ -1,98 +1,98 @@ /*************************************************************************** * 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 "SnapTransaction.h" #include "SnapBackend.h" #include "SnapResource.h" #include #include #include #include #include #include "libsnapclient/config-paths.h" #include "utils.h" SnapTransaction::SnapTransaction(SnapResource* app, QSnapdRequest* request, Role role, AbstractResource::State newState) : Transaction(app, app, role) , m_app(app) , m_request(request) , m_newState(newState) { setCancellable(false); connect(request, &QSnapdRequest::progress, this, &SnapTransaction::progressed); connect(request, &QSnapdRequest::complete, this, &SnapTransaction::finishTransaction); setStatus(SetupStatus); setStatus(DownloadingStatus); request->runAsync(); } void SnapTransaction::cancel() { m_request->cancel(); } void SnapTransaction::finishTransaction() { - qDebug() << "done!"; switch(m_request->error()) { case QSnapdRequest::NoError: + static_cast(m_app->backend())->refreshStates(); m_app->setState(m_newState); break; case QSnapdRequest::AuthDataRequired: { QProcess* p = new QProcess; p->setProgram(QStringLiteral(CMAKE_INSTALL_FULL_LIBEXECDIR "/discover/SnapMacaroonDialog")); p->start(); connect(p, static_cast(&QProcess::finished), this, [this, p] (int code) { p->deleteLater(); if (code != 0) { qWarning() << "login failed... code:" << code << p->readAll(); Q_EMIT passiveMessage(m_request->errorString()); setStatus(DoneStatus); return; } const auto doc = QJsonDocument::fromJson(p->readAllStandardOutput()); const auto result = doc.object(); const auto macaroon = result[QStringLiteral("macaroon")].toString(); const auto discharges = kTransform(result[QStringLiteral("discharges")].toArray(), [](const QJsonValue& val) { return val.toString(); }); static_cast(m_app->backend())->client()->setAuthData(new QSnapdAuthData(macaroon, discharges)); m_request->runAsync(); }); } return; default: Q_EMIT passiveMessage(m_request->errorString()); break; } setStatus(DoneStatus); } void SnapTransaction::progressed() { const auto change = m_request->change(); int percentage = 0, count = 0; for(int i = 0, c = change->taskCount(); itask(i)->progressDone()) / change->task(i)->progressTotal(); } setProgress(percentage / qMax(count, 1)); }