/* Platform tools on Linux, implemented by the app (not by the engine): they * exist exactly where the desktop service does, and the engine only ever sees * the capability -- platforms + probe decide whether the tool is offered. */ #include #include #include #include #include #include #include #include "core/AgentCore.h" #include "core/ToolProvider.h" namespace app { namespace { /* whereis-style check without a shell: the same rule the engine uses for the * clipboard helpers. */ bool haveProgram(const char *name) { QByteArray path = qgetenv("PATH"); const QList dirs = path.split(':'); for (const QByteArray &d : dirs) { QByteArray dir = d.isEmpty() ? QByteArray(".") : d; QByteArray cand = dir + "/" + name; if (QFileInfo::exists(QString::fromLocal8Bit(cand)) && QFileInfo(QString::fromLocal8Bit(cand)).isExecutable()) return true; } return false; } /* 0 when notifications can be shown; -1 with the reason otherwise. */ int notifyProbe(char *why, size_t why_sz) { if (haveProgram("notify-send")) return 0; if (haveProgram("gdbus") && !qgetenv("DBUS_SESSION_BUS_ADDRESS").isEmpty()) return 0; snprintf(why, why_sz, "no notification service (no notify-send, no D-Bus session bus)"); return -1; } const char *argOf(const ctl_tool_call_t *call, const char *key) { for (int i = 0; i < call->nargs; i++) if (strcmp(call->args[i].key, key) == 0) return call->args[i].value; return nullptr; } /* Runs on the turn's worker thread: no GUI objects here. */ int notifyRun(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *call, cst_tool_result_t *r, void *ud) { (void)ctx; (void)ud; const char *title = argOf(call, "title"); const char *message = argOf(call, "message"); if (!message || !message[0]) { const char *msg = "error: notification_send needs a message"; r->out = strdup(msg); r->out_len = r->out ? strlen(msg) : 0; r->exit_code = 1; return 0; } QStringList args; if (title && title[0]) args << QString::fromUtf8(title); else args << QStringLiteral("CST-agent"); args << QString::fromUtf8(message); const int rc = QProcess::execute(QStringLiteral("notify-send"), args); const QByteArray text = (rc == 0 ? QByteArrayLiteral("notification sent") : QByteArrayLiteral("notify-send failed (rc=") + QByteArray::number(rc) + QByteArrayLiteral(")")); r->out = strdup(text.constData()); r->out_len = r->out ? strlen(r->out) : 0; r->exit_code = rc == 0 ? 0 : 1; return 0; } /* Registered once; the engine keeps a copy until the owner is unregistered. */ cst_tool_def_t g_notify; } // namespace int platformRegisterLinux(AgentCore *core) { static const cst_tool_param_t params[CST_TOOL_MAX_PARAMS] = { {"message", "string", 1, "text of the notification"}, {"title", "string", 0, "title line (default: CST-agent)"}, {nullptr, nullptr, 0, nullptr}, {nullptr, nullptr, 0, nullptr}, {nullptr, nullptr, 0, nullptr}, {nullptr, nullptr, 0, nullptr}}; memset(&g_notify, 0, sizeof(g_notify)); setMeta(&g_notify, "notification_send", "Show a desktop notification (what the user sees outside the app).", CST_TP_ASK, "notify", kDesktopOnly, notifyProbe); memcpy(g_notify.params, params, sizeof(params)); g_notify.run = nullptr; g_notify.run_ex = notifyRun; g_notify.ud = nullptr; g_notify.owner = "app:linux"; return core->registerTool(&g_notify) ? 1 : 0; } } // namespace app