Debian Codegraph @

libcellbroadcast/lcb-message.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2025 The Phosh.mobi e.V.
3 *
4 * SPDX-License-Identifier: GPL-3.0+
5 *
6 * Author: Evangelos Ribeiro Tzaras <[email protected]>
7 */
8
9 #include "lcb-message-priv.h"
10 #include "lcb-enums.h"
11 #include "lcb-enum-types.h"
12
13 enum {
14 PROP_0,
15 PROP_TEXT,
16 PROP_CHANNEL,
17 PROP_MSG_CODE,
18 PROP_UPDATE,
19 PROP_TIMESTAMP,
20 PROP_SEVERITY,
21 PROP_SEVERITY_SUBJECT,
22 PROP_MSG_ID,
23 PROP_OPERATOR_CODE,
24 PROP_LAST_PROP
25 };
26
27 static GParamSpec *props[PROP_LAST_PROP];
28
29 typedef struct _LcbMessagePrivate {
30 char *text;
31 guint channel;
32 guint msg_code;
33 guint update;
34 gint64 timestamp;
35 LcbSeverityLevel severity; /* determined at time of receival */
36 char *severity_subject; /* determined/localized at time of receival */
37 char *msg_id; /* unique and sorts nicely: timestamp-channel-code-update */
38 char *updated_by; /* contains id of message that updates our message */
39 char *operator_code;
40 } LcbMessagePrivate;
41
42 G_DEFINE_TYPE_WITH_PRIVATE (LcbMessage, lcb_message, G_TYPE_OBJECT);
43
44 static void
45 lcb_message_set_property (GObject *object,
46 guint prop_id,
47 const GValue *value,
48 GParamSpec *pspec)
49 {
50 LcbMessage *self = LCB_MESSAGE (object);
51 LcbMessagePrivate *priv = lcb_message_get_instance_private (self);
52 gint64 _timestamp;
53
54 switch (prop_id) {
55 case PROP_TEXT:
56 priv->text = g_value_dup_string (value);
57 break;
58
59 case PROP_CHANNEL:
60 priv->channel = g_value_get_uint (value);
61 break;
62
63 case PROP_MSG_CODE:
64 priv->msg_code = g_value_get_uint (value);
65 break;
66
67 case PROP_UPDATE:
68 priv->update = g_value_get_uint (value);
69 break;
70
71 case PROP_TIMESTAMP:
72 _timestamp = g_value_get_int64 (value);
73 if (_timestamp == 0) {
74 g_autoptr (GDateTime) now = g_date_time_new_now_local ();
75
76 _timestamp = g_date_time_to_unix (now);
77 }
78
79 priv->timestamp = _timestamp;
80 break;
81
82 case PROP_SEVERITY:
83 priv->severity = g_value_get_flags (value);
84 break;
85
86 case PROP_SEVERITY_SUBJECT:
87 priv->severity_subject = g_value_dup_string (value);
88 break;
89
90 case PROP_OPERATOR_CODE:
91 priv->operator_code = g_value_dup_string (value);
92 break;
93
94 default:
95 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
96 }
97 }
98
99 static void
100 lcb_message_get_property (GObject *object,
101 guint prop_id,
102 GValue *value,
103 GParamSpec *pspec)
104 {
105 LcbMessage *self = LCB_MESSAGE (object);
106
107 switch (prop_id) {
108 case PROP_TEXT:
109 g_value_set_string (value, lcb_message_get_text (self));
110 break;
111
112 case PROP_CHANNEL:
113 g_value_set_uint (value, lcb_message_get_channel (self));
114 break;
115
116 case PROP_MSG_CODE:
117 g_value_set_uint (value, lcb_message_get_msg_code (self));
118 break;
119
120 case PROP_UPDATE:
121 g_value_set_uint (value, lcb_message_get_update (self));
122 break;
123
124 case PROP_TIMESTAMP:
125 g_value_set_int64 (value, lcb_message_get_timestamp (self));
126 break;
127
128 case PROP_SEVERITY:
129 g_value_set_flags (value, lcb_message_get_severity (self));
130 break;
131
132 case PROP_SEVERITY_SUBJECT:
133 g_value_set_string (value, lcb_message_get_severity_subject (self));
134 break;
135
136 case PROP_MSG_ID:
137 g_value_set_string (value, lcb_message_get_msg_id (self));
138 break;
139
140 case PROP_OPERATOR_CODE:
141 g_value_set_string (value, lcb_message_get_operator_code (self));
142 break;
143
144 default:
145 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
146 }
147 }
148
149 static void
150 lcb_message_constructed (GObject *object)
151 {
152 LcbMessage *self = LCB_MESSAGE (object);
153 LcbMessagePrivate *priv = lcb_message_get_instance_private (self);
154
155 priv->msg_id =
156 g_strdup_printf ("%" G_GINT64_FORMAT "-%" G_GUINT16_FORMAT "-%" G_GUINT16_FORMAT "-%" G_GUINT16_FORMAT,
157 priv->timestamp, priv->channel, priv->msg_code, priv->update);
158
159 G_OBJECT_CLASS (lcb_message_parent_class)->constructed (object);
160 }
161
162 static void
163 lcb_message_dispose (GObject *object)
164 {
165 LcbMessage *self = LCB_MESSAGE (object);
166 LcbMessagePrivate *priv = lcb_message_get_instance_private (self);
167
168 g_clear_pointer (&priv->text, g_free);
169 g_clear_pointer (&priv->msg_id, g_free);
170 g_clear_pointer (&priv->severity_subject, g_free);
171 g_clear_pointer (&priv->operator_code, g_free);
172
173 G_OBJECT_CLASS (lcb_message_parent_class)->dispose (object);
174 }
175
176 static void
177 lcb_message_class_init (LcbMessageClass *klass)
178 {
179 GObjectClass *object_class = G_OBJECT_CLASS (klass);
180
181 object_class->set_property = lcb_message_set_property;
182 object_class->get_property = lcb_message_get_property;
183 object_class->constructed = lcb_message_constructed;
184 object_class->dispose = lcb_message_dispose;
185
186 /**
187 * LcbMessage:text:
188 *
189 * The body of a cell broadcast message
190 */
191 props[PROP_TEXT] =
192 g_param_spec_string ("text", "", "", NULL,
193 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
194
195 /**
196 * LcbMessage:channel:
197 *
198 * The channel of the message.
199 */
200 props[PROP_CHANNEL] =
201 g_param_spec_uint ("channel", "", "",
202 0, G_MAXUINT16, 0,
203 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
204
205 /**
206 * LcbMessage:message-code:
207 *
208 * The message code
209 */
210 props[PROP_MSG_CODE] =
211 g_param_spec_uint ("message-code", "", "",
212 0, G_MAXUINT16, 0,
213 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
214
215 /**
216 * LcbMessage:update:
217 *
218 * When this message updates an earlier message
219 * it will have a value greater than 0.
220 * A "new" message will have `0` here.
221 */
222 props[PROP_UPDATE] =
223 g_param_spec_uint ("update", "", "",
224 0, 65535, 0,
225 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
226
227 /**
228 * LcbMessage:timestamp:
229 *
230 * Timestamp in seconds in UTC.
231 * Setting the special value "0" results in the current timestamp.
232 */
233 props[PROP_TIMESTAMP] =
234 g_param_spec_int64 ("timestamp", "", "",
235 0, G_MAXINT64, 0,
236 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
237
238 /**
239 * LcbMessage:severity:
240 *
241 * The severity level of this message as determined at time of receival
242 */
243 props[PROP_SEVERITY] =
244 g_param_spec_flags ("severity", "", "",
245 LCB_TYPE_SEVERITY_LEVEL,
246 LCB_SEVERITY_LEVEL_UNKNOWN,
247 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
248
249 /**
250 * LcbMessage:severity-subject:
251 *
252 * The subject determined by the severity at the time of receival
253 */
254 props[PROP_SEVERITY_SUBJECT] =
255 g_param_spec_string ("severity-subject", "", "", NULL,
256 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
257
258 /**
259 * LcbMessage:msg-id:
260 *
261 * The (unique) id of this message: timestamp-channel-msg_code-update
262 */
263 props[PROP_MSG_ID] =
264 g_param_spec_string ("msg-id", "", "", NULL,
265 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
266 /**
267 * LcbMessage:operator-code:
268 *
269 * The operator code when the message was received
270 */
271 props[PROP_OPERATOR_CODE] =
272 g_param_spec_string ("operator-code", "", "", NULL,
273 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
274
275 g_object_class_install_properties (object_class, PROP_LAST_PROP, props);
276 }
277
278 static void
279 lcb_message_init (LcbMessage *self)
280 {
281 }
282
283
284 /**
285 * lcb_message_new:
286 * @text: The message body
287 * @channel: The channel
288 * @message_code: Differientates messages within a channel
289 * @update: Update number of the message
290 * @timestamp: The timestamp of receival
291 * @severity: The severity of the message
292 *
293 * If @timestamp is `0`, set the current time instead.
294 *
295 * Returns: (transfer full): A newly allocated message.
296 */
297 LcbMessage *
298 lcb_message_new (const char *text,
299 guint channel,
300 guint message_code,
301 guint update,
302 gint64 timestamp,
303 LcbSeverityLevel severity,
304 const char *severity_subject,
305 const char *operator_code)
306 {
307 return g_object_new (LCB_TYPE_MESSAGE,
308 "text", text,
309 "channel", channel,
310 "message-code", message_code,
311 "update", update,
312 "timestamp", timestamp,
313 "severity", severity,
314 "severity-subject", severity_subject,
315 "operator-code", operator_code,
316 NULL);
317 }
318
319 const char *
320 lcb_message_get_text (LcbMessage *self)
321 {
322 LcbMessagePrivate *priv = lcb_message_get_instance_private (self);
323
324 g_return_val_if_fail (LCB_IS_MESSAGE (self), NULL);
325
326 return priv->text;
327 }
328
329
330 guint
331 lcb_message_get_channel (LcbMessage *self)
332 {
333 LcbMessagePrivate *priv = lcb_message_get_instance_private (self);
334
335 g_return_val_if_fail (LCB_IS_MESSAGE (self), 0);
336
337 return priv->channel;
338 }
339
340
341 guint
342 lcb_message_get_msg_code (LcbMessage *self)
343 {
344 LcbMessagePrivate *priv = lcb_message_get_instance_private (self);
345
346 g_return_val_if_fail (LCB_IS_MESSAGE (self), 0);
347
348 return priv->msg_code;
349 }
350
351
352 guint
353 lcb_message_get_update (LcbMessage *self)
354 {
355 LcbMessagePrivate *priv = lcb_message_get_instance_private (self);
356
357 g_return_val_if_fail (LCB_IS_MESSAGE (self), 0);
358
359 return priv->update;
360 }
361
362
363 gint64
364 lcb_message_get_timestamp (LcbMessage *self)
365 {
366 LcbMessagePrivate *priv = lcb_message_get_instance_private (self);
367
368 g_return_val_if_fail (LCB_IS_MESSAGE (self), 0);
369
370 return priv->timestamp;
371 }
372
373 LcbSeverityLevel
374 lcb_message_get_severity (LcbMessage *self)
375 {
376 LcbMessagePrivate *priv = lcb_message_get_instance_private (self);
377
378 g_return_val_if_fail (LCB_IS_MESSAGE (self), LCB_SEVERITY_LEVEL_UNKNOWN);
379
380 return priv->severity;
381 }
382
383 const char *
384 lcb_message_get_severity_subject (LcbMessage *self)
385 {
386 LcbMessagePrivate *priv = lcb_message_get_instance_private (self);
387
388 g_return_val_if_fail (LCB_IS_MESSAGE (self), NULL);
389
390 return priv->severity_subject;
391 }
392
393 const char *
394 lcb_message_get_msg_id (LcbMessage *self)
395 {
396 LcbMessagePrivate *priv = lcb_message_get_instance_private (self);
397
398 g_return_val_if_fail (LCB_IS_MESSAGE (self), NULL);
399
400 return priv->msg_id;
401 }
402
403 const char *
404 lcb_message_get_operator_code (LcbMessage *self)
405 {
406 LcbMessagePrivate *priv = lcb_message_get_instance_private (self);
407
408 g_return_val_if_fail (LCB_IS_MESSAGE (self), NULL);
409
410 return priv->operator_code;
411 }
412
413 gboolean
414 lcb_message_has_been_updated (LcbMessage *self)
415 {
416 LcbMessagePrivate *priv = lcb_message_get_instance_private (self);
417
418 g_return_val_if_fail (LCB_IS_MESSAGE (self), FALSE);
419
420 return !!priv->updated_by;
421 }
422
423 const char *
424 lcb_message_get_updated_by (LcbMessage *self)
425 {
426 LcbMessagePrivate *priv = lcb_message_get_instance_private (self);
427
428 g_return_val_if_fail (LCB_IS_MESSAGE (self), FALSE);
429
430 return priv->updated_by;
431 }