Nostro/implementations/Nostro.Ui.Shell-Portal.cpp

198 lines
7.8 KiB
C++
Raw Permalink Normal View History

feat(app): add the Nostro app, a window for phones and desktops Linux phones have no banking apps, and a command line is no answer on a phone. The app does everything the nostro command does and, like it, only talks to nostrod: it holds no key and no token. - Nostro.Ui, a toolkit whose screens are functions that fill a frame, ported from imsd-messages ca5cd72d; Nostro.App, the app's state, forms and screens; Nostro.Ui.Shell, which puts them on a Wayland window with Crafter.Graphics; nostro-app; and Nostro.DebugCommands for the --debug-api build. - Client: every method also in an asynchronous form for an sd-event loop, with DaemonMissing and OutcomeUnknown. - nostrod's Flatpak is net.catcrafts.Nostrod now and the app takes net.catcrafts.Nostro; install.sh moves the state. The app's Flatpak, both Flatpaks cross-built for aarch64, and the licence texts they carry. - nostro-fake-system, a made-up bus and bank with a real nostrod on them, to look at the app against. The README's screenshots are taken on it. - The nostro command: an API key's environment is the key's own to say, so --sandbox is for bunq-oauth alone, and a virtual card is listed as VIRTUAL. The published contract changes in three places, before any release (design rule 10): - Transactions' query takes `stored`: answered from what nostrod has stored, without asking the bank. The app searches with it; before, every search was a call to bunq. - AddBunq's `environment` no longer chooses over what the key says of itself: a key of the other environment is refused. - SetCardStatus refuses to freeze or unfreeze a virtual card. bunq reports one as DEACTIVATED however well it pays, and leaves it as it is when asked, without an error. The app is GPL-3.0-only, the library stays LGPL-3.0-only, and no code moved from one to the other (design rule 13). Transaction's schema is unchanged. Verified: the 17 tests pass with every line, function and region of what they link run, and the five lint runs are clean. Every screen was looked at on the made-up system, at a phone's and a desktop's sizes in the light and the dark look, on Plasma under Wayland. What is said of bunq's cards comes from its sandbox, with throwaway users, and from one real virtual card. Known to have been used against production: connecting, and reading accounts, payments and cards. Not known to have been tried: a payment from the app against bunq, OAuth against bunq, and anything on a phone. Building the app takes --graphics=, a Crafter.Graphics checkout on its branch claude/nostro-app, until that branch is merged and pinned here. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-19 12:27:14 +01:00
// SPDX-License-Identifier: GPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
module;
#include <systemd/sd-bus.h>
#include <systemd/sd-event.h>
module Nostro.Ui.Shell:Portal_impl;
import std;
import Nostro.Ui;
import :Portal;
import :Loop;
using namespace Nostro::Ui;
using namespace Nostro::Ui::Shell;
namespace {
constexpr std::string_view portalName = "org.freedesktop.portal.Desktop";
constexpr std::string_view portalPath = "/org/freedesktop/portal/desktop";
constexpr std::string_view settingsInterface = "org.freedesktop.portal.Settings";
constexpr std::string_view openInterface = "org.freedesktop.portal.OpenURI";
constexpr std::string_view appearanceSettings = "org.freedesktop.appearance";
// The string a message has next; empty when it has none.
std::string ReadString(sd_bus_message* message) {
// sd-bus hands out a C string of the message's own.
// lint-disable-next-line no-char-pointer
const char* text = nullptr;
if(sd_bus_message_read_basic(message, 's', &text) < 0 || text == nullptr) {
return {};
}
return text;
}
}
Portal::Portal(Loop& loop, std::chrono::milliseconds patience) : loop(loop) {
if(sd_bus_open_user(&bus) < 0) {
bus = nullptr;
return;
}
// The subscription before the read, so that a change between the two is not missed.
sd_bus_match_signal(bus, &changed, portalName.data(), portalPath.data(), settingsInterface.data(), "SettingChanged", OnChanged, this);
// One call for all of it: ReadAll is in every version of the portal.
if(sd_bus_call_method_async(bus, &late, portalName.data(), portalPath.data(), settingsInterface.data(), "ReadAll", OnSettings, this, "as", 1, appearanceSettings.data()) >= 0) {
// The bus is served by hand until the answer is in or the patience is out; the loop is not running yet. A
// portal that is still starting, as at a login, answers later through the loop.
const std::int64_t deadline = NowMs() + patience.count();
while(!answered && NowMs() < deadline) {
const std::int32_t processed = sd_bus_process(bus, nullptr);
if(processed < 0 || (processed == 0 && sd_bus_wait(bus, static_cast<std::uint64_t>(deadline - NowMs()) * 1000) < 0)) {
break;
}
}
}
sd_bus_attach_event(bus, loop.Event(), SD_EVENT_PRIORITY_NORMAL);
}
Portal::~Portal() {
for(Opening& opening : openings) {
sd_bus_slot_unref(opening.slot);
}
sd_bus_slot_unref(late);
sd_bus_slot_unref(changed);
sd_bus_flush_close_unref(bus);
}
bool Portal::Connected() const noexcept {
return bus != nullptr;
}
const Appearance& Portal::Current() const noexcept {
return current;
}
void Portal::Told() {
if(onAppearance) {
onAppearance(current);
}
}
std::int32_t Portal::OnSettings(sd_bus_message* reply, void* data, sd_bus_error*) {
Portal& self = *static_cast<Portal*>(data);
self.answered = true;
self.loop.Guard("reading the system's appearance", [&self, reply] {
if(!sd_bus_message_is_method_error(reply, nullptr) && self.TakeSettings(reply)) {
self.Told();
}
});
return 0;
}
std::int32_t Portal::OnChanged(sd_bus_message* signal, void* data, sd_bus_error*) {
Portal& self = *static_cast<Portal*>(data);
self.loop.Guard("a change of the system's appearance", [&self, signal] {
const std::string space = ReadString(signal);
const std::string key = ReadString(signal);
if(space == appearanceSettings && self.TakeSetting(key, signal)) {
self.Told();
}
});
return 0;
}
// ReadAll's answer: every setting of every namespace asked for, a{sa{sv}}.
bool Portal::TakeSettings(sd_bus_message* reply) {
bool any = false;
if(sd_bus_message_enter_container(reply, 'a', "{sa{sv}}") <= 0) {
return false;
}
while(sd_bus_message_enter_container(reply, 'e', "sa{sv}") > 0) {
const std::string space = ReadString(reply);
if(sd_bus_message_enter_container(reply, 'a', "{sv}") > 0) {
while(sd_bus_message_enter_container(reply, 'e', "sv") > 0) {
const std::string key = ReadString(reply);
if(space == appearanceSettings) {
any = TakeSetting(key, reply) || any;
} else {
sd_bus_message_skip(reply, "v");
}
sd_bus_message_exit_container(reply);
}
sd_bus_message_exit_container(reply);
}
sd_bus_message_exit_container(reply);
}
sd_bus_message_exit_container(reply);
return any;
}
// One setting, with the message at its value: a variant, in which the portal's older Read wrapped the value once more.
bool Portal::TakeSetting(std::string_view key, sd_bus_message* value) {
const Appearance before = current;
std::int32_t depth = 0;
char type = 0;
// sd-bus hands out the signature as a C string of the message's own.
// lint-disable-next-line no-char-pointer
const char* contents = nullptr;
while(sd_bus_message_peek_type(value, &type, &contents) > 0 && type == 'v' && sd_bus_message_enter_container(value, 'v', contents) >= 0) {
depth++;
}
const std::string_view inside = contents != nullptr ? std::string_view(contents) : std::string_view();
if(key == "color-scheme" && type == 'u') {
// 1 prefers dark; 0 has no preference and 2 prefers light.
std::uint32_t scheme = 0;
if(sd_bus_message_read_basic(value, 'u', &scheme) >= 0) {
current.known = true;
current.dark = scheme == 1;
}
} else if(key == "accent-color" && type == 'r' && inside == "ddd") {
double red = 0;
double green = 0;
double blue = 0;
if(sd_bus_message_read(value, "(ddd)", &red, &green, &blue) >= 0) {
// Out of range is how the portal says there is none.
const auto valid = [](double part) { return part >= 0 && part <= 1; };
if(valid(red) && valid(green) && valid(blue)) {
current.accent = Color{static_cast<float>(red), static_cast<float>(green), static_cast<float>(blue)};
} else {
current.accent.reset();
}
}
} else {
// Whatever it is, one value of it.
sd_bus_message_skip(value, nullptr);
}
for(; depth > 0; depth--) {
sd_bus_message_exit_container(value);
}
return !(current == before);
}
void Portal::OpenUri(std::string_view uri, std::function<void(bool)> then) {
Opening& opening = openings.emplace_back();
opening.portal = this;
opening.then = std::move(then);
const std::string address(uri);
// No parent window, and no options: the desktop asks the user which program where it needs to.
if(bus == nullptr || sd_bus_call_method_async(bus, &opening.slot, portalName.data(), portalPath.data(), openInterface.data(), "OpenURI", OnOpened, &opening, "ssa{sv}", "", address.c_str(), 0) < 0) {
const std::function<void(bool)> refused = std::move(opening.then);
openings.pop_back();
if(refused) {
refused(false);
}
}
}
std::int32_t Portal::OnOpened(sd_bus_message* reply, void* data, sd_bus_error*) {
Opening& opening = *static_cast<Opening*>(data);
Portal& self = *opening.portal;
const bool taken = !sd_bus_message_is_method_error(reply, nullptr);
const std::function<void(bool)> then = std::move(opening.then);
// sd-bus holds the slot while this runs, so it may be let go of here.
sd_bus_slot_unref(opening.slot);
self.openings.remove_if([&opening](const Opening& other) { return &other == &opening; });
if(then) {
self.loop.Guard("opening an address", [&then, taken] { then(taken); });
}
return 0;
}