Branch data Line data Source code
1 : : // SPDX-License-Identifier: GPL-3.0-or-later
2 : : // SPDX-FileCopyrightText: Andy Holmes <andrew.g.r.holmes@gmail.com>
3 : :
4 : : #define G_LOG_DOMAIN "valent-message"
5 : :
6 : : #include "config.h"
7 : :
8 : : #include <gio/gio.h>
9 : : #include <libvalent-core.h>
10 : :
11 : : #include "valent-message.h"
12 : : #include "valent-message-attachment.h"
13 : :
14 : : /**
15 : : * ValentMessage:
16 : : *
17 : : * A class for representing a message.
18 : : *
19 : : * `ValentMessage` is a class for representing a unit of communication,
20 : : * typically a phone message like SMS or MMS.
21 : : *
22 : : * Since: 1.0
23 : : */
24 : : struct _ValentMessage
25 : : {
26 : : ValentResource parent_instance;
27 : :
28 : : GListModel *attachments;
29 : : ValentMessageBox box;
30 : : int64_t date;
31 : : int64_t id;
32 : : unsigned int read : 1;
33 : : GStrv recipients;
34 : : char *sender;
35 : : int64_t subscription_id;
36 : : char *text;
37 : : int64_t thread_id;
38 : : char *iri;
39 : : };
40 : :
41 [ + + + - ]: 238 : G_DEFINE_FINAL_TYPE (ValentMessage, valent_message, VALENT_TYPE_RESOURCE)
42 : :
43 : : typedef enum {
44 : : PROP_ATTACHMENTS = 1,
45 : : PROP_BOX,
46 : : PROP_DATE,
47 : : PROP_ID,
48 : : PROP_READ,
49 : : PROP_RECIPIENTS,
50 : : PROP_SENDER,
51 : : PROP_SUBSCRIPTION_ID,
52 : : PROP_TEXT,
53 : : PROP_THREAD_ID,
54 : : } ValentMessageProperty;
55 : :
56 : : static GParamSpec *properties[PROP_THREAD_ID + 1] = { NULL, };
57 : :
58 : :
59 : : /*
60 : : * GObject
61 : : */
62 : : static void
63 : 23 : valent_message_finalize (GObject *object)
64 : : {
65 : 23 : ValentMessage *self = VALENT_MESSAGE (object);
66 : :
67 [ + + ]: 23 : g_clear_object (&self->attachments);
68 [ + + ]: 23 : g_clear_pointer (&self->sender, g_free);
69 [ + + ]: 23 : g_clear_pointer (&self->recipients, g_strfreev);
70 [ + + ]: 23 : g_clear_pointer (&self->text, g_free);
71 : :
72 : 23 : G_OBJECT_CLASS (valent_message_parent_class)->finalize (object);
73 : 23 : }
74 : :
75 : : static void
76 : 13 : valent_message_get_property (GObject *object,
77 : : guint prop_id,
78 : : GValue *value,
79 : : GParamSpec *pspec)
80 : : {
81 : 13 : ValentMessage *self = VALENT_MESSAGE (object);
82 : :
83 [ + + + + : 13 : switch ((ValentMessageProperty)prop_id)
+ + + + +
+ - ]
84 : : {
85 : 1 : case PROP_ATTACHMENTS:
86 : 1 : g_value_set_object (value, valent_message_get_attachments (self));
87 : 1 : break;
88 : :
89 : 1 : case PROP_BOX:
90 : 1 : g_value_set_uint (value, self->box);
91 : 1 : break;
92 : :
93 : 4 : case PROP_DATE:
94 : 4 : g_value_set_int64 (value, self->date);
95 : 4 : break;
96 : :
97 : 1 : case PROP_ID:
98 : 1 : g_value_set_int64 (value, self->id);
99 : 1 : break;
100 : :
101 : 1 : case PROP_READ:
102 : 1 : g_value_set_boolean (value, self->read);
103 : 1 : break;
104 : :
105 : 1 : case PROP_RECIPIENTS:
106 : 1 : g_value_set_boxed (value, self->recipients);
107 : 1 : break;
108 : :
109 : 1 : case PROP_SENDER:
110 : 1 : g_value_set_string (value, self->sender);
111 : 1 : break;
112 : :
113 : 1 : case PROP_SUBSCRIPTION_ID:
114 : 1 : g_value_set_int64 (value, self->subscription_id);
115 : 1 : break;
116 : :
117 : 1 : case PROP_TEXT:
118 : 1 : g_value_set_string (value, self->text);
119 : 1 : break;
120 : :
121 : 1 : case PROP_THREAD_ID:
122 : 1 : g_value_set_int64 (value, self->thread_id);
123 : 1 : break;
124 : :
125 : 0 : default:
126 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
127 : : }
128 : 13 : }
129 : :
130 : : static void
131 : 250 : valent_message_set_property (GObject *object,
132 : : guint prop_id,
133 : : const GValue *value,
134 : : GParamSpec *pspec)
135 : : {
136 : 250 : ValentMessage *self = VALENT_MESSAGE (object);
137 : :
138 [ + + + + : 250 : switch ((ValentMessageProperty)prop_id)
+ + + + +
+ - ]
139 : : {
140 : 25 : case PROP_ATTACHMENTS:
141 : 25 : self->attachments = g_value_dup_object (value);
142 : 25 : break;
143 : :
144 : 25 : case PROP_BOX:
145 : 25 : self->box = g_value_get_uint (value);
146 : 25 : break;
147 : :
148 : 25 : case PROP_DATE:
149 : 25 : self->date = g_value_get_int64 (value);
150 : 25 : break;
151 : :
152 : 25 : case PROP_ID:
153 : 25 : self->id = g_value_get_int64 (value);
154 : 25 : break;
155 : :
156 : 25 : case PROP_READ:
157 : 25 : self->read = g_value_get_boolean (value);
158 : 25 : break;
159 : :
160 : 25 : case PROP_RECIPIENTS:
161 : 25 : self->recipients = g_value_dup_boxed (value);
162 : 25 : break;
163 : :
164 : 25 : case PROP_SENDER:
165 : 25 : self->sender = g_value_dup_string (value);
166 : 25 : break;
167 : :
168 : 25 : case PROP_SUBSCRIPTION_ID:
169 : 25 : self->subscription_id = g_value_get_int64 (value);
170 : 25 : break;
171 : :
172 : 25 : case PROP_TEXT:
173 : 25 : self->text = g_value_dup_string (value);
174 : 25 : break;
175 : :
176 : 25 : case PROP_THREAD_ID:
177 : 25 : self->thread_id = g_value_get_int64 (value);
178 : 25 : break;
179 : :
180 : 0 : default:
181 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
182 : : }
183 : 250 : }
184 : :
185 : : static void
186 : 7 : valent_message_class_init (ValentMessageClass *klass)
187 : : {
188 : 7 : GObjectClass *object_class = G_OBJECT_CLASS (klass);
189 : :
190 : 7 : object_class->finalize = valent_message_finalize;
191 : 7 : object_class->get_property = valent_message_get_property;
192 : 7 : object_class->set_property = valent_message_set_property;
193 : :
194 : : /**
195 : : * ValentMessage:attachments: (getter get_attachments)
196 : : *
197 : : * The list of attachments.
198 : : *
199 : : * Since: 1.0
200 : : */
201 : 14 : properties [PROP_ATTACHMENTS] =
202 : 7 : g_param_spec_object ("attachments", NULL, NULL,
203 : : G_TYPE_LIST_MODEL,
204 : : (G_PARAM_READWRITE |
205 : : G_PARAM_CONSTRUCT_ONLY |
206 : : G_PARAM_EXPLICIT_NOTIFY |
207 : : G_PARAM_STATIC_STRINGS));
208 : :
209 : : /**
210 : : * ValentMessage:box: (getter get_box)
211 : : *
212 : : * The `ValentMessageBox` of the message.
213 : : *
214 : : * Since: 1.0
215 : : */
216 : 14 : properties [PROP_BOX] =
217 : 7 : g_param_spec_uint ("box", NULL, NULL,
218 : : VALENT_MESSAGE_BOX_ALL, VALENT_MESSAGE_BOX_FAILED,
219 : : VALENT_MESSAGE_BOX_ALL,
220 : : (G_PARAM_READWRITE |
221 : : G_PARAM_CONSTRUCT_ONLY |
222 : : G_PARAM_EXPLICIT_NOTIFY |
223 : : G_PARAM_STATIC_STRINGS));
224 : :
225 : : /**
226 : : * ValentMessage:date: (getter get_date)
227 : : *
228 : : * A UNIX epoch timestamp for the message.
229 : : *
230 : : * Since: 1.0
231 : : */
232 : 14 : properties [PROP_DATE] =
233 : 7 : g_param_spec_int64 ("date", NULL, NULL,
234 : : G_MININT64, G_MAXINT64,
235 : : 0,
236 : : (G_PARAM_READWRITE |
237 : : G_PARAM_CONSTRUCT_ONLY |
238 : : G_PARAM_EXPLICIT_NOTIFY |
239 : : G_PARAM_STATIC_STRINGS));
240 : :
241 : : /**
242 : : * ValentMessage:id: (getter get_id)
243 : : *
244 : : * The unique ID for this message.
245 : : *
246 : : * Since: 1.0
247 : : */
248 : 14 : properties [PROP_ID] =
249 : 7 : g_param_spec_int64 ("id", NULL, NULL,
250 : : G_MININT64, G_MAXINT64,
251 : : 0,
252 : : (G_PARAM_READWRITE |
253 : : G_PARAM_CONSTRUCT_ONLY |
254 : : G_PARAM_EXPLICIT_NOTIFY |
255 : : G_PARAM_STATIC_STRINGS));
256 : :
257 : : /**
258 : : * ValentMessage:read: (getter get_read)
259 : : *
260 : : * Whether the message has been read.
261 : : *
262 : : * Since: 1.0
263 : : */
264 : 14 : properties [PROP_READ] =
265 : 7 : g_param_spec_boolean ("read", NULL, NULL,
266 : : FALSE,
267 : : (G_PARAM_READWRITE |
268 : : G_PARAM_CONSTRUCT_ONLY |
269 : : G_PARAM_EXPLICIT_NOTIFY |
270 : : G_PARAM_STATIC_STRINGS));
271 : :
272 : : /**
273 : : * ValentMessage:recipients: (getter get_recipients)
274 : : *
275 : : * The recipients of the message.
276 : : *
277 : : * This will usually be a list of phone numbers, email addresses or some
278 : : * other electronic medium.
279 : : *
280 : : * Since: 1.0
281 : : */
282 : 14 : properties [PROP_RECIPIENTS] =
283 : 7 : g_param_spec_boxed ("recipients", NULL, NULL,
284 : : G_TYPE_STRV,
285 : : (G_PARAM_READWRITE |
286 : : G_PARAM_CONSTRUCT_ONLY |
287 : : G_PARAM_EXPLICIT_NOTIFY |
288 : : G_PARAM_STATIC_STRINGS));
289 : :
290 : : /**
291 : : * ValentMessage:sender: (getter get_sender)
292 : : *
293 : : * The sender of the message. This will usually be a phone number, email
294 : : * address or some other electronic medium.
295 : : *
296 : : * Since: 1.0
297 : : */
298 : 14 : properties [PROP_SENDER] =
299 : 7 : g_param_spec_string ("sender", NULL, NULL,
300 : : NULL,
301 : : (G_PARAM_READWRITE |
302 : : G_PARAM_CONSTRUCT_ONLY |
303 : : G_PARAM_EXPLICIT_NOTIFY |
304 : : G_PARAM_STATIC_STRINGS));
305 : :
306 : : /**
307 : : * ValentMessage:subscription-id: (getter get_subscription_id)
308 : : *
309 : : * The subscription ID for this message.
310 : : *
311 : : * Since: 1.0
312 : : */
313 : 14 : properties [PROP_SUBSCRIPTION_ID] =
314 : 7 : g_param_spec_int64 ("subscription-id", NULL, NULL,
315 : : G_MININT64, G_MAXINT64,
316 : : -1,
317 : : (G_PARAM_READWRITE |
318 : : G_PARAM_CONSTRUCT_ONLY |
319 : : G_PARAM_EXPLICIT_NOTIFY |
320 : : G_PARAM_STATIC_STRINGS));
321 : :
322 : : /**
323 : : * ValentMessage:text: (getter get_text)
324 : : *
325 : : * The text content of the message.
326 : : *
327 : : * Since: 1.0
328 : : */
329 : 14 : properties [PROP_TEXT] =
330 : 7 : g_param_spec_string ("text", NULL, NULL,
331 : : NULL,
332 : : (G_PARAM_READWRITE |
333 : : G_PARAM_CONSTRUCT_ONLY |
334 : : G_PARAM_EXPLICIT_NOTIFY |
335 : : G_PARAM_STATIC_STRINGS));
336 : :
337 : : /**
338 : : * ValentMessage:thread-id: (getter get_thread_id)
339 : : *
340 : : * The thread this message belongs to.
341 : : *
342 : : * Since: 1.0
343 : : */
344 : 14 : properties [PROP_THREAD_ID] =
345 : 7 : g_param_spec_int64 ("thread-id", NULL, NULL,
346 : : G_MININT64, G_MAXINT64,
347 : : 0,
348 : : (G_PARAM_READWRITE |
349 : : G_PARAM_CONSTRUCT_ONLY |
350 : : G_PARAM_EXPLICIT_NOTIFY |
351 : : G_PARAM_STATIC_STRINGS));
352 : :
353 : 7 : g_object_class_install_properties (object_class, G_N_ELEMENTS (properties), properties);
354 : 7 : }
355 : :
356 : : static void
357 : 25 : valent_message_init (ValentMessage *message)
358 : : {
359 : 25 : }
360 : :
361 : : /**
362 : : * valent_message_get_attachments: (get-property attachments)
363 : : * @message: a `ValentMessage`
364 : : *
365 : : * Get the list of attachments.
366 : : *
367 : : * Returns: (transfer none) (not nullable): a `GListModel`
368 : : *
369 : : * Since: 1.0
370 : : */
371 : : GListModel *
372 : 6 : valent_message_get_attachments (ValentMessage *message)
373 : : {
374 [ + - ]: 6 : g_return_val_if_fail (VALENT_IS_MESSAGE (message), NULL);
375 : :
376 [ + + ]: 6 : if (message->attachments == NULL)
377 : : {
378 : 1 : message->attachments =
379 : 1 : G_LIST_MODEL (g_list_store_new (VALENT_TYPE_MESSAGE_ATTACHMENT));
380 : : }
381 : :
382 : 6 : return message->attachments;
383 : : }
384 : :
385 : : /**
386 : : * valent_message_get_box: (get-property box)
387 : : * @message: a `ValentMessage`
388 : : *
389 : : * Get the `ValentMessageBox` of @message.
390 : : *
391 : : * Returns: a `ValentMessageBox`
392 : : *
393 : : * Since: 1.0
394 : : */
395 : : ValentMessageBox
396 : 5 : valent_message_get_box (ValentMessage *message)
397 : : {
398 [ + - ]: 5 : g_return_val_if_fail (VALENT_IS_MESSAGE (message), VALENT_MESSAGE_BOX_ALL);
399 : :
400 : 5 : return message->box;
401 : : }
402 : :
403 : : /**
404 : : * valent_message_get_date: (get-property date)
405 : : * @message: a `ValentMessage`
406 : : *
407 : : * Get the timestamp for @message.
408 : : *
409 : : * Returns: the message timestamp
410 : : *
411 : : * Since: 1.0
412 : : */
413 : : int64_t
414 : 27 : valent_message_get_date (ValentMessage *message)
415 : : {
416 [ + - ]: 27 : g_return_val_if_fail (VALENT_IS_MESSAGE (message), 0);
417 : :
418 : 27 : return message->date;
419 : : }
420 : :
421 : : /**
422 : : * valent_message_get_id: (get-property id)
423 : : * @message: a `ValentMessage`
424 : : *
425 : : * Get the unique ID for @message.
426 : : *
427 : : * Returns: the message ID
428 : : *
429 : : * Since: 1.0
430 : : */
431 : : int64_t
432 : 2 : valent_message_get_id (ValentMessage *message)
433 : : {
434 [ + - ]: 2 : g_return_val_if_fail (VALENT_IS_MESSAGE (message), 0);
435 : :
436 : 2 : return message->id;
437 : : }
438 : :
439 : : /**
440 : : * valent_message_get_read: (get-property read)
441 : : * @message: a `ValentMessage`
442 : : *
443 : : * Get the read status of @message.
444 : : *
445 : : * Returns: %TRUE if the message has been read
446 : : *
447 : : * Since: 1.0
448 : : */
449 : : gboolean
450 : 4 : valent_message_get_read (ValentMessage *message)
451 : : {
452 [ + - ]: 4 : g_return_val_if_fail (VALENT_IS_MESSAGE (message), FALSE);
453 : :
454 : 4 : return message->read;
455 : : }
456 : :
457 : : /**
458 : : * valent_message_get_recipients: (get-property recipients)
459 : : * @message: a `ValentMessage`
460 : : *
461 : : * Get the recipients of @message.
462 : : *
463 : : * Returns: (transfer none) (nullable): the message recipients
464 : : *
465 : : * Since: 1.0
466 : : */
467 : : const char * const *
468 : 2 : valent_message_get_recipients (ValentMessage *message)
469 : : {
470 [ + - ]: 2 : g_return_val_if_fail (VALENT_IS_MESSAGE (message), NULL);
471 : :
472 : 2 : return (const char * const *)message->recipients;
473 : : }
474 : :
475 : : /**
476 : : * valent_message_get_sender: (get-property sender)
477 : : * @message: a `ValentMessage`
478 : : *
479 : : * Get the sender of @message.
480 : : *
481 : : * Returns: (transfer none) (nullable): the message sender
482 : : *
483 : : * Since: 1.0
484 : : */
485 : : const char *
486 : 1 : valent_message_get_sender (ValentMessage *message)
487 : : {
488 [ + - ]: 1 : g_return_val_if_fail (VALENT_IS_MESSAGE (message), NULL);
489 : :
490 : 1 : return message->sender;
491 : : }
492 : :
493 : : /**
494 : : * valent_message_get_subscription_id: (get-property subscription-id)
495 : : * @message: a `ValentMessage`
496 : : *
497 : : * Get the subscription ID for @message.
498 : : *
499 : : * Returns: the subscription ID
500 : : *
501 : : * Since: 1.0
502 : : */
503 : : int64_t
504 : 1 : valent_message_get_subscription_id (ValentMessage *message)
505 : : {
506 [ + - ]: 1 : g_return_val_if_fail (VALENT_IS_MESSAGE (message), 0);
507 : :
508 : 1 : return message->subscription_id;
509 : : }
510 : :
511 : : /**
512 : : * valent_message_get_text: (get-property text)
513 : : * @message: a `ValentMessage`
514 : : *
515 : : * Get the text content of @message.
516 : : *
517 : : * Returns: (transfer none) (nullable): the message text
518 : : *
519 : : * Since: 1.0
520 : : */
521 : : const char *
522 : 4 : valent_message_get_text (ValentMessage *message)
523 : : {
524 [ + - ]: 4 : g_return_val_if_fail (VALENT_IS_MESSAGE (message), NULL);
525 : :
526 : 4 : return message->text;
527 : : }
528 : :
529 : : /**
530 : : * valent_message_get_thread_id: (get-property thread-id)
531 : : * @message: a `ValentMessage`
532 : : *
533 : : * Get the thread ID @message belongs to.
534 : : *
535 : : * Returns: the thread ID
536 : : *
537 : : * Since: 1.0
538 : : */
539 : : int64_t
540 : 1 : valent_message_get_thread_id (ValentMessage *message)
541 : : {
542 [ + - ]: 1 : g_return_val_if_fail (VALENT_IS_MESSAGE (message), 0);
543 : :
544 : 1 : return message->thread_id;
545 : : }
546 : :
|