// 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 #include using namespace Qt::StringLiterals; 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"_L1] == "available"_L1 ? Upgradeable : Installed; } bool hasCategory(const QString &category) const override { return category == "System"_L1; } 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"_L1].toObject()["base"_L1].toString(); } QString availableVersion() const override { return record["plan"_L1].toObject()["target"_L1].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"_L1] == "pending"_L1) text += tr("

A deployment is staged. Restart when ready.

"); if (record["state"_L1] == "applying"_L1) text += tr("

The operating system update is being prepared. Detailed progress is recorded in the system log.

"); if (record["state"_L1] == "downloading"_L1) text += tr("

The verified operating system image is being downloaded.

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

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

"_L1; text += "
"_L1 + record["support_description"_L1].toString().toHtmlEscaped() + "
"_L1; text += "
    "_L1; for (const auto &entry : record["plan"_L1].toObject()["package_changes"_L1].toArray()) { const auto change = entry.toObject(); const QString before = change["old_version"_L1].isString() ? change["old_version"_L1].toString() : tr("not installed"); const QString after = change["new_version"_L1].isString() ? change["new_version"_L1].toString() : tr("will be removed"); text += "
  • "_L1 + change["name"_L1].toString().toHtmlEscaped() + ": "_L1 + before.toHtmlEscaped() + QStringLiteral(" → ") + after.toHtmlEscaped() + "
  • "_L1; } return text + "
"_L1; } 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"_L1].toInt() != 1) next = {{"state"_L1, "error"_L1}, {"error"_L1, tr("Update status unavailable; refresh to retrieve a verified plan.")}}; if (record == next) return; 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"_L1].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"_L1] == "pending"_L1); Q_EMIT updatesCountChanged(); Q_EMIT resourcesChanged(resource, {"state", "installedVersion", "availableVersion", "longDescription"}); const QString state = resource->record["state"_L1].toString(); QSharedPointer message; if (state == "error"_L1) { message.reset(new InlineMessage(InlineMessage::Error, QStringLiteral("dialog-error"), resource->record["error"_L1].toString())); } else if (state == "pending"_L1) { message.reset(new InlineMessage(InlineMessage::Information, QStringLiteral("system-reboot"), tr("Archipelago is ready. Restart when you choose to activate the new deployment."))); } else if (state == "applying"_L1 || state == "downloading"_L1) { message.reset(new InlineMessage(InlineMessage::Information, QStringLiteral("system-software-update"), tr("An Archipelago transaction is running. Please wait for its result."))); } Q_EMIT inlineMessageChanged(message); }); 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"