From bd9fe31778919b5cd7c1e23a0e4ce215e2ceef81 Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Wed, 7 Sep 2022 08:52:22 -0600 Subject: [PATCH] tweaks to library & downloader API --- libs/ardour/ardour/library.h | 7 ++++--- libs/ardour/library.cc | 35 +++++++++++++++++++++++++++-------- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/libs/ardour/ardour/library.h b/libs/ardour/ardour/library.h index 93134e3266..9f5fc1afdc 100644 --- a/libs/ardour/ardour/library.h +++ b/libs/ardour/ardour/library.h @@ -15,11 +15,12 @@ namespace ARDOUR { class LibraryDescription { public: - LibraryDescription (std::string const & n, std::string const & d, std::string const & u, std::string const & l, std::string const & td) - : _name (n), _description (d), _url (u), _license (l), _toplevel_dir (td), _installed (false) {} + LibraryDescription (std::string const & n, std::string const & a, std::string const & d, std::string const & u, std::string const & l, std::string const & td) + : _name (n), _author (a), _description (d), _url (u), _license (l), _toplevel_dir (td), _installed (false) {} std::string const & name() const { return _name; } std::string const & description() const { return _description; } + std::string const & author() const { return _author; } std::string const & url() const { return _url; } std::string const & license() const { return _license; } std::string const & toplevel_dir() const { return _toplevel_dir; } @@ -29,6 +30,7 @@ class LibraryDescription private: std::string _name; + std::string _author; std::string _description; std::string _url; std::string _license; @@ -61,7 +63,6 @@ class Downloader { FILE* file; CURL* curl; bool _cancel; - double dsize; /* temporary to match CURL API */ std::atomic _download_size; /* read-only from requestor thread */ std::atomic _downloaded; /* read-only from requestor thread */ std::atomic _status; diff --git a/libs/ardour/library.cc b/libs/ardour/library.cc index 849c1eed51..aed3690806 100644 --- a/libs/ardour/library.cc +++ b/libs/ardour/library.cc @@ -61,11 +61,15 @@ LibraryFetcher::get_descriptions () XMLNode const & root (*tree.root()); for (auto const & node : root.children()) { - string n, d, u, l, td; + string n, d, u, l, td, a; if (!node->get_property (X_("name"), n)) { std::cerr << "no name\n"; continue; } + if (!node->get_property (X_("author"), a)) { + std::cerr << "no author\n"; + continue; + } if (!node->get_property (X_("url"), u)) { std::cerr << "no urln"; continue; @@ -82,7 +86,7 @@ LibraryFetcher::get_descriptions () d = node->content(); - _descriptions.push_back (LibraryDescription (n, d, u, l, td)); + _descriptions.push_back (LibraryDescription (n, a, d, u, l, td)); _descriptions.back().set_installed (installed (_descriptions.back())); std::cerr << "got description for " << _descriptions.back().name() << std::endl; @@ -183,7 +187,7 @@ LibraryFetcher::installed (LibraryDescription const & desc) } static size_t -CurlFILEWrite_CallbackFunc_StdString(void *contents, size_t size, size_t nmemb, Downloader* dl) +CurlWrite_CallbackFunc_Downloader(void *contents, size_t size, size_t nmemb, Downloader* dl) { return dl->write (contents, size, nmemb); } @@ -220,7 +224,7 @@ Downloader::Downloader (string const & u, string const & dir) int Downloader::start () { - file_path = Glib::build_filename (Config->get_clip_library_dir(), destdir, Glib::path_get_basename (url)); + file_path = Glib::build_filename (destdir, Glib::path_get_basename (url)); file = fopen (file_path.c_str(), "w"); if (!file) { @@ -256,6 +260,10 @@ void Downloader::download () { { + /* First curl fetch to get the data size so that we can offer a + * progress meter + */ + curl = curl_easy_init (); if (!curl) { _status = -1; @@ -267,10 +275,21 @@ Downloader::download () curl_easy_setopt(curl, CURLOPT_NOBODY, 1L); curl_easy_setopt(curl, CURLOPT_HEADER, 0L); curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1L); - curl_easy_perform (curl); - curl_easy_getinfo (curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &dsize); - _download_size = dsize; + + CURLcode res = curl_easy_perform (curl); + + if (res == CURLE_OK) { + double dsize; + curl_easy_getinfo (curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &dsize); + _download_size = dsize; + } + curl_easy_cleanup (curl); + + if (res != CURLE_OK ) { + _status = -2; + return; + } } curl = curl_easy_init (); @@ -281,7 +300,7 @@ Downloader::download () curl_easy_setopt (curl, CURLOPT_URL, url.c_str()); curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1L); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlFILEWrite_CallbackFunc_StdString); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWrite_CallbackFunc_Downloader); curl_easy_setopt(curl, CURLOPT_WRITEDATA, this); CURLcode res = curl_easy_perform (curl); curl_easy_cleanup (curl);