Bus, main: start at login through the Background portal

A text that arrives while the app is not running waits in imsd until the
app is opened, and nothing notifies. Inside its flatpak the instance now
asks the desktop portal's Background interface when it starts (not for
the --send errand or --headless) to run on with the window closed and to
start `imsd-messages --daemon` at login, the mode that imports and
notifies with no window. The portal keeps the answer and writes the
login entry again at each request it grants, so a deleted entry comes
back while the background permission allows it; set to no, the
permission makes the portal refuse the request, and an entry already
written stays. Outside its own flatpak nothing is asked.

tests/Portal covers the request path the answer arrives on and the
options the request carries.
This commit is contained in:
Jorijn van der Graaf 2026-10-20 23:23:30 +02:00
commit f07a3716b3
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
6 changed files with 221 additions and 9 deletions

60
tests/Portal/main.cpp Normal file
View file

@ -0,0 +1,60 @@
// SPDX-License-Identifier: GPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
// lint-disable-file no-char-pointer
// The desktop portal's side of the bus layer: where a request's answer
// arrives, and the options the Background request carries.
#include <gio/gio.h>
import std;
import ImsdMessages.Bus;
using namespace ImsdMessages;
namespace {
std::int32_t failures = 0;
void Check(bool ok, std::string_view what) {
std::println("{} {}", ok ? "PASS" : "FAIL", what);
if (!ok) ++failures;
}
std::string LookupString(GVariant* dict, const char* key) {
const gchar* value = nullptr;
return g_variant_lookup(dict, key, "&s", &value) && value ? std::string(value) : std::string("<absent>");
}
std::optional<bool> LookupBool(GVariant* dict, const char* key) {
gboolean value = FALSE;
if (!g_variant_lookup(dict, key, "b", &value)) return std::nullopt;
return value != FALSE;
}
}
int main() {
Check(Bus::PortalRequestPath(":1.42", "imsd_messages_7") == "/org/freedesktop/portal/desktop/request/1_42/imsd_messages_7", "a unique name loses its ':' and its '.' becomes '_'");
Check(Bus::PortalRequestPath(":1.2345", "t") == "/org/freedesktop/portal/desktop/request/1_2345/t", "every digit of the connection number stays");
const std::vector<std::string> commandline = { "imsd-messages", "--daemon" };
GVariant* options = g_variant_ref_sink(Bus::BackgroundOptions("tok", "to receive texts", commandline));
Check(g_variant_is_of_type(options, G_VARIANT_TYPE("a{sv}")), "the options are an a{sv}");
Check(LookupString(options, "handle_token") == "tok", "handle_token is the token");
Check(LookupString(options, "reason") == "to receive texts", "reason is passed through");
Check(LookupBool(options, "autostart") == true, "autostart is asked for");
Check(LookupBool(options, "dbus-activatable") == false, "the autostart entry runs the command line, not D-Bus activation");
GVariant* cmd = g_variant_lookup_value(options, "commandline", G_VARIANT_TYPE("as"));
std::vector<std::string> got;
if (cmd) {
gsize n = 0;
const gchar** strv = g_variant_get_strv(cmd, &n);
for (gsize i = 0; i < n; ++i) got.emplace_back(strv[i]);
g_free(static_cast<gpointer>(strv));
g_variant_unref(cmd);
}
Check(got == commandline, "commandline is the daemon's argv, in order");
g_variant_unref(options);
if (failures) std::println("{} check(s) failed", failures);
return failures ? 1 : 0;
}