// SPDX-License-Identifier: GPL-3.0-only // SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® // lint-disable-file no-char-pointer /* ImsdMessages.Bus:Background — inside its own flatpak, the app asks the desktop portal's Background interface to keep running with the window closed and to be started at login as `imsd-messages --daemon`, so texts are imported and notified with no window open. 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. Outside its own flatpak nothing is asked. */ module; #include export module ImsdMessages.Bus:Background; import std; import :Instance; namespace ImsdMessages::Bus { constexpr const char* desktopPortal = "org.freedesktop.portal.Desktop"; constexpr const char* desktopPortalPath = "/org/freedesktop/portal/desktop"; constexpr const char* backgroundIface = "org.freedesktop.portal.Background"; constexpr const char* requestIface = "org.freedesktop.portal.Request"; } export namespace ImsdMessages::Bus { // Where the portal puts the Request of a call made with `token`: under // the caller's unique name, the ':' dropped and every '.' a '_'. std::string PortalRequestPath(std::string_view uniqueName, std::string_view token) { std::string sender; for (const char c : uniqueName) { if (c == ':') continue; sender.push_back(c == '.' ? '_' : c); } return std::format("{}/request/{}/{}", desktopPortalPath, sender, token); } // RequestBackground's options: autostart with `commandline`, not through // D-Bus activation. GVariant* BackgroundOptions(std::string_view token, std::string_view reason, std::span commandline) { GVariantBuilder cmd; g_variant_builder_init(&cmd, G_VARIANT_TYPE("as")); for (const std::string& arg : commandline) g_variant_builder_add(&cmd, "s", arg.c_str()); GVariantBuilder options; g_variant_builder_init(&options, G_VARIANT_TYPE("a{sv}")); g_variant_builder_add(&options, "{sv}", "handle_token", g_variant_new_string(std::string(token).c_str())); g_variant_builder_add(&options, "{sv}", "reason", g_variant_new_string(std::string(reason).c_str())); g_variant_builder_add(&options, "{sv}", "autostart", g_variant_new_boolean(TRUE)); g_variant_builder_add(&options, "{sv}", "commandline", g_variant_builder_end(&cmd)); g_variant_builder_add(&options, "{sv}", "dbus-activatable", g_variant_new_boolean(FALSE)); return g_variant_builder_end(&options); } // This process is the app's own flatpak: flatpak sets FLATPAK_ID. bool InOwnFlatpak() { const gchar* id = g_getenv("FLATPAK_ID"); return id && std::string_view(id) == appId; } class BackgroundRequest { public: BackgroundRequest(GDBusConnection* session, std::string reason, std::vector commandline) : conn(session), reason(std::move(reason)), commandline(std::move(commandline)) {} ~BackgroundRequest() { if (conn && sub) g_dbus_connection_signal_unsubscribe(conn, sub); } BackgroundRequest(const BackgroundRequest&) = delete; BackgroundRequest& operator=(const BackgroundRequest&) = delete; // What the portal answered, one line. std::function onLog; void Start() { if (!conn) return; const gchar* unique = g_dbus_connection_get_unique_name(conn); if (!unique) return; const std::string token = std::format("imsd_messages_{}", g_random_int()); // Subscribed before the call: the answer may come before the // call returns the request's path. Watch(PortalRequestPath(unique, token)); g_dbus_connection_call(conn, desktopPortal, desktopPortalPath, backgroundIface, "RequestBackground", g_variant_new("(s@a{sv})", "", BackgroundOptions(token, reason, commandline)), G_VARIANT_TYPE("(o)"), G_DBUS_CALL_FLAGS_NONE, -1, nullptr, OnCalled, this); } private: void Watch(const std::string& requestPath) { if (sub) g_dbus_connection_signal_unsubscribe(conn, sub); path = requestPath; sub = g_dbus_connection_signal_subscribe(conn, desktopPortal, requestIface, "Response", path.c_str(), nullptr, G_DBUS_SIGNAL_FLAGS_NONE, OnResponse, this, nullptr); } void Log(std::string_view line) { if (onLog) onLog(line); } static void OnCalled(GObject* source, GAsyncResult* result, gpointer data) { BackgroundRequest& self = *static_cast(data); GError* err = nullptr; GVariant* reply = g_dbus_connection_call_finish(G_DBUS_CONNECTION(source), result, &err); if (err) { self.Log(std::format("autostart: the desktop portal did not take the request ({})", err->message)); g_error_free(err); if (self.sub) g_dbus_connection_signal_unsubscribe(self.conn, self.sub); self.sub = 0; return; } const gchar* handle = nullptr; g_variant_get(reply, "(&o)", &handle); // A portal older than handle_token picks its own path. if (handle && self.path != handle) self.Watch(handle); g_variant_unref(reply); } static void OnResponse(GDBusConnection*, const gchar*, const gchar*, const gchar*, const gchar*, GVariant* params, gpointer data) { BackgroundRequest& self = *static_cast(data); guint32 response = 2; GVariant* results = nullptr; g_variant_get(params, "(u@a{sv})", &response, &results); gboolean background = FALSE; gboolean autostart = FALSE; if (results) { g_variant_lookup(results, "background", "b", &background); g_variant_lookup(results, "autostart", "b", &autostart); g_variant_unref(results); } if (response == 0) self.Log(std::format("autostart: running in the background {}, starting at login {}", background ? "allowed" : "refused", autostart ? "on" : "off")); else self.Log(response == 1 ? "autostart: the desktop portal refused the request" : "autostart: the request ended without an answer"); g_dbus_connection_signal_unsubscribe(self.conn, self.sub); self.sub = 0; } GDBusConnection* conn; std::string reason; std::vector commandline; std::string path; guint sub = 0; }; }