// SPDX-License-Identifier: GPL-2.0-or-later // One immutable OS transaction; package/runtime applications belong to other backends. #include #include #include #include #include #include #include #include #include class OSResource final : public AbstractResource { Q_OBJECT public: explicit OSResource(AbstractResourcesBackend *backend) : AbstractResource(backend) {} QJsonObject record; QString packageName() const override { return QStringLiteral("archipelago-os"); } QString name() const override { return QStringLiteral("Archipelago KDE"); } QString comment() override { return tr("Verified operating system image; activation requires a restart"); } QVariant icon() const override { return QStringLiteral("distributor-logo"); } bool canExecute() const override { return false; } void invokeApplication() const override {} bool isRemovable() const override { return false; } State state() override { return record["state"] == "available" ? Upgradeable : Installed; } bool hasCategory(const QString &category) const override { return category == "System"; } Type type() const override { return System; } quint64 size() override { return 0; } // The transport has not reported download size. QString sizeDescription() override { return tr("Download size is determined by OSTree"); } QJsonArray licenses() override { return {}; } QString installedVersion() const override { return record["plan"].toObject()["base"].toString(); } QString availableVersion() const override { return record["plan"].toObject()["target"].toString(); } QString origin() const override { return QStringLiteral("Archipelago"); } QString section() override { return QStringLiteral("System"); } QString author() const override { return QStringLiteral("Archipelago"); } QString sourceIcon() const override { return QStringLiteral("drive-harddisk"); } QDate releaseDate() const override { return {}; } QList addonsInformation() override { return {}; } QString longDescription() override { QString text = comment().toHtmlEscaped(); if (record["state"] == "pending") text += tr("

A deployment is staged. Restart when ready.

"); if (record["state"] == "error") text += "

" + record["error"].toString().toHtmlEscaped() + "

"; text += "
" + record["support_description"].toString().toHtmlEscaped() + "
"; text += "
    "; for (const auto &entry : record["plan"].toObject()["package_changes"].toArray()) { const auto change = entry.toObject(); const QString before = change["old_version"].isString() ? change["old_version"].toString() : tr("not installed"); const QString after = change["new_version"].isString() ? change["new_version"].toString() : tr("will be removed"); text += "
  • " + change["name"].toString().toHtmlEscaped() + ": " + before.toHtmlEscaped() + QStringLiteral(" → ") + after.toHtmlEscaped() + "
  • "; } return text + "
"; } void fetchChangelog() override { Q_EMIT changelogFetched(longDescription()); } void reload() { QFile file(QStringLiteral("/var/lib/archipelago/software/status.json")); QJsonObject next; if (file.open(QIODevice::ReadOnly)) next = QJsonDocument::fromJson(file.readAll()).object(); if (next["version"].toInt() != 1) next = {{"state", "error"}, {"error", tr("Update status unavailable; refresh to retrieve a verified plan.")}}; record = next; Q_EMIT stateChanged(); Q_EMIT versionsChanged(); Q_EMIT longDescriptionChanged(); } }; class OSTransaction final : public Transaction { Q_OBJECT public: explicit OSTransaction(OSResource *resource) : Transaction(resource->backend(), resource, InstallRole, {}) { setCancellable(false); const QString id = resource->record["plan_id"].toString(); QTimer::singleShot(0, this, [this, resource, id] { if (resource->state() != AbstractResource::Upgradeable || !QRegularExpression(QStringLiteral("^[a-f0-9]{64}$")).match(id).hasMatch()) { Q_EMIT passiveMessage(tr("No valid update plan; refresh updates first.")); setStatus(DoneWithErrorStatus); deleteLater(); return; } auto process = new QProcess(this); setStatus(CommittingStatus); connect(process, &QProcess::errorOccurred, this, [this, process](QProcess::ProcessError error) { if (error != QProcess::FailedToStart) return; Q_EMIT passiveMessage(process->errorString()); setStatus(DoneWithErrorStatus); deleteLater(); }); connect(process, &QProcess::finished, this, [this, process, resource](int code, QProcess::ExitStatus status) { resource->reload(); const bool success = code == 0 && status == QProcess::NormalExit; if (!success) Q_EMIT passiveMessage(QString::fromUtf8(process->readAllStandardError())); if (success) setProgress(100); setStatus(success ? DoneStatus : DoneWithErrorStatus); deleteLater(); }); process->start(QStringLiteral("/usr/bin/pkexec"), {QStringLiteral("/usr/lib/archipelago/software-helper"), QStringLiteral("apply"), id}); }); } void cancel() override {} // The shared privileged operation continues independently of the GUI. }; class ArchipelagoBackend final : public AbstractResourcesBackend { Q_OBJECT public: explicit ArchipelagoBackend(QObject *parent = nullptr) : AbstractResourcesBackend(parent) { resource = new OSResource(this); updater = new StandardBackendUpdater(this); connect(resource, &AbstractResource::stateChanged, this, [this] { updater->setNeedsReboot(resource->record["state"] == "pending"); Q_EMIT updatesCountChanged(); Q_EMIT resourcesChanged(resource, {"state", "installedVersion", "availableVersion", "longDescription"}); }); resource->reload(); auto timer = new QTimer(this); connect(timer, &QTimer::timeout, resource, &OSResource::reload); timer->start(30000); } bool isValid() const override { QFile purpose(QStringLiteral("/usr/lib/archipelago/purpose")); return QFile::exists(QStringLiteral("/run/ostree-booted")) && purpose.open(QIODevice::ReadOnly) && purpose.readAll().trimmed() == "purpose/desktop/kde"; } QString displayName() const override { return QStringLiteral("Archipelago"); } int updatesCount() const override { return resource->state() == AbstractResource::Upgradeable ? 1 : 0; } AbstractBackendUpdater *backendUpdater() const override { return updater; } AbstractReviewsBackend *reviewsBackend() const override { return nullptr; } int fetchingUpdatesProgress() const override { return refreshing ? 0 : 100; } ResultsStream *search(const Filters &filters) override { QVector found{StreamResult(resource)}; filters.filterJustInCase(found); return new ResultsStream(QStringLiteral("Archipelago"), found); } Transaction *installApplication(AbstractResource *app, const AddonList &) override { return app == resource ? new OSTransaction(resource) : nullptr; } Transaction *removeApplication(AbstractResource *) override { Q_EMIT passiveMessage(tr("The operating system cannot be removed.")); return nullptr; } void checkForUpdates() override { if (refreshing) return; refreshing = true; Q_EMIT fetchingUpdatesProgressChanged(); auto process = new QProcess(this); const auto finished = [this, process](bool success) { refreshing = false; resource->reload(); if (!success) Q_EMIT passiveMessage(process->errorString() + QString::fromUtf8(process->readAllStandardError())); Q_EMIT fetchingUpdatesProgressChanged(); process->deleteLater(); }; connect(process, &QProcess::finished, this, [finished](int code, QProcess::ExitStatus status) { finished(code == 0 && status == QProcess::NormalExit); }); connect(process, &QProcess::errorOccurred, this, [finished](QProcess::ProcessError error) { if (error == QProcess::FailedToStart) finished(false); }); process->start(QStringLiteral("/usr/bin/pkexec"), {QStringLiteral("/usr/lib/archipelago/software-helper"), QStringLiteral("refresh")}); } private: OSResource *resource; StandardBackendUpdater *updater; bool refreshing = false; }; DISCOVER_BACKEND_PLUGIN(ArchipelagoBackend) #include "ArchipelagoBackend.moc"