implement download progress bar default tip

Mon, 12 Feb 2024 21:13:23 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 12 Feb 2024 21:13:23 +0100
changeset 31
bf810176ddb8
parent 30
762afc7adc63

implement download progress bar

application/davcontroller.c file | annotate | diff | comparison | revisions
--- a/application/davcontroller.c	Mon Feb 12 20:38:54 2024 +0100
+++ b/application/davcontroller.c	Mon Feb 12 21:13:23 2024 +0100
@@ -687,6 +687,9 @@
     size_t downloaded_files;
     size_t downloaded_directories;
 
+    size_t current_file_size;
+    size_t current_file_downloaded;
+
     UiObject *dialog;
     UiDouble *progress;
     UiString *label_top_left;
@@ -698,7 +701,6 @@
 
 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, ' ');
@@ -706,8 +708,11 @@
         *sz_downloaded_end = 0;
     }
 
-    double progress = (double)download->downloaded_bytes / (double)download->total_bytes;
-    ui_set(download->progress, progress);
+    if (download->total_bytes > 0) {
+        double progress = (double)download->downloaded_bytes / (double)download->total_bytes;
+        ui_set(download->progress, progress*100);
+    }
+    
 
     cxmutstr label1;
     if (download->total_files + download->total_directories > 1) {
@@ -729,30 +734,57 @@
     free(label1.ptr);
 
 
-    return 0;
+    return 1;
 }
 
 // dav download file
 typedef struct DDFile {
     DavFileDownload *download;
+    size_t size;
     char *path;
     char *to;
+    FILE *fd;
 } DDFile;
 
+static size_t ddfile_write(const void *buf, size_t size, size_t count, void *stream) {
+    DDFile *file = stream;
+
+    size_t w = fwrite(buf, size, count, file->fd);
+    file->download->current_file_downloaded += w;
+
+    file->download->downloaded_bytes += w;
+
+    if (file->download->current_file_downloaded > file->download->current_file_size) {
+        size_t diff = file->download->current_file_downloaded - file->download->current_file_size;
+        file->download->current_file_size = file->download->current_file_downloaded;
+        file->download->total_bytes += diff;
+    }
+
+    ui_call_mainthread(uithr_download_update_progress, file->download);
+
+    return w;
+}
+
 static int qthr_download_resource(void *data) {
     DDFile *file = data;
 
+    file->download->current_file_downloaded = 0;
+    file->download->current_file_size = file->size;
+
     FILE *f = fopen(file->to, "wb");
     if (!f) {
         return 0;
     }
+    file->fd = f;
 
     DavResource *res = dav_resource_new(file->download->download_sn, file->path);
-    dav_get_content(res, f, (dav_write_func)fwrite);
+    dav_get_content(res, file, (dav_write_func)ddfile_write);
 
-    file->download->downloaded_bytes += res->contentlength;
+    file->download->downloaded_bytes += ftell(f);
     file->download->downloaded_files++;
 
+    ui_call_mainthread(uithr_download_update_progress, file->download);
+
     dav_resource_free(res);
 
     fclose(f);
@@ -760,6 +792,8 @@
     free(file->path);
     free(file->to);
     free(file);
+
+    return 0;
 }
 
 
@@ -805,10 +839,8 @@
                 continue;
             }
 
-            download->total_directories++;
-
             // update ui
-            //ui_call_mainthread(uithr_download_update_progress, download);
+            ui_call_mainthread(uithr_download_update_progress, download);
             
             char *path = util_concat_path(download->local_path, sub_path);
             int err = sys_mkdir(path);
@@ -832,6 +864,7 @@
             DDFile *file = malloc(sizeof(DDFile));
             file->download = download;
             file->path = strdup(res->path);
+            file->size = res->contentlength;
             if (download->isdirectory) {
                 file->to = util_concat_path(download->local_path, sub_path);
             } else {
@@ -843,16 +876,19 @@
             download->total_bytes += res->contentlength;
 
             // update ui
-            //ui_call_mainthread(uithr_download_update_progress, download);
+            ui_call_mainthread(uithr_download_update_progress, download);
 
             ui_threadpool_job(download->queue, download->ui, qthr_download_resource, file, NULL, NULL);
         } 
     }
 
     cxListDestroy(stack);
+
+    return 0;
 }
 
 static void uithr_download_scan_finished(UiEvent *event, void *data) {
+    DavFileDownload *download = data;
 
 }
 

mercurial