GCC Code Coverage Report


Directory: src/
File: src/drive/msg-drive-item.c
Date: 2024-07-13 00:54:47
Exec Total Coverage
Lines: 103 112 92.0%
Functions: 21 21 100.0%
Branches: 40 61 65.6%

Line Branch Exec Source
1 /* Copyright 2022-2024 Jan-Michael Brummer <jan-michael.brummer1@volkswagen.de>
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU Lesser General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 #include <libsoup/soup.h>
18 #include <json-glib/json-glib.h>
19
20 #include "msg-drive-item.h"
21 #include "msg-drive-item-file.h"
22 #include "msg-drive-item-folder.h"
23 #include "msg-error.h"
24 #include "msg-json-utils.h"
25
26 typedef struct {
27 char *id;
28 char *parent_id;
29 char *drive_id;
30 char *name;
31 char *user;
32 char *etag;
33
34 GDateTime *created;
35 GDateTime *modified;
36
37 guint64 size;
38
39 gboolean is_shared;
40 } MsgDriveItemPrivate;
41
42
5/7
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 100 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 98 times.
620 G_DEFINE_TYPE_WITH_PRIVATE (MsgDriveItem, msg_drive_item, G_TYPE_OBJECT);
43
44 static void
45 42 msg_drive_item_finalize (GObject *object)
46 {
47 42 MsgDriveItem *self = MSG_DRIVE_ITEM (object);
48 42 MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self);
49
50
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 7 times.
42 g_clear_pointer (&priv->id, g_free);
51
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 14 times.
42 g_clear_pointer (&priv->parent_id, g_free);
52
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 7 times.
42 g_clear_pointer (&priv->drive_id, g_free);
53
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 g_clear_pointer (&priv->name, g_free);
54
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 7 times.
42 g_clear_pointer (&priv->user, g_free);
55
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 7 times.
42 g_clear_pointer (&priv->etag, g_free);
56
57
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 11 times.
42 g_clear_pointer (&priv->created, g_date_time_unref);
58
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 7 times.
42 g_clear_pointer (&priv->modified, g_date_time_unref);
59
60 42 G_OBJECT_CLASS (msg_drive_item_parent_class)->finalize (object);
61 42 }
62
63 static void
64 49 msg_drive_item_init (__attribute__ ((unused)) MsgDriveItem *item)
65 {
66 49 }
67
68 static void
69 2 msg_drive_item_class_init (MsgDriveItemClass *class)
70 {
71 2 GObjectClass *object_class = G_OBJECT_CLASS (class);
72
73 2 object_class->finalize = msg_drive_item_finalize;
74 2 }
75
76 /**
77 * msg_drive_item_new_from_json:
78 * @object: The json object to parse
79 * @error: a #GError
80 *
81 * Creates a new `MsgDriveItem` from json response object.
82 *
83 * Returns: the newly created `MsgDriveItem`
84 */
85 MsgDriveItem *
86 42 msg_drive_item_new_from_json (JsonObject *object,
87 GError **error)
88 {
89 42 g_autoptr (MsgDriveItem) self = NULL;
90 42 g_autoptr (GError) _error = NULL;
91 42 JsonObject *obj = object;
92 MsgDriveItemPrivate *priv;
93 42 gboolean remote = FALSE;
94
95
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 38 times.
42 if (json_object_has_member (object, "remoteItem")) {
96 4 obj = json_object_get_object_member (object, "remoteItem");
97 4 remote = TRUE;
98 }
99
100
2/2
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 24 times.
42 if (json_object_has_member (obj, "file")) {
101 18 self = MSG_DRIVE_ITEM (msg_drive_item_file_new_from_json (obj));
102
1/2
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
24 } else if (json_object_has_member (obj, "folder")) {
103 24 self = MSG_DRIVE_ITEM (msg_drive_item_folder_new_from_json (obj, &_error));
104 } else {
105 g_warning ("Unknown item type\n");
106 g_set_error (error,
107 MSG_ERROR,
108 MSG_ERROR_FAILED,
109 "Unknown item type\n");
110 return NULL;
111 }
112
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (!self)
114 return NULL;
115
116 42 priv = msg_drive_item_get_instance_private (self);
117 42 priv->is_shared = remote;
118 42 priv->id = g_strdup (msg_json_object_get_string (obj, "id"));
119
120
1/2
✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
42 if (json_object_has_member (obj, "parentReference")) {
121 42 JsonObject *parent_reference = json_object_get_object_member (obj, "parentReference");
122
123 42 priv->drive_id = g_strdup (msg_json_object_get_string (parent_reference, "driveId"));
124
125 84 priv->parent_id = g_strdup (msg_json_object_get_string (parent_reference, "id"));
126 }
127
128
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 29 times.
42 if (!priv->parent_id) {
129 13 JsonObject *parent_reference = json_object_get_object_member (object, "parentReference");
130 26 priv->parent_id = g_strdup (msg_json_object_get_string (parent_reference, "id"));
131 }
132
133 42 priv->name = g_strdup (msg_json_object_get_string (object, "name"));
134
135 42 priv->size = json_object_get_int_member (obj, "size");
136 42 priv->etag = g_strdup (msg_json_object_get_string (object, "eTag"));
137
138
1/2
✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
42 if (json_object_has_member (obj, "createdBy")) {
139 JsonObject *created_by;
140
141 42 created_by = json_object_get_object_member (obj, "createdBy");
142
2/4
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 42 times.
✗ Branch 4 not taken.
42 if (created_by && json_object_has_member (created_by, "user")) {
143 42 JsonObject *user = json_object_get_object_member (created_by, "user");
144 84 priv->user = g_strdup (msg_json_object_get_string (user, "displayName"));
145 }
146 } else if (json_object_has_member (obj, "lastModifiedBy")) {
147 JsonObject *created_by;
148
149 created_by = json_object_get_object_member (obj, "lastModifiedBy");
150 if (created_by && json_object_has_member (created_by, "user")) {
151 JsonObject *user = json_object_get_object_member (created_by, "user");
152 priv->user = g_strdup (msg_json_object_get_string (user, "displayName"));
153 }
154 }
155
156
2/2
✓ Branch 1 taken 38 times.
✓ Branch 2 taken 4 times.
42 if (json_object_has_member (obj, "createdDateTime")) {
157 38 const char *time = json_object_get_string_member (obj, "createdDateTime");
158 38 priv->created = g_date_time_new_from_iso8601 (time, NULL);
159 }
160
161
1/2
✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
42 if (json_object_has_member (object, "lastModifiedDateTime")) {
162 42 const char *time = json_object_get_string_member (object, "lastModifiedDateTime");
163 42 priv->modified = g_date_time_new_from_iso8601 (time, NULL);
164 }
165
166 42 return g_steal_pointer (&self);
167 }
168
169 /**
170 * msg_drive_item_get_name:
171 * @self: a drive item
172 *
173 * Gets name of drive item.
174 *
175 * Returns: (transfer none): name of drive item
176 */
177 const char *
178 9 msg_drive_item_get_name (MsgDriveItem *self)
179 {
180 9 MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self);
181
182 9 return priv->name;
183 }
184
185 /**
186 * msg_drive_item_set_name:
187 * @self: a drive item
188 * @name: new name of drive item
189 *
190 * Sets name of drive item.
191 */
192 void
193 7 msg_drive_item_set_name (MsgDriveItem *self,
194 const char *name)
195 {
196 7 MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self);
197
198
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 g_clear_pointer (&priv->name, g_free);
199 7 priv->name = g_strdup (name);
200 7 }
201
202 /**
203 * msg_drive_item_get_id:
204 * @self: a drive item
205 *
206 * Get id of drive item.
207 *
208 * Returns: (transfer none): id of drive item
209 */
210 const char *
211 48 msg_drive_item_get_id (MsgDriveItem *self)
212 {
213 48 MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self);
214
215 48 return priv->id;
216 }
217
218 /**
219 * msg_drive_item_set_id:
220 * @self: a drive item
221 * @id: new id of drive item
222 *
223 * Sets id of drive item.
224 */
225 void
226 2 msg_drive_item_set_id (MsgDriveItem *self,
227 const char *id)
228 {
229 2 MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self);
230
231
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 g_clear_pointer (&priv->id, g_free);
232 2 priv->id = g_strdup (id);
233 2 }
234
235 /**
236 * msg_drive_item_get_created:
237 * @self: a drive item
238 *
239 * Get created time of drive item.
240 *
241 * Returns: created date time of drive item
242 */
243 gint64
244 1 msg_drive_item_get_created (MsgDriveItem *self)
245 {
246 1 MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self);
247
248
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 return priv->created ? g_date_time_to_unix (priv->created) : 0;
249 }
250
251 /**
252 * msg_drive_item_get_modified:
253 * @self: a drive item
254 *
255 * Gets modified time of drive item.
256 *
257 * Returns: modified date time of drive item
258 */
259 gint64
260 1 msg_drive_item_get_modified (MsgDriveItem *self)
261 {
262 1 MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self);
263
264
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 return priv->modified ? g_date_time_to_unix (priv->modified) : 0;
265 }
266
267 /**
268 * msg_drive_item_get_etag:
269 * @self: a drive item
270 *
271 * Gets etag of drive item.
272 *
273 * Returns: (transfer none): etag of drive item
274 */
275 const char *
276 1 msg_drive_item_get_etag (MsgDriveItem *self)
277 {
278 1 MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self);
279
280 1 return priv->etag;
281 }
282
283 /**
284 * msg_drive_item_get_user:
285 * @self: a drive item
286 *
287 * Gets user of drive item.
288 *
289 * Returns: (transfer none): user of drive item
290 */
291 const char *
292 1 msg_drive_item_get_user (MsgDriveItem *self)
293 {
294 1 MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self);
295
296 1 return priv->user;
297 }
298
299 /**
300 * msg_drive_item_get_size
301 * @self: a drive item
302 *
303 * Gets size of drive item.
304 *
305 * Returns: size of drive item
306 */
307 guint64
308 1 msg_drive_item_get_size (MsgDriveItem *self)
309 {
310 1 MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self);
311
312 1 return priv->size;
313 }
314
315 /**
316 * msg_drive_item_get_drive_id:
317 * @self: a drive item
318 *
319 * Gets drive id of drive item.
320 *
321 * Returns: (transfer none): drive id of drive item
322 */
323 const char *
324 46 msg_drive_item_get_drive_id (MsgDriveItem *self)
325 {
326 46 MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self);
327
328 46 return priv->drive_id;
329 }
330
331 /**
332 * msg_drive_item_get_parent_id:
333 * @self: a drive item
334 *
335 * Gets parent id of drive item.
336 *
337 * Returns: (transfer none): parent id of drive item
338 */
339 const char *
340 2 msg_drive_item_get_parent_id (MsgDriveItem *self)
341 {
342 2 MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self);
343
344 2 return priv->parent_id;
345 }
346
347 /**
348 * msg_drive_item_is_shared:
349 * @self: a drive item
350 *
351 * Gets whether item is shared.
352 *
353 * Returns: %TRUE if item is shared, otherwise %FALSE
354 */
355 gboolean
356 1 msg_drive_item_is_shared (MsgDriveItem *self)
357 {
358 1 MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self);
359
360 1 return priv->is_shared;
361 }
362
363 /**
364 * msg_drive_item_set_parent_id:
365 * @self: a drive item
366 * @parent_id: parent id
367 *
368 * Set parent id of drive item.
369 */
370 void
371 2 msg_drive_item_set_parent_id (MsgDriveItem *self,
372 const char *parent_id)
373 {
374 2 MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self);
375
376
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 g_clear_pointer (&priv->parent_id, g_free);
377 2 priv->parent_id = g_strdup (parent_id);
378 2 }
379