strawberry/app/main.cpp
trvon c3dc258d5a
Some checks failed
Build and Test / build (debug, clang) (push) Has been cancelled
Build and Test / build (debug, gcc) (push) Has been cancelled
Build and Test / build (release, clang) (push) Has been cancelled
Build and Test / build (release, gcc) (push) Has been cancelled
Build and Test / macos (push) Has been cancelled
Build and Test / docker (push) Has been cancelled
App: tray icon survives the sway/waybar startup race
strawberry-tray did a one-shot QSystemTrayIcon::isSystemTrayAvailable() check at
startup and, if no StatusNotifier host was up yet, permanently fell back to
window-only mode (never creating the tray icon). Under sway the app is usually
`exec`ed before waybar registers its tray module, so the indicator never showed
even after waybar came up.

- main.cpp no longer disables the tray on that one-shot check; it keeps the tray
  and logs a hint.
- AppController::start(): if the tray host isn't available yet, retry every 1.5 s;
  when it appears, hide()+show() the icon to (re)register with the host. Only if
  no host shows up within ~15 s does it fall back to opening the window.

Verified live on sway: with waybar up, the fixed binary registers a
StatusNotifierItem (icon visible); the retry covers the start-before-waybar case.
Suite 51/51; clang static analyzer clean on the changed app TUs.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-28 19:15:12 -06:00

125 lines
5.5 KiB
C++

// strawberry-tray: desktop app + tray indicator for the Strawberry scanner.
//
// Runs as the desktop user. Reads evidence read-only and talks to the root
// daemon only through its group-restricted Unix control socket. It opens no
// network sockets.
#include "app_controller.hpp"
#include "strawberry/config.hpp"
#include <QApplication>
#include <QCommandLineParser>
#include <QDir>
#include <QLocalServer>
#include <QLocalSocket>
#include <QStandardPaths>
#include <QSystemTrayIcon>
#include <QTimer>
#include <iostream>
#include <unistd.h>
namespace {
QString instance_socket_path() {
QString runtime = QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation);
if (runtime.isEmpty()) {
runtime = QDir::tempPath();
}
return runtime + QStringLiteral("/strawberry-tray-%1.sock").arg(::getuid());
}
// If another instance runs, ask it to show its window and return true.
bool forward_to_running_instance(const QString& path) {
QLocalSocket socket;
socket.connectToServer(path);
if (!socket.waitForConnected(300)) {
return false;
}
socket.write("show\n");
socket.waitForBytesWritten(300);
return true;
}
} // namespace
int main(int argc, char* argv[]) {
QApplication app(argc, argv);
QApplication::setApplicationName(QStringLiteral("strawberry-tray"));
QApplication::setApplicationDisplayName(QStringLiteral("Strawberry"));
QApplication::setDesktopFileName(QStringLiteral("strawberry-tray"));
QApplication::setQuitOnLastWindowClosed(false);
QCommandLineParser parser;
parser.setApplicationDescription(QStringLiteral("Strawberry scanner status, findings and controls"));
parser.addHelpOption();
QCommandLineOption show_option(QStringLiteral("show"), QStringLiteral("Open the main window on start."));
QCommandLineOption no_tray_option(QStringLiteral("no-tray"), QStringLiteral("Run without a tray icon (window only)."));
QCommandLineOption config_option(QStringLiteral("config"), QStringLiteral("Daemon config to read the socket path from."),
QStringLiteral("path"), QStringLiteral("/etc/strawberry/strawberry.conf"));
QCommandLineOption socket_option(QStringLiteral("socket"), QStringLiteral("Control socket path (overrides config)."),
QStringLiteral("path"));
QCommandLineOption db_option(QStringLiteral("db"), QStringLiteral("Evidence database to read (default: as reported by the daemon)."),
QStringLiteral("path"));
QCommandLineOption poll_option(QStringLiteral("poll-ms"), QStringLiteral("Status poll interval."),
QStringLiteral("ms"), QStringLiteral("3000"));
QCommandLineOption tab_option(QStringLiteral("tab"), QStringLiteral("Tab to open: findings, processes, rules, allowlist, response, diagnostics."),
QStringLiteral("name"));
QCommandLineOption quit_after_option(QStringLiteral("quit-after-ms"),
QStringLiteral("Exit after this many ms (smoke tests)."), QStringLiteral("ms"));
parser.addOptions({show_option, no_tray_option, config_option, socket_option, db_option, poll_option, tab_option, quit_after_option});
parser.process(app);
const QString instance_path = instance_socket_path();
if (forward_to_running_instance(instance_path)) {
return 0;
}
QLocalServer::removeServer(instance_path);
QLocalServer instance_server;
instance_server.setSocketOptions(QLocalServer::UserAccessOption);
instance_server.listen(instance_path);
strawberry::app::AppOptions options;
options.show_window = parser.isSet(show_option) || parser.isSet(tab_option);
options.initial_tab = parser.value(tab_option);
options.tray = !parser.isSet(no_tray_option);
options.poll_ms = std::max(500, parser.value(poll_option).toInt());
options.db_path_override = parser.value(db_option);
if (parser.isSet(socket_option)) {
options.socket_path = parser.value(socket_option);
} else {
strawberry::Config config;
config.load_from_file(parser.value(config_option).toStdString());
options.socket_path = QString::fromStdString(config.control_socket_path);
}
if (options.tray && !QSystemTrayIcon::isSystemTrayAvailable()) {
// The tray host may just not be up yet (a sway/waybar autostart race),
// so don't give up the tray here: AppController keeps the icon and
// registers it when a host appears, falling back to the window only if
// none shows up. (On sway, ensure waybar's "tray" module is enabled.)
std::cerr << "[strawberry-tray] system tray not available yet; will keep trying "
"(on sway, enable waybar's \"tray\" module)" << std::endl;
}
strawberry::app::AppController controller(options);
QObject::connect(&instance_server, &QLocalServer::newConnection, &controller, [&]() {
while (auto* peer = instance_server.nextPendingConnection()) {
QObject::connect(peer, &QLocalSocket::readyRead, peer, [&, peer]() {
if (peer->readAll().startsWith("show")) {
controller.show_window();
}
peer->disconnectFromServer();
});
QObject::connect(peer, &QLocalSocket::disconnected, peer, &QObject::deleteLater);
}
});
controller.start();
if (parser.isSet(quit_after_option)) {
QTimer::singleShot(parser.value(quit_after_option).toInt(), &app, &QApplication::quit);
}
return app.exec();
}