diff --git a/libdiscover/backends/SnapBackend/SnapBackend.cpp b/libdiscover/backends/SnapBackend/SnapBackend.cpp index 276a7a35..9fe0afcc 100644 --- a/libdiscover/backends/SnapBackend/SnapBackend.cpp +++ b/libdiscover/backends/SnapBackend/SnapBackend.cpp @@ -1,190 +1,190 @@ /*************************************************************************** * 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 #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 refreshStates(); 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()); } else if (!filters.search.isEmpty()) { return populate(m_client.find(QSnapdClient::FindFlag::None, filters.search)); } return voidStream(); } ResultsStream * SnapBackend::findResourceByPackageName(const QUrl& search) { return search.scheme() == QLatin1String("snap") ? populate(m_client.find(QSnapdClient::MatchName, search.host())) : voidStream(); } template ResultsStream* SnapBackend::populate(T* job) { auto stream = new ResultsStream(QStringLiteral("Snap-populate")); connect(job, &QSnapdFindRequest::complete, stream, [stream, this, 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, 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()) 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); + return new SnapTransaction(&m_client, app, 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); + 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.list())}); connect(ret, &StoredResultsStream::finished, this, [this, ret](){ for (auto res: m_resources) { if (ret->resources().contains(res)) res->setState(AbstractResource::Installed); else res->setState(AbstractResource::None); } }); } #include "SnapBackend.moc" diff --git a/libdiscover/backends/SnapBackend/SnapTransaction.cpp b/libdiscover/backends/SnapBackend/SnapTransaction.cpp index d90b2ebc..b7655117 100644 --- a/libdiscover/backends/SnapBackend/SnapTransaction.cpp +++ b/libdiscover/backends/SnapBackend/SnapTransaction.cpp @@ -1,98 +1,121 @@ /*************************************************************************** * 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 #include "libsnapclient/config-paths.h" #include "utils.h" -SnapTransaction::SnapTransaction(SnapResource* app, QSnapdRequest* request, Role role, AbstractResource::State newState) +SnapTransaction::SnapTransaction(QSnapdClient* client, SnapResource* app, Role role, AbstractResource::State newState) : Transaction(app, app, role) + , m_client(client) , 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(); + if (role == RemoveRole) + setRequest(m_client->remove(app->packageName())); + else + setRequest(m_client->install(app->packageName())); } void SnapTransaction::cancel() { m_request->cancel(); } void SnapTransaction::finishTransaction() { switch(m_request->error()) { case QSnapdRequest::NoError: static_cast(m_app->backend())->refreshStates(); m_app->setState(m_newState); break; + case QSnapdRequest::NeedsClassic: + if (role() == Transaction::InstallRole) { + Q_EMIT proceedRequest(m_app->name(), i18n("This snap application needs security confinement measures disabled.")); + return; + } + 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: + qDebug() << "snap error" << m_request << m_request->error() << m_request->errorString(); Q_EMIT passiveMessage(m_request->errorString()); break; } setStatus(DoneStatus); } +void SnapTransaction::proceed() +{ + setRequest(m_client->install(QSnapdClient::Classic, m_app->packageName())); +} + +void SnapTransaction::setRequest(QSnapdRequest* req) +{ + m_request.reset(req); + + setCancellable(false); + connect(m_request.data(), &QSnapdRequest::progress, this, &SnapTransaction::progressed); + connect(m_request.data(), &QSnapdRequest::complete, this, &SnapTransaction::finishTransaction); + setStatus(SetupStatus); + + setStatus(DownloadingStatus); + m_request->runAsync(); +} + 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)); } diff --git a/libdiscover/backends/SnapBackend/SnapTransaction.h b/libdiscover/backends/SnapBackend/SnapTransaction.h index 457e0a6f..75dffc0b 100644 --- a/libdiscover/backends/SnapBackend/SnapTransaction.h +++ b/libdiscover/backends/SnapBackend/SnapTransaction.h @@ -1,50 +1,54 @@ /*************************************************************************** * 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 SNAPTRANSACTION_H #define SNAPTRANSACTION_H #include #include #include class SnapResource; class QSnapdRequest; +class QSnapdClient; class SnapTransaction : public Transaction { Q_OBJECT public: - SnapTransaction(SnapResource* app, QSnapdRequest* request, Role role, AbstractResource::State newState); + SnapTransaction(QSnapdClient* client, SnapResource* app, Role role, AbstractResource::State newState); void cancel() override; + void proceed() override; private Q_SLOTS: void finishTransaction(); private: + void setRequest(QSnapdRequest* req); void progressed(); + QSnapdClient * const m_client; SnapResource * const m_app; - const QScopedPointer m_request; + QScopedPointer m_request; const AbstractResource::State m_newState; }; #endif // SNAPTRANSACTION_H