strawberry/app/main.cpp
trvon 76ec40e4f5
Replace web UI with a Qt tray app
Remove the browser UI, its Python server and the web API contract test.
Add the Qt6 tray app (status, findings, process search, policy
controls, notifications) and EvidenceStore, the read-only view of the
daemon's evidence that the app and MCP server share. The GUI demo
config becomes strawberry.demo.conf.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 18:17:03 -06:00

122 lines
5.2 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()) {
std::cerr << "[strawberry-tray] No system tray found (on sway, enable waybar's \"tray\" module); "
"opening the window instead" << std::endl;
options.tray = false;
}
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();
}