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