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 (object, "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 | 42 | priv->name = g_strdup (msg_json_object_get_string (obj, "name")); | |
129 | 42 | priv->size = json_object_get_int_member (obj, "size"); | |
130 | 42 | priv->etag = g_strdup (msg_json_object_get_string (object, "eTag")); | |
131 | |||
132 |
1/2✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
|
42 | if (json_object_has_member (obj, "createdBy")) { |
133 | JsonObject *created_by; | ||
134 | |||
135 | 42 | created_by = json_object_get_object_member (obj, "createdBy"); | |
136 |
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")) { |
137 | 42 | JsonObject *user = json_object_get_object_member (created_by, "user"); | |
138 | 84 | priv->user = g_strdup (msg_json_object_get_string (user, "displayName")); | |
139 | } | ||
140 | ✗ | } else if (json_object_has_member (obj, "lastModifiedBy")) { | |
141 | JsonObject *created_by; | ||
142 | |||
143 | ✗ | created_by = json_object_get_object_member (obj, "lastModifiedBy"); | |
144 | ✗ | if (created_by && json_object_has_member (created_by, "user")) { | |
145 | ✗ | JsonObject *user = json_object_get_object_member (created_by, "user"); | |
146 | ✗ | priv->user = g_strdup (msg_json_object_get_string (user, "displayName")); | |
147 | } | ||
148 | } | ||
149 | |||
150 |
2/2✓ Branch 1 taken 38 times.
✓ Branch 2 taken 4 times.
|
42 | if (json_object_has_member (obj, "createdDateTime")) { |
151 | 38 | const char *time = json_object_get_string_member (obj, "createdDateTime"); | |
152 | 38 | priv->created = g_date_time_new_from_iso8601 (time, NULL); | |
153 | } | ||
154 | |||
155 |
1/2✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
|
42 | if (json_object_has_member (object, "lastModifiedDateTime")) { |
156 | 42 | const char *time = json_object_get_string_member (object, "lastModifiedDateTime"); | |
157 | 42 | priv->modified = g_date_time_new_from_iso8601 (time, NULL); | |
158 | } | ||
159 | |||
160 | 42 | return g_steal_pointer (&self); | |
161 | } | ||
162 | |||
163 | /** | ||
164 | * msg_drive_item_get_name: | ||
165 | * @self: a drive item | ||
166 | * | ||
167 | * Gets name of drive item. | ||
168 | * | ||
169 | * Returns: (transfer none): name of drive item | ||
170 | */ | ||
171 | const char * | ||
172 | 9 | msg_drive_item_get_name (MsgDriveItem *self) | |
173 | { | ||
174 | 9 | MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self); | |
175 | |||
176 | 9 | return priv->name; | |
177 | } | ||
178 | |||
179 | /** | ||
180 | * msg_drive_item_set_name: | ||
181 | * @self: a drive item | ||
182 | * @name: new name of drive item | ||
183 | * | ||
184 | * Sets name of drive item. | ||
185 | */ | ||
186 | void | ||
187 | 7 | msg_drive_item_set_name (MsgDriveItem *self, | |
188 | const char *name) | ||
189 | { | ||
190 | 7 | MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self); | |
191 | |||
192 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
|
7 | g_clear_pointer (&priv->name, g_free); |
193 | 7 | priv->name = g_strdup (name); | |
194 | 7 | } | |
195 | |||
196 | /** | ||
197 | * msg_drive_item_get_id: | ||
198 | * @self: a drive item | ||
199 | * | ||
200 | * Get id of drive item. | ||
201 | * | ||
202 | * Returns: (transfer none): id of drive item | ||
203 | */ | ||
204 | const char * | ||
205 | 48 | msg_drive_item_get_id (MsgDriveItem *self) | |
206 | { | ||
207 | 48 | MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self); | |
208 | |||
209 | 48 | return priv->id; | |
210 | } | ||
211 | |||
212 | /** | ||
213 | * msg_drive_item_set_id: | ||
214 | * @self: a drive item | ||
215 | * @id: new id of drive item | ||
216 | * | ||
217 | * Sets id of drive item. | ||
218 | */ | ||
219 | void | ||
220 | 2 | msg_drive_item_set_id (MsgDriveItem *self, | |
221 | const char *id) | ||
222 | { | ||
223 | 2 | MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self); | |
224 | |||
225 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | g_clear_pointer (&priv->id, g_free); |
226 | 2 | priv->id = g_strdup (id); | |
227 | 2 | } | |
228 | |||
229 | /** | ||
230 | * msg_drive_item_get_created: | ||
231 | * @self: a drive item | ||
232 | * | ||
233 | * Get created time of drive item. | ||
234 | * | ||
235 | * Returns: created date time of drive item | ||
236 | */ | ||
237 | gint64 | ||
238 | 1 | msg_drive_item_get_created (MsgDriveItem *self) | |
239 | { | ||
240 | 1 | MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self); | |
241 | |||
242 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | return priv->created ? g_date_time_to_unix (priv->created) : 0; |
243 | } | ||
244 | |||
245 | /** | ||
246 | * msg_drive_item_get_modified: | ||
247 | * @self: a drive item | ||
248 | * | ||
249 | * Gets modified time of drive item. | ||
250 | * | ||
251 | * Returns: modified date time of drive item | ||
252 | */ | ||
253 | gint64 | ||
254 | 1 | msg_drive_item_get_modified (MsgDriveItem *self) | |
255 | { | ||
256 | 1 | MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self); | |
257 | |||
258 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | return priv->modified ? g_date_time_to_unix (priv->modified) : 0; |
259 | } | ||
260 | |||
261 | /** | ||
262 | * msg_drive_item_get_etag: | ||
263 | * @self: a drive item | ||
264 | * | ||
265 | * Gets etag of drive item. | ||
266 | * | ||
267 | * Returns: (transfer none): etag of drive item | ||
268 | */ | ||
269 | const char * | ||
270 | 1 | msg_drive_item_get_etag (MsgDriveItem *self) | |
271 | { | ||
272 | 1 | MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self); | |
273 | |||
274 | 1 | return priv->etag; | |
275 | } | ||
276 | |||
277 | /** | ||
278 | * msg_drive_item_get_user: | ||
279 | * @self: a drive item | ||
280 | * | ||
281 | * Gets user of drive item. | ||
282 | * | ||
283 | * Returns: (transfer none): user of drive item | ||
284 | */ | ||
285 | const char * | ||
286 | 1 | msg_drive_item_get_user (MsgDriveItem *self) | |
287 | { | ||
288 | 1 | MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self); | |
289 | |||
290 | 1 | return priv->user; | |
291 | } | ||
292 | |||
293 | /** | ||
294 | * msg_drive_item_get_size | ||
295 | * @self: a drive item | ||
296 | * | ||
297 | * Gets size of drive item. | ||
298 | * | ||
299 | * Returns: size of drive item | ||
300 | */ | ||
301 | guint64 | ||
302 | 1 | msg_drive_item_get_size (MsgDriveItem *self) | |
303 | { | ||
304 | 1 | MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self); | |
305 | |||
306 | 1 | return priv->size; | |
307 | } | ||
308 | |||
309 | /** | ||
310 | * msg_drive_item_get_drive_id: | ||
311 | * @self: a drive item | ||
312 | * | ||
313 | * Gets drive id of drive item. | ||
314 | * | ||
315 | * Returns: (transfer none): drive id of drive item | ||
316 | */ | ||
317 | const char * | ||
318 | 46 | msg_drive_item_get_drive_id (MsgDriveItem *self) | |
319 | { | ||
320 | 46 | MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self); | |
321 | |||
322 | 46 | return priv->drive_id; | |
323 | } | ||
324 | |||
325 | /** | ||
326 | * msg_drive_item_get_parent_id: | ||
327 | * @self: a drive item | ||
328 | * | ||
329 | * Gets parent id of drive item. | ||
330 | * | ||
331 | * Returns: (transfer none): parent id of drive item | ||
332 | */ | ||
333 | const char * | ||
334 | 2 | msg_drive_item_get_parent_id (MsgDriveItem *self) | |
335 | { | ||
336 | 2 | MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self); | |
337 | |||
338 | 2 | return priv->parent_id; | |
339 | } | ||
340 | |||
341 | /** | ||
342 | * msg_drive_item_is_shared: | ||
343 | * @self: a drive item | ||
344 | * | ||
345 | * Gets whether item is shared. | ||
346 | * | ||
347 | * Returns: %TRUE if item is shared, otherwise %FALSE | ||
348 | */ | ||
349 | gboolean | ||
350 | 1 | msg_drive_item_is_shared (MsgDriveItem *self) | |
351 | { | ||
352 | 1 | MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self); | |
353 | |||
354 | 1 | return priv->is_shared; | |
355 | } | ||
356 | |||
357 | /** | ||
358 | * msg_drive_item_set_parent_id: | ||
359 | * @self: a drive item | ||
360 | * @parent_id: parent id | ||
361 | * | ||
362 | * Set parent id of drive item. | ||
363 | */ | ||
364 | void | ||
365 | 2 | msg_drive_item_set_parent_id (MsgDriveItem *self, | |
366 | const char *parent_id) | ||
367 | { | ||
368 | 2 | MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self); | |
369 | |||
370 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | g_clear_pointer (&priv->parent_id, g_free); |
371 | 2 | priv->parent_id = g_strdup (parent_id); | |
372 | 2 | } | |
373 |