Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ ui/backend/recording records draw calls — the test harness

ui/backend/qt QtCanvas, QtPaintedWidget, QtTheme — Qt6 Widgets
ui/backend/qt/update release check, verified download, installer, issue log — Qt6 Network
…/widgets update banner, update assistant, issue report — Qt6 Widgets
```

Still to come: `ui/model` (FormSpec, TableModel), `ui/sim`,
Expand Down
33 changes: 22 additions & 11 deletions doc/portability.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,28 @@ banner and dialogs. Nothing in it names a product: the caller passes the release
its current version, its issue page and its log file name. It is Qt-only because it is HTTP, files, processes and
JSON, which Qt already does on every desktop platform the tools ship on.

| Component | What it does |
|----------------------|-------------------------------------------------------------------------------------------------------------------------------------------------|
| `ReleaseVersion` | Parses and orders `vMAJOR.MINOR.PATCH` tags |
| `ReleaseFeed` | Reads a GitHub "latest release": its version, page, every asset downloadable over HTTPS with its SHA-256, and the installer for this platform |
| `UpdateChecker` | Fetches the feed and says whether a newer release exists |
| `UpdateDownloader` | Downloads an installer next to a `.part` file, checks its size and SHA-256, and refuses an asset without a published checksum or with a path |
| `UpdateInstallation` | Tells a packaged install from a portable one and builds the command that installs an update (`apt-get` through `pkexec`, or the NSIS setup) |
| `UpdateInstaller` | Runs that command and reports whether the tool must exit first |
| `UpdateSession` | Offer, download, install, cancel and dismiss as one state machine; `SetBlocked` holds the install back while the tool is busy with hardware |
| `IssueLog` | A rotating log file plus the recent lines in memory, optionally capturing Qt's own messages |
| `IssueReport` | Builds the new-issue URL with the product, version, platform and recent log, trimmed to fit a URL |
| Component | What it does |
|----------------------|-----------------------------------------------------------------------------------------------------------------------------------------------|
| `ReleaseVersion` | Parses and orders `vMAJOR.MINOR.PATCH` tags |
| `ReleaseFeed` | Reads a GitHub "latest release": its version, page, every asset downloadable over HTTPS with its SHA-256, and the installer for this platform |
| `UpdateChecker` | Fetches the feed and says whether a newer release exists |
| `UpdateDownloader` | Downloads an installer next to a `.part` file, checks its size and SHA-256, and refuses an asset without a published checksum or with a path |
| `UpdateInstallation` | Tells a packaged install from a portable one and builds the command that installs an update (`apt-get` through `pkexec`, or the NSIS setup) |
| `UpdateInstaller` | Runs that command and reports whether the tool must exit first |
| `UpdateSession` | Offer, download, install, cancel and dismiss as one state machine; `SetBlocked` holds the install back while the tool is busy with hardware |
| `IssueLog` | A rotating log file plus the recent lines in memory, optionally capturing Qt's own messages |
| `IssueReport` | Builds the new-issue URL with the product, version, platform and recent log, trimmed to fit a URL |

`ui.backend.qt.update_widgets` (`ui/backend/qt/update/widgets`) adds the Widgets side every tool repeated: the banner, the
check-download-install flow behind it and the issue report. A tool describes itself once in an `UpdateProduct`
(names, version, feed, release and issue pages, user agent, and the action that releases a blocked install) and places
the banner in its window.

| Component | What it does |
|-------------------|----------------------------------------------------------------------------------------------------------------------------------------------|
| `UpdateBanner` | Offer, download progress, ready, installing and failure, with Install, Release Notes, Skip, Cancel and Later |
| `UpdateAssistant` | Owns the checker, session and banner: automatic daily checks (a setting), skipped versions, the install confirmation and the restart signals |
| `OpenIssueReport` | Opens the prefilled new-issue page with the recent log, or says where to report it when no browser opens |

The installer asset is found by its name's suffix: `-linux-x86_64.deb` on Linux and `-windows-x86_64-setup.exe`
on Windows, so a release attaches one of each per tool.
Expand Down
1 change: 1 addition & 0 deletions ui/backend/qt/update/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ ui_add_library(ui.backend.qt.update QT
)

add_subdirectory(test)
add_subdirectory(widgets)
14 changes: 14 additions & 0 deletions ui/backend/qt/update/widgets/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
ui_add_library(ui.backend.qt.update_widgets QT
SOURCES
UpdateAssistant.cpp
UpdateAssistant.hpp
UpdateBanner.cpp
UpdateBanner.hpp
UpdateProduct.hpp
LINK
ui.backend.qt
ui.backend.qt.update
Qt6::Widgets
)

add_subdirectory(test)
228 changes: 228 additions & 0 deletions ui/backend/qt/update/widgets/UpdateAssistant.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
#include "ui/backend/qt/update/widgets/UpdateAssistant.hpp"
#include "ui/backend/qt/update/IssueReport.hpp"
#include <QCoreApplication>
#include <QDesktopServices>
#include <QDir>
#include <QDirIterator>
#include <QMessageBox>
#include <QSettings>
#include <QStandardPaths>
#include <utility>

namespace ui::backend::qt::update
{
namespace
{
const QString skippedVersionKey{ "updates/skippedVersion" };
const QString automaticKey{ "updates/checkAtStartup" };

void RemoveStaleDownloads(const QString& directory)
{
QDirIterator files{ directory, QDir::Files };
while (files.hasNext())
QFile::remove(files.next());
}
}

UpdateAssistant::UpdateAssistant(const UpdateProduct& product, QWidget& window, UpdateSession::CommandFactory commandFor)
: QObject(&window)
, product(product)
, window(window)
, checker(ReleaseVersion::Parse(product.version.toStdString()).value_or(ReleaseVersion{}), product.feedUrl, product.userAgent)
, session(DownloadDirectory(), product.userAgent, std::move(commandFor))
, banner(new UpdateBanner(product, &window))
{
ConnectChecker();
ConnectSession();
RemoveStaleDownloads(DownloadDirectory());
connect(&automaticTimer, &QTimer::timeout, this, [this]()
{
if (Automatic())
Check(false);
});
}

UpdateBanner& UpdateAssistant::Banner()
{
return *banner;
}

const UpdateState& UpdateAssistant::State() const
{
return session.State();
}

void UpdateAssistant::Check(bool userInitiated)
{
userRequested = userRequested || userInitiated;
if (userInitiated)
emit Checking();
checker.Check();
}

void UpdateAssistant::StartAutomaticChecks()
{
automaticTimer.start(automaticInterval);
if (Automatic())
Check(false);
}

bool UpdateAssistant::Automatic() const
{
return QSettings{}.value(automaticKey, true).toBool();
}

void UpdateAssistant::SetAutomatic(bool automatic) const
{
QSettings{}.setValue(automaticKey, automatic);
}

void UpdateAssistant::SetBlocked(bool blocked)
{
session.SetBlocked(blocked);
}

std::optional<InstallCommand> UpdateAssistant::DefaultCommand(const QString& installerPath)
{
return DefaultUpdateCommand(CurrentInstallerPlatform(), installerPath, QCoreApplication::applicationFilePath(), QCoreApplication::applicationPid());
}

QString UpdateAssistant::DownloadDirectory()
{
return QDir{ QStandardPaths::writableLocation(QStandardPaths::CacheLocation) }.filePath("updates");
}

void UpdateAssistant::ConnectChecker()
{
connect(&checker, &UpdateChecker::UpdateAvailable, this, &UpdateAssistant::OnAvailable);
connect(&checker, &UpdateChecker::UpToDate, this, [this](const ReleaseInfo& latest)
{
emit Report(IssueSeverity::info, QString("Up to date (latest release %1)").arg(latest.tag));
if (std::exchange(userRequested, false))
QMessageBox::information(&window, "Check for Updates", QString("You are running the latest version (%1).").arg(product.version));
});
connect(&checker, &UpdateChecker::CheckFailed, this, [this](const QString& reason)
{
emit Report(IssueSeverity::warning, QString("Update check failed: %1").arg(reason));
if (std::exchange(userRequested, false))
QMessageBox::warning(&window, "Check for Updates",
QString("Could not check for updates: %1\n\nReleases are also listed at %2").arg(reason, product.releasesUrl.toString()));
});
}

void UpdateAssistant::ConnectSession()
{
connect(&session, &UpdateSession::StateChanged, this, &UpdateAssistant::OnStateChanged);
connect(&session, &UpdateSession::RestartRequested, this, [this]()
{
emit Report(IssueSeverity::info, QString("Closing so the installer can update to %1").arg(session.State().release->tag));
emit RestartRequested();
});
connect(&session, &UpdateSession::Relaunch, this, [this]()
{
emit Report(IssueSeverity::info, QString("Update %1 installed; restarting").arg(session.State().release->tag));
emit RelaunchRequested();
});
connect(banner, &UpdateBanner::InstallRequested, this, &UpdateAssistant::RequestInstall);
connect(banner, &UpdateBanner::CancelRequested, &session, &UpdateSession::Cancel);
connect(banner, &UpdateBanner::DismissRequested, &session, &UpdateSession::Dismiss);
connect(banner, &UpdateBanner::SkipRequested, this, &UpdateAssistant::Skip);
}

void UpdateAssistant::OnAvailable(const ReleaseInfo& release)
{
const auto requested = std::exchange(userRequested, false);
if (!requested && QSettings{}.value(skippedVersionKey).toString() == release.tag)
{
emit Report(IssueSeverity::info, QString("Update available: %1 (skipped)").arg(release.tag));
return;
}

emit Report(IssueSeverity::info, QString("Update available: %1").arg(release.tag));
session.Offer(release);
}

void UpdateAssistant::OnStateChanged(const UpdateState& state)
{
banner->Render(state);

if (std::exchange(lastPhase, state.phase) == state.phase || !state.release)
return;

if (const auto report = PhaseReport(state))
emit Report(report->severity, report->message);
}

void UpdateAssistant::RequestInstall()
{
const auto& state = session.State();
if (state.phase == UpdatePhase::available || state.phase == UpdatePhase::failed)
{
QMessageBox confirm{ &window };
confirm.setIcon(QMessageBox::Question);
confirm.setWindowTitle("Install Update");
confirm.setText(QString("Install %1 %2?").arg(product.displayName, QString::fromStdString(state.release->version.ToString())));
confirm.setInformativeText(InstallSteps() + (state.blocked ? QString("\n\nInstalling waits until you %1.").arg(product.blockedAction.toLower()) : QString{}));
auto* install = confirm.addButton("&Install and Restart", QMessageBox::AcceptRole);
confirm.addButton(QMessageBox::Cancel);
confirm.setDefaultButton(install);
confirm.exec();
if (confirm.clickedButton() != install)
return;
}

session.Install();
}

void UpdateAssistant::Skip()
{
if (const auto& release = session.State().release)
{
QSettings{}.setValue(skippedVersionKey, release->tag);
emit Report(IssueSeverity::info, QString("Update %1 skipped").arg(release->tag));
}
session.Dismiss();
}

QString UpdateAssistant::InstallSteps() const
{
if (CurrentInstallerPlatform() == InstallerPlatform::windowsX64)
return QString("%1 downloads the installer and checks it against the release. It then closes, Windows asks for permission, and the new version starts when the installation is done.").arg(product.shortName);

return QString("%1 downloads the package and checks it against the release, then asks for your password to install it and restarts.").arg(product.shortName);
}

std::optional<PhaseMessage> PhaseReport(const UpdateState& state)
{
if (!state.release)
return std::nullopt;

const auto& tag = state.release->tag;
switch (state.phase)
{

Check warning on line 202 in ui/backend/qt/update/widgets/UpdateAssistant.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Reduce verbosity with "using enum" for "ui::backend::qt::update::IssueSeverity".

See more on https://sonarcloud.io/project/issues?id=embedded-pro_ui-cpp&issues=AaH7BAp-IgZiFB_GQ7yY&open=AaH7BAp-IgZiFB_GQ7yY&pullRequest=32

Check warning on line 202 in ui/backend/qt/update/widgets/UpdateAssistant.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Reduce verbosity with "using enum" for "ui::backend::qt::update::UpdatePhase".

See more on https://sonarcloud.io/project/issues?id=embedded-pro_ui-cpp&issues=AaH7BAp-IgZiFB_GQ7yZ&open=AaH7BAp-IgZiFB_GQ7yZ&pullRequest=32
case UpdatePhase::downloading:
return PhaseMessage{ IssueSeverity::info, QString("Downloading update %1 from %2").arg(tag, state.release->installer ? state.release->installer->url.toString() : state.release->pageUrl.toString()) };
case UpdatePhase::ready:
return PhaseMessage{ IssueSeverity::info, QString("Update %1 downloaded and verified").arg(tag) };
case UpdatePhase::installing:
return PhaseMessage{ IssueSeverity::info, QString("Installing update %1").arg(tag) };
case UpdatePhase::failed:
return PhaseMessage{ IssueSeverity::warning, QString("Update to %1 failed: %2").arg(tag, state.failure) };
default:
return std::nullopt;
}
}

bool OpenIssueReport(const UpdateProduct& product, const IssueLog& log, QWidget* parent)
{
const auto environment = CurrentIssueEnvironment(IssueProduct{ product.displayName, product.shortName }, product.version, log.FilePath());
const auto url = BuildIssueUrl(product.newIssueUrl, environment, log.RecentLines(60));
if (QDesktopServices::openUrl(url))
return true;

QMessageBox::warning(parent, "Report Issue",
QString("Could not open a web browser.\n\nPlease report the issue at %1 and attach the log file:\n%2")
.arg(product.newIssueUrl.toString(), QDir::toNativeSeparators(log.FilePath())));
return false;
}
}
73 changes: 73 additions & 0 deletions ui/backend/qt/update/widgets/UpdateAssistant.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#pragma once

#include "ui/backend/qt/update/IssueLog.hpp"
#include "ui/backend/qt/update/UpdateChecker.hpp"
#include "ui/backend/qt/update/UpdateInstallation.hpp"
#include "ui/backend/qt/update/UpdateSession.hpp"
#include "ui/backend/qt/update/widgets/UpdateBanner.hpp"
#include "ui/backend/qt/update/widgets/UpdateProduct.hpp"
#include <QObject>
#include <QString>
#include <QTimer>
#include <QWidget>
#include <chrono>
#include <optional>

namespace ui::backend::qt::update
{
class UpdateAssistant
: public QObject
{
Q_OBJECT

public:
static constexpr std::chrono::hours automaticInterval{ 24 };

UpdateAssistant(const UpdateProduct& product, QWidget& window, UpdateSession::CommandFactory commandFor = DefaultCommand);

UpdateBanner& Banner();
const UpdateState& State() const;

void Check(bool userInitiated);
void StartAutomaticChecks();
bool Automatic() const;
void SetAutomatic(bool automatic) const;
void SetBlocked(bool blocked);

static std::optional<InstallCommand> DefaultCommand(const QString& installerPath);
static QString DownloadDirectory();

signals:
void Report(ui::backend::qt::update::IssueSeverity severity, const QString& message);
void Checking();
void RestartRequested();
void RelaunchRequested();

private:
void ConnectChecker();
void ConnectSession();
void OnAvailable(const ReleaseInfo& release);
void OnStateChanged(const UpdateState& state);
void RequestInstall();
void Skip();
QString InstallSteps() const;

UpdateProduct product;
QWidget& window;
UpdateChecker checker;
UpdateSession session;
UpdateBanner* banner;
QTimer automaticTimer;
bool userRequested{ false };
UpdatePhase lastPhase{ UpdatePhase::idle };
};

struct PhaseMessage
{
IssueSeverity severity;
QString message;
};

std::optional<PhaseMessage> PhaseReport(const UpdateState& state);
bool OpenIssueReport(const UpdateProduct& product, const IssueLog& log, QWidget* parent);
}
Loading
Loading