application/download.c

Wed, 27 Nov 2024 16:42:19 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 27 Nov 2024 16:42:19 +0100
changeset 92
43aee6c8c166
parent 91
7ee944cf53de
child 95
e92c72705da4
permissions
-rw-r--r--

add encryption and lock indicator, resolves #501

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

mercurial