From: Michael Tremer Date: Mon, 12 Aug 2024 10:48:44 +0000 (+0000) Subject: httpclient: Register the progress with the client X-Git-Tag: 0.9.30~1217 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=95f35ace56ef3c8c42946cda2d8660809d828e2c;p=pakfire.git httpclient: Register the progress with the client Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/httpclient.c b/src/libpakfire/httpclient.c index 4ec33229a..825fe5d8d 100644 --- a/src/libpakfire/httpclient.c +++ b/src/libpakfire/httpclient.c @@ -51,6 +51,9 @@ struct pakfire_httpclient { // Timer sd_event_source* timer; + // Progress + struct pakfire_progress* progress; + int parallel; // cURL share handle @@ -440,6 +443,23 @@ static int pakfire_httpclient_setup_curl(struct pakfire_httpclient* client) { return 0; } +static int pakfire_httpclient_setup_progress(struct pakfire_httpclient* client) { + int r; + + const int flags = + PAKFIRE_PROGRESS_SHOW_PERCENTAGE | + PAKFIRE_PROGRESS_SHOW_ETA | + PAKFIRE_PROGRESS_SHOW_BYTES_TRANSFERRED | + PAKFIRE_PROGRESS_SHOW_TRANSFER_SPEED; + + // Create a new progress indicator + r = pakfire_progress_create(&client->progress, client->ctx, flags, NULL); + if (r) + return r; + + return 0; +} + static void pakfire_httpclient_free(struct pakfire_httpclient* client) { struct pakfire_xfer_element* x = NULL; @@ -459,6 +479,8 @@ static void pakfire_httpclient_free(struct pakfire_httpclient* client) { pakfire_httpclient_xfer_free(x); } + if (client->progress) + pakfire_progress_unref(client->progress); if (client->share) curl_share_cleanup(client->share); if (client->curl) @@ -510,6 +532,11 @@ int pakfire_httpclient_create(struct pakfire_httpclient** client, struct pakfire if (r) goto ERROR; + // Setup progress + r = pakfire_httpclient_setup_progress(c); + if (r) + goto ERROR; + // Success *client = c; @@ -589,10 +616,6 @@ static unsigned int pakfire_httpclient_total_queued_xfers(struct pakfire_httpcli } int pakfire_httpclient_run(struct pakfire_httpclient* client, const char* title) { - struct pakfire_progress* progress = NULL; - int progress_flags = - PAKFIRE_PROGRESS_SHOW_PERCENTAGE | - PAKFIRE_PROGRESS_SHOW_ETA; int r = 0; struct pakfire_xfer_element* x = NULL; @@ -600,38 +623,21 @@ int pakfire_httpclient_run(struct pakfire_httpclient* client, const char* title) // Fetch the total downloadsize const size_t downloadsize = pakfire_httpclient_total_downloadsize(client); - // If we know the downloadsize, we can show more details - if (downloadsize) { - progress_flags |= - PAKFIRE_PROGRESS_SHOW_BYTES_TRANSFERRED | - PAKFIRE_PROGRESS_SHOW_TRANSFER_SPEED; - } else { - progress_flags |= - PAKFIRE_PROGRESS_SHOW_COUNTER; - } - // Fetch the total number of queued transfers const unsigned int num_queued_xfers = pakfire_httpclient_total_queued_xfers(client); - // Create a new progress indicator - r = pakfire_progress_create(&progress, client->ctx, progress_flags, NULL); + // Set the title + r = pakfire_progress_set_title(client->progress, title); if (r) goto ERROR; - // Set the title - if (title) { - r = pakfire_progress_set_title(progress, title); - if (r) - goto ERROR; - } - // Start the progress - r = pakfire_progress_start(progress, (downloadsize) ? downloadsize : num_queued_xfers); + r = pakfire_progress_start(client->progress, (downloadsize) ? downloadsize : num_queued_xfers); if (r) goto ERROR; // Make sure that we have up to parallel transfers active - r = pakfire_httpclient_start_transfers(client, progress); + r = pakfire_httpclient_start_transfers(client, client->progress); if (r) goto ERROR; @@ -646,7 +652,7 @@ int pakfire_httpclient_run(struct pakfire_httpclient* client, const char* title) } while (client->still_running > 0); // We are finished! - r = pakfire_progress_finish(progress); + r = pakfire_progress_finish(client->progress); if (r) goto ERROR; @@ -660,8 +666,5 @@ ERROR: pakfire_xfer_fail(x->xfer); } - if (progress) - pakfire_progress_unref(progress); - return r; }