application/download.c

changeset 87
5360027fb282
equal deleted inserted replaced
86:8e7c57c23133 87:5360027fb282
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2024 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "download.h"
30
31 #include "davcontroller.h"
32 #include "window.h"
33
34 #include <cx/printf.h>
35
36 #include "config.h"
37
38 #include "system.h"
39 #include "common/context.h"
40
41 #include <libidav/config.h>
42 #include <libidav/utils.h>
43
44
45 static int uithr_download_update_progress(void *data) {
46 DavFileDownload *download = data;
47 if(download->cancel) {
48 return 1;
49 }
50
51 char *sz_total = util_size_str(FALSE, download->progress.total_bytes);
52 char *sz_downloaded = util_size_str2(FALSE, download->progress.transferred_bytes, download->progress.total_bytes, 2);
53 char *sz_downloaded_end = strchr(sz_downloaded, ' ');
54 if (sz_downloaded_end) {
55 *sz_downloaded_end = 0;
56 }
57
58 if (download->progress.total_bytes > 0) {
59 double progress = (double)download->progress.transferred_bytes / (double)download->progress.total_bytes;
60 ui_set(download->progressbar, progress*100);
61 }
62
63
64 cxmutstr label1;
65 if (download->progress.total_files + download->progress.total_directories > 1) {
66 label1 = cx_asprintf(
67 "%s/%s %zu/%zu files",
68 sz_downloaded,
69 sz_total,
70 download->progress.transferred_files+download->progress.transferred_directories,
71 download->progress.total_files+download->progress.total_directories);
72 } else {
73 label1 = cx_asprintf(
74 "%s/%s",
75 sz_downloaded,
76 sz_total);
77 }
78 ui_set(download->label_top_left, label1.ptr);
79
80 free(sz_total);
81 free(label1.ptr);
82
83
84 return 1;
85 }
86
87 static size_t ddfile_write(const void *buf, size_t size, size_t count, void *stream) {
88 DDFile *file = stream;
89 if(file->download->cancel) {
90 return 0;
91 }
92
93 size_t w = fwrite(buf, size, count, file->fd);
94 file->download->progress.current_file_transferred += w;
95
96 file->download->progress.transferred_bytes += w;
97
98 if (file->download->progress.current_file_transferred > file->download->progress.current_file_size) {
99 size_t diff = file->download->progress.current_file_transferred - file->download->progress.current_file_size;
100 file->download->progress.current_file_size = file->download->progress.current_file_transferred;
101 file->download->progress.total_bytes += diff;
102 }
103
104 ui_call_mainthread(uithr_download_update_progress, file->download);
105
106 return w;
107 }
108
109 static int qthr_download_resource(void *data) {
110 DDFile *file = data;
111 if(file->download->cancel) {
112 return 0;
113 }
114
115 file->download->progress.current_file_transferred = 0;
116 file->download->progress.current_file_size = file->size;
117
118 FILE *f = sys_fopen(file->to, "wb");
119 if (!f) {
120 return 0;
121 }
122 file->fd = f;
123
124 DavResource *res = dav_resource_new(file->download->download_sn, file->path);
125 dav_get_content(res, file, (dav_write_func)ddfile_write);
126
127 file->download->progress.transferred_files++;
128
129 ui_call_mainthread(uithr_download_update_progress, file->download);
130
131 dav_resource_free(res);
132
133 fclose(f);
134
135 free(file->path);
136 free(file->to);
137 free(file);
138
139 return 0;
140 }
141
142 static int qthr_download_finished(void *data) {
143 return 0;
144 }
145
146 static void uithr_download_finished(UiEvent *event, void *data) {
147 DavFileDownload *download = data;
148 if(download->cancel) {
149 ui_set(download->label_bottom_left, "Canceled");
150 }
151 if(download->dialog->ref > 1) {
152 ui_close(download->dialog);
153 }
154 ui_object_unref(download->dialog);
155 }
156
157
158 typedef struct DlStackElm {
159 DavResource *resource;
160 char *sub_path;
161 } DlStackElm;
162
163 static int jobthr_download_scan(void *data) {
164 DavFileDownload *download = data;
165
166 // check if the specified local location is a directory
167 SYS_STAT s;
168 if (!sys_stat(download->local_path, &s)) {
169 if (S_ISDIR(s.st_mode)) {
170 download->isdirectory = TRUE;
171 }
172 }
173
174 CxList *stack = cxLinkedListCreateSimple(sizeof(DlStackElm));
175
176 // add selected files to the download queue
177 DavResource *res = download->reslist;
178 while (res) {
179 DlStackElm elm;
180 elm.resource = res;
181 elm.sub_path = strdup(res->name);
182 cxListAdd(stack, &elm);
183
184 res = res->next;
185 }
186
187 while (cxListSize(stack) > 0 && !download->cancel) {
188 DlStackElm *elm = cxListAt(stack, 0);
189 DavResource *res = elm->resource;
190 char *sub_path = elm->sub_path;
191 cxListRemove(stack, 0);
192
193 if (res->iscollection) {
194 if (dav_load(res)) {
195 // TODO: handle error
196 continue;
197 }
198
199 // update ui
200 ui_call_mainthread(uithr_download_update_progress, download);
201
202 char *path = util_concat_path(download->local_path, sub_path);
203 int err = sys_mkdir(path);
204 free(path);
205 if (err) {
206 // TODO: handle error
207 }
208
209 DavResource *child = res->children;
210 while (child) {
211 char *child_path = util_concat_path(sub_path, child->name);
212 DlStackElm childelm;
213 childelm.resource = child;
214 childelm.sub_path = child_path;
215 cxListAdd(stack, &childelm);
216
217 child = child->next;
218 }
219 } else {
220 // add the file to the download queue
221 DDFile *file = malloc(sizeof(DDFile));
222 file->download = download;
223 file->path = strdup(res->path);
224 file->size = res->contentlength;
225 if (download->isdirectory) {
226 file->to = util_concat_path(download->local_path, sub_path);
227 } else {
228 file->to = strdup(download->local_path);
229 }
230
231 // stats
232 download->progress.total_files++;
233 download->progress.total_bytes += res->contentlength;
234
235 // update ui
236 ui_call_mainthread(uithr_download_update_progress, download);
237
238 ui_threadpool_job(download->queue, download->dialog, qthr_download_resource, file, NULL, NULL);
239 }
240 }
241
242 ui_threadpool_job(download->queue, download->dialog, qthr_download_finished, download, uithr_download_finished, download);
243
244 cxListDestroy(stack);
245
246 return 0;
247 }
248
249 static void uithr_download_scan_finished(UiEvent *event, void *data) {
250 DavFileDownload *download = data;
251
252 }
253
254 static void download_window_closed(UiEvent *event, void *data) {
255 DavFileDownload *download = event->obj->window;
256
257 dav_session_destroy(download->sn);
258 ui_threadpool_destroy(download->queue);
259 }
260
261 void action_download_cancel(UiEvent *event, void *data) {
262 DavFileDownload *download = event->window;
263 if(!download->cancel) {
264 ui_set(download->label_bottom_left, "Cancel...");
265 download->cancel = TRUE;
266 }
267 }
268
269
270 DavFileDownload* dav_download_create(DavBrowser *browser, UiObject *dialog, DavResource *reslist, const char *local_path) {
271 UiContext *ctx = dialog->ctx;
272 DavFileDownload *download = ui_malloc(ctx, sizeof(DavFileDownload));
273 memset(download, 0, sizeof(DavFileDownload));
274 download->dialog = dialog;
275 dialog->window = download;
276 ui_object_ref(dialog);
277
278 download->browser = browser;
279 download->sn = reslist->session;
280 download->download_sn = dav_session_clone(download->sn);
281 download->reslist = reslist; // TODO: is this safe or do we need a copy?
282 download->local_path = ui_strdup(dialog->ctx, local_path);
283
284 download->queue = ui_threadpool_create(1);
285
286 CxMempool *mp = ui_cx_mempool(ctx);
287 cxMempoolRegister(mp, download->download_sn, (cx_destructor_func)dav_session_destroy);
288 cxMempoolRegister(mp, download->queue, (cx_destructor_func)ui_threadpool_destroy);
289
290 download->progressbar = ui_double_new(ctx, "progressbar");
291 download->label_top_left = ui_string_new(ctx, "label_top_left");
292 download->label_top_right = ui_string_new(ctx, "label_top_right");
293 download->label_bottom_left = ui_string_new(ctx, "label_bottom_left");
294 download->label_bottom_right = ui_string_new(ctx, "label_bottom_right");
295
296 ui_set(download->label_top_left, "");
297 ui_set(download->label_top_right, "");
298 ui_set(download->label_bottom_left, "");
299 ui_set(download->label_bottom_right, "");
300 ui_set(download->progressbar, 0);
301
302 return download;
303 }
304
305 void dav_download_start(DavFileDownload *download) {
306 ui_show(download->dialog);
307 ui_job(download->dialog, jobthr_download_scan, download, uithr_download_scan_finished, download);
308 }

mercurial