| 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 "drive/msg-drive.h" | ||
| 18 | #include "msg-error.h" | ||
| 19 | #include "msg-json-utils.h" | ||
| 20 | |||
| 21 | /** | ||
| 22 | * MsgDrive: | ||
| 23 | * | ||
| 24 | * #MsgDriveService is a subclass of #MsgService for communicating with the MS Graph API. | ||
| 25 | * | ||
| 26 | * Details: https://learn.microsoft.com/en-us/graph/api/resources/drive?view=graph-rest-1.0 | ||
| 27 | */ | ||
| 28 | |||
| 29 | struct _MsgDrive { | ||
| 30 | GObject parent_instance; | ||
| 31 | |||
| 32 | MsgDriveType drive_type; | ||
| 33 | char *id; | ||
| 34 | char *name; | ||
| 35 | gulong remaining; | ||
| 36 | gulong total; | ||
| 37 | gulong used; | ||
| 38 | GDateTime *created; | ||
| 39 | GDateTime *modified; | ||
| 40 | }; | ||
| 41 | |||
| 42 |
5/6✓ Branch 0 taken 2 times.
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 22 times.
|
28 | G_DEFINE_TYPE (MsgDrive, msg_drive, G_TYPE_OBJECT); |
| 43 | |||
| 44 | static void | ||
| 45 | 11 | msg_drive_finalize (GObject *object) | |
| 46 | { | ||
| 47 | 11 | MsgDrive *self = MSG_DRIVE (object); | |
| 48 | |||
| 49 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1 times.
|
11 | g_clear_pointer (&self->id, g_free); |
| 50 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
|
11 | g_clear_pointer (&self->name, g_free); |
| 51 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
|
11 | g_clear_pointer (&self->created, g_date_time_unref); |
| 52 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
|
11 | g_clear_pointer (&self->modified, g_date_time_unref); |
| 53 | |||
| 54 | 11 | G_OBJECT_CLASS (msg_drive_parent_class)->finalize (object); | |
| 55 | 11 | } | |
| 56 | |||
| 57 | static void | ||
| 58 | 11 | msg_drive_init (__attribute__ ((unused)) MsgDrive *self) | |
| 59 | { | ||
| 60 | 11 | } | |
| 61 | |||
| 62 | static void | ||
| 63 | 2 | msg_drive_class_init (MsgDriveClass *class) | |
| 64 | { | ||
| 65 | 2 | GObjectClass *object_class = G_OBJECT_CLASS (class); | |
| 66 | |||
| 67 | 2 | object_class->finalize = msg_drive_finalize; | |
| 68 | 2 | } | |
| 69 | |||
| 70 | /** | ||
| 71 | * msg_drive_new: | ||
| 72 | * | ||
| 73 | * Creates a new `MsgDrive`. | ||
| 74 | * | ||
| 75 | * Returns: the newly created `MsgDrive` | ||
| 76 | */ | ||
| 77 | MsgDrive * | ||
| 78 | 11 | msg_drive_new (void) | |
| 79 | { | ||
| 80 | 11 | return g_object_new (MSG_TYPE_DRIVE, NULL); | |
| 81 | } | ||
| 82 | |||
| 83 | /** | ||
| 84 | * msg_drive_new_from_json: | ||
| 85 | * @object: The json object to parse | ||
| 86 | * @error: a #GError | ||
| 87 | * | ||
| 88 | * Creates a new `MsgDrive` from json response object. | ||
| 89 | * | ||
| 90 | * Returns: the newly created `MsgDrive` | ||
| 91 | */ | ||
| 92 | MsgDrive * | ||
| 93 | 10 | msg_drive_new_from_json (JsonObject *object, | |
| 94 | GError **error) | ||
| 95 | { | ||
| 96 | MsgDrive *self; | ||
| 97 | 10 | const char *drive_type = NULL; | |
| 98 | |||
| 99 | 10 | self = msg_drive_new (); | |
| 100 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | g_assert (self != NULL); |
| 101 | |||
| 102 | 10 | drive_type = json_object_get_string_member (object, "driveType"); | |
| 103 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | if (g_strcmp0 (drive_type, "personal") == 0) { |
| 104 | 10 | self->drive_type = MSG_DRIVE_TYPE_PERSONAL; | |
| 105 | ✗ | } else if (g_strcmp0 (drive_type, "business") == 0) { | |
| 106 | ✗ | self->drive_type = MSG_DRIVE_TYPE_BUSINESS; | |
| 107 | ✗ | } else if (g_strcmp0 (drive_type, "documentLibrary") == 0) { | |
| 108 | ✗ | self->drive_type = MSG_DRIVE_TYPE_DOCUMENT_LIBRARY; | |
| 109 | } else { | ||
| 110 | ✗ | g_set_error (error, | |
| 111 | msg_error_quark (), | ||
| 112 | MSG_ERROR_FAILED, | ||
| 113 | "Unknown drive type: %s", drive_type); | ||
| 114 | |||
| 115 | ✗ | return NULL; | |
| 116 | } | ||
| 117 | |||
| 118 | 10 | self->id = g_strdup (msg_json_object_get_string (object, "id")); | |
| 119 | 10 | self->name = g_strdup (msg_json_object_get_string (object, "name")); | |
| 120 | |||
| 121 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | if (json_object_has_member (object, "createdDateTime")) |
| 122 | ✗ | self->created = g_date_time_new_from_iso8601 (json_object_get_string_member (object, "createdDateTime"), NULL); | |
| 123 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | if (json_object_has_member (object, "lastModifiedDateTime")) |
| 124 | ✗ | self->modified = g_date_time_new_from_iso8601 (json_object_get_string_member (object, "lastModifiedDateTime"), NULL); | |
| 125 | |||
| 126 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | if (json_object_has_member (object, "quota")) { |
| 127 | 10 | JsonObject *quota = json_object_get_object_member (object, "quota"); | |
| 128 | |||
| 129 | 10 | self->total = json_object_get_int_member (quota, "total"); | |
| 130 | 10 | self->used = json_object_get_int_member (quota, "used"); | |
| 131 | 10 | self->remaining = json_object_get_int_member (quota, "remaining"); | |
| 132 | } | ||
| 133 | |||
| 134 | 10 | return self; | |
| 135 | } | ||
| 136 | |||
| 137 | /** | ||
| 138 | * msg_drive_get_id: | ||
| 139 | * @self: a drive | ||
| 140 | * | ||
| 141 | * Gets the ID of the drive. | ||
| 142 | * | ||
| 143 | * Returns: (transfer none): the id of the drive | ||
| 144 | */ | ||
| 145 | const char * | ||
| 146 | 19 | msg_drive_get_id (MsgDrive *self) | |
| 147 | { | ||
| 148 | 19 | return self->id; | |
| 149 | } | ||
| 150 | |||
| 151 | /** | ||
| 152 | * msg_drive_get_drive_type: | ||
| 153 | * @self: a drive | ||
| 154 | * | ||
| 155 | * Gets the drive type of the drive. | ||
| 156 | * | ||
| 157 | * Returns: the drive type of drive | ||
| 158 | */ | ||
| 159 | MsgDriveType | ||
| 160 | 11 | msg_drive_get_drive_type (MsgDrive *self) | |
| 161 | { | ||
| 162 | 11 | return self->drive_type; | |
| 163 | } | ||
| 164 | |||
| 165 | /** | ||
| 166 | * msg_drive_get_name: | ||
| 167 | * @self: a drive | ||
| 168 | * | ||
| 169 | * Gets tthe name of the drive. | ||
| 170 | * | ||
| 171 | * Returns: (transfer none): name of drive | ||
| 172 | */ | ||
| 173 | const char * | ||
| 174 | 11 | msg_drive_get_name (MsgDrive *self) | |
| 175 | { | ||
| 176 | 11 | return self->name; | |
| 177 | } | ||
| 178 | |||
| 179 | /** | ||
| 180 | * msg_drive_get_total: | ||
| 181 | * @self: a drive | ||
| 182 | * | ||
| 183 | * Gets the total size of the drive. | ||
| 184 | * | ||
| 185 | * Returns: total size of drive | ||
| 186 | */ | ||
| 187 | gulong | ||
| 188 | 1 | msg_drive_get_total (MsgDrive *self) | |
| 189 | { | ||
| 190 | 1 | return self->total; | |
| 191 | } | ||
| 192 | |||
| 193 | /** | ||
| 194 | * msg_drive_get_used: | ||
| 195 | * @self: a drive | ||
| 196 | * | ||
| 197 | * Gets the used size of the drive. | ||
| 198 | * | ||
| 199 | * Returns: used size of drive | ||
| 200 | */ | ||
| 201 | gulong | ||
| 202 | 1 | msg_drive_get_used (MsgDrive *self) | |
| 203 | { | ||
| 204 | 1 | return self->used; | |
| 205 | } | ||
| 206 | |||
| 207 | /** | ||
| 208 | * msg_drive_get_remaining: | ||
| 209 | * @self: a drive | ||
| 210 | * | ||
| 211 | * Gets the remaining size of the drive. | ||
| 212 | * | ||
| 213 | * Returns: remaining size of drive | ||
| 214 | */ | ||
| 215 | gulong | ||
| 216 | 1 | msg_drive_get_remaining (MsgDrive *self) | |
| 217 | { | ||
| 218 | 1 | return self->remaining; | |
| 219 | } | ||
| 220 | |||
| 221 | /** | ||
| 222 | * msg_drive_get_created: | ||
| 223 | * @self: a drive | ||
| 224 | * | ||
| 225 | * Get created time of drive. | ||
| 226 | * | ||
| 227 | * Returns: (transfer none): created time of drive | ||
| 228 | */ | ||
| 229 | const GDateTime * | ||
| 230 | 1 | msg_drive_get_created (MsgDrive *self) | |
| 231 | { | ||
| 232 | 1 | return self->created; | |
| 233 | } | ||
| 234 | |||
| 235 | /** | ||
| 236 | * msg_drive_get_modified: | ||
| 237 | * @self: a drive | ||
| 238 | * | ||
| 239 | * Gets the modified time of the drive. | ||
| 240 | * | ||
| 241 | * Returns: (transfer none): modified time of drive | ||
| 242 | */ | ||
| 243 | const GDateTime * | ||
| 244 | 1 | msg_drive_get_modified (MsgDrive *self) | |
| 245 | { | ||
| 246 | 1 | return self->modified; | |
| 247 | } | ||
| 248 |