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-attachment"
5 : :
6 : : #include "config.h"
7 : :
8 : : #include <gio/gio.h>
9 : : #include <libvalent-core.h>
10 : :
11 : : #include "valent-message-attachment.h"
12 : :
13 : : /**
14 : : * ValentMessageAttachment:
15 : : *
16 : : * A class for representing a message attachment.
17 : : *
18 : : * `ValentMessageAttachment is a class for representing a message attachment,
19 : : * typically a small file or image with preview.
20 : : *
21 : : * Since: 1.0
22 : : */
23 : : struct _ValentMessageAttachment
24 : : {
25 : : ValentResource parent_instance;
26 : :
27 : : GFile *file;
28 : : GIcon *preview;
29 : : };
30 : :
31 [ + + + - ]: 172 : G_DEFINE_FINAL_TYPE (ValentMessageAttachment, valent_message_attachment, VALENT_TYPE_RESOURCE)
32 : :
33 : : typedef enum {
34 : : PROP_FILE = 1,
35 : : PROP_PREVIEW,
36 : : } ValentMessageAttachmentProperty;
37 : :
38 : : static GParamSpec *properties[PROP_PREVIEW + 1] = { NULL, };
39 : :
40 : :
41 : : /*
42 : : * GObject
43 : : */
44 : : static void
45 : 3 : valent_message_attachment_finalize (GObject *object)
46 : : {
47 : 3 : ValentMessageAttachment *self = VALENT_MESSAGE_ATTACHMENT (object);
48 : :
49 [ + + ]: 3 : g_clear_object (&self->file);
50 [ + - ]: 3 : g_clear_object (&self->preview);
51 : :
52 : 3 : G_OBJECT_CLASS (valent_message_attachment_parent_class)->finalize (object);
53 : 3 : }
54 : :
55 : : static void
56 : 0 : valent_message_attachment_get_property (GObject *object,
57 : : guint prop_id,
58 : : GValue *value,
59 : : GParamSpec *pspec)
60 : : {
61 : 0 : ValentMessageAttachment *self = VALENT_MESSAGE_ATTACHMENT (object);
62 : :
63 [ # # # ]: 0 : switch ((ValentMessageAttachmentProperty)prop_id)
64 : : {
65 : 0 : case PROP_FILE:
66 : 0 : g_value_set_object (value, self->file);
67 : 0 : break;
68 : :
69 : 0 : case PROP_PREVIEW:
70 : 0 : g_value_set_object (value, self->preview);
71 : 0 : break;
72 : :
73 : 0 : default:
74 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
75 : : }
76 : 0 : }
77 : :
78 : : static void
79 : 6 : valent_message_attachment_set_property (GObject *object,
80 : : guint prop_id,
81 : : const GValue *value,
82 : : GParamSpec *pspec)
83 : : {
84 : 6 : ValentMessageAttachment *self = VALENT_MESSAGE_ATTACHMENT (object);
85 : :
86 [ + + - ]: 6 : switch ((ValentMessageAttachmentProperty)prop_id)
87 : : {
88 : 3 : case PROP_FILE:
89 : 3 : valent_message_attachment_set_file (self, g_value_get_object (value));
90 : 3 : break;
91 : :
92 : 3 : case PROP_PREVIEW:
93 : 3 : valent_message_attachment_set_preview (self, g_value_get_object (value));
94 : 3 : break;
95 : :
96 : 0 : default:
97 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
98 : : }
99 : 6 : }
100 : :
101 : : static void
102 : 3 : valent_message_attachment_class_init (ValentMessageAttachmentClass *klass)
103 : : {
104 : 3 : GObjectClass *object_class = G_OBJECT_CLASS (klass);
105 : :
106 : 3 : object_class->finalize = valent_message_attachment_finalize;
107 : 3 : object_class->get_property = valent_message_attachment_get_property;
108 : 3 : object_class->set_property = valent_message_attachment_set_property;
109 : :
110 : : /**
111 : : * ValentMessageAttachment:file: (getter get_file) (setter set_file)
112 : : *
113 : : * A file for the attachment.
114 : : *
115 : : * Since: 1.0
116 : : */
117 : 6 : properties [PROP_FILE] =
118 : 3 : g_param_spec_object ("file", NULL, NULL,
119 : : G_TYPE_FILE,
120 : : (G_PARAM_READWRITE |
121 : : G_PARAM_EXPLICIT_NOTIFY |
122 : : G_PARAM_STATIC_STRINGS));
123 : :
124 : : /**
125 : : * ValentMessageAttachment:preview: (getter get_preview) (setter set_preview)
126 : : *
127 : : * A thumbnail preview of the attachment.
128 : : *
129 : : * Since: 1.0
130 : : */
131 : 6 : properties [PROP_PREVIEW] =
132 : 3 : g_param_spec_object ("preview", NULL, NULL,
133 : : G_TYPE_ICON,
134 : : (G_PARAM_READWRITE |
135 : : G_PARAM_EXPLICIT_NOTIFY |
136 : : G_PARAM_STATIC_STRINGS));
137 : :
138 : 3 : g_object_class_install_properties (object_class, G_N_ELEMENTS (properties), properties);
139 : 3 : }
140 : :
141 : : static void
142 : 3 : valent_message_attachment_init (ValentMessageAttachment *self)
143 : : {
144 : 3 : }
145 : :
146 : : /**
147 : : * valent_message_attachment_get_file: (get-property file)
148 : : * @attachment: a `ValentMessageAttachment`
149 : : *
150 : : * Get the file for @attachment.
151 : : *
152 : : * Returns: (transfer none) (nullable): the attachment file
153 : : *
154 : : * Since: 1.0
155 : : */
156 : : GFile *
157 : 0 : valent_message_attachment_get_file (ValentMessageAttachment *attachment)
158 : : {
159 [ # # ]: 0 : g_return_val_if_fail (VALENT_IS_MESSAGE_ATTACHMENT (attachment), NULL);
160 : :
161 : 0 : return attachment->file;
162 : : }
163 : :
164 : : /**
165 : : * valent_message_attachment_set_file: (set-property file)
166 : : * @attachment: a `ValentMessageAttachment`
167 : : * @file: (nullable): the attachment file
168 : : *
169 : : * Set the file for @attachment to @file.
170 : : *
171 : : * Since: 1.0
172 : : */
173 : : void
174 : 3 : valent_message_attachment_set_file (ValentMessageAttachment *attachment,
175 : : GFile *file)
176 : : {
177 [ + - ]: 3 : g_return_if_fail (VALENT_IS_MESSAGE_ATTACHMENT (attachment));
178 [ + + + - : 3 : g_return_if_fail (file == NULL || G_IS_FILE (file));
+ - - + ]
179 : :
180 [ + + ]: 3 : if (g_set_object (&attachment->file, file))
181 : 1 : g_object_notify_by_pspec (G_OBJECT (attachment), properties[PROP_FILE]);
182 : : }
183 : :
184 : : /**
185 : : * valent_message_attachment_get_preview: (get-property preview)
186 : : * @attachment: a `ValentMessageAttachment`
187 : : *
188 : : * Get the thumbnail preview of @attachment.
189 : : *
190 : : * Returns: (transfer none) (nullable): a thumbnail preview
191 : : *
192 : : * Since: 1.0
193 : : */
194 : : GIcon *
195 : 0 : valent_message_attachment_get_preview (ValentMessageAttachment *attachment)
196 : : {
197 [ # # ]: 0 : g_return_val_if_fail (VALENT_IS_MESSAGE_ATTACHMENT (attachment), NULL);
198 : :
199 : 0 : return attachment->preview;
200 : : }
201 : :
202 : : /**
203 : : * valent_message_attachment_set_preview: (set-property preview)
204 : : * @attachment: a `ValentMessageAttachment`
205 : : * @preview: (nullable): the attachment preview
206 : : *
207 : : * Set the preview for @attachment to @preview.
208 : : *
209 : : * Since: 1.0
210 : : */
211 : : void
212 : 3 : valent_message_attachment_set_preview (ValentMessageAttachment *attachment,
213 : : GIcon *preview)
214 : : {
215 [ + - ]: 3 : g_return_if_fail (VALENT_IS_MESSAGE_ATTACHMENT (attachment));
216 : :
217 [ + - ]: 3 : if (g_set_object (&attachment->preview, preview))
218 : 3 : g_object_notify_by_pspec (G_OBJECT (attachment), properties[PROP_PREVIEW]);
219 : : }
220 : :
|