| 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 *remote_id; | ||
| 31 | char *remote_parent_id; | ||
| 32 | char *remote_drive_id; | ||
| 33 | char *name; | ||
| 34 | char *user; | ||
| 35 | char *etag; | ||
| 36 | |||
| 37 | GDateTime *created; | ||
| 38 | GDateTime *modified; | ||
| 39 | |||
| 40 | guint64 size; | ||
| 41 | |||
| 42 | gboolean is_shared; | ||
| 43 | } MsgDriveItemPrivate; | ||
| 44 | |||
| 45 |
5/6✓ Branch 0 taken 4 times.
✓ Branch 1 taken 96 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 96 times.
|
316 | G_DEFINE_TYPE_WITH_PRIVATE (MsgDriveItem, msg_drive_item, G_TYPE_OBJECT); |
| 46 | |||
| 47 | static void | ||
| 48 | 41 | msg_drive_item_finalize (GObject *object) | |
| 49 | { | ||
| 50 | 41 | MsgDriveItem *self = MSG_DRIVE_ITEM (object); | |
| 51 | 41 | MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self); | |
| 52 | |||
| 53 |
2/2✓ Branch 0 taken 34 times.
✓ Branch 1 taken 7 times.
|
41 | g_clear_pointer (&priv->id, g_free); |
| 54 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
|
41 | g_clear_pointer (&priv->parent_id, g_free); |
| 55 |
2/2✓ Branch 0 taken 34 times.
✓ Branch 1 taken 7 times.
|
41 | g_clear_pointer (&priv->drive_id, g_free); |
| 56 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 36 times.
|
41 | g_clear_pointer (&priv->remote_id, g_free); |
| 57 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
|
41 | g_clear_pointer (&priv->remote_parent_id, g_free); |
| 58 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 36 times.
|
41 | g_clear_pointer (&priv->remote_drive_id, g_free); |
| 59 |
1/2✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
|
41 | g_clear_pointer (&priv->name, g_free); |
| 60 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 11 times.
|
41 | g_clear_pointer (&priv->user, g_free); |
| 61 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 19 times.
|
41 | g_clear_pointer (&priv->etag, g_free); |
| 62 | |||
| 63 |
2/2✓ Branch 0 taken 34 times.
✓ Branch 1 taken 7 times.
|
41 | g_clear_pointer (&priv->created, g_date_time_unref); |
| 64 |
2/2✓ Branch 0 taken 34 times.
✓ Branch 1 taken 7 times.
|
41 | g_clear_pointer (&priv->modified, g_date_time_unref); |
| 65 | |||
| 66 | 41 | G_OBJECT_CLASS (msg_drive_item_parent_class)->finalize (object); | |
| 67 | 41 | } | |
| 68 | |||
| 69 | static void | ||
| 70 | 48 | msg_drive_item_init (__attribute__ ((unused)) MsgDriveItem *item) | |
| 71 | { | ||
| 72 | 48 | } | |
| 73 | |||
| 74 | static void | ||
| 75 | 2 | msg_drive_item_class_init (MsgDriveItemClass *class) | |
| 76 | { | ||
| 77 | 2 | GObjectClass *object_class = G_OBJECT_CLASS (class); | |
| 78 | |||
| 79 | 2 | object_class->finalize = msg_drive_item_finalize; | |
| 80 | 2 | } | |
| 81 | |||
| 82 | /** | ||
| 83 | * msg_drive_item_new_from_json: | ||
| 84 | * @object: The json object to parse | ||
| 85 | * @error: a #GError | ||
| 86 | * | ||
| 87 | * Creates a new `MsgDriveItem` from json response object. | ||
| 88 | * | ||
| 89 | * Returns: the newly created `MsgDriveItem` | ||
| 90 | */ | ||
| 91 | MsgDriveItem * | ||
| 92 | 41 | msg_drive_item_new_from_json (JsonObject *object, | |
| 93 | GError **error) | ||
| 94 | { | ||
| 95 | 41 | g_autoptr (MsgDriveItem) self = NULL; | |
| 96 | 41 | g_autoptr (GError) _error = NULL; | |
| 97 | MsgDriveItemPrivate *priv; | ||
| 98 | 41 | gboolean remote = FALSE; | |
| 99 | 41 | gboolean is_file = FALSE; | |
| 100 | 41 | gboolean is_folder = FALSE; | |
| 101 | |||
| 102 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 24 times.
|
41 | if (json_object_has_member (object, "file")) |
| 103 | 17 | is_file = TRUE; | |
| 104 |
2/2✓ Branch 0 taken 19 times.
✓ Branch 1 taken 5 times.
|
24 | else if (json_object_has_member (object, "folder")) |
| 105 | 19 | is_folder = TRUE; | |
| 106 | |||
| 107 |
5/6✓ Branch 0 taken 24 times.
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 19 times.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
|
41 | if (!is_file && !is_folder && json_object_has_member (object, "remoteItem")) { |
| 108 | 5 | JsonObject *remote_item = json_object_get_object_member (object, "remoteItem"); | |
| 109 | |||
| 110 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4 times.
|
5 | if (json_object_has_member (remote_item, "file")) |
| 111 | 1 | is_file = TRUE; | |
| 112 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | else if (json_object_has_member (remote_item, "folder")) |
| 113 | 4 | is_folder = TRUE; | |
| 114 | } | ||
| 115 | |||
| 116 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 23 times.
|
41 | if (is_file) { |
| 117 | 18 | self = MSG_DRIVE_ITEM (msg_drive_item_file_new_from_json (object)); | |
| 118 |
1/2✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
|
23 | } else if (is_folder) { |
| 119 | 23 | self = MSG_DRIVE_ITEM (msg_drive_item_folder_new_from_json (object, &_error)); | |
| 120 | } else { | ||
| 121 | ✗ | g_warning ("Unknown item type\n"); | |
| 122 | ✗ | g_set_error (error, | |
| 123 | MSG_ERROR, | ||
| 124 | MSG_ERROR_FAILED, | ||
| 125 | "Unknown item type\n"); | ||
| 126 | ✗ | return NULL; | |
| 127 | } | ||
| 128 | |||
| 129 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
|
41 | if (!self) |
| 130 | ✗ | return NULL; | |
| 131 | |||
| 132 | 41 | priv = msg_drive_item_get_instance_private (self); | |
| 133 | |||
| 134 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 36 times.
|
41 | if (json_object_has_member (object, "remoteItem")) { |
| 135 | 5 | JsonObject *remote_item = json_object_get_object_member (object, "remoteItem"); | |
| 136 | 5 | JsonObject *parent_reference = json_object_get_object_member (remote_item, "parentReference"); | |
| 137 | |||
| 138 | 5 | priv->remote_id = g_strdup (msg_json_object_get_string (remote_item, "id")); | |
| 139 | 5 | priv->remote_drive_id = g_strdup (msg_json_object_get_string (parent_reference, "driveId")); | |
| 140 | 5 | priv->remote_parent_id = g_strdup (msg_json_object_get_string (parent_reference, "id")); | |
| 141 | 5 | remote = TRUE; | |
| 142 | } | ||
| 143 | |||
| 144 | 41 | priv->is_shared = remote; | |
| 145 | 41 | priv->id = g_strdup (msg_json_object_get_string (object, "id")); | |
| 146 | |||
| 147 |
1/2✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
|
41 | if (json_object_has_member (object, "parentReference")) { |
| 148 | 41 | JsonObject *parent_reference = json_object_get_object_member (object, "parentReference"); | |
| 149 | |||
| 150 | 82 | priv->drive_id = g_strdup (msg_json_object_get_string (parent_reference, "driveId")); | |
| 151 | } | ||
| 152 | |||
| 153 | 41 | priv->name = g_strdup (msg_json_object_get_string (object, "name")); | |
| 154 | |||
| 155 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 5 times.
|
41 | if (json_object_has_member (object, "size")) |
| 156 | 36 | priv->size = json_object_get_int_member (object, "size"); | |
| 157 | else | ||
| 158 | 5 | priv->size = 0; | |
| 159 | |||
| 160 | 41 | priv->etag = g_strdup (msg_json_object_get_string (object, "eTag")); | |
| 161 | |||
| 162 |
2/2✓ Branch 0 taken 37 times.
✓ Branch 1 taken 4 times.
|
41 | if (json_object_has_member (object, "createdBy")) { |
| 163 | JsonObject *created_by; | ||
| 164 | |||
| 165 | 37 | created_by = json_object_get_object_member (object, "createdBy"); | |
| 166 |
2/4✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 37 times.
✗ Branch 3 not taken.
|
37 | if (created_by && json_object_has_member (created_by, "user")) { |
| 167 | 37 | JsonObject *user = json_object_get_object_member (created_by, "user"); | |
| 168 | 74 | priv->user = g_strdup (msg_json_object_get_string (user, "displayName")); | |
| 169 | } | ||
| 170 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | } else if (json_object_has_member (object, "lastModifiedBy")) { |
| 171 | JsonObject *created_by; | ||
| 172 | |||
| 173 | ✗ | created_by = json_object_get_object_member (object, "lastModifiedBy"); | |
| 174 | ✗ | if (created_by && json_object_has_member (created_by, "user")) { | |
| 175 | ✗ | JsonObject *user = json_object_get_object_member (created_by, "user"); | |
| 176 | ✗ | priv->user = g_strdup (msg_json_object_get_string (user, "displayName")); | |
| 177 | } | ||
| 178 | } | ||
| 179 | |||
| 180 |
1/2✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
|
41 | if (json_object_has_member (object, "createdDateTime")) { |
| 181 | 41 | const char *time = json_object_get_string_member (object, "createdDateTime"); | |
| 182 | 41 | priv->created = g_date_time_new_from_iso8601 (time, NULL); | |
| 183 | } | ||
| 184 | |||
| 185 |
1/2✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
|
41 | if (json_object_has_member (object, "lastModifiedDateTime")) { |
| 186 | 41 | const char *time = json_object_get_string_member (object, "lastModifiedDateTime"); | |
| 187 | 41 | priv->modified = g_date_time_new_from_iso8601 (time, NULL); | |
| 188 | } | ||
| 189 | |||
| 190 | 41 | return g_steal_pointer (&self); | |
| 191 | } | ||
| 192 | |||
| 193 | /** | ||
| 194 | * msg_drive_item_get_name: | ||
| 195 | * @self: a drive item | ||
| 196 | * | ||
| 197 | * Gets name of drive item. | ||
| 198 | * | ||
| 199 | * Returns: (transfer none): name of drive item | ||
| 200 | */ | ||
| 201 | const char * | ||
| 202 | 16 | msg_drive_item_get_name (MsgDriveItem *self) | |
| 203 | { | ||
| 204 | 16 | MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self); | |
| 205 | |||
| 206 | 16 | return priv->name; | |
| 207 | } | ||
| 208 | |||
| 209 | /** | ||
| 210 | * msg_drive_item_set_name: | ||
| 211 | * @self: a drive item | ||
| 212 | * @name: new name of drive item | ||
| 213 | * | ||
| 214 | * Sets name of drive item. | ||
| 215 | */ | ||
| 216 | void | ||
| 217 | 7 | msg_drive_item_set_name (MsgDriveItem *self, | |
| 218 | const char *name) | ||
| 219 | { | ||
| 220 | 7 | MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self); | |
| 221 | |||
| 222 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
|
7 | g_clear_pointer (&priv->name, g_free); |
| 223 | 7 | priv->name = g_strdup (name); | |
| 224 | 7 | } | |
| 225 | |||
| 226 | /** | ||
| 227 | * msg_drive_item_get_id: | ||
| 228 | * @self: a drive item | ||
| 229 | * | ||
| 230 | * Get id of drive item. | ||
| 231 | * | ||
| 232 | * Returns: (transfer none): id of drive item | ||
| 233 | */ | ||
| 234 | const char * | ||
| 235 | 48 | msg_drive_item_get_id (MsgDriveItem *self) | |
| 236 | { | ||
| 237 | 48 | MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self); | |
| 238 | |||
| 239 | 48 | return priv->id; | |
| 240 | } | ||
| 241 | |||
| 242 | /** | ||
| 243 | * msg_drive_item_set_id: | ||
| 244 | * @self: a drive item | ||
| 245 | * @id: new id of drive item | ||
| 246 | * | ||
| 247 | * Sets id of drive item. | ||
| 248 | */ | ||
| 249 | void | ||
| 250 | 2 | msg_drive_item_set_id (MsgDriveItem *self, | |
| 251 | const char *id) | ||
| 252 | { | ||
| 253 | 2 | MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self); | |
| 254 | |||
| 255 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | g_clear_pointer (&priv->id, g_free); |
| 256 | 2 | priv->id = g_strdup (id); | |
| 257 | 2 | } | |
| 258 | |||
| 259 | /** | ||
| 260 | * msg_drive_item_get_created: | ||
| 261 | * @self: a drive item | ||
| 262 | * | ||
| 263 | * Get created time of drive item. | ||
| 264 | * | ||
| 265 | * Returns: created date time of drive item | ||
| 266 | */ | ||
| 267 | gint64 | ||
| 268 | 1 | msg_drive_item_get_created (MsgDriveItem *self) | |
| 269 | { | ||
| 270 | 1 | MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self); | |
| 271 | |||
| 272 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | return priv->created ? g_date_time_to_unix (priv->created) : 0; |
| 273 | } | ||
| 274 | |||
| 275 | /** | ||
| 276 | * msg_drive_item_get_modified: | ||
| 277 | * @self: a drive item | ||
| 278 | * | ||
| 279 | * Gets modified time of drive item. | ||
| 280 | * | ||
| 281 | * Returns: modified date time of drive item | ||
| 282 | */ | ||
| 283 | gint64 | ||
| 284 | 1 | msg_drive_item_get_modified (MsgDriveItem *self) | |
| 285 | { | ||
| 286 | 1 | MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self); | |
| 287 | |||
| 288 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | return priv->modified ? g_date_time_to_unix (priv->modified) : 0; |
| 289 | } | ||
| 290 | |||
| 291 | /** | ||
| 292 | * msg_drive_item_get_etag: | ||
| 293 | * @self: a drive item | ||
| 294 | * | ||
| 295 | * Gets etag of drive item. | ||
| 296 | * | ||
| 297 | * Returns: (transfer none): etag of drive item | ||
| 298 | */ | ||
| 299 | const char * | ||
| 300 | 1 | msg_drive_item_get_etag (MsgDriveItem *self) | |
| 301 | { | ||
| 302 | 1 | MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self); | |
| 303 | |||
| 304 | 1 | return priv->etag; | |
| 305 | } | ||
| 306 | |||
| 307 | /** | ||
| 308 | * msg_drive_item_get_user: | ||
| 309 | * @self: a drive item | ||
| 310 | * | ||
| 311 | * Gets user of drive item. | ||
| 312 | * | ||
| 313 | * Returns: (transfer none): user of drive item | ||
| 314 | */ | ||
| 315 | const char * | ||
| 316 | 1 | msg_drive_item_get_user (MsgDriveItem *self) | |
| 317 | { | ||
| 318 | 1 | MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self); | |
| 319 | |||
| 320 | 1 | return priv->user; | |
| 321 | } | ||
| 322 | |||
| 323 | /** | ||
| 324 | * msg_drive_item_get_size | ||
| 325 | * @self: a drive item | ||
| 326 | * | ||
| 327 | * Gets size of drive item. | ||
| 328 | * | ||
| 329 | * Returns: size of drive item | ||
| 330 | */ | ||
| 331 | guint64 | ||
| 332 | 1 | msg_drive_item_get_size (MsgDriveItem *self) | |
| 333 | { | ||
| 334 | 1 | MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self); | |
| 335 | |||
| 336 | 1 | return priv->size; | |
| 337 | } | ||
| 338 | |||
| 339 | /** | ||
| 340 | * msg_drive_item_get_drive_id: | ||
| 341 | * @self: a drive item | ||
| 342 | * | ||
| 343 | * Gets drive id of drive item. | ||
| 344 | * | ||
| 345 | * Returns: (transfer none): drive id of drive item | ||
| 346 | */ | ||
| 347 | const char * | ||
| 348 | 46 | msg_drive_item_get_drive_id (MsgDriveItem *self) | |
| 349 | { | ||
| 350 | 46 | MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self); | |
| 351 | |||
| 352 | 46 | return priv->drive_id; | |
| 353 | } | ||
| 354 | |||
| 355 | /** | ||
| 356 | * msg_drive_item_get_parent_id: | ||
| 357 | * @self: a drive item | ||
| 358 | * | ||
| 359 | * Gets parent id of drive item. | ||
| 360 | * | ||
| 361 | * Returns: (transfer none): parent id of drive item | ||
| 362 | */ | ||
| 363 | const char * | ||
| 364 | 2 | msg_drive_item_get_parent_id (MsgDriveItem *self) | |
| 365 | { | ||
| 366 | 2 | MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self); | |
| 367 | |||
| 368 | 2 | return priv->parent_id; | |
| 369 | } | ||
| 370 | |||
| 371 | /** | ||
| 372 | * msg_drive_item_is_shared: | ||
| 373 | * @self: a drive item | ||
| 374 | * | ||
| 375 | * Gets whether item is shared. | ||
| 376 | * | ||
| 377 | * Returns: %TRUE if item is shared, otherwise %FALSE | ||
| 378 | */ | ||
| 379 | gboolean | ||
| 380 | 4 | msg_drive_item_is_shared (MsgDriveItem *self) | |
| 381 | { | ||
| 382 | 4 | MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self); | |
| 383 | |||
| 384 | 4 | return priv->is_shared; | |
| 385 | } | ||
| 386 | |||
| 387 | /** | ||
| 388 | * msg_drive_item_set_parent_id: | ||
| 389 | * @self: a drive item | ||
| 390 | * @parent_id: parent id | ||
| 391 | * | ||
| 392 | * Set parent id of drive item. | ||
| 393 | */ | ||
| 394 | void | ||
| 395 | 2 | msg_drive_item_set_parent_id (MsgDriveItem *self, | |
| 396 | const char *parent_id) | ||
| 397 | { | ||
| 398 | 2 | MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self); | |
| 399 | |||
| 400 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | g_clear_pointer (&priv->parent_id, g_free); |
| 401 | 2 | priv->parent_id = g_strdup (parent_id); | |
| 402 | 2 | } | |
| 403 | |||
| 404 | /** | ||
| 405 | * msg_drive_item_get_remote_drive_id: | ||
| 406 | * @self: a drive item | ||
| 407 | * | ||
| 408 | * Get remote parent drive id of drive item. | ||
| 409 | */ | ||
| 410 | const char * | ||
| 411 | ✗ | msg_drive_item_get_remote_drive_id (MsgDriveItem *self) | |
| 412 | { | ||
| 413 | ✗ | MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self); | |
| 414 | |||
| 415 | ✗ | return priv->remote_drive_id; | |
| 416 | } | ||
| 417 | |||
| 418 | /** | ||
| 419 | * msg_drive_item_get_remote_id: | ||
| 420 | * @self: a drive item | ||
| 421 | * | ||
| 422 | * Get remote id of drive item. | ||
| 423 | */ | ||
| 424 | const char * | ||
| 425 | ✗ | msg_drive_item_get_remote_id (MsgDriveItem *self) | |
| 426 | { | ||
| 427 | ✗ | MsgDriveItemPrivate *priv = msg_drive_item_get_instance_private (self); | |
| 428 | |||
| 429 | ✗ | return priv->remote_id; | |
| 430 | } | ||
| 431 |