-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnotifications.cpp
More file actions
301 lines (269 loc) · 10.2 KB
/
Copy pathnotifications.cpp
File metadata and controls
301 lines (269 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include "notifications.hpp"
#include "strings.hpp"
#include "notifications_backend.hpp"
#include <nlohmann/json.hpp>
#include <algorithm>
#include <atomic>
#include <cctype>
#include <cstdint>
#include <mutex>
#include <utility>
namespace acecode::desktop {
namespace {
std::mutex g_notification_mu;
ClickHandler g_click_handler;
NotificationAuthorizationHandler g_authorization_handler;
NotificationAuthorizationState g_authorization_state;
void* g_activation_window = nullptr;
bool g_initialized = false;
bool g_accept_authorization_updates = false;
std::atomic<std::uint64_t> g_notification_sequence{0};
void set_parse_error(std::string* error, std::string message) {
if (error) *error = std::move(message);
}
std::string trim_ascii(std::string value) {
auto is_space = [](unsigned char ch) { return std::isspace(ch) != 0; };
value.erase(value.begin(), std::find_if(value.begin(), value.end(),
[&](char ch) {
return !is_space(
static_cast<unsigned char>(ch));
}));
value.erase(std::find_if(value.rbegin(), value.rend(),
[&](char ch) {
return !is_space(static_cast<unsigned char>(ch));
}).base(),
value.end());
return value;
}
std::size_t utf8_codepoint_bytes(const std::string& text, std::size_t offset) {
const auto lead = static_cast<unsigned char>(text[offset]);
std::size_t length = 1;
if ((lead & 0xE0u) == 0xC0u) length = 2;
else if ((lead & 0xF0u) == 0xE0u) length = 3;
else if ((lead & 0xF8u) == 0xF0u) length = 4;
if (offset + length > text.size()) return 1;
for (std::size_t i = 1; i < length; ++i) {
const auto next = static_cast<unsigned char>(text[offset + i]);
if ((next & 0xC0u) != 0x80u) return 1;
}
return length;
}
} // namespace
namespace notification_detail {
void publish_authorization_state(NotificationAuthorizationState state) {
NotificationAuthorizationHandler handler;
bool changed = false;
{
std::lock_guard<std::mutex> lock(g_notification_mu);
if (!g_accept_authorization_updates) return;
changed = state.status != g_authorization_state.status ||
state.can_request != g_authorization_state.can_request ||
state.can_open_settings !=
g_authorization_state.can_open_settings;
g_authorization_state = state;
if (changed) handler = g_authorization_handler;
}
if (handler) handler(state);
}
} // namespace notification_detail
bool init_notifications(const NotificationInitOptions& options) {
{
std::lock_guard<std::mutex> lock(g_notification_mu);
if (g_initialized) return true;
g_activation_window = options.activation_window;
// Backends can publish an initial state synchronously from initialize()
// or asynchronously immediately afterwards.
g_accept_authorization_updates = true;
}
const bool initialized = notification_backend::initialize(options);
{
std::lock_guard<std::mutex> lock(g_notification_mu);
g_initialized = initialized;
if (!initialized) {
g_accept_authorization_updates = false;
g_activation_window = nullptr;
g_authorization_state = {
NotificationAuthorizationStatus::Unavailable, false, false};
}
}
return initialized;
}
void set_click_handler(ClickHandler handler) {
std::lock_guard<std::mutex> lock(g_notification_mu);
g_click_handler = std::move(handler);
}
void set_notification_authorization_handler(
NotificationAuthorizationHandler handler) {
std::lock_guard<std::mutex> lock(g_notification_mu);
g_authorization_handler = std::move(handler);
}
NotificationAuthorizationState notification_authorization_state() {
std::lock_guard<std::mutex> lock(g_notification_mu);
return g_authorization_state;
}
const char* notification_authorization_status_name(
NotificationAuthorizationStatus status) {
switch (status) {
case NotificationAuthorizationStatus::Unknown:
return "unknown";
case NotificationAuthorizationStatus::NotDetermined:
return "not_determined";
case NotificationAuthorizationStatus::Requesting:
return "requesting";
case NotificationAuthorizationStatus::Denied:
return "denied";
case NotificationAuthorizationStatus::Authorized:
return "authorized";
case NotificationAuthorizationStatus::Provisional:
return "provisional";
case NotificationAuthorizationStatus::Unavailable:
return "unavailable";
}
return "unavailable";
}
bool refresh_notification_authorization() {
{
std::lock_guard<std::mutex> lock(g_notification_mu);
if (!g_initialized) return false;
}
return notification_backend::refresh_authorization();
}
bool request_notification_authorization() {
{
std::lock_guard<std::mutex> lock(g_notification_mu);
if (!g_initialized) return false;
}
return notification_backend::request_authorization();
}
bool open_notification_settings() {
{
std::lock_guard<std::mutex> lock(g_notification_mu);
if (!g_initialized) return false;
}
return notification_backend::open_settings();
}
bool show_notification(const NotifyPayload& payload) {
{
std::lock_guard<std::mutex> lock(g_notification_mu);
if (!g_initialized) return false;
}
if (payload.title.empty() && payload.body.empty()) return false;
return notification_backend::show(payload);
}
void shutdown_notifications() {
{
std::lock_guard<std::mutex> lock(g_notification_mu);
g_initialized = false;
// Disable publications before backend teardown. Authorization
// completions already queued by the OS are then harmless even if they
// arrive while the native delegate is being detached.
g_accept_authorization_updates = false;
g_click_handler = nullptr;
g_authorization_handler = nullptr;
g_activation_window = nullptr;
g_authorization_state = {
NotificationAuthorizationStatus::Unavailable, false, false};
}
notification_backend::shutdown();
}
std::string truncate_notification_text(const std::string& text,
std::size_t max_codepoints) {
if (text.empty() || max_codepoints == 0) return {};
std::size_t offset = 0;
std::size_t count = 0;
while (offset < text.size() && count < max_codepoints) {
offset += utf8_codepoint_bytes(text, offset);
++count;
}
if (offset >= text.size()) return text;
return text.substr(0, offset) + u8"…";
}
NotifyPayload build_completion_notification(
const std::string& session_id,
const std::string& workspace_hash,
const std::string& session_title,
const std::string& final_assistant_text,
const std::string& locale) {
NotifyPayload payload;
payload.id = "completion-" +
(session_id.empty() ? std::string("unknown") : session_id) + "-" +
std::to_string(g_notification_sequence.fetch_add(1) + 1);
payload.workspace_hash = workspace_hash;
payload.session_id = session_id;
const std::string title = trim_ascii(session_title);
payload.title = std::string(
desktop_string(DesktopStringId::NotificationCompleted, locale)) +
(title.empty()
? std::string(desktop_string(
DesktopStringId::NotificationSession, locale))
: title);
const std::string body = trim_ascii(final_assistant_text);
payload.body = body.empty()
? std::string(desktop_string(
DesktopStringId::NotificationBlankTurn, locale))
: truncate_notification_text(body);
return payload;
}
std::optional<NotifyPayload> parse_notification_bridge_args(
const std::string& args_json,
std::string* error) {
if (error) error->clear();
try {
nlohmann::json value = nlohmann::json::parse(args_json);
if (value.is_array()) {
if (value.empty()) {
set_parse_error(error, "notification arguments are empty");
return std::nullopt;
}
value = value.front();
}
if (value.is_string()) {
value = nlohmann::json::parse(value.get<std::string>());
}
if (!value.is_object()) {
set_parse_error(error, "notification payload must be an object");
return std::nullopt;
}
NotifyPayload payload;
auto copy_string = [&](const char* key, std::string& out) {
if (value.contains(key) && value[key].is_string()) {
out = value[key].get<std::string>();
}
};
copy_string("id", payload.id);
copy_string("workspace_hash", payload.workspace_hash);
copy_string("session_id", payload.session_id);
copy_string("title", payload.title);
copy_string("body", payload.body);
if (payload.title.empty() && payload.body.empty()) {
set_parse_error(error, "notification title and body are empty");
return std::nullopt;
}
return payload;
} catch (const std::exception& e) {
set_parse_error(error, e.what());
return std::nullopt;
}
}
void dispatch_notification_activation(const NotifyPayload& payload) {
ClickHandler handler;
void* activation_window = nullptr;
{
std::lock_guard<std::mutex> lock(g_notification_mu);
handler = g_click_handler;
activation_window = g_activation_window;
}
if (!handler) return;
notification_backend::activate_window(activation_window);
handler(payload);
}
void* capture_tui_notification_window() {
return notification_backend::capture_tui_window();
}
bool notification_window_is_foreground(void* native_window) {
return notification_backend::window_is_foreground(native_window);
}
bool activate_notification_window(void* native_window) {
return notification_backend::activate_window(native_window);
}
} // namespace acecode::desktop