--- a/application/davcontroller.c Mon Feb 12 17:32:02 2024 +0100 +++ b/application/davcontroller.c Mon Feb 12 20:38:54 2024 +0100 @@ -673,6 +673,7 @@ DavBrowser *browser; DavSession *sn; + DavSession *download_sn; DavResource *reslist; char *local_path; DavBool isdirectory; @@ -694,10 +695,47 @@ UiString *label_bottom_right; } DavFileDownload; + +static int uithr_download_update_progress(void *data) { + DavFileDownload *download = data; + + char *sz_total = util_size_str(FALSE, download->total_bytes); + char *sz_downloaded = util_size_str2(FALSE, download->downloaded_bytes, download->total_bytes, 2); + char *sz_downloaded_end = strchr(sz_downloaded, ' '); + if (sz_downloaded_end) { + *sz_downloaded_end = 0; + } + + double progress = (double)download->downloaded_bytes / (double)download->total_bytes; + ui_set(download->progress, progress); + + cxmutstr label1; + if (download->total_files + download->total_directories > 1) { + label1 = cx_asprintf( + "%s/%s %zu/%zu files", + sz_downloaded, + sz_total, + download->downloaded_files+download->downloaded_directories, + download->total_files+download->total_directories); + } else { + label1 = cx_asprintf( + "%s/%s", + sz_downloaded, + sz_total); + } + ui_set(download->label_top_left, label1.ptr); + + free(sz_total); + free(label1.ptr); + + + return 0; +} + // dav download file typedef struct DDFile { DavFileDownload *download; - DavResource *res; + char *path; char *to; } DDFile; @@ -709,11 +747,27 @@ return 0; } - dav_get_content(file->res, f, (dav_write_func)fwrite); + DavResource *res = dav_resource_new(file->download->download_sn, file->path); + dav_get_content(res, f, (dav_write_func)fwrite); + + file->download->downloaded_bytes += res->contentlength; + file->download->downloaded_files++; + + dav_resource_free(res); fclose(f); + + free(file->path); + free(file->to); + free(file); } + +typedef struct DlStackElm { + DavResource *resource; + char *sub_path; +} DlStackElm; + static int jobthr_download_scan(void *data) { DavFileDownload *download = data; DavBrowser *browser = download->browser; @@ -726,22 +780,76 @@ } } + CxList *stack = cxLinkedListCreateSimple(sizeof(DlStackElm)); + // add selected files to the download queue DavResource *res = download->reslist; while (res) { - DDFile *file = malloc(sizeof(DDFile)); - file->download = download; - file->res = res; - if (download->isdirectory) { - file->to = util_concat_path(download->local_path, res->name); - } else { - file->to = strdup(download->local_path); - } - - ui_threadpool_job(download->queue, download->ui, qthr_download_resource, file, NULL, NULL); + DlStackElm elm; + elm.resource = res; + elm.sub_path = strdup(res->name); + cxListAdd(stack, &elm); res = res->next; } + + while (stack->size > 0) { + DlStackElm *elm = cxListAt(stack, 0); + DavResource *res = elm->resource; + char *sub_path = elm->sub_path; + cxListRemove(stack, 0); + + if (res->iscollection) { + if (dav_load(res)) { + // TODO: handle error + continue; + } + + download->total_directories++; + + // update ui + //ui_call_mainthread(uithr_download_update_progress, download); + + char *path = util_concat_path(download->local_path, sub_path); + int err = sys_mkdir(path); + free(path); + if (err) { + // TODO: handle error + } + + DavResource *child = res->children; + while (child) { + char *child_path = util_concat_path(sub_path, child->name); + DlStackElm childelm; + childelm.resource = child; + childelm.sub_path = child_path; + cxListAdd(stack, &childelm); + + child = child->next; + } + } else { + // add the file to the download queue + DDFile *file = malloc(sizeof(DDFile)); + file->download = download; + file->path = strdup(res->path); + if (download->isdirectory) { + file->to = util_concat_path(download->local_path, sub_path); + } else { + file->to = strdup(download->local_path); + } + + // stats + download->total_files++; + download->total_bytes += res->contentlength; + + // update ui + //ui_call_mainthread(uithr_download_update_progress, download); + + ui_threadpool_job(download->queue, download->ui, qthr_download_resource, file, NULL, NULL); + } + } + + cxListDestroy(stack); } static void uithr_download_scan_finished(UiEvent *event, void *data) { @@ -757,8 +865,10 @@ DavFileDownload *download = malloc(sizeof(DavFileDownload)); memset(download, 0, sizeof(DavFileDownload)); + download->ui = ui; download->browser = browser; download->sn = reslist->session; + download->download_sn = dav_session_clone(download->sn); download->reslist = reslist; download->local_path = strdup(local_path);