46 DavFileDownload *download = data; |
46 DavFileDownload *download = data; |
47 if(download->cancel) { |
47 if(download->cancel) { |
48 return 1; |
48 return 1; |
49 } |
49 } |
50 |
50 |
|
51 time_t t = time(NULL); |
|
52 |
51 char *sz_total = util_size_str(FALSE, download->progress.total_bytes); |
53 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); |
54 char *sz_downloaded = util_size_str2(FALSE, download->progress.transferred_bytes, download->progress.total_bytes, 2); |
53 char *sz_downloaded_end = strchr(sz_downloaded, ' '); |
55 char *sz_downloaded_end = strchr(sz_downloaded, ' '); |
54 if (sz_downloaded_end) { |
56 if (sz_downloaded_end) { |
55 *sz_downloaded_end = 0; |
57 *sz_downloaded_end = 0; |
56 } |
58 } |
57 |
59 |
58 if (download->progress.total_bytes > 0) { |
60 if (download->progress.total_bytes > 0) { |
59 double progress = (double)download->progress.transferred_bytes / (double)download->progress.total_bytes; |
61 double progress = ((double)download->progress.transferred_bytes / (double)download->progress.total_bytes)*100; |
60 ui_set(download->progressbar, progress*100); |
62 ui_set(download->progressbar, progress); |
|
63 download->trans.progress = progress; |
|
64 /* |
|
65 if(t > download->trans.last_update + 2) { |
|
66 snprintf(download->trans.label+download->trans.label_len, 12, " %d%%", (int)progress); |
|
67 application_update_transferlist(); |
|
68 download->trans.last_update = t; |
|
69 } |
|
70 */ |
61 } |
71 } |
62 |
72 |
63 |
73 |
64 cxmutstr label1; |
74 cxmutstr label1; |
65 if (download->progress.total_files + download->progress.total_directories > 1) { |
75 if (download->progress.total_files + download->progress.total_directories > 1) { |
261 static void uithr_download_scan_finished(UiEvent *event, void *data) { |
270 static void uithr_download_scan_finished(UiEvent *event, void *data) { |
262 DavFileDownload *download = data; |
271 DavFileDownload *download = data; |
263 |
272 |
264 } |
273 } |
265 |
274 |
266 static void download_window_closed(UiEvent *event, void *data) { |
|
267 DavFileDownload *download = event->obj->window; |
|
268 |
|
269 dav_session_destroy(download->sn); |
|
270 ui_threadpool_destroy(download->queue); |
|
271 } |
|
272 |
|
273 void action_download_cancel(UiEvent *event, void *data) { |
275 void action_download_cancel(UiEvent *event, void *data) { |
274 DavFileDownload *download = event->window; |
276 DavFileDownload *download = event->window; |
275 if(!download->cancel) { |
277 if(!download->cancel) { |
276 ui_set(download->label_bottom_left, "Cancel..."); |
278 ui_set(download->label_bottom_left, "Cancel..."); |
277 download->cancel = TRUE; |
279 download->cancel = TRUE; |
278 } |
280 } |
279 } |
281 } |
280 |
282 |
|
283 static void dav_file_download_cleanup(DavFileDownload *download) { |
|
284 application_remove_transfer(&download->trans); |
|
285 ui_object_unref(download->browser->window); |
|
286 } |
281 |
287 |
282 DavFileDownload* dav_download_create(DavBrowser *browser, UiObject *dialog, DavResource *reslist, const char *local_path) { |
288 DavFileDownload* dav_download_create(DavBrowser *browser, UiObject *dialog, DavResource *reslist, const char *local_path) { |
283 UiContext *ctx = dialog->ctx; |
289 UiContext *ctx = dialog->ctx; |
|
290 CxMempool *mp = ui_cx_mempool(ctx); |
284 DavFileDownload *download = ui_malloc(ctx, sizeof(DavFileDownload)); |
291 DavFileDownload *download = ui_malloc(ctx, sizeof(DavFileDownload)); |
285 memset(download, 0, sizeof(DavFileDownload)); |
292 memset(download, 0, sizeof(DavFileDownload)); |
286 download->dialog = dialog; |
293 download->dialog = dialog; |
287 dialog->window = download; |
294 dialog->window = download; |
288 ui_object_ref(dialog); |
295 ui_object_ref(dialog); |
|
296 ui_object_ref(browser->window); |
|
297 |
|
298 size_t label_len = strlen(reslist->name) + 16; |
|
299 download->trans.label = cxCalloc(mp->allocator, label_len, 1); |
|
300 download->trans.label_len = snprintf(download->trans.label, label_len, "< %s%s", reslist->name, reslist->next ? " ..." : ""); |
|
301 download->trans.window = dialog; |
289 |
302 |
290 download->browser = browser; |
303 download->browser = browser; |
291 download->sn = reslist->session; |
304 download->sn = reslist->session; |
292 download->download_sn = dav_session_clone(download->sn); |
305 download->download_sn = dav_session_clone(download->sn); |
293 download->reslist = reslist; // TODO: is this safe or do we need a copy? |
306 download->reslist = reslist; // TODO: is this safe or do we need a copy? |
294 download->local_path = ui_strdup(dialog->ctx, local_path); |
307 download->local_path = ui_strdup(dialog->ctx, local_path); |
295 |
308 |
296 download->queue = ui_threadpool_create(1); |
309 download->queue = ui_threadpool_create(1); |
297 |
310 |
298 CxMempool *mp = ui_cx_mempool(ctx); |
|
299 cxMempoolRegister(mp, download->download_sn, (cx_destructor_func)dav_session_destroy); |
311 cxMempoolRegister(mp, download->download_sn, (cx_destructor_func)dav_session_destroy); |
300 cxMempoolRegister(mp, download->queue, (cx_destructor_func)ui_threadpool_destroy); |
312 cxMempoolRegister(mp, download->queue, (cx_destructor_func)ui_threadpool_destroy); |
|
313 cxMempoolSetDestructor(download, (cx_destructor_func)dav_file_download_cleanup); |
301 |
314 |
302 download->progressbar = ui_double_new(ctx, "progressbar"); |
315 download->progressbar = ui_double_new(ctx, "progressbar"); |
303 download->label_top_left = ui_string_new(ctx, "label_top_left"); |
316 download->label_top_left = ui_string_new(ctx, "label_top_left"); |
304 download->label_top_right = ui_string_new(ctx, "label_top_right"); |
317 download->label_top_right = ui_string_new(ctx, "label_top_right"); |
305 download->label_bottom_left = ui_string_new(ctx, "label_bottom_left"); |
318 download->label_bottom_left = ui_string_new(ctx, "label_bottom_left"); |