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-item-file.h" | ||
18 | #include "msg-error.h" | ||
19 | #include "msg-json-utils.h" | ||
20 | |||
21 | struct _MsgDriveItemFile { | ||
22 | MsgDriveItem parent_instance; | ||
23 | char *mime_type; | ||
24 | char *thumbnail_uri; | ||
25 | }; | ||
26 | |||
27 |
5/6✓ Branch 0 taken 2 times.
✓ Branch 1 taken 54 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 52 times.
|
58 | G_DEFINE_TYPE (MsgDriveItemFile, msg_drive_item_file, MSG_TYPE_DRIVE_ITEM); |
28 | |||
29 | static void | ||
30 | 24 | msg_drive_item_file_finalize (GObject *_self) | |
31 | { | ||
32 | 24 | MsgDriveItemFile *self = MSG_DRIVE_ITEM_FILE (_self); | |
33 | |||
34 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 7 times.
|
24 | g_clear_pointer (&self->mime_type, g_free); |
35 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 23 times.
|
24 | g_clear_pointer (&self->thumbnail_uri, g_free); |
36 | |||
37 | 24 | G_OBJECT_CLASS (msg_drive_item_file_parent_class)->finalize (_self); | |
38 | 24 | } | |
39 | |||
40 | static void | ||
41 | 25 | msg_drive_item_file_init (__attribute__ ((unused)) MsgDriveItemFile *self) | |
42 | { | ||
43 | 25 | } | |
44 | |||
45 | static void | ||
46 | 2 | msg_drive_item_file_class_init (MsgDriveItemFileClass *klass) | |
47 | { | ||
48 | 2 | GObjectClass *gobject_class = G_OBJECT_CLASS (klass); | |
49 | |||
50 | 2 | gobject_class->finalize = msg_drive_item_file_finalize; | |
51 | 2 | } | |
52 | |||
53 | /** | ||
54 | * msg_drive_item_file_new: | ||
55 | * | ||
56 | * Creates a new `MsgDriveItemFile`. | ||
57 | * | ||
58 | * Returns: the newly created `MsgDriveItemFile` | ||
59 | */ | ||
60 | MsgDriveItemFile * | ||
61 | 25 | msg_drive_item_file_new (void) | |
62 | { | ||
63 | 25 | return g_object_new (MSG_TYPE_DRIVE_ITEM_FILE, NULL); | |
64 | } | ||
65 | |||
66 | /** | ||
67 | * msg_drive_item_file_new_from_json: | ||
68 | * @object: The json object to parse | ||
69 | * | ||
70 | * Creates a new `MsgDriveItemFile` from json response object. | ||
71 | * | ||
72 | * Returns: the newly created `MsgDriveItemFile` | ||
73 | */ | ||
74 | MsgDriveItemFile * | ||
75 | 18 | msg_drive_item_file_new_from_json (JsonObject *object) | |
76 | { | ||
77 | 18 | MsgDriveItemFile *self = msg_drive_item_file_new (); | |
78 | 18 | JsonObject *file = NULL; | |
79 | |||
80 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 1 times.
|
18 | if (json_object_has_member (object, "file")) |
81 | 17 | file = json_object_get_object_member (object, "file"); | |
82 | else { | ||
83 | 1 | JsonObject *remote_item = json_object_get_object_member (object, "remoteItem"); | |
84 | 1 | file = json_object_get_object_member (remote_item, "file"); | |
85 | } | ||
86 | |||
87 | 18 | self->mime_type = g_strdup (msg_json_object_get_string (file, "mimeType")); | |
88 | |||
89 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 16 times.
|
18 | if (json_object_has_member (object, "thumbnails")) { |
90 | JsonArray *array; | ||
91 | guint array_length; | ||
92 | guint index; | ||
93 | |||
94 | 2 | array = json_object_get_array_member (object, "thumbnails"); | |
95 | 2 | array_length = json_array_get_length (array); | |
96 | |||
97 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
|
3 | for (index = 0; index < array_length; index++) { |
98 | JsonObject *item_object; | ||
99 | JsonObject *small; | ||
100 | |||
101 | /* TODO: Extract best thumbnail */ | ||
102 | 1 | item_object = json_array_get_object_element (array, index); | |
103 | 1 | small = json_object_get_object_member (item_object, "small"); | |
104 | |||
105 | 2 | self->thumbnail_uri = g_strdup (msg_json_object_get_string (small, "url")); | |
106 | } | ||
107 | } | ||
108 | |||
109 | 18 | return self; | |
110 | } | ||
111 | |||
112 | /** | ||
113 | * msg_drive_item_file_get_mime_type: | ||
114 | * @self: a drive item file | ||
115 | * | ||
116 | * Gets mime type of drive item file. | ||
117 | * | ||
118 | * Returns: (transfer none): mime type of drive item file | ||
119 | */ | ||
120 | const char * | ||
121 | 1 | msg_drive_item_file_get_mime_type (MsgDriveItemFile *self) | |
122 | { | ||
123 | 1 | return self->mime_type; | |
124 | } | ||
125 | |||
126 | /** | ||
127 | * msg_drive_item_file_get_thumbnail_uri: | ||
128 | * @self: a drive item file | ||
129 | * | ||
130 | * Gets thumbnail uri of drive item file. | ||
131 | * | ||
132 | * Returns: (transfer none): thumbnail uri of drive item file | ||
133 | */ | ||
134 | const char * | ||
135 | 1 | msg_drive_item_file_get_thumbnail_uri (MsgDriveItemFile *self) | |
136 | { | ||
137 | 1 | return self->thumbnail_uri; | |
138 | } | ||
139 |