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>
96 lines
3.4 KiB
C++
96 lines
3.4 KiB
C++
#include "daemon_client.hpp"
|
|
|
|
#include <QJsonDocument>
|
|
#include <QLocalSocket>
|
|
#include <QTimer>
|
|
|
|
#include <memory>
|
|
|
|
namespace strawberry::app {
|
|
|
|
namespace {
|
|
constexpr int kTimeoutMs = 3000;
|
|
constexpr qint64 kMaxReplyBytes = 1 << 20;
|
|
}
|
|
|
|
DaemonClient::DaemonClient(QString socket_path, QObject* parent)
|
|
: QObject(parent), socket_path_(std::move(socket_path)) {}
|
|
|
|
void DaemonClient::send(const QString& verb) {
|
|
send_raw(verb, verb.toUtf8() + '\n');
|
|
}
|
|
|
|
void DaemonClient::send_with_payload(const QString& verb, const QJsonObject& payload) {
|
|
send_raw(verb, verb.toUtf8() + '\n' + QJsonDocument(payload).toJson(QJsonDocument::Compact) + '\n');
|
|
}
|
|
|
|
void DaemonClient::send_raw(const QString& verb, const QByteArray& request) {
|
|
auto* socket = new QLocalSocket(this);
|
|
auto* timer = new QTimer(socket);
|
|
timer->setSingleShot(true);
|
|
auto buffer = std::make_shared<QByteArray>();
|
|
auto finished = std::make_shared<bool>(false);
|
|
|
|
const auto finish = [this, socket, verb, finished](bool ok, const QJsonObject& data, const QString& error) {
|
|
if (*finished) {
|
|
return;
|
|
}
|
|
*finished = true;
|
|
emit replied(verb, ok, data, error);
|
|
socket->abort();
|
|
socket->deleteLater();
|
|
};
|
|
|
|
connect(timer, &QTimer::timeout, socket, [finish]() {
|
|
finish(false, {}, QStringLiteral("daemon did not answer in time"));
|
|
});
|
|
|
|
connect(socket, &QLocalSocket::connected, socket, [socket, request]() {
|
|
socket->write(request);
|
|
});
|
|
|
|
connect(socket, &QLocalSocket::readyRead, socket, [socket, buffer, finish]() {
|
|
buffer->append(socket->readAll());
|
|
const auto newline = buffer->indexOf('\n');
|
|
if (newline < 0) {
|
|
if (buffer->size() > kMaxReplyBytes) {
|
|
finish(false, {}, QStringLiteral("daemon reply too large"));
|
|
}
|
|
return;
|
|
}
|
|
|
|
QJsonParseError parse_error{};
|
|
const auto doc = QJsonDocument::fromJson(buffer->left(newline), &parse_error);
|
|
if (parse_error.error != QJsonParseError::NoError || !doc.isObject()) {
|
|
finish(false, {}, QStringLiteral("malformed reply from daemon"));
|
|
return;
|
|
}
|
|
const auto reply = doc.object();
|
|
if (!reply.value(QStringLiteral("ok")).toBool()) {
|
|
finish(false, {}, reply.value(QStringLiteral("error")).toString(QStringLiteral("request failed")));
|
|
return;
|
|
}
|
|
finish(true, reply.value(QStringLiteral("data")).toObject(), {});
|
|
});
|
|
|
|
connect(socket, &QLocalSocket::errorOccurred, socket, [socket, finish](QLocalSocket::LocalSocketError code) {
|
|
QString reason = socket->errorString();
|
|
if (code == QLocalSocket::ServerNotFoundError) {
|
|
reason = QStringLiteral("scanner is not running");
|
|
} else if (code == QLocalSocket::SocketAccessError) {
|
|
reason = QStringLiteral("permission denied (are you in the 'strawberry' group?)");
|
|
} else if (code == QLocalSocket::PeerClosedError) {
|
|
return; // normal end after a reply; readyRead already handled it
|
|
}
|
|
finish(false, {}, reason);
|
|
});
|
|
|
|
timer->start(kTimeoutMs);
|
|
socket->connectToServer(socket_path_);
|
|
}
|
|
|
|
DaemonSnapshot DaemonClient::snapshot_from_status(const QJsonObject& data) {
|
|
return strawberry::snapshot_from_status_json(QJsonDocument(data).toJson(QJsonDocument::Compact).toStdString());
|
|
}
|
|
|
|
} // namespace strawberry::app
|